yordanganev

tictactoedemo

Feb 24th, 2021
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleAppTicTacToe
  4. {
  5.     public class TicTacToe     
  6.     {
  7.         // Private class members
  8.         private char[,] board = new char[3, 3];
  9.         private int currentPlayer;
  10.        
  11.         private const int CoordinateLimit = 2;
  12.        
  13.         private const int playerX = 0; // todo check ENUM
  14.         private const int playerO = 1;
  15.        
  16.         private readonly char[] marks = new char[2] {'x', 'o'};
  17.        
  18.         public TicTacToe()
  19.         {
  20.             Console.WriteLine("Game started!");
  21.             currentPlayer = playerX;
  22.         }
  23.        
  24.         // Public class functions
  25.         public void displayBoard()
  26.         {
  27.             for(int y = 0; y < board.GetLength(1); y++)
  28.             {
  29.                 for(int x = 0; x < board.GetLength(0); x++)
  30.                 {
  31.                     if ( board[x,y] == 0) // is empty
  32.                     {
  33.                         Console.Write("- ");
  34.                     }
  35.                     else
  36.                     {
  37.                         Console.Write(board[x,y]);
  38.                         Console.Write(' ');
  39.                     }
  40.                 }
  41.                 Console.WriteLine();
  42.             }
  43.            
  44.         }
  45.        
  46.         public int currentPlayerInt()
  47.         {
  48.             return currentPlayer;
  49.         }
  50.        
  51.         public string currentPlayerText()
  52.         {
  53.             return (currentPlayer == playerX) ? "Player X" : "Player O";
  54.         }
  55.        
  56.        
  57.        
  58.         /*
  59.        
  60.          */
  61.        
  62.         public bool consoleSelectBox()
  63.         {
  64.             int x;
  65.             x = Int32. Console.ReadLine();
  66.            
  67.             int y;
  68.            
  69.            
  70.             return selectBox(x, y);
  71.         }
  72.        
  73.        
  74.         /* Selects box by current player
  75.            
  76.            Returns true if selected box is valid
  77.            Returns false if box is already marked
  78.            Returns false if x or y out of range
  79.          */
  80.         public bool selectBox(int x, int y)
  81.         {
  82.             // Check if x,y are in range
  83.             if ( x > CoordinateLimit || y > CoordinateLimit )
  84.             {
  85.                 Console.WriteLine("Selected index out of range ");
  86.                 return false;
  87.             }
  88.            
  89.             // Check if box is empty
  90.             if ( board[x,y] != 0 )
  91.             {
  92.                 Console.WriteLine("This box is already marked!");
  93.                 return false;
  94.             }
  95.             else
  96.             {
  97.                 board[x,y] = marks[currentPlayer];
  98.                
  99.                 // currentPlayer = !currentPlayer;
  100.                 if (currentPlayer == playerX)
  101.                 {
  102.                     currentPlayer = playerO;
  103.                 }
  104.                 else
  105.                 {
  106.                     currentPlayer = playerX;
  107.                 }
  108.                 Console.WriteLine(currentPlayer);
  109.                 this.displayBoard();
  110.                
  111.                 return true;
  112.             }
  113.         }
  114.        
  115.     }
  116.    
  117.     public class Program
  118.     {
  119.         public static void Main()
  120.         {
  121.             TicTacToe game = new TicTacToe();
  122.             game.displayBoard();
  123.            
  124.             Console.WriteLine(game.currentPlayerInt());
  125.             Console.WriteLine(game.currentPlayerText());
  126.             game.selectBox(1,1);
  127.            
  128.             Console.WriteLine(game.currentPlayerText());
  129.             game.selectBox(1,2);
  130.            
  131.             Console.WriteLine();
  132.             game.selectBox(1,1); //
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment