yordanganev

tictactoedemo_todo

Feb 24th, 2021
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 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. Console.WriteLine("Select box (x,y): ");
  65.  
  66. Console.Write("x: ");
  67. int x = Int32.Parse(Console.ReadLine());
  68.  
  69. Console.Write("y: ");
  70. int y = Int32.Parse(Console.ReadLine());
  71.  
  72. return selectBox(x, y);
  73. }
  74.  
  75.  
  76. /* Selects box by current player
  77.  
  78. Returns true if selected box is valid
  79. Returns false if box is already marked
  80. Returns false if x or y out of range
  81. */
  82. public bool selectBox(int x, int y)
  83. {
  84. // Check if x,y are in range
  85. if ( (x > CoordinateLimit) || (x < 0) || y > CoordinateLimit )
  86. {
  87. Console.WriteLine("Selected index out of range ");
  88. return false;
  89. }
  90.  
  91. // Check if box is empty
  92. if ( board[x,y] != 0 )
  93. {
  94. Console.WriteLine("This box is already marked!");
  95. return false;
  96. }
  97. else
  98. {
  99. board[x,y] = marks[currentPlayer];
  100.  
  101. checkWinner();
  102. // currentPlayer = !currentPlayer;
  103. if (currentPlayer == playerX)
  104. {
  105. currentPlayer = playerO;
  106. }
  107. else
  108. {
  109. currentPlayer = playerX;
  110. }
  111. Console.WriteLine(currentPlayer);
  112. this.displayBoard();
  113.  
  114. return true;
  115. }
  116. }
  117.  
  118. private bool checkWinner()
  119. {
  120. for (int col = 0; col < 3; col++)
  121. {
  122.  
  123. }
  124.  
  125. return false;
  126. }
  127.  
  128. }
  129.  
  130. public class Program
  131. {
  132. public static void Main()
  133. {
  134. TicTacToe game = new TicTacToe();
  135. game.displayBoard();
  136.  
  137. Console.WriteLine(game.currentPlayerInt());
  138. Console.WriteLine(game.currentPlayerText());
  139. game.selectBox(1,1);
  140.  
  141. Console.WriteLine(game.currentPlayerText());
  142. game.selectBox(1,2);
  143.  
  144. Console.WriteLine();
  145. game.selectBox(1,1); //
  146.  
  147. game.consoleSelectBox();
  148. }
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment