?
Solved

Boolean test in C#

Posted on 2012-09-08
2
Medium Priority
?
645 Views
Last Modified: 2012-09-08
I am trying to create a Boolean test for false  "Try Again" but I am getting an error on the else statement..... should I try it another way? Want to let the player know that they guessed wrong

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

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

            Console.Write("Welcome to Hangman! ");
            //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 a letter: ");
                char playerGuess = char.Parse(Console.ReadLine());
                for (int j = 0; j < mysteryWord.Length; j++)
                {
                    if (playerGuess == mysteryWord[j])
                        guess[j] = playerGuess;

                }

                Console.WriteLine(guess);
            }
       // }
            //else  --- Getting Errors in this block
   // {
          //  Console.Write("Try Again);
        }
    }
}

Open in new window

0
Comment
Question by:December2000
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 38380103
"Want to let the player know that they guessed wrong"

Create a bool flag that gets toggled when a match is found.  Something like:
                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(guess);
                if (incorrect)
                {
                    Console.WriteLine("The letter '" + playerGuess + "' was not in the word.");
                }

Open in new window

0
 

Author Closing Comment

by:December2000
ID: 38380123
You are awesome idle!
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question