Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is the Java code for Tic Tac Toe game, can you please modify my code asking whether the computer starts first or the player

Below is the Java code for Tic Tac Toe game, can you please modify my code asking whether the computer starts first or the player starts first the game. Also, add a HELP button/page that displays user name, rules of the game and the month/day/year when the implementation was finished.

Rules of the game:

1. The game is played on a grid that's 3 squares by 3 squares.

2. You are X, your friend (or the computer in this case) is O. Players take turns putting their marks in empty squares.

3. The first player to get 3 of her marks in a row (up, down, across, or diagonally) is the winner.

4. When all 9 squares are full, the game is over. If no player has 3 marks in a row, the game ends in a tie.

My Code:

import java.util.*;

import java.util.List;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class TicTacToe extends JPanel

{

static String[] board;

static String turn;

static List buttons=new ArrayList();

static JFrame frame=new JFrame();

static int value=0;

static String winner = null;

static JLabel information=new JLabel("Welcome to 2 Player Tic Tac Toe.
X will play first.
Click near the center of a slot to place X in:");

public static void main(String[] args)

{

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

TicTacToe ticTacToe=new TicTacToe();

frame.add(ticTacToe);

frame.setContentPane(ticTacToe);

frame.setVisible(true);

board = new String[9];

turn = "X";

populateEmptyBoard();

}

static void winningText()

{

winner=checkWinner();

if(winner!=null)

{

if (winner.equalsIgnoreCase("draw"))

{

information.setText("It's a draw!
Thanks for playing.");

} else

{

information.setText("Congratulations!
"+ winner+ " has won!
Thanks for playing.");

}

}

}

static String checkWinner()

{

for (int a = 0; a < 8; a++)

{

String line = null;

switch (a)

{

case 0:

line = board[0] + board[1] + board[2];

break;

case 1:

line = board[3] + board[4] + board[5];

break;

case 2:

line = board[6] + board[7] + board[8];

break;

case 3:

line = board[0] + board[3] + board[6];

break;

case 4:

line = board[1] + board[4] + board[7];

break;

case 5:

line = board[2] + board[5] + board[8];

break;

case 6:

line = board[0] + board[4] + board[8];

break;

case 7:

line = board[2] + board[4] + board[6];

break;

}

if (line.equals("XXX"))

{

return "X";

}

else if (line.equals("OOO"))

{

return "O";

}

}

for (int a = 0; a < 9; a++)

{

if (Arrays.asList(board).contains("Empty"))

{

break;

}

else if (a == 8) return "draw";

}

information.setText(""+turn+"'s turn.
Click near the center of a slot to place "+turn+" in.");

return null;

}

static void populateEmptyBoard()

{

frame.setLayout(null);

information.setBounds(10,160, 400, 400);

frame.add(information);

int xIncrease=0;

int yIncrease=-100;

for(int i=0; i<9; i++) board[i]="Empty";

for(int y=0; y<3; y++)

{

xIncrease=0;

yIncrease+=100;

for (int x = 0; x < 3; x++)

{

buttons.add(new JButton(""));

buttons.get(value).setBounds(xIncrease, yIncrease, 100, 100);

buttons.get(value).setOpaque(false);

buttons.get(value).setContentAreaFilled(false);

buttons.get(value).setBorderPainted(false);

frame.add(buttons.get(value));

buttons.get(value).addActionListener(new ActionListener()

{

int val=value;

public void actionPerformed(ActionEvent e)

{

if(winner==null && board[val]=="Empty")

{

board[val]=turn;

buttons.get(val).setText(turn);

if (turn.equals("X"))

{

turn = "O";

}

else

{

turn = "X";

}

winningText();

}

else if(winner!=null) information.setText("The game has already been finished!");

else information.setText("The slot is already taken!
Pick another slot!");

}

});

value++;

xIncrease+=100;

}

frame.setSize(500,500);

frame.setResizable(false);

}

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

int x=100;

g.setColor(Color.BLACK);

for(int i=0; i<2; i++)

{

g.drawLine(0, 0+x, 300,0+x);

if(i==2) break;

g.drawLine(0+x,0,0+x, 300);

x+=100;

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions