Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment: Create an object-oriented program that will simulate the game of tic-tac-toe. You will be creating two files for this assignment: Board.java and Game.java. public

Assignment:Create an object-oriented program that will simulate the game of tic-tac-toe. You will be creating two files for this assignment:Board.javaandGame.java.public class Board{} will define an object representing a tic-tac-toe board (a two-dimensional String or character array), whilepublic class Game{} will simulate the game of tic-tac-toe, using an instance of theBoard class. In the game of tic-tac-toe, two players take turns marking an available spot in a 3x3 gameboard that we will represent as:

1 2 3

4 5 6

7 8 9

Players alternate turns placing their tokens (either X or O) on the board.When one player has placed three tokens in a horizontal, vertical or diagonal line on the game board, the game is over and that player has won. A draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. For example, the following board is an example of a draw because no one placed three of the same tokens in a horizontal, vertical or diagonal line:

O X O

X O O

X O X

You should perform the following in your code:

  • Withinpublic class Board{}, you should define (at the minimum):
    • A two-dimensional array to represent your board. I suggest using a 2D String array so you can put numbers (1-9) and letters (X, O) in the array without any type mismatches.
    • public Board(): A constructor to initialize the gameboard to hold values 1-9
    • public void printBoard(): a method to display the current game board
    • public void changePosition(char pos, char play): Places play (either "X" or "O" at the numbered position passed in)
    • public boolean checkWin(): a method to determine if there is a win in the board (or a draw if the board is full with no winners)
    • public boolean checkSpace(char pos)Method used to determine if the position (space #) passed in is valid (i.e. an integer value, 1-9, and has not been set to "X" or "O" yet.
  • Withinpublic class Game{}, this is where the game logic will be handled, and input will be accepted from the user. Be sure to:
    • Begin by creating an instance of the Board class (Board b = new Board();)
    • Using the board object, display the 3x3 game board showing the numbers 1-9 (see figure above).
      • b.printBoard();
    • Prompt the first player to enter the placement of the O token.
      • You may ask the user for an integer between 1-9 or you can ask them for a row and column number (remember that array indices start at 0 and stop at length-1)
      • Validate the entered position number. Is the position number out of range? Is an X or O already in the position number given by one of the players?You do NOT have to check that the user provides an integer but doing this would be good practice.
      • If an invalid position is entered, tell the user of the error and allow them to enter again.
    • Next, display the 3x3 game board showing the placement of the O on the board.
      • For example, if a user chooses position (1,1) or 5, the board should display:

1 2 3

4 O 6

7 8 9

  • Subsequently, prompt the second player to enter the placement of the X token.
    • Again check for valid entries from the user.
  • Display the 3x3 game board showing the placement of the X on the board.
  • Continue alternating the prompting of the X and O token until there is a winner or a draw.
    • You must display a meaningful message in either of these cases.
  • Youmust use methods in both files, and all methods must be documented using pre/post conditions.

You must determine the design of the main program and no solution code is provided.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

One federal and state statute utilizing standard legal notation

Answered: 1 week ago