using System; using System.Linq; class main { publicstaticvoid Main (string[] args) { Console.WriteLine("Hi, my name is Stevie. I am thinking of an Animal.\nGuess which animal I am thinking of> "); string[] words = newstring[7]{"sheep","goat","lion","horse","dog","cat","lizard"}; Random rnd = new Random(); int random = rnd.Next(0, 5); string secretWord = words[random]; secretWord = secretWord.ToUpper(); 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(printProgress); Console.Write("\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--; } Console.WriteLine((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!!!!!!!!!"); } } }
using System; using System.Linq; class main { publicstaticvoid Main (string[] args) { Console.WriteLine("Hi, my name is Stevie. I am thinking of an Animal.\nGuess which animal I am thinking of> "); string[] words = newstring[7]{"sheep","goat","lion","horse","dog","cat","lizard"}; Random rnd = new Random(); int random = rnd.Next(0, 5); string secretWord = words[random]; secretWord = secretWord.ToUpper(); 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(printProgress); Console.Write("\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--; } Console.WriteLine((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!!!!!!!!!"); } } }
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
using System;
using System.Linq;
class main
{
publicstaticvoid Main (string[] args)
{
Console.WriteLine("Hi, my name is Stevie. I am thinking of an Animal.\nGuess which animal I am thinking of> ");
string[] words = newstring[7]{"sheep","goat","lion","horse","dog","cat","lizard"};
Random rnd = new Random();
int random = rnd.Next(0, 5);
string secretWord = words[random];
secretWord = secretWord.ToUpper();
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(printProgress);
Console.Write("\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--;
}
Console.WriteLine((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!!!!!!!!!");
}
}
}
Hello! This program is written in C# to play a word guessing game with the user. How would a method to organize the code be used? Like taking some code out of main to put it in a separate method and giving the method a name. Thank you!
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education