# A logic / forloop question

• October 8th, 2011, 05:52 PM
Herah
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.
Code Java:

``` /** 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; }```
• October 8th, 2011, 06:02 PM
newbie
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 :)
• October 8th, 2011, 06:15 PM
Herah
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.