Advertisement
Guest User

code

a guest
Apr 26th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Game : MonoBehaviour {
  6.  
  7. // TODO the last answer doesn't disappear
  8. // TODO countries with long names are don't have the right appearance
  9.  
  10. // Lists of countries
  11. string[] countriesBeginner = { "France", "Italy", "The United Kingdom", "Germany", "United States", "Spain", "Russia", "Japan", "China", "Brazil", "Mexico", "Portugal", "India", "Argentina", "Canada", "Australia", "South Korea"};
  12. string[] countriesEasy = { "Ireland", "Norway", "Greece" , "Austria", "Belgium", "Netherlands", "Vatican", "Denmark", "Finland", "Hungary", "Czech Republic", "Egypt", "Poland", "Sweden", "Monaco", "Peru", "Switzerland", "Luxembourg", "Ukraine", "Israel", "Iceland"};
  13. string[] countriesMedium = { "Iraq", "Bulgaria", "Cuba", "Chile", "Thailand", "Singapore", "Afganistan", "Iran", "San Marino", "Turkey", "New Zealand", "Jamaica", "South Africa", "Panama", "Belarus", "Colombia", "Croatia", "Indonesia", "Romania", "Estonia", "Venezuela", "Latvia", "Philippines", "Bolivia", "North Korea", "Uruguay", "Bosnia and Herzegovina" };
  14. string[] countriesHard = { "Malaysia", "Pakistan", "Albania", "Ecuador", "United Arab Emirates", "Vietnam", "Kuwait", "Syria", "El Salvador", "Serbia", "Taiwan", "Algeria", "Kenya", "Nepal", "Haiti", "Lebanon", "Slovakia", "Tunisia", "Andorra", "Lithuania", "Malta", "Bangladesh", "Jordan", "Mongolia", "Saudi Arabia", "Azerbaijan", "Cyprus", "Guatemala", "Qatar", "Macedonia", "Libya", "Paraguay", "Slovenia", "Cambodia", "Ethiopia", "Guyana", "Liechtenstein", "Morocco", "Costa Rica", "Georgia", "Kosovo", "Bahamas", "Kazakhstan", "Yemen", "Armenia", "Dominican Republic", "Nicaragua", "Oman", "Senegal", "Sierra Leone", "Maldives", "Moldova", "Ghana" };
  15. string[] countriesExpert = { "Saint Vincent and the Grenadines", "Djibouti", "Montenegro", "Suriname", "East Timor", "Somalia", "Trinidad and Tobago", "Zimbabwe", "Bahrain", "Democratic Republic of Congo", "Laos", "Angola", "Barbados", "Belize", "Fiji", "Honduras", "Uzbekistan", "Antigua and Barbuda", "Bhutan", "Republic of the Congo", "Guinea-Bissau", "Sao Tome and Principe", "Sudan", "Nigeria", "Papua New Guinea", "Seychelles", "Grenada", "Kyrgyztan", "Liberia" };
  16. string[] countriesExtreme = { "Madagascar", "Namibia", "Brunei", "Burkina Faso", "Uganda", "Gabon", "Rwanda", "South Sudan", "Turkmenistan", "Botswana", "Cameroon", "Dominica", "Eritrea", "Mozambique", "Tajikistan", "Togo", "Zambia", "Benin", "Mali", "Mauritius", "Samoa", "Sri Lanka", "Saint Lucia", "Myanmar", "Cape Verde", "Central African Republic", "Chad", "Guinea", "Vanuatu", "Burundi", "Comoros", "Gambia", "Malawi", "Niger", "Saint Kitts and Nevis", "Tanzania", "Lesotho", "Swaziland", "Tuvalu", "Cote d'lvoire", "Equatorial Guinea", "Mauritania", "Nauru", "Kiribati", "Marshall Islands", "Federated States of Micronesia", "Solomon Islands", "Tonga", "Palau" };
  17.  
  18. // Lists of capitals
  19. string[] capitalsBeginner = { "paris", "rome", "london", "berlin", "washington dc", "madrid", "moscow", "tokyo", "beijing", "brasilia", "mexico city", "lisbon", "new delhi", "buenos aires", "ottawa", "canberra", "seoul" };
  20. string[] capitalsEasy = { "dublin", "oslo", "athens", "vienna", "brussels", "amsterdam", "vatican city", "copenhagen", "helsinki", "budapest", "prague", "cairo", "warsaw", "stockholm", "monaco", "lima", "bern", "luxembourg", "kiev", "jerusalem", "reykjavik" };
  21. string[] capitalsMedium = { "baghdad", "sofia", "havana", "santiago", "bangkok", "singapore", "kabul", "tehran", "san marino", "ankara", "wellington", "kingston", "cape town", "panama city", "minsk", "bogota", "zagreb", "jakarta", "bucharest", "tallinn", "caracas", "riga", "manila", "la paz", "pyongyang", "montevideo", "sarajevo"};
  22. string[] capitalsHard = { "kuala lumpur", "islamabad", "tirana", "quito", "abu dhabi", "hanoi", "kuwait city", "damascus", "san salvador", "belgrade", "taipei", "algiers", "nairobi", "kathmandu", "port-au-prince", "beirut", "bratislava", "tunis", "andorra la vella", "vilnius", "valletta", "dhaka", "amman", "ulaanbaatar", "riyadh", "baku", "nicosia", "guatemala city", "doha", "skopje", "tripoli", "asuncion", "ljubljana", "phnom penh", "addis ababa", "georgetown", "vaduz", "rabat", "san jose", "tbilisi", "pristina", "nassau", "astana", "sana'a", "yerevan", "santo domingo", "managua", "muscat", "dakar", "freetown", "male", "chisinau", "accra" };
  23. string[] capitalsExpert = { "kingstown", "djibouti", "podgorica", "paramaribo", "dili", "mogadishu", "port of spain", "harare", "manama", "kinshasa", "vientiane", "luanda", "bridgetown", "belmopan", "suva", "tegucigalpa", "tashkent", "saint john's", "thimphu", "brazzaville", "bissau", "sao tome", "khartoum", "abuja", "port moresby", "victoria", "saint georges", "bishkek", "monrovia" };
  24. string[] capitalsExtreme = { "antananarivo", "windhoek", "bandar seri begawan", "ouagadougou", "kampala", "libreville", "kigali", "juba", "ashgabat", "gaborone", "yaounde", "roseau", "asmara", "maputo", "dushanbe", "lome", "lusaka", "porto novo", "bamako", "port louis", "apia", "sri jayawardenera pura kotte", "castries", "naypyidaw", "praia", "bangui", "n'djamena", "conakry", "port vila", "bujumbura", "moroni", "banjul", "lilongwe", "niamey", "basseterre", "dodoma", "maseru", "mbabane", "funafuti", "yamoussoukro", "malabo", "nouakchott", "yaren", "south tarawa", "majuro", "palikir", "honiara", "nuku'alofa", "ngerulmud" };
  25.  
  26. // Game state
  27. int level;
  28. enum Screen { Start, Help, MainMenu, Capital, Win, Records};
  29. Screen currentScreen;
  30. string currentCountry;
  31. string currentCapital;
  32.  
  33. // For counting input and answers
  34. int item = 0;
  35. int remaining = 0;
  36. int right = 0;
  37. int wrong = 0;
  38. int hints = 5;
  39.  
  40. // Resets the variables above
  41. void resetVariables()
  42. {
  43. right = 0;
  44. wrong = 0;
  45. item = 0;
  46. remaining = 0;
  47. hints = 5;
  48. }
  49.  
  50. // For keeping track of high scores
  51. int scoreBeginner;
  52. int scoreEasy;
  53. int scoreMedium;
  54. int scoreHard;
  55. int scoreExpert;
  56. int scoreExtreme;
  57.  
  58. // Other
  59. const string menuHint = "Type menu to get back to the start";
  60. int currentLevel;
  61.  
  62. void Start ()
  63. {
  64. showStartMenu();
  65. }
  66.  
  67. // Used to check player's input
  68. // Programs response depends on the current screen
  69. // Typing menu will alwasy bring the player to the start menu
  70. void OnUserInput(string input)
  71. {
  72. if (input == "menu") // we can always go to the main menu
  73. {
  74. showStartMenu();
  75. resetVariables();
  76. }
  77. else if (currentScreen == Screen.Start)
  78. {
  79. runStartMenu(input);
  80. }
  81. else if (currentScreen == Screen.MainMenu)
  82. {
  83. runMainMenu(input);
  84. }
  85. else if (currentScreen == Screen.Capital)
  86. {
  87. runGame(input);
  88. }
  89. else if (currentScreen == Screen.Win)
  90. {
  91. playAgain(input);
  92. }
  93. else if (currentScreen == Screen.Records)
  94. {
  95. runRecords(input);
  96. }
  97. else if (currentScreen == Screen.Help)
  98. {
  99. runHelp(input);
  100. }
  101. }
  102.  
  103. // Used to run the start screen
  104. // If the player inputs 1-3 the program goes to the corresponding screen
  105. // If the player inputs anything else the program resets the screen
  106. void runStartMenu(string input)
  107. {
  108. if (input == "1")
  109. {
  110. showMainMenu();
  111. }
  112. else if (input == "2")
  113. {
  114. showHelp();
  115. }
  116. else if (input == "3")
  117. {
  118. showRecords();
  119. }
  120. else
  121. {
  122. showStartMenu();
  123. }
  124. }
  125.  
  126. // Clears the screen and displays the start menu
  127. void showStartMenu()
  128. {
  129. currentScreen = Screen.Start;
  130. Terminal.ClearScreen();
  131. Terminal.WriteLine("WELCOME TO THE CAPITAL QUIZ");
  132. Terminal.WriteLine("");
  133. Terminal.WriteLine("1 - Select difficulty");
  134. Terminal.WriteLine("2 - See instructions");
  135. Terminal.WriteLine("3 - High Scores");
  136. Terminal.WriteLine("");
  137. Terminal.WriteLine("Choose your selection by entering the");
  138. Terminal.WriteLine("number");
  139. Terminal.WriteLine("");
  140. }
  141.  
  142. // Used to run the main menu
  143. // Converts player's input to an Int and checks if corresponding difficulty level exists
  144. // If the level exists program starts the game
  145. void runMainMenu(string input)
  146. {
  147. bool isValidLevelNumber = (input == "1" || input == "2" || input == "3" || input == "4" || input == "5" || input == "6");
  148. if (isValidLevelNumber)
  149. {
  150. level = int.Parse(input);
  151. currentLevel = level;
  152. setRemaining();
  153. askForCapital();
  154. print(item);
  155. print(countriesBeginner[item]);
  156. }
  157. else
  158. {
  159. showMainMenu();
  160. }
  161. }
  162.  
  163. // Clears the screen and displays the menu
  164. void showMainMenu()
  165. {
  166. currentScreen = Screen.MainMenu;
  167. Terminal.ClearScreen();
  168. Terminal.WriteLine("CHOOSE DIFFICULTY");
  169. Terminal.WriteLine("");
  170. Terminal.WriteLine("1 - Beginner");
  171. Terminal.WriteLine("2 - Easy");
  172. Terminal.WriteLine("3 - Medium");
  173. Terminal.WriteLine("4 - Hard");
  174. Terminal.WriteLine("5 - Expert");
  175. Terminal.WriteLine("6 - Extreme");
  176. Terminal.WriteLine("");
  177. Terminal.WriteLine("Please enter your selection:");
  178. Terminal.WriteLine("");
  179. }
  180.  
  181. // Used for running the game
  182. // Checks the player's input and manages the hints
  183. // Checks if the player's answer is correct or false
  184. void runGame(string input)
  185. {
  186. if (input == "hint" && hints > 0)
  187. {
  188. hints--;
  189. askForCapital();
  190. Terminal.WriteLine("HINT: " + currentCapital.Anagram());
  191. }
  192. else if (input == "hint" && hints <= 0)
  193. {
  194. askForCapital();
  195. Terminal.WriteLine("You are out hints");
  196. }
  197. else if (currentCapital == input)
  198. {
  199. right++;
  200. item++;
  201. askForCapital();
  202. }
  203. else
  204. {
  205. wrong++;
  206. item++;
  207. askForCapital();
  208. }
  209. }
  210.  
  211. // Used to run the Records screen
  212. void runRecords(string input)
  213. {
  214. if (input == "menu")
  215. {
  216. showStartMenu();
  217. resetVariables();
  218. }
  219. else
  220. {
  221. showRecords();
  222. }
  223. }
  224.  
  225. void runHelp(string input)
  226. {
  227. if (input == "menu")
  228. {
  229. showStartMenu();
  230. resetVariables();
  231. }
  232. else
  233. {
  234. showHelp();
  235. }
  236. }
  237.  
  238. // If player types play after answering all the questions the program starts the game again
  239. void playAgain(string input)
  240. {
  241. if (input == "play")
  242. {
  243. level = currentLevel;
  244. resetVariables();
  245. setRemaining();
  246. askForCapital();
  247. print(item);
  248. print(countriesBeginner[item]);
  249. }
  250. else
  251. {
  252. displayWinScreen();
  253. }
  254. }
  255.  
  256. // Displays insturctions
  257. void showHelp()
  258. {
  259. currentScreen = Screen.Help;
  260. Terminal.ClearScreen();
  261. Terminal.WriteLine("INSTRUCTIONS");
  262. Terminal.WriteLine("");
  263. Terminal.WriteLine("You may type menu at any point to get");
  264. Terminal.WriteLine("back to the start");
  265. Terminal.WriteLine("");
  266. Terminal.WriteLine("All capital names exclude all capital");
  267. Terminal.WriteLine("letters, dots, dashes and letters not");
  268. Terminal.WriteLine("in the English alphabet");
  269. Terminal.WriteLine("");
  270. Terminal.WriteLine("Typing hint turns the correct capital");
  271. Terminal.WriteLine("name into an anagram");
  272. }
  273.  
  274. // Calculates player's records and clear rates and displays them
  275. void showRecords()
  276. {
  277. currentScreen = Screen.Records;
  278. Terminal.ClearScreen();
  279. string rateBeginner = Math.Round(((double)scoreBeginner / countriesBeginner.Length) * 100, 1).ToString();
  280. string rateEasy = Math.Round(((double)scoreEasy / countriesEasy.Length) * 100, 1).ToString();
  281. string rateMedium = Math.Round(((double)scoreMedium / countriesMedium.Length) * 100, 1).ToString();
  282. string rateHard = Math.Round(((double)scoreHard / countriesHard.Length) * 100, 1).ToString();
  283. string rateExpert = Math.Round(((double)scoreExpert / countriesExpert.Length) * 100, 1).ToString();
  284. string rateExtreme = Math.Round(((double)scoreExtreme / countriesExtreme.Length) * 100, 1).ToString();
  285. Terminal.WriteLine("YOUR HIGH SCORES");
  286. Terminal.WriteLine("");
  287. Terminal.WriteLine("Beginner: [" + scoreBeginner + "/" + countriesBeginner.Length + "] " + rateBeginner + "%");
  288. Terminal.WriteLine("Easy: [" + scoreEasy + "/" + countriesEasy.Length + "] " + rateEasy + "%");
  289. Terminal.WriteLine("Medium: [" + scoreMedium + "/" + countriesMedium.Length + "] " + rateMedium + "%");
  290. Terminal.WriteLine("Hard: [" + scoreHard + "/" + countriesHard.Length + "] " + rateHard + "%");
  291. Terminal.WriteLine("Expert: [" + scoreExpert + "/" + countriesExpert.Length + "] " + rateExpert + "%");
  292. Terminal.WriteLine("Extreme: [" + scoreExtreme + "/" + countriesExtreme.Length + "] " + rateExtreme + "%");
  293. Terminal.WriteLine("");
  294. Terminal.WriteLine(menuHint);
  295. Terminal.WriteLine("");
  296. }
  297.  
  298. // Sets remaining variable's value depending on how many items the corresponding list has
  299. private void setRemaining()
  300. {
  301. switch (level)
  302. {
  303. case 1:
  304. remaining = countriesBeginner.Length;
  305. break;
  306. case 2:
  307. remaining = countriesEasy.Length;
  308. break;
  309. case 3:
  310. remaining = countriesMedium.Length;
  311. break;
  312. case 4:
  313. remaining = countriesHard.Length;
  314. break;
  315. case 5:
  316. remaining = countriesExpert.Length;
  317. break;
  318. case 6:
  319. remaining = countriesExtreme.Length;
  320. break;
  321. }
  322. }
  323.  
  324. // Asks the player for an answer
  325. // If the player has answered all the questions the program dislplays the win screen
  326. void askForCapital()
  327. {
  328. currentScreen = Screen.Capital;
  329. Terminal.ClearScreen();
  330. countAnswers();
  331. setCountry();
  332. setCapital();
  333. Terminal.WriteLine("");
  334. Terminal.WriteLine("HINTS: " + hints + "/5");
  335. Terminal.WriteLine("======================================");
  336. Terminal.WriteLine("What is the capital of " + currentCountry);
  337. Terminal.WriteLine("Type hint for help");
  338. Terminal.WriteLine("");
  339. if (right + wrong == remaining)
  340. {
  341. displayWinScreen();
  342. }
  343. }
  344.  
  345. // Counts answers and hints and returns a string value including them
  346. void countAnswers()
  347. {
  348. string answers = "RIGHT: " + right + " WRONG: " + wrong + " Answered: " + item + "/" + remaining;
  349. Terminal.WriteLine(answers);
  350. if (right + wrong == remaining)
  351. {
  352. displayWinScreen();
  353. }
  354. }
  355.  
  356. // Sets a value currentCountry
  357. void setCountry()
  358. {
  359. switch (level)
  360. {
  361. case 1:
  362. currentCountry = countriesBeginner[item];
  363. break;
  364. case 2:
  365. currentCountry = countriesEasy[item];
  366. break;
  367. case 3:
  368. currentCountry = countriesMedium[item];
  369. break;
  370. case 4:
  371. currentCountry = countriesHard[item];
  372. break;
  373. case 5:
  374. currentCountry = countriesExpert[item];
  375. break;
  376. case 6:
  377. currentCountry = countriesExtreme[item];
  378. break;
  379. }
  380. }
  381.  
  382. // Sets a value for currentCapital
  383. void setCapital()
  384. {
  385. switch (level)
  386. {
  387. case 1:
  388. currentCapital = capitalsBeginner[item];
  389. break;
  390. case 2:
  391. currentCapital = capitalsEasy[item];
  392. break;
  393. case 3:
  394. currentCapital = capitalsMedium[item];
  395. break;
  396. case 4:
  397. currentCapital = capitalsHard[item];
  398. break;
  399. case 5:
  400. currentCapital = capitalsExpert[item];
  401. break;
  402. case 6:
  403. currentCapital = capitalsExtreme[item];
  404. break;
  405. default:
  406. Debug.LogError("Invalid level number");
  407. break;
  408. }
  409. }
  410.  
  411. // Displays the win screen including the player's score
  412. void displayWinScreen()
  413. {
  414. currentScreen = Screen.Win;
  415. Terminal.ClearScreen();
  416. compareScores();
  417. Terminal.WriteLine("RIGHT: " + right + " WRONG: " + wrong);
  418. Terminal.WriteLine("");
  419. Terminal.WriteLine("type play to play again or type menu toget back to the start");
  420. Terminal.WriteLine("");
  421. }
  422.  
  423. // If the new score is higher than the high score the program sets a new value for the corresponding level's high score
  424. private void compareScores()
  425. {
  426. switch (level)
  427. {
  428. case 1:
  429. if (right > scoreBeginner)
  430. {
  431. scoreBeginner = right;
  432. Terminal.WriteLine("NEW HIGH SCORE!");
  433. Terminal.WriteLine("");
  434. }
  435. break;
  436. case 2:
  437. if (right > scoreEasy)
  438. {
  439. scoreEasy = right;
  440. Terminal.WriteLine("NEW HIGH SCORE!");
  441. Terminal.WriteLine("");
  442. }
  443. break;
  444. case 3:
  445. if (right > scoreMedium)
  446. {
  447. scoreMedium = right;
  448. Terminal.WriteLine("NEW HIGH SCORE!");
  449. Terminal.WriteLine("");
  450. }
  451. break;
  452. case 4:
  453. if (right > scoreHard)
  454. {
  455. scoreHard = right;
  456. Terminal.WriteLine("NEW HIGH SCORE!");
  457. Terminal.WriteLine("");
  458. }
  459. break;
  460. case 5:
  461. if (right > scoreExpert)
  462. {
  463. scoreExpert = right;
  464. Terminal.WriteLine("NEW HIGH SCORE!");
  465. Terminal.WriteLine("");
  466. }
  467. break;
  468. case 6:
  469. if (right > scoreExtreme)
  470. {
  471. scoreExtreme = right;
  472. Terminal.WriteLine("NEW HIGH SCORE!");
  473. Terminal.WriteLine("");
  474. }
  475. break;
  476. }
  477. }
  478. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement