I am unable to get desired output in the following code, for a console based Tic Tac Toe program.. Can anyone help me identify the error:
import java.util.Scanner;
Following is the TicTacToe class:
public class TicTacToe {
TicTacToe(){
System.out.println("---------------------");
System.out.println(" Tic Tac Toe ");
System.out.println("---------------------");
System.out.println();
}
public static void main(String[] args) {
TicTacToe t = new TicTacToe();
Scanner sc = new Scanner(System.in);
int a[][] = new int [3][3];
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
a[i][j] = 2;
System.out.print(" "+a[i][j]+" ");
}
System.out.println();
}
int count = 0;
I have used do-while loop for executing the break statements.. Although this is inefficient, nothing else struck me at that moment.
do{
int[][] turn1 = t.player1(a);
t.display(turn1);
int[][] turn2 = t.player2(turn1);
t.display(turn2);
int[][] turn3 = t.player1(turn2);
t.display(turn3);
int[][] turn4 = t.player2(turn3);
t.display(turn4);
int[][] turn5 = t.player1(turn4);
t.display(turn5);
if(t.checkwin(turn5))
break;
int[][] turn6 = t.player2(turn5);
t.display(turn6);
if(t.checkwin(turn6))
break;
int[][] turn7 = t.player1(turn6);
t.display(turn7);
if(t.checkwin(turn5))
break;
int[][] turn8 = t.player2(turn7);
t.display(turn8);
if(t.checkwin(turn5))
break;
int[][] turn9 = t.player1(turn8);
t.display(turn9);
if(t.checkwin(turn5))
break;
System.out.println("Game Draw");
count++;
}while(count ==1);
}
Following is the checkwin() method that take 2D array and returns boolean which is used for breaking or continuing the previous do-while loop.
public boolean checkwin(int m[][]){
for(int i=0; i<3; i++){
if(m[i][0] == m[i][1] && m[i][1] == m[i][2]){
if(m[i][0] == 1){
player1wins();
return true;
}
else if(m[i][0] == 0){
player2wins();
return true;
}
}
The condition for following if statement shows error..
if(m[0][i] == [1][i] && m[1][i] == m[2][i]){
if(m[0][i] == 1){
player1wins();
return true;
}
else if(m[0][i] == 0){
player2wins();
return true;
}
}
}
The condition for following if statement shows error..
if(m[0][0] == [1][1] && m[1][1] == m[2][2]){
if(m[0][0] == 1){
player1wins();
return true;
}
else if(m[0][0] == 0){
player2wins();
return true;
}
}
return false;
}
Following player1wins(), player2wins() methods display if either of them won..
public static void player1wins(){
System.out.println("1st player wins");
}
public static void player2wins(){
System.out.println("2nd player wins");
}
Following display method which prints matrix after each turn..
public void display(int matrix[][]){
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
System.out.print(" "+matrix[i][j]+" ");
}
System.out.println();
}
}
Following player1() and player2() methods takes input coordinates from the user.. They take input matrix as parameter and returns a 2D matrix..
public int[][] player1(int matrix[][]){
Scanner sc = new Scanner(System.in);
System.out.println("1st player's turn...");
System.out.print("Enter x coordinate: ");
int x = sc.nextInt();
System.out.print("Enter y coordinate: ");
int y = sc.nextInt();
matrix[x][y] = 1;
return matrix;
}
public int[][] player2(int matrix[][]){
Scanner sc = new Scanner(System.in);
System.out.println("2nd player's turn...");
System.out.print("Enter x coordinate: ");
int x = sc.nextInt();
System.out.print("Enter y coordinate: ");
int y = sc.nextInt();
matrix[x][y] = 0;
return matrix;
}
}
Moreover, if anyone can help me optimize this console based tic-tac-toe java code or post any link for the same, it would be of great help.. Thanks, in advance..
Your if statement looks to be syntactically incorrect:
if(m[0][i] == [1][i] && m[1][i] == m[2][i]){
The [1][i]
is missing an m
before it. It should be this:
if(m[0][i] == m[1][i] && m[1][i] == m[2][i]){
The same applies to the other if statement:
Change
if(m[0][0] == [1][1] && m[1][1] == m[2][2]){
to
if(m[0][0] == m[1][1] && m[1][1] == m[2][2]){
These are both in your checkwin() method.
EDIT: Another bug: Your code checks the board for turn 5 on each turn. (For turn 7, 8, and 9, this is the case. 6 is fine).
Also, rather than using a do loop, you could just use return
instead of break
.
But I would recommend eliminating the repetition of that bit entirely: change it to this, using for and a 3d array.
int[][][] turns = new int[10][][];
turns[0] = a;
// We start at 1 rather than 0 because the first value is a.
for (int i = 1; i <= 9; i++) {
if (i % 2 == 1) { // Odd-numbered turn
turns[i] = t.player1(turns[i - 1]);
} else { // Even-numbered turn
turns[i] = t.player2(turns[i - 1]);
}
t.display(turns[i]);
if (t.checkwin(turns[i])) {
return;
}
}
System.out.println("Game Draw");
This is much cleaner-looking, and should work well.