By using C# and Visual Studio, I decided to have some fun by coding a console version of the classic Tic Tac Toe game. Also known as Naughts and Crosses, Exey Oseys or Xs and Os. It's a fun game to code, below is an overview of the project file. Feel free to edit or improve the code for your own project!
Overview
It's a simple setup, there is char array that holds board numbers from 1 to 9. The player inputs the corresponding number where they would like to place their signature, and walla! This input will override the array number with the players signature. Once a signature has been assigned to the array it cannot be replaced.
Below has a global static array variable named arrBoard, and a public variable in BoardReset called arrBoardInitialize. This is an essential part of the game loop in ensuring the board is set up correctly each time the game resets. arrBoard has its variables overwritten with the player signatures, as the game resets, the player signatures are overwritten by arrBoardInitialize, essentially rebooting the game.
Introduction
This makes use of ASCII Art, a quick google search, and you'll find a few websites that let you type and customise the font. It's a bit fiddly but looks excellent afterwards. See image below for how it's implemented:
Rules 1
Rules 2
Board Layout
Board layout is pretty simple too but can be quite tricky depending on how you want it to look. It involves running the program to check how it looks, then going back and forward until you're happy with it.
Game Ends In A Draw
Calculates the turns through every loop cycle, if the number of turns is equal to 10, well it's a draw.
Win Horizontally
There are three possible outcomes for a horizontal win, by checking each one against the arrBoard and playerSignature variable, the program can determine if there is a winner, and also who the winner is. This is achieved by checking the players signature to see if it's equal to X or O. The same concepts apply to vertical and diagonal wins.
Win Vertically
Win Diagonally
The Code
Here's a copy of the code, again feel free to edit and tweak. Happy coding!