I get it’s a simple project and it’s done well but I’d probably create a Player object with randomNum and Points variables with get ; set to show the fundamentals of OOP.
Instead of playerPoints++, it'd be playerPoints += playerRandomNum. And for the enemy, instead of enemyPoints++, you'd need to change it to enemyPoints += enemyRandomNum. This will add on the points the player or enemy have rolled, instead of just increasing their points by 1. Does that answer your question?
Quick question. I recently learned C# so I understand all of this but when you call Random() maybe explaining what Random is would be a bit better. Like saying it's a built-in function. had to go thru the docs to figure out what it was.
How do you make this into a 3 player game? I tried doing it myself but only player 1 and player 2 works, if player 3 gets a higher number than the other two he still doesn't get a point
You need to change the if statements to accommodate for more than 2 players. To find out who has won a round you could firstly find out the highest score rolled from all players and store it inside an int. Then you could check which player(s) scored this number and then assign them a point. (Don't forget to check for the case that all players roll the same number and thus no one would score a point at all!) All of this would be easier to achieve if the scores were held in an array. Firstly, set up two arrays of size 3 to hold the numbers rolled for each player and the total points for all players - int playerCount = 3; // Used to represent the number of players wanted in game int[] numbersRolled = new int[playerCount]; int[] playerPoints = new int[playerCount]; numbersRolled[0] would represent the number player 1 has rolled. numbersRolled[1] would represent the number player 2 has rolled, and numbersRolled[2] would represent the number player 3 has rolled. Then playerPoints[0] would represent player 1's current score, playerPoints[1] would be player 2's current score and playerPoints[2] would be player 3's current score. In the game loop, create a new for loop (using j instead of i) with it's condition being j < playerCount This for loop will then repeat for each player. for(int j = 0; j < playerCount; j++) { Console.WriteLine("Player " + (j + 1) + ", press any key to roll the dice."); Console.ReadKey(); numbersRolled[j] = random.Next(1, 7); Console.WriteLine("Player " + (j + 1) + " rolled a " + numbersRolled[j]); Console.WriteLine(); } The number each player has rolled is stored inside the numbersRolled array. We can use this later on to check who has rolled the highest and therefore scores a point. Now, let's check the case that all player's have rolled the same number - if(numbersRolled[0] == numbersRolled[1] && numbersRolled[1] == numbersRolled[2]) { Console.WriteLine("It's a draw!"); } So, here we check if player1's score is the same as player2's score and if player's 2 score is the same as player3's score. If this is true, then we can just print out a message saying it's a draw. Else, then we want to calculate the highest score out of all the rolls. We can do this using another loop. else { // Calculate highest number rolled int highestScore = 0; for(int j = 0; j < playerCount; j++) { if(numbersRolled[j] > highestScore) { highestScore = numbersRolled[j]; } } // Assign points to player(s) with the highest number rolled for (int j = 0; j < playerCount; j++) { if(numbersRolled[j] == highestScore) { playerPoints[j]++; } } } The first part here loops through all the scores and records the highest. The second part then loops through all the players. If the player has achieved the highest score recorded, then they score a point. Finally, let's display the scores - Console.WriteLine("The score is now - Player 1 : " + playerPoints[0] + ". Player 2 : " + playerPoints[1] + ". Player 3 : " + playerPoints[2]); Console.WriteLine(); Finally, outside the main for loop, let's display the final results on who is the final winner. Once again, let's account for the case that all player's score the same amount and cause a tie, first. Else, we'll use similar logic as before to calculate who has the highest score and thus who has won. if(playerPoints[0] == playerPoints[1] && playerPoints[1] == playerPoints[2]) { Console.WriteLine("It's a draw!"); } else { // Calculate highest score int highestScore = 0; for (int i = 0; i < playerCount; i++) { if (playerPoints[i] > highestScore) { highestScore = playerPoints[i]; } } // Display winners for (int i = 0; i < playerCount; i++) { if (numbersRolled[i] == highestScore) { Console.WriteLine("Player " + i + " is the winner!"); } } } And that should be it! Sorry for the long reply, it turned out to be a lot more complicated than I initially thought and needed a lot of code refactoring. But it should do the trick! As some of the code is repetitive and to stop Main from getting too convoluted and overpacked, I'd really recommend splitting the different sections into methods. :) If you try it, let me know if you run into any issues or whether you need any help. Thanks very much for watching the video too! :)
This is superbly done. I hope you keep making more C# walkthroughs for beginner, intermediate, and advanced levels!
moving from JS to C# this playlist is perfect - not a repeat of the basics but gets me stuck in with easy and comfortable projects many thanks!
You should make more videos! You are very good at explaining things! Thank you.
Holy crap ive been up all night and i have really similar homework. thanks for tutorial i can finally get some sleep. Subbed!
Good video! Keep making them!
The Quality is extremely good
great video really appreciate your work,hope you keep making more C# projects for beginner.
Nicely done, but remember - the generator is pseudo-random, you're need to add seed while you're creating an object of Random class :)
I love your British accent ;) and I love your Unity tutorials!
superb ...
I am very happy with this video ...
pls make more c# videos
Keep going, great content :)
Great video and well explained. Thanks!
This is great, You are very easy to listing to
Fun and useful, thank you!
I get it’s a simple project and it’s done well but I’d probably create a Player object with randomNum and Points variables with get ; set to show the fundamentals of OOP.
Super awesome! Thanks!
Great video Thank you. 😊
Amazing video.
i hope youre still creating videos !!!!
good videos! wish you success!
Thanks a lot! :)
How do I do this with Windows Form Application on c#?
Thank you so much for this, it was very well explained ^_^
if i want refactor this code, how am i able to count the points and first to get 100 points wins?
any idea how to do this but with the interface with multiple forms?
Amazing video but my enemy points is never increasing could you please give a bit of help on that
Thanks for the video
What´s the front side suposed to look like
Nice Video! I have a question tho, what if i want to count the points, from the random dice number has generated.
Instead of playerPoints++, it'd be playerPoints += playerRandomNum.
And for the enemy, instead of enemyPoints++, you'd need to change it to enemyPoints += enemyRandomNum.
This will add on the points the player or enemy have rolled, instead of just increasing their points by 1.
Does that answer your question?
yup, I just tried it and it works. Thank you so much!
Quick question. I recently learned C# so I understand all of this but when you call Random() maybe explaining what Random is would be a bit better. Like saying it's a built-in function. had to go thru the docs to figure out what it was.
yes, you can even find it even when you hover cursor on Random it will show meaning of it , you can also find it in the Docs
great video
How do you make this into a 3 player game? I tried doing it myself but only player 1 and player 2 works, if player 3 gets a higher number than the other two he still doesn't get a point
You need to change the if statements to accommodate for more than 2 players. To find out who has won a round you could firstly find out the highest score rolled from all players and store it inside an int. Then you could check which player(s) scored this number and then assign them a point. (Don't forget to check for the case that all players roll the same number and thus no one would score a point at all!)
All of this would be easier to achieve if the scores were held in an array.
Firstly, set up two arrays of size 3 to hold the numbers rolled for each player and the total points for all players -
int playerCount = 3; // Used to represent the number of players wanted in game
int[] numbersRolled = new int[playerCount];
int[] playerPoints = new int[playerCount];
numbersRolled[0] would represent the number player 1 has rolled.
numbersRolled[1] would represent the number player 2 has rolled,
and numbersRolled[2] would represent the number player 3 has rolled.
Then playerPoints[0] would represent player 1's current score,
playerPoints[1] would be player 2's current score
and playerPoints[2] would be player 3's current score.
In the game loop, create a new for loop (using j instead of i) with it's condition being
j < playerCount
This for loop will then repeat for each player.
for(int j = 0; j < playerCount; j++)
{
Console.WriteLine("Player " + (j + 1) + ", press any key to roll the dice.");
Console.ReadKey();
numbersRolled[j] = random.Next(1, 7);
Console.WriteLine("Player " + (j + 1) + " rolled a " + numbersRolled[j]);
Console.WriteLine();
}
The number each player has rolled is stored inside the numbersRolled array.
We can use this later on to check who has rolled the highest and therefore scores a point.
Now, let's check the case that all player's have rolled the same number -
if(numbersRolled[0] == numbersRolled[1] && numbersRolled[1] == numbersRolled[2])
{
Console.WriteLine("It's a draw!");
}
So, here we check if player1's score is the same as player2's score and if player's 2 score is the same as player3's score.
If this is true, then we can just print out a message saying it's a draw.
Else, then we want to calculate the highest score out of all the rolls. We can do this using another loop.
else
{
// Calculate highest number rolled
int highestScore = 0;
for(int j = 0; j < playerCount; j++)
{
if(numbersRolled[j] > highestScore)
{
highestScore = numbersRolled[j];
}
}
// Assign points to player(s) with the highest number rolled
for (int j = 0; j < playerCount; j++)
{
if(numbersRolled[j] == highestScore)
{
playerPoints[j]++;
}
}
}
The first part here loops through all the scores and records the highest.
The second part then loops through all the players. If the player has achieved the highest score recorded, then they score a point.
Finally, let's display the scores -
Console.WriteLine("The score is now - Player 1 : " + playerPoints[0] +
". Player 2 : " + playerPoints[1] +
". Player 3 : " + playerPoints[2]);
Console.WriteLine();
Finally, outside the main for loop, let's display the final results on who is the final winner.
Once again, let's account for the case that all player's score the same amount and cause a tie, first. Else, we'll use similar logic as before to calculate who has the highest score and thus who has won.
if(playerPoints[0] == playerPoints[1] && playerPoints[1] == playerPoints[2])
{
Console.WriteLine("It's a draw!");
}
else
{
// Calculate highest score
int highestScore = 0;
for (int i = 0; i < playerCount; i++)
{
if (playerPoints[i] > highestScore)
{
highestScore = playerPoints[i];
}
}
// Display winners
for (int i = 0; i < playerCount; i++)
{
if (numbersRolled[i] == highestScore)
{
Console.WriteLine("Player " + i + " is the winner!");
}
}
}
And that should be it! Sorry for the long reply, it turned out to be a lot more complicated than I initially thought and needed a lot of code refactoring. But it should do the trick!
As some of the code is repetitive and to stop Main from getting too convoluted and overpacked, I'd really recommend splitting the different sections into methods. :)
If you try it, let me know if you run into any issues or whether you need any help.
Thanks very much for watching the video too! :)
it seems a little like java script i = i + 1 is the same for Java Script
👌
Thank you so much for the video. Could you be my tutor xD? Subscribed.
👍