Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: A logic / forloop question

1. ## A logic / forloop question

This is another portion of the same program referenced earlier. It's sort of like tic tac toe, but with a variable board size. I have already gotten my method for checking to see if anyone has a tic tac toe to work, but currently I have a different method for each board size, and each player, which gets really really long. I know there must be a way to only do it once. I was thinking nested forloops, but it doesn't seem to work with the logic I am using, as the x and y increment together. Some tips would be great. I need this to work for any board size, and check horizontal, vertical and diagonal win possibilities.
```	/** Checks the board to see if the player has scored any points by filling a row, column or diagonal with their pieces. If so,
* player is awarded points for each space filled and pieces are removed from the board. @param gameBoard. @return gameBoard.
*
*/
public static int[][] checkBoard3Player1(int[][]gameBoard)
{
boolean[] rows = new boolean[gameBoard.length];
for(int x = 0; x < gameBoard.length; x ++){
if(gameBoard[x][0] == gameBoard[x][1] && gameBoard[x][0] == gameBoard[x][2] && gameBoard[x][0] != 0)
rows[x] = true;

}
boolean[] cols = new boolean[gameBoard.length];
for(int y = 0; y < gameBoard.length; y++)
{	if(gameBoard[0][y] == gameBoard[1][y] && gameBoard[0][y] == gameBoard[2][y] && gameBoard[0][y] != 0)
cols[y] = true;
}
boolean diagLtoR = false;
if(gameBoard[0][0] == gameBoard[1][1] && gameBoard[0][0] == gameBoard [2][2] && gameBoard[0][0] != 0)
diagLtoR = true;
boolean diagRtoL = false;
if(gameBoard[0][2] == gameBoard[1][1] && gameBoard[0][2] == gameBoard [2][0] && gameBoard[0][2] != 0)
diagRtoL = true;

for(int x = 0; x < rows.length; x ++)
{
if(rows[x])
{
for(int gbi = 0; gbi < gameBoard.length; gbi ++)
{
gameBoard[x][gbi] = 0;
player1Score++;
}
}
}
for(int y = 0; y < cols.length; y ++)
{
if(cols[y])
{
for(int gbi = 0; gbi < gameBoard.length; gbi ++)
{
gameBoard[gbi][y] = 0;
player1Score++;
}
}
}
if(diagLtoR)
{
gameBoard[0][0] = 0;
gameBoard[1][1] = 0;
gameBoard[2][2] = 0;
player1Score++;
}
if(diagRtoL)
{
gameBoard[0][2] = 0;
gameBoard[1][1] = 0;
gameBoard[2][0] = 0;
player1Score++;
}

return gameBoard;
}```

2. ## Re: A logic / forloop question

As It's your design process, It's kind of hard to comment, but I'd recommend you go with a more object oriented approach, by splitting the logic into multiple classes, such as 1 that defines a player, his score, how to check his score and say another class that defines the board.

Obviously that was a very vague answer, but where others may easily be able to interpret what you're doing, I'm not quite with you

3. ## Re: A logic / forloop question

Thanks Newbie, you're right about the object design. My original game was all in one class, which was a bit of a monster.

I have seperated it into appropriate classes now, and want to move this method into the board class as well. I will modify it for that.

All I am really asking is how to check each portion of the board without having to have a different method for each board size.

for example,
3x3 board
5x5 board

want logic to check both for having tic tac toe win, without having 2 differnt methods.