Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.76 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. using System.Threading;
  7. using System.Media;
  8. using System.Speech.Synthesis;
  9. // A console app that will play Tic-Tac-Toe with you.
  10.  
  11. namespace Tic_Tac_Toe
  12. {
  13. class Program
  14. {
  15. //variables
  16. static string one = "1";
  17. static string two = "2";
  18. static string three = "3";
  19. static string four = "4";
  20. static string five = "5";
  21. static string six = "6";
  22. static string seven = "7";
  23. static string eight = "8";
  24. static string nine = "9";
  25. static Boolean gameResult = false;
  26. static Boolean UserOrPc = false; // false = User wins. true = PC wins.
  27. static int wins;
  28. static int totalGames;
  29. static int userChoice;
  30. static int botChoice;
  31. static int counter;
  32. static int vs;
  33.  
  34.  
  35. static SoundPlayer s = new SoundPlayer((Properties.Resources.TheFatRat___Epic));
  36. static void PlayMusic()
  37. {
  38. //a function that will handle the music playback
  39.  
  40. while (true)
  41. {
  42. s.Play();
  43. Thread.Sleep(179000);
  44. }
  45. }
  46.  
  47. static void CheckForWin()
  48. {
  49. //this function checks each turn if the user or the computer has won.
  50.  
  51. //checks if the user has won
  52. if(one == "X" && two == "X" && three == "X")
  53. {
  54. gameResult = true;
  55. }
  56. if ( four == "X" && five == "X" && six == "X")
  57. {
  58. gameResult = true;
  59. }
  60. if (seven == "X" && eight == "X" && nine == "X")
  61. {
  62. gameResult = true;
  63. }
  64. if (one == "X" && four == "X" && seven == "X")
  65. {
  66. gameResult = true;
  67. }
  68. if (two == "X" && five == "X" && eight == "X")
  69. {
  70. gameResult = true;
  71. }
  72. if (three == "X" && six == "X" && nine == "X")
  73. {
  74. gameResult = true;
  75. }
  76. if (one == "X" && five == "X" && nine == "X")
  77. {
  78. gameResult = true;
  79. }
  80. if (seven == "X" && five == "X" && three == "X")
  81. {
  82. gameResult = true;
  83. }
  84. //checks if the computer has won
  85. if (one == "O" && two == "O" && three == "O")
  86. {
  87. gameResult = true;
  88. UserOrPc = true;
  89. }
  90. if (four == "O" && five == "O" && six == "O")
  91. {
  92. gameResult = true;
  93. UserOrPc = true;
  94. }
  95. if (seven == "O" && eight == "O" && nine == "O")
  96. {
  97. gameResult = true;
  98. UserOrPc = true;
  99. }
  100. if (one == "O" && four == "O" && seven == "O")
  101. {
  102. gameResult = true;
  103. UserOrPc = true;
  104. }
  105. if (two == "O" && five == "O" && eight == "O")
  106. {
  107. gameResult = true;
  108. UserOrPc = true;
  109. }
  110. if (three == "O" && six == "O" && nine == "O")
  111. {
  112. gameResult = true;
  113. UserOrPc = true;
  114. }
  115. if (one == "O" && five == "O" && nine == "O")
  116. {
  117. gameResult = true;
  118. UserOrPc = true;
  119. }
  120. if (seven == "O" && five == "X" && three == "X")
  121. {
  122. gameResult = true;
  123. UserOrPc = true;
  124. }
  125.  
  126. }
  127.  
  128. static void DrawUI()
  129. {
  130. //this function draws the board and instructs the user on how to play
  131. Console.ForegroundColor = ConsoleColor.White;
  132. Console.WriteLine("Games Played:" + totalGames);
  133. Console.WriteLine(" " + "Wins:" + wins);
  134. Console.ForegroundColor = ConsoleColor.Yellow;
  135. Console.WriteLine(" " + one + "|"+ two + "|" + three);
  136. Console.WriteLine(" " + four + "|" + five + "|" + six);
  137. Console.WriteLine(" " + seven + "|" + eight + "|" + nine);
  138. Console.WriteLine("");
  139. Console.ForegroundColor = ConsoleColor.Magenta;
  140. Console.WriteLine("To play type the number you want to change to X and hit enter.");
  141. }
  142.  
  143. static void Main()
  144. {
  145. SpeechSynthesizer synth = new SpeechSynthesizer();
  146. Random r = new Random();
  147. Thread MusicWorker = new Thread(PlayMusic);
  148. MusicWorker.Start();
  149.  
  150. Main:
  151. {
  152. //reseting vars
  153. one = "1";
  154. two = "2";
  155. three = "3";
  156. four = "4";
  157. five = "5";
  158. six = "6";
  159. seven = "7";
  160. eight = "8";
  161. nine = "9";
  162. vs = 0;
  163. userChoice = 0;
  164. botChoice = 0;
  165. counter = 0;
  166. gameResult = false;
  167. UserOrPc = false;
  168.  
  169. Console.ForegroundColor = ConsoleColor.Red;
  170. Console.WriteLine("Tic Tac Toe v1.0 by Kalin Lalov");
  171. Console.ForegroundColor = ConsoleColor.White;
  172. Console.WriteLine("-------------------------------------------------------");
  173. Console.ForegroundColor = ConsoleColor.Green;
  174. Thread.Sleep(150);
  175. Console.WriteLine("");
  176. Console.WriteLine("Type 0 to play vs the computer or 1 to play vs another player...");
  177. try
  178. {
  179. vs = int.Parse(Console.ReadLine());
  180. if (vs < 0 || vs > 1)
  181. {
  182. vs = 0;
  183. Console.Clear();
  184. Console.WriteLine("The specified value is incorrect. Type either 0 or 1");
  185. Thread.Sleep(2500);
  186. Console.Clear();
  187. goto Main;
  188.  
  189. }
  190. }
  191. catch (Exception)
  192. {
  193. Console.Clear();
  194. Console.WriteLine("That`s not a number!");
  195. Thread.Sleep(2000);
  196. vs = 0;
  197. goto Main;
  198. }
  199. if(vs == 1)
  200. {
  201.  
  202. }
  203. Console.Clear();
  204. UsersTurn:
  205. {
  206. Console.Clear();
  207. DrawUI();
  208. try
  209. {
  210. userChoice = int.Parse(Console.ReadLine());
  211.  
  212. if (userChoice < 1 || userChoice > 9)
  213. {
  214. userChoice = 0;
  215. Console.Clear();
  216. Console.WriteLine("Wrong.You must enter a number between 1 and 9");
  217. Thread.Sleep(4000);
  218. Console.Clear();
  219. goto UsersTurn;
  220.  
  221. }
  222. switch (userChoice)
  223. {
  224.  
  225. case 1:
  226.  
  227. if (one == "X" || one == "O")
  228. {
  229. Console.Clear();
  230. Console.WriteLine("This slot is already taken. Choose another one.");
  231. Thread.Sleep(4000);
  232. userChoice = 0;
  233. Console.Clear();
  234. goto UsersTurn;
  235. }
  236. one = "X";
  237.  
  238. break;
  239. case 2:
  240. if (two == "X" || two == "O")
  241. {
  242. Console.Clear();
  243. Console.WriteLine("This slot is already taken. Choose another one.");
  244. Thread.Sleep(4000);
  245. userChoice = 0;
  246. Console.Clear();
  247. goto UsersTurn;
  248. }
  249. two = "X";
  250. break;
  251. case 3:
  252. if (three == "X" || three == "O")
  253. {
  254. Console.Clear();
  255. Console.WriteLine("This slot is already taken. Choose another one.");
  256. Thread.Sleep(4000);
  257. userChoice = 0;
  258. Console.Clear();
  259. goto UsersTurn;
  260. }
  261. three = "X";
  262. break;
  263. case 4:
  264. if (four == "X" || four == "O")
  265. {
  266. Console.Clear();
  267. Console.WriteLine("This slot is already taken. Choose another one.");
  268. Thread.Sleep(4000);
  269. userChoice = 0;
  270. Console.Clear();
  271. goto UsersTurn;
  272. }
  273. four = "X";
  274. break;
  275. case 5:
  276. if (five == "X" || five == "O")
  277. {
  278. Console.Clear();
  279. Console.WriteLine("This slot is already taken. Choose another one.");
  280. Thread.Sleep(4000);
  281. userChoice = 0;
  282. Console.Clear();
  283. goto UsersTurn;
  284. }
  285. five = "X";
  286. break;
  287. case 6:
  288. if (six == "X" || six == "O")
  289. {
  290. Console.Clear();
  291. Console.WriteLine("This slot is already taken. Choose another one.");
  292. Thread.Sleep(4000);
  293. userChoice = 0;
  294. Console.Clear();
  295. goto UsersTurn;
  296. }
  297. six = "X";
  298. break;
  299. case 7:
  300. if (seven == "X" || seven == "O")
  301. {
  302. Console.Clear();
  303. Console.WriteLine("This slot is already taken. Choose another one.");
  304. Thread.Sleep(4000);
  305. userChoice = 0;
  306. Console.Clear();
  307. goto UsersTurn;
  308. }
  309. seven = "X";
  310. break;
  311. case 8:
  312. if (eight == "X" || eight == "O")
  313. {
  314. Console.Clear();
  315. Console.WriteLine("This slot is already taken. Choose another one.");
  316. Thread.Sleep(4000);
  317. userChoice = 0;
  318. Console.Clear();
  319. goto UsersTurn;
  320. }
  321. eight = "X";
  322. break;
  323. case 9:
  324. if (nine == "X" || nine == "O")
  325. {
  326. Console.Clear();
  327. Console.WriteLine("This slot is already taken. Choose another one.");
  328. Thread.Sleep(4000);
  329. userChoice = 0;
  330. Console.Clear();
  331. goto UsersTurn;
  332. }
  333. nine = "X";
  334. break;
  335.  
  336.  
  337. }
  338.  
  339.  
  340. }
  341. catch/* em all */(Exception)
  342. {
  343. Console.Clear();
  344. Console.WriteLine("That`s not a number!");
  345. Thread.Sleep(2000);
  346. userChoice = 0;
  347. Console.Clear();
  348. goto UsersTurn;
  349.  
  350.  
  351. }
  352. }
  353.  
  354. ComputerChooses:
  355. {
  356. botChoice = r.Next(1, 9);
  357. switch (botChoice)
  358. {
  359.  
  360. case 1:
  361. if (one == "X" || one == "O")
  362. {
  363. botChoice = 0;
  364. goto ComputerChooses;
  365. }
  366. one = "O";
  367. break;
  368. case 2:
  369. if (two == "X" || two == "O")
  370. {
  371. botChoice = 0;
  372. goto ComputerChooses;
  373. }
  374. two = "O";
  375. break;
  376. case 3:
  377. if (three == "X" || three == "O")
  378. {
  379. botChoice = 0;
  380. goto ComputerChooses;
  381. }
  382. three = "O";
  383. break;
  384. case 4:
  385. if (four == "X" || four == "O")
  386. {
  387. botChoice = 0;
  388. goto ComputerChooses;
  389. }
  390. four = "O";
  391. break;
  392. case 5:
  393. if (five == "X" || five == "O")
  394. {
  395. botChoice = 0;
  396. goto ComputerChooses;
  397. }
  398. five = "O";
  399. break;
  400. case 6:
  401. if (six == "X" || six == "O")
  402. {
  403. botChoice = 0;
  404. goto ComputerChooses;
  405. }
  406. six = "O";
  407. break;
  408. case 7:
  409. if (seven == "X" || seven == "O")
  410. {
  411. botChoice = 0;
  412. goto ComputerChooses;
  413. }
  414. seven = "O";
  415. break;
  416. case 8:
  417. if (eight == "X" || eight == "O")
  418. {
  419. botChoice = 0;
  420. goto ComputerChooses;
  421. }
  422. eight = "O";
  423. break;
  424. case 9:
  425. if (nine == "X" || nine == "O")
  426. {
  427. botChoice = 0;
  428. goto ComputerChooses;
  429. }
  430. nine = "O";
  431. break;
  432.  
  433. }
  434. Console.Clear();
  435. DrawUI();
  436. CheckForWin();
  437. counter++;
  438. if (gameResult == true)
  439. {
  440. Thread.Sleep(2000);
  441. if (UserOrPc == true)
  442. {
  443. //PC wins
  444. Console.Clear();
  445. Console.ForegroundColor = ConsoleColor.Red;
  446. Console.WriteLine("You lost! Press any key to try again");
  447. synth.Speak("You lost! Press any key to try again");
  448. totalGames++;
  449. Console.ReadKey();
  450. Console.Clear();
  451. goto Main;
  452. }
  453. else
  454. {
  455. //User wins
  456. Console.Clear();
  457. Console.ForegroundColor = ConsoleColor.Red;
  458. Console.WriteLine("You won! Press any key to play again");
  459. synth.Speak("You won!!! Press any key to play again");
  460. totalGames++;
  461. wins++;
  462. Console.ReadKey();
  463. Console.Clear();
  464. goto Main;
  465. }
  466.  
  467. }
  468. if (counter == 4)
  469. { //draw. No one has won.
  470. CheckForWin();
  471. if (gameResult == false)
  472. {
  473. Thread.Sleep(2000);
  474. Console.Clear();
  475. Console.WriteLine("Draw. Press any key to play again");
  476. Console.ReadKey();
  477. Console.Clear();
  478. totalGames++;
  479. goto Main;
  480. }
  481. }
  482. goto UsersTurn;
  483. }
  484. }
  485. }
  486. }
  487. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement