You are on page 1of 7

Your code will be graded on the following:

● Win method works correctly. Display a pop up window when a player wins, and the
window should say which player won.
● If the game ends in a tie, display a pop up window saying it’s a tie.
● Counting the number of wins (and ties)
● Option to display the number of wins and ties
● Resetting the board when appropriate
● Some changes to the look of the game (colors, pictures, etc.)

Optional:
● Computer player
● Make the computer player semi intelligent

Copy and paste code here:

For TicTacToe2:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TicTacToe2{

public static void main(String[] args){

JFrame f = new JFrame("Tic Tac Toe"); //create a JFrame and title it


f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //terminates the program
when the frame is closed
JPanel p = new TicTacToePanel(); //create a new panel using the
TicTacToePanel class
f.getContentPane().add(p); //add the panel to the frame

JMenu fileMenu = new JMenu("File"); //create a menu for the top of the frame
JMenuItem newGame = new JMenuItem("New Game");
JMenuItem winCount = new JMenuItem("Win Count");
newGame.addActionListener(new ActionListener(){ //add a listener to the menu
item
public void actionPerformed(ActionEvent e){
TicTacToePanel.resetBoard();
}
});

winCount.addActionListener(new ActionListener(){ //add a listener to the menu


item
public void actionPerformed(ActionEvent e){
TicTacToePanel.showWincount();
}
});

fileMenu.add(newGame);
fileMenu.add(winCount);
JMenuBar bar = new JMenuBar();
bar.add(fileMenu);
f.setJMenuBar(bar);
//f.pack();
f.setSize(600,600);
f.setVisible(true);

}
}

For TicTacToePanel:
import java.util.random.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TicTacToePanel extends JPanel{

//class level variables


private static JButton[][] board;
private static boolean xTurn;
private final static int ROWS = 3;
private final static int COLUMNS = ROWS;
static ImageIcon myPicture = new ImageIcon("src/patrick.jfif");
static int play1wins = 0;
static int play2wins = 0;
static int ties = 0;
static int count = 0;
//new int array to check winner
static int[][] tictac = new int[3][3];

//constructor - used to setup the tic-tac-toe board


public TicTacToePanel(){
setLayout(new GridLayout(ROWS, COLUMNS, 4, 4)); //rows, columns,
horizontal gap, vertical gap
board = new JButton[ROWS][COLUMNS];
Font myFont=new Font("Arial", Font.PLAIN, 60); //can also use BOLD or ITALIC,
number is font size

//create and add buttons to the JPanel


for(int row = 0; row <board.length; row++){
for(int col = 0; col < board[row].length; col++) {
board[row][col] = new JButton("");
board[row][col].addActionListener(new ButtonListener()); //add a
listener so that something happens when the button is clicked
board[row][col].setFont(myFont); //set the font
board[row][col].setIcon(myPicture);
board[row][col].setSize(200,200); //set the size of the button
add(board[row][col]); //add the board to the panel
//note that the add method is not called on an object because the
class extends JPanel, so the object constructed through the constructor is a JPanel
}
}

xTurn = true; //let player x go first


setSize(600,600); //set the size of the JPanel (see note above on how the class
extends JPanel)
}

//method to clear the board for a new game


public static void resetBoard(){
count = 0;
for(int row = 0; row < board.length; row++){
for(int col = 0; col <board[row].length; col++) {
board[row][col].setText("");
board[row][col].setIcon(myPicture);
tictac[row][col] = 0;
}

xTurn = true;
}
}
//method to check to see if there is a winner
public boolean win(){
//columns
if (tictac[0][0] == tictac[1][0] && tictac[1][0] == tictac[2][0] && tictac[2][0] == 1) {
return true;
}
else if (tictac[0][0] == tictac[1][0] && tictac[1][0] == tictac[2][0] && tictac[2][0] == 2)
{
return true;
}
if (tictac[0][1] == tictac[1][1] && tictac[1][1] == tictac[2][1] && tictac[2][1] == 1) {
return true;
}
else if (tictac[0][1] == tictac[1][1] && tictac[1][1] == tictac[2][1] && tictac[2][1] == 2)
{
return true;
}
if (tictac[0][2] == tictac[1][2] && tictac[1][2] == tictac[2][2] && tictac[2][2] == 1) {
return true;
}
else if (tictac[0][2] == tictac[1][2] && tictac[1][2] == tictac[2][2] && tictac[2][2] == 2)
{
return true;
}

//rows
if (tictac[0][0] == tictac[0][1] && tictac[0][1] == tictac[0][2] && tictac[0][2] == 1) {
return true;
}
else if (tictac[0][0] == tictac[0][1] && tictac[0][1] == tictac[0][2] && tictac[0][2] == 2)
{
return true;
}
if (tictac[1][0] == tictac[1][1] && tictac[1][1] == tictac[1][2] && tictac[1][2] == 1) {
return true;
}
else if (tictac[1][0] == tictac[1][1] && tictac[1][1] == tictac[1][2] && tictac[1][2] == 2)
{
return true;
}
if (tictac[2][0] == tictac[2][1] && tictac[2][1] == tictac[2][2] && tictac[2][2] == 1) {
return true;
}
else if (tictac[2][0] == tictac[2][1] && tictac[2][1] == tictac[2][2] && tictac[2][2] == 2)
{
return true;
}

//diagonals
if (tictac[0][0] == tictac[1][1] && tictac[1][1] == tictac[2][2] && tictac[2][2] == 1) {
return true;
}
else if (tictac[0][0] == tictac[1][1] && tictac[1][1] == tictac[2][2] && tictac[2][2] == 2)
{
return true;
}

if (tictac[0][2] == tictac[1][1] && tictac[1][1] == tictac[2][0] && tictac[2][0] == 1) {


return true;
}

else if (tictac[0][2] == tictac[1][1] && tictac[1][1] == tictac[2][0] && tictac[2][0] == 2)


{
return true;
}
//write code to see if a player won
//hint - use .getText() to get the text value of the button
//if you want the option of a bigger board, try to make it that if ROWS and
COLUMNS are changed, the code still works
else {
return false;
}
}

//will execute when a button is clicked, as long as the listener was added to the button
public class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JButton current = (JButton)e.getSource(); //get the source of the event
and cast it to a JButton

//find where click was


int r = 0; int c = 0;
for(int row = 0; row < board.length; row++ ) {
for(int col = 0; col < board[0].length; col++) {
if(board[row][col] == current) {
r = row;
c = col;
}
}
}

if (current.getText().equals("")){ //add an X or an O if the button is blank


if(xTurn){
ImageIcon myPicture = new ImageIcon("src/susapple.jfif");
current.setIcon(myPicture);
count++;
tictac[r][c] = 1;
xTurn = false;
}
else{
ImageIcon myPicture = new ImageIcon("src/bbldrake.jfif");
current.setIcon(myPicture);
count++;
current.setText("O");
tictac[r][c] = 2;
xTurn = true;
}

if(win()) {
if(xTurn) //if xTurn is true, then O just played
JOptionPane.showMessageDialog(null, "O wins!!");
//display a pop up window when a player wins
else
JOptionPane.showMessageDialog(null, "X wins!!");
//display a pop up window when a player wins
}

if (count == 9 && win() == false) {


JOptionPane.showMessageDialog(null, "Tie!!");
}

if(win()) {
if (xTurn)
play1wins++;

else
play2wins++;
}

if (count == 9 && win() == false) {


ties++;
}
}

}
}

public static void showWincount() {


// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "O wins: " + play1wins + "\nX wins: " +
play2wins + "\nTies: " + ties);
}

You might also like