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.
Can you create a 2-player Tic Tac Toe game (also known as noughts and crosses) on...
Can you create a 2-player Tic Tac Toe game (also known as noughts and crosses) on MATLAB, using nested loops (for, if, else, while loops) arrays, conditional execution and functions, as well as displaying the game board of X and O on a plot.
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.
**THIS IS FOR TIC TAC TOE GAME ** Draw three game trees, each having at least...
**THIS IS FOR TIC TAC TOE GAME ** Draw three game trees, each having at least 6 plies (one ply is one level in a game tree), label +1, 0, -1 for the leaves and label all the nodes with +1, 0, -1 values using minimax search. Design three game trees in such a way that each tree demonstrates exactly one of the following cases: i. the first player has a forced win ii. the first player has a forced...
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.
C++: Develop a simple Tc-Tac-Toe game between a human and the computer. Place the Tac-Tac-Toe board...
C++: Develop a simple Tc-Tac-Toe game between a human and the computer. Place the Tac-Tac-Toe board in an array of char (e.g., char board[9] ) and use functions to create "operations" from which you can build a game (see class notes, for example: reset(), Go(), display(), etc.... avoid an over used of global variables, use constants instead of pure numbers or characters, provide comments for other programmers to see how your code works.
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...