Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program.cs
- using System;
- using System.Reflection;
- namespace WumpusCaveGame
- {
- class Program
- {
- static Game game;
- static void Main(string[] args)
- {
- Console.WriteLine("Please Enter a Number Between 10 and 20 For Cave Size:");
- string s = Console.ReadLine();
- Console.WriteLine();
- int size;
- try
- {
- size = int.Parse(s);
- if (size < 10 || size > 20)
- {
- Console.WriteLine("Wrong Input. Please Ensure the number is between 10 - 20");
- Console.WriteLine("Press Any Key To Continue...");
- Console.ReadLine();
- reset();
- }
- game = new Game(size);
- }
- catch
- {
- Console.WriteLine("Wrong Input. Please Ensure You Input A Number");
- Console.WriteLine("Press Any Key To Coninue...");
- Console.ReadLine();
- reset();
- }
- game.Start();
- }
- static void reset()
- {
- var fileName = Assembly.GetExecutingAssembly().Location;
- System.Diagnostics.Process.Start(fileName);
- Environment.Exit(0);
- }
- }
- }
- Game.cs
- using System;
- namespace WumpusCaveGame
- {
- class Game
- {
- bool gameFinished = false;
- int caveSize;
- int numRooms;
- bool playerDead = false;
- bool playerExit = false;
- bool playerArmed = false;
- int playerScore = 0;
- int playerX;
- int playerY;
- const int WUMPUS_ROOM_PERCENT = 15;
- const int PIT_ROOM_PERCENT = 5;
- const int GOLD_ROOM_PERCENT = 15;
- const int WEAPON_ROOM_PERCENT = 15;
- String[,] display;
- String[,] cave;
- String[] helpMsg = { "? -- help to show this list of moves a player can make",
- "N -- move north 1 space - cannot move north if the cave ends (outside of grid)",
- "S -- move south 1 space - cannot move south if the cave ends (outside of grid)",
- "E -- move east 1 space - cannot move east if the cave ends (outside of grid)",
- "W -- moves west 1 space - cannot move west if the cave ends (outside of grid)",
- "L -- loot either gold or weapon in the room",
- "R -- run out of the cave entrance and head to the local inn to share your tale",
- "X -- this is a hard exit out of the game. The game ends with no points awarded." };
- Random rand = new Random();
- public Game(int caveSize)
- {
- this.caveSize = caveSize;
- this.numRooms = caveSize * caveSize;
- }
- public void Start()
- {
- Console.Clear();
- genCave();
- while (!gameFinished)
- {
- writeScreen();
- if (playerDead)
- {
- gameFinished = true;
- continue;
- }
- String res = Console.ReadLine();
- switch (res.ToUpper())
- {
- case "?":
- Console.WriteLine(Environment.NewLine);;
- foreach (String s in helpMsg)
- Console.WriteLine(s);
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("Press any key to continue...");
- Console.ReadLine();
- break;
- case "N":
- if (!display[playerX, playerY - 1].Equals("#"))
- {
- playerY -= 1;
- checkUnexploredRoom();
- changeDisplay();
- }
- else
- {
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("You try to move North and bump into a wall.");
- Console.WriteLine("Press any key to continue...");
- Console.ReadLine();
- break;
- }
- break;
- case "S":
- if (!display[playerX, playerY + 1].Equals("#"))
- {
- playerY += 1;
- checkUnexploredRoom();
- changeDisplay();
- }
- else
- {
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("You try to move South and bump into a wall.");
- Console.WriteLine("Press any key to continue...");
- Console.ReadLine();
- break;
- }
- break;
- case "E":
- if (!display[playerX + 1, playerY].Equals("#"))
- {
- playerX += 1;
- checkUnexploredRoom();
- changeDisplay();
- }
- else
- {
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("You try to move East and bump into a wall.");
- Console.WriteLine("Press any key to continue...");
- Console.ReadLine();
- break;
- }
- break;
- case "W":
- if (!display[playerX - 1, playerY].Equals("#"))
- {
- playerX -= 1;
- checkUnexploredRoom();
- changeDisplay();
- }
- else
- {
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("You try to move West and bump into a wall.");
- Console.WriteLine("Press any key to continue...");
- Console.ReadLine();
- break;
- }
- break;
- case "L":
- if (cave[playerX, playerY].Equals("$") || cave[playerX, playerY].Equals("W"))
- {
- switch (cave[playerX, playerY])
- {
- case "$":
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("You loot the gold on the floor and gain 5 points.");
- playerScore += 5;
- cave[playerX, playerY] = ".";
- changeDisplay();
- Console.WriteLine("Press any key to continue...");
- Console.ReadLine();
- break;
- case "W":
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("You pull the sword from the rock and gain 5 points.");
- playerArmed = true;
- playerScore += 5;
- cave[playerX, playerY] = ".";
- changeDisplay();
- for (int x = 0; x < cave.GetLength(0); x++)
- {
- for (int y = 0; y < cave.GetLength(1); y++)
- {
- if (cave[x, y].Equals("W"))
- cave[x, y] = "$";
- }
- }
- Console.WriteLine("Press any key to continue...");
- Console.ReadLine();
- break;
- }
- }
- else
- {
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("There is nothing here to loot.");
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("Press any key to continue...");
- Console.ReadLine();
- break;
- }
- break;
- case "R":
- if (cave[playerX, playerY].Equals("^"))
- {
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("You exit the Wumpus cave and run to town.");
- Console.WriteLine("People buy you ales as you tell the story of your adventure.");
- Console.WriteLine("***GAME OVER***");
- Console.WriteLine("You scored " + playerScore + " points! Well Played!");
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("Press any key to continue...");
- Console.ReadLine();
- gameFinished = true;
- break;
- }
- else
- {
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("You try to run from the cave but realise the exit is nowhere to be found.");
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("Press any key to continue...");
- Console.ReadLine();
- break;
- }
- case "X":
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("The cave proves to be too tough for you so you run away as fast as you can.");
- Console.WriteLine("***GAME OVER***");
- Console.WriteLine("You scored " + playerScore + " points! Well Played!");
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("Press any key to continue...");
- Console.ReadLine();
- gameFinished = true;
- break;
- default:
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("Invalid Command.");
- Console.WriteLine("Please try again.");
- Console.ReadLine();
- break;
- }
- Console.Clear();
- }
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("Press any key to exit");
- Console.ReadLine();
- Environment.Exit(0);
- }
- public void writeScreen()
- {
- string desc = "";
- switch (cave[playerX, playerY])
- {
- case "^":
- desc = "Entrance Room -- you see see the entrance here. You wish to run away?";
- break;
- case ".":
- desc = "Empty Room -- you see nothing which is something";
- break;
- case "P":
- desc = "Pit trap -- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahhhhhhhhhh noooooooooooooooooo Splat";
- break;
- case "!":
- desc = "Wumpus Room -- Overwhelmed in Stench a Wumpus stands before you ready to eat you.";
- break;
- case "$":
- desc = "Gold Room - before you lies the the gold of adventure seekers who feed a Wumpus Recently";
- break;
- case "W":
- desc = "Weapon Room - Cast before you in a rock a sword awaits to be looted and name yourself King.";
- break;
- default:
- desc = "WTF is this room???";
- break;
- }
- if ((cave[playerX, playerY].Equals("!") && !playerArmed) || cave[playerX, playerY].Equals("P"))
- {
- Console.WriteLine(desc);
- switch (cave[playerX, playerY])
- {
- case "!":
- Console.WriteLine("A Wumpus attacks you and makes you his lunch.");
- break;
- case "P":
- Console.WriteLine("You fall to your death. Your screams are heard by no one.");
- break;
- }
- Console.WriteLine(Environment.NewLine);;
- Console.WriteLine("***GAME OVER***");
- Console.WriteLine("You scored " + playerScore + " Points!");
- playerDead = true;
- return;
- }
- Console.WriteLine(Environment.NewLine);
- Console.WriteLine(Environment.NewLine);
- Console.Write(display[playerX - 1, playerY - 1]);
- Console.Write(display[playerX, playerY - 1]);
- Console.Write(display[playerX + 1, playerY - 1]);
- Console.WriteLine(Environment.NewLine);;
- Console.Write(display[playerX - 1, playerY]);
- Console.Write("@");
- Console.Write(display[playerX + 1, playerY]);
- Console.WriteLine(Environment.NewLine);;
- Console.Write(display[playerX - 1, playerY + 1]);
- Console.Write(display[playerX, playerY + 1]);
- Console.Write(display[playerX + 1, playerY + 1]);
- Console.WriteLine(Environment.NewLine);
- bool wumpusDetected = false;
- bool trapDetected = false;
- if (cave[playerX - 1, playerY].Equals("!"))
- wumpusDetected = true;
- else if (cave[playerX + 1, playerY].Equals("!"))
- wumpusDetected = true;
- else if (cave[playerX, playerY - 1].Equals("!"))
- wumpusDetected = true;
- else if (cave[playerX, playerY + 1].Equals("!"))
- wumpusDetected = true;
- if (cave[playerX - 1, playerY].Equals("P"))
- trapDetected = true;
- else if (cave[playerX + 1, playerY].Equals("P"))
- trapDetected = true;
- else if (cave[playerX, playerY - 1].Equals("P"))
- trapDetected = true;
- else if (cave[playerX, playerY + 1].Equals("P"))
- trapDetected = true;
- string pointS = "[" + playerScore + " points earned] ";
- if (playerArmed)
- pointS += "You are armed and dangerous";
- else
- pointS += "You are unarmed";
- Console.WriteLine(desc);
- checkWumpusKill();
- if (wumpusDetected)
- Console.WriteLine("You Detect a Foul Stench in the Air");
- if (trapDetected)
- Console.WriteLine("You Hear a howling wind");
- Console.WriteLine(pointS);
- Console.Write("Enter Move (? for help) > ");
- }
- public void genCave()
- {
- // Create Arrays using caveSize + 2 for the wall border
- display = new String[caveSize + 2, caveSize + 2];
- cave = new String[caveSize + 2, caveSize + 2];
- // Initialize Arrays, adding walls, empty rooms and undiscovered rooms to display.
- for (int y = 0; y < display.GetLength(0); y++)
- {
- for (int x = 0; x < display.GetLength(1); x++)
- {
- if (y == 0 || y == (display.GetLength(0) - 1) ||
- x == 0 || x == (display.GetLength(1) - 1))
- {
- display[y, x] = "#";
- cave[y, x] = "#";
- }
- else
- {
- display[y, x] = "?";
- cave[y, x] = ".";
- }
- }
- }
- // Add Enemy rooms to array
- for (int i = 0; i < (numRooms / 100) * WUMPUS_ROOM_PERCENT; i++)
- {
- int x = rand.Next(display.GetLength(0));
- int y = rand.Next(display.GetLength(1));
- if (cave[x, y].Equals("."))
- {
- cave[x, y] = "!";
- }
- else
- {
- i--;
- }
- }
- // Add Trap rooms to array
- for (int i = 0; i < (numRooms / 100) * PIT_ROOM_PERCENT; i++)
- {
- int x = rand.Next(display.GetLength(0));
- int y = rand.Next(display.GetLength(1));
- if (cave[x, y].Equals("."))
- {
- cave[x, y] = "P";
- }
- else
- {
- i--;
- }
- }
- // Add Gold rooms to array
- for (int i = 0; i < (numRooms / 100) * GOLD_ROOM_PERCENT; i++)
- {
- int x = rand.Next(display.GetLength(0));
- int y = rand.Next(display.GetLength(1));
- if (cave[x, y].Equals("."))
- {
- cave[x, y] = "$";
- }
- else
- {
- i--;
- }
- }
- // Add Weapon rooms to array
- for (int i = 0; i < (numRooms / 100) * WEAPON_ROOM_PERCENT; i++)
- {
- int x = rand.Next(display.GetLength(0));
- int y = rand.Next(display.GetLength(1));
- if (cave[x, y].Equals("."))
- {
- cave[x, y] = "W";
- }
- else
- {
- i--;
- }
- }
- for (int i = 0; i < 1; i++)
- {
- int x = rand.Next(display.GetLength(0));
- int y = rand.Next(display.GetLength(1));
- if (cave[x, y].Equals("."))
- {
- cave[x, y] = "^";
- display[x, y] = "^";
- playerX = x;
- playerY = y;
- }
- else
- {
- i--;
- }
- }
- }
- public void checkUnexploredRoom()
- {
- if (cave[playerX, playerY].Equals(".") && display[playerX, playerY].Equals("?"))
- playerScore += 1;
- }
- public void changeDisplay()
- {
- if (!display[playerX, playerY].Equals(cave[playerX, playerY]))
- display[playerX, playerY] = cave[playerX, playerY];
- }
- public void checkWumpusKill()
- {
- if (cave[playerX, playerY].Equals("!") && playerArmed)
- {
- Console.WriteLine("You slay the mighty Wumpus and gain 10 points.");
- playerScore += 10;
- cave[playerX, playerY] = ".";
- changeDisplay();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement