Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp1
  8. {
  9.  
  10. class Program
  11. {
  12. static int readInt(string komunikat, uint min = uint.MinValue, uint max = uint.MaxValue)
  13. {
  14. string str;
  15. int liczba;
  16. while (true)
  17. {
  18. Console.Write(komunikat);
  19. str = Console.ReadLine();
  20. try
  21. {
  22. liczba = int.Parse(str);
  23. if ((liczba <= max) && (liczba >= min)) break;
  24. else Console.WriteLine("Wprowadzona liczba jest spoza zakresu <{0},{1}> ", min, max);
  25. }
  26. catch (FormatException)
  27. {
  28. Console.WriteLine("Wprowadzono liczbę w złym formacie");
  29. }
  30. catch (OverflowException)
  31. {
  32. Console.WriteLine("Liczba przekroczyła zakres dla typu uint ");
  33. }
  34. catch (ArgumentNullException) // ^Z
  35. {
  36. Console.WriteLine("Napotkano koniec strumienia");
  37. }
  38. Console.WriteLine("Spróbuj jeszcze raz");
  39. }//koniec wprowadzania liczby elementów
  40. return liczba;
  41. }
  42. static void szablon(string[,] p)
  43. {
  44. Console.Clear();
  45. Console.WriteLine(" {0} | {1} | {2} ", p[0, 0], p[0, 1], p[0, 2]);
  46. Console.WriteLine("---+---+---");
  47. Console.WriteLine(" {0} | {1} | {2} ", p[1, 0], p[1, 1], p[1, 2]);
  48. Console.WriteLine("---+---+---");
  49. Console.WriteLine(" {0} | {1} | {2} ", p[2, 0], p[2, 1], p[2, 2]);
  50. }
  51.  
  52. static void wybierzPole(string[,] p, string znak)
  53. {
  54. while (true)
  55. {
  56. int numerPola = readInt("Podaj numer pola dla gracza " + znak + " : ", 1, 9);
  57. if (!sprawdzPole(p, numerPola, znak))
  58. Console.WriteLine("Wybrane pole jest już wypełnione");
  59. else break;
  60. }
  61. }
  62. static bool sprawdzPole(string[,] p, int nr, string znak)
  63. {
  64. int x = 0;
  65. bool puste = true;
  66. for (int w = 0; w < 3; w++)
  67. for (int k = 0; k < 3; k++)
  68. {
  69. x++;
  70. if (x == nr)
  71. if ((p[w, k] == "O") || (p[w, k] == "X"))
  72. {
  73. puste = false;
  74. }
  75. else
  76. {
  77. p[w, k] = znak;
  78. szablon(p);
  79. }
  80. }
  81. return puste;
  82. }
  83.  
  84. static bool testKonca(string[,] p, string znak)
  85. {
  86. bool koniec = false;
  87. string tmp;
  88. //wiersze
  89. for (int w = 0; w < 3; w++)
  90. {
  91. tmp = znak;
  92. for (int k = 0; k < 3; k++)
  93.  
  94. {
  95. if (tmp != p[w, k])
  96. {
  97. tmp = "";
  98. break;
  99. }
  100.  
  101. }
  102. if (tmp != "") return true;
  103. }
  104. //kolumny
  105. for (int k = 0; k < 3; k++)
  106. {
  107. tmp = znak;
  108. for (int w = 0; w < 3; w++)
  109. {
  110. if (tmp != p[w, k])
  111. {
  112. tmp = "";
  113. break;
  114. }
  115. }
  116. return true;
  117. }
  118.  
  119. // przekątna1
  120. tmp = znak;
  121. for (int w=0; w<3;w++)
  122. {
  123.  
  124. for (int k=0;k<3;k++)
  125. if(w==k)
  126. if(p[w,k] !=znak)
  127. {
  128. tmp = "";
  129. break;
  130. }
  131.  
  132. }
  133. if(tmp !="") return true;
  134. //przekatna2
  135. tmp = znak;
  136. for (int w=0;w<3; w++)
  137. {
  138. for (int k = 0; k < 3; k++)
  139. if (w + k == 2)
  140. if (p[w, k] != znak) tmp = "";
  141.  
  142. }
  143. if (tmp != "") return true;
  144. //remis
  145. for (int w = 0; w < 3; w++)
  146. for (int k = 0; k < 3; k++)
  147. if (p[w, k] != "X" && p[w, k] != "O") return false;
  148. Console.WriteLine("Remis");
  149.  
  150.  
  151. return koniec;
  152. }
  153.  
  154. static void Main(string[] args)
  155. {
  156. string[,] pola = new string[3, 3];
  157. int nr = 0;
  158. for (int w = 0; w < 3; w++)
  159. for (int k = 0; k < 3; k++)
  160. {
  161. nr++;
  162. pola[w, k] = nr.ToString();
  163. }
  164. szablon(pola);
  165. while (true)
  166. {
  167. wybierzPole(pola, "X");
  168. if (testKonca(pola, "X"))
  169. {
  170. Console.WriteLine("Wygrał gracz X");
  171. break;
  172. }
  173. wybierzPole(pola, "O");
  174. if (testKonca(pola, "O"))
  175. {
  176. Console.WriteLine("Wygrał gracz O");
  177. break;
  178. }
  179. }
  180.  
  181. Console.ReadKey();
  182. }
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement