Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Project
- {
- class MainClass
- {
- static List<string> wordList;
- static Random rnd;
- public static void Main (string[] args)
- {
- wordList = new List<string>();
- rnd = new Random ();
- bool playing = handleMenu ();
- while (playing)
- playing = handleMenu ();
- }
- private static void pause() {
- Console.Write("Tryck en knapp för att gå vidare");
- Console.ReadKey ();
- }
- private static void addWordToList() {
- Console.WriteLine ("Lägg till ord:");
- Console.Write ("> ");
- string word = Console.ReadLine ();
- word = word.ToLower ();
- if (wordList.Contains (word))
- Console.WriteLine ("Ordet är redan inlagt!");
- else if (word.Trim() == String.Empty)
- Console.WriteLine ("Ordet får inte vara tomt");
- else
- wordList.Add (word);
- pause ();
- }
- private static void printWordList() {
- wordList.Sort ();
- foreach (string word in wordList) {
- Console.WriteLine (word);
- }
- pause ();
- }
- private static void printHangMan(int stage) {
- string line;
- System.IO.StreamReader file =
- new System.IO.StreamReader(String.Format("{0}.txt", stage));
- while((line = file.ReadLine()) != null)
- {
- Console.WriteLine (line);
- }
- file.Close();
- }
- private static string generateHiddenWordLine(string hiddenWord, List<char> triedCharacters) {
- string result = "";
- foreach (char wordChar in hiddenWord) {
- if (triedCharacters.Contains(wordChar))
- result += wordChar.ToString();
- else
- result += "-";
- }
- return result;
- }
- private static void playGame() {
- /* Let user decide how many tries he should get */
- int amountOfGuesses = 0;
- try {
- Console.WriteLine("Ange antalet max gissningar:");
- Console.Write("> ");
- amountOfGuesses = Convert.ToInt32(Console.ReadLine ());
- if (!(amountOfGuesses >= 1 && amountOfGuesses <= 30)) {
- Console.WriteLine("Antalet gissningar måste vara mellan 1 och 30");
- pause();
- return;
- }
- } catch (Exception ex) {
- Console.WriteLine ("Du måste ange ett numeriskt tal");
- pause ();
- return;
- }
- /* Randomize a word to use */
- int r = rnd.Next (wordList.Count);
- string word = wordList [r];
- int currentTry = 1;
- int badTries = 0;
- /* Play until a correct word was guessed or tries depleted */
- bool playing = true;
- List<char> triedCharacters = new List<char> ();
- while (playing) {
- Console.Clear ();
- /* Print hangman based on percent of bad tries */
- double foo = (float)badTries / (float)amountOfGuesses;
- //if (badTries == (amountOfGuesses - 1))
- // printHangMan (5);
- if (foo >= 0 && foo <= 0.25)
- printHangMan (1);
- else if (foo >= 0.26 && foo <= 0.5)
- printHangMan(2);
- else if (foo >= 0.51 && foo <= 0.75)
- printHangMan(3);
- else if (foo >= 0.76 && foo <= 0.99)
- printHangMan(4);
- else
- printHangMan(5);
- /* Print statistics */
- Console.Write(String.Format (
- "Felgissningar: {0}/{1}\n" +
- "Gissade bokstäver: ",
- badTries,
- amountOfGuesses
- ));
- /* Collect list of used characters */
- foreach (char triedChar in triedCharacters)
- Console.Write (triedChar.ToString ());
- Console.Write ("\n"); // Insert line break
- Console.WriteLine ("Aktuellt ord: " + generateHiddenWordLine (word, triedCharacters));
- if (badTries == amountOfGuesses) {
- Console.WriteLine ("Du har förbrukat dina försök och förlorat spelet!");
- Console.WriteLine ("Ordet var:");
- Console.WriteLine (word);
- break;
- }
- Console.Write ("Gissa bokstav: ");
- string guessedChar = Console.ReadLine ();
- guessedChar = guessedChar.ToLower ();
- if (guessedChar.Length > 1) {
- if (guessedChar.Equals (word)) {
- Console.WriteLine ("DU VANN! GRATTIS");
- break;
- } else {
- badTries++;
- }
- } else if (guessedChar.Length == 1) {
- //if (triedCharacters.Contains (Char.Parse (guessedChar))) {
- if (word.Contains (guessedChar)) {
- triedCharacters.Add (Char.Parse (guessedChar));
- if (generateHiddenWordLine (word, triedCharacters) == word) {
- Console.WriteLine ("DU VANN! GRATTIS");
- break;
- }
- } else {
- triedCharacters.Add (Char.Parse (guessedChar));
- badTries++;
- }
- }
- //badTries++;
- }
- pause ();
- }
- private static bool handleMenu() {
- Console.Clear();
- Console.Write(String.Format(
- "Hänga hen!\nBy Fredrik Lorensson\n\n" +
- " 1) Lägg till ord\n" +
- " 2) Lista alla ord\n" +
- " 3) Spela\n" +
- " 4) Avsluta\n\n" +
- "Välj 1-4: "
- ));
- int menuChoice = Convert.ToInt32(Char.GetNumericValue(Console.ReadKey ().KeyChar));
- Console.Write ("\n");
- switch (menuChoice) {
- case 1:
- addWordToList ();
- break;
- case 2:
- printWordList ();
- break;
- case 3:
- playGame ();
- break;
- case 4:
- return false;
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement