Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Online C# Compiler.
- Code, Compile, Run and Debug C# program online.
- Write your code in this editor and press "Run" button to execute it.
- *******************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ClashOfGiantsConsole
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Point 1
- // variables that stores information about rolled pips in each round
- int playerPips = 0;
- int opponentPips = 0;
- // Point 1.1
- // variables that stores amounts of win rounds
- int playerWins = 0;
- int computerWins = 0;
- // Point 1.2
- // rounds' counter, it is used to create end game condition
- int round = 0;
- // Point 1.3
- // print out information that the game is starting
- Console.WriteLine("Hi! Do you want to shake the dice with me?");
- Console.WriteLine("What is your name?");
- string name;
- name = Console.ReadLine();
- Console.WriteLine($"Hello {name}");
- Console.WriteLine("Press any key to start");
- // Point 1.4
- // read key freezes the application until user press any key
- // clear method removes everything from the console
- Console.Clear();
- //to repeat game until somebody wins, do while loop can be used
- do
- {
- // Point 2
- // increment rounds counter
- Console.WriteLine($"Round: {round}");
- Console.WriteLine($"Your turn");
- Console.ReadKey();
- Console.Clear();
- // draw number of pips rolled by player
- playerPips = RollDice();
- Console.WriteLine($"Your score: {playerPips}");
- Console.ReadKey();
- Console.Clear();
- Console.WriteLine($"Round: {round}");
- Console.WriteLine($"My turn :)");
- Console.ReadKey();
- Console.Clear();
- // now opponent's turn
- opponentPips = RollDice();
- Console.WriteLine($"My score: {opponentPips}");
- Console.ReadKey();
- Console.Clear();
- // let's proceed to winner checking
- // if somebody won, update points
- // in case of a draw, nobody gets any points
- Console.WriteLine($"Let's see who won this round...");
- Console.ReadKey();
- Console.Clear();
- // Point 3
- //first, check if player won
- // Point 3.1
- //check if computer won
- // Point 3.2
- //nobody won - it is a draw
- Console.WriteLine($"Current score: You - {playerWins} : {computerWins} - Me");
- Console.ReadKey();
- Console.Clear();
- // check, if it is 5th round
- // if so, check who have won and display appropriate message
- // if no, game go on
- if (round == 5)
- {
- if (playerWins > computerWins)
- {
- Console.WriteLine("Congratulations, you have won!");
- }
- else if (playerWins < computerWins)
- {
- Console.WriteLine("Unfortunately, you have lost!");
- }
- else
- {
- Console.WriteLine("It's a draw!");
- }
- Console.ReadKey();
- }
- } while (round < 5);
- }
- //method to draw number of points in one roll
- //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
- private static int RollDice()
- {
- //say a few words about Random class - it is a pseudo-random numbers generator
- //explain that drawn numbers are based on the system's time
- Random random = new Random();
- //create a helper variable to store a result
- //the Next method takes minimum and maximum number from drawing range
- //however, it's important that maximum is not included in this range
- //players roll three dice so min = 3 and max = 19
- int drawnPoints = random.Next(3, 19);
- return drawnPoints;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement