Hwk10java package hwk10 import javaawtBorderLayout import ja

Hwk10.java

package hwk10;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Component;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.BoxLayout;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

@SuppressWarnings(\"serial\")

public class Hwk10 extends JFrame {

   Hwk10() {

       this.setTitle(\"Tic Tac Toe\");

       this.setBounds(0, 0, 600, 600);

       this.setDefaultCloseOperation(EXIT_ON_CLOSE);

   }

  

  

   public static void main(String[] args) throws IOException {

       JFrame frame = new Hwk10();

frame.setLayout(new BorderLayout());

  

// create a panel with box layer aligned with y axis

JPanel gamePanel = new JPanel();

gamePanel.setLayout(new BoxLayout(gamePanel, BoxLayout.Y_AXIS));

  

// create a new game button that is centered on x axis

JButton newGame = new JButton(\"New Game\");

newGame.setAlignmentX(Component.CENTER_ALIGNMENT);

// add the new game button to the game panel

gamePanel.add(newGame);

  

// create a box panel with 3 x 3 grid layout

JPanel box = new JPanel();

box.setLayout(new GridLayout(3, 3));

  

// create an array of 9 buttons and add them to the box panel

JButton[] buttons = new JButton[9];

for (int i = 0; i < 9; i++) {

buttons[i] = new JButton(\"\");

box.add(buttons[i]);

}

  

  

// instantiate a tic-tac-toe game object

final TicTacToe game = new TicTacToe(buttons);

  

// add an action listener to the new-game buttom so that it clears the game on click.

ActionListener WhenHitTheButton = new ActionListener() {

public void actionPerformed(ActionEvent e) {

game.clear();

}

};

newGame.addActionListener(WhenHitTheButton);

  

// add game panel (north) and box panel (center) to the frame and set it visible

frame.add(gamePanel, BorderLayout.NORTH);

frame.add(box, BorderLayout.CENTER);

      

       frame.setVisible(true);

   }

}

class TicTacToe {

   int[] data = new int[9];

   JButton[] buttons;

   boolean isX = true;

   // this variable keeps track of how many steps the game has played (it cannot be >= 9)

   int steps = 0;

   // use the flag to indicate whether the game has ended due to that X wins or O wins but not due to a draw

   boolean gameEnded = true;

  

   ImageIcon icon_x, icon_o;

  

   // save buttons, load icon images, and clear the game (by calling \"clear\" method)

   TicTacToe(JButton[] buttons) throws IOException {

       this.buttons = buttons;

       icon_x = new ImageIcon(ImageIO.read(getClass().getResource(\"x.png\")));

       icon_o = new ImageIcon(ImageIO.read(getClass().getResource(\"o.png\")));

       clear();

   }

  

   // add the same listener to all buttons

   void addListener() {

       // TODO

   }

   // create an action listener

   ActionListener buttonListener = new ActionListener() {

       // 1. Use the event source to find out which button is clicked

       // 2. Find out if anyone wins and if so, has it X or O player won

       // 3. In the console,

       // if X wins, print \"X wins\",

       // if O wins, print \"O wins\",

       // if it is a draw (max number of steps has reached), print \"Draw\"

       // if nobody wins, do nothing and continue the game

       // 4. Don\'t forget to increment the \"steps\" variable to find out whether max number of steps has reached.

       // 5. Switch player in this method as well if it is a legal move.

       @Override

       public void actionPerformed(ActionEvent e) {

           Object src = e.getSource();

           // TODO

       }

   };

  

   // reset \"steps\" to 0

   // for each button, set the background color to \"white\" and remove icons from buttons \"setIcon(null)\"

   // reset \"data\" array to 0

   // if game has ended (due to someone winnning), add listener back to buttons and reset the flag to false

   void clear() {

       // TODO

   }

  

   // remove listener from all buttons and set \"gameEnded\" flag to true

   void removeListener() {

       // TODO

   }

  

   // put X or O at i

   // if a button click is legal (i.e. the button was no clicked before), set icons to the button and update \"data\" array accordingly

   // return true if and only if the button click is legal

   boolean play(int i, boolean isX) {

       // TODO

   }

   // if X wins, return 1,

   // if O wins, return 2,

   // if draws or no result, return 0

   //

   // if someone wins, the remove listeners so that subsequent clicks won\'t change the remaining unclicked buttons

   int win() {

       // TODO

   }

   // check whether the ith row matches

   // if so, set the background for that row of buttons to blue color

   // and also return 1 if X wins, 2 if O wins, 0 if nobody wins

   int checkRow(int i) {

       // TODO

   }

   // check whether the ith column matches

   // if so, set the background for that column of buttons to blue color

   // and also return 1 if X wins, 2 if O wins, 0 if nobody wins

   int checkColumn(int i) {

       // TODO

   }

   // check whether a diagonal matches

   // if so, set the background for that diagonal of buttons to blue color

   // and also return 1 if X wins, 2 if O wins, 0 if nobody wins

   // note that there are two diagonals to check

   int checkDiagonal() {

       // TODO

   }

   // set buttons at the specified indices to blue

   void setBackground(int[] indices) {

       // TODO

   }

}

Homework 10 November 12, 2016 1 Overview In this assignment, you will continue homework 9 by adding some features to the Tic Tac Toe game. 2 Requirements You should check whether the player X or O wins or a draw has been reached each time a legal move has been made. You should print out the game result (X wins, O wins, or draw) in the console. If a player wins (not draw), you should disable the remaining unclicked buttons in the game grid so that they cannot be selected. You can accomplish this by removing listeners from all buttons. If a player wins, you should highlight the winning row, column, or diagonal by changing the background of the corresponding buttons to blue. When new game button is clicked, you should remove all icons from the buttons in the grid and change their background color to white again 3 GUI You should try t with this to emulate the behavior shown in the video I pos homework 4 Submission You should submit your source code by the name of Hwk10.java to the drop box

Solution

package hwk10;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Arrays;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings(\"serial\")
public class Hwk10 extends JFrame {

Hwk10() {
this.setTitle(\"Tic Tac Toe\");
this.setBounds(0, 0, 600, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) throws IOException {
JFrame frame = new Hwk10();
frame.setLayout(new BorderLayout());

// create a panel with box layer aligned with y axis
JPanel gamePanel = new JPanel();
gamePanel.setLayout(new BoxLayout(gamePanel, BoxLayout.Y_AXIS));

// create a new game button that is centered on x axis
JButton newGame = new JButton(\"New Game\");
newGame.setAlignmentX(Component.CENTER_ALIGNMENT);
// add the new game button to the game panel
gamePanel.add(newGame);

// create a box panel with 3 x 3 grid layout
JPanel box = new JPanel();
box.setLayout(new GridLayout(3, 3));

// create an array of 9 buttons and add them to the box panel
JButton[] buttons = new JButton[9];
for (int i = 0; i < 9; i++) {
buttons[i] = new JButton(\"\");
box.add(buttons[i]);
}

// instantiate a tic-tac-toe game object
final TicTacToe game = new TicTacToe(buttons);

// add an action listener to the new-game buttom so that it clears the game on click.
ActionListener WhenHitTheButton = new ActionListener() {
public void actionPerformed(ActionEvent e) {
game.clear();
}
};
newGame.addActionListener(WhenHitTheButton);

// add game panel (north) and box panel (center) to the frame and set it visible
frame.add(gamePanel, BorderLayout.NORTH);
frame.add(box, BorderLayout.CENTER);

frame.setVisible(true);
}
}

class TicTacToe {

int[] data = new int[9];
JButton[] buttons;
boolean isX = true;
// this variable keeps track of how many steps the game has played (it cannot be >= 9)
int steps = 0;
// use the flag to indicate whether the game has ended due to that X wins or O wins but not due to a draw
boolean gameEnded = true;

ImageIcon icon_x, icon_o;

// save buttons, load icon images, and clear the game (by calling \"clear\" method)
TicTacToe(JButton[] buttons) throws IOException {
this.buttons = buttons;

//resizing image
int WIDTH = 100;
int HEIGHT = 100;
  
icon_x = new ImageIcon(ImageIO.read(getClass().getResource(\"x.png\")).getScaledInstance( WIDTH, HEIGHT, java.awt.Image.SCALE_SMOOTH ));
icon_o = new ImageIcon(ImageIO.read(getClass().getResource(\"o.png\")).getScaledInstance( WIDTH, HEIGHT, java.awt.Image.SCALE_SMOOTH ));
clear();
}

// add the same listener to all buttons
void addListener() {
// TODO
for(JButton b:buttons){
b.addActionListener(buttonListener);
}
}
// create an action listener
ActionListener buttonListener = new ActionListener() {
// 1. Use the event source to find out which button is clicked
// 2. Find out if anyone wins and if so, has it X or O player won
// 3. In the console,
// if X wins, print \"X wins\",
// if O wins, print \"O wins\",
// if it is a draw (max number of steps has reached), print \"Draw\"
// if nobody wins, do nothing and continue the game
// 4. Don\'t forget to increment the \"steps\" variable to find out whether max number of steps has reached.
// 5. Switch player in this method as well if it is a legal move.
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
// TODO
boolean move = false;
  
for(int i=0;i<9;i++){
if(src == buttons[i]){
//top left
move = play(i, isX);
break;
}
}
  
  
if(move){
steps++;
isX = !isX;
  
int what = win();
  
switch(what){
case 0:{
System.out.println(\"Draw\");
removeListener();
break;
}
case 1:{
System.out.println(\"X wins\");
removeListener();

break;
}
case 2:{
System.out.println(\"O wins\");
removeListener();
break;
}
}
}
  
}
};

// reset \"steps\" to 0
// for each button, set the background color to \"white\" and remove icons from buttons \"setIcon(null)\"
// reset \"data\" array to 0
// if game has ended (due to someone winnning), add listener back to buttons and reset the flag to false
void clear() {
// TODO
steps = 0;
Arrays.fill(data, -1);
  
for(JButton b:buttons){
b.setIcon(null);
b.setBackground(Color.WHITE);
}
isX = true;
  
addListener();
gameEnded = false;
}

// remove listener from all buttons and set \"gameEnded\" flag to true
void removeListener() {
// TODO
for(JButton b:buttons){
b.addActionListener(null);
}
gameEnded = true;
}

// put X or O at i
// if a button click is legal (i.e. the button was no clicked before), set icons to the button and update \"data\" array accordingly
// return true if and only if the button click is legal
boolean play(int i, boolean isX) {
// TODO
if(gameEnded)
return false;
  
if(data[i] != -1) //-1 means not clicked
return false;
  
if(isX){
buttons[i].setIcon(icon_x);
data[i] = 1;
}
else{
buttons[i].setIcon(icon_o);
data[i] = 0;
}
  
return true;
}
// if X wins, return 1,
// if O wins, return 2,
// if draws or no result, return 0
//
// if someone wins, the remove listeners so that subsequent clicks won\'t change the remaining unclicked buttons

int win() {
// TODO
if(steps == 9)
return 0;
  
for(int i=1;i<4;i++){
int c = checkRow(i);
if(c != 0){
return c;
}
}
  
for(int i=1;i<4;i++){
int c = checkColumn(i);
if(c != 0){
return c;
}
}
  
int ch = checkDiagonal();
if( ch != 0)
return ch;
  
return -1;
}
// check whether the ith row matches
// if so, set the background for that row of buttons to blue color
// and also return 1 if X wins, 2 if O wins, 0 if nobody wins

int checkRow(int i) {
// TODO
switch(i){
case 1:{
  
if(data[0] != -1 && data[0] == data[1] && data[1] == data[2]){
setBackground(new int[]{0,1,2});
  
if(data[0] == 1)
return 1;
if(data[0] == 0)
return 2;
}
  
break;
}
case 2:{
if(data[3] != -1 && data[3] == data[4] && data[4] == data[5]){
setBackground(new int[]{3,4,5});
  
if(data[3] == 1)
return 1;
if(data[3] == 0)
return 2;
}
break;
}
case 3:{
if(data[6] != -1 && data[6] == data[7] && data[7] == data[8]){
setBackground(new int[]{6,7,8});
  
if(data[6] == 1)
return 1;
if(data[6] == 0)
return 2;
}
break;
}
}
return 0;
}
// check whether the ith column matches
// if so, set the background for that column of buttons to blue color
// and also return 1 if X wins, 2 if O wins, 0 if nobody wins

int checkColumn(int i) {
// TODO
switch(i){
case 1:{
  
if(data[0] != -1 && data[0] == data[3] && data[3] == data[6]){
setBackground(new int[]{0,3,6});
  
if(data[0] == 1)
return 1;
if(data[0] == 0)
return 2;
}
  
break;
}
case 2:{
if(data[1] != -1 && data[1] == data[4] && data[4] == data[7]){
setBackground(new int[]{1,4,7});
  
if(data[1] == 1)
return 1;
if(data[1] == 0)
return 2;
}
break;
}
case 3:{
if(data[2] != -1 && data[2] == data[5] && data[5] == data[8]){
setBackground(new int[]{2,5,8});
  
if(data[2] == 1)
return 1;
if(data[2] == 0)
return 2;
}
break;
}
}
return 0;
}
  
// check whether a diagonal matches
// if so, set the background for that diagonal of buttons to blue color
// and also return 1 if X wins, 2 if O wins, 0 if nobody wins
// note that there are two diagonals to check

int checkDiagonal() {
// TODO
if(data[0] != -1 && data[0] == data[4] && data[4] == data[8]){
setBackground(new int[]{0,4,8});
  
if(data[0] == 1)
return 1;
if(data[0] == 0)
return 2;
}else if(data[2] != -1 && data[2] == data[4] && data[4] == data[6]){
setBackground(new int[]{2,4,6});

if(data[2] == 1)
return 1;
if(data[2] == 0)
return 2;
}
return 0;
}

// set buttons at the specified indices to blue

void setBackground(int[] indices) {
for(int i=0;i<indices.length;i++){
buttons[indices[i]].setBackground(Color.blue);
}
}
}

Hwk10.java package hwk10; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.GridLayout; import java.awt.event.Acti
Hwk10.java package hwk10; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.GridLayout; import java.awt.event.Acti
Hwk10.java package hwk10; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.GridLayout; import java.awt.event.Acti
Hwk10.java package hwk10; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.GridLayout; import java.awt.event.Acti
Hwk10.java package hwk10; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.GridLayout; import java.awt.event.Acti
Hwk10.java package hwk10; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.GridLayout; import java.awt.event.Acti
Hwk10.java package hwk10; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.GridLayout; import java.awt.event.Acti
Hwk10.java package hwk10; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.GridLayout; import java.awt.event.Acti
Hwk10.java package hwk10; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.GridLayout; import java.awt.event.Acti
Hwk10.java package hwk10; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.GridLayout; import java.awt.event.Acti
Hwk10.java package hwk10; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.GridLayout; import java.awt.event.Acti

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site