I am building a simple connect four game in java, however when I input a move and the board is printed again, it does not update the board. I have tried putting print statements and messing with the board to try to pinpoint where the disconnect is coming from, but cannot seem to find the issue. Any guidance would be appreciated.
Heres my code:
(I have 3 classes, excluding my main)
Board Class:
private final int numberOfrows = 7;
private final int numberOfcolumns = 15;
String[][] board = new String[numberOfrows][numberOfcolumns];;
public Board(){
this.board = display();
}
private String[][] display(){
for (int i = 0;i < board.length; i++)
{ for (int j = 0;j< board[i].length;j++)
{
if (j% 2 == 0) board[i][j] ="|";
else board[i][j] = " ";
if (i == 6) board[i][j]= "-";
}
}
return board;
}
public void printBoard(){
for (int i = 0;i < board.length; i++)
{
for (int j = 0;j< board[i].length; j++)
{
System.out.print(board[i][j]);
}
System.out.println();
}
}
public void updateBoard(int move, String token){
for (int i = 5; i >= 0; i--){
if (board[i][move].equals("")){
board[i][move]=token;
break;
}
}
}
public final void reset()
{
this.board = display();
return;
}
public final boolean checkForwin(){
//horizontal check
for (int i = 0; i < numberOfrows-1; i++){
for (int y = 0; y < numberOfrows; y+=2){
if (!" ".equals(board[i][y+1]) && board[i][y+1].equals(board[i][y+3]) &&
board[i][y+3].equals(board[i][y+5]) &&
board[i][y+5].equals(board[i][y+7])){
return true;}
}
}
//vertical check
for (int i = 1; i < numberOfcolumns-3; i+=2){
for (int y = 0; y < numberOfrows-4; y++){
if (!" ".equals(board[y][i]) && board[y][i].equals(board[y+1][i]) &&
board[y+1][i].equals(board[y+2][i]) &&
board[y+2][i].equals(board[y+3][i])){
return true;}
}
}
//ascending diagonal check
for (int i = 0; i < numberOfrows-4; i++){
for (int y = 7; y < numberOfrows; y+=2 ){
if (!" ".equals(board[i][y]) && board[i][y].equals(board[i+1][y-2]) &&
board[i+1][y-2].equals(board[i+2][y-4]) &&
board[i+2][y-4].equals(board[i+3][y-6])){
return true;}
}
}
//descending diagonal check
for (int i = 0; i < numberOfrows-4; i++){
for (int y = 1; y < numberOfcolumns-6; y+=2){
if (!" ".equals(board[i][y]) && board[i][y].equals(board[i+1][y+2]) &&
board[i+1][y+2].equals(board[i+2][y+4]) &&
board[i+2][y+4].equals(board[i+3][y+6])){
return true;}
}
}
return false;
}
ConnectFour class:
private String token1="R";
private String token2="B";
private boolean switchTurn=false;
private Board board;
private String winningColour;
public ConnectFour(Board board){
this.board = board;
}
public void playGame(HumanPlayer player1, HumanPlayer player2){
board.reset();
board.printBoard();
while (!board.checkForwin()){
if (switchTurn){
board.updateBoard(player1.playerMove(), token1);
switchTurn = false;
board.printBoard();
}
else{
//System.out.println(playmade2);
board.updateBoard(player2.playerMove(), token2);
switchTurn = true;
board.printBoard();
}
}
winningColour = switchTurn ? token1 : token2;
System.out.println("End of game, player in "+winningColour+" has won!");
}
HumanPlayer class:
private int row;
protected int playerMove(){
Scanner move = new Scanner(System.in);
System.out.println("Enter move (0-6):");
row = 2*move.nextInt()+1;
return row;
}
My runner class:
public static void main(String[] args) {
Board board = new Board();
ConnectFour game = new ConnectFour(board);
HumanPlayer player1 = new HumanPlayer();
HumanPlayer player2 = new HumanPlayer();
game.playGame(player1, player2);
}