Noughts and Crosses
C++ Noughts and Crosses
Hello! I created this noughts and crosses game in my first year at university. apologises my memory of this project is a little foggy since it has been a while since I created it. Never the less I want to try and explain the process of making the game. please note the screens shots shared are from the final project and not the step by step process.
The Beginning
When given the assignment, The class and I were given a worksheet that we could follow to get a basic noughts and crosses console UI and Input commands for placing either noughts and crosses in the area chosen by the player.
Our Task Was to create the winning conditions for when a player get a row of their chosen symbol and to create a simple menu where the player can decide to play a multiplayer match or learn how to play the game.
Once the win condition and the menu were implemented I wasn't happy with the final result. I thought to myself it would be more fun to add a single player mode against a AI, Being very new to programming and the C++ language I knew creating a AI was going to be a challenging task but I was pretty stubborn to prove to myself I could achieve the goal.
Creating AI
I started easy. I thought to myself I just wanted a AI that can take a turn, I wasn't bothered whether or not the AI could actually win a game.
First I created a Enum following the same string letters the players use when playing a single game. at the time this just made it easier for me to think of the logic for the AI
After Creating the Enum I made it once it was the AI turn it would select random element from the Array and use the result to select a square to fill.
A bug a quickly realised when creating this approach The AI didn't understand when a square was filled and kept trying to fill in a square on the grid that has already been allocated by either the player or the AI itself
To fix the Issue I made the AI check if the square on the grid was filled and if true to check the input again which would make the AI redo their move until it found a square that was available. This approach worked and the AI was functional. however it was very easy to beat and the it just was more luck based if the AI blocked the player. I wanted to give the player a option between a more challenging AI.
The Advance AI is more on the messy side of coding so apologies but here we go.
Yeah a curse of the if statements but at the time it was the only solution I could think of. The AI Checks where the player has filled placed their counters on the grid and prioritises blocking the player instead of finding a win condition. This limited the amount of moves the players can take if they wanted the game not to end in a tie. However if the AI believes it has the chance to win it will also choose the winning condition over blocking the player.
This created a much greater challenge for the player but I wanted to go one step further! I ended up creating one final mode called the 3 lives mode for players who wanted a real challenge.
Three lives mode only lets the player have three moves to win the game against the Advanced AI. This game mode is actually beatable but is hard since the AI prioritises blocking the players win condition. I won't lie when I say I don't remember what makes the AI slip up but I'd say every 1/10 games I played with the AI sometimes the AI will decide not to block. I would say it was luck or a glitch but there is one move set that made this makes it winnable.
Comments
Post a Comment