This code is not printing the hangman after every wrong guess and i'm not sure how to fix
This code is not printing the hangman after every wrong guess and i'm not sure how to fix
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
using System;
using System.Linq;
class MainClass
{
publicstaticvoid Main (string[] args)
{
string[] words = newstring[4]{"sheep","goat","mouse","lion"};
Random rnd = new Random();
int random = rnd.Next(1, 3);
string secretWord = words[random];
bool wordTest = secretWord.All(Char.IsLetter);
while (wordTest == false || secretWord.Length == 0)
{
Console.WriteLine("A word must contain (only) letters");
Console.Write("Please enter a word: ");
secretWord = Console.ReadLine();
wordTest = secretWord.All(Char.IsLetter);
}
secretWord = secretWord.ToUpper();
MyGame(secretWord);
}
publicstaticvoid MyGame(String secretWord)
{
Console.WriteLine("\n\n");
int lives = 5;
int counter = -1;
int wordLength = secretWord.Length;
char[] secretArray = secretWord.ToCharArray();
char[] printArray = newchar[wordLength];
char[] guessedLetters = newchar[26];
int numberStore = 0;
bool victory = false;
foreach(char letter in printArray)
{
counter++;
printArray[counter] = '-';
}
while(lives > 0)
{
counter = -1;
string printProgress = String.Concat(printArray);
bool letterFound = false;
int multiples = 0;
if (printProgress == secretWord)
{
victory = true;
break;
}
if (lives > 1)
{
Console.WriteLine("You have {0} lives!", lives);
}
else
{
Console.WriteLine("You only have {0} life left!!", lives);
}
Console.WriteLine("current progress: " + printProgress);
Console.Write("\n\n\n");
Console.Write("Guess a letter: ");
string playerGuess = Console.ReadLine();
bool guessTest = playerGuess.All(Char.IsLetter);
while (guessTest == false || playerGuess.Length != 1)
{
Console.WriteLine("Please enter only a single letter!");
Console.Write("Guess a letter: ");
playerGuess = Console.ReadLine();
guessTest = playerGuess.All(Char.IsLetter);
}
playerGuess = playerGuess.ToUpper();
char playerChar = Convert.ToChar(playerGuess);
if (guessedLetters.Contains(playerChar) == false)
{
guessedLetters[numberStore] = playerChar;
numberStore++;
foreach(char letter in secretArray)
{
counter++;
if (letter == playerChar)
{
printArray[counter] = playerChar;
letterFound = true;
multiples++;
}
}
if (letterFound)
{
Console.WriteLine("Found {0} letter {1}!", multiples, playerChar);
}
else
{
Console.WriteLine("No letter {0}!", playerChar);
lives--;
}
}
else
{
Console.WriteLine("You already guessed {0}!!", playerChar);
}
}
if (victory)
{
Console.WriteLine("\n\nThe word was: {0}", secretWord);
Console.WriteLine("\n\nYOU WIN!!!!!!!!!!!");
}
else
{
Console.WriteLine("\n\nThe word was: {0}", secretWord);
Console.WriteLine("\n\nYOU LOSE!!!!!!!!!");
}
}
privatestaticstring DrawGuy(int livesLeft)
{
string drawHangman = "";
if (livesLeft < 5)
{
drawHangman += "--------\n";
}
if (livesLeft < 4)
{
drawHangman += " |\n";
}
if (livesLeft < 3)
{
drawHangman += " O\n";
}
if (livesLeft < 2)
{
drawHangman += " /|\\ \n";
}
if (livesLeft == 0)
{
drawHangman += " / \\ \n";
}
return drawHangman;
}
}
This code is not printing the hangman after every wrong guess and i'm not sure how to fix it.
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps with 2 images
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY