Create a java Swing GUI application in a new Net beans proje

Create a java Swing GUI application in a new Net beans project called Tic Tac Toe GUI. Your project will have a TicTacToeFrame.java class and a java main class: TicTacToeRunner.java. The game is the same in every respect to the previous lab from CP I (where we refactored Tic Tac Toe console code from the text) except that there is now a GUI to handle the display of the game state and the input from the user. Starting with X each player alternates making a move by clicking on a square. The game blocks until a legal move is entered and then switches to receive input from the other player. The game should check for wins after each move starting with the 5^th and should check for a tie (7^th move). Use J Option Pane to mgs the user as needed for illegal moves and when the game is won or tied, or the user quits. Do not use any console (System.out.print...) output as this is a GUI program. Use grid layout to create a 3 X 3 matrix of J Button objects for the Tic Tac Toe board. Provide a quit button as well. use an Array of J Button objects for the squares. IF you do this in a clever way as a 2D array, it will interface nicely with your previous code for the game. I suggest that you sub-class the J Button to create a Tic Tac Toe Button class that holds the state of the button. You also want to create a single listener instance for all the Buttons on the board. It should determine the row col position of the Button and interface with the code for the game logic.

Solution

//TicTacToeFrame.java

import java.awt.*;
import java.awt.event.*;

class TicTacToeFrame extends Frame implements ActionListener {
   public TicTacToeFrame(){
       final int width = 200, height = 200;
       setSize(width,height);
       setBackground(Color.white);

       setLayout(new GridLayout(rows,cols));

       Button[][] board = getBoard();
       for(int r = 0; r < rows; r++){
           for(int c = 0; c < cols; c++){
               Button square = new Button(\"\");
               square.setActionCommand(String.valueOf(r*cols+c));
               square.addActionListener(this);
               board[r][c] = square;
               add(square);
           }
       }
   }
   public void actionPerformed(ActionEvent event){
       String command = event.getActionCommand();
       try{
           int value = Integer.parseInt(command);
           if((0 <= value) && (value < (rows*cols))){
               if(selectSquare(value)){
                   toggleSymbol();
               }
               else{
                   System.out.println(\"Square already taken.\");
               }
           }
       }
       catch(NumberFormatException e){
           System.out.println(\"Unknown command: \"+command);
       }
   }
   protected boolean selectSquare(int value){
       int row = value/cols;
       int col = value%cols;
       Button[][] board = getBoard();
       Button square = board[row][col];

       if(square.getLabel().equals(\"\")){
           square.setLabel(nextSymbol);
           repaint();
           return true;
       }
       else{
           return false;
       }
   }
   protected String getSymbol(){
       return nextSymbol;
   }
   protected void toggleSymbol(){
       if(nextSymbol == Nought){
           nextSymbol = Cross;
       }
       else{
           nextSymbol = Nought;
       }
   }
   protected Button[][] getBoard(){
       return board;
   }
   private static final int rows = 3, cols = 3;
   private static String Nought = \"O\", Cross = \"X\";
   private String nextSymbol = Nought;
   private Button[][] board = new Button[rows][cols];
}

//TicTacToeRunner.java

package p2.TicTacToeFrame

public class TicTacToeRunner {

   BoardGame game_;
   int homeCol_;
   int homeRow_;
   int col_ = 0;
   int row_ = 0;

   public TicTacToeRunner(BoardGame game, int col, int row)
   {
       game_ = game;
       homeCol_ = col;
       homeRow_ = row;
   }

   public int run(int dcol, int drow)
   {
       int score = 1;
       this.goHome();
       score += this.forwardRun(dcol, drow);
       this.goHome();
       dcol = -dcol;
       drow = -drow;
       score += this.forwardRun(dcol, drow);
       return score;
   }
  
   private void goHome() {
       col_= homeCol_;
       row_ = homeRow_;
   }
  
   private int forwardRun(int dcol, int drow)
   {
       this.move(dcol, drow);
       if (this.samePlayer())
           return 1 + this.forwardRun(dcol, drow);
       else
           return 0;
   }
  
   private void move(int dcol, int drow) {
       col_ = col_ + dcol;
       row_ = row_ + drow;
   }

   private boolean samePlayer() {
       if (game_.inRange(col_,row_)) {
           Player home = game_.get(homeCol_, homeRow_);
           Player here = game_.get(col_, row_);
           return home == here;
       } else {
           return false;
       }
   }
}

 Create a java Swing GUI application in a new Net beans project called Tic Tac Toe GUI. Your project will have a TicTacToeFrame.java class and a java main class
 Create a java Swing GUI application in a new Net beans project called Tic Tac Toe GUI. Your project will have a TicTacToeFrame.java class and a java main class
 Create a java Swing GUI application in a new Net beans project called Tic Tac Toe GUI. Your project will have a TicTacToeFrame.java class and a java main class

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site