This lab is due at 11:59 pm on Wednesday, October 11.
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. Try not to write it all at once - 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 will probably call and use other methods. 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. It will be randomly determined who goes first. The computer should make moves at random (it should just pick a random unoccupied spot - for extra credit, you can create some logic for the computer player, see below for details). When it is the user's turn, they can pick any unoccupied spot to place their mark. Make sure that the board is printed out before each move so the user can see which spots are empty.
Here are some tips if you would like:
You may find it useful to create an outline before beginning. Here is an example outline one of my students made recently:
//Jason Bunkell's outline for TTT:
*-------Pre Game-------
*0: Set Up Board
*1: Pick symbol for player and computer (x or o). If you like, you can choose to always have player be x and computer be o or vice versa
*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 is a tie
* -----Display Results-----
* 9a: Winner
* 9b: Tie
Here are some more tips/thoughts that may or may not help:
-represent the board using a 3x3 array, probably using chars or 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.
-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:
-have 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)
-have 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.
-have a method called computerMove which will input the board, randomly determine the computer's move based on which spaces are open, updates the board, and returns the changed board.
-have 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 pass all 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.
Submit TicTacToe.java
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.