Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace TIC_TAC_TOE
  5. {
  6. class Program
  7. {
  8. //making array and
  9. //by default I am providing 0-9 where no use of zero
  10. static char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  11. static int player = 1; //By default player 1 is set
  12. static int choice; //This holds the choice at which position user want to mark
  13.  
  14. // The flag veriable checks who has won if it's value is 1 then some one has won the match if -1 then Match has Draw if 0 then match is still running
  15. static int flag = 0;
  16.  
  17. static void Main(string[] args)
  18. {
  19. do
  20. {
  21. Console.Clear();// whenever loop will be again start then screen will be clear
  22. Console.WriteLine("Player1:X and Player2:O");
  23. Console.WriteLine("\n");
  24. if (player % 2 == 0)//checking the chance of the player
  25. {
  26. Console.WriteLine("Player 2 Chance");
  27. }
  28. else
  29. {
  30. Console.WriteLine("Player 1 Chance");
  31. }
  32. Console.WriteLine("\n");
  33. Board();// calling the board Function
  34. choice = int.Parse(Console.ReadLine());//Taking users choice
  35.  
  36. // checking that position where user want to run is marked (with X or O) or not
  37. if (arr[choice] != 'X' && arr[choice] != 'O')
  38. {
  39. if (player % 2 == 0) //if chance is of player 2 then mark O else mark X
  40. {
  41. arr[choice] = 'O';
  42. player++;
  43. }
  44. else
  45. {
  46. arr[choice] = 'X';
  47. player++;
  48. }
  49. }
  50. else //If there is any possition where user want to run and that is already marked then show message and load board again
  51. {
  52. Console.WriteLine("Sorry the row {0} is already marked with {1}", choice, arr[choice]);
  53. Console.WriteLine("\n");
  54. Console.WriteLine("Please wait 2 second board is loading again.....");
  55. Thread.Sleep(2000);
  56. }
  57. flag = CheckWin();// calling of check win
  58. } while (flag != 1 && flag != -1);// This loof will be run until all cell of the grid is not marked with X and O or some player is not win
  59.  
  60. Console.Clear();// clearing the console
  61. Board();// getting filled board again
  62.  
  63. if (flag == 1)// if flag value is 1 then some one has win or means who played marked last time which has win
  64. {
  65. Console.WriteLine("Player {0} has won", (player % 2) + 1);
  66. }
  67. else// if flag value is -1 the match will be draw and no one is winner
  68. {
  69. Console.WriteLine("Draw");
  70. }
  71. Console.ReadLine();
  72. }
  73. // Board method which creats board
  74. private static void Board()
  75. {
  76. Console.WriteLine(" | | ");
  77. Console.WriteLine(" {0} | {1} | {2}", arr[1], arr[2], arr[3]);
  78. Console.WriteLine("_____|_____|_____ ");
  79. Console.WriteLine(" | | ");
  80. Console.WriteLine(" {0} | {1} | {2}", arr[4], arr[5], arr[6]);
  81. Console.WriteLine("_____|_____|_____ ");
  82. Console.WriteLine(" | | ");
  83. Console.WriteLine(" {0} | {1} | {2}", arr[7], arr[8], arr[9]);
  84. Console.WriteLine(" | | ");
  85. }
  86.  
  87. //Checking that any player has won or not
  88. private static int CheckWin()
  89. {
  90. #region Horzontal Winning Condtion
  91. //Winning Condition For First Row
  92. if (arr[1] == arr[2] && arr[2] == arr[3])
  93. {
  94. return 1;
  95. }
  96. //Winning Condition For Second Row
  97. else if (arr[4] == arr[5] && arr[5] == arr[6])
  98. {
  99. return 1;
  100. }
  101. //Winning Condition For Third Row
  102. else if (arr[6] == arr[7] && arr[7] == arr[8])
  103. {
  104. return 1;
  105. }
  106. #endregion
  107.  
  108. #region vertical Winning Condtion
  109. //Winning Condition For First Column
  110. else if (arr[1] == arr[4] && arr[4] == arr[7])
  111. {
  112. return 1;
  113. }
  114. //Winning Condition For Second Column
  115. else if (arr[2] == arr[5] && arr[5] == arr[8])
  116. {
  117. return 1;
  118. }
  119. //Winning Condition For Third Column
  120. else if (arr[3] == arr[6] && arr[6] == arr[9])
  121. {
  122. return 1;
  123. }
  124. #endregion
  125.  
  126. #region Diagonal Winning Condition
  127. else if (arr[1] == arr[5] && arr[5] == arr[9])
  128. {
  129. return 1;
  130. }
  131. else if (arr[3] == arr[5] && arr[5] == arr[7])
  132. {
  133. return 1;
  134. }
  135. #endregion
  136.  
  137. #region Checking For Draw
  138. // If all the cells or values filled with X or O then any player has won the match
  139. else if (arr[1] != '1' && arr[2] != '2' && arr[3] != '3' && arr[4] != '4' && arr[5] != '5' && arr[6] != '6' && arr[7] != '7' && arr[8] != '8' && arr[9] != '9')
  140. {
  141. return -1;
  142. }
  143. #endregion
  144.  
  145. else
  146. {
  147. return 0;
  148. }
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement