Hangman

Split From Necro

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 1301 Views - Last Post: 14 June 2013 - 03:44 AM Rate Topic: -----

#1 Mikey1200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-June 13

Hangman

Posted 10 June 2013 - 04:56 AM

When I put this in everything works, except for the .Length the three times I have it entered. it says it does not correspond to the "int" Anyone tell me how to fix it to make it work

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

namespace Final_Project
{
    class FinalProject
    {
        static void Main(string[] args)
        {









            string[] wordBank = new string[10];
            wordBank[0] = "Math";
            wordBank[1] = "Computer Science";
            wordBank[2] = "Programs";
            wordBank[3] = "Equations";
            wordBank[4] = "Mr Lynch";
            wordBank[5] = "Calculator";
            wordBank[6] = "Hard Work";
            wordBank[7] = "Dedication";
            wordBank[8] = "Knowledge";
            wordBank[9] = "Goal";
            wordBank[10] = "Good Year";

            Random randGen = new Random();
            int generateRandomWord = randGen.Next(0, 9);
            string randomWord = wordBank[generateRandomWord];
            char[] guess = new char[randomWord.Length];
            for (int w = 0; w < generateRandomWord.Length; w++)
                guess[w] = '*';

            while (true)
            {
                Console.WriteLine("Please Enter a Guess");
                char playerGuess = char.Parse(Console.ReadLine());
                for (int m = 0; m < generateRandomWord.Length; m++)
                {
                    if (playerGuess == generateRandomWord.Length[m])
                        guess[m] = playerGuess;
                }
                Console.WriteLine(guess);
            }
        }
    }
}


This post has been edited by Skydiver: 10 June 2013 - 06:21 AM
Reason for edit:: Put it code tags. Learn to do this in the future.


Is This A Good Question/Topic? 0
  • +

Replies To: Hangman

#2 Mikey1200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-June 13

Re: Hangman

Posted 10 June 2013 - 04:58 AM

Everything works except I am getting errors on where it says".Length" It says it does not correspond with the "int" can anyone help me?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Final_Project
{
    class FinalProject
    {
        static void Main(string[] args)
        {









            string[] wordBank = new string[10];
            wordBank[0] = "Math";
            wordBank[1] = "Computer Science";
            wordBank[2] = "Programs";
            wordBank[3] = "Equations";
            wordBank[4] = "Mr Lynch";
            wordBank[5] = "Calculator";
            wordBank[6] = "Hard Work";
            wordBank[7] = "Dedication";
            wordBank[8] = "Knowledge";
            wordBank[9] = "Mike Sperando";
            wordBank[10] = "Good Year";

            Random randGen = new Random();
            int generateRandomWord = randGen.Next(0, 9);
            string randomWord = wordBank[generateRandomWord];
            char[] guess = new char[randomWord.Length];
            for (int w = 0; w < generateRandomWord.Length; w++)
                guess[w] = '*';

            while (true)
            {
                Console.WriteLine("Please Enter a Guess");
                char playerGuess = char.Parse(Console.ReadLine());
                for (int m = 0; m < generateRandomWord.Length; m++)
                {
                    if (playerGuess == generateRandomWord.Length[m])
                        guess[m] = playerGuess;
                }
                Console.WriteLine(guess);
            }
        }
    }
}

This post has been edited by modi123_1: 10 June 2013 - 05:11 AM
Reason for edit:: please use code tags

Was This Post Helpful? 0
  • +
  • -

#3 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 313
  • View blog
  • Posts: 490
  • Joined: 13-June 12

Re: Hangman

Posted 10 June 2013 - 05:18 AM

Yes. Because "generateRandomWord" is an integer variable, and so has no ".Length" property. I would guess you mean to use the "randomWord" string variable.

This post has been edited by MrShoes: 10 June 2013 - 05:19 AM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,415
  • Joined: 05-May 12

Re: Hangman

Posted 10 June 2013 - 06:24 AM

Merged the duplicate topics. Please do not open duplicate topics.
Was This Post Helpful? 0
  • +
  • -

#5 DiscimusVivere  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 36
  • Joined: 16-October 12

Re: Hangman

Posted 10 June 2013 - 06:51 PM

I Agree with MrShoes post.
integer doesn't have ".Length" property, use "randomWord.Length" instead "generateRandomWord.Length"

This post has been edited by DiscimusVivere: 10 June 2013 - 06:53 PM

Was This Post Helpful? 0
  • +
  • -

#6 Mikey1200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-June 13

Re: Hangman

Posted 11 June 2013 - 01:58 PM

Its saying that the line "char playerGuess = char.Parse(Console.ReadLine());" must be exactly one character long. how can I fix it and is there anything else I can do to make this a successful program of Hangman because its a final project and I need it to be perfect. If you can help me and add whatever else you can add to make no errors and make this program perfect I would love that






            string[] wordBank = new string[10];
            wordBank[0] = "Math";
            wordBank[1] = "Computer Science";
            wordBank[2] = "Programs";
            wordBank[3] = "Equations";
            wordBank[4] = "Mr Lynch";
            wordBank[5] = "Calculator";
            wordBank[6] = "Hard Work";
            wordBank[7] = "Dedication";
            wordBank[8] = "Knowledge";
            wordBank[9] = "Mike Sperando";
            

            Random randGen = new Random();
            int generateRandomWord = randGen.Next(0, 9);
            string randomWord = wordBank[generateRandomWord];
            char[] guess = new char[randomWord.Length];
            for (int w = 0; w < randomWord.Length; w++)
                guess[w] = '*';

            while (true)
            {
                Console.WriteLine("Please Enter a Guess");
                char playerGuess = char.Parse(Console.ReadLine());
                for (int m = 0; m < randomWord.Length; m++)
                {
                    if (playerGuess == randomWord.Length)
                        guess[m] = playerGuess;
                }
                Console.WriteLine(guess);

                Console.ReadKey();


            }
        }
    }
}

This post has been edited by Atli: 11 June 2013 - 02:03 PM
Reason for edit:: Use [code] tags when posting code.

Was This Post Helpful? 0
  • +
  • -

#7 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Hangman

Posted 11 June 2013 - 02:10 PM

The Char.Parse method expects a string with exactly one character. If you give it more than that, the FormatException is thrown. Therefore, what you are doing there is highly unadvicible; reading raw user input and piping it directly to that method.

A better tactic would be to save the user input from Console.ReadLine() into a string, and then fetch only the first char from that string.

Either that or just use the Console.Read method instead, but that also comes with it's own challenges.
Was This Post Helpful? 0
  • +
  • -

#8 Mikey1200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-June 13

Re: Hangman

Posted 11 June 2013 - 02:36 PM

View PostAtli, on 11 June 2013 - 02:10 PM, said:

The Char.Parse method expects a string with exactly one character. If you give it more than that, the FormatException is thrown. Therefore, what you are doing there is highly unadvicible; reading raw user input and piping it directly to that method.

A better tactic would be to save the user input from Console.ReadLine() into a string, and then fetch only the first char from that string.

Either that or just use the Console.Read method instead, but that also comes with it's own challenges.


So what should I replace with what
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,093
  • Joined: 27-December 08

Re: Hangman

Posted 11 June 2013 - 02:37 PM

Duplicate threads merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#10 Mikey1200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-June 13

Re: Hangman

Posted 11 June 2013 - 02:46 PM

View Postmacosxnerd101, on 11 June 2013 - 02:37 PM, said:

Duplicate threads merged. Please avoid duplicate posting.

So what should I change and what should I change it to? I need help asap!!!!
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,093
  • Joined: 27-December 08

Re: Hangman

Posted 11 June 2013 - 03:28 PM

I think Atli provided you with good advice. We're here to help, but we're not holding your hand.
Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Hangman

Posted 11 June 2013 - 03:28 PM

You need to read and then also think.
Programming isn't only about typing code into an editor. There thinking about the design, algorithms, implementations and etc.
Was This Post Helpful? 2
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,415
  • Joined: 05-May 12

Re: Hangman

Posted 11 June 2013 - 05:59 PM

And there is also Console.ReadKey(). I remember using the Turbo Pascal equivalent in my first CS class for my Hangman project. I also added in music and animation to blow the competition out of the water. "William Tell Overture" when the player won, and "Funeral March of a Marionette" when the player lost. :) Ah such fond memories.

Anyway for a quick and dirty demo of using Console.ReadKey() check out my console implementation of craps in the other thread.

This post has been edited by Skydiver: 11 June 2013 - 06:02 PM

Was This Post Helpful? 0
  • +
  • -

#14 Mikey1200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-June 13

Re: Hangman

Posted 12 June 2013 - 05:01 AM

View PostSkydiver, on 11 June 2013 - 05:59 PM, said:

And there is also Console.ReadKey(). I remember using the Turbo Pascal equivalent in my first CS class for my Hangman project. I also added in music and animation to blow the competition out of the water. "William Tell Overture" when the player won, and "Funeral March of a Marionette" when the player lost. :) Ah such fond memories.

Anyway for a quick and dirty demo of using Console.ReadKey() check out my console implementation of craps in the other thread.


Can Someone tell me wheat I am missing in here for my Hangman game to work. It tells me every letter that I guess is wrong I went through the alphabet a-z and it says every letter is wrong. What can I change so it does not say that?

How can I fix my code so that it tells me when I guess a correct letter to the word because when I run it and guess letter a-z it tells me every letter in incorrect

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

namespace Final_Project
{
    class FinalProject
    {
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            
      
            Console.WriteLine("        ___ ___");
            Console.WriteLine("|  |   |   |   |");
            Console.WriteLine("|__|   |   |   |");
            Console.WriteLine("|  |   |   |   |");
            Console.WriteLine("|  |   |   |   |");
            Console.WriteLine("__________________");

            Console.WriteLine("\n\n");
            Console.WriteLine("Instructions: This is a verison of Hangman when you are never wrong!");
            
            

            string[] wordBank = new string[10];
            wordBank[0] = "Math";
            wordBank[1] = "Computer Science";
            wordBank[2] = "Programs";
            wordBank[3] = "Equations";
            wordBank[4] = "Mr Lynch";
            wordBank[5] = "Calculator";
            wordBank[6] = "Hard Work";
            wordBank[7] = "Dedication";
            wordBank[8] = "Knowledge";
            wordBank[9] = "Mike Sperando";


                      
            Random randGen = new Random();
            int generateRandomWord = randGen.Next(0, 9);
            int Length = wordBank[generateRandomWord].Length;
            string randomWord = wordBank[generateRandomWord];
            char[] guess = new char[Length];

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

            while (true)
            {
                Console.WriteLine("");
                Console.WriteLine("");
                Console.WriteLine("Please Enter a Guess");
                char playerGuess = char.Parse(Console.ReadLine());
                for (int m = 0; m < Length; m++)
                {
                    if (wordBank[generateRandomWord].LastIndexOf(playerGuess) == playerGuess)
                    {
                        Console.WriteLine("You have guessed the word correct");
                    }
                }
                Console.WriteLine(guess);
                if (playerGuess == generateRandomWord)
                {
                    Console.WriteLine("You have guessed the letter correct");   
                }
                else
                {
                    Console.WriteLine("You are wrong guess again");
                }
            }
           
            
            
                
            
        }
    }
}


mod edit:
:code:

This post has been edited by Curtis Rutland: 12 June 2013 - 07:27 AM

Was This Post Helpful? 0
  • +
  • -

#15 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,972
  • Joined: 08-June 10

Re: Hangman

Posted 12 June 2013 - 07:27 AM

Why should we continue to help when you haven't taken previous advice?
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3