This lab is due at 11:59 pm on Monday, December 8th. This will be your final lab of the semester. Make sure you are working at home in addition to the six days we will have in class. Since this is due so close to finals, I will not be accepting any late assignments or revisions for this lab. Please make sure your projected is completed on time.
You are going to write a program (in one file named TicTacToe.java) that represents a Tic Tac Toe game. You may write the program however you like, although I have some tips down below if you want a starting point. This is a big program. Break it down into small chunks and think about how you want to organize it.
You should write the actual code to play the game in a main() method, but that method should call and use other methods (and perhaps, other Classes if you like). You may write and use whatever other methods you would like. I will be testing your program by playing your game and running main.
The user will play against a computer. The computer should make moves at random (it should just pick a random unoccupied spot - for extra credit, you can create a computer player that will play perfectly and never lose, see below for details). When it is the user's turn, they can pick any unoccupied spot to place their mark.
Here are some other details you should include in your program:
Your game will randomly determine who goes first.
The player should be able to choose if they would like to be "x" or "o".
On the player's turn, if they try to type in a spot which is already occupied or not part of the grid, it should ask them to type in another spot until they pick one which is available (use a while loop).
Make sure that the board is printed out before and after each move so the user can see which spots are empty.
After a game is complete, your program should loop around again and ask the user if they would like to play another round. It should keep track of how many wins, losses and ties and print them out after each game (add this feature at the end after you know a single game works properly).
You may find it useful to create an outline before beginning. Here is an example outline one of my students made a few years ago:
//Jason Bunkell's outline for TTT:
*-------Pre Game-------
*0: Set Up Board
*1: Pick symbol for player and computer (x or o).
*2: Randomly choose who goes first
*------Main Loop Starts------
*3a:If user's turn, ask where the user wants to play
*3b:If computer's turn, pick a random row and column
*4:Check if chosen space is available. If not, repeat step 3.
*5:Place the x or o. Print the board.
*6:Switch players after move
Check to see if game is over in steps 7-8 (if not, return to step 3)
*------Game Ending------
*7: Three In a Row wins, check all possibilities
*8: No More Spaces (and no three in a row) is a tie
* -----Display Results-----
* 9a: Winner, Loser, Tie
* 9b: Add one to appropriate count. Ask user if they would like to continue.
Here are some more tips/thoughts that may or may not help:
-represent the board using a 3x3 array, probably using Strings to represent whether each location is "x", "o" or " " [a blank space]. Create this array in main and pass it along to different methods as an input to those methods.
-In main, have a while loop where each time through the loop represents one turn. This loop should check whether either player has won the game after a move is placed.
-run the game through main but also write several different other methods to split your program into parts. Here are some suggestions for these methods:
-write a void method called printScreen which will input a 2d array for the board and then print the game board out to the user (however you decide you want to print it)
-write a method called checkWinner that will input the board and determine whether either player (or neither) has won the game (and then return the winner). Call this after each move by each player.
-write a method called computerMove which will input the board, randomly determine the computer's move based on which spaces are open, update the board, and return the changed board.
-write a method called playerMove that inputs the board, has the user type in their move (using scanner), updates the board, and returns the changed board.
-remember that you have to may need to pass some variables (like the board) from method to method as inputs
-think carefully about how you want the user to input their moves. You could have them type in row and column, number the locations 1 through 9, have them type "top middle" or whatever, it is up to you! Just make it clear in printlns how the user is supposed to enter their move.
When you finish, test your code several possible ways! For example, try to let the computer win and make sure that works. Try to tie and make sure that works. Try to win in a couple different ways and make sure they all work.
Have fun!
Submit TicTacToe.java (and any other classes you make)
FOR EXTRA CREDIT: Program an AI in tic-tac-toe that, instead of playing randomly, can beat/tie me every time... IF you are up to it.