vK 3 1 RON
vK 3 1 RON

Reputation: 115

Get one Array length and make a new array with that length

I'm new to C# - like really new and so I don't have nearly the experience - I'd like to accomplish this goal as simply as possible but I'll accept any sort of help you can give. I'm making a basic hangman game - and I've come to the point where I need to replace the user inputted 'char's into a new array and then replace all that array with '*' or '_' whatever. But I can't figure out how I could set the new array to the length that is determined during the game.

This code will look horrible to most of you since it was built by a simpleton - but I thank you for any help.

            //Welcome();
        string playerOne = "";
        bool validWord = false;
        string playerOneWord = "";
        do
        {
            Console.WriteLine("Okay player 1 - enter your word!");
            playerOne = Console.ReadLine();
            if (playerOne == "")
            {
                Console.WriteLine("Please enter an actual word.");
            }
            else
            {
                playerOneWord = playerOne;
                validWord = true;
            }
        } while (validWord == false);
        char[] charPlayOne = playerOneWord.ToCharArray();
        Console.WriteLine("Excellent, I'm now going to clear this chat so player 2 can't see your word!");
        Thread.Sleep(3500);
        Console.Clear();
        char[] playOneDisguised = new char [];
        Array.Copy(charPlayOne, playOneDisguised, 50);
        Console.WriteLine("Hello Player 2! Player 1 has chosen a word, the word looks like this: ");
        Thread.Sleep(1200);
        foreach (char k in playOneDisguised)
        {
            playOneDisguised[k] = Char.Parse("*") ;
        }
        Console.WriteLine(playOneDisguised);

        Console.ReadLine();
    }

    static void Welcome()
    {
        Console.WriteLine("Hello and welcome to hang man!");
        Thread.Sleep(1200);
        Console.WriteLine("This is a two player game. Make sure you have friends! unlike me...");
        Thread.Sleep(3000);
        Console.WriteLine("The rules are simple. 1 Player chooses a word - then player 2 has to guess the letters of the word.");
        Thread.Sleep(5000);
        Console.WriteLine("Press enter to begin.");
        Console.ReadLine();
    }

And yes - I do know that they'll be a problem with the 'if (playOne == "")' later on but I'm just trying to get it to work for now :)

Upvotes: 0

Views: 75

Answers (3)

user7772660
user7772660

Reputation:

    string playerOne = null;
    bool validWord = false;
    do
    {
        Console.WriteLine("Okay player 1 - enter your word!");
        playerOne = Console.ReadLine();
        if (playerOne.Length <= 0) Console.WriteLine("Please enter an actual word.");
        else validWord = true;
    } while (validWord == false);
    Console.WriteLine("Excellent, I'm now going to clear this chat so player 2 can't see your word!");
    Thread.Sleep(1500);
    Console.Clear();
    Console.WriteLine("Hello Player 2! Player 1 has chosen a word, the word looks like this: ");
    Thread.Sleep(1200);
    for (int i = 0; i < playerOne.Length; i++)
    { 
        if ((i % 2) == 0) Console.Write(playerOne[i]);
        else Console.Write("*");
    }
    Console.ReadLine();

You dont need to copy a string to hide some chars.

Upvotes: 0

Kevin
Kevin

Reputation: 2243

Simple - you declare a new array with a numerical argument:

// src is a char[] array
int cnt = src.Count();
char[] dest = new char[cnt];

... anyway, the main reason I'm answering is to let you know something that'll help you down the line: Refactoring.

Refactoring is splitting/reorganizing your code into manageable, easy to read/maintain/troubleshoot chunks. The idea is to have each function do one single thing (and only have one reason to change) - if you want, do some googling on 'Single Responsibility Principle'; it's probably the most important concept on how to program nice, clean code.

Anyway, look how many things that top function does. Here's a good example of refactoring:

    string playerOneWord = "";
    do
    {
        Console.WriteLine("Okay player 1 - enter your word!");
        playerOne = Console.ReadLine();
        if (playerOne == "")
        {
            Console.WriteLine("Please enter an actual word.");
        }
        else
        {
            playerOneWord = playerOne;
            validWord = true;
        }
    } while (validWord == false);

... what is this doing? It's getting a word from player one. Well, why not break that into its own function?

string playerOneWord = GetWordFromPlayerOne();

// ... later on, elsewhere

private string GetWordFromPlayerOne()
{
    // your code to get the input from player 1, looping until its valid
}

... hopefully that makes sense. Anyway, best of luck with your coding :-)

Upvotes: 0

zambonee
zambonee

Reputation: 1647

char[] playOneDisguised = new char [charPlayOne.Length];

or to simplify your code: char[] playOneDisguised = Enumerable.Repeat('*', charPlayOne.Length).ToArray();

Upvotes: 2

Related Questions