[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 690
  • Last Modified:

Loops in C#

Ok, I determined to learn this....trying to go beyond the basic requirements. With that said I still dont get loops? .... I want the user to be advised that they won and give them a choice to play again or exit. Hopefully this make it click :) What am I missing?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MysteryWord
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.Write("Welcome to Mystery Word! ");
            //string mysteryWord = "mysteryword";

            string[] wordBank = new string[10];
            wordBank[0] = "love";
            wordBank[1] = "abundance";
            wordBank[2] = "money";
            wordBank[3] = "code";
            wordBank[4] = "car";
            wordBank[5] = "hack";
            wordBank[6] = "lazy";
            wordBank[7] = "ambition";
            wordBank[8] = "excellence";
            wordBank[9] = "integrity";
            Random randGen = new Random();
            var idx = randGen.Next(0, 9);
            string mysteryWord = wordBank[idx];


            char[] guess = new char[mysteryWord.Length];

            for (int p = 0; p < mysteryWord.Length; p++)
                guess[p] = '*';

            while (true)
            {
                Console.Write("Please enter an alphabet: ");
                char playerGuess = char.Parse(Console.ReadLine());
               
                  bool incorrect = true; // <-- assume they are wrong until proven otherwise
                for (int j = 0; j < mysteryWord.Length; j++)
                {
                    if (playerGuess == mysteryWord[j])
                    {
                        guess[j] = playerGuess;
                        incorrect = false;

                        Console.WriteLine("The letter '" + playerGuess + " is in the Mystery Word,  Good job!");
                    }
                }
                Console.WriteLine(guess);
                if (incorrect)
                {
                    Console.WriteLine("The letter '" + playerGuess + "' is not in the word. Try again!");
                }
                
             //   if (wordBank = mysteryWord) error here ifd is the word is guess correctly want to indicate to the player that they won and restart the game or quit
               // {
                  //  Console.WriteLine(" You Win!");
                }
            }
        }
    }
 

Open in new window

0
December2000
Asked:
December2000
1 Solution
 
Vel EousResearch & Development ManagerCommented:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MysteryWord
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.Write("Welcome to Mystery Word! ");
            //string mysteryWord = "mysteryword";

            string[] wordBank = new string[10];
            wordBank[0] = "love";
            wordBank[1] = "abundance";
            wordBank[2] = "money";
            wordBank[3] = "code";
            wordBank[4] = "car";
            wordBank[5] = "hack";
            wordBank[6] = "lazy";
            wordBank[7] = "ambition";
            wordBank[8] = "excellence";
            wordBank[9] = "integrity";
            Random randGen = new Random();
            var idx = randGen.Next(0, 9);
            string mysteryWord = wordBank[idx];


            char[] guess = new char[mysteryWord.Length];

            for (int p = 0; p < mysteryWord.Length; p++)
                guess[p] = '*';

            while (true)
            {
                Console.Write("Please enter an alphabet: ");
                char playerGuess = char.Parse(Console.ReadLine());
                for (int j = 0; j < mysteryWord.Length; j++)
                {
                    if (playerGuess == mysteryWord[j])
                    {
                        guess[j] = playerGuess;
                        Console.WriteLine("The letter '" + playerGuess + " is in the Mystery Word,  Good job!");
                    }
                }
                Console.WriteLine(guess);
                string str = new string(guess); // convert your guess char array to a string
                if (str == mysteryWord) // compare your guess with mystery word string
                {
                    Console.WriteLine("You Won!");
                    break;
                }
                else
                {
                    Console.WriteLine("The letter '" + playerGuess + "' is not in the word. Try again!");
                }
            }
            Console.WriteLine("Press <RETURN> to exit.");
            Console.ReadLine();
        }
    }
}

Open in new window

0
 
December2000Author Commented:
Thank you!!!!! :)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now