C# - Basic hangman game lives issue - c#

I'll make it short: Basic hangman game, it loops through player 2 letter choice and if it doesn't match the first character of player 1's word, it'll remove a life, the the second, until it either finds a match or just removed however many lives the player 1 word length is.
I obviously don't want that, I want it to check the array - if none match then remove a life.
for (int i = 0; i < playerTwoGuesses.Length; i++)
{
Thread.Sleep(1400);
Console.Write("Guess: ");
count = 0;
do
{
try
{
playerTwoGuesses[i] = char.Parse(Console.ReadLine());
validGuess = true;
}
catch (Exception)
{
Console.WriteLine("Please enter a single character only.");
}
} while (validGuess == false);
for (int j = 0; j < playerOneDisguised.Length; j++)
{
if (playerOneCharacters[j] == playerTwoGuesses[i])
{
playerOneDisguised[j] = playerTwoGuesses[i];
}
else
{
lives = lives - 1;
}
}
if (lives == 0)
{
Console.WriteLine("Oh no! It seems you've lost. Closing game in 5 seconds.");
Thread.Sleep(5000);
Environment.Exit(0);
}
Console.WriteLine(playerOneDisguised);
for (int k = 0; k < playerOneDisguised.Length; k++)
{
if (playerOneDisguised[k] != '*')
{
count = count + 1;
if (count == playerOneDisguised.Length)
{
Console.WriteLine("Congratulations you've won!");
Thread.Sleep(1000);
Console.WriteLine("Closing game in 5 seconds.");
Thread.Sleep(5000);
Environment.Exit(0);
}
}
}
}

Try this:
bool match = false;
for (int j = 0; j < playerOneDisguised.Length; j++)
{
for (int y = 0; y < playerTwoGuesses.Length; y++)
{
if (playerOneCharacters[j] == playerTwoGuesses[y])
{
playerOneDisguised[j] = playerTwoGuesses[y];
match = true;
}
}
}
if (match == false) {
lives = lives - 1;
}
// Reset it back to false
match = false;

Rather than looping over the array yourself, you can just ask if the guessed char is present in it. You then only need an if statement to handle a guess
if (Array.Exists(playerTwoGuesses, element => element == lastGuess) {

Related

why is the output in lottery code isn't working?

I'm supposed to make a code in c# (microsoft visual studio 2017) that lets the user input six numbers, and then compare them to an array of six randomly generated numbers(with no duplicates).
If the user has one match, he's supposed to get a message saying he had one match, and different messages for two or three matches, and so on.
This is what I got so far:
static bool isValueInArray(int value, int[] a)
{
for (int i = 0; i < a.Length; i++)
{
if (a[i] == value)
{
return true;
}
}
return false;
}
static void Main(string[] args)
{
int min = 0;
int max = 6;
Random randnum = new Random();//random number generator
int[] numbers = new int[6];// generates six numbers
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = randnum.Next(min, max);//sets the numbers between 0 to 6
if(isValueInArray( i, numbers))
{
numbers[i] = randnum.Next(min, max);
}
}
try
{
Console.WriteLine("Hello and Welcome to our little game of lottery! lets see just how much luck you got!"); // greetings and instructions
Console.WriteLine("You'll now get to choose 6 different numbers between 0 to 6 to play with.");
Console.WriteLine("Go ahead and type them in.");
int[] lottery = new int[6];
for (int i = 0; i < lottery.Length; i++)
{
lottery[i] = int.Parse(Console.ReadLine()); // array to catch six numbers input
if (lottery[i] > 6)//checking if the numbers fit between 0 and 6
{
Console.WriteLine("whoops! the number you enetered isn't in range! please try again ^^");
break;
}
int x = 6;
for (int a = 0; a < lottery.Length; a++)
{
for (int b = 0; b < numbers.Length; b++)
{
if (lottery[a] == numbers[b])
{
a++;
x--;
if (x == 6)
{
Console.WriteLine("six match");
break;
}
else if (x == 5)
{
Console.WriteLine("five match");
break;
}
else if (x == 4)
{
Console.WriteLine("four match");
break;
}
else if (x == 3)
{
Console.WriteLine("three match");
break;
}
else if (x == 2)
{
Console.WriteLine("two match");
break;
}
else if (x == 1)
{
Console.WriteLine("one match");
break;
}
}
}
}
}
}
catch (FormatException)// checking if the input is in char format
{
Console.WriteLine("only numbers please!");
}
}
My problem is with the output. The program seems to go over all of the "else if" options and print all of them, instead of picking and printing just one of them.
You have everything mixed together. Write out the steps:
Generate numbers
Get input from user
Count number of matches
Print results
Each step should be performed separately.
// These should be the min/max lottery numbers
int min = 1;
int max = 100;
int numberOfLotteryNumbers = 6;
// Renamed for clarity
int[] lotteryNumbers = new int[numberOfLotteryNumbers];
int[] userNumbers = new int[numberOfLotteryNumbers];
// Step 1 - generate numbers
for (int i = 0; i < lotteryNumbers.Length; i++) {
int randomNumber;
do {
randomNumber = randnum.Next(min, max);
} while (isValueInArray(randomNumber, lotteryNumbers));
lotteryNumbers[i] = randomNumber;
}
// Step 2 - get numbers from user
for (int i = 0; i < lottery.Length; i++) {
int userInput;
do {
userInput = int.Parse(Console.ReadLine());
} while (userInput < min || userInput > max || isValueInArray(userInput, userNumbers));
userNumbers[i] = userInput;
}
// Step 3 - calc matches
int matches = 0;
for (int i = 0; i < userNumbers.Length; i++) {
if (isValueInArray(userNumbers[i], lotteryNumbers) {
matches += 1;
}
}
// Step 4 - print results
Console.WriteLine("There are {0} matches.", matches);
You may have to rearrange the code blocks in a way to get user input first, calculate the number of matches first, then display results as following code:
Note: Your approach to guarantee unique numbers in the randomly generated number ain't going to work as you expect to, you are passing "i" where you may want to pass "numbers[i]" instead to the isValueInArray" function. Let aside the idea that you will always end with values 0-5 in the array since you want 6 numbers.
int min = 0;
int max = 6;
Random randnum = new Random();//random number generator
int[] numbers = new int[6];// generates six numbers
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = randnum.Next(min, max);//sets the numbers between 0 to 6
if (isValueInArray(i, numbers))
{
numbers[i] = randnum.Next(min, max);
}
}
try
{
Console.WriteLine("Hello and Welcome to our little game of lottery! lets see just how much luck you got!"); // greetings and instructions
Console.WriteLine("You'll now get to choose 6 different numbers between 0 to 6 to play with.");
Console.WriteLine("Go ahead and type them in.");
int[] lottery = new int[6];
int x = 0;
//read user numbers
for (int i = 0; i < lottery.Length; i++)
{
lottery[i] = int.Parse(Console.ReadLine()); // array to catch six numbers input
while (lottery[i] > 6)//checking if the numbers fit between 0 and 6
{
Console.WriteLine("whoops! the number you enetered isn't in range! please try again ^^");
lottery[i] = int.Parse(Console.ReadLine()); // array to catch six numbers input
}
}
//count number of matches
for (int a = 0; a < lottery.Length; a++)
{
for (int b = 0; b < numbers.Length; b++)
{
if (lottery[a] == numbers[b])
{
//a++;
x++;
break;
}
}
}
//display results
if (x == 6)
{
Console.WriteLine("six matches");
}
else if (x == 5)
{
Console.WriteLine("five matches");
}
else if (x == 4)
{
Console.WriteLine("four matches");
}
else if (x == 3)
{
Console.WriteLine("three matches");
}
else if (x == 2)
{
Console.WriteLine("two matches");
}
else if (x == 1)
{
Console.WriteLine("one match");
}
}
catch (FormatException)// checking if the input is in char format
{
Console.WriteLine("only numbers please!");
}
Console.Read();
}
You can use a counter to achieve your goal. Just increment counter value on match:
int counter = 0;
for (int i = 0; i < lottery.Length; i++)
{
// ..
if (x == number)
{
counter++;
break;
}
// ..
}
Console.WriteLine("You have " + counter + " matches");

Loop not recognizing statement

So I'm working on this dice game for school but I've stumbled upon this problem:
First, let me give some context.
I have an array called diceResults[] in which the amount of thrown dice is saved in the array (i.e. you rolled two one's, so the first index of the array is 2)
Now One pair, Two pair, Three of a kind and Full house are all working fine.
But when a Full house is rolled it recognizes a pair, three of a kind, and a full house. But it doesn't recognize the second pair in the three of a kind.
I've tried several solutions but none seem to work.
for (int i = 0; i < diceResults.Length; i++)
{
if (diceResults[i] == 2)
{
onePair = true;
for (int j = i + 1; j < diceResults.Length; j++)
{
if (diceResults[j] == 2 || diceResults[j] == 3)
{
twoPair = true;
}
}
}
}
for (int i = 0; i < diceResults.Length; i++)
{
if (diceResults[i] == 3)
{
threeKind = true;
for (int k = 0; k < diceResults.Length; k++)
{
if (diceResults[k] == 2)
{
fullHouse = true;
}
}
}
I've tried setting 2 different if statements containing (diceResults[j] == 2) and (diceResults[j] == 3) in below each-other too, but this didn't seem to work either.
I'm checking what values are set using the following method.
private void CheckBoolValues()
{
if (onePair == true)
MessageBox.Show("pair yes");
if (twoPair == true)
MessageBox.Show("2 pair yes");
if (threeKind == true)
MessageBox.Show("3 of a kind yes");
if (fourKind == true)
MessageBox.Show("4 of a kind yes");
if (yahtzee == true)
MessageBox.Show("yahtzee yes");
if (lowStraight == true)
MessageBox.Show("lowstraight yes");
if (highStraight == true)
MessageBox.Show("high straight yes");
if (fullHouse == true)
MessageBox.Show("full house yes");
}
Would appreciate some help!
Shouldn't your first few lines of code be:
for (int i = 0; i < diceResults.Length; i++)
{
if ((diceResults[i] == 2) || (diceResults[i] == 3))
{
onePair = true;
...
If you roll 3 1s and 2 2s, you'll never detect the "second pair", because first loop only looks at dice totals over 2. But it's correct to set twoPair = true at the point you set fullHouse = true. Also you don't actually needed a nested loop to implement this.
BTW I'm not sure what the rules of Yahtzee are, but is 4 of a kind considered "two pairs"?
I personaly see no sense you want to detect a pair in a three of a kind but anyways. You could just check if there's already a pair of two in the results like this. Also doing multiple for-loops would be kind of superfluous.
bool onePair = false;
bool twoPair = false;
bool threeKind = false;
bool fullHouse = false;
for (int i = 0; i < diceResults.Length; i++)
{
if (diceResults[i] >= 2)
{
if(onePair == true) {
twoPair = true;
}
onePair = true;
}
if (diceResults[i] >= 3)
{
threeKind = true;
for (int j = 0; j< diceResults.Length; j++)
{
if (diceResults[k] == 2)
{
fullHouse = true;
}
}
}
}

Increment variable after third time something else happened

Excuse me for the silly question, but i can't manage to solve it.
How can i make something to happen every third time ?
Times(left number):
shipmentId=0
shipmentId=0
shipmentId=1
shipmentId=1
shipmentId=2
shipmentId=2 ....
int occurrence = 0;
int counter = 0;
foreach (var el in elmOrderData)
{
if (el.Name == "shipmentIndex")
{// we are entering here for every element that his name is "shipmentIndex"
el.SetValue(shipmentId);
secondTime++;
}
if ((secondTime % 2) == 0)
{// every third time we see "shipmentIndex"
secondTime = 1;
shipmentId++;
}
}
You could use a bool as in the following example. This will display messageboxes 1,3,5,7 and 9:
bool test = false;
for (int i = 0; i < 10; i++)
{
if (test)
MessageBox.Show(i.ToString());
test = !test;
}
Trying to piece together your notes - whether 'it' happens every time or not. How about this?
int occurrence = 0;
int counter = 0;
foreach(var a in list)
{
if(some_condition)
{
// do something..
occurrence++
if(occurrence % 2 == 0)
{
counter++
}
}
}
Why not just increment everytime and just divide by 2?
for(var i = 0; i<20; i++)
{
var j = i / 2; // or bit shift
//do work on j instead of i
}

Smooth display of a grid without jerks on console

I'm making this pacman console based game, in which there is a 40x40 board. There are pacmans and enemies on the board eating food. pacman is represented by 0 and enemy by #. Everytime there position changes the display function gets called, which in current logic, after every second clears the whole console and reprints each element on the board.. but this way, there are these jerks whenever whole board gets displayed. Kindly tell me a better way to achieve a smooth motion of paacman and enemy, ideally i should only change only the position of pacman and enemy, but i need to clear the console in order to show the grid on the same spot, otherwise it will just keep scrolling down. The code for display function is below:
public static void display()
{
Console.Clear();
for (int i = 0; i < 40; i++)
{
for (int j = 0; j < 40; j++)
{
bool packFound = false;
bool enmFound = false;
foreach (Packman element in myVers.packmans)
{
if (element.x == i && element.y == j)
{
packFound = true;
break;
}
}
foreach (Packman element in myVers.enemys)
{
if (element.x == i && element.y == j)
{
enmFound = true;
break;
}
}
if (packFound == true)
{
Console.Write('0');
myVers.board[i, j] = ' ';
}
else if (enmFound == true)
{
myVers.board[i, j] = ' ';
Console.Write('#');
}
else
{
Console.Write(myVers.board[i, j]);
}
}
Console.Write('\n');
}
//Console.Write("\nFood Count " + myVers.foodCount + "\n");
}
What if you would only redraw the characters that really changed? Maybe with use of Console.SetCursorPosition
Try building the String for the board first then pass the whole thing to the console at one time. Still perform the clear operation.
This would be a job for:
String Builder
public static void display()
{
var boardStr = new StringBuilder();
for (int i = 0; i < 40; i++)
{
for (int j = 0; j < 40; j++)
{
bool packFound = false;
bool enmFound = false;
foreach (Packman element in myVers.packmans)
{
if (element.x == i && element.y == j)
{
packFound = true;
break;
}
}
foreach (Packman element in myVers.enemys)
{
if (element.x == i && element.y == j)
{
enmFound = true;
break;
}
}
if (packFound == true)
{
boardStr.Append("0");
myVers.board[i, j] = ' ';
}
else if (enmFound == true)
{
myVers.board[i, j] = ' ';
boardStr.Append("#");
}
else
{
boardStr.Append(myVers.board[i, j].ToString());
}
}
boardStr.Append("\n");
}
//Console.Write("\nFood Count " + myVers.foodCount + "\n");
Console.Clear();
Console.Write(boardStr.ToString());
}

How to reset or restart nested loops

loop one
{
looptwo
{
if(condition=true)
{
reset values//restart both loops
}
}
}
and possibilities for reset values is 3
basically i want to compair two matrices
a= 1 2 3 4
1 2 3 4
b= 3 4 5 6
4 6 7 8
and when row 1 of a[] is matched with row 1 of b[].....i will add these rows and a[]
become = 2 4 6 8
for(i=0;i<rows;i++)
for(j=0;j<columns;j++)
{
a[i]=a[i]+b[i,j]
}
and again find my maches from restart with new a[] Matrix
and i have to insure that all rows of b[] matrix are checked with a[] which are 3 in this case
You have to use goto to break out of multiple loop levels in C#. For example:
RESTART:
while (a) {
while (b) {
if (that_other_thing)
goto RESTART;
}
}
Well, you don't have to use goto but the alternative might be using a bunch of flag variables to indicate that a restart is required. And that code will probably be pretty hard to follow.
The best choice here is to move the loops into their own method, and return from inside the inner loop. Example:
public void MyMehod(){
loop one{
looptwo{
if(condition=true){
return;
}
}
}
}
If this is not possible for some reason, you can use a bool value that you set in the inner loop to bail out of all of them, but this is a bit more messy:
bool endloop = false;
while(!endloop){
while(!endloop){
if(condition){
endloop = true;
}
}
}
For a while loop it looks ok, but even more messy for a for loop or a foreach loop.
Start:
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if(j == 5)
goto Start;
}
}
Although structuring your code in a way to not use a goto is a much better approach...
If you can guarantee that you will have a condition that will tell you that you don't need to restart, you could wrap the whole thing in one more loop.
bool keepLooping = true;
while (keepLooping)
{
keepLooping = false;
for (int x = 0; x < maxx; x++)
{
for (int y = 0; y < maxy; y++)
{
if (DoSomething(x, y))
{
keepLooping = true;
break;
}
}
if (keepLooping)
{
break;
}
}
}
If you are checking a list for duplicates and modifying them do make all entries unique, you might do something like this (assuming string values):
List<string> a = GetNamesFromeSomewhere();
bool duplicateFound = true;
while (duplicateFound )
{
duplicateFound = false;
for (int x = 0; x < a.Length; x++)
{
for (int y = x + 1; y < a.Length; y++)
{
if (a[x].Equals(a[y]))
{
//Change a[y], but now we have to recheck for duplicates...
a[y] += "_";
duplicateFound = true;
break;
}
}
if (duplicateFound)
{
break;
}
}
}
if you use numeric loop variables like i and j you can just reset the values
e.g.
for (i=0; i<10; i++) {
for (j=0; j<10; j++) {
if (var[i][j] == 'x') {
i=0; j=0; break;
}
}
}
you can also use the method approach as suggested earlier
void someFunction(params) {
for (i=0; i<10; i++) {
for (j=0; j<10; j++) {
if (var[i][j] == 'x') {
someFunction(params)
return;
}
}
}
}

Categories

Resources