Advertisement
mralasic

mralasic

Sep 12th, 2021
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ClashOfGiantsConsole
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. // Point 1
  15. // variables that stores information about rolled pips in each round
  16. int playerPips = 0;
  17. int opponentPips = 0;
  18.  
  19.  
  20. // Point 1.1
  21. // variables that stores amounts of win rounds
  22. int playerWins = 0;
  23. int computerWins = 0;
  24.  
  25.  
  26. // Point 1.2
  27. // rounds' counter, it is used to create end game condition
  28. int round = 0;
  29.  
  30.  
  31. // Point 1.3
  32. // print out information that the game is starting
  33. Console.WriteLine("Hi! Do you want to shake the dice with me?");
  34.  
  35. string name;
  36. Console.WriteLine("What is your name?");
  37. name = Console.ReadLine();
  38.  
  39. Console.WriteLine($"Hello {name}");
  40. Console.WriteLine("Press any key to start");
  41.  
  42. // Point 1.4
  43. // read key freezes the application until user press any key
  44. Console.ReadKey();
  45.  
  46. // clear method removes everything from the console
  47. Console.Clear();
  48.  
  49. //to repeat game until somebody wins, do while loop can be used
  50. do
  51. {
  52. // Point 2
  53. // increment rounds counter
  54. round++;
  55.  
  56. Console.WriteLine($"Round: {round}");
  57. Console.WriteLine($"Your turn");
  58. Console.ReadKey();
  59. Console.Clear();
  60.  
  61. // draw number of pips rolled by player
  62. playerPips = RollDice();
  63. Console.WriteLine($"Your score: {playerPips}");
  64. Console.ReadKey();
  65. Console.Clear();
  66.  
  67. Console.WriteLine($"Round: {round}");
  68. Console.WriteLine($"My turn :)");
  69. Console.ReadKey();
  70. Console.Clear();
  71.  
  72. // now opponent's turn
  73. opponentPips = RollDice();
  74. Console.WriteLine($"My score: {opponentPips}");
  75. Console.ReadKey();
  76. Console.Clear();
  77.  
  78. // let's proceed to winner checking
  79. // if somebody won, update points
  80. // in case of a draw, nobody gets any points
  81. Console.WriteLine($"Let's see who won this round...");
  82. Console.ReadKey();
  83. Console.Clear();
  84.  
  85. // Point 3
  86. //first, check if player won
  87. if (playerPips > opponentPips){
  88. playerWins++;
  89. Console.WriteLine("This time you won.... ;( ");
  90. }
  91. // Point 3.1
  92. //check if computer won
  93. else if (opponentPips > playerPips){
  94. computerWins++;
  95. Console.WriteLine("Ha Ha!!! This round is mine :D");
  96. }
  97. // Point 3.2
  98. //nobody won - it is a draw
  99. else {
  100. Console.WriteLine("It's a draw!!!");
  101. }
  102.  
  103. Console.WriteLine($"Current score: You - {playerWins} : {computerWins} - Me");
  104. Console.ReadKey();
  105. Console.Clear();
  106.  
  107. // check, if it is 5th round
  108. // if so, check who have won and display appropriate message
  109. // if no, game go on
  110. if (round == 5)
  111. {
  112. if (playerWins > computerWins)
  113. {
  114. Console.WriteLine("Congratulations, you have won!");
  115. }
  116. else if (playerWins < computerWins)
  117. {
  118. Console.WriteLine("Unfortunately, you have lost!");
  119. }
  120. else
  121. {
  122. Console.WriteLine("It's a draw!");
  123. }
  124. Console.ReadKey();
  125. }
  126. } while (round < 5);
  127. }
  128.  
  129. //method to draw number of points in one roll
  130. //you can mention that this piece of code is common for the player an the opponent so you don't have to write the same method 2 times
  131. private static int RollDice()
  132. {
  133. //say a few words about Random class - it is a pseudo-random numbers generator
  134. //explain that drawn numbers are based on the system's time
  135. Random random = new Random();
  136.  
  137. //create a helper variable to store a result
  138. //the Next method takes minimum and maximum number from drawing range
  139. //however, it's important that maximum is not included in this range
  140. //players roll three dice so min = 3 and max = 19
  141. int drawnPoints = random.Next(3, 19);
  142.  
  143. return drawnPoints;
  144. }
  145. }
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement