samedi 21 septembre 2019

How to check in a 2-Dimensional Array if one element is followed by the same element n number of times

I'm creating this project for my CS class that mimics a Tic Tac Toe Board. It worked fine for the original 3x3 square but now I need to make the program ask the user how big of a board they want it to be. It can only be a 3x3 to 14x14 square in size. I also am asking how many connections or how many X's or O's in a row they want to win the game. The connection can only be equal to or less than the size but not less than 3.

For the condition that checked for horizontal x's or o's in a row originally looked like this,

for(int r = 0; r < 3; r++) {
  for(int c = 0; c < 1; c++) {
    if(board[r][c] == Cell.X && board[r][c + 1] == Cell.X && board[r][c + 2] == Cell.X) {
        return GameStatus.X_WON;
    }
    else if(board[r][c] == Cell.O && board[r][c + 1] == Cell.O && board[r][c + 2] == Cell.O) {
        return GameStatus.O_WON;
    }
  }
}

I have gotten the program to take inputs for the connections and board size called,

int connections;
SuperTicTacToe.getSize();

I wrote this out this,

private GameStatus isWinner() {
  int count = 0;
  for(int r = 0; r < SuperTicTacToe.getSize(); r++) {
    for(int c = 0; c < 1; c++) {
      for(int i = 0; i < connections; i++){
        if(board[r][c + i] == Cell.X ){
           count++;
        }
        if(count == connections){
           return GameStatus.X_WON;
        }
      }
    }
  }
}

but the only thing I cannot seem to do is get the program to somehow check In a row there is connection number of x's or o's there. It only seems to check if there is connection number of x's on the board and not count anything in the last column.

Is there a way to check in a row if an element is equal to the same element in front of it n number of times?

Aucun commentaire:

Enregistrer un commentaire