I'm trying to create a noughts and crosses game, that runs without the need for any GUI components, with the following code. (I've marked in bold where the errors occur)
(Some pointers: I've used 1 and 2 to denote the players rather than x and o as I could then denote the turn number in conjunction with the player who's go it is)
package noughts.and.crosses; /** * * @author Alex */ import java.util.Scanner; public class NoughtsAndCrosses { public static class GameBoard { private int[][] theBoard; private int currentTurn; public void GameBoard(int firstTurn){ currentTurn = firstTurn; theBoard = new int[3][3]; //Initialise board for (int j = 0; j < 3; j++) { for (int i = 0; i < 3; i++) { theBoard[i][j] = 0; } } } public void showBoard() { for (int j = 0; j < 3; j++){ System.out.println(theBoard[0][j] + "/n"); } for (int j = 0; j < 3; j++){ System.out.println(theBoard[1][j] + "/n"); } for (int j = 0; j < 3; j++){ System.out.println(theBoard[2][j] + "/n"); } } //Define two players as 1 and 2 //1 = x, 2 = o public void nextTurn(){ if (currentTurn == 1){ currentTurn = 2; } else { currentTurn = 1; } } public int getCurrentTurn(){ return currentTurn; } public boolean isComplete(){ boolean complete = true; for (int j = 0; j < 3; j++) { for (int i = 0; i < 3; i++) { theBoard[i][j] = 0; complete = false; } } return complete; } public void makeMove(int row, int col){ int move = theBoard[row][col]; if (theBoard[row][col] != 0){ System.out.println("That space is already occupied, make another selection."); } else { theBoard[row][col] = currentTurn; } } public int isWinner(){ int winner = 0; //Check for a winner for (int i = 0; i < 3; i++){ if (theBoard[i][0] == theBoard[i][1] && theBoard[i][0] == theBoard[i][2]){ if (theBoard[i][0] != 0){ winner = theBoard[i][0]; } } } for (int j = 0; j < 3; j++){ if (theBoard[0][j] == theBoard[1][j] && theBoard[0][j] == theBoard[2][j]){ if (theBoard[0][j] != 0){ winner = theBoard[0][j]; } } } if (theBoard[3][3] == theBoard[2][2] && theBoard[3][3] == theBoard[1][1]){ if (theBoard[3][3] !=0){ winner = theBoard[3][3]; } } if (theBoard[3][1] == theBoard[2][2] && theBoard[3][1] == theBoard[1][3]){ if (theBoard[3][1] != 0){ winner = theBoard[3][1]; } } return winner; } } public static void main(String[] args) { GameBoard board = new GameBoard(1); Scanner input = new Scanner(System.in); int turn; int inputRow; int inputCol; int number; while (board.isWinner() == 0 && board.isComplete() = false){ board.showBoard(); turn = board.getCurrentTurn(); System.out.println("Player" + turn + "it's your move."); System.out.print("Enter a row number 1-3"); number = input.nextInt(); inputRow = number - 1; System.out.print("Enter a column number 1-3"); number = input.nextInt(); inputCol = number - 1; board.makeMove(inputRow, inputCol); board.nextTurn(); } turn = board.getCurrentTurn(); System.out.println("Player" + turn + "make a valid move."); System.out.println("To move enter a row and then a column number"); System.out.print("Enter a row number 1-3"); number = input.nextInt(); inputRow = number - 1; System.out.print("Enter a column number 1-3"); number = input.nextInt(); inputCol = number - 1; board.makeMove(inputRow, inputCol); System.out.println("The winner is player" + board.isWinner()); } }
The command windows displays, however, the following errors:
NoughtsAndCrosses.java:100: error: constructor GameBoard in class GameBoard cannot be applied to given types; GameBoard board = new GameBoard(1); ^ required: no arguments found: int reason: actual and formal argument lists differ in length NoughtsAndCrosses.java:107: error: unexpected type while (board.isWinner() == 0 && board.isComplete() = false){ ^ required: variable found: value 2 errors
However for the first error, I believe that I need an integer within the parentheses since otherwise the game won't know which turn to start on? I also have defined the constructor "GameBoard(int firstTurn)" and so don't understand why it asking for no arguments.
The second error I am also none the wiser since as it is not clear to me what it is asking me to change.
I'm very much a beginner to Java so I apologise if I come across as not knowing much at all.