I am trying to make a program for a game of pig where there user enters a point total to play for and then takes turns with a computer player until one player reaches the point total. For the human turn the player rolls and if they roll 2-6 then they can use r to roll again or h to hold. when hold is selected it adds up the total points and goes to the next turn. If 1 is rolled then they get 0 points for the round and it goes to the computers turn. For some reason when I select h to hold it just keeps going and when it does end the points don't get added up. Not sure how to fix this
any help would be appericated
static int pigRoll()
{
Random random = new Random();
int die1 = 0;
die1 = random.Next(1, 6);
Console.WriteLine($"You rolled {die1}");
return die1;
}
static double humanTurn()
{
double pointTotal = 0;
string gameSelect = null;
var pigDiceRoll = 0;
Console.WriteLine("It's your turn");
do
{
pigDiceRoll = pigRoll();
if (pigDiceRoll != 1)
{
Console.WriteLine("r to roll or h to hold (r/h)");
gameSelect = Console.ReadLine();
pointTotal = pointTotal + pigDiceRoll;
}
else if(pigDiceRoll ==1)
{
pointTotal = 0;
}
} while (gameSelect != "r" || pigDiceRoll != 1);
Console.WriteLine($"Your turn point total is {pointTotal}");
return pointTotal;
}
The while statement should read:
while (gameSelect == "r" && pigDiceRoll != 1)
This translates to keep looping while the user wants to roll again and they didn't roll a 1.
Alternatively you could use:
while (!(gameSelect != "r" || pigDiceRoll == 1))
Both are logically the same, but the first is probably easier to read.
Related
I'm working on an assignment, where I have to make an application where you can throw 1-4 dice. The results are then added to a list.
However, if any of them is a 6, that one isn't added to the list, instead 2 additional dice are thrown.
As long as a die returns a 6, 2 more dice have to be thrown, until no die returns a 6.
Does anyone here know how to solve this? My programming skills are really basic, and I haven't used them much since last year.
for (int i = 0; i < qty; i++)
{
int diceNr = RollDice(random);
dicelist.Add(diceNr);
Console.WriteLine(diceNr);
if (diceNr == 6)
{
dicelist.Remove(diceNr);
Console.WriteLine("You got a six, that means you get 2 extra throws!");
for (int x = 0; x < 2; x++)
{
diceNr = RollDice(random);
dicelist.Add(diceNr);
}
You can try using a while loop as follows:
int i = quantity; #initialize a variable to your maximum number of throws
while(i > 0){ #until you have throws yet
i--; #this is the equivalent of one throw
int diceNr = RollDice(random);
dicelist.Add(diceNr);
Console.WriteLine(diceNr);
if(diceNr == 6){
Console.WriteLine("You got a six, that means you get 2 extra throws!");
i = i + 2; #add your bonus throws
}
}
You can use a while loop that exits once you are out of rolls. If you roll a six, add two more to the remaining quantity otherwise add the rolled value to the list.
int qty = 6;
// decrement remaining quantity each iteration
// until zero rolls remain
while(qty-- > 0)
{
int diceNr = RollDice(random);
Console.WriteLine(diceNr);
if (diceNr == 6)
{
// rolled six, add two more rolls
Console.WriteLine("You got a six, that means you get 2 extra throws!");
qty += 2;
}
else
{
// not six, add to list
dicelist.Add(diceNr);
}
}
// output all the non-six values
var allRolls = string.Join(",", diceList);
Console.WriteLine("All rolls: " + allRolls);
You could also do this with recursion (though I don't recommend it for this problem):
private static void Play(List<int> diceList, Random random, int rolls)
{
// base case, no rolls remain
if (rolls == 0)
return;
int diceNr = RollDice(random);
Console.WriteLine(diceNr);
if (diceNr == 6)
{
Console.WriteLine("You got a six, that means you get 2 extra throws!");
// add one to rolls since we are "reusing" this slot
// adding two would give us one more roll than we really want
rolls++
}
else
{
diceList.Add(diceNr);
// decrement rolls by one
rolls--;
}
// recursively call Play with updated roll count
// which is either original + 1 OR original - 1
Play(diceList, random, rolls);
}
private static int RollDice(Random r)
{
return r.Next(1, 7);
}
public static void Main()
{
var diceList = new List<int>();
var random = new Random();
int qty = 6;
Play(diceList, random, qty);
// output all the non-six values
var allRolls = string.Join(",", diceList);
Console.WriteLine("All rolls: " + allRolls);
}
My (readable) solution would look like:
class Program
{
static void Main(string[] args)
{
var game = new Game(4);
game.Play();
}
}
public class Game
{
private readonly int _noOfDices;
private readonly List<int> _result;
private readonly Random _random = new Random();
public Game(int noOfDices)
{
_noOfDices = noOfDices;
_result = new List<int>();
}
public void RollDice()
{
var no = _random.Next(1, 7);
if (no == 6)
{
RollDice();
RollDice();
}
else
{
_result.Add(no);
}
}
public void Play()
{
for (var i = 1; i <= _noOfDices; i++)
{
RollDice();
}
Console.WriteLine($"Output: {string.Join(',',_result)}");
}
}
Output:
This question has to do with this challenge on HackerRank. It seems to be failing some cases, but I'm not clear what's wrong with the algorithm (many people seem to have problem with timeouts, that's not an issue here, everything runs plenty fast, and all the cases that are visible to me pass, so I don't have a specific case that's failing).
The essential outline of how the algorithm works is as follows:
First be sure that Alice isn't already winning over the existing highest score (degenerate case), if she is just tell the world she's #1 from start to finish. Otherwise, at least one score on the leaderboard beats Alice's first try.
Start by walking down the scores list from the highest until we find a place where Alice fits in and record the scores that beat Alice's initial score along the way.
If we reach the end of the scores list before finding a place for Alice's bottom score, pretend there is a score at the bottom of the list which matches Alice's first score (this is just convenient for the main loop and reduces the problem to one where Alice's first score is on the list somewhere)
At this point we have a (sorted) array of scores with their associated ranks, rankAry[r - 1] is the minimum score needed for Alice to attain rank r as of the end of the if clause following the first while loop.
From there, the main algorithm takes over where we walk through Alice's scores and note her rank as we go by comparing against the benchmarks from the scores array that we setup as rankAry earlier. curRank is our candidate rank at each stage which we've definitely achieved by the time this loop starts (by construction).
If we're at rank 1 we will be forever more, so just populate the current rank as 1 and move on.
If we're currently tied with or beating the current benchmark and that's not the end of the line, keep peeking at the next one and if we're also beating that next one, decrease the current benchmark location and iterate
Once this terminates, we've found the one we're going to supplant and we cannot supplant anything further, so assign this rank to this score and repeat until done
As far as I can tell this handles all cases correctly, even if Alice has repeated values or increases between the benchmarks from scores, we should stay at the same rank until we hit the new benchmarks, but the site feedback indicates there must be a bug somewhere.
All the other approaches I've been able to find seem to be some variation on doing a binary search to find the score each time, but I prefer not having to constantly search each time and just use the auxiliary space, so I'm a little stumped on what could be off.
static int[] climbingLeaderboard(int[] scores, int[] alice) {
int[] res = new int[alice.Length];
if (scores.Length == 0 || alice[0] >= scores[0]) { //degenerate cases
for (int i = 0; i < alice.Length; ++i) {
res[i] = 1;
}
return res;
}
int[] rankAry = new int[scores.Length + 1];
rankAry[0] = scores[0]; //top score rank
int curPos = 1; //start at the front and move down
int curRank = 1; //initialize
//initialize from the front. This way we can figure out ranks as we go
while (curPos < scores.Length && scores[curPos] > alice[0]) {
if (scores[curPos] < scores[curPos-1]) {
rankAry[curRank] = scores[curPos]; //update the rank break point
curRank++; //moved down in rank
}
curPos++; //move down the array
}
if (curPos == scores.Length) { //smallest score still bigger than Alice's first
rankAry[curRank] = alice[0]; //pretend there was a virtual value at the end
curRank++; //give rank Alice will have for first score when we get there
}
for (int i = 0; i < alice.Length; ++i) {
if (curRank == 1) { //if we're at the top, we're going to stay there
res[i] = 1;
continue;
}
//Non-degenerate cases
while (alice[i] >= rankAry[curRank - 1]) {
if (curRank == 1 || alice[i] < rankAry[curRank - 2]) {
break;
}
curRank--;
}
res[i] = curRank;
}
return res;
}
You have a couple of bugs in your algorithm.
Wrong mapping
Your rankAry must map a rank (your index) to a score. However, with this line rankAry[0] = scores[0];, the highest score is mapped to 0, but the highest possible rank is 1 and not 0. So, change that to:
rankAry[1] = scores[0];
Wrong initial rank
For some reason, your curRank is set to 1 as below:
int curRank = 1; //initialize
However, it's wrong since your alice[0] is less than scores[0] because of the following block running at the beginning of your method:
if (scores.Length == 0 || alice[0] >= scores[0]) { //degenerate cases
for (int i = 0; i < alice.Length; ++i) {
res[i] = 1;
}
return res;
}
So, at best your curRank is 2. Hence, change it to:
int curRank = 2;
Then, you can also remove curRank++ as your curRank has a correct initial value from:
if (curPos == scores.Length) { //smallest score still bigger than Alice's first
rankAry[curRank] = alice[0]; //pretend there was a virtual value at the end
curRank++; // it's not longer needed so remove it
}
Improve "Non-degenerate cases" handling
Your break condition should consider rankAry at curRank - 1 and not curRank - 2 as it's enough to check the adjacent rank value. Also, a value at curRank - 2 will produce wrong results for some input but I won't explain for which cases specifically - I'll leave it up to you to find out.
Fixed Code
So, I fixed your method according to my comment above and it passed it all the tests. Here it is.
static int[] climbingLeaderboard(int[] scores, int[] alice) {
int[] res = new int[alice.Length];
if (scores.Length == 0 || alice[0] >= scores[0]) { //degenerate cases
for (int i = 0; i < alice.Length; ++i) {
res[i] = 1;
}
return res;
}
int[] rankAry = new int[scores.Length + 1];
rankAry[1] = scores[0]; //top score rank
int curPos = 1; //start at the front and move down
int curRank = 2; //initialize
//initialize from the front. This way we can figure out ranks as we go
while (curPos < scores.Length && scores[curPos] > alice[0]) {
if (scores[curPos] < scores[curPos-1]) {
rankAry[curRank] = scores[curPos]; //update the rank break point
curRank++; //moved down in rank
}
curPos++; //move down the array
}
if (curPos == scores.Length) { //smallest score still bigger than Alice's first
rankAry[curRank] = alice[0]; //pretend there was a virtual value at the end
}
for (int i = 0; i < alice.Length; ++i) {
if (curRank == 1) { //if we're at the top, we're going to stay there
res[i] = 1;
continue;
}
//Non-degenerate cases
while (alice[i] >= rankAry[curRank - 1]) {
if (curRank == 1 || alice[i] < rankAry[curRank - 1]) {
break;
}
curRank--;
}
res[i] = curRank;
}
return res;
}
I am a beginner at C# and I have a homework assignment where I have to write a dice roll simulator.
The user can chose a maximum of 5 die and then as they roll each one the sum is added up with the one rolled previously. At the end the number of rolls and the the total sum is presented.
However, if the user rolls a 6 then this result is not added to the sum but the die is rolled twice more and then the sum is added.
I have a few points where I have gotten stuck and/or would like to know a better way of writing it. I have included a snippet below.
Below I have stated my Random class every time the dice rolls but is it possible for me to do this only once and then use throughout my code?
Is there a simple way to write the part with what happens if you roll a 6? I mean I guess I could write another if statement every time the die rolls but seems that this would very long?
if (intnumberofdie == 1)
{
Random rnd1 = new Random();
int dice1 = rnd1.Next(1, 7);
Console.WriteLine("You have rolled " + dice1);
else if (intnumberofdie == 2)
{
Random rnd1 = new Random();
int dice1 = rnd1.Next(1, 7);
Console.WriteLine("The first die rolled " + dice1);
Console.ReadKey();
Random rnd2 = new Random();
int dice2 = rnd2.Next(1, 7);
Console.WriteLine("The second die has rolled " + (dice2));
Console.ReadKey();
Console.WriteLine("The total sum so far is " + (dice1 + dice2));
}
Give this one a go:
var rnd1 = new Random();
var rollsRemaining = 6;
var sum = 0;
while (rollsRemaining > 0)
{
int dice1 = rnd1.Next(1, 7);
Console.WriteLine("You have rolled " + dice1);
if (dice1 == 6)
{
rollsRemaining += 2;
}
else
{
sum += dice1;
rollsRemaining -= 1;
}
}
Console.WriteLine("The sum is " + sum);
Since you are a beginner I'll try to explain a few things first.
If you have a number of times you have to do the same thing, you should never handle each case yourself. This means you shouldn't use the if else if else.. but instead use a loop like for or while.
I have written a short function which should do what you want.
private int GetNumber(int amountDie) {
if (amountDie <= 0 || amountDie > 5) return 0;
int total = 0;
for (int i = 0; i < amountDie; i++)
{
int roll = rnd.Next(1, 7);
if (roll == 6)
{
int specialRoll1 = rnd.Next(1, 7);
int specialRoll2 = rnd.Next(1, 7);
total += specialRoll1;
total += specialRoll2;
}
else {
total += roll;
}
}
return total;
}
Now some more explaining:
Since you said that you can't have more than 5 die, we will first check if the value given was valid, otherwise we'll just return 0 (there are better things to do in this case but for simplicity we'll just say they get back 0).
After that we declare a total to keep track of how many points we have to return.
Then we start the loop. For each dice, we roll once (first line in the for-construct).
Since we need to check if the user rolled a 6, we have this if there. Inside it we roll two die and add both results to the total. Note, that for these two rolls 6 is just like any other number and not handled differently (if you need that, it would be a bit more complicating).
If the user didn't roll a 6, we just add whatever they rolled to the total.
After we've done that for all die we simply return the total we got.
You'll see that if you just copy paste this code, it will not compile. The reason for that is that you are missing a Random-object. In your code you always created a new one everytime you rolled. This is a problem since the Random class isn't as random as you might think and you should always create just one for many random numbers.
Thats why I added this at the top of my class:
private static readonly Random rnd = new Random();
This random-object is only created once and then used for every die. This gives you a lot more randomness and prevents that every user always rolls the same value.
I hope this shows you how you can create a method like this. Of course you can add the console-output etc like you did in your code.
I have seen questions related to Craps, but they were related to python and Java. I hope someone familiar with C# can help me. The code below is meant to simulate the game of craps. The code mostly works, except for one issue.
I suppose I should briefly explain the game. Two 6 sided dice are rolled. If the result is a 7 or 11. The player or "shooter" wins automatically. If a 2, 3, or 12 is rolled, the shooter loses automatically. However, if another number is rolled, that number becomes the "point". The shooter rolls again until either they roll the point again or a 7. If the point is rerolled the shooter wins. If a 7 is rolled, this time it is a loss.
So, the problem is that this code still gives wins or losses automatically for 2, 3, 11, and 12 after the first roll. For example: The shooter rolls a 6. 6 becomes the point and the shooter rolls again. If the shooter rolls a 3 this code marks this result as a loss when it should just keep rolling until a 6 or 7 is rolled. This incorrect end happens whenever a 2, 3, 11, or 12 is rolled after the first roll. Any help on correcting the logic of this code would be greatly appreciated. Thanks Also, I hope this question is formatted correctly. Please let me know if it is not.
class Craps
{
const int dieSides = 6;
int roll;
//const int repeatGame = 1000;
Random random = new Random();
public void RollDice()
{
int die1 = 0;
int die2 = 0;
die1 = random.Next(1, 7);
die2 = random.Next(1, 7);
roll = die1 + die2;
//Console.WriteLine("The shooter roled: {0}", roll);
}
public void PlayCraps()
{
RollDice();
Console.WriteLine("The shooter roled: {0}", roll);
int gameStatus = 0;
int point = roll;
int numRolls = 1;
if (roll != 7 || roll != 11 || roll != 2 || roll != 3 || roll != 12)
{
Console.WriteLine("The point is: {0}", point);
}
while (gameStatus < 1)
{
if (roll == 7 || roll == 11)
{
Console.WriteLine("You won!");
gameStatus++;
break;
}
else if (roll == 2 || roll == 3 || roll == 12)
{
Console.WriteLine("You lost.");
gameStatus++;
break;
}
else
{
while (point != roll || roll != 7)
{
RollDice();
if (roll == point)
{
Console.WriteLine("The shooter roled: {0}", roll);
Console.WriteLine("You won!");
numRolls++;
gameStatus++;
break;
}
if (roll == 7)
{
Console.WriteLine("The shooter roled: {0}", roll);
Console.WriteLine("You lost");
numRolls++;
gameStatus++;
break;
}
else
{
numRolls++;
Console.WriteLine("The shooter roled: {0}", roll);
break;
}
}
}
}
Console.WriteLine("This game was {0} roll(s)", numRolls);
}
static void Main(string[] args)
{
Craps NewGame = new Craps();
NewGame.PlayCraps();
Console.ReadLine();
}
}
User pm100 commented saying that the final break returned the code back to the outer loop and that this was causing the error. Removing this fixed the issue described in my question. There may be other errors or ways to improve this code, but that comment solved the particular focus of this question.
I previously created a program for users to play my NumBaseBall Game. I would like to add three more functionalities to it:
(1) Display the number of wins,
(2) Display the frequency played
(3) Display the player's winning %
I have an external class called GameResults that contains a method for calculating strikes or balls that determines the outcome of the game and another that checks to see if the player wants to play again.
How can I achieve these additional three functionalities without having my cumulative data for each erased each time a new instance of GameResults is created if the user decides to play again?
Here is my working code:
class Program
{
static void Main(string[] args)
{
do
{
PlayBall();
}
while (GameResults.PlayAgain());
}
public static void PlayBall()
{
Console.WriteLine("\t\t\t*************************************");
Console.WriteLine("\t\t\t* Let's Have Fun *");
Console.WriteLine("\t\t\t* Welcome To The *");
Console.WriteLine("\t\t\t* Number Baseball Game *");
Console.WriteLine("\t\t\t*************************************\n");
GameResults gameresults = new GameResults();
for (int trysCounter = 1; trysCounter <= 5; trysCounter++)
{
gameresults.Strikes = 0;
Random r = new Random();
var myRange = Enumerable.Range(1, 9);
var computerNumbers = myRange.OrderBy(i => r.Next()).Take(3).ToList();
Console.WriteLine("The Game's Three Random Integers Are: (Hidden from user)");
foreach (int integer in computerNumbers)
{
Console.WriteLine("{0}", integer);
}
List<int> playerNumbers = new List<int>();
Console.WriteLine("Please Enter Three Unique Single Digit Integers and Press ENTER after each:");
for (int i = 0; i < 3; i++)
{
Console.Write("");
int number = Convert.ToInt32(Console.ReadLine());
playerNumbers.Add(number);
}
gameresults.StrikesOrBalls(computerNumbers, playerNumbers);
Console.WriteLine("-----------------------> Computer's Numbers = {0}{1}{2}", computerNumbers[0], computerNumbers[1], computerNumbers[2]);
Console.WriteLine("-----------------------> Player's Numbers = {0}{1}{2}", playerNumbers[0], playerNumbers[1], playerNumbers[2]);
Console.WriteLine("-----------------------> Game Results = {0} STRIKES & {1} BALLS\n", gameresults.Strikes, gameresults.Balls);
Console.WriteLine("You have tried {0} times\n", trysCounter);
gameresults.TotalStrikes = gameresults.TotalStrikes + gameresults.Strikes;
Console.WriteLine("STRIKES = {0} ", gameresults.TotalStrikes);
if (gameresults.TotalStrikes >= 3)
{
Console.WriteLine("-----------------------> YOU ARE A WINNER!!!");
break;
}
}
if (gameresults.TotalStrikes < 3)
{
Console.WriteLine("YOU LOSE :(");
}
}
}
Here are my external methods:
public void StrikesOrBalls(List<int> computer, List<int> player)
{
for (int c = 0; c < 3; c++)
{
for (int p = 0; p < 3; p++)
{
if (computer[c] == player[p] && computer[c] != player[c])
Balls++;
}
}
if(computer[0]== player[0])
Strikes++;
if(computer[1] == player[1])
Strikes++;
if(computer[2] == player[2])
Strikes++;
}
public static bool PlayAgain()
{
while (true)
{
Console.Write("Do you want to play again [YES/NO]?\n");
string answer = Console.ReadLine().ToUpper();
if (answer == "YES")
return true;
if (answer == "NO")
Console.WriteLine("Thanks for playing NumBaseBall :) Goodbye!");
return false;
}
}