TizzyT

TicTacToe -TizzyT

Jul 13th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.49 KB | None | 0 0
  1. using System;
  2.  
  3. namespace TicTacToe
  4. {
  5.     internal static class Program
  6.     {
  7.         private static void Main(string[] args)
  8.         {
  9.             if (args == null)
  10.             {
  11.                 throw new ArgumentNullException(nameof(args));
  12.             }
  13.  
  14.             Game ttt = new Game();
  15.             while (true)
  16.             {
  17.                 ttt = new Game();
  18.                 ttt.Play();
  19.                 Console.WriteLine("    Press Enter To Start A New Game ...");
  20.                 Console.ReadLine();
  21.             }
  22.         }
  23.  
  24.         public class Game
  25.         {
  26.             private enum Player : sbyte
  27.             {
  28.                 None = 0,
  29.                 X = 10, //Human Player
  30.                 O = -10 //AI Player
  31.             }
  32.  
  33.             private readonly sbyte[][] board;
  34.             private readonly Random rnd;
  35.             private Player turn;
  36.  
  37.             //Creates a new game instance
  38.             public Game()
  39.             {
  40.                 //Clears the screen of any previous games
  41.                 Console.Clear();
  42.  
  43.                 //Choose who goes first
  44.                 rnd = new Random();
  45.                 turn = Player.X;
  46.                 if (rnd.Next(0, 2) == 1) turn = Player.O;
  47.  
  48.                 //Initializes the game board
  49.                 board = new sbyte[][] { new sbyte[]{ 7, 8, 9},
  50.                                         new sbyte[]{ 4, 5, 6},
  51.                                         new sbyte[]{ 1, 2, 3} };
  52.             }
  53.  
  54.             //Starts the game
  55.             public void Play()
  56.             {
  57.                 PrintBoard();
  58.                 int i = 0;
  59.                 for (; i < 9 && !CheckWinner(); i++)
  60.                 {
  61.                     PlayMove();
  62.                 }
  63.                 if (i == 9 && !CheckWinner()) DisplayWinner(Player.None);
  64.             }
  65.  
  66.             //Prints the entire board
  67.             public void PrintBoard()
  68.             {
  69.                 Console.SetCursorPosition(0, 0);
  70.                 Console.CursorVisible = false;
  71.                 Console.ForegroundColor = ConsoleColor.Blue;
  72.                 Console.WriteLine();
  73.  
  74.                 //Draws first row
  75.                 Console.Write("   "); DrawPiece(board[0][0]); Console.Write(" "); DrawBoard(BoardTypes.Vertical); Console.Write(" "); DrawPiece(board[0][1]); Console.Write(" "); DrawBoard(BoardTypes.Vertical); Console.Write(" "); DrawPiece(board[0][2]);
  76.                 Console.WriteLine();
  77.                 Console.Write("  "); DrawBoard(BoardTypes.Horizontal); DrawBoard(BoardTypes.Intersection); DrawBoard(BoardTypes.Horizontal); DrawBoard(BoardTypes.Intersection); DrawBoard(BoardTypes.Horizontal);
  78.                 Console.WriteLine();
  79.  
  80.                 //Draws second row
  81.                 Console.Write("   "); DrawPiece(board[1][0]); Console.Write(" "); DrawBoard(BoardTypes.Vertical); Console.Write(" "); DrawPiece(board[1][1]); Console.Write(" "); DrawBoard(BoardTypes.Vertical); Console.Write(" "); DrawPiece(board[1][2]);
  82.                 Console.WriteLine();
  83.                 Console.Write("  "); DrawBoard(BoardTypes.Horizontal); DrawBoard(BoardTypes.Intersection); DrawBoard(BoardTypes.Horizontal); DrawBoard(BoardTypes.Intersection); DrawBoard(BoardTypes.Horizontal);
  84.                 Console.WriteLine();
  85.  
  86.                 //Draws third row
  87.                 Console.Write("   "); DrawPiece(board[2][0]); Console.Write(" "); DrawBoard(BoardTypes.Vertical); Console.Write(" "); DrawPiece(board[2][1]); Console.Write(" "); DrawBoard(BoardTypes.Vertical); Console.Write(" "); DrawPiece(board[2][2]);
  88.                 Console.WriteLine();
  89.                 Console.WriteLine();
  90.             }
  91.  
  92.             //Prints a message after the board
  93.             private void PrintMessage(string msg)
  94.             {
  95.                 ConsoleColor PrevColor = Console.ForegroundColor;
  96.                 Console.ForegroundColor = ConsoleColor.DarkMagenta;
  97.                 Console.WriteLine(msg);
  98.                 Console.ForegroundColor = PrevColor;
  99.             }
  100.  
  101.             //Has the player make a move or the AI pick a move
  102.             private void PlayMove()
  103.             {
  104.                 if (turn == Player.X)
  105.                 {
  106.                     PrintMessage(" Pick a position:");
  107.                     while (!SetPiece(turn, Console.ReadKey().KeyChar)) ;
  108.                     turn = Player.O;
  109.                 }
  110.                 else
  111.                 {
  112.                     //Dumb AI (random non-calculated move)
  113.                     while (!SetPiece(turn, char.Parse(rnd.Next(1, 10).ToString()))) ;
  114.                     turn = Player.X;
  115.                 }
  116.                 PrintBoard();
  117.             }
  118.  
  119.             //Checks for valid position input and sets the move on the board
  120.             private bool SetPiece(Player player, char pos)
  121.             {
  122.                 switch (pos)
  123.                 {
  124.                     case '1':
  125.                         if (board[2][0] == 1)
  126.                         {
  127.                             board[2][0] = (sbyte)player;
  128.                             return true;
  129.                         }
  130.                         break;
  131.                     case '2':
  132.                         if (board[2][1] == 2)
  133.                         {
  134.                             board[2][1] = (sbyte)player;
  135.                             return true;
  136.                         }
  137.                         break;
  138.                     case '3':
  139.                         if (board[2][2] == 3)
  140.                         {
  141.                             board[2][2] = (sbyte)player;
  142.                             return true;
  143.                         }
  144.                         break;
  145.                     case '4':
  146.                         if (board[1][0] == 4)
  147.                         {
  148.                             board[1][0] = (sbyte)player;
  149.                             return true;
  150.                         }
  151.                         break;
  152.                     case '5':
  153.                         if (board[1][1] == 5)
  154.                         {
  155.                             board[1][1] = (sbyte)player;
  156.                             return true;
  157.                         }
  158.                         break;
  159.                     case '6':
  160.                         if (board[1][2] == 6)
  161.                         {
  162.                             board[1][2] = (sbyte)player;
  163.                             return true;
  164.                         }
  165.                         break;
  166.                     case '7':
  167.                         if (board[0][0] == 7)
  168.                         {
  169.                             board[0][0] = (sbyte)player;
  170.                             return true;
  171.                         }
  172.                         break;
  173.                     case '8':
  174.                         if (board[0][1] == 8)
  175.                         {
  176.                             board[0][1] = (sbyte)player;
  177.                             return true;
  178.                         }
  179.                         break;
  180.                     case '9':
  181.                         if (board[0][2] == 9)
  182.                         {
  183.                             board[0][2] = (sbyte)player;
  184.                             return true;
  185.                         }
  186.                         break;
  187.                 }
  188.                 return false;
  189.             }
  190.  
  191.             //Draws the player piece with color
  192.             private void DrawPiece(sbyte value)
  193.             {
  194.                 ConsoleColor prevColor = Console.ForegroundColor;
  195.                 if (value == 10)
  196.                 {
  197.                     Console.ForegroundColor = ConsoleColor.Red;
  198.                     Console.Write('X');
  199.                     Console.ForegroundColor = prevColor;
  200.                 }
  201.                 else if (value == -10)
  202.                 {
  203.                     Console.ForegroundColor = ConsoleColor.Green;
  204.                     Console.Write('O');
  205.                     Console.ForegroundColor = prevColor;
  206.                 }
  207.                 else
  208.                 {
  209.                     Console.ForegroundColor = ConsoleColor.DarkGray;
  210.                     Console.Write(value.ToString());
  211.                     Console.ForegroundColor = prevColor;
  212.                 }
  213.             }
  214.  
  215.             //Specifies the 3 parts of a board
  216.             private enum BoardTypes
  217.             {
  218.                 Vertical,
  219.                 Horizontal,
  220.                 Intersection
  221.             }
  222.  
  223.             //Draws parts of the board
  224.             private void DrawBoard(BoardTypes type)
  225.             {
  226.                 switch (type)
  227.                 {
  228.                     case BoardTypes.Vertical:
  229.                         Console.Write('║');
  230.                         break;
  231.                     case BoardTypes.Horizontal:
  232.                         Console.Write("═══");
  233.                         break;
  234.  
  235.                     case BoardTypes.Intersection:
  236.                         Console.Write('╬');
  237.                         break;
  238.                 }
  239.             }
  240.  
  241.             //Checks the board for winning pattern
  242.             private bool CheckWinner()
  243.             {
  244.                 //Check Rows for winner
  245.                 for (int y = 0; y < board.Length; y++)
  246.                 {
  247.                     int total = 0;
  248.                     for (int x = 0; x < board[y].Length; x++)
  249.                     {
  250.                         total += board[y][x];
  251.                     }
  252.                     if (total == 30)
  253.                     {
  254.                         DisplayWinner(Player.X);
  255.                         return true;
  256.                     }
  257.                     if (total == -30)
  258.                     {
  259.                         DisplayWinner(Player.O);
  260.                         return true;
  261.                     }
  262.                 }
  263.  
  264.                 //Check Columns for winner
  265.                 for (int x = 0; x < board.Length; x++)
  266.                 {
  267.                     int total = 0;
  268.                     for (int y = 0; y < board[x].Length; y++)
  269.                         total += board[y][x];
  270.                     if (total == 30)
  271.                     {
  272.                         DisplayWinner(Player.X);
  273.                         return true;
  274.                     }
  275.                     if (total == -30)
  276.                     {
  277.                         DisplayWinner(Player.O);
  278.                         return true;
  279.                     }
  280.                 }
  281.  
  282.                 //Check diagonals
  283.                 int d1 = 0, d2 = 0;
  284.                 for (int i1 = 0, i2 = 2; i1 < board.Length; i1++, i2--)
  285.                 {
  286.                     d1 += board[i1][i1];
  287.                     d2 += board[i1][i2];
  288.                 }
  289.                 if (d1 == 30 || d2 == 30)
  290.                 {
  291.                     DisplayWinner(Player.X);
  292.                     return true;
  293.                 }
  294.                 if (d1 == -30 || d2 == -30)
  295.                 {
  296.                     DisplayWinner(Player.O);
  297.                     return true;
  298.                 }
  299.                 return false;
  300.             }
  301.  
  302.             //Displays the Win/Lose/Draw message
  303.             private void DisplayWinner(Player player)
  304.             {
  305.                 ConsoleColor PrevColor = Console.ForegroundColor;
  306.                 Console.ForegroundColor = ConsoleColor.Cyan;
  307.                 if (player == Player.X)
  308.                     Console.WriteLine("  Congrats, You've Won !!!");
  309.                 else if (player == Player.O)
  310.                     Console.WriteLine("  Sorry, You've Lost !!!");
  311.                 else
  312.                     Console.WriteLine("  Game Ended In A Draw !!!");
  313.                 Console.ForegroundColor = PrevColor;
  314.             }
  315.         }
  316.     }
  317. }
Advertisement
Add Comment
Please, Sign In to add comment