Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace TicTacToe
- {
- internal static class Program
- {
- private static void Main(string[] args)
- {
- if (args == null)
- {
- throw new ArgumentNullException(nameof(args));
- }
- Game ttt = new Game();
- while (true)
- {
- ttt = new Game();
- ttt.Play();
- Console.WriteLine(" Press Enter To Start A New Game ...");
- Console.ReadLine();
- }
- }
- public class Game
- {
- private enum Player : sbyte
- {
- None = 0,
- X = 10, //Human Player
- O = -10 //AI Player
- }
- private readonly sbyte[][] board;
- private readonly Random rnd;
- private Player turn;
- //Creates a new game instance
- public Game()
- {
- //Clears the screen of any previous games
- Console.Clear();
- //Choose who goes first
- rnd = new Random();
- turn = Player.X;
- if (rnd.Next(0, 2) == 1) turn = Player.O;
- //Initializes the game board
- board = new sbyte[][] { new sbyte[]{ 7, 8, 9},
- new sbyte[]{ 4, 5, 6},
- new sbyte[]{ 1, 2, 3} };
- }
- //Starts the game
- public void Play()
- {
- PrintBoard();
- int i = 0;
- for (; i < 9 && !CheckWinner(); i++)
- {
- PlayMove();
- }
- if (i == 9 && !CheckWinner()) DisplayWinner(Player.None);
- }
- //Prints the entire board
- public void PrintBoard()
- {
- Console.SetCursorPosition(0, 0);
- Console.CursorVisible = false;
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.WriteLine();
- //Draws first row
- 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]);
- Console.WriteLine();
- Console.Write(" "); DrawBoard(BoardTypes.Horizontal); DrawBoard(BoardTypes.Intersection); DrawBoard(BoardTypes.Horizontal); DrawBoard(BoardTypes.Intersection); DrawBoard(BoardTypes.Horizontal);
- Console.WriteLine();
- //Draws second row
- 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]);
- Console.WriteLine();
- Console.Write(" "); DrawBoard(BoardTypes.Horizontal); DrawBoard(BoardTypes.Intersection); DrawBoard(BoardTypes.Horizontal); DrawBoard(BoardTypes.Intersection); DrawBoard(BoardTypes.Horizontal);
- Console.WriteLine();
- //Draws third row
- 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]);
- Console.WriteLine();
- Console.WriteLine();
- }
- //Prints a message after the board
- private void PrintMessage(string msg)
- {
- ConsoleColor PrevColor = Console.ForegroundColor;
- Console.ForegroundColor = ConsoleColor.DarkMagenta;
- Console.WriteLine(msg);
- Console.ForegroundColor = PrevColor;
- }
- //Has the player make a move or the AI pick a move
- private void PlayMove()
- {
- if (turn == Player.X)
- {
- PrintMessage(" Pick a position:");
- while (!SetPiece(turn, Console.ReadKey().KeyChar)) ;
- turn = Player.O;
- }
- else
- {
- //Dumb AI (random non-calculated move)
- while (!SetPiece(turn, char.Parse(rnd.Next(1, 10).ToString()))) ;
- turn = Player.X;
- }
- PrintBoard();
- }
- //Checks for valid position input and sets the move on the board
- private bool SetPiece(Player player, char pos)
- {
- switch (pos)
- {
- case '1':
- if (board[2][0] == 1)
- {
- board[2][0] = (sbyte)player;
- return true;
- }
- break;
- case '2':
- if (board[2][1] == 2)
- {
- board[2][1] = (sbyte)player;
- return true;
- }
- break;
- case '3':
- if (board[2][2] == 3)
- {
- board[2][2] = (sbyte)player;
- return true;
- }
- break;
- case '4':
- if (board[1][0] == 4)
- {
- board[1][0] = (sbyte)player;
- return true;
- }
- break;
- case '5':
- if (board[1][1] == 5)
- {
- board[1][1] = (sbyte)player;
- return true;
- }
- break;
- case '6':
- if (board[1][2] == 6)
- {
- board[1][2] = (sbyte)player;
- return true;
- }
- break;
- case '7':
- if (board[0][0] == 7)
- {
- board[0][0] = (sbyte)player;
- return true;
- }
- break;
- case '8':
- if (board[0][1] == 8)
- {
- board[0][1] = (sbyte)player;
- return true;
- }
- break;
- case '9':
- if (board[0][2] == 9)
- {
- board[0][2] = (sbyte)player;
- return true;
- }
- break;
- }
- return false;
- }
- //Draws the player piece with color
- private void DrawPiece(sbyte value)
- {
- ConsoleColor prevColor = Console.ForegroundColor;
- if (value == 10)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Write('X');
- Console.ForegroundColor = prevColor;
- }
- else if (value == -10)
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write('O');
- Console.ForegroundColor = prevColor;
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.DarkGray;
- Console.Write(value.ToString());
- Console.ForegroundColor = prevColor;
- }
- }
- //Specifies the 3 parts of a board
- private enum BoardTypes
- {
- Vertical,
- Horizontal,
- Intersection
- }
- //Draws parts of the board
- private void DrawBoard(BoardTypes type)
- {
- switch (type)
- {
- case BoardTypes.Vertical:
- Console.Write('║');
- break;
- case BoardTypes.Horizontal:
- Console.Write("═══");
- break;
- case BoardTypes.Intersection:
- Console.Write('╬');
- break;
- }
- }
- //Checks the board for winning pattern
- private bool CheckWinner()
- {
- //Check Rows for winner
- for (int y = 0; y < board.Length; y++)
- {
- int total = 0;
- for (int x = 0; x < board[y].Length; x++)
- {
- total += board[y][x];
- }
- if (total == 30)
- {
- DisplayWinner(Player.X);
- return true;
- }
- if (total == -30)
- {
- DisplayWinner(Player.O);
- return true;
- }
- }
- //Check Columns for winner
- for (int x = 0; x < board.Length; x++)
- {
- int total = 0;
- for (int y = 0; y < board[x].Length; y++)
- total += board[y][x];
- if (total == 30)
- {
- DisplayWinner(Player.X);
- return true;
- }
- if (total == -30)
- {
- DisplayWinner(Player.O);
- return true;
- }
- }
- //Check diagonals
- int d1 = 0, d2 = 0;
- for (int i1 = 0, i2 = 2; i1 < board.Length; i1++, i2--)
- {
- d1 += board[i1][i1];
- d2 += board[i1][i2];
- }
- if (d1 == 30 || d2 == 30)
- {
- DisplayWinner(Player.X);
- return true;
- }
- if (d1 == -30 || d2 == -30)
- {
- DisplayWinner(Player.O);
- return true;
- }
- return false;
- }
- //Displays the Win/Lose/Draw message
- private void DisplayWinner(Player player)
- {
- ConsoleColor PrevColor = Console.ForegroundColor;
- Console.ForegroundColor = ConsoleColor.Cyan;
- if (player == Player.X)
- Console.WriteLine(" Congrats, You've Won !!!");
- else if (player == Player.O)
- Console.WriteLine(" Sorry, You've Lost !!!");
- else
- Console.WriteLine(" Game Ended In A Draw !!!");
- Console.ForegroundColor = PrevColor;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment