|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +class RockPaperScissors{ |
| 6 | + static void Main(string[] args){ |
| 7 | + Console.WriteLine("<-----------RockPaperScissors----------->"); |
| 8 | + Console.WriteLine("Created by Morasiu (morasiu2@gmail.com)"); |
| 9 | + |
| 10 | + Game(); |
| 11 | + Console.ReadKey(); |
| 12 | + } |
| 13 | + |
| 14 | + static void Game(){ |
| 15 | + List<string> playerPicks = new List<string>(); |
| 16 | + int playerWin = 0; |
| 17 | + int compWin = 0; |
| 18 | + while (true){ |
| 19 | + string playerPick = GetPlayerPick(); |
| 20 | + playerPicks.Add(playerPick); |
| 21 | + string compPick = GetComputerPick(playerPicks); |
| 22 | + Console.WriteLine("Your: " + GetFullName(playerPick) + "\nComputer: " + GetFullName(compPick) + "\n"); |
| 23 | + //Hm... Why not switch? It looks nice. |
| 24 | + switch (playerPick){ |
| 25 | + case "r": |
| 26 | + if (compPick == "p"){ |
| 27 | + Console.WriteLine("Paper beats rock.\nYou lose."); |
| 28 | + compWin++; |
| 29 | + } else if (compPick == "s"){ |
| 30 | + Console.WriteLine("Rock beats scissors.\nYou win!"); |
| 31 | + playerWin++; |
| 32 | + } else if (compPick == "r"){ |
| 33 | + Console.WriteLine("Rock vs rock.\nIt's a draw."); |
| 34 | + } |
| 35 | + break; |
| 36 | + case "s": |
| 37 | + if (compPick == "r"){ |
| 38 | + Console.WriteLine("Rock beats scissors.\nYou lose."); |
| 39 | + compWin++; |
| 40 | + } else if (compPick == "p"){ |
| 41 | + Console.WriteLine("Scissors beats paper.\nYou win!"); |
| 42 | + playerWin++; |
| 43 | + } else if (compPick == "s"){ |
| 44 | + Console.WriteLine("Scissors vs scissors.\nIt's a draw."); |
| 45 | + } |
| 46 | + break; |
| 47 | + case "p": |
| 48 | + if (compPick == "s"){ |
| 49 | + Console.WriteLine("Scissors beats paper.\nYou lose."); |
| 50 | + compWin++; |
| 51 | + } else if (compPick == "r"){ |
| 52 | + Console.WriteLine("Paper beats rock.\nYou win!"); |
| 53 | + playerWin++; |
| 54 | + } else if (compPick == "p"){ |
| 55 | + Console.WriteLine("Paper vs paper.\nIt's a draw."); |
| 56 | + } |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + Console.WriteLine("You: " + playerWin + " Computer: " + compWin); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + static string GetComputerPick(List<string> playerPicks){ |
| 65 | + //Of course I could make some random, but where is the fun? |
| 66 | + string pick = ""; |
| 67 | + if (playerPicks.Count == 1){ |
| 68 | + Random rand = new Random(); |
| 69 | + string[] possiblePicks = new [] {"r", "p", "s"}; |
| 70 | + int randIndex = rand.Next(0, possiblePicks.Length); |
| 71 | + pick = possiblePicks[randIndex]; |
| 72 | + } else{ |
| 73 | + //AI time. It's my algorytm. |
| 74 | + |
| 75 | + } |
| 76 | + return pick; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + static string GetWinPick(string pick){ |
| 81 | + //Return pick which will win with given pick. |
| 82 | + string winPick; |
| 83 | + if (pick == "s") |
| 84 | + winPick = "r"; |
| 85 | + else if (pick == "p") |
| 86 | + winPick == "s"; |
| 87 | + else if (pick == "r") |
| 88 | + winPick == "p"; |
| 89 | + return winPick; |
| 90 | + } |
| 91 | + |
| 92 | + static string GetPlayerPick(){ |
| 93 | + while (true) { |
| 94 | + Console.WriteLine("\nPick rock (r), paper (p) or scissors (s): "); |
| 95 | + string pick = Console.ReadLine(); |
| 96 | + if (new [] {"r", "p", "s"}.Contains(pick)) |
| 97 | + return pick; |
| 98 | + Console.WriteLine("Wrong option"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + static string GetFullName(string pick){ |
| 103 | + switch (pick){ |
| 104 | + case "r": |
| 105 | + return "Rock"; |
| 106 | + case "p": |
| 107 | + return "Paper"; |
| 108 | + case "s": |
| 109 | + return "Scissors"; |
| 110 | + default: |
| 111 | + Console.WriteLine("Wrong option"); |
| 112 | + return ""; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments