Create a program that allows two users to play tic tac toe
Part A: Plan Your Project
Stop, think, and write down some notes
You're going to have to plan out how to organize this. What sort of methods do you think you'll need? What constants? What variables?
You can assume this game will always be played on a grid that is exactly 3x3.
Don't forget to separate your program's logic (update) from its display stuff (render) like we did in previous projects. It will save you from a lot of confusion, bugs, and wasted time.
If you're stuck, look at the tasks below. I've offered a suggested order to do things in. You can use those to start getting a picture of what you'll need. You can also look back at Puzzle. This project is similar to that one in a lot of ways, except in two dimensions.
If you ask for help on this project, you will first be asked to show the paper you used to plan out the project. If you don't do this step, you're on your own.
Draw the lines and some basic data in each position
- Create a two dimensional array
- Fill it with some default value like "z"
- Try printing it out to the screen in a grid pattern using two loops
- You can draw borders between them using either 4 lines or 9 boxes with transparent fill but a stroke that stands out.
Allow the user to click on the grid to play the game
- When a user clicks in an area, make it change to an "x"
- This is basically the same as light puzzle, but with two dimensions.
Alternate between two players
- Make it so that every other click alternates between "x" and "o"
- It can start on "x" or "o" or randomly choose.
- To do this, there are different ways to do so. Two ideas are:
- Count the number of clicks and check for even/odd with modulus
- Create a boolean variable and change its value
- Use a char or string to store the current letter you're placing
Check if the player wins
- A player wins if any one of these four conditions are true
- They have three in a horizontal row
- They have three in a vertical column
- They have three in a downwards sloping diagonal
- They have three in a upwards sloping diagonal
- You may want to write four conditions to check for each of those cases and connect them with OR operators.
- You're allowed to assume that this game will always be a 3x3 and hardcode these conditions if you're uncomfortable with using nested for loops in each case.
- Don't try and do this all at once then run it.
- Write a single win condition (horizontal, for instance) and test for it. Print to the console if it works.
- Repeat with the other three.
- Then join them together and test your program a few times with different cases.
- Finally, if that works go ahead and add text output to the screen in place of the print statements.