SHARE
TWEET

Maze Game

a guest May 22nd, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace MazeGame
  2. {
  3. //Exercise4_3
  4.  
  5. public class Maze
  6.     {
  7.         private Room startRoom;
  8.         private Room winningRoom;
  9.         private Room losingRoom;
  10.  
  11.         public Maze()
  12.         {
  13.             //Rooms
  14.             Room bedroom = new Room("bedroom");
  15.             Room kitchen = new Room("kitchen");
  16.             Room boxroom = new Room("boxroom");
  17.             Room garage = new Room("garage");
  18.             Room childsRoom = new Room("childsRoom");
  19.             Room garden = new Room("garden");
  20.             Room livingroom = new Room("livingroom");
  21.             //Connection
  22.             garden.setConnectedRooms(null, livingroom, null, null);
  23.             livingroom.setConnectedRooms(garden, bedroom, garage, null);
  24.             bedroom.setConnectedRooms(livingroom, childsRoom, kitchen, null);
  25.             childsRoom.setConnectedRooms(bedroom, null, boxroom, null);
  26.             boxroom.setConnectedRooms(kitchen, null, null, childsRoom);
  27.             kitchen.setConnectedRooms(garage, boxroom,null , bedroom);
  28.             garage.setConnectedRooms(null, kitchen, null, livingroom);
  29.  
  30.             startRoom = kitchen;
  31.             winningRoom = garden;
  32.             losingRoom = childsRoom;
  33.         }
  34.  
  35.         public Room getStartRoom()
  36.         {
  37.             return startRoom;
  38.         }
  39.  
  40.         public Room getWinningRoom()
  41.         {
  42.             return winningRoom;
  43.         }
  44.  
  45.         public Room getLosingRoom()
  46.         {
  47.             return losingRoom;
  48.         }
  49.     }
  50.  
  51. //Exercise4_4
  52.  
  53. public class Player
  54.     {
  55.         private Room currentRoom;
  56.         public Player(Room _currentRoom)
  57.         {
  58.             currentRoom = _currentRoom;
  59.         }
  60.  
  61.         public Room getCurrentRoom()
  62.         {
  63.             return this.currentRoom;
  64.         }
  65.  
  66.         public bool move (char direction)
  67.         {
  68.             Room nextroom = currentRoom.getConnectedRoom(direction);
  69.  
  70.             if (nextroom == null)
  71.             {
  72.                 return false;
  73.             }
  74.             else { currentRoom = nextroom;return true; }
  75.         }
  76.     }
  77.  
  78. //Exercise4_2
  79.  
  80. public class Room
  81.     {
  82.         private string name;
  83.  
  84.         private Room south;
  85.         private Room north;
  86.         private Room west;
  87.         private Room east;
  88.  
  89.  
  90.         public Room(string _name)
  91.         {
  92.             name = _name;
  93.         }
  94.  
  95.         public string getName()
  96.         {
  97.             return this.name;
  98.         }
  99.  
  100.         public void setConnectedRooms(Room north, Room south, Room west, Room east)
  101.         {
  102.             this.north = north;
  103.             this.south = south;
  104.             this.west = west;
  105.             this.east = east;
  106.         }
  107.  
  108.         public Room getConnectedRoom(char direction)
  109.         {
  110.             switch (direction)
  111.             {
  112.                 case 'n': return north;
  113.                 case 'e': return east;
  114.                 case 's': return south;
  115.                 case 'w': return west;
  116.                 default: return null;
  117.  
  118.             }
  119.         }
  120.     }
  121.  
  122. //Exercise4_5
  123. class Program
  124.     {
  125.         static void Main(string[] args)
  126.         {
  127.            
  128.            Maze maze = new Maze();
  129.             Player Kobosil = new Player(maze.getStartRoom());
  130.  
  131.             while (Kobosil.getCurrentRoom() != maze.getWinningRoom() && Kobosil.getCurrentRoom() != maze.getLosingRoom())
  132.             {
  133.                 Console.WriteLine("You are in the " + Kobosil.getCurrentRoom().getName());
  134.                 Console.WriteLine("Type in 'n', 'e', 's' or 'w' to move to another room!");
  135.                 Console.WriteLine();
  136.                 char success = Convert.ToChar(Console.ReadLine());
  137.                 Console.WriteLine();
  138.                 if (Kobosil.move(success) == true)
  139.                 {
  140.                     Console.WriteLine("You moved to another Romm.");
  141.                     Console.WriteLine();
  142.                 }
  143.                 else Console.WriteLine("You ran into a wall. Idiot!");
  144.             }
  145.             if (Kobosil.getCurrentRoom() == maze.getWinningRoom())
  146.             {
  147.                 Console.WriteLine("Awesome! You win");
  148.             }
  149.             else Console.WriteLine("To Bad! You Loose");
  150.          
  151.             Console.ReadKey();
  152.         }
  153.     }
  154.  
  155. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top