I have a college assignment in which I have to make a Tic Tac Toe game work. Some of the code has been provided by my instructor like method signatures etc. Now the rules for this particular project state that a player wins if they get n consecutive spaces on an nxn grid. So 3 for 3x3, 5 for 5x5, etc. Now I got it to work for a 3x3 grid by kind of hard coding it.
Please have a look at how it is done in my code. Now I check if 3 cells contain the same player (X or O). But I don't know how to generalize my method as n increases.
package tictactoe;
/**
* An implementation of the TicTacToeGame interface.
*
* ATTENTION STUDENTS: You MUST use exactly this class specification. Do not
* rename the class, and do not remove the "implements TicTacToeGame", or
* you will receive no credit for your submission.
*/
public class TicTacToe implements TicTacToeGame {
int size;
String whoseTurn = "X";//X,O,""
String[] board;
int count;
/**
* Constructs a new instance, implementing the TicTacToeGame interface.
*
* ATTENTION STUDENTS: You MUST use exactly this signature for your constructor.
* Do not rename the class and do not change the argument, or you will receive
* no credit for your submission.
*
* @param n the length and width of the board; n >= 3
*/
public TicTacToe(int n) {
size = n*n;
board = new String[size];
resetBoard();
count = 0;
}
@Override
public int getN() {
return size;
}
@Override
public String toString() {
String result = "";
int ctr = 1;
for(int i = 0; i < size; i++){
result += board[i];
if(ctr % Math.sqrt(size) == 0)
result += '\n';
ctr++;
}
return result;
}
@Override
public String getWinner() {
int len = (int)(Math.sqrt(size));
int diag = len - 1;
int antiDiag = len + 1;
for(int i = 0; i < (size - len); i++){
if((board[i].equals("X")) && (board[i+1].equals("X")) && (board[i+2].equals("X")))
return "X";
if((board[i].equals("O")) && (board[i+1].equals("O")) && (board[i+2].equals("O")))
return "O";
if((board[i].equals("X")) && (board[i+len].equals("X")) && (board[i+2*len].equals("X")))
return "X";
if((board[i].equals("O")) && (board[i+len].equals("O")) && (board[i+2*len].equals("O")))
return "O";
if((board[diag].equals("X")) && (board[2*diag].equals("X")) && (board[3*diag].equals("X")))
return "X";
if((board[diag].equals("O")) && (board[2*diag].equals("O")) && (board[3*diag].equals("O")))
return "O";
if((board[antiDiag].equals("X")) && (board[2*antiDiag].equals("X")) && (board[3*antiDiag].equals("X")))
return "X";
if((board[antiDiag].equals("O")) && (board[2*antiDiag].equals("O")) && (board[3*antiDiag].equals("O")))
return "O";
}
return "";
}
@Override
public String getCurrentPlayer() {
while(count < size)
return whoseTurn;
return "";
}
@Override
public boolean isValidMove(int space) {
if((count<(size)) && (board[space].equals(" ")))
return true;
whoseTurn = "";
return false;
}
@Override
public void move(int space) throws IllegalArgumentException {
board[space] = this.getCurrentPlayer();
if(whoseTurn.equals("X"))
whoseTurn = "O";
else
whoseTurn = "X";
count++;
}
private void resetBoard(){
for(int i = 0; i < size; i++)
board[i] = " ";
}
}
board
as a one-dimensionalString
array? Are you required to setsize
equal ton*n
? \$\endgroup\$