Question

JAVA Programming * create a 3 dimensional tic tac toe game. make 1 player the computer.

JAVA Programming

* create a 3 dimensional tic tac toe game. make 1 player the computer.

Homework Answers

Answer #1

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;
}
  
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Prove that the first player in a game of (the usual 3 × 3) tic-tac-toe can...
Prove that the first player in a game of (the usual 3 × 3) tic-tac-toe can always (at least) force a draw. Note that one way to do this is to draw the full game tree, but there is an easier proof.
Consider a modified version of the very famous tic-tac-toe game in which player 1 moves, then...
Consider a modified version of the very famous tic-tac-toe game in which player 1 moves, then player 2 moves, and then player 3 moves and the game ends. The number of strategies for player 1, 2, and, 3 are respectively: a. 9, 72, and 504. b. None of the available options in this list. c. 9, 9, and 9. d. 9, 8, and 7.
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. // A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. // At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the...
***JAVA*** Write a program that randomly populates a standard tic tac toe board, and then determines...
***JAVA*** Write a program that randomly populates a standard tic tac toe board, and then determines if there is a winner or if there is a tie (“cat”). Also the program should print out the randomly generated board to verify its findings. You may also assume that the board is fully populated before a winner is checked, and in the event that both players have a winning condition print out that both win. Example Dialog: Welcome to Random Tic Tac...
A Tic-tac-toe board has 9 spaces. The first player fills one of the spaces with an...
A Tic-tac-toe board has 9 spaces. The first player fills one of the spaces with an X, then the second player fills a space with an O. The players continue to alternate filling a space with their respective symbol until no empty spaces remain on the board. How many different arrangements of X’s and O’s are possible at the end of the game? You must fully justify your answer.
Which of the following is a simultaneous decision game? A tic-tac-toe B chess C poker D...
Which of the following is a simultaneous decision game? A tic-tac-toe B chess C poker D rock-paper-scissors (Roshambo)
Part 1: Create the grid tic-tac-toe gameboard using buttons and iteration. Part 2: Human user gets...
Part 1: Create the grid tic-tac-toe gameboard using buttons and iteration. Part 2: Human user gets to select an open cell on the grid - place an X on that button selected Part 3: Check for a win using DOM iteration - new game option if row or column matching X pattern Part 4: Computer gets to select an open cell on the grid - place an O on that button selected Part 5: Check for a win using DOM...
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional char array with three rows and three columns as the game board. Each element in the array should be initialized with an asterisk (*). The program should run a loop that: • Displays the contents of the board array. • Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and...
In Java programming language 11.Create the code for a for loop to print the numbers 1  through...
In Java programming language 11.Create the code for a for loop to print the numbers 1  through 5 inclusive vertically so the output is 1 2 3 4 5 11b What is the output of the following               OUTPUT int i=3; while(int i >0) { System.out.println(i); i--; } 11c What is the output of the following               OUTPUT for(int i=3;i<10;i++) System.out.println(3*i); 11d Create the line of code to print the numbers 10 through 1 decreasing by 1 each time 11e Create the line of code...
Java programming. Fill the empty boxes with the Answer. 1) The Java compiler javac translates Java...
Java programming. Fill the empty boxes with the Answer. 1) The Java compiler javac translates Java source code into [Answer]. 2) In Java a class object can inherit the properties of a superclass by using the [Answer] keyword. 3) A(n) [Answer] method consists of the same method name but different number or types of parameters 4) In a class definition, the special method that is called to create an instance of that class is known as a/an [Answer] 5) The...