Big Java Late Objects
Big Java Late Objects
2nd Edition
ISBN: 9781119330455
Author: Horstmann
Publisher: WILEY
Question
Book Icon
Chapter 11, Problem 11PP
Program Plan Intro

Tic Tac Toe

Program plan:

Filename: “TicTacToeComponent.java”

This program file is used to define a class “TicTacToeComponent”. In the code,

  • Import the required packages.
  • Define a class “TicTacToeComponent”.
    • Declare class members “board”, “squareDim”, “leftPad”, “topPad”, “MARK_PAD”.
    • Define a constructor “TicTacToeComponent()”.
      • Initialize the class members.
    • Define a method “paintComponent()”.
      • Draw board.
    • Define a method “drawMark()”.
      • If “mark” is “X”,
        • Draw “line1” and “line2”.
          • Else, if the “mark” is “O”,
            • Draw circle.

Filename: “TicTacToeBoard.java”

This program file is used to define a class “TicTacToeBoard”. In the code,

  • Define a class “TicTacToeBoard”.
    • Declare the class members “BLANK”, “X_MARK”, “O_MARK” and “board”.
    • Define the constructor “TicTacToeBoard()”.
      • Iterates for loops,
        • Initialize “board[][]”.
    • Define a method “availableSquare()”.
      • Return the result of expression.
    • Define a method “determineWinner()”.
      • Iterate for loop,
        • If the condition is true,
          • Return the element “board[row][0]”.
      • Iterate for loop,
        • If the condition is true,
          • Return the element “board[0][col]”.
      • If the condition is true,
        • Return element “board[0][0]”.
      • If the condition is true,
        • Return element “board[0][2]”
    • Return “BLANK”.
    • Define a method “getMark()”.
      • If the condition is true,
        • Return the element “board[row][col]”.
      • Otherwise,
        • Return “BLANK”.
    • Define a method “setMark()”.
      • If the condition is true,
        • Set “mark” to “board[row][col]”.
    • Define a method “reset()”.
      • Iterate for loops,
        • Set “BLANK” to “board[row][col]”.
    • Define a method “validMark()”.
      • Return the truth value of expression.

Filename: “TicTacToeFrame.java”

This program file is used to define a class “TicTacToeFrame”. In the code,

  • Define a class “TicTacToeFrame”.
    • Define the class members.
    • Define a constructor “TicTacToeFrame()”.
      • Set a variable “done” as “false”.
      • Define a “board”.
      • Define an object of “BoxLayout”.
      • Set the layout of “panel”.
      • Set the “player”.
      • Define a label and set its text as “X's turn”.
      • Create “component”.
      • Create a “JButton” and set its text as “Reset Game”.
      • Define the object of “MousePressedListener” named “listener”.
      • Add mouse listener to “component”.
      • Define a panel “panel2”.
      • Add button to “panel2”.
      • Add “turnLabel” to “panel2”.
      • Add “panel2” to “panel1”.
      • Add “panel1” to the layout.
      • Set the size of frame.
    • Define a method “selectSquare()”.
      • Call “setMark()” using “board”.
    • Define a method “computeRow()”.
      • If the condition is “true”,
        • Return “-1”.
          • Return the truth value of expression.
    • Define a method “computeCol()”.
      • If the condition is “true”,
        • Return “-1”.
          • Return the truth value of expression.
    • Define a method “reset()”.
      • Call the method “reset()” using “board”.
      • Set the “player”.
      • Call the method “setTurnLabel()”.
      • Set “done” equal to false.
    • Define a method “switchPlayer()”.
      • If “player” equal to “X_PLAYER”.
        • Set “player” equal to “O_PLAYER”.
          • Else,
            • Set “player” equal to “X_PLAYER”.
          • Call the method “setTurnLabel()”.
    • Define a method “setWinner()”.
      • If “player” equal to “X_PLAYER”.
        • Set text of label as “X wins!”.
          • Else,
            • Set text of label as “O wins!”.
          • Set “done” equal to “true”.
    • Define a method “setTurnLabel()”.
      • If “player” equal to “X_PLAYER”.
        • Set text of label as “X’s turn”.
          • Else,
            • Set text of label as “O’s turn”.
    • Define a method “getMark()”.
      • If “player” equal to “X_PLAYER”.
        • Return the value.
          • Else,
            • Return the value.
    • Define a class “MousePressedListener”.’
      • Define a constructor “MousePressedListener()”.
        • Set “mouseX”.
        • Set “mouseY”.
        • Set “row”.
        • Set “col”.
        • If “true”,
          • Call “selectSquare()”.
          • If the condition is “true”.
            • Call “setWinner()”.
          • Otherwise,
            • Call “switchPlayer()”.
              • Call “repaint()”.
      • Declare do nothing methods named “mouseReleased()”, “mouseClicked()”, “mouseEntered()”, “mouseExited()”.
    • Define a class “ResetListener”.
      • Define method “actionPerformed()”.
        • Call “reset()”.
        • Call “repaint()”.

Filename: “TicTacToe.java”

This program file is used to define a class “TicTacToe”. In the code,

  • Import the required packages.
  • Define a class “TicTacToe”.
    • Define the method “main()”.
      • Define the object of “JFrame”.
      • Set the title of JFrame as “Tic-Tac-Toe”.
      • Set default close.
      • Set the frame as visible.

Blurred answer
Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education