You will build an application that allows a person to play tic-tac-toe against the computer. Your application will print out the board, ask the user to select a spot, update the spot selected, and make a move on its own. This will continue until the game ends (3 sequential spots having the same mark in the same row, column, diagonal, or if no more moves are possible due to the lack of vacant spots). Your application will also do limited error checking to ensure occupied spots won't be overwritten with new values.

Assignment

  • Create an Outlab5 project and paste this into a Driver. You won't need to change the Driver.
  • Read through the Driver and understand the flow of how this program will execute.
  • Here are some specifications and assumptions for you:
    • The board will always be a standard 3-by-3 tic-tac-toe board. No need to have your code generalizable to other sizes.
    • No player is allowed to mark an already marked spot. If the human tries to do that, an error message must be printed and the player is to be prompted for another spot until they enter a valid one (however many times it takes).
    • No other error checking on user input needs to occur. Assume that the user will only type in row and columns that are valid (0-2).
    • The computer move method will randomly select a spot for the computer to place its mark. The spot to be marked must not already be occupied (if it is, just randomly select a new spot).
    • The printWinner() method does not need to say which player (computer or human) won, just the mark that won (X or O).
    • Here is output when the computer wins, human wins, there is a tie, and where occupied spots are attempted.

Hints

  • You won't be able to give this lab a good honest effort until we talk about multi-dimensional arrays on 24 Oct. Before then you can still map out how you expect objects to interact.
  • This is a hard lab and will take many hours. This is why you have 3 weeks to work on it, and it is worth 10% of your total class grade. Take it seriously. It WILL take you a lot of time. My solution has close to 300 lines of code.
  • You will probably jump back and forth between the two classes, but to get you started, kick things off with the Board class. Think you need something in the Board class to represent the actual board? The point of the constructor is to give things an initial condition. Can you think of a good default initial condition for a tic-tac-toe board? What should the spots be filled with? Do you mean literally nothing (null), or do you mean an empty space (" ")?
  • After you make the Board constructor (and maybe an instance variable...), work on the print method. It will be hard and will take you some time, but will be good practice dealing with 2-d arrays in preparation for the rest of the assignment.
  • After you have a 2-d array working and printing, think about the rest of the assignment. In English, what do you need to check to see if the game is over? I check four different things (1 - Do the rows have a winner? 2 - Do the columns have a winner? ...). Perhaps each thing you check, should be its own method.
  • Start with plain English when building your methods: How should the move methods operate? Generate (randomly or through input) row and column values. If that spot is empty, place the mark. If not...
  • My Player class has only two non-constructor methods, and they are both called from the Driver. My Board class has nine non-constructor methods and some are only called from insde the Board class, some are called from the Driver, and some are called from the Player class. It is important to be organized when building this program.
  • Think about some lessons from previous labs. Which class should be responsible for reading input when the human wants to make a move? Which class should be responsible for changing the board? Perhaps I need a method in the board class that will enable Players to check whether or not a space is vacant, or maybe another method that will enable a Player to add a mark to the board.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.