Advertisement
Brad522

WumpusCaveGame

Apr 6th, 2014
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 20.02 KB | None | 0 0
  1. Program.cs
  2.  
  3. using System;
  4. using System.Reflection;
  5.  
  6. namespace WumpusCaveGame
  7. {
  8.     class Program
  9.     {
  10.  
  11.         static Game game;
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             Console.WriteLine("Please Enter a Number Between 10 and 20 For Cave Size:");
  16.             string s = Console.ReadLine();
  17.             Console.WriteLine();
  18.             int size;
  19.  
  20.             try
  21.             {
  22.                 size = int.Parse(s);
  23.  
  24.                 if (size < 10 || size > 20)
  25.                 {
  26.                     Console.WriteLine("Wrong Input. Please Ensure the number is between 10 - 20");
  27.                     Console.WriteLine("Press Any Key To Continue...");
  28.                     Console.ReadLine();
  29.                     reset();
  30.                 }
  31.  
  32.                 game = new Game(size);
  33.             }
  34.             catch
  35.             {
  36.                 Console.WriteLine("Wrong Input. Please Ensure You Input A Number");
  37.                 Console.WriteLine("Press Any Key To Coninue...");
  38.                 Console.ReadLine();
  39.                 reset();
  40.             }
  41.  
  42.             game.Start();
  43.  
  44.         }
  45.  
  46.         static void reset()
  47.         {
  48.             var fileName = Assembly.GetExecutingAssembly().Location;
  49.             System.Diagnostics.Process.Start(fileName);
  50.  
  51.             Environment.Exit(0);
  52.         }
  53.     }
  54. }
  55.  
  56. Game.cs
  57.  
  58. using System;
  59.  
  60. namespace WumpusCaveGame
  61. {
  62.     class Game
  63.     {
  64.         bool gameFinished = false;
  65.         int caveSize;
  66.         int numRooms;
  67.  
  68.         bool playerDead = false;
  69.         bool playerExit = false;
  70.  
  71.         bool playerArmed = false;
  72.         int playerScore = 0;
  73.         int playerX;
  74.         int playerY;
  75.  
  76.         const int WUMPUS_ROOM_PERCENT = 15;
  77.         const int PIT_ROOM_PERCENT = 5;
  78.         const int GOLD_ROOM_PERCENT = 15;
  79.         const int WEAPON_ROOM_PERCENT = 15;
  80.  
  81.         String[,] display;
  82.         String[,] cave;
  83.  
  84.         String[] helpMsg =  { "? -- help to show this list of moves a player can make",
  85.                               "N -- move north 1 space - cannot move north if the cave ends (outside of grid)",
  86.                               "S -- move south 1 space - cannot move south if the cave ends (outside of grid)",
  87.                               "E -- move east 1 space - cannot move east if the cave ends (outside of grid)",
  88.                               "W -- moves west 1 space - cannot move west if the cave ends (outside of grid)",
  89.                               "L -- loot either gold or weapon in the room",
  90.                               "R -- run out of the cave entrance and head to the local inn to share your tale",
  91.                               "X -- this is a hard exit out of the game. The game ends with no points awarded." };
  92.  
  93.         Random rand = new Random();
  94.  
  95.         public Game(int caveSize)
  96.         {
  97.             this.caveSize = caveSize;
  98.             this.numRooms = caveSize * caveSize;
  99.         }
  100.  
  101.         public void Start()
  102.         {
  103.             Console.Clear();
  104.  
  105.             genCave();
  106.  
  107.             while (!gameFinished)
  108.             {
  109.  
  110.                 writeScreen();
  111.  
  112.                 if (playerDead)
  113.                 {
  114.                     gameFinished = true;
  115.                     continue;
  116.                 }
  117.  
  118.                 String res = Console.ReadLine();
  119.  
  120.                 switch (res.ToUpper())
  121.                 {
  122.                     case "?":
  123.                         Console.WriteLine(Environment.NewLine);;
  124.                         foreach (String s in helpMsg)
  125.                             Console.WriteLine(s);
  126.                         Console.WriteLine(Environment.NewLine);;
  127.                         Console.WriteLine("Press any key to continue...");
  128.                         Console.ReadLine();
  129.                         break;
  130.                     case "N":
  131.                         if (!display[playerX, playerY - 1].Equals("#"))
  132.                         {
  133.                             playerY -= 1;
  134.                             checkUnexploredRoom();
  135.                             changeDisplay();
  136.                         }
  137.                         else
  138.                         {
  139.                             Console.WriteLine(Environment.NewLine);;
  140.                             Console.WriteLine("You try to move North and bump into a wall.");
  141.                             Console.WriteLine("Press any key to continue...");
  142.                             Console.ReadLine();
  143.                             break;
  144.                         }
  145.                         break;
  146.                     case "S":
  147.                         if (!display[playerX, playerY + 1].Equals("#"))
  148.                         {
  149.                             playerY += 1;
  150.                             checkUnexploredRoom();
  151.                             changeDisplay();
  152.                         }
  153.                         else
  154.                         {
  155.                             Console.WriteLine(Environment.NewLine);;
  156.                             Console.WriteLine("You try to move South and bump into a wall.");
  157.                             Console.WriteLine("Press any key to continue...");
  158.                             Console.ReadLine();
  159.                             break;
  160.                         }
  161.                         break;
  162.                     case "E":
  163.                         if (!display[playerX + 1, playerY].Equals("#"))
  164.                         {
  165.                             playerX += 1;
  166.                             checkUnexploredRoom();
  167.                             changeDisplay();
  168.                         }
  169.                         else
  170.                         {
  171.                             Console.WriteLine(Environment.NewLine);;
  172.                             Console.WriteLine("You try to move East and bump into a wall.");
  173.                             Console.WriteLine("Press any key to continue...");
  174.                             Console.ReadLine();
  175.                             break;
  176.                         }
  177.                         break;
  178.                     case "W":
  179.                         if (!display[playerX - 1, playerY].Equals("#"))
  180.                         {
  181.                             playerX -= 1;
  182.                             checkUnexploredRoom();
  183.                             changeDisplay();
  184.                         }
  185.                         else
  186.                         {
  187.                             Console.WriteLine(Environment.NewLine);;
  188.                             Console.WriteLine("You try to move West and bump into a wall.");
  189.                             Console.WriteLine("Press any key to continue...");
  190.                             Console.ReadLine();
  191.                             break;
  192.                         }
  193.                         break;
  194.                     case "L":
  195.                         if (cave[playerX, playerY].Equals("$") || cave[playerX, playerY].Equals("W"))
  196.                         {
  197.                             switch (cave[playerX, playerY])
  198.                             {
  199.                                 case "$":
  200.                                     Console.WriteLine(Environment.NewLine);;
  201.                                     Console.WriteLine("You loot the gold on the floor and gain 5 points.");
  202.                                     playerScore += 5;
  203.                                     cave[playerX, playerY] = ".";
  204.                                     changeDisplay();
  205.                                     Console.WriteLine("Press any key to continue...");
  206.                                     Console.ReadLine();
  207.                                     break;
  208.                                 case "W":
  209.                                     Console.WriteLine(Environment.NewLine);;
  210.                                     Console.WriteLine("You pull the sword from the rock and gain 5 points.");
  211.                                     playerArmed = true;
  212.                                     playerScore += 5;
  213.                                     cave[playerX, playerY] = ".";
  214.                                     changeDisplay();
  215.                                     for (int x = 0; x < cave.GetLength(0); x++)
  216.                                     {
  217.                                         for (int y = 0; y < cave.GetLength(1); y++)
  218.                                         {
  219.                                             if (cave[x, y].Equals("W"))
  220.                                                 cave[x, y] = "$";
  221.                                         }
  222.                                     }
  223.                                     Console.WriteLine("Press any key to continue...");
  224.                                     Console.ReadLine();
  225.                                     break;
  226.                             }
  227.                         }
  228.                         else
  229.                         {
  230.                             Console.WriteLine(Environment.NewLine);;
  231.                             Console.WriteLine("There is nothing here to loot.");
  232.                             Console.WriteLine(Environment.NewLine);;
  233.                             Console.WriteLine("Press any key to continue...");
  234.                             Console.ReadLine();
  235.                             break;
  236.                         }
  237.                         break;
  238.                     case "R":
  239.                         if (cave[playerX, playerY].Equals("^"))
  240.                         {
  241.                             Console.WriteLine(Environment.NewLine);;
  242.                             Console.WriteLine("You exit the Wumpus cave and run to town.");
  243.                             Console.WriteLine("People buy you ales as you tell the story of your adventure.");
  244.                             Console.WriteLine("***GAME OVER***");
  245.                             Console.WriteLine("You scored " + playerScore + " points! Well Played!");
  246.                             Console.WriteLine(Environment.NewLine);;
  247.                             Console.WriteLine("Press any key to continue...");
  248.                             Console.ReadLine();
  249.                             gameFinished = true;
  250.                             break;
  251.                         }
  252.                         else
  253.                         {
  254.                             Console.WriteLine(Environment.NewLine);;
  255.                             Console.WriteLine("You try to run from the cave but realise the exit is nowhere to be found.");
  256.                             Console.WriteLine(Environment.NewLine);;
  257.                             Console.WriteLine("Press any key to continue...");
  258.                             Console.ReadLine();
  259.                             break;
  260.                         }
  261.                     case "X":
  262.                         Console.WriteLine(Environment.NewLine);;
  263.                         Console.WriteLine("The cave proves to be too tough for you so you run away as fast as you can.");
  264.                         Console.WriteLine("***GAME OVER***");
  265.                         Console.WriteLine("You scored " + playerScore + " points! Well Played!");
  266.                         Console.WriteLine(Environment.NewLine);;
  267.                         Console.WriteLine("Press any key to continue...");
  268.                         Console.ReadLine();
  269.                         gameFinished = true;
  270.                         break;
  271.                     default:
  272.                         Console.WriteLine(Environment.NewLine);;
  273.                         Console.WriteLine("Invalid Command.");
  274.                         Console.WriteLine("Please try again.");
  275.                         Console.ReadLine();
  276.                         break;
  277.                 }
  278.  
  279.                 Console.Clear();
  280.             }
  281.  
  282.             Console.WriteLine(Environment.NewLine);;
  283.             Console.WriteLine("Press any key to exit");
  284.             Console.ReadLine();
  285.             Environment.Exit(0);
  286.         }
  287.  
  288.         public void writeScreen()
  289.         {
  290.  
  291.             string desc = "";
  292.  
  293.             switch (cave[playerX, playerY])
  294.             {
  295.                 case "^":
  296.                     desc = "Entrance Room -- you see see the entrance here. You wish to run away?";
  297.                     break;
  298.                 case ".":
  299.                     desc = "Empty Room -- you see nothing which is something";
  300.                     break;
  301.                 case "P":
  302.                     desc = "Pit trap -- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahhhhhhhhhh noooooooooooooooooo Splat";
  303.                     break;
  304.                 case "!":
  305.                     desc = "Wumpus Room -- Overwhelmed in Stench a Wumpus stands before you ready to eat you.";
  306.                     break;
  307.                 case "$":
  308.                     desc = "Gold Room - before you lies the the gold of adventure seekers who feed a Wumpus Recently";
  309.                     break;
  310.                 case "W":
  311.                     desc = "Weapon Room - Cast before you in a rock a sword awaits to be looted and name yourself King.";
  312.                     break;
  313.                 default:
  314.                     desc = "WTF is this room???";
  315.                     break;
  316.             }
  317.  
  318.             if ((cave[playerX, playerY].Equals("!") && !playerArmed) || cave[playerX, playerY].Equals("P"))
  319.             {
  320.                 Console.WriteLine(desc);
  321.                 switch (cave[playerX, playerY])
  322.                 {
  323.                     case "!":
  324.                         Console.WriteLine("A Wumpus attacks you and makes you his lunch.");
  325.                         break;
  326.                     case "P":
  327.                         Console.WriteLine("You fall to your death. Your screams are heard by no one.");
  328.                         break;
  329.                 }
  330.                 Console.WriteLine(Environment.NewLine);;
  331.                 Console.WriteLine("***GAME OVER***");
  332.                 Console.WriteLine("You scored " + playerScore + " Points!");
  333.                 playerDead = true;
  334.                 return;
  335.             }
  336.  
  337.             Console.WriteLine(Environment.NewLine);
  338.             Console.WriteLine(Environment.NewLine);
  339.             Console.Write(display[playerX - 1, playerY - 1]);
  340.             Console.Write(display[playerX, playerY - 1]);
  341.             Console.Write(display[playerX + 1, playerY - 1]);
  342.             Console.WriteLine(Environment.NewLine);;
  343.             Console.Write(display[playerX - 1, playerY]);
  344.             Console.Write("@");
  345.             Console.Write(display[playerX + 1, playerY]);
  346.             Console.WriteLine(Environment.NewLine);;
  347.             Console.Write(display[playerX - 1, playerY + 1]);
  348.             Console.Write(display[playerX, playerY + 1]);
  349.             Console.Write(display[playerX + 1, playerY + 1]);
  350.             Console.WriteLine(Environment.NewLine);
  351.  
  352.             bool wumpusDetected = false;
  353.             bool trapDetected = false;
  354.  
  355.             if (cave[playerX - 1, playerY].Equals("!"))
  356.                 wumpusDetected = true;
  357.             else if (cave[playerX + 1, playerY].Equals("!"))
  358.                 wumpusDetected = true;
  359.             else if (cave[playerX, playerY - 1].Equals("!"))
  360.                 wumpusDetected = true;
  361.             else if (cave[playerX, playerY + 1].Equals("!"))
  362.                 wumpusDetected = true;
  363.             if (cave[playerX - 1, playerY].Equals("P"))
  364.                 trapDetected = true;
  365.             else if (cave[playerX + 1, playerY].Equals("P"))
  366.                 trapDetected = true;
  367.             else if (cave[playerX, playerY - 1].Equals("P"))
  368.                 trapDetected = true;
  369.             else if (cave[playerX, playerY + 1].Equals("P"))
  370.                 trapDetected = true;
  371.  
  372.             string pointS = "[" + playerScore + " points earned] ";
  373.             if (playerArmed)
  374.                 pointS += "You are armed and dangerous";
  375.             else
  376.                 pointS += "You are unarmed";
  377.  
  378.             Console.WriteLine(desc);
  379.             checkWumpusKill();
  380.             if (wumpusDetected)
  381.                 Console.WriteLine("You Detect a Foul Stench in the Air");
  382.             if (trapDetected)
  383.                 Console.WriteLine("You Hear a howling wind");
  384.             Console.WriteLine(pointS);
  385.             Console.Write("Enter Move (? for help) > ");
  386.         }
  387.  
  388.         public void genCave()
  389.         {
  390.             // Create Arrays using caveSize + 2 for the wall border
  391.             display = new String[caveSize + 2, caveSize + 2];
  392.             cave = new String[caveSize + 2, caveSize + 2];
  393.  
  394.             // Initialize Arrays, adding walls, empty rooms and undiscovered rooms to display.
  395.             for (int y = 0; y < display.GetLength(0); y++)
  396.             {
  397.                 for (int x = 0; x < display.GetLength(1); x++)
  398.                 {
  399.                     if (y == 0 || y == (display.GetLength(0) - 1) ||
  400.                        x == 0 || x == (display.GetLength(1) - 1))
  401.                     {
  402.                         display[y, x] = "#";
  403.                         cave[y, x] = "#";
  404.                     }
  405.                     else
  406.                     {
  407.                         display[y, x] = "?";
  408.                         cave[y, x] = ".";
  409.                     }
  410.                 }
  411.             }
  412.  
  413.             // Add Enemy rooms to array
  414.             for (int i = 0; i < (numRooms / 100) * WUMPUS_ROOM_PERCENT; i++)
  415.             {
  416.                 int x = rand.Next(display.GetLength(0));
  417.                 int y = rand.Next(display.GetLength(1));
  418.  
  419.                 if (cave[x, y].Equals("."))
  420.                 {
  421.                     cave[x, y] = "!";
  422.                 }
  423.                 else
  424.                 {
  425.                     i--;
  426.                 }
  427.             }
  428.  
  429.             // Add Trap rooms to array
  430.             for (int i = 0; i < (numRooms / 100) * PIT_ROOM_PERCENT; i++)
  431.             {
  432.                 int x = rand.Next(display.GetLength(0));
  433.                 int y = rand.Next(display.GetLength(1));
  434.  
  435.                 if (cave[x, y].Equals("."))
  436.                 {
  437.                     cave[x, y] = "P";
  438.                 }
  439.                 else
  440.                 {
  441.                     i--;
  442.                 }
  443.             }
  444.  
  445.             // Add Gold rooms to array
  446.             for (int i = 0; i < (numRooms / 100) * GOLD_ROOM_PERCENT; i++)
  447.             {
  448.                 int x = rand.Next(display.GetLength(0));
  449.                 int y = rand.Next(display.GetLength(1));
  450.  
  451.                 if (cave[x, y].Equals("."))
  452.                 {
  453.                     cave[x, y] = "$";
  454.                 }
  455.                 else
  456.                 {
  457.                     i--;
  458.                 }
  459.             }
  460.  
  461.             // Add Weapon rooms to array
  462.             for (int i = 0; i < (numRooms / 100) * WEAPON_ROOM_PERCENT; i++)
  463.             {
  464.                 int x = rand.Next(display.GetLength(0));
  465.                 int y = rand.Next(display.GetLength(1));
  466.  
  467.                 if (cave[x, y].Equals("."))
  468.                 {
  469.                     cave[x, y] = "W";
  470.                 }
  471.                 else
  472.                 {
  473.                     i--;
  474.                 }
  475.             }
  476.  
  477.             for (int i = 0; i < 1; i++)
  478.             {
  479.                 int x = rand.Next(display.GetLength(0));
  480.                 int y = rand.Next(display.GetLength(1));
  481.  
  482.                 if (cave[x, y].Equals("."))
  483.                 {
  484.                     cave[x, y] = "^";
  485.                     display[x, y] = "^";
  486.                     playerX = x;
  487.                     playerY = y;
  488.                 }
  489.                 else
  490.                 {
  491.                     i--;
  492.                 }
  493.             }
  494.         }
  495.  
  496.         public void checkUnexploredRoom()
  497.         {
  498.             if (cave[playerX, playerY].Equals(".") && display[playerX, playerY].Equals("?"))
  499.                 playerScore += 1;
  500.         }
  501.  
  502.         public void changeDisplay()
  503.         {
  504.             if (!display[playerX, playerY].Equals(cave[playerX, playerY]))
  505.                 display[playerX, playerY] = cave[playerX, playerY];
  506.         }
  507.  
  508.         public void checkWumpusKill()
  509.         {
  510.             if (cave[playerX, playerY].Equals("!") && playerArmed)
  511.             {
  512.                 Console.WriteLine("You slay the mighty Wumpus and gain 10 points.");
  513.                 playerScore += 10;
  514.                 cave[playerX, playerY] = ".";
  515.                 changeDisplay();
  516.             }
  517.         }
  518.     }
  519. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement