Advertisement
mralasic

mralasic

Sep 12th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 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.  
  55.  
  56.  
  57. Console.WriteLine($"Round: {round}");
  58. Console.WriteLine($"Your turn");
  59. Console.ReadKey();
  60. Console.Clear();
  61.  
  62. // draw number of pips rolled by player
  63. playerPips = RollDice();
  64. Console.WriteLine($"Your score: {playerPips}");
  65. Console.ReadKey();
  66. Console.Clear();
  67.  
  68. Console.WriteLine($"Round: {round}");
  69. Console.WriteLine($"My turn :)");
  70. Console.ReadKey();
  71. Console.Clear();
  72.  
  73. // now opponent's turn
  74. opponentPips = RollDice();
  75. Console.WriteLine($"My score: {opponentPips}");
  76. Console.ReadKey();
  77. Console.Clear();
  78.  
  79. // let's proceed to winner checking
  80. // if somebody won, update points
  81. // in case of a draw, nobody gets any points
  82. Console.WriteLine($"Let's see who won this round...");
  83. Console.ReadKey();
  84. Console.Clear();
  85.  
  86. // Point 3
  87. //first, check if player won
  88.  
  89. // Point 3.1
  90. //check if computer won
  91.  
  92. // Point 3.2
  93. //nobody won - it is a draw
  94.  
  95.  
  96. Console.WriteLine($"Current score: You - {playerWins} : {computerWins} - Me");
  97. Console.ReadKey();
  98. Console.Clear();
  99.  
  100. // check, if it is 5th round
  101. // if so, check who have won and display appropriate message
  102. // if no, game go on
  103. if (round == 5)
  104. {
  105. if (playerWins > computerWins)
  106. {
  107. Console.WriteLine("Congratulations, you have won!");
  108. }
  109. else if (playerWins < computerWins)
  110. {
  111. Console.WriteLine("Unfortunately, you have lost!");
  112. }
  113. else
  114. {
  115. Console.WriteLine("It's a draw!");
  116. }
  117. Console.ReadKey();
  118. }
  119. } while (round < 5);
  120. }
  121.  
  122. //method to draw number of points in one roll
  123. //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
  124. private static int RollDice()
  125. {
  126. //say a few words about Random class - it is a pseudo-random numbers generator
  127. //explain that drawn numbers are based on the system's time
  128. Random random = new Random();
  129.  
  130. //create a helper variable to store a result
  131. //the Next method takes minimum and maximum number from drawing range
  132. //however, it's important that maximum is not included in this range
  133. //players roll three dice so min = 3 and max = 19
  134. int drawnPoints = random.Next(3, 19);
  135.  
  136. return drawnPoints;
  137. }
  138. }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement