7 Replies - 3948 Views - Last Post: 13 March 2013 - 01:55 PM Rate Topic: -----

#1 nstrydom  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 39
  • Joined: 26-April 12

Add char to char array issue, hangman game

Posted 26 April 2012 - 08:58 PM

Personal Introduction
Spoiler


Now, Im writing a quick console Hangman game, the problem is I discovered my approach required the building of a char array -> char[] from multiple char variables. I'm now lost, can someone point me in the right direction?


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

namespace ConsoleApplication1
{
    class Program
    {
        string[] wordList = { 
                                "Baseball", "Tackle", "Dubstep", "Ignorance", "Limitation", "Sausage",
                                "Destruction", "Patriot", "Computing", "Assembly", "Coding", "Hackers",
                                "Football", "Downward"
                            };
        static void Main(string[] args)
        {
            int guessRemain = 7;
            int wordSel = GenerateRandom();
            Program o = new Program();


            char[] wordChar = o.wordList[wordSel].ToLower().ToCharArray();
            int MAX_BUF = wordChar.Length;
            

            Console.WriteLine("\nHANGMAN v 1.0\n\n\n\n");
            char[] userInput = PromptUserEntry();
            char[] solution = ScanForMatchingLetter(wordChar, MAX_BUF, userInput);
            
 
            Console.Read();
        }
        private static char ScanForMatchingLetter(char[] wordChar, int MAX_BUF, char[] userInput)
        {
            char[] solution = new char[MAX_BUF];
            for (int i = 0; i < MAX_BUF; ++i)
            {
                if (userInput[0] == wordChar[i])
                {
                    solution[i] = userInput[0];
                    
                }


            }
            return solution;
        }
        private static char[] PromptUserEntry()
        {
            Console.WriteLine("Pick a letter:");
            char[] userInput = Console.ReadLine().ToCharArray();
            return userInput;
        }
        private static void DisplayGuessLetterLine(char[] solution)
        {

            Console.Write(solution);
            
        }
        private static int GenerateRandom()
        {
            Random randNum = new Random();
            int wordSel = randNum.Next(0, 13);
            return wordSel;
        }

    }
}




Any input on the subject is greatly appreciate, and as you can see I'm not quite newbie but I still have a lot to learn. The goal is to receive help and give help, I learn better that way. Thank you!

This post has been edited by tlhIn`toq: 26 April 2012 - 09:04 PM
Reason for edit:: Added spoiler tags


Is This A Good Question/Topic? 0
  • +

Replies To: Add char to char array issue, hangman game

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5509
  • View blog
  • Posts: 11,814
  • Joined: 02-June 10

Re: Add char to char array issue, hangman game

Posted 26 April 2012 - 09:10 PM

Quote

problem is I discovered my approach required the building of a char array -> char[] from multiple char variables. I'm now lost, can someone point me in the right direction?


How is that a problem? Lost about what? Are you experiencing a crash, or exception error?


Quote

namespace ConsoleApplication1

Is this really your first C# application? Have you worked through any self-teaching books from cover to cover first, before trying to develop a program of your own design from scratch?

You have a return type problem here; the return type is specified as a char but you are returning a char[]

private static char ScanForMatchingLetter(char[] wordChar, int MAX_BUF, char[] userInput)
        {
            char[] solution = new char[MAX_BUF];
            for (int i = 0; i < MAX_BUF; ++i)
            {
                if (userInput[0] == wordChar[i])
                {
                    solution[i] = userInput[0];
                    
                }


            }
            return solution;
        }


Don't make a new Random every time you call this method. Just make one instance and keep re-using it. Otherwise you won't get any variety in your generator


            Random randNum = new Random();

private static int GenerateRandom()
        {
            int wordSel = randNum.Next(0, 13);
            return wordSel;
        }

Was This Post Helpful? 1
  • +
  • -

#3 nstrydom  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 39
  • Joined: 26-April 12

Re: Add char to char array issue, hangman game

Posted 26 April 2012 - 09:31 PM

Wow, is it really that simple that I set the wrong return type... Well thank you for pointing that out, I knew that more than one mind makes finding syntax errors easier, and also i appreciate the tip on the Random instance. And yes its an exception error, sorry for being to vague, not my first C# application but my first programming forum. Out of curiousity
how would you have approached it.

Output should look something like this
HANGMAN v 1.0



_ _ _t _ _ _t _ _ _ _

Guesses remaining: 4
Pick a letter: 



Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5509
  • View blog
  • Posts: 11,814
  • Joined: 02-June 10

Re: Add char to char array issue, hangman game

Posted 26 April 2012 - 10:52 PM

Quote

Out of curiousity
how would you have approached it.


Look down at the bottom of this page...

See all those links to other hangman game threads... Some of them even specifically state they are hangman in console, just like your project.

I would have started by looking at the techniques that others before me tried, and hoped they sparked something that hadn't even occurred to me.

Beyond that, I might recommend this article on how to stub out a project in an organized plan then flush out each part as possible.

You seem to be taking a serious approach to learning this.

Hopefully some of my standard resources will help:

Quote

Where do I start?


You start by learning a coding language FIRST.
Learn to plan before you type.
THEN you start designing software with a purpose.




Finding answers to specific problems:
Spoiler






Resources, references and suggestions for new programmers. - Updated Mar 2012
Spoiler

Was This Post Helpful? 1
  • +
  • -

#5 umairkz  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 2
  • Joined: 13-March 13

Re: Add char to char array issue, hangman game

Posted 13 March 2013 - 01:32 PM

View Postnstrydom, on 26 April 2012 - 09:31 PM, said:

Wow, is it really that simple that I set the wrong return type... Well thank you for pointing that out, I knew that more than one mind makes finding syntax errors easier, and also i appreciate the tip on the Random instance. And yes its an exception error, sorry for being to vague, not my first C# application but my first programming forum. Out of curiousity
how would you have approached it.

Output should look something like this
HANGMAN v 1.0



_ _ _t _ _ _t _ _ _ _

Guesses remaining: 4
Pick a letter: 



can u post the full code of this game. because this error occor char to char[]
i tried it but its notworking

Was This Post Helpful? -1
  • +
  • -

#6 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Re: Add char to char array issue, hangman game

Posted 13 March 2013 - 01:46 PM

Quote

Wow, is it really that simple that I set the wrong return type... Well thank you for pointing that out, I knew that more than one mind makes finding syntax errors easier, and also i appreciate the tip on the Random instance. And yes its an exception error, sorry for being to vague, not my first C# application but my first programming forum. Out of curiousity
how would you have approached it.

Output should look something like this


I was pretty proud of my console Hangman game back in the day. It's here. I consider it my first "real" program.
Was This Post Helpful? 0
  • +
  • -

#7 umairkz  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 2
  • Joined: 13-March 13

Re: Add char to char array issue, hangman game

Posted 13 March 2013 - 01:51 PM

I was pretty proud of my console Hangman game back in the day. It's here. I consider it my first "real" program.
[/quote]

but i want simple code as it is in this forum .
i appreciate your code . that was awsome.
but can u giv me little simple working code for hangman.
Was This Post Helpful? -1
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Add char to char array issue, hangman game

Posted 13 March 2013 - 01:55 PM

No, we don't hand out code to cheaters.

Closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1