Noughts and Crosses/Tic Tac Toe code problem
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)
Code :
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:
Code :
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.
Re: Noughts and Crosses/Tic Tac Toe code problem
This is not a constructor but rather a "pseudo-constructor":
Code :
public void GameBoard(int firstTurn){
//....
}
Note that constructors declare no return type, not void, not anything.
Cheers!
Re: Noughts and Crosses/Tic Tac Toe code problem
Ah of course, that was silly of me. Thanks!
I now just have the problem with board.isComplete() = false.
It states "unexpected type, required variable, found value" but I'm not sure what it refers to.
--- Update ---
I've realised the problem, it was simply me putting "=" instead of "==".
Re: Noughts and Crosses/Tic Tac Toe code problem
Quote:
Originally Posted by
Maukkv
Ah of course, that was silly of me. Thanks!
I now just have the problem with board.isComplete() = false.
It states "unexpected type, required variable, found value" but I'm not sure what it refers to.
--- Update ---
I've realised the problem, it was simply me putting "=" instead of "==".
And I'll suggest that you use neither. More succinct and clear than:
Code :
if (board.isComplete() == false) {
// ...
}
is to simply do:
Code :
if (!board.isComplete()) {
// ...
}
Re: Noughts and Crosses/Tic Tac Toe code problem
Okay thanks again.
I am actually having problems getting the game to work now when I make a move.
When I try to input rows and columns in order to activate the makeMove() method nothing happens and I am still presented with an array output of zeros. For example, this is the output I get from my code:
Code :
000
000
000
Player 1 it's your move.
Enter a row number 1-3: 1
Enter a column number 1-3: 2
current turn 2
000
000
000
Player 2 it's your move.
Enter a row number 1-3: 1
Enter a column number 1-3: 2
current turn 1
000
000
000
What I was hoping that would happen is that if I entered a row and column number it would replace the corresponding zero with the number of the current turn. I'm specifically new to the scanner, so I'm not sure if the way I've implemented that is wrong or if it is my code that is wrong in some way?
My code is:
Code :
public class NoughtsAndCrosses {
public static class GameBoard {
private int[][] theBoard;
private int currentTurn;
public 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() {
System.out.print(theBoard[0][0]);
System.out.print(theBoard[0][1]);
System.out.println(theBoard[0][2]);
System.out.print(theBoard[1][0]);
System.out.print(theBoard[1][1]);
System.out.println(theBoard[1][2]);
System.out.print(theBoard[2][0]);
System.out.print(theBoard[2][1]);
System.out.println(theBoard[2][2]);
}
//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){
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[2][2] == theBoard[1][1] && theBoard[2][2] == theBoard[0][0]){
if(theBoard[2][2] !=0){
winner = theBoard[2][2];
}
}
if(theBoard[2][0] == theBoard[1][1] && theBoard[2][0] == theBoard[0][2]){
if(theBoard[2][0] != 0){
winner = theBoard[2][0];
}
}
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()){
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();
System.out.println("current turn " + board.getCurrentTurn());
}
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());
}
}
Again thanks for the help!
Re: Noughts and Crosses/Tic Tac Toe code problem
A suggestion: Use variables in place of the hard coded magic numbers: 1 and 2 for the players.
Define two variables:
Code :
final int Player1 = 1; // define value for players
final int Player2 = 2;
Then you can easily see where a player is used by doing a find on "Player" vs looking for a 1 or 2.
Better will be an enum when you have gotten to that lesson
Re: Noughts and Crosses/Tic Tac Toe code problem
Look very closely at your isComplete() method, where you call it, and what *exactly* it does to the board int array.
Re: Noughts and Crosses/Tic Tac Toe code problem
Ah Excellent! It works as planned. Thanks guys! I didn't realise that in that instance the isComplete() method was setting the value of each entry back to zero each time.
:)