Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.75 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 KolkoIKrzyzyk
  8. {
  9. class Program
  10. {
  11. static bool loop(string[,] fields)
  12. {
  13. Console.WriteLine($" + - - - - - +\n");
  14. Console.WriteLine($" | {getFillerForLoop(fields[0, 0])} | {getFillerForLoop(fields[1, 0])} | {getFillerForLoop(fields[2, 0])} |\n");
  15. Console.WriteLine($" | {getFillerForLoop(fields[0, 1])} | {getFillerForLoop(fields[1, 1])} | {getFillerForLoop(fields[2, 1])} |\n");
  16. Console.WriteLine($" | {getFillerForLoop(fields[0, 2])} | {getFillerForLoop(fields[1, 2])} | {getFillerForLoop(fields[2, 2])} |\n");
  17. Console.WriteLine($" + - - - - - +\n");
  18.  
  19. if (someoneWon(fields))
  20. {
  21. return false;
  22. }
  23.  
  24. return true;
  25. }
  26.  
  27. static string getFillerForLoop(string field)
  28. {
  29. if(field != null)
  30. {
  31. return field;
  32. }
  33. else
  34. {
  35. return "_";
  36. }
  37. }
  38.  
  39. static bool someoneWon(string[,] fields)
  40. {
  41. if((fields[0, 0] == "o" && fields[0, 1] == "o" && fields[0, 2] == "o") ||
  42. (fields[1, 0] == "o" && fields[1, 1] == "o" && fields[1, 2] == "o") ||
  43. (fields[2, 0] == "o" && fields[2, 1] == "o" && fields[2, 2] == "o") ||
  44. //pionowe warunki o
  45.  
  46. (fields[0, 0] == "o" && fields[1, 1] == "o" && fields[2, 2] == "o") ||
  47. (fields[2, 0] == "o" && fields[1, 1] == "o" && fields[0, 2] == "o") ||
  48. //ukosy o
  49.  
  50. (fields[0, 0] == "o" && fields[1, 0] == "o" && fields[2, 0] == "o") ||
  51. (fields[0, 1] == "o" && fields[1, 1] == "o" && fields[2, 1] == "o") ||
  52. (fields[0, 2] == "o" && fields[1, 2] == "o" && fields[2, 2] == "o") ||
  53. //poziome o
  54.  
  55. (fields[0, 0] == "x" && fields[0, 1] == "x" && fields[0, 2] == "x") ||
  56. (fields[1, 0] == "x" && fields[1, 1] == "x" && fields[1, 2] == "x") ||
  57. (fields[2, 0] == "x" && fields[2, 1] == "x" && fields[2, 2] == "x") ||
  58. //pionowe warunki x
  59.  
  60. (fields[0, 0] == "x" && fields[1, 1] == "x" && fields[2, 2] == "x") ||
  61. (fields[2, 0] == "x" && fields[1, 1] == "x" && fields[0, 2] == "x") ||
  62. //ukosy x
  63.  
  64. (fields[0, 0] == "x" && fields[1, 0] == "x" && fields[2, 0] == "x") ||
  65. (fields[0, 1] == "x" && fields[1, 1] == "x" && fields[2, 1] == "x") ||
  66. (fields[0, 2] == "x" && fields[1, 2] == "x" && fields[2, 2] == "x"))
  67. {
  68. return true;
  69. }
  70. return false;
  71. }
  72.  
  73.  
  74. /// <summary>
  75. /// Metoda określenia położenia x o przyjmujac tablice aktualnych polozen
  76. /// </summary>
  77. /// <param name="fieldName"></param>
  78. /// <param name="playerNumber"></param>
  79. /// <param name="fields"></param>
  80. /// <returns>Zwraca status ruchu (zwrot obiektu przez referencje) -> status 1 -> poprawny ruch / 0 -> ruch niepoprawny</returns>
  81. static int getXO(string fieldName, int playerNumber, string[,] fields)
  82. {
  83. switch (fieldName.ToLowerInvariant())
  84. {
  85. case "a1":
  86.  
  87. if (!verifyEmptyField(fields[0, 0]))
  88. {
  89. Console.WriteLine("Wybraleś już używane pole, lub pole poza zakresem, wybierz jeszcze raz");
  90. return 0;
  91. }
  92.  
  93. fields[0,0] = playerNumber == 1 ? "x" : "o";
  94. return 1;
  95.  
  96. case "a2":
  97.  
  98. if (!verifyEmptyField(fields[1, 0]))
  99. {
  100. Console.WriteLine("Wybraleś już używane pole, lub pole poza zakresem, wybierz jeszcze raz");
  101. return 0;
  102. }
  103.  
  104. fields[1, 0] = playerNumber == 1 ? "x" : "o";
  105. return 1;
  106.  
  107. case "a3":
  108.  
  109. if (!verifyEmptyField(fields[2, 0]))
  110. {
  111. Console.WriteLine("Wybraleś już używane pole, lub pole poza zakresem, wybierz jeszcze raz");
  112. return 0;
  113. }
  114.  
  115. fields[2, 0] = playerNumber == 1 ? "x" : "o";
  116. return 1;
  117.  
  118. case "b1":
  119.  
  120. if (!verifyEmptyField(fields[0, 1]))
  121. {
  122. Console.WriteLine("Wybraleś już używane pole, lub pole poza zakresem, wybierz jeszcze raz");
  123. return 0;
  124. }
  125.  
  126. fields[0, 1] = playerNumber == 1 ? "x" : "o";
  127. return 1;
  128.  
  129. case "b2":
  130.  
  131. if (!verifyEmptyField(fields[1, 1]))
  132. {
  133. Console.WriteLine("Wybraleś już używane pole, lub pole poza zakresem, wybierz jeszcze raz");
  134. return 0;
  135. }
  136.  
  137. fields[1, 1] = playerNumber == 1 ? "x" : "o";
  138. return 1;
  139.  
  140. case "b3":
  141.  
  142. if (!verifyEmptyField(fields[2, 1]))
  143. {
  144. Console.WriteLine("Wybraleś już używane pole, lub pole poza zakresem, wybierz jeszcze raz");
  145. return 0;
  146. }
  147.  
  148. fields[2, 1] = playerNumber == 1 ? "x" : "o";
  149. return 1;
  150.  
  151. case "c1":
  152.  
  153. if (!verifyEmptyField(fields[0, 2]))
  154. {
  155. Console.WriteLine("Wybraleś już używane pole, lub pole poza zakresem, wybierz jeszcze raz");
  156. return 0;
  157. }
  158.  
  159. fields[0, 2] = playerNumber == 1 ? "x" : "o";
  160. return 1;
  161.  
  162. case "c2":
  163.  
  164. if (!verifyEmptyField(fields[1, 2]))
  165. {
  166. Console.WriteLine("Wybraleś już używane pole, lub pole poza zakresem, wybierz jeszcze raz");
  167. return 0;
  168. }
  169.  
  170. fields[1, 2] = playerNumber == 1 ? "x" : "o";
  171. return 1;
  172.  
  173. case "c3":
  174.  
  175. if (!verifyEmptyField(fields[2, 2]))
  176. {
  177. Console.WriteLine("Wybraleś już używane pole, lub pole poza zakresem, wybierz jeszcze raz");
  178. return 0;
  179. }
  180.  
  181. fields[2, 2] = playerNumber == 1 ? "x" : "o";
  182. return 1;
  183.  
  184. default:
  185.  
  186. Console.WriteLine("Wybraleś już używane pole, lub pole poza zakresem, wybierz jeszcze raz");
  187. return 0;
  188. }
  189. }
  190.  
  191. static bool verifyEmptyField(string field)
  192. {
  193. if(field != null)
  194. {
  195. return false;
  196. }
  197. return true;
  198. }
  199.  
  200. static void Main(string[] args)
  201. {
  202. Console.WriteLine("Witaj w aplikacji kółko i krzyżyk\n\n");
  203.  
  204. Console.WriteLine($" + - - - - - - -+\n");
  205. Console.WriteLine($" | a1 | a2 | a3 |\n");
  206. Console.WriteLine($" | b1 | b2 | b3 |\n");
  207. Console.WriteLine($" | c1 | c2 | c3 |\n");
  208. Console.WriteLine($" + - - - - - - -+\n");
  209.  
  210. Console.WriteLine($"Powyżej zobaczyles instrukcje numeracji pól, \ngrę zaczyna gracz nr1 decydujac jakie pole chce zająć. \nGotowy? Zaczynamy grę!\n\n");
  211.  
  212. string[,] fields = new string[3, 3];
  213. var fieldName = "";
  214. var playerNumber = 0;
  215. var i = 0;
  216. do
  217. {
  218. if (i % 2 == 0)
  219. {
  220. Console.WriteLine($"GRACZ 1: Wybierz pole: ");
  221. playerNumber = 0;
  222. }
  223. else
  224. {
  225. Console.WriteLine($"GRACZ 2: Wybierz pole: ");
  226. playerNumber = 1;
  227. }
  228. var previousFields = fields.Cast<string>();
  229. fieldName = Console.ReadLine();
  230. int result = getXO(fieldName, playerNumber, fields);
  231.  
  232. if (result != 0)
  233. {
  234. i++;
  235. }
  236. } while (loop(fields));
  237.  
  238. Console.WriteLine($"\n\n Gracz {playerNumber+1} wygrał!");
  239. Console.ReadKey();
  240. }
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement