0% found this document useful (0 votes)
45 views

Aarya Sood - Tictactoe Code

The code will be graded on the following criteria: - Displaying a pop up window indicating the winner or a tie. - Counting and displaying the number of wins and ties. - Resetting the game board appropriately. - Customizing the look of the game through colors, pictures, etc. Optional criteria include adding a computer player and making it semi-intelligent. The code provided implements a Tic Tac Toe game with a panel to display the board, buttons for player input, and methods to check for a winner and reset the board.

Uploaded by

api-702523169
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Aarya Sood - Tictactoe Code

The code will be graded on the following criteria: - Displaying a pop up window indicating the winner or a tie. - Counting and displaying the number of wins and ties. - Resetting the game board appropriately. - Customizing the look of the game through colors, pictures, etc. Optional criteria include adding a computer player and making it semi-intelligent. The code provided implements a Tic Tac Toe game with a panel to display the board, buttons for player input, and methods to check for a winner and reset the board.

Uploaded by

api-702523169
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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:

TicTacToe2:
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");
newGame.addActionListener(new ActionListener(){ //add a listener to the
menu item
public void actionPerformed(ActionEvent e){
TicTacToePanel.resetBoard();
}
});

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

TicTacToePanel:

private static JButton[][] board;


private static boolean xTurn;
private final static int ROWS = 3;
private final static int COLUMNS = ROWS;
private static int turnCount = 0;
static int win = 0;
static int countwin = 0;
static int winx = 0;
static int losex = 0;
static int wino = 0;
static int loseo = 0;
static int numofx = 0;
static int numofo = 0;
static int numofties = 0;

//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("Comic Sans MS", Font.BOLD, 50); //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].setSize(200,200); //set the size of the button
board[row][col].setBackground(new Color(40, 35, 110));
board[row][col].setForeground(Color.WHITE);
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(){
for(int row = 0; row < board.length; row++){
for(int col = 0; col <board[row].length; col++)
board[row][col].setText("");
}
xTurn = true;
turnCount = 0;
}

//method to check to see if there is a winner


public boolean win(){
//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

boolean winner = false;

if(!board[0][0].getText().equals("") &&
board[0][0].getText().equals(board[0][1].getText()) &&
board[0][2].getText().equals(board[0][0].getText())) {
win++;
return true;
}
else if(board[1][0].getText().equals(board[1][1].getText()) &&
board[1][2].getText().equals(board[1][0].getText())) {
win++;
}
else if(board[2][0].getText().equals(board[2][1].getText()) &&
board[2][0].getText().equals(board[2][2].getText())) {
win++;
}
else if(board[0][0].getText().equals(board[1][0].getText()) &&
board[0][0].getText().equals(board[2][0].getText())) {
win++;
}
else if(board[0][1].getText().equals(board[1][1].getText()) &&
board[0][1].getText().equals(board[2][1].getText())) {
win++;
}
else if(board[0][2].getText().equals(board[1][2].getText()) &&
board[0][2].getText().equals(board[2][2].getText())) {
win++;
}
else if(board[0][0].getText().equals(board[1][1].getText()) &&
board[0][0].getText().equals(board[2][2].getText())) {
win++;
}
else if(board[0][2].getText().equals(board[1][1].getText()) &&
board[0][2].getText().equals(board[2][0].getText())) {
win++;
}
if(board[0][0].getText().equals(board[0][1].getText()) &&
board[0][2].getText().equals(board[0][0].getText())) {
if(board[0][0].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[0][0].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
if(board[1][0].getText().equals(board[1][1].getText()) &&
board[1][2].getText().equals(board[1][0].getText())) {
if(board[1][0].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[1][0].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
if(board[2][0].getText().equals(board[2][1].getText()) &&
board[2][0].getText().equals(board[2][2].getText())) {
if(board[2][0].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[2][0].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
if(board[0][0].getText().equals(board[1][0].getText()) &&
board[0][0].getText().equals(board[2][0].getText())) {
if(board[0][0].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[0][0].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
if(board[0][1].getText().equals(board[1][1].getText()) &&
board[0][1].getText().equals(board[2][1].getText())) {
if(board[0][1].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[0][1].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
if(board[0][2].getText().equals(board[1][2].getText()) &&
board[0][2].getText().equals(board[2][2].getText())) {
if(board[0][2].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[0][2].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
if(board[0][0].getText().equals(board[1][1].getText()) &&
board[0][0].getText().equals(board[2][2].getText())) {
if(board[0][0].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[0][0].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
if(board[0][2].getText().equals(board[1][1].getText()) &&
board[0][2].getText().equals(board[2][0].getText())) {
if(board[0][2].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[0][2].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
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
int[][] matrix;
if (current.getText().equals("")){ //add an X or an O if the button is
blank
if(xTurn){
current.setText("x");
turnCount++;
xTurn = false;
}
else{
current.setText("o");
turnCount++;
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
JOptionPane.showMessageDialog(null, "Times
o has won: " + numofo++);
}
else
JOptionPane.showMessageDialog(null, "x
wins!!"); //display a pop up window when a player wins
JOptionPane.showMessageDialog(null, "Times x has
won: " + numofx++);
}
else if(turnCount == 9) {
JOptionPane.showMessageDialog(null, "It's a tie!!");
turnCount++;
JOptionPane.showMessageDialog(null, "Number of
ties: " + numofties++);
}

}
}

private boolean tie() {


// TODO Auto-generated method stub
return false;
}
}
}

You might also like