Advertisement
Guest User

C# Noughts and Crosses

a guest
Mar 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.43 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 Noughts_and_crosses2
  8. {
  9. class Program
  10. {
  11. public void playerTurn()
  12. {
  13.  
  14. }
  15. static void Main(string[] args)
  16. {
  17.  
  18. string[,] game = new string[3, 3];
  19. for (int x = 0; x < game.GetLength(0); x++)
  20. {
  21. for (int y = 0; y < game.GetLength(1); y++)
  22. {
  23. game[x, y] = "-";
  24. }
  25. }
  26. int loop2 = 0;
  27. while (loop2 == 0)
  28. {
  29. int loop = 0;
  30. while (loop == 0)
  31. {
  32. Console.WriteLine("Where would you like to place a cross?");
  33. string position = Console.ReadLine();
  34.  
  35. if (position == "1")
  36. if (game[0, 0] != "-")
  37. {
  38. Console.WriteLine("There is already something in that slot");
  39. }
  40. else
  41. {
  42. game[0, 0] = "x";
  43.  
  44. loop = 1;
  45. }
  46. else if (position == "2")
  47. if (game[1, 0] != "-")
  48. {
  49. Console.WriteLine("There is already something in that slot");
  50. }
  51. else
  52. {
  53. game[1, 0] = "x";
  54.  
  55. loop = 1;
  56. }
  57. else if (position == "3")
  58. if (game[2, 0] != "-")
  59. {
  60. Console.WriteLine("There is already something in that slot");
  61. }
  62. else
  63. {
  64. game[2, 0] = "x";
  65.  
  66. loop = 1;
  67. }
  68. else if (position == "4")
  69. if (game[0, 1] != "-")
  70. {
  71. Console.WriteLine("There is already something in that slot");
  72. }
  73. else
  74. {
  75. game[0, 1] = "x";
  76.  
  77. loop = 1;
  78. }
  79. else if (position == "5")
  80. if (game[1, 1] != "-")
  81. {
  82. Console.WriteLine("There is already something in that slot");
  83. }
  84. else
  85. {
  86. game[1, 1] = "x";
  87.  
  88. loop = 1;
  89. }
  90. else if (position == "6")
  91. if (game[2, 1] != "-")
  92. {
  93. Console.WriteLine("There is already something in that slot");
  94. }
  95. else
  96. {
  97. game[2, 1] = "x";
  98.  
  99. loop = 1;
  100. }
  101. else if (position == "7")
  102. if (game[0, 2] != "-")
  103. {
  104. Console.WriteLine("There is already something in that slot");
  105. }
  106. else
  107. {
  108. game[0, 2] = "x";
  109.  
  110. loop = 1;
  111. }
  112. else if (position == "8")
  113. if (game[1, 2] != "-")
  114. {
  115. Console.WriteLine("There is already something in that slot");
  116. }
  117. else
  118. {
  119. game[1, 2] = "x";
  120. loop = 1;
  121. }
  122. else if (position == "9")
  123. if (game[2, 2] != "-")
  124. {
  125. Console.WriteLine("There is already something in that slot");
  126. }
  127. else
  128. {
  129. game[2, 2] = "x";
  130. loop = 1;
  131. }
  132.  
  133. }
  134. while(loop == 1)
  135. {
  136. if (game[0, 0] == "x" && game[0, 1] == "x" && game[0, 2] == "x")
  137. {
  138. Console.WriteLine("Crosses win!");
  139. loop = 9001;
  140. loop2 = 1006;
  141. }
  142. else if (game[1, 0] == "x" && game[1, 1] == "x" && game[1, 2] == "x")
  143. {
  144. Console.WriteLine("Crosses win!");
  145. loop = 9001;
  146. loop2 = 1006;
  147. }
  148. else if (game[2, 0] == "x" && game[2, 1] == "x" && game[2, 2] == "x")
  149. {
  150. Console.WriteLine("Crosses win!");
  151. loop = 9001;
  152. loop2 = 1006;
  153. }
  154. else if (game[0, 0] == "x" && game[1, 0] == "x" && game[2, 0] == "x")
  155. {
  156. Console.WriteLine("Crosses win!");
  157. loop = 9001;
  158. loop2 = 1006;
  159. }
  160. else if (game[0, 2] == "x" && game[1, 2] == "x" && game[2, 2] == "x")
  161. {
  162. Console.WriteLine("Crosses win!");
  163. loop = 9001;
  164. loop2 = 1006;
  165. }
  166. else if (game[0, 1] == "x" && game[1, 1] == "x" && game[2, 1] == "x")
  167. {
  168. Console.WriteLine("Crosses win!");
  169. loop = 9001;
  170. loop2 = 1006;
  171. }
  172. else if (game[0, 0] == "x" && game[1, 1] == "x" && game[2, 2] == "x")
  173. {
  174. Console.WriteLine("Crosses win!");
  175. loop = 9001;
  176. loop2 = 1006;
  177. }
  178. else if (game[2, 0] == "x" && game[1, 1] == "x" && game[0, 2] == "x")
  179. {
  180. Console.WriteLine("Crosses win!");
  181. loop = 9001;
  182. loop2 = 1006;
  183. }
  184. else
  185. {
  186. loop = 2;
  187. }
  188.  
  189. }
  190. while (loop == 2)
  191. {
  192. Random randNum = new Random();
  193. int randomNum = randNum.Next(1, 9);
  194.  
  195. if (randomNum == 1)
  196. if (game[0, 0] != "-")
  197. {
  198.  
  199. }
  200. else
  201. {
  202. game[0, 0] = "o";
  203. Console.WriteLine("{0}{1}{2}", game[0, 0], game[1, 0], game[2, 0]);
  204. Console.WriteLine("{0}{1}{2}", game[0, 1], game[1, 1], game[2, 1]);
  205. Console.WriteLine("{0}{1}{2}", game[0, 2], game[1, 2], game[2, 2]);
  206. loop = 3;
  207. }
  208. else if (randomNum == 2)
  209. if (game[1, 0] != "-")
  210. {
  211.  
  212. }
  213. else
  214. {
  215. game[1, 0] = "o";
  216. Console.WriteLine("{0}{1}{2}", game[0, 0], game[1, 0], game[2, 0]);
  217. Console.WriteLine("{0}{1}{2}", game[0, 1], game[1, 1], game[2, 1]);
  218. Console.WriteLine("{0}{1}{2}", game[0, 2], game[1, 2], game[2, 2]);
  219. loop = 3;
  220. }
  221. else if (randomNum == 3)
  222. if (game[2, 0] != "-")
  223. {
  224.  
  225. }
  226. else
  227. {
  228. game[2, 0] = "o";
  229. Console.WriteLine("{0}{1}{2}", game[0, 0], game[1, 0], game[2, 0]);
  230. Console.WriteLine("{0}{1}{2}", game[0, 1], game[1, 1], game[2, 1]);
  231. Console.WriteLine("{0}{1}{2}", game[0, 2], game[1, 2], game[2, 2]);
  232. loop = 3;
  233. }
  234. else if (randomNum == 4)
  235. if (game[0, 1] != "-")
  236. {
  237.  
  238. }
  239. else
  240. {
  241. game[0, 1] = "o";
  242. Console.WriteLine("{0}{1}{2}", game[0, 0], game[1, 0], game[2, 0]);
  243. Console.WriteLine("{0}{1}{2}", game[0, 1], game[1, 1], game[2, 1]);
  244. Console.WriteLine("{0}{1}{2}", game[0, 2], game[1, 2], game[2, 2]);
  245. loop = 3;
  246. }
  247. else if (randomNum == 5)
  248. if (game[1, 1] != "-")
  249. {
  250.  
  251. }
  252. else
  253. {
  254. game[1, 1] = "o";
  255. Console.WriteLine("{0}{1}{2}", game[0, 0], game[1, 0], game[2, 0]);
  256. Console.WriteLine("{0}{1}{2}", game[0, 1], game[1, 1], game[2, 1]);
  257. Console.WriteLine("{0}{1}{2}", game[0, 2], game[1, 2], game[2, 2]);
  258. loop = 3;
  259. }
  260. else if (randomNum == 6)
  261. if (game[2, 1] != "-")
  262. {
  263.  
  264. }
  265. else
  266. {
  267. game[2, 1] = "o";
  268. Console.WriteLine("{0}{1}{2}", game[0, 0], game[1, 0], game[2, 0]);
  269. Console.WriteLine("{0}{1}{2}", game[0, 1], game[1, 1], game[2, 1]);
  270. Console.WriteLine("{0}{1}{2}", game[0, 2], game[1, 2], game[2, 2]);
  271. loop = 3;
  272. }
  273. else if (randomNum == 7)
  274. if (game[0, 2] != "-")
  275. {
  276.  
  277. }
  278. else
  279. {
  280. game[0, 2] = "o";
  281. Console.WriteLine("{0}{1}{2}", game[0, 0], game[1, 0], game[2, 0]);
  282. Console.WriteLine("{0}{1}{2}", game[0, 1], game[1, 1], game[2, 1]);
  283. Console.WriteLine("{0}{1}{2}", game[0, 2], game[1, 2], game[2, 2]);
  284. loop = 3;
  285. }
  286. else if (randomNum == 8)
  287. if (game[1, 2] != "-")
  288. {
  289.  
  290. }
  291. else
  292. {
  293. game[1, 2] = "o";
  294. Console.WriteLine("{0}{1}{2}", game[0, 0], game[1, 0], game[2, 0]);
  295. Console.WriteLine("{0}{1}{2}", game[0, 1], game[1, 1], game[2, 1]);
  296. Console.WriteLine("{0}{1}{2}", game[0, 2], game[1, 2], game[2, 2]);
  297. loop = 3;
  298. }
  299. else if (randomNum == 9)
  300. if (game[2, 2] != "-")
  301. {
  302.  
  303. }
  304. else
  305. {
  306. game[2, 2] = "o";
  307. Console.WriteLine("{0}{1}{2}", game[0, 0], game[1, 0], game[2, 0]);
  308. Console.WriteLine("{0}{1}{2}", game[0, 1], game[1, 1], game[2, 1]);
  309. Console.WriteLine("{0}{1}{2}", game[0, 2], game[1, 2], game[2, 2]);
  310. loop = 3;
  311. }
  312. while (loop == 3)
  313. {
  314. if (game[0, 0] == "o" && game[0, 1] == "o" && game[0, 2] == "o")
  315. {
  316. Console.WriteLine("Noughts win!");
  317. loop = 9001;
  318. loop2 = 1006;
  319. }
  320. else if (game[1, 0] == "o" && game[1, 1] == "o" && game[1, 2] == "o")
  321. {
  322. Console.WriteLine("Noughts win!");
  323. loop = 9001;
  324. loop2 = 1006;
  325. }
  326. else if (game[2, 0] == "o" && game[2, 1] == "o" && game[2, 2] == "o")
  327. {
  328. Console.WriteLine("Noughts win!");
  329. loop = 9001;
  330. loop2 = 1006;
  331. }
  332. else if (game[0, 0] == "o" && game[1, 0] == "o" && game[2, 0] == "o")
  333. {
  334. Console.WriteLine("Noughts win!");
  335. loop = 9001;
  336. loop2 = 1006;
  337. }
  338. else if (game[0, 2] == "o" && game[1, 2] == "o" && game[2, 2] == "o")
  339. {
  340. Console.WriteLine("Noughts win!");
  341. loop = 9001;
  342. loop2 = 1006;
  343. }
  344. else if (game[0, 1] == "o" && game[1, 1] == "o" && game[2, 1] == "o")
  345. {
  346. Console.WriteLine("Noughts win!");
  347. loop = 9001;
  348. loop2 = 1006;
  349. }
  350. else if (game[0, 0] == "o" && game[1, 1] == "o" && game[2, 2] == "o")
  351. {
  352. Console.WriteLine("Noughts win!");
  353. loop = 9001;
  354. loop2 = 1006;
  355. }
  356. else if (game[2, 0] == "o" && game[1, 1] == "o" && game[0, 2] == "o")
  357. {
  358. Console.WriteLine("Noughts win!");
  359. loop = 9001;
  360. loop2 = 1006;
  361. }
  362. else
  363. {
  364. loop = 0;
  365. }
  366. }
  367. }
  368. Console.ReadLine();
  369.  
  370. }
  371. }
  372. }
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement