JAVA Programming
* create a 3 dimensional tic tac toe game. make 1 player the computer.
Answer:
import java.util.*;
public class PlayGame
{
private int incrementer;
private char location[]=new char[10];
private char gamer;
public static void main(String args[])
{
String ch;
PlayGame Toe=new PlayGame();
do{
Toe.beginBoard();
Toe.startplay();
System.out.println ("Would you like to play again (Enter 'Y')?
");
Scanner in =new Scanner(System.in);
ch=in.nextLine();
System.out.println("ch value is "+ch);
}while (ch.equals("Y"));
}
public void beginBoard()
{
char locationdef[] = {'0','1', '2', '3', '4', '5', '6', '7', '8',
'9'};
int i;
incrementer = 0;
gamer = 'X';
for (i=1; i<10; i++) location[i]=locationdef[i];
presentBoard();
}
public String presentBoard()
{
System.out.println( "\n\n" );
System.out.println( "\n\n" );
System.out.println( "\n\n\t\t" + location [1] + " | " +location
[2]+ " | " +location [3]);
System.out.println( " \t\t | | " );
System.out.println( " \t\t ___|____|___ " );
System.out.println( "\n\n\t\t" +location [4]+ " | " +location [5]+
" | " +location [6]);
System.out.println( " \t\t | | " );
System.out.println( " \t\t ___|____|___ " );
System.out.println( "\n\n\t\t" +location [7]+ " | " +location [8]+
" | " +location [9]);
System.out.println( " \t\t | | " );
System.out.println( " \t\t | | " );
System.out.println( "\n\n" );
return "presentBoard";
}
public void startplay()
{
int center;
char blank = ' ';
System.out.println( "gamer " + locategamer() +" will go first and
be the letter 'X'" );
do {
presentBoard();
System.out.println( "\n\n gamer " + locategamer() +" choose a
location." );
boolean currentlocation = true;
while (currentlocation) {
Scanner in =new Scanner (System.in);
center=in.nextInt();
currentlocation = checklocation(center);
if(currentlocation==false)
location[center]=locategamer();
}
System.out.println( "Excellent move" );
presentBoard();
latergamer();
}while ( getWinner() == blank );
}
public char getWinner()
{
char Winner = ' ';
if (location[1] == 'X' && location[2] == 'X' &&
location[3] == 'X') Winner = 'X';
if (location[4] == 'X' && location[5] == 'X' &&
location[6] == 'X') Winner = 'X';
if (location[7] == 'X' && location[8] == 'X' &&
location[9] == 'X') Winner = 'X';
if (location[1] == 'X' && location[4] == 'X' &&
location[7] == 'X') Winner = 'X';
if (location[2] == 'X' && location[5] == 'X' &&
location[8] == 'X') Winner = 'X';
if (location[3] == 'X' && location[6] == 'X' &&
location[9] == 'X') Winner = 'X';
if (location[1] == 'X' && location[5] == 'X' &&
location[9] == 'X') Winner = 'X';
if (location[3] == 'X' && location[5] == 'X' &&
location[7] == 'X') Winner = 'X';
if (Winner == 'X' )
{System.out.println("gamer1 wins the game." );
return Winner;
}
if (location[1] == 'O' && location[2] == 'O' &&
location[3] == 'O') Winner = 'O';
if (location[4] == 'O' && location[5] == 'O' &&
location[6] == 'O') Winner = 'O';
if (location[7] == 'O' && location[8] == 'O' &&
location[9] == 'O') Winner = 'O';
if (location[1] == 'O' && location[4] == 'O' &&
location[7] == 'O') Winner = 'O';
if (location[2] == 'O' && location[5] == 'O' &&
location[8] == 'O') Winner = 'O';
if (location[3] == 'O' && location[6] == 'O' &&
location[9] == 'O') Winner = 'O';
if (location[1] == 'O' && location[5] == 'O' &&
location[9] == 'O') Winner = 'O';
if (location[3] == 'O' && location[5] == 'O' &&
location[7] == 'O') Winner = 'O';
if (Winner == 'O' )
{
System.out.println( "gamer2 wins the game." );
return Winner; }
for(int i=1;i<10;i++)
{
if(location[i]=='X' || location[i]=='O')
{
if(i==9)
{
char Draw='D';
System.out.println(" Game is draw ");
return Draw;
}
continue;
}
else
break;
}
return Winner;
}
public boolean checklocation(int center)
{
if (location[center] == 'X' || location[center] == 'O')
{
System.out.println("That location is already occupied please choose
another location");
return true;
}
else {
return false;
}
}
public void latergamer()
{
if (gamer == 'X')
gamer = 'O';
else gamer = 'X';
}
public String locatename()
{
return " Game Tic Tac Toe" ;
}
public char locategamer()
{
return gamer;
}
}
Get Answers For Free
Most questions answered within 1 hours.