Advertisement
mralasic

Untitled

Sep 11th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Online C# Compiler.
  4. Code, Compile, Run and Debug C# program online.
  5. Write your code in this editor and press "Run" button to execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15.  
  16. namespace ClashOfGiantsConsole
  17. {
  18. class Program
  19. {
  20. static void Main(string[] args)
  21. {
  22. // Point 1
  23. // variables that stores information about rolled pips in each round
  24. int playerPips = 0;
  25. int opponentPips = 0;
  26.  
  27. // Point 1.1
  28. // variables that stores amounts of win rounds
  29. int playerWins = 0;
  30. int computerWins = 0;
  31.  
  32. // Point 1.2
  33. // rounds' counter, it is used to create end game condition
  34. int round = 0;
  35.  
  36.  
  37. // Point 1.3
  38. // print out information that the game is starting
  39. Console.WriteLine("Hi! Do you want to shake the dice with me?");
  40. Console.WriteLine("What is your name?");
  41.  
  42. string name;
  43. name = Console.ReadLine();
  44.  
  45. Console.WriteLine($"Hello {name}");
  46. Console.WriteLine("Press any key to start");
  47.  
  48.  
  49.  
  50. // Point 1.4
  51. // read key freezes the application until user press any key
  52.  
  53.  
  54. // clear method removes everything from the console
  55. Console.Clear();
  56.  
  57. //to repeat game until somebody wins, do while loop can be used
  58. do
  59. {
  60. // Point 2
  61. // increment rounds counter
  62.  
  63.  
  64. Console.WriteLine($"Round: {round}");
  65. Console.WriteLine($"Your turn");
  66. Console.ReadKey();
  67. Console.Clear();
  68.  
  69. // draw number of pips rolled by player
  70. playerPips = RollDice();
  71. Console.WriteLine($"Your score: {playerPips}");
  72. Console.ReadKey();
  73. Console.Clear();
  74.  
  75. Console.WriteLine($"Round: {round}");
  76. Console.WriteLine($"My turn :)");
  77. Console.ReadKey();
  78. Console.Clear();
  79.  
  80. // now opponent's turn
  81. opponentPips = RollDice();
  82. Console.WriteLine($"My score: {opponentPips}");
  83. Console.ReadKey();
  84. Console.Clear();
  85.  
  86. // let's proceed to winner checking
  87. // if somebody won, update points
  88. // in case of a draw, nobody gets any points
  89. Console.WriteLine($"Let's see who won this round...");
  90. Console.ReadKey();
  91. Console.Clear();
  92.  
  93. // Point 3
  94. //first, check if player won
  95.  
  96. // Point 3.1
  97. //check if computer won
  98.  
  99. // Point 3.2
  100. //nobody won - it is 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.  
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement