Jump to content

tic tac toe help can't run errors

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
carlitos2012

carlitos2012

    Newbie

  • Members
  • Pip
  • 4 posts
import java.util.Scanner;
import java.util.Random;
public class tictactoe
{
public static void main(String[] args)
{
Scanner keyboard=new Scanner(System.in);
String[][] board= {{" "," "," "},{" "," "," "},{" "," "," "}};
int x=0;
int y=0;
int option=0;

String player1;
String player2;

System.out.println("Enter 1 for 2 player, or press 7 to exit");
option=keyboard.nextInt();
if (option==1)
{
System.out.println("Player 1 enter your name");
player1=keyboard.next();
System.out.println("Player 2 enter your name");
player2=keyboard.next();
boolean w=winner(board);
while (w==true||w==false)
{
System.out.println(player1+" enter your choice");
x=keyboard.nextInt();
y=keyboard.nextInt();
if (board[x][y]=="X")
{
System.out.println("That space is already taken");
continue;
}
if (board[x][y]=="Y")
{

System.out.println("That space is already taken");
continue;
}
board[x][y]="X";
displayBoard(board);
do
{
System.out.println(player2+" enter your choice");
x=keyboard.nextInt();
y=keyboard.nextInt();
if (board[x][y]=="X")
{
System.out.println("That space is already taken");
continue;
}
if (board[x][y]=="O")
{
System.out.println("That space is already taken");
continue;
}
board[x][y]="O";
displayBoard(board);
break;
}
while (true);
}

}
}
public static void displayBoard (String z[][])
{
System.out.println(" "+z[0][0]+" | "+z[0][1]+" | "+z[0][2]);
System.out.println(" ---------");
System.out.println(" "+z[1][0]+" | "+z[1][1]+" | "+z[1][2]);
System.out.println(" ---------");
System.out.println(" "+z[2][0]+" | "+z[2][1]+" | "+z[2][2]);
}
}

public static boolean winner (String z[][])
{
if ( ( z[0][0]=="X" && z[0][1]=="X" && z[0][2]=="X" )
|| ( z[1][0]=="X" && z[1][1]=="X" && z[1][2]=="X" )
|| ( z[2][0]=="X" && z[2][1]=="X" && z[2][2]=="X" )
|| ( z[0][0]=="X" && z[1][0]=="X" && z[2][0]=="X" )
|| ( z[0][1]=="X" && z[1][1]=="X" && z[2][1]=="X" )
|| ( z[0][0]=="X" && z[1][1]=="X" && z[2][2]=="X" )
|| ( z[0][2]=="X" && z[1][1]=="X" && z[2][0]=="X" ) )
return true;
}
}
}


}

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
At least give us the error stacktrace and post the code in code tags (click the #-button)