Skip to content

Instantly share code, notes, and snippets.

@peterthorsteinson
Created October 8, 2019 03:09
Show Gist options
  • Save peterthorsteinson/ca6ef07ca8281f36b1f087d05e7f7177 to your computer and use it in GitHub Desktop.
Save peterthorsteinson/ca6ef07ca8281f36b1f087d05e7f7177 to your computer and use it in GitHub Desktop.
using System;
using System.Text;
namespace TicTacToe_OO // object oriented implementation of tic tac toe
{
class Program
{
static void Main()
{
while (true)
{
Console.WriteLine("\nTic Tac Toe");
Console.WriteLine("Please enter an integer from 1 to 9 to make a move, or 0 to quit game.\n");
Game game = new Game();
string result = game.Start();
Console.WriteLine("\nGame Over. Result: " + result + ".");
if (result == "Quit")
{
return;
}
}
}
}
class Game
{
public String Start()
{
Board board = new Board();
board.Display();
while (true) // each iteration of this loop is the placement of an "X" or a "O as a single move
{
int move; // must get integer 0 to 9, 0 means terminate game, other values are move positions
if (!int.TryParse(Console.ReadKey(true).KeyChar.ToString(), out move))
{
Console.WriteLine("\nPlease enter an integer from 1 to 9 to make a move, or 0 to quit game.");
continue;
}
if (move == 0)
{
return "Quit";
}
board.MakeMove(move); // move is 1 -> 9
string result = board.WhoIfAnyoneHasWon();
if (result == "Still in play")
{
continue; // continue playing game
}
return result;
}
}
}
class Board
{
char player = 'X'; // X always plays first move
char[] cells = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }; // 9 cells in 3x3 grid
int[] translate = { 1, 5, 9, 25, 29, 33, 49, 53, 57 }; // translate int to offset in display string
int moveCounter = 0; // must never go greater than 9 moves
public void Display()
{
Console.WriteLine();
StringBuilder sb = new StringBuilder(
" | | \n" + // 1, 5, 9 <- offsets in display string for indexes 1, 2, 3
"---+---+---\n" +
" | | \n" + // 25, 29, 33 <- offsets in display string for indexes 4, 5, 6
"---+---+---\n" +
" | | \n"); // 49, 53, 57 <- offsets in display string for indexes 7, 8, 9
for (int i = 0; i < 9; i++)
{
sb[translate[i]] = cells[i];
}
Console.WriteLine(sb);
}
public void MakeMove(int move) // move is integer from 1 to 9 inclusive
{
move = move - 1;
if (cells[move] != ' ') { Console.Beep(); return; } // cell must be open for move
cells[move] = player;
Display();
if (player == 'X') player = 'O'; else player = 'X'; // alternate current player after every move
moveCounter++;
}
public string WhoIfAnyoneHasWon() // returns "X", "O", "Draw" "Still in play";
{
// check to see if X won yet
if ((cells[0] == 'X' && cells[1] == 'X' && cells[2] == 'X') || // row 1
(cells[3] == 'X' && cells[4] == 'X' && cells[5] == 'X') || // row 2
(cells[6] == 'X' && cells[7] == 'X' && cells[8] == 'X') || // row 3
(cells[0] == 'X' && cells[3] == 'X' && cells[6] == 'X') || // col 1
(cells[1] == 'X' && cells[4] == 'X' && cells[7] == 'X') || // col 2
(cells[2] == 'X' && cells[5] == 'X' && cells[8] == 'X') || // col 3
(cells[0] == 'X' && cells[4] == 'X' && cells[8] == 'X') || // diag 1
(cells[6] == 'X' && cells[4] == 'X' && cells[2] == 'X')) // diag 2
return "X wins";
// check to see if O won yet
if ((cells[0] == 'O' && cells[1] == 'O' && cells[2] == 'O') || // row 1
(cells[3] == 'O' && cells[4] == 'O' && cells[5] == 'O') || // row 2
(cells[6] == 'O' && cells[7] == 'O' && cells[8] == 'O') || // row 3
(cells[1] == 'O' && cells[4] == 'O' && cells[7] == 'O') || // col 2
(cells[2] == 'O' && cells[5] == 'O' && cells[8] == 'O') || // col 3
(cells[0] == 'O' && cells[4] == 'O' && cells[8] == 'O') || // diag 1
(cells[6] == 'O' && cells[4] == 'O' && cells[2] == 'O')) // diag 2
return "O wins";
// if max number of completed moves is 9 and nobody has won yet then it is a draw
if (moveCounter == 9) return "Draw";
return "Still in play";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment