IT rekvalifikace s garancí práce. Seniorní programátoři vydělávají až 160 000 Kč/měsíc a rekvalifikace je prvním krokem. Zjisti, jak na to!
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

Diskuze: Aplikace funguje jen když místo metody GetHiddenWord dám přímo string

V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.

Aktivity
Avatar
Jan Kubala
Člen
Avatar
Jan Kubala:25.2.2018 15:59

Ahoj, přešel jsem z C++ na C# a jsem hodně zmatený z toho, že v C# nejsou hlavičkové soubory a vůbec nevím jak metody pracují. Zkusil jsem si udělat obyčejného oběšence. Nakonec jsem to nějak krkolomně poskládal, ale pořád nemůžu přijít na to, proč aplikace funguje jen když stringu hiddenWord zadám přímo string. Tak když uživatel zadává písmena, tak se odkrývají, ale když použiju metodu pro vybrání náhodného slova, tak to nefunguje. I když se do hiddenWord nahrálo. Neví někdo co s tím? Díky za jakékoliv návrhy.

using System;
using System.Collec­tions.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp1
{
class Program
{
static bool isWon = false;
static bool playAgain = true;
static string hiddenWord = GetNewHiddenWord(); // PROBLÉM
static int totalLives = 10;
static int wordLength = hiddenWord.Length;
static int usedLetterPos = 0;
static int LettersRevealed = 0;
static char[] usedLetters = new char[255];
static char[] revealProgress = new char[wordLength];
static void Main(string[] args)

{
PlayGame();
}
static void PlayGame()
{
Reset();
while (totalLives > 0 && playAgain == true && !isWon)
{
Console.Write­Line("Lives:" + totalLives + "\nEnter a letter:");
Console.Write­Line(revealPro­gress);
Console.Write­Line(usedLetter­s);
GetGuess();
}
}

public static void Reset()
{
Console.Clear();
isWon = false;
totalLives = 10;
LettersRevealed = 0;

Array.Clear(u­sedLetters, 0, usedLetters.Len­gth);
Array.Clear(re­vealProgress, 0, revealProgres­s.Length);
for (int filler = 0; filler < wordLength; filler++)
{
revealProgres­s[filler] = '*';
}
}

public static void GetGuess()
{
char guess = Console.ReadKe­y().KeyChar;
if (guess == '0')
{
playAgain = false;
Console.Clear();
Console.Write­Line("Goodbye!");
}

if (Char.IsLetter(gu­ess))
{
bool takeLifeOff = true;
guess = Char.ToUpper(gu­ess);
Console.Clear();
if (((IList<char>)u­sedLetters).Con­tains(guess))
{
Console.Write­Line("This was already entered!");
}
else
{
usedLetters[u­sedLetterPos] = guess;
usedLetterPos++;
for (int HWChar = 0; HWChar < wordLength; HWChar++)
{
if (guess == hiddenWord[HWChar])
{
revealProgres­s[HWChar] = guess;
totalLives++;
takeLifeOff = false;
LettersRevealed++;
}
}
if (LettersRevealed == wordLength)
isWon = true;
if (takeLifeOff)
totalLives--;
}
}
if (LettersRevealed == wordLength)
isWon = true;
}

static string GetNewHiddenWord()
{
string[] Words = new string[] { "after", "monkey", "author", "ground", "black", "horse", "guild", "faces", "nitro" };
Random rnd = new Random();
int random = rnd.Next(0, 20);
hiddenWord = Words[random];
return hiddenWord;
}
}
}

 
Odpovědět
25.2.2018 15:59
Avatar
vajkuba1234
Člen
Avatar
Odpovídá na Jan Kubala
vajkuba1234:25.2.2018 16:52

Dej to do code, toto se neda cist...

Nahoru Odpovědět
25.2.2018 16:52
No hope, no future, JUST WAR!
Avatar
Jan Kubala
Člen
Avatar
Odpovídá na vajkuba1234
Jan Kubala:25.2.2018 16:56
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp1
{
class Program
{
static bool isWon = false;
static bool playAgain = true;
static string hiddenWord = GetNewHiddenWord(); // PROBLÉM
static int totalLives = 10;
static int wordLength = hiddenWord.Length;
static int usedLetterPos = 0;
static int LettersRevealed = 0;
static char[] usedLetters = new char[255];
static char[] revealProgress = new char[wordLength];
static void Main(string[] args)

{
PlayGame();
}
static void PlayGame()
{
Reset();
while (totalLives > 0 && playAgain == true && !isWon)
{
Console.WriteLine("Lives:" + totalLives + "\nEnter a letter:");
Console.WriteLine(revealProgress);
Console.WriteLine(usedLetters);
GetGuess();
}
}

public static void Reset()
{
Console.Clear();
isWon = false;
totalLives = 10;
LettersRevealed = 0;

Array.Clear(usedLetters, 0, usedLetters.Length);
Array.Clear(revealProgress, 0, revealProgress.Length);
for (int filler = 0; filler < wordLength; filler++)
{
revealProgress[filler] = '*';
}
}

public static void GetGuess()
{
char guess = Console.ReadKey().KeyChar;
if (guess == '0')
{
playAgain = false;
Console.Clear();
Console.WriteLine("Goodbye!");
}

if (Char.IsLetter(guess))
{
bool takeLifeOff = true;
guess = Char.ToUpper(guess);
Console.Clear();
if (((IList<char>)usedLetters).Contains(guess))
{
Console.WriteLine("This was already entered!");
}
else
{
usedLetters[usedLetterPos] = guess;
usedLetterPos++;
for (int HWChar = 0; HWChar < wordLength; HWChar++)
{
if (guess == hiddenWord[HWChar])
{
revealProgress[HWChar] = guess;
totalLives++;
takeLifeOff = false;
LettersRevealed++;
}
}
if (LettersRevealed == wordLength)
isWon = true;
if (takeLifeOff)
totalLives--;
}
}
if (LettersRevealed == wordLength)
isWon = true;
}

static string GetNewHiddenWord()
{
string[] Words = new string[] { "after", "monkey", "author", "ground", "black", "horse", "guild", "faces", "nitro" };
Random rnd = new Random();
int random = rnd.Next(0, 20);
hiddenWord = Words[random];
return hiddenWord;
}
}
}
 
Nahoru Odpovědět
25.2.2018 16:56
Avatar
HONZ4
Člen
Avatar
Odpovídá na Jan Kubala
HONZ4:25.2.2018 17:44

random máš mimo rozsah:

static string GetNewHiddenWord()
        {
            string[] Words = new string[] { "AFTER", "MONKEY", "AUTHOR", "GROUND", "BLACK", "HORSE", "GUILD", "FACES", "NITRO" };
            Random rnd = new Random();
            int random = rnd.Next(0, Words.Length - 1);
            return Words[random];
        }

ps, ta aplikace je napsaná hodně divným stylem ;)

Editováno 25.2.2018 17:46
Akceptované řešení
+20 Zkušeností
+2,50 Kč
Řešení problému
 
Nahoru Odpovědět
25.2.2018 17:44
Avatar
Jan Kubala
Člen
Avatar
Odpovídá na HONZ4
Jan Kubala:25.2.2018 17:53

Díky za odpověd, toho jsem si vůbec nevšiml. Ale hlavní problém zůstává.
Když tam dám přímo string a zapnu to, tak se zobrazí hvězdičky reprezentující slovo a pokud uživatel uhádne písmenko obsažené v tom skrytém slově, tak se na tom místě hvězdička změní na to písmenko. Ale když je tam ta metoda tak to najednou ohluchne.

 
Nahoru Odpovědět
25.2.2018 17:53
Avatar
HONZ4
Člen
Avatar
Odpovídá na Jan Kubala
HONZ4:25.2.2018 18:30

změnil jsi tu metodu přesně jak jsem ti napsal (včetně těch velkých písmen)?

Jinak vůbec nechápu s čím máš problém. Ten kód jsem zkoušel, v podstatě funguje. Zkoumat ho nechci, je to strašně chaoticky napsané.

Pokud ti to vyhazuje nějakou chybu, tak ji zkopíruj a napiš sem.

 
Nahoru Odpovědět
25.2.2018 18:30
Avatar
Jan Kubala
Člen
Avatar
Jan Kubala:25.2.2018 18:56

Přehlédl jsem to, že to musí být velkými písmeny :D Díky moc za upozornění!

 
Nahoru Odpovědět
25.2.2018 18:56
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 7 zpráv z 7.