Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace kolkokrzyzyk_2
  7. {
  8. class Program
  9. {
  10. static String[] board = new String[9];
  11. static String playsAgain = "Y";
  12. static int counter = 0;
  13.  
  14. static void initializeVariable()
  15. {
  16. for (int i = 0; i < 9; i++)
  17. {
  18. board[i] = i.ToString();
  19. }
  20. }
  21.  
  22. static void Main(string[] args)
  23. {
  24. do
  25. {
  26. introduction();
  27. initializeVariable();
  28. while (hasWon() == false && counter < 9)
  29. {
  30. askData("X");
  31. if (hasWon() == true)
  32. break;
  33. askData("O");
  34. }
  35. if (hasWon() == true)
  36. {
  37. Console.WriteLine(" Gratulacje, wygrałeś");
  38. }
  39. else
  40. {
  41. Console.WriteLine(" Remis!");
  42. }
  43. Console.WriteLine("Jeśli nie chcesz grać od nowa wciśnij N");
  44. } while (Console.ReadLine() != "N");
  45. goodBye();
  46. }
  47.  
  48. static void goodBye()
  49. {
  50. Console.WriteLine("Do zobaczenia");
  51. Console.ReadLine();
  52.  
  53. }
  54.  
  55. static void askData(String player)
  56. {
  57. Console.Clear();
  58.  
  59. Console.WriteLine("Player: " + player);
  60. int selection;
  61.  
  62. do
  63. {
  64.  
  65.  
  66. Console.WriteLine("Wybierz Pole");
  67. drawBoard();
  68. selection = Convert.ToInt32(Console.ReadLine());
  69. if (selection < 0 || selection > 9 || (board[selection].Equals("X") || board[selection].Equals("O")))
  70. Console.WriteLine("Zły wybór");
  71. else
  72. break;
  73. } while (!int.TryParse(Console.ReadLine(), out selection) || selection < 0 || selection > 9);
  74. board[selection] = player;
  75.  
  76.  
  77. }
  78.  
  79. static void drawBoard()
  80. {
  81. for (int i = 0; i < 7; i += 3) //Draw the board.
  82. Console.WriteLine(board[i] + "|" + board[i + 1] + "|" + board[i + 2]);
  83. }
  84.  
  85. static Boolean hasWon()
  86. {
  87. for (int i = 0; i < 7; i += 3)
  88. {
  89. if (board[i].Equals(board[i + 1]) && board[i + 1].Equals(board[i + 2]))
  90. {
  91. return true;
  92. }
  93. }
  94. if (board[0].Equals(board[3]) && board[3].Equals(board[6]))
  95. return true;
  96. if (board[1].Equals(board[4]) && board[4].Equals(board[7]))
  97. return true;
  98. if (board[2].Equals(board[5]) && board[3].Equals(board[8]))
  99. return true;
  100. if (board[2].Equals(board[4]) && board[4].Equals(board[6]))
  101. return true;
  102. if (board[0].Equals(board[4]) && board[4].Equals(board[8]))
  103. return true;
  104. return false;
  105. }
  106.  
  107.  
  108. static void introduction()
  109. {
  110. Console.Title = ("Kółko Krzyżyk");
  111. Console.WriteLine("Witaj w grze kółko krzyżyk.\n");
  112. Console.WriteLine("Naciśnij dowolny klawisz, aby kontynuować");
  113. Console.ReadLine();
  114. Console.Clear();
  115. }
  116.  
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement