Create a Tic-Tac-Toe game
Tic-Tac-Toe is a pretty easy to learn and fun to play game. One player uses Xs and the player Os to try and get three of their letters in a row. In this activity, you will create a tic-tac-toe game.
Turn In: Code to play tic-tac-toe
Your completed project will be a game that allows you to play tic-tac-toe.
If you have not played tic-tac-toe in a while, play a few games with a classmate or family member. As you play, think the things that are happening in the game, the strategy, and how you might code all of that.
One of the key decisions before starting to code is how are you going to represent the tic-tac-toe board in your game. Your game will need to be able to figure out if someone has already selected that location, place the X or O, determine if there is a winner, and determine if all of the spots on the board are filled.
While you could try to sit down and write the entire game at once, a more prudent approach is to break down the game in parts and build the game one piece at a time, testing out your game as you build it. This allows you to code then test your code and once that part of the game is working properly, then you can move on to the next part of the game.
If you are familiar with using subprograms (i.e. functions) or using object oriented programming, that would be an excellent way to manage the complexity of this project and build it in an interactive way (i.e. one piece at a time).
The first recommended task is to write the code to display your tic-tac-toe board. You should have decided how you are going to store the X and O moves and put a few sample X and O characters on your board so that when you display it, you can verify that they are in the correct position. For instance, a X in the middle spot and an O in both the lower left corner and the upper right corner of the board.
Once you have your board displaying properly, then write the code that asks a player for the position of their X (or O) and make sure it stores it in the correct location.
Note: Coding environments that are character based will look different than a graphical based environment and will work a little different than a web environment. For instance, you may be typing in a position to place your letter or you may click on the board to do so.
Winner or Game Board Filled
The game is over when either one player wins or the game board is filled. You may want to next write the code to do those checks.
You may want to start by writing a function or method called something like BoardFull that returns a true if all of the spots on the board are taken and false if they are not all taken. Test the code but putting values in all of the spots on your board and also but not having them all full and make sure you can get correct true/false value back.
Next, you need to determine if you have a winner. The code to check whether X or O wins is the same, the only difference is the letter you are checking. A winner can be three of the same letter in a row (left to right), a column (up to down), or diagonal from upper right to lower left or from upper left to lower right. Your function or method will return a true if you have a winner and false if you do not.
As with the BoardFull, test the winner check code for all possible winning combinations and also some that are not winners.
With the pieces in place to display the board, store a letter in the place the user indicates, determine if you have a winner, and determine if the board is full, next is the processing of Player 1 and Player 2. The program will need to determine who is an X and who is an O, then alternate back and forth between them. For each turn the player is asked for the location they want to move and then their letter is placed in that location. Be sure to check and make sure that there is not currently a letter in that location.
The processing should continue until either all of the spots are filled in or one of the players win.
At this point, you should have a functional tic-tac-toe game that two people can play.
Be sure to test a number of different scenerios including X wins, O wins, neither win, and wins in all directions (horizontal, vertical, and diagonal). For your testing, involve friends/family who don't know how to code and show them what you have been working on.
Optional - Play Against the Machine
For an additional challenge, you can adjust your code to allow the user to pick playing against the computer or against another real player. If they choose to play against the computer then you can give them the option of easy or difficult level.
With the easy level, the computer would randomly choose a move from the available moves.
For the difficult level, you would code in some strategy on moves to make to try and win. The strategy should be similar to what you would use if you were playing.
There should be minimal changes needed to your existing code. The primary change is instead of asking the other player to put in the location of their next move, you will be calling a method or function for that information. First, you will need to wrote the code to chose the location that the computer will move when it is the computer's turn.
Copyright © 2021 Eric Schumm. Permission granted to freely use this in your classroom.