Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify current C# code: 1. Create a sequential access file of 25 words 2. Read the sequential access file into the array word list. Remove

Modify current C# code:

1. Create a sequential access file of 25 words

2. Read the sequential access file into the array word list. Remove the code that asks the user to enter 25 words for the word list.

C# CODE

using System;

namespace TestConsole { class Program { static void Main(string[] args) { String origWord = ""; String letter = ""; bool dashReplaced = false; bool gameOver = false; int numIncorrect = 0; int guessesLeft = 10; String guessWord = ""; int numChars = 0; String usedLetters = ""; int usedSub = 0; int wordCount = 25; string[] originalWords = new string[25];

for (var i = 0; i < 25; i++) { Console.Clear(); Console.WriteLine("Enter " + wordCount + " Original words"); originalWords[i] = Console.ReadLine(); wordCount--; }

Random randomWord = new Random(); int rndWord = randomWord.Next(0, 24); origWord = originalWords[rndWord];

numChars = origWord.Length;

origWord = origWord.ToUpper();

char[] updatedWord = new Char[numChars];

for (int i = 0; i < numChars; i++) { guessWord += "-"; updatedWord[i] = '-'; }//end for

Console.Clear();

Console.WriteLine("WORD GUESS GAME");

while (gameOver == false) { dashReplaced = false;

Console.Write("Used letters: ");

for (usedSub = 0; usedSub < usedLetters.Length; usedSub++) { Console.Write(usedLetters.Substring(usedSub, 1) + " "); }//end for

Console.WriteLine();

Console.WriteLine("Guess this word: " + guessWord);

Console.WriteLine("Enter a letter: ");

letter = Console.ReadLine(); letter = letter.ToUpper();

usedSub = 0;

while (usedSub < usedLetters.Length && usedLetters.Substring(usedSub, 1) != letter) { usedSub++; }//end while

if (usedSub < usedLetters.Length) { Console.WriteLine("You already guessed letter " + letter); } else { usedLetters += letter;

for (int x = 0; x < numChars; x++) { if (origWord.Substring(x, 1) == letter) { updatedWord[x] = Convert.ToChar(letter);

dashReplaced = true; }//end if }//end for

guessWord = new String(updatedWord);

if (dashReplaced == true) { if (guessWord.IndexOf('-') == -1) //check to see if any dashes remain { gameOver = true; Console.WriteLine("Yes, the word is " + guessWord); Console.WriteLine("Great guessing! You win!"); }//end if } else { numIncorrect++; guessesLeft--;

Console.WriteLine("You have " + guessesLeft + " guesses left.");

if (numIncorrect == 10) { gameOver = true; Console.WriteLine("Sorry, you lose. You made too many incorrect guesses."); Console.WriteLine("The word is " + origWord); }//end if }//end if }//end if } //end while

Console.ReadLine(); } } }

using System;

namespace TestConsole { class Program { static void Main(string[] args) { String origWord = ""; String letter = ""; bool dashReplaced = false; bool gameOver = false; int numIncorrect = 0; int guessesLeft = 10; String guessWord = ""; int numChars = 0; String usedLetters = ""; int usedSub = 0; int wordCount = 25; string[] originalWords = new string[25];

for (var i = 0; i < 25; i++) { Console.Clear(); Console.WriteLine("Enter " + wordCount + " Original words"); originalWords[i] = Console.ReadLine(); wordCount--; }

Random randomWord = new Random(); int rndWord = randomWord.Next(0, 24); origWord = originalWords[rndWord];

numChars = origWord.Length;

origWord = origWord.ToUpper();

char[] updatedWord = new Char[numChars];

for (int i = 0; i < numChars; i++) { guessWord += "-"; updatedWord[i] = '-'; }//end for

Console.Clear();

Console.WriteLine("WORD GUESS GAME");

while (gameOver == false) { dashReplaced = false;

Console.Write("Used letters: ");

for (usedSub = 0; usedSub < usedLetters.Length; usedSub++) { Console.Write(usedLetters.Substring(usedSub, 1) + " "); }//end for

Console.WriteLine();

Console.WriteLine("Guess this word: " + guessWord);

Console.WriteLine("Enter a letter: ");

letter = Console.ReadLine(); letter = letter.ToUpper();

usedSub = 0;

while (usedSub < usedLetters.Length && usedLetters.Substring(usedSub, 1) != letter) { usedSub++; }//end while

if (usedSub < usedLetters.Length) { Console.WriteLine("You already guessed letter " + letter); } else { usedLetters += letter;

for (int x = 0; x < numChars; x++) { if (origWord.Substring(x, 1) == letter) { updatedWord[x] = Convert.ToChar(letter);

dashReplaced = true; }//end if }//end for

guessWord = new String(updatedWord);

if (dashReplaced == true) { if (guessWord.IndexOf('-') == -1) //check to see if any dashes remain { gameOver = true; Console.WriteLine("Yes, the word is " + guessWord); Console.WriteLine("Great guessing! You win!"); }//end if } else { numIncorrect++; guessesLeft--;

Console.WriteLine("You have " + guessesLeft + " guesses left.");

if (numIncorrect == 10) { gameOver = true; Console.WriteLine("Sorry, you lose. You made too many incorrect guesses."); Console.WriteLine("The word is " + origWord); }//end if }//end if }//end if } //end while

Console.ReadLine(); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database In Depth Relational Theory For Practitioners

Authors: C.J. Date

1st Edition

0596100124, 978-0596100124

More Books

Students also viewed these Databases questions

Question

Find the derivative of the function. f (x) = ln 2x / x + 3

Answered: 1 week ago