Question

Please do this in Java (Java FX) Tic-Tac-Toe game (aka X's and O's) in which the...

Please do this in Java (Java FX)

Tic-Tac-Toe game (aka X's and O's) in which the player, who is "X", plays against the application, which is "O". The game should be built using the basic window-with-canvas code used for the various tutorials and course assignments. The user plays by dragging "X" tokens onto the Tic-Tac-Toe board area after which the application will automatically play by moving an "O" onto the board area. When a game is over the app should show who won, if any. If necessary review how Tic-Tac-Toe works by searching online. Here are the detailed requirements.

public class Token {
   public void drawWith(GraphicsContext thePen){
       //empty code
   }
}

public class PlayerToken extends Token {
   public void drawWith(GraphicsContext thePen){
     //code for drawing "X" tokens
   }
}

public class OpponentToken extends Token {
   public void drawWith(GraphicsContext thePen){
     //code for drawing "O" tokens
   }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;
  
public class TicTacToeTest{
  
public static void main(String[ ] args) {
  
TicTacToe t = new TicTacToe();
Scanner s = new Scanner(System.in);
int x=0,y=0;
do
{
System.out.println(t.player==t.X?"Player X turn":"Player O turn");
System.out.println("Enter x and y places");
x=s.nextInt();
y=s.nextInt();
  
t.putSign(x, y);
System.out.println(t.toString());
System.out.println("_____________________________");
t.displayWinner();
  
}while(t.isEmpty);
}
}
  
class TicTacToe
{
public static final int X = 1, O = -1;
public static final int EMPTY = 0;
  
public int player = X;
private int[][] board = new int[3][3];
public boolean isEmpty = false;
  
/** Puts an X or O mark at position i,j. */
public void putSign(int x, int y)
{
if(x<0 || x>2 || y<0 || y>2)
{
System.out.println("Invalid board position");
return;
}
if(board[x][y] != EMPTY)
{
System.out.println("Board position occupied");
return;
}
board[x][y] = player; // place the mark for the current player
player = -player; // switch players (uses fact that O = - X)
}
  
/** Checks whether the board configuration is a win for the given player. */
public boolean isWin(int player)
{
return ((board[0][0] + board[0][1] + board[0][2] == player*3) ||
(board[1][0] + board[1][1] + board[1][2] == player*3) ||
(board[2][0] + board[2][1] + board[2][2] == player*3) ||
(board[0][0] + board[1][0] + board[2][0] == player*3) ||
(board[0][1] + board[1][1] + board[2][1] == player*3) ||
(board[0][2] + board[1][2] + board[2][2] == player*3) ||
(board[0][0] + board[1][1] + board[2][2] == player*3) ||
(board[2][0] + board[1][1] + board[0][2] == player*3));
}
  
/**display the winning player or indicate a tie (or unfinished game).*/
public void displayWinner()
{
if(isWin(X))
{
System.out.println("\n X wins...!!");
isEmpty=false;
}
else if(isWin(O))
{
System.out.println("\n O wins...!!");
isEmpty=false;
}
else
{
if(!isEmpty)
{
System.out.println("its a tie");
}
  
}
}
  
public String toString()
{
StringBuilder s = new StringBuilder();
isEmpty = false;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
switch(board[i][j])
{
case X:
s.append(" X ");
break;
case O:
s.append(" O ");
break;
case EMPTY:
s.append(" ");
isEmpty=true;
break;
}
if(j<2)
{
s.append("|");
}
  
}
if(i<2)
{
s.append("\n-----------\n");
}
}
return s.toString();
}
}

Add a comment
Know the answer?
Add Answer to:
Please do this in Java (Java FX) Tic-Tac-Toe game (aka X's and O's) in which the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an...

    (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...

  • Java project In a game of tic-tac-toe, two players take turns marking an available cell in...

    Java project In a game of tic-tac-toe, two players take turns marking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Create...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1....

    PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1. Create the data structure – Nine slots that can each contain an X, an O, or a blank. – To represent the board with a dictionary, you can assign each slot a string-value key. – String values in the key-value pair to represent what’s in each slot on the board: ■ 'X' ■ 'O' ■ ‘ ‘ 2. Create a function to print the...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • In Java create a program where a user plays Tic Tac Toe against the computer. Create...

    In Java create a program where a user plays Tic Tac Toe against the computer. Create a class TicTacToe that will enable you write a program to play Tic-Tac-Toe. The class contains a private 3x3 two-dimensional array. Use an enum type to represent the value in each cell of the array. The enum’s constants should be named X, O, and EMPTY (for a position that does not contain an X or O). The constructor should initialize the board elements to...

  • Hi, I am a student in college who is trying to make a tic tac toe...

    Hi, I am a student in college who is trying to make a tic tac toe game on Java. Basically, we have to make our own game, even ones that already exist, on Java. We also have to implement an interface, that I have no idea exactly. The professor also stated we need at least 2 different data structures and algorithms for the code. The professor said we could use code online, we'd just have to make some of our...

  • Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game....

    Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...

  • (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe....

    (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT