In this final programming project, you will write a java applet tic-tac-toe game. This game will have an artificial intelligence (AI) so that a human player can play against the computer. I have given you the starting program file TicTacToe.java where you will have to fill in the missing pieces. The program I have given you has some functionality, make sure you read the comments and understand which pieces you can change and which pieces you should not change. You MUST read paint() method to understand the game logic.
This is a very hard program and I recommend starting simple and then adding more complicated features. Particularly, you are strongly recommended to start IMMEDIATELY!
Tic-tac-toe (or Noughts and crosses, Xs and Os) is a game for two players. Each player take turns marking the spaces in a 3x3 grid. The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal row wins the game. You can read the wikipedia page to learn more about the game.
Also, you must play the games by yourself to understand the details. I have made three versions for your to play with (and eventually you have to implement them by yourself). The three versions are: random CPU player, smart CPU player, and smart CPU player with first-move.
Special Topic 1: Mouse Clicking
You have been given method mouseDown() that detects mouse clicks and gets xx and y as the location of the mouse click. x and y will range from 0 to the size of the window. You will be using the Graphics class to display your game board. The window is 600 x 600 with (0,0) in the upper left corner. You should think of the board as a visual array with gameBoard as the top left corner and each square is 200 x 200. To determine which square the user has selected you will need to convert the location of x and y to your array indices in setMouseSquare() method. There is a very easy way to do this without using if statements or switch statements. You will need to write a method that determines the current index location the user has selected.
Special Topic 2: Game Display
Once you know which index the user has selected you will need to display a circle (or whatever shape you want) in the correct square on the board. You have had the experience of drawing circles in Lab 3 and Lab 5 (displaying Smiley). You can determine the location of your shape by multiplying and adding your current index by numbers. You will need to write a method that determins the exact position to draw circles.
Special Topic 3: Generating Random Numbers
The basic version of your AI will generate a random move (as long as it is legal). You can check the example here: random CPU player.
A random integer can be generated by a random number generator. You can use it like this to get a random integer in the range from 0 to N-1:
int N; // You can set its value
Random generator = new Random();
int randomNum = generator.nextInt(N);
You need to use the random number to make a legal random move. There are many ways to do it.
Special Topic 4: Smart Moves
The advanced version of your AI will generate a smart move. You can check the example here: smart CPU player. Your grade in this part will be determined according to the "smartness" of your program. At least, your program should be able to:
There are more rules that you can think of. It is possible to write a perfect AI that never lose. If you want to find a good opponent to test your program, use this example to play against your AI: smart CPU player with first-move
Special Topic 5: Extra Points
You receive extra 10 points if your CPUMove() method can work in CPU-first-move manner. You can change the paint() method to other names and change the CPUFirstpaint() method to paint() to let the computer player move first. Ideally, you don't have to write new strategies -- try to make the decision based on the current board instead of the current move.
Moreover, I am planning a tournament for you to play against each other. The current planned extra points are: 3/2/1 points on the final grade to the champion/runner-up/third-place. I will decide the organization of the tournament (league/knock-off, online/offline, etc.) depending on the number of competitors. Write me an email to register before Tuesday, April 23.
Milestones and Submission Requirements
How to hand in the assignment