I am trying to save the method outOfBounds which is called inside the lengthOfColor method more than once to a local variable, so that less processing power is used. I provided the lengthOfColor method in which I want to store the variable, and I also provided the outOfBounds method. As you can see the outOfBounds method is a boolean and I am not sure how to store it with integer parameters.
private Integer[] lengthOfColor(int col, boolean color, int pattern, int row) {
int x = 0;
int y = 0;
if (pattern == 1) {
// vertical pattern
y = 1;
} else if (pattern == 2) {
// horizontal pattern
x = 1;
} else if (pattern == 3) {
// diagonal slope left pattern
x = 1;
y = 1;
} else {
// diagonal slope right pattern
x = 1;
y = -1;
}
// length = how many neighbor slots are of same color
// possible equals number of slots, that you can play off of.
// whichSide = left or right if horizontal and top or bottom if vertical.
int length = 0;
int possible = 0;
Integer[] whichSide = new Integer[]{1, -1};
for (int side : whichSide) {
int i = 1;
boolean complete = false;
//while complete is false continue the loop
while (!complete) {
//mainX == horizontal pattern distance
//mainY == vertical pattern distance
int mainX = x * i * side;
int mainY = y * i * side;
//if still inbounds and if the same slot is filled and it matches the color, increment length
if (!outOfBounds(col, mainX, mainY, row) && getIsFilled(col, mainX, mainY, row) &&
checkColor(col, mainX, mainY, row) == color)
{
length++;
}
//if still inbounds and if the same slot is empty, increment possible number of spots and change complete to true
else if (!outOfBounds(col, mainX, mainY, row) && !getIsFilled(col, mainX, mainY, row) &&
getLowestEmptyIndex(myGame.getColumn(col + mainX)) == getLowestEmptyIndex(myGame.getColumn(col)) + mainY - row)
{
possible++;
complete = true;
}
//finish the statement to avoid a infinite loop if neither conditions are met.
else
{
complete = true;
}
// If not complete, then check one slot further.
i = i + 1;
}
}
return new Integer[] {length, possible};
}
private boolean outOfBounds(int col, int x , int y, int row)
{
int currentX = col;
int currentY = getLowestEmptyIndex(myGame.getColumn(col)) - row;
return currentX + x >= myGame.getColumnCount() || currentY + y >= myGame.getRowCount() || currentX + x < 0 || currentY + y < 0;
}
I see that mainX and mainY change values so there isn't any real optimization that can be done outside of the for and while loop besides creating a boolean value that holds the result of outOfBounds before the if check is called which would reduce the number of operations you need to do. To be honest, the optimization is so insignificant that it wouldn't really matter but would be good coding practice I suppose (JIT might optimize for you as well depending on your code). More importantly the method reduces the extra lines of code you need to type and does not necessarily mean that there is less computing.
So something like this before any outOfBounds call but inside the while loop,
boolean outOfBounds = outOfBounds(col, mainX, mainY, row);
and change your current if(!outOfBounds(col, mainX, mainY, row) && ....) into if (!outOfBounds && ...)
Also the #1 rule to optimization is to not optimize until you are done with your project and notice a significant performance dip. In which case you would start with the biggest bottleneck until the optimal performance is gained. Of course this does not mean coding in an incorrect way which would of course create unnecessary performance losses. In those cases it would also be wise to consider whether or not you are looking at the problem the right way rather than micro-optimizing.
Here's a snippet of what I would do to micro-optimize the code shown.
private Integer[] lengthOfColor(int col, boolean color, int pattern, int row) { // consider changing Integer[] into
// int[] if you don't need a boxed integer. It will increase performance
int x = 0;
int y = 0;
// length = how many neighbor slots are of same color
// possible equals number of slots, that you can play off of.
// whichSide = left or right if horizontal and top or bottom if vertical.
int length = 0;
int possible = 0;
switch (pattern) { // switch may be a tad faster but insignificant. More importantly it provides clarity.
case 1:
y = 1;
break;
case 2:
x = 1;
break;
case 3:
x = 1;
y = 1;
break;
default:
x = 1;
y = -1;
break;
}
//int[] whichSide = new int[]{1, -1}; // changed to int[] because you don't need a boxed primitive from what is
// shown
// nevermind, this line isn't needed and you will be able to avoid an instantiation.
for (int i = 1; i != -3; i-=2) {
int count = 1;
int mainX; // bring this to a higher scope. (honestly this is micro optimization but a habit of mine if this is
// can be considered in scope)
int mainY;
boolean outOfBounds = false;
//boolean complete = false; // removed as its unnecessary to break out of the while loop.
//while complete is false continue the loop
while (true) {
//mainX == horizontal pattern distance
//mainY == vertical pattern distance
mainX = x * count * i;
mainY = y * count * i;
outOfBounds = outOfBounds(col, mainX, mainY, row);
//if still inbounds and if the same slot is filled and it matches the color, increment length
if (!outOfBounds && getIsFilled(col, mainX, mainY, row) &&
checkColor(col, mainX, mainY, row) == color) {
length++;
}
//if still inbounds and if the same slot is empty, increment possible number of spots and change complete to
// true
else if (!outOfBounds && !getIsFilled(col, mainX, mainY, row) &&
getLowestEmptyIndex(myGame.getColumn(col + mainX)) == getLowestEmptyIndex(myGame.getColumn(col)) + mainY -
row) {
possible++;
break;
}
//finish the statement to avoid a infinite loop if neither conditions are met.
else {
break;
}
// If not complete, then check one slot further.
count++;
}
}
return new Integer[]{length, possible}; // once again consider whether or not you need a boxed integer
}
private boolean outOfBounds(int col, int x, int y, int row) {
//int currentX = col; this is an unnecessary line
int currentY = getLowestEmptyIndex(myGame.getColumn(col)) - row;
return col + x >= myGame.getColumnCount() || currentY + y >= myGame.getRowCount() || col + x < 0 ||
currentY + y < 0;
}
Related
I wrote algortihm tht checks just part of tic tac toe game board, let's say its 10x10 size.
There's no need to iterate throught whole board every time, just around player move index + game diffuculty in each direction.
Here is my impl:
private boolean checkHorizontalWin(String gameBoard, int gameSize, int gameDifficulty, int moveIdx) {
// CHECK HORIZONTAL WIN
int moveX = moveIdx / gameSize; // 15/10=1
int moveY = moveIdx % gameSize; // 15%10=5
int startX = clamp(moveX - gameDifficulty, 0, gameSize);
int startY = clamp(moveY - gameDifficulty, 0, gameSize);
int endX = clamp(moveX + gameDifficulty, 0, gameSize);
int endY = clamp(moveY + gameDifficulty, 0, gameSize);
for (int row = startY; row < endY; row++) {
char candidate = getPawnAtCoords(gameBoard, gameSize, row, 0);
int counter = 1;
for (int column = startX; column < endX; column++) {
char pawn = getPawnAtCoords(gameBoard, gameSize, row, column);
if ((pawn == candidate) && (pawn != '-')) {
counter++;
} else {
counter = 1;
candidate = pawn;
}
if (counter == gameDifficulty) {
return true;
}
}
}
return false;
}
private int clamp(int val, int min, int max) {
return Math.max(min, Math.min(max, val));
}
private char getPawnAtCoords(String board, int gameSize, int row, int column) {
int index = row * gameSize + column; // 1 * 10 + 5
return board.charAt(index);
}
Algorithm is not working as it's should when player move index is placed on board corner.
Anyone gat an idea?
The problem is that with candidate = getPawnAtCoords(gameBoard, gameSize, row, 0) you read a cell that might not be in the region you want to inspect: the column 0 might not be anywhere near. Yet, you count that cell. So for instance, with a 3-in-a-row game, when you scan the first row where a move was made in column 10, then that row might look like this:
X------XX-X----
^ last move
^ inner loop starts here, but count is already 1 before it starts
^ this X is counted erroneously
Your initialisation of candidate will count the left X and then will find two more X starting at index 7 (i.e. 10 - 3), and conclude there is a 3-in-a-row, but this is a false positive.
You can solve this in many ways. For instance, this will do the trick:
char candidate = '-';
int counter = 0;
Secondly, your encoding of an index (from row/column) is different from the decoding of an index (into row/column). In getPawnAtCoords the column is the smaller unit, and the row the bigger one (it is multiplied), yet in checkHorizontalWin you extract moveX as the bigger unit (as you divide), which is later used for the loop variable column.
You can fix this by changing the initialisation of moveX and moveY to:
int moveX = moveIdx % gameSize;
int moveY = moveIdx / gameSize;
Finally, this is an overly complex algorithm. You already know what the last move was, as you can read gameBoard[moveX] and it is that character you want to check with, nothing else. So the variable candidate could be set to it -- only once. Then you don't need that pawn == '-' check anymore and only need to reset the counter to 0 when you find a character that is different from candidate.
Moreover, for horizontal wins it is not necessary to look at any other row than the row of the last move.
Algorithm is required for Finding the group of 1s in a matrix, but the group of 1s should contain only vertical entry
This is more in the brain-teaser category.
For small matrices, we just access it column by column.
Assuming input is a 2 dimension array of integer, and output is a list of the following class:
class GroupIdentifier {
int col;
int rowStart;
int colStart;
/* the corresponding getter/setter/constructor etc */
}
Here is the function you are looking for:
int FIRST_ONE = 2;
int IN_GROUP = 1;
int OUT_GROUP = 0;
public List<GroupIdentifier> findVerticalGroupsOf1 (int[][] a,
int numRow, int numCol) {
List<GroupIdentifier> answer = new ArrayList<GroupIdentifier>();
for (col = 0; col < numCol; col = col + 1) {
// let's create the tempArr holding the current col
int[] tempArr = new int[numRow];
for (row = 0; row < numRow; row = row + 1 =) {
tempArr[row] = a[row][col];
}
// let's find the groups
// first, init the state
int state = (tempArr[0] == 1) ? FIRST_ONE : OUT_GROUP;
int start = (state == FIRST_ONE) ? 0 : -1;
// I see this problem as a simple state-machine
// We have three states: FIRST_ONE encountered (0 to 1),
// still tracking the 1s IN_GROUP ... (1 to 1) and
// get OUT_GROUP (1 to 0).
// the switch case in the following loop does exactly that.
// So, whenever we get an OUT_GROUP event, we get an answer.
for (int i = 1; i < numRow; i = i + 1) { // edit: changed to numRow as
// it was a typo error
switch (state) :
case FIRST_ONE :
if (tempArr[i] == 0)
state = OUT_GROUP;
else
state = IN_GROUP;
break;
case IN_GROUP :
if (tempArr[i] == 0) {
GroupIdentifier gi = new GroupIdentifier (col, start, i - 1);
answer.add(gi);
}
break;
case OUT_GROUP :
if (tempArr[i] == 1) {
start = i;
state = FIRST_ONE;
}
break;
}
}
// since this question looks like homework,
// i will leave out the boundary case handling
// here. it's not that hard; just copy/paste the
// switch statement and fondle around.
return answer;
}
As a computer scientist myself, I think of ways to optimize it.
For larger matrices, what I would do is to precompute the all combinations of tempArr and store them as Integer --> List map. Then, I will traverse the columns without computing again.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So I tried this program which was supposed to find out what position these doors are in. They start out closed, and then you toggle every door. The next time you toggle every second, then every third, fourth, so on. I got the coding right but it seems really inefficient. If I tried this with larger numbers, then the program would take too long. Is there any way to do this more efficiently? Here is my coding:
public class doors
{
public static void main(String [] args)
{
int x, y = 1;
boolean [] doors = new boolean[100];
for (int a = 0; a < doors.length; a++)
doors[a] = false; //false means closed
do
{
for (x = 0; x < doors.length - 1; x++)
{
if ((x+1) % y == 0)
{
if (doors[x] == true)
doors[x] = false;
else if (doors[x] == false)
doors[x] = true;
}
}
y++;
}while (y <= doors.length);
for (boolean b : doors)
System.out.println(b);
}
}
// Thanks!!!
Let's simplify the code one step at a time.
1) When an array is allocated, all elements are initialized to their default value, which means 0, false, or null. In your case that means false, so no need to loop through and do that.
// before
boolean [] doors = new boolean[100];
for (int a = 0; a < doors.length; a++)
doors[a] = false; //false means closed
// after
boolean[] doors = new boolean[100];
2a) Since the array is not a zero-length array, the do-while loop might as well be a normal while loop (it will execute at least once).
2b) As a normal while loop, with an initializer right before the loop, and the increment at the end of the loop, it is the same as a for loop.
2c) Since y is not used after the loop, it can be declared in the loop.
// before
int x, y = 1;
do
{
//code
y++;
}while (y <= doors.length);
// after
int x;
for (int y = 1; y <= doors.length; y++)
{
//code
}
3a) doors[x] == true is the same as doors[x]. doors[x] == false is the same as ! doors[x].
3b) if (x) {} else if (! x) {} is the same as if (x) {} else {}. The test after else is redundant.
3c) if (x) x = false; else x = true; is the same as x = ! x;.
3d) You can skip the double evaluation by doing ^= true (XOR).
// before
if (doors[x] == true)
doors[x] = false;
else if (doors[x] == false)
doors[x] = true;
// after
doors[x] = ! doors[x];
// skip double evaluation of index lookup
doors[x] ^= true;
4a) (x+1) % y == 0 will only be true for x values of y-1, 2*y-1, 3*y-1, and so on, so make loop start at y-1 and increment by y, eliminating the need for the if statement.
4c) Since x is not used after the loop, it can be declared in the loop.
// before
int x;
for (x = 0; x < doors.length - 1; x++)
{
if ((x+1) % y == 0)
{
//code
}
}
// after
for (int x = y - 1; x < doors.length - 1; x += y)
{
//code
}
Result so far (after also removing unnecessary braces)
boolean[] doors = new boolean[100];
for (int y = 1; y <= doors.length; y++)
for (int x = y - 1; x < doors.length - 1; x += y)
doors[x] ^= true;
for (boolean b : doors)
System.out.println(b);
5a) Limiting x to < doors.length - 1 means that last value of the array will never be used/updated. Drop the - 1.
// before
for (int x = y - 1; x < doors.length - 1; x += y)
// after
for (int x = y - 1; x < doors.length; x += y)
6a) Printing the array with one boolean true/false value per lines is not very human readable. Print is as a binary number on one line.
// before
for (boolean b : doors)
System.out.println(b);
// after
for (boolean b : doors)
System.out.print(b ? '1' : '0');
System.out.println();
7a) Move the printing inside the outer loop, and you'll see an interesting pattern.
boolean[] doors = new boolean[100];
for (int y = 1; y <= doors.length; y++) {
for (int x = y - 1; x < doors.length; x += y)
doors[x] ^= true;
for (boolean b : doors)
System.out.print(b ? '1' : '0');
System.out.println();
}
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
1010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010
1000111000111000111000111000111000111000111000111000111000111000111000111000111000111000111000111000
1001111100101001111100101001111100101001111100101001111100101001111100101001111100101001111100101001
1001011101101011111000100001101100001000111110101101110100111001011101101011111000100001101100001000
1001001101111011101000110001111100011000101110111101100100101001001101111011101000110001111100011000
1001000101111111101010110000111100111000111110110101100000101011001100111011001000100001110100011100
1001000001111110101010100000111000111001111110100101100100101010001100101011001100100000110100001100
1001000011111110111010100010111000101001111100100101110100101000001100111011001110100000100100001110
1001000010111110111110100010101000101000111100100001110100111000001101111011001010100000110100001111
1001000010011110111111100010101010101000111000100001111100111000011101111011101010100001110100001101
1001000010001110111111110010101010111000111000110001111100101000011101101011101010110001110100011101
1001000010000110111111110110101010111010111000110000111100101000111101101011111010110001111100011101
1001000010000010111111110111101010111010101000110000111000101000111100101011111010100001111100011001
1001000010000000111111110111111010111010101010110000111000111000111100101001111010100001101100011001
1001000010000001111111110111111110111010101010100000111000111001111100101001111110100001101100001001
1001000010000001011111110111111111111010101010100010111000111001111000101001111110101001101100001001
1001000010000001001111110111111111101010101010100010101000111001111000111001111110101001111100001001
1001000010000001000111110111111111101110101010100010101010111001111000111000111110101001111100101001
1001000010000001000011110111111111101111101010100010101010101001111000111000111010101001111100101000
1001000010000001000001110111111111101111111010100010101010101011111000111000111010111001111100101000
1001000010000001000000110111111111101111111110100010101010101011101000111000111010111000111100101000
1001000010000001000000010111111111101111111111100010101010101011101010111000111010111000111000101000
1001000010000001000000000111111111101111111111110010101010101011101010101000111010111000111000111000
1001000010000001000000001111111111101111111111110110101010101011101010101010111010111000111000111001
1001000010000001000000001011111111101111111111110111101010101011101010101010101010111000111000111001
1001000010000001000000001001111111101111111111110111111010101011101010101010101000111000111000111001
1001000010000001000000001000111111101111111111110111111110101011101010101010101000101000111000111001
1001000010000001000000001000011111101111111111110111111111101011101010101010101000101010111000111001
1001000010000001000000001000001111101111111111110111111111111011101010101010101000101010101000111001
1001000010000001000000001000000111101111111111110111111111111111101010101010101000101010101010111001
1001000010000001000000001000000011101111111111110111111111111110101010101010101000101010101010101001
1001000010000001000000001000000001101111111111110111111111111110111010101010101000101010101010101011
1001000010000001000000001000000000101111111111110111111111111110111110101010101000101010101010101011
1001000010000001000000001000000000001111111111110111111111111110111111101010101000101010101010101011
1001000010000001000000001000000000011111111111110111111111111110111111111010101000101010101010101011
1001000010000001000000001000000000010111111111110111111111111110111111111110101000101010101010101011
1001000010000001000000001000000000010011111111110111111111111110111111111111101000101010101010101011
1001000010000001000000001000000000010001111111110111111111111110111111111111111000101010101010101011
1001000010000001000000001000000000010000111111110111111111111110111111111111111100101010101010101011
1001000010000001000000001000000000010000011111110111111111111110111111111111111101101010101010101011
1001000010000001000000001000000000010000001111110111111111111110111111111111111101111010101010101011
1001000010000001000000001000000000010000000111110111111111111110111111111111111101111110101010101011
1001000010000001000000001000000000010000000011110111111111111110111111111111111101111111101010101011
1001000010000001000000001000000000010000000001110111111111111110111111111111111101111111111010101011
1001000010000001000000001000000000010000000000110111111111111110111111111111111101111111111110101011
1001000010000001000000001000000000010000000000010111111111111110111111111111111101111111111111101011
1001000010000001000000001000000000010000000000000111111111111110111111111111111101111111111111111011
1001000010000001000000001000000000010000000000001111111111111110111111111111111101111111111111111111
1001000010000001000000001000000000010000000000001011111111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001001111111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000111111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000011111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000001111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000011111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000001111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000011110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000001110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000010111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000000111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001011111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001001111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000011111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000001111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000011111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000001111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000011111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000001111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000011101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000001101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000001111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000011111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010011111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010001111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000011111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000001111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000011111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000001111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000011111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000001111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000011110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000001110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000000110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000000010
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000000000
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000000001
Now, when you look at the pattern of the result (last line), you'll see that your code could be reduced to (very fast):
boolean[] doors = new boolean[100];
for (int idx = 0, incr = 1; idx < doors.length; idx += (incr += 2))
doors[idx] = true;
//print here
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000000001
First, the default value in your doors array is false (but you could use Arrays.fill to fill it if you needed to). Next you only need to increment your loop by y. Then you can toggle with doors[x] = !doors[x];. Something like,
int y = 1;
boolean[] doors = new boolean[100];
// Arrays.fill(doors, false);
do {
for (int x = y - 1; x < doors.length; x += y) {
doors[x] = !doors[x];
}
y++;
} while (y <= doors.length);
System.out.println(Arrays.toString(doors));
or using nested for loops like
boolean[] doors = new boolean[100];
// Arrays.fill(doors, false);
for (int y = 1; y < doors.length; y++) {
for (int x = y - 1; x <= doors.length; x += y) {
doors[x] = !doors[x];
}
}
System.out.println(Arrays.toString(doors));
As the guys have mentioned above there is a bunch of things you could do to simplify the code. By doing that you will start to better see the algorithms being used and therefore see where they can be improved.
With code like this, simplifying things like if statements is usually more about clarifying code than improving performance. The reason I say that is compilers these days and very good as optimising code and whilst a few instructions may be dropped the performance difference is likely to be minimal.
Where you are more likely to gain noticeable improvement is where there are loops. Reducing the number of iterations in a loop will often have a dramatic effect on overall time. To that end I can see two possible places you can improve things.
Firstly the initial setup loop may not be required. I haven't checked, but the array of doors may be false by default, so looping through to initialise could possibly be dropped.
Secondly, the inner loop increments by 1 and tests if that door matches y. You can short circuit a lot of this by simply incrementing the inner loop by y. That way you will always be on a door you want to flip. Which means you can drop the test for the correct door, and just flip it. As y increases, the number of iterations of the x loop would decrease. Meaning that it would speed up each time y increased.
Finally when I see code like this I always recommend one thing. Always put brackets on your if statements. Subtle bugs are easy to introduce when brackets are not used.
instead of using for statement , use foreach and switch instead
example
for(boolean i:doors){
//do something
}
I've been researching "dead code" and "unreachable code" for awhile now and I still can't seem to figure out what's going on with this problem in my program. This is a snippet of what I have; the method "gameEnd()" which checks for winners in Tic Tac Toe:
private boolean gameEnd() {
// Setting local variables
int x = xMouseSquare;
int y = yMouseSquare;
int[][] g = gameBoard;
int c = CPU;
int h = HUMAN;
// Checking for a winner
/* Checking columns (xMouseSquare)
* Enter the y value, the vertical value, first; then x, the horizontal value, second
*/
// Set y equal to 0 and then add 1
for (y = 0; y < 3; y++) {
// Set CPU c equal to 0
c = 0;
// Set x equal to 0 and then add 1
for (x = 0; x < 3; x++) {
// Add CPU's value to the game board
c += g[x][y];
// If statement returning the absolute value of CPU
if (Math.abs(c) == 3) {
// If these values are correct, return true; the game ends with CPU win horizontally
return true;
}
}
}
// If not, return false; game continues until all marks are filled
return false;
// Set y equal to 0 and then add 1
for (y = 0; y < 3; y++) {
// This time, however, set HUMAN h equal to 0
h = 0;
// Set x equal to 0 and then add 1
for (x = 0; x < 3; x++) {
// Then add HUMAN's value to the game board
h += g[x][y];
// If statement returning the absolute value of HUMAN
if (Math.abs(h) == -3) {
// If these values are correct, return true; the game ends with HUMAN win horizontally
return true;
}
}
}
// If not, return false; game continues until all marks are filled
return false;
{
/* Checking rows (yMouseSquare)
* Enter the x value, the horizontal value, first; then y, the vertical value, second
*/
// Set x equal to 0 and then add 1
for (x = 0; x < 3; x++) {
// Set CPU equal to 0
c = 0;
// Set y equal to 0 and then add 1
for (y = 0; y < 3; y++) {
// Add CPU's value to the game board, but with y and then x
c += g[y][x];
// If statement returning the absolute value of CPU
if (Math.abs(c) == 3) {
// If these values are correct, return true; the game ends with CPU win vertically
return true;
}
}
}
// If not, return false; game continues until all marks are filled
return false;
{
// Set x equal to 0 and then add 1
for (x = 0; x < 3; x++) {
// This time, however, set HUMAN h equal to 0
h = 0;
// Set y equal to 0 and then add 1
for (y = 0; y < 3; y++) {
// Then add HUMAN's value to the game board
h += g[x][y];
// If statement returning the absolute value of HUMAN
if (Math.abs(h) == -3) {
// If these values are correct, return true; the game ends with CPU win vertically
return true;
}
}
}
// If not, return false; game continues until all marks are filled
return false;
}
}
}
} // error on this bracket; but when I remove it, some of the code above becomes unreachable. Can anyone point to what I'm doing wrong?
If it was indented properly, I think it would show that the first occurrence of 'return false' will always be executed if the first instance of 'return true' was not hit, hence all remaining code is never reached.
Here is your problem right here
// If not, return false; game continues until all marks are filled
return false; <-- code exits here, everything below will not run.
{
// Set x equal to 0 and then add 1
for (x = 0; x < 3; x++) {
...
I'm trying to make the game Tetris in java.
I've gotten it to the point where:
a new block is generated when it hits the floor or its y+1 is not null (meaning there's another block under it)
public void collisionCheck(int x, int y) {
if (activetile.getY() == this.height-2 || getTileAt(x, y+1) != null) {
activetile = new Tile(this, 0, 0);
}
}
A row clears when the bottom row is full of non-null values, or the Tetris pieces (for y = 4 (the floor), loop through x till x = 4 and check if all non-null)
public void checkBottomFull(int x, int y) {
while (getTileAt(x,y) != null) {
say("(" + x + ", " + y +")");
if (x == 3) {
say("row is full");
//replace full row with tiles from above
for (int i = 0; i < 4; i++) {
for (int j = 5; j > 0; j--) {
grid[j][i] = getTileAt(i,j-1);
grid[j-1][i] = null;
}
}
break;
}
x++;
}
}
Right now, I'm using keys to move the block:
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_DOWN) {
activetile.setLocation(activetile.getX(), activetile.getY()+1);
System.out.println("coordinates: " + activetile.getX() + ", " + activetile.getY());
collisionCheck(activetile.getX(),activetile.getY());
checkBottomFull(0,4);
repaint();
}
}
There's two issues I'm having:
1) In the picture you'll notice I've dropped the block all the way to the floor... and the row cleared. After it's cleared, it will generate a block to the top left (x=0, y=1) which I have no control over.
2) On the floor there seems to be a red line... which I'm assuming is a row of blocks hidden by the JFrame... I'm not sure why that's there.
FYI: If you're wondering why grid[j][i] has the rows and columns flipped (aka, why it's not grid[i][j]) is because I instantiated it as grid = new Tile[height][width];
Any thoughts?
Thanks!
It is hard to say what is wrong without actually debugging your app.
But maybe try this one:
public void checkBottomFull(int x, int y) {
while (getTileAt(x,y) != null) {
say("(" + x + ", " + y +")");
if (x == 3) {
say("row is full");
//replace full row with tiles from above
for (int i = 0; i < 4; i++) {
for (int j = 4; j >= 0; j--) {
grid[j][i] = getTileAt(i,j-1);
grid[j-1][i] = null;
}
}
break;
}
x++;
}
}
You have 5 rows (indexed from 0 to 4) and 4 columns (indexed from 0 to 3).
What values of height and width do you pass to:
grid = new Tile[height][width];
Because from what I see you should do something like that:
grid = new Tile[5][4];
Bah,
Turns out in the key event, I needed to check if the bottom was full before checking if there is a collision.
I guess what was happening is, when I was checking collisionCheck(activetile.getX(),activetile.getY()); before checkBottomFull(0,4);, when the bottom was full, it would clear the row and set the current row equal to the row above it: grid[j][i] = getTileAt(i,j-1);, the problem was that collisionCheck was generating a new piece and the that newly generated piece was getting cleared and replaced by checkBottomFull.
Putting the collision check after the checkBottomFull ensures that the newly generated piece won't be replaced if bottom is full.