C# :: Aufgabe #106
10 Lösungen

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.
Lösungen:

using System; using System.Collections.Generic; using System.Linq; namespace spock { class Program { static void Main() { Hand PlayerHand, ComputerHand, Stein = new Hand("Stein", new string[] { "Echse", "Schere" }), Papier = new Hand("Papier", new string[] { "Stein", "Spock" }), Schere = new Hand("Schere", new string[] { "Echse", "Papier" }), Echse = new Hand("Echse", new string[] { "Spock", "Papier" }), Spock = new Hand("Spock", new string[] { "Stein", "Schere" }); Dictionary<ConsoleKey, Hand> Keys = new Dictionary<ConsoleKey, Hand>() { { ConsoleKey.D1, Stein }, { ConsoleKey.NumPad1, Stein }, { ConsoleKey.D2, Papier }, { ConsoleKey.NumPad2, Papier }, { ConsoleKey.D3, Schere }, { ConsoleKey.NumPad3, Schere }, { ConsoleKey.D4, Echse }, { ConsoleKey.NumPad4, Echse }, { ConsoleKey.D5, Spock }, { ConsoleKey.NumPad5, Spock } }; Hand[] Hands = Keys.Values.Distinct().ToArray(); Console.WriteLine("Wählen Sie Ihre Hand:"); for(int i = 0; i < Hands.Length; i++) Console.WriteLine("[{0}] {1}", i + 1, Hands[i].Name); ConsoleKey Input; do Input = Console.ReadKey(true).Key; while (!Keys.ContainsKey(Input)); PlayerHand = Keys[Input]; ComputerHand = Hands[new Random().Next(Hands.Length)]; bool playerWins = PlayerHand.CanBeat(ComputerHand), computerWins = ComputerHand.CanBeat(PlayerHand); Console.WriteLine(); if(playerWins ^ computerWins) { if(playerWins) Console.WriteLine("Der Spieler gewinnt.\n{0} schlägt {1}.", PlayerHand.Name, ComputerHand.Name); else Console.WriteLine("Der Computer gewinnt.\n{0} schlägt {1}.", ComputerHand.Name, PlayerHand.Name); }else Console.WriteLine("Es konnte kein Sieger ermittelt werden.\nSpieler: {0}\nComputer: {1}", PlayerHand.Name, ComputerHand.Name); Console.ReadKey(); } } class Hand { string name; string[] canBeat; public string Name { get { return name; } } public Hand(string name, string[] canBeat) { this.name = name; this.canBeat = canBeat; } public bool CanBeat(Hand EnemyHand) { return canBeat.Contains(EnemyHand.Name); } } }
Problemlos durch weitere Handzeichen erweiterbarer Alghorhythmus.
Gruß
C#-Code
Gruß

[Flags] enum SSP : byte { None = 0, Schere = 1 << 0, Stein = 1 << 1, Papier = 1 << 2, Echse = 1 << 3, Spock = 1 << 4 } class Program { static void Main(string[] args) { Dictionary<byte, string> Results = new Dictionary<byte,string>() { { (byte)(SSP.Schere | SSP.Papier), "Schere schneidet Papier." }, { (byte)(SSP.Papier | SSP.Stein), "Papier bedeckt Stein." }, { (byte)(SSP.Stein | SSP.Echse), "Stein zerquetscht Echse." }, { (byte)(SSP.Echse | SSP.Spock), "Echse vergiftet Spock." }, { (byte)(SSP.Spock | SSP.Schere), "Spock zertrümmert Schere." }, { (byte)(SSP.Schere | SSP.Echse), "Schere köpft Echse." }, { (byte)(SSP.Echse | SSP.Papier), "Echse frisst Papier." }, { (byte)(SSP.Papier | SSP.Spock), "Papier widerlegt Spock." }, { (byte)(SSP.Spock | SSP.Stein), "Spock verdampft Stein." }, { (byte)(SSP.Stein | SSP.Schere), "Stein schleift Schere." }, { (byte)(SSP.Stein | SSP.Stein), "Stein gegen Stein." }, { (byte)(SSP.Schere | SSP.Schere), "Schere gegen Schere." }, { (byte)(SSP.Papier | SSP.Papier), "Papier gegen Papier." }, { (byte)(SSP.Echse | SSP.Echse), "Echse gegen Echse." }, { (byte)(SSP.Spock | SSP.Spock), "Spock gegen Spock." } }; char Input = default(char); for (byte User = 0, Npc = 0; Input != '0'; Input = Console.ReadKey().KeyChar, Console.Clear()) { Console.WriteLine("Runde {0}\n" + "Scores :" + "\n\tUser -> {1}" + "\n\tNPC -> {2}" + "\n\nDu musst wählen ..." + "\n0 - Exit" + "\n1 - Schere" + "\n2 - Stein" + "\n3 - Papier" + "\n4 - Echse" + "\n5 - Spock", User + Npc, User, Npc); if (!"012345".Contains(Input)) continue; SSP UserChoice = (SSP)Enum.Parse(typeof(SSP), Convert.ToString(1 << (byte)Input - 49, 10)), NpcChoice = (SSP)Enum.Parse(typeof(SSP), Convert.ToString(1 << new Random().Next(0, 5), 10)); string Result = Results[ (byte)(UserChoice | NpcChoice) ], Winner = Result.Split(' ')[0]; if (Winner == UserChoice.ToString()) User++; if (Winner == NpcChoice.ToString()) Npc++; Console.WriteLine(Result + "\n{0}", (Winner == UserChoice.ToString()) ? (Winner == NpcChoice.ToString()) ? "Unentschieden!" : "Du hast gewonnen!" : "Du hast verloren!"); } } }

class Program { static void Main(string[] args) { string[] auswahl = new string[5]; auswahl[0] = "Stein"; auswahl[1] = "Papier"; auswahl[2] = "Schere"; auswahl[3] = "Echse"; auswahl[4] = "Spock"; int handAuswahlUser; int punktUser = 0; int punktPc = 0; Console.WriteLine("Willkommen bei Schere, Stein, Papier, Echse, Spock." + "\nSpielanleitung:\n" + "\n-Stein zerstört Schere und zerquetscht Echse" + "\n-Schere schneidet Papier und köpft Echse" + "\n-Papier bedeckt Stein und widerlegt Spock" + "\n-Echse vergiftet Spock und frisst Papier" + "\n-Spock zertrümmert Schere und verdampft Stein\n"); Console.Write("Bitte deinen Namen angeben:"); string name = Console.ReadLine(); Console.Write("\nWer als erstes 3 Punkte hat, gewinnt!"); do { Console.WriteLine( "\n1: "+ auswahl[0]+", 2: "+ auswahl[1]+ ", 3: " + auswahl[2] + ", 4: " + auswahl[3] + ", 5: " + auswahl[4] + "."); Random r = new Random(); int handAuswahlPc = r.Next(1, 6); do { Console.Write("\n\nBitte Nummer eingeben welche Hand gespielt werden soll:"); }while (!int.TryParse(Console.ReadLine(), out handAuswahlUser)); if (handAuswahlPc != handAuswahlUser) { Console.Write("\n" + name + " wählt: " + auswahl[handAuswahlUser - 1] + "\n\nDer Computer wählt eine Hand..." + "Er wählt: " + auswahl[handAuswahlPc - 1]); } else { Console.Write("\n" + name + " wählt: " + auswahl[handAuswahlUser - 1] + "\n\nDer Computer wählt eine Hand..." + "Er wählt ebenfalls " + auswahl[handAuswahlPc - 1]); } #region Stein //User wählt Stein if (handAuswahlUser == 1 && handAuswahlPc == 4 ^ handAuswahlPc == 3) { punktUser++; Console.WriteLine("\n\n" + name + " gewinnt."); } else if (handAuswahlUser == 1 && handAuswahlPc == 2 ^ handAuswahlPc == 5) { punktPc++; Console.WriteLine("\n\nDer PC gewinnt."); } else if (handAuswahlUser == 1 && handAuswahlPc == 1) Console.WriteLine("\n\nVerdammt.. Unentschieden."); #endregion #region Papier //User wählt Papier if (handAuswahlUser == 2 && handAuswahlPc == 1 ^ handAuswahlPc == 5) { punktUser++; Console.WriteLine("\n\n" + name + " gewinnt."); } else if (handAuswahlUser == 2 && handAuswahlPc == 3 ^ handAuswahlPc == 4) { punktPc++; Console.WriteLine("\n\nDer PC gewinnt."); } else if (handAuswahlUser == 2 && handAuswahlPc == 2) Console.WriteLine("\n\nVerdammt.. Unentschieden."); #endregion #region Schere //User wählt Schere if (handAuswahlUser == 3 && handAuswahlPc == 2 ^ handAuswahlPc == 4) { punktUser++; Console.WriteLine("\n\n" + name + " gewinnt."); } else if (handAuswahlUser == 3 && handAuswahlPc == 1 ^ handAuswahlPc == 5) { punktPc++; Console.WriteLine("\n\nDer PC gewinnt."); } else if (handAuswahlUser == 3 && handAuswahlPc == 3) Console.WriteLine("\n\nVerdammt.. Unentschieden."); #endregion #region Echse //User wählt Echse if (handAuswahlUser == 4 && handAuswahlPc == 5 ^ handAuswahlPc == 2) { punktUser++; Console.WriteLine("\n\n" + name + " gewinnt."); } else if (handAuswahlUser == 4 && handAuswahlPc == 3 ^ handAuswahlPc == 1) { punktPc++; Console.WriteLine("\n\nDer PC gewinnt."); } else if (handAuswahlUser == 4 && handAuswahlPc == 4) Console.WriteLine("\n\nVerdammt.. Unentschieden."); #endregion #region Spock //User wählt Spock if (handAuswahlUser == 5 && handAuswahlPc == 1 ^ handAuswahlPc == 3) { punktUser++; Console.WriteLine("\n\n" + name + " gewinnt."); } else if (handAuswahlUser == 5 && handAuswahlPc == 2 ^ handAuswahlPc == 4) { punktPc++; Console.WriteLine("\n\nDer PC gewinnt."); } else if (handAuswahlUser == 5 && handAuswahlPc == 5) Console.WriteLine("\n\nVerdammt.. Unentschieden."); #endregion Console.WriteLine("\nZwischenstand: " + name + " " + punktUser + ":" + punktPc + " PC"); Console.WriteLine("___________________________________________________________________"); } while (punktPc<3 && punktUser <3); if (punktUser == 3) { Console.WriteLine("Glückwunsch, du hast den Computer besiegt!\nDas Programm wird in 5 Sekunden beendet..."); Thread.Sleep(5000); } else { Console.WriteLine("Schade, der Computer hat gewonnen.\nDas Programm wird in 5 Sekunden beendet..."); Thread.Sleep(5000); } } }

static void Main(string[] args) { Random rnd = new Random(); List<Knobeln> Knobelliste = new List<Knobeln>(); Knobeln Schere = new Knobeln(); Schere.Name = "Schere"; Schere.StrongerThan = new string[] { "Papier", "Echse" }; Schere.WeakerThan = new string[] { "Spock", "Stein" }; Knobelliste.Add(Schere); Knobeln Papier = new Knobeln(); Papier.Name = "Papier"; Papier.StrongerThan = new string[] { "Stein", "Spock" }; Papier.WeakerThan = new string[] { "Schere", "Echse" }; Knobelliste.Add(Papier); Knobeln Stein = new Knobeln(); Stein.Name = "Stein"; Stein.StrongerThan = new string[] { "Echse", "Schere" }; Stein.WeakerThan = new string[] { "Papier", "Spock" }; Knobelliste.Add(Stein); Knobeln Echse = new Knobeln(); Echse.Name = "Echse"; Echse.StrongerThan = new string[] { "Spock", "Papier" }; Echse.WeakerThan = new string[] { "Schere", "Stein" }; Knobelliste.Add(Echse); Knobeln Spock = new Knobeln(); Spock.Name = "Spock"; Spock.StrongerThan = new string[] { "Schere", "Stein" }; Spock.WeakerThan = new string[] { "Echse", "Papier" }; Knobelliste.Add(Spock); Console.WriteLine("Wählen Sie aus(Schere,Stein,Papier,Echse,Spock)"); var tmpChoice1 = Console.ReadLine(); var tmpCharacter1 = from element in Knobelliste where element.Name == tmpChoice1 select element; int counter = 0; var r = rnd.Next(Knobelliste.Count); Knobeln tmpCharacter2 = new Knobeln(); foreach (var item in Knobelliste) { if (counter == r) { tmpCharacter2 = item; } counter++; } if (tmpCharacter1.First().StrongerThan.Contains(tmpCharacter2.Name)) { Console.WriteLine("You have won!"); Console.WriteLine("You beat " + tmpCharacter2.Name); Console.ReadKey(); } else if (tmpCharacter1.First().WeakerThan.Contains(tmpCharacter2.Name)) { Console.WriteLine("You have lost!"); Console.WriteLine(tmpCharacter2.Name + "beat you "); Console.ReadKey(); } } class Knobeln { public string Name { get; set; } public string[] StrongerThan { get; set; } public string[] WeakerThan { get; set; } public Knobeln() { } }
Lösung mit Enums und Geprüfter Eingabe in .NET 4.5.2
C#-Code

using System; using System.Collections.Generic; namespace SpockEchse { class Program { public enum Hand { Exit = 0, Stein = 1, Papier = 2, Schere = 3, Echse = 4, Spock = 5 } static void Main(string[] args) { var WennSchlageIchDict = new Dictionary<Hand, List<Hand>>() { { Hand.Stein, new List<Hand> {Hand.Echse, Hand.Schere } }, { Hand.Papier, new List<Hand> {Hand.Stein, Hand.Spock} }, { Hand.Schere, new List<Hand> {Hand.Echse, Hand.Papier } }, { Hand.Echse, new List<Hand> {Hand.Spock, Hand.Papier } }, { Hand.Spock, new List<Hand> {Hand.Stein, Hand.Schere } }, }; foreach (Hand loop in Enum.GetValues(typeof(Hand))) Console.WriteLine($"{(int)loop} {loop}"); string inLine; while (!string.IsNullOrEmpty(inLine = Console.ReadLine())) { Hand handSpieler; if (inLine.Length > 1) //Text wandeln das genau der 1 Buchstabe im Wort gross ist. inLine = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(inLine.ToLower()); //Enum Parsen und prüfen ob das Ergebnis gültig ist. if (!Enum.TryParse(inLine, out handSpieler) || !Enum.IsDefined(typeof(Hand), handSpieler)) { Console.WriteLine($"Eingabe '{inLine}' konnte nicht erkannt werden."); continue; } if (handSpieler == Hand.Exit) break; Hand handNpc = (Hand)new Random().Next(1, 6); if (handNpc == handSpieler) Console.WriteLine("Unentschieden"); else if (WennSchlageIchDict[handSpieler].Contains(handNpc)) Console.WriteLine($"Du hast gegen {handNpc} gewonnen, weiter so."); else Console.WriteLine($"Du hast gegen {handNpc} verloren, beim nächsten mal hast du mehr Glück."); } } } }

<Window x:Class="SSPES.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="181" Width="270"> <StackPanel HorizontalAlignment="Center" Margin="5"> <Label HorizontalAlignment="Center">Opponend</Label> <Label Name="lbl_choice" HorizontalAlignment="Center">Choice</Label> <Label HorizontalAlignment="Center">VS</Label> <StackPanel Orientation="Horizontal"> <Button Name="btn_sissor" Margin="5">Sissor</Button> <Button Name="btn_stone" Margin="5">Stone</Button> <Button Name="btn_paper" Margin="5">Paper</Button> <Button Name="btn_lizard" Margin="5">Lizard</Button> <Button Name="btn_spock" Margin="5">Spock</Button> </StackPanel> <Label Name="lbl_result" HorizontalAlignment="Center"></Label> </StackPanel> </Window>

namespace SSPES { public partial class MainWindow : Window { private Game gameInstance; private Brush neutral; public MainWindow() { InitializeComponent(); gameInstance = new Game(); neutral = btn_lizard.Background; btn_lizard.Click += btn_Click; btn_paper.Click += btn_Click; btn_sissor.Click += btn_Click; btn_spock.Click += btn_Click; btn_stone.Click += btn_Click; } void btn_Click(object sender, RoutedEventArgs e) { var used = new SolidColorBrush(Color.FromRgb(255, 0, 0)); resetButtons(); if (sender == btn_lizard) { gameInstance.choose(Items.Lizard); ((Button)sender).Background = used; show_result(); } else if(sender == btn_paper) { gameInstance.choose(Items.Paper); ((Button)sender).Background = used; show_result(); } else if (sender == btn_sissor) { gameInstance.choose(Items.Sissor); ((Button)sender).Background = used; show_result(); } else if (sender == btn_spock) { gameInstance.choose(Items.Spock); ((Button)sender).Background = used; show_result(); } else if (sender == btn_stone) { gameInstance.choose(Items.Stone); ((Button)sender).Background = used; show_result(); } else { } } private void resetButtons() { btn_lizard.Background = neutral; btn_paper.Background = neutral; btn_sissor.Background = neutral; btn_spock.Background = neutral; btn_stone.Background = neutral; } private void show_result() { lbl_choice.Content = gameInstance.Choice.ToString(); lbl_result.Content = gameInstance.Result.ToString(); } } }

using Game; namespace SSPES { public enum Items { [SuccessorItems(Items.Stone, Items.Sissor)] Lizard, [SuccessorItems(Items.Sissor, Items.Lizard)] Paper, [SuccessorItems(Items.Spock, Items.Stone)] Sissor, [SuccessorItems(Items.Lizard, Items.Paper)] Spock, [SuccessorItems(Items.Paper, Items.Spock)] Stone } public class Game { private Random rand; public Game() { rand = new Random(); } public string Item { get; private set; } public void choose(Items item) { Choice = rand.randomItem(); Result = item.vs(Choice); } public Items Choice { get; private set; } public MatchResults Result { get; private set; } } public static class Helpers { public static MatchResults vs(this Items attacker, Items defender) { if (GetAttr(attacker).successors.Contains(defender)) { return MatchResults.Lose; }else if(attacker.Equals(defender)) { return MatchResults.Draw; } return MatchResults.Win; } public static Items randomItem(this Random rand) { var ItemFields = typeof(Items).GetFields(); var index = rand.Next(ItemFields.Length-1); var itemField = ItemFields[index+1]; return ((Items)itemField.GetValue(null)); } private static SuccessorItems GetAttr(Items item) { return (SuccessorItems)Attribute.GetCustomAttribute(ForValue(item), typeof(SuccessorItems)); } private static MemberInfo ForValue(Items item) { return typeof(Items).GetField(Enum.GetName(typeof(Items), item)); } } }

namespace SSPES { public class SuccessorItems : Attribute { public SuccessorItems(params Items[] successors) { this.successors = successors; } public Items[] successors { get; private set; } } }

namespace Game { public enum MatchResults { Win, Draw, Lose } }
Hier mal eine GUI-Version
Vorschau:
C#-Code
C#-Code
Vorschau:
http://www.xup.in/dl,14667007/spses.jpg/

using System; using System.Drawing; namespace SPSES { public partial class Form1 : Form { TypOfHand userHand; TypOfHand kiHand; public Form1() { InitializeComponent(); lblWinner.Visible = false; lblYou.Visible = false; lblComputer.Visible = false; } private void Image_Click(object sender, EventArgs e) { lblWinner.Visible = false; lblYou.Visible = false; lblComputer.Visible = false; PictureBox box = (PictureBox)sender; switch (box.Name) { case "pbStein": userHand = new TypOfHand((byte)EHand.Stein); break; case "pbPapier": userHand = new TypOfHand((byte)EHand.Papier); break; case "pbScheere": userHand = new TypOfHand((byte)EHand.Scheere); break; case "pbEchse": userHand = new TypOfHand((byte)EHand.Echse); break; case "pbSpock": userHand = new TypOfHand((byte)EHand.Spock); break; default: userHand = null; break; } if (userHand == null) throw new ArgumentNullException("Ihre Auswahl konnte nicht ermittelt werden"); pbUserHand.BackgroundImage = userHand.bild; Random ran = new Random(); byte randomHand = (byte)Math.Pow(2, ran.Next(0, 5)); kiHand = new TypOfHand(randomHand); pbKiHand.BackgroundImage = kiHand.bild; lblWinner.Text = Kominationen.GetKombo((byte)(userHand.typ | kiHand.typ)); Sieger sieg = new Sieger(userHand.typ, kiHand.typ); if (sieg.GetSieger() == "spieler") { // User gewinnt lblYou.Text = "Gewonnen"; lblComputer.Text = "Verloren"; lblYou.Visible = true; lblComputer.Visible = true; } else if (sieg.GetSieger() == "ki") { // KI gewinnt lblYou.Text = "Verloren"; lblComputer.Text = "Gewonnen"; lblYou.Visible = true; lblComputer.Visible = true; } else { // Unentschieden lblYou.Text = "~"; lblComputer.Text = "~"; lblYou.Visible = true; lblComputer.Visible = true; } lblWinner.Visible = true; } } }

using System; using System.Collections.Generic; using System.Drawing; namespace SPSES { class TypOfHand { /// <summary> /// Typ der gewählten Hand /// </summary> public byte typ { get; private set; } /// <summary> /// Ein Bild welches die Hand darstellt /// </summary> public Image bild { get; private set; } public Dictionary<byte, Image> ImagesFromByte { get; private set; } public TypOfHand(byte typ) { ImagesFromByte = new Dictionary<byte, Image>() { { (byte)EHand.Stein, (Image)SPSES.Properties.Resources.Stein }, { (byte)EHand.Papier, (Image)SPSES.Properties.Resources.Papier }, { (byte)EHand.Scheere, (Image)SPSES.Properties.Resources.Scheere }, { (byte)EHand.Echse, (Image)SPSES.Properties.Resources.Echse }, { (byte)EHand.Spock, (Image)SPSES.Properties.Resources.Spock } }; this.typ = typ; this.bild = ImagesFromByte[typ]; } } public class Kominationen { #region Dictionarys static readonly Dictionary<byte, string> KombosFromByte = new Dictionary<byte, string>() { { (byte)(EHand.Stein | EHand.Stein), " Unentschieden " }, { (byte)(EHand.Stein | EHand.Papier), " Papier umwickelt Stein " }, { (byte)(EHand.Stein | EHand.Scheere), "Stein zerschlägt Scheere" }, { (byte)(EHand.Stein | EHand.Echse), "Stein zerquetscht Echse " }, { (byte)(EHand.Stein | EHand.Spock), " Spock verdampft Stein " }, { (byte)(EHand.Papier | EHand.Papier), " Unentschieden " }, { (byte)(EHand.Papier | EHand.Scheere), "Scheere schneidet Papier" }, { (byte)(EHand.Papier | EHand.Echse), " Echse frisst Papier " }, { (byte)(EHand.Papier | EHand.Spock), "Papier wiederlegt Spock " }, { (byte)(EHand.Scheere | EHand.Scheere)," Unentschieden " }, { (byte)(EHand.Scheere | EHand.Echse), " Scheere köpft Echse " }, { (byte)(EHand.Scheere | EHand.Spock), " Spock zerlegt Scheere " }, { (byte)(EHand.Echse | EHand.Echse), " Unentschieden " }, { (byte)(EHand.Echse | EHand.Spock), " Echse vergiftet Spock " }, { (byte)(EHand.Spock | EHand.Spock), " Unentschieden " } }; #endregion public static string GetKombo(byte kombo) { return KombosFromByte[kombo]; } } public class Sieger { byte spieler; byte ki; public Sieger(byte spieler, byte ki) { this.spieler = spieler; this.ki = ki; } /// <summary> /// Ermittelt den Sieger der Runde /// </summary> /// <returns>"spieler", "ki" oder "unentschieden"</returns> public string GetSieger() { byte kombo = (byte)(spieler | ki); if (kombo == 5 || kombo == 9) //Stein gewinnt if (spieler == (byte)EHand.Stein) return "spieler"; else return "ki"; else if (kombo == 3 || kombo == 18) // Papier gewinnt if (spieler == (byte)EHand.Papier) return "spieler"; else return "ki"; else if (kombo == 6 || kombo == 12) // Scheere gewinnt if (spieler == (byte)EHand.Scheere) return "spieler"; else return "ki"; else if (kombo == 10 || kombo == 24) // Echse gewinnt if (spieler == (byte)EHand.Echse) return "spieler"; else return "ki"; else if (kombo == 17 || kombo == 20) // Spock gewinnt if (spieler == (byte)EHand.Spock) return "spieler"; else return "ki"; else if (kombo == 1 || kombo == 2 || kombo == 4 || kombo == 8 || kombo == 16) return "unentschieden"; else throw new Exception("Sieger konnte nicht ermittelt werden"); } } [Flags] public enum EHand { None = 0, Stein = 1 << 0, Papier = 1 << 1, Scheere = 1 << 2, Echse = 1 << 3, Spock = 1 << 4 } }

namespace SPSES { public enum Spielauswahl { None, Stein, Papier, Schere, Echse, Spock, } public class Spieler { /// <summary> /// Die Spielauswahl des Spielers. /// </summary> private Spielauswahl auswahl = new Spielauswahl(); public Spielauswahl Auswahl { get { if (auswahl != Spielauswahl.None) { return auswahl; } else { throw new ArgumentNullException("auswahl"); } } set { auswahl = value; } } /// <summary> /// Der Spieler wird aufgefordert eine Auswahl zu treffen. /// </summary> public void TreffeAuswahl() { string input; int countLoop = 0; bool loop = true; do { countLoop++; Console.WriteLine("\nBitte treffen Sie eine Spielauswahl."); Console.WriteLine("Eingabe: Stein; Papier; Schere; Echse; Spock"); input = Console.ReadLine(); // Kontrolliere Eingabe und lege Auswahl fest. if (Enum.TryParse(input, true, out this.auswahl)) { Console.WriteLine("\nEingabe erfolgreich."); loop = false; } else { if (countLoop == 100) { throw new ArgumentException("Eingabeversuch zu oft wiederholt."); } else { Console.WriteLine("\nEingabe fehlerhaft."); } } } while (loop); } } public class Computer { private Spielauswahl auswahl; public Spielauswahl Auswahl { get { if (auswahl != Spielauswahl.None) { return auswahl; } else { throw new ArgumentNullException("auswahl"); } } set { auswahl = value; } } /// <summary> /// Der Computer trifft eine Spielauswahl /// </summary> public void TreffeAuswahl() { Random randomGenerator = new Random(); int zufall; zufall = randomGenerator.Next(1, 6); this.auswahl = (Spielauswahl)zufall; } } public class Auswertung { public static bool? WerteAus(Spielauswahl auswahlSpieler, Spielauswahl auswahlComputer) { if (auswahlSpieler == auswahlComputer) { return null; } else if (auswahlSpieler == Spielauswahl.None || auswahlComputer == Spielauswahl.None) { throw new ArgumentNullException(); } else { int ergebnis = (int)auswahlSpieler + ((int)auswahlComputer * 6); switch (ergebnis) { case 8: case 11: case 15: case 16: case 19: case 23: case 25: case 27: case 32: case 34: return true; default: return false; } } } } class Program { /// <summary> /// Programmablauf-Steuerung /// </summary> static void Main(string[] args) { Spieler spieler = new Spieler(); Computer computer = new Computer(); bool spielen = true; bool? ergebnis = null; Console.WriteLine("Willkommen bei Stein, Papier, Schere, Echse, Spock!"); do { spieler.TreffeAuswahl(); computer.TreffeAuswahl(); ergebnis = Auswertung.WerteAus(spieler.Auswahl, computer.Auswahl); if (ergebnis == true) { Console.WriteLine("\nGlückwunsch! Sie haben gewonnen!"); Console.WriteLine("{0} schlägt {1}", spieler.Auswahl.ToString(), computer.Auswahl.ToString()); } else if (ergebnis == false) { Console.WriteLine("\nSchade, leider haben Sie nicht gewonnen."); Console.WriteLine("{0} schlägt {1}", computer.Auswahl.ToString(), spieler.Auswahl.ToString()); Console.WriteLine("Viel Glück beim nächsten mal!"); } else { Console.WriteLine("\nDas Ergebnis ist unentschieden!"); Console.WriteLine("Spieler: {0}, Computer: {1}", spieler.Auswahl.ToString(), computer.Auswahl.ToString()); } Console.WriteLine("\nLust auf eine weitere Runde? (j/n):"); string input = Console.ReadLine(); input.ToLower(); if (input == "n") { Console.WriteLine("\nSchade. Bis zum nächsten Mal!"); spielen = false; } else if (input != "j") { Console.WriteLine("\nEingabe fehlerhaft! Spiel wird beendet. Auf Wiedersehen!"); spielen = false; } } while (spielen); } } }

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; } } }

internal static class Program { private static readonly string[] Figure = {"Stein", "Papier", "Schere", "Echse", "Spock"}; private static void Main() { while (true) { Run(); } } private static void Run() { Console.WriteLine("Wählen Sie eine Figur"); Console.WriteLine("1. Stein"); Console.WriteLine("2. Papier"); Console.WriteLine("3. Schere"); Console.WriteLine("4. Echse"); Console.WriteLine("5. Spock"); int input = int.Parse(Console.ReadLine()); Match(input); } private static void Match(int player) { Random r = new Random(); int computer = 0; for (int i = 0; i < 5; ++i) { computer = r.Next(1, 6); } Console.Clear(); Console.WriteLine($"Player: {Figure[player - 1]} vs. Computer: {Figure[computer - 1]}"); if (player == computer) { Console.WriteLine("Unendschieden!"); Run(); } if ( player == 3 && computer == 2 || player == 2 && computer == 1 || player == 1 && computer == 4 || player == 4 && computer == 5 || player == 5 && computer == 3 || player == 3 && computer == 4 || player == 4 && computer == 2 || player == 2 && computer == 5 || player == 5 && computer == 1 || player == 1 && computer == 3 ) { Console.WriteLine("Player Win"); } else { Console.WriteLine("Computer Win"); } Console.Read(); } }