C# :: Aufgabe #106 :: Lösung #9

10 Lösungen Lösungen öffentlich
#106

Stein, Papier, Schere, Echse, Spock

Anfänger - C# von Energy - 24.09.2015 um 15:22 Uhr
Programmiere das Spiel Stein, Papier, Schere, Echse, Spock, sodass man selbst eine Figur auswählen kann und der Computer eine zufällige Figur auswählt. Ermittele dann, wer diese Partie gewonnen hat.
#9
vote_ok
von Danny (20 Punkte) - 13.11.2015 um 17:28 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RPSLS
{
    class Program
    {
        static void Main(string[] args)
        {
           
                List<string> options = new List<string>();
                options.Add("paper");
                options.Add("scissors");
                options.Add("rock");
                options.Add("lizard");
                options.Add("spock");
         
                Console.WriteLine("Hello! Lets play a game. Type in one of the following words:\n" +
                    "rock \npaper \nscissors \nlizard \nspock" +
                    "\nand press Enter. If you want to quit the game, type in \"exit\"." + 
                    "\nFor game rules type in \"rules\".");
                string yourMove;
               
            do 
               {
                yourMove = Console.ReadLine();

                if (yourMove == "rules")
                {
                    Console.WriteLine("\nScissors cuts Paper, Paper covers Rock" +
                        "\nRock crushes Lizard, Lizard poisons Spock" +
                        "\nSpock smashes Scissors, Scissors decapitates Lizard" +
                        "\nLizard eats Paper, Paper disproves Spock" +
                        "\nSpock vaporizes Rock, Rock crushes Scissors");
                }
                else if (options.Contains(yourMove))
                {
                    string opponentsMove = Program.randomizer(options);
                    string[] whoIsStronger = Program.gameLogic(yourMove);

                    Console.WriteLine("\nYour move was \"{0}\" and my move was \"{1}\".",
                        yourMove, opponentsMove);
                    Console.WriteLine(Program.results(whoIsStronger, yourMove, opponentsMove));
                }
                else
                {
                    Console.WriteLine("\nYou're not that bright, huh? Let me repeat myself:" +
                    "\nType in one of the following words:\n" +
                    "rock \npaper \nscissors \nlizard \nspock" +
                    "\nand press Enter. If you want to quit the game, type in \"exit\"." +
                    "\nFor game rules type in \"rules\".");
                }
               } while (yourMove != "exit");
            }
        
           
        private static string randomizer(List<string> options)
        {
            Random random = new Random();
            int position = random.Next(options.Count);
            string computerChoice = options[position];
            //public für alles, privat für eine Classe
            //static - der typ bleibt gleich
            //final - wert bleibt gleich 
            return string.Format(computerChoice);
        }

        private static string[] gameLogic(string yourMove)
        {
            string[] elementsStronger = new string[2];
              
            if (yourMove == "paper")
                {
                    elementsStronger[0] = "scissors";
                    elementsStronger[1] = "lizard"; 
                }
                else if (yourMove == "rock")
                {
                    elementsStronger[0] = "paper";
                    elementsStronger[1] = "spock"; 
                }
                else if (yourMove == "lizard")
                {
                    elementsStronger[0] = "scissors";
                    elementsStronger[1] = "rock"; 
                }
                else if (yourMove == "spock")
                {
                    elementsStronger[0] = "lizard";
                    elementsStronger[1] = "paper"; 
                }
               else if (yourMove == "scissors")
               {
                   elementsStronger[0] = "spock";
                   elementsStronger[1] = "rock"; 
               }
               return elementsStronger;
       }

        private static string results(string[] whoIsStronger, string yourMove, string opponentsMove)
        {
            string gameResult;
            bool lost = whoIsStronger.Contains(opponentsMove);
            if (lost == true)
            {
                gameResult = "You lost. Ha!";
            }
            else
            {
                if (yourMove == opponentsMove)
                {
                    gameResult = "It's a draw! What are the odds!";
                }
                else
                {
                    gameResult = "You won. Don't think that you are better than me.";
                }
            }
            return gameResult;
        }
    }
 }

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben