Advertisement
Guest User

Erise 1.0 xD

a guest
Nov 28th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.48 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.System.*;
  3. import java.io.*;
  4.  
  5.  
  6.  
  7. class wumpus{
  8.  
  9.     private BufferedReader      reader;
  10.  
  11.     private Random              generator;
  12.     private int                 locationExits[][];
  13.     private int                 currentLocation;
  14.    
  15.     private int                 steps;
  16.     private int                 arrows;
  17.    
  18.     private int                 wumpusLocation;
  19.     private boolean             wumpusAwake;
  20.    
  21.     private int                 bats[];
  22.     private int                 holes[];
  23.    
  24.    
  25.    
  26.     private void init(){
  27.    
  28.         reader          = new BufferedReader(new InputStreamReader(System.in));
  29.         generator       = new Random();
  30.    
  31.         locationExits   = new int[20][3];
  32.         generateBurrow();
  33.        
  34.         currentLocation = generator.nextInt(20);
  35.        
  36.         steps           = 0;
  37.         arrows          = 5;
  38.         do{
  39.             wumpusLocation = generator.nextInt(20);
  40.         }while(wumpusLocation == currentLocation);
  41.        
  42.         wumpusAwake     = false;
  43.        
  44.         bats            = new int[2];
  45.        
  46.         //place all the bats!
  47.         for(int i=0; i<bats.length; ++i){
  48.             boolean weAreCool = true;
  49.             do{
  50.                 weAreCool = true;
  51.            
  52.                 bats[i] = generator.nextInt(20);
  53.                
  54.                 if(bats[i] == currentLocation || bats[i] == wumpusLocation){
  55.                     weAreCool = false;
  56.                 }
  57.                
  58.                 for(int j=0; j<bats.length; ++j){
  59.                     if(i==j){
  60.                         continue;
  61.                     }
  62.                     if(bats[i] == bats[j]){
  63.                         weAreCool = false;
  64.                     }
  65.                 }
  66.                
  67.             }while(!weAreCool);
  68.         }
  69.        
  70.         holes           = new int[2];
  71.        
  72.         //place all the holes!
  73.         for(int i=0; i<holes.length; ++i){
  74.             boolean weAreCool = true;
  75.             do{
  76.                 weAreCool = true;
  77.            
  78.                 holes[i] = generator.nextInt(20);
  79.                
  80.                 if(holes[i] == currentLocation || holes[i] == wumpusLocation){
  81.                     weAreCool = false;
  82.                 }
  83.                
  84.                 for(int j=0; j<holes.length; ++j){
  85.                     if(holes[i] == bats[j]){
  86.                         weAreCool = false;
  87.                     }
  88.                
  89.                     if(i==j){
  90.                         continue;
  91.                     }
  92.                     if(holes[i] == holes[j]){
  93.                         weAreCool = false;
  94.                     }
  95.                 }
  96.                
  97.             }while(!weAreCool);
  98.         }
  99.        
  100.        
  101.        
  102.         showLocation();
  103.        
  104.     }
  105.    
  106.    
  107.     private boolean run(){
  108.        
  109.         return runPrompt();
  110.        
  111.     }
  112.    
  113.    
  114.     private boolean runPrompt(){
  115.         String command = "nil";
  116.    
  117.         System.out.print(">");
  118.         try{
  119.             command = reader.readLine();
  120.         }
  121.         catch(IOException e)
  122.         {
  123.             e.printStackTrace();
  124.         }
  125.        
  126.         command.toLowerCase();
  127.        
  128.        
  129.         if(command.compareTo("quit") == 0){
  130.             return false;
  131.         }else if(command.compareTo("map") == 0){
  132.             printMap();
  133.         }else if(command.compareTo("look") == 0){
  134.             showLocation();
  135.         }else if(command.compareTo("reset") == 0){
  136.             System.out.println("\nZresetowales gre xD");
  137.             init();
  138.         }else if(command.compareTo("halp") == 0){
  139.             printHalp();
  140.         }else{
  141.        
  142.             String[] cmds = command.split(" ");
  143.             if(cmds.length == 2){
  144.                 if(cmds[0].compareTo("go") == 0){
  145.                     int loc = 9000;
  146.                     try{
  147.                         loc = Integer.parseInt(cmds[1]);
  148.                     }
  149.                     catch(NumberFormatException e){
  150.                         //System.out.println("ale musisz podac numer lokacji seba xD");
  151.                         loc = 9000;
  152.                     }
  153.                    
  154.                     if(tryGo(loc)){
  155.                         steps ++;
  156.                         showLocation();
  157.                         runWumpus();
  158.                     }else{
  159.                         System.out.println("nie mozesz tam isc ;_;");
  160.                     }
  161.                 }else if(cmds[0].compareTo("shoot") == 0){
  162.                     int loc = 9000;
  163.                     try{
  164.                         loc = Integer.parseInt(cmds[1]);
  165.                     }
  166.                     catch(NumberFormatException e){
  167.                         loc = 9000;
  168.                     }
  169.                    
  170.                     if(loc!=9000){
  171.                         shoot(loc);
  172.                         runWumpus();
  173.                     }
  174.                 }
  175.             }else{
  176.                 System.out.println("nie rozumiem ;_;");
  177.             }
  178.         }
  179.            
  180.         return true;
  181.        
  182.     }
  183.    
  184.    
  185.     private boolean tryGo(int location){
  186.         for(int i=0; i<3; ++i){
  187.             if(locationExits[currentLocation][i] == location){
  188.                 currentLocation = location;
  189.                 return true;
  190.             }
  191.         }
  192.        
  193.         return false;
  194.     }
  195.    
  196.    
  197.     private void showLocation(){
  198.         System.out.println(" ");
  199.         System.out.println("Jestes tutaj: " + currentLocation);
  200.         System.out.println("A takie so wyjscia: " + locationExits[currentLocation][0] + " " + locationExits[currentLocation][1] + " " + locationExits[currentLocation][2]);
  201.        
  202.         checkHazards();
  203.     }
  204.    
  205.    
  206.     private void shoot(int location){
  207.         for(int i=0; i<3; ++i){
  208.             if(locationExits[currentLocation][i] == location){
  209.                 if(wumpusLocation == location){
  210.                     System.out.println("\n\nTYLE WYGRAC! Zabiles Erise!");
  211.                     System.out.println("Dokonales tego w " + steps + " krokach.");
  212.                     System.out.println("Twoja wioska bedzie miala co jesc przez nastepny rok!");
  213.                     System.out.println("Nie wiem czo zrobic wiec resetuje dla ciebie gre xD");
  214.                     init();
  215.                     return;
  216.                 }else{
  217.                     System.out.println("Harpun w nic nie trafil!");
  218.                     System.out.println("Slyszysz ze cos wielkiego zaczyna spacerowac po komnatach...");
  219.                     wumpusAwake = true;
  220.                     return;
  221.                 }
  222.             }
  223.         }
  224.        
  225.         System.out.println("Nie mozesz tam strzelic co.");
  226.     }
  227.    
  228.    
  229.     private void checkHazards(){
  230.         //tu gdzie jestesmy
  231.         if(wumpusLocation == currentLocation){
  232.             System.out.println("\n\nOM NOM NOM!! Tyle przegrac. Erise cie zjadl xD");
  233.             System.out.println("Zaczynasz gre od poczatku xD");
  234.             init();
  235.         }
  236.        
  237.         for(int i=0; i<bats.length; ++i){
  238.             if(bats[i] == currentLocation){
  239.                 System.out.println("Trafiles na ZUBATY!");
  240.                 System.out.println("Zabieraja cie do innej lokacji. Tak bardzo losowo. xD");
  241.                 boolean weAreCool   = true;
  242.                 int     newPosition = 0;
  243.                 do{
  244.                     weAreCool   = true;
  245.                     newPosition = generator.nextInt(20);
  246.                    
  247.                     if(newPosition == currentLocation || newPosition == wumpusLocation){
  248.                         weAreCool = false;
  249.                     }
  250.                    
  251.                     for(int j=0; j<holes.length; ++j){
  252.                         if(newPosition == bats[j]){
  253.                             weAreCool = false;
  254.                         }
  255.                    
  256.                         if(newPosition== holes[j]){
  257.                             weAreCool = false;
  258.                         }
  259.                     }
  260.                    
  261.                 }while(!weAreCool);
  262.                
  263.                 currentLocation = newPosition;
  264.                
  265.                 showLocation();
  266.                
  267.             }
  268.         }
  269.        
  270.         for(int i=0; i<holes.length; ++i){
  271.             if(holes[i] == currentLocation){
  272.                 System.out.println("AAYYYYIIEEEEE!");
  273.                 System.out.println("Wpadles do dziury wykopanej przez jakiegos trola.");
  274.                 System.out.println("Przegrywasz w zycie, a ja resetuje ci gre.");
  275.                 init();
  276.             }
  277.         }
  278.        
  279.        
  280.         //wyjscia
  281.         for(int i=0; i<3; ++i){
  282.             if(wumpusLocation == locationExits[currentLocation][i]){
  283.                 System.out.println("Czujesz zapach Erise.");
  284.             }
  285.            
  286.             for(int j=0; j<bats.length; ++j){
  287.                 if(bats[j] == locationExits[currentLocation][i]){
  288.                     System.out.println("Slyszysz trzepot skrzydel.");
  289.                 }
  290.             }
  291.            
  292.             for(int j=0; j<holes.length; ++j){
  293.                 if(holes[j] == locationExits[currentLocation][i]){
  294.                     System.out.println("Twoj pajeczy zmysl dygocze.");
  295.                 }
  296.             }
  297.         }
  298.     }
  299.    
  300.    
  301.    
  302.     private void runWumpus(){
  303.         if(!wumpusAwake){
  304.             return;
  305.         }
  306.        
  307.         int randomId = generator.nextInt(3);
  308.         wumpusLocation = locationExits[wumpusLocation][randomId];
  309.         System.out.println("!THUMB!");
  310.     }
  311.    
  312.    
  313.     private void generateBurrow(){
  314.    
  315.         boolean tryAgain = false;
  316.    
  317.         //najpierw reset wszystkich lokacji
  318.         for(int i=0; i<20; ++i){
  319.             for(int j=0; j<3; ++j){
  320.                 locationExits[i][j] = -1;
  321.             }
  322.         }
  323.        
  324.         //ustawianie przejsc pomiedzy lokacjami
  325.         for(int i=0; i<20; ++i){
  326.             int currentExitIndex = 0;
  327.            
  328.             //sprawdzamy czy inne lokacje nie kieruja nas juz tutaj
  329.             for(int j=0; j<i; ++j){
  330.            
  331.                 for(int k=0; k<3; ++k){
  332.                     if(locationExits[j][k] == i){
  333.                         locationExits[i][currentExitIndex] = j;
  334.                         currentExitIndex ++;
  335.                     }
  336.                 }
  337.             }
  338.            
  339.             //i jesli zostaly nam jakies wolne miejsca, to je teraz wylosujemy xD
  340.             for(int j=currentExitIndex; j<3; ++j){
  341.                 int     randomLocation  = -1;
  342.                 boolean goodToGo        = true;
  343.                 int     depth           = 0;
  344.                
  345.                
  346.                 do{
  347.                     goodToGo            = true;
  348.                     int range           = 20 - i;
  349.                     locationExits[i][j] = i + generator.nextInt(range);
  350.                    
  351.                     if(locationExits[i][j] == i){
  352.                         goodToGo = false;
  353.                     }
  354.                
  355.                     for(int k=0; k<j; ++k){
  356.                         if(locationExits[i][j] == locationExits[i][k]){
  357.                             //ale nie chcemy dwoch takich samych wyjsc z jednej lokacji seba xD
  358.                             goodToGo = false;
  359.                         }
  360.                     }
  361.                    
  362.                     int numOfCurrentExits = 0;
  363.                     for(int k=0; k<i; ++k){
  364.                         for(int l=0; l<3; ++l){
  365.                             if(locationExits[i][j] == locationExits[k][l]){
  366.                                 numOfCurrentExits ++;
  367.                             }
  368.                         }
  369.                     }
  370.                    
  371.                     if(numOfCurrentExits>=3){
  372.                         goodToGo = false;
  373.                     }
  374.                    
  375.                     depth ++;
  376.                     if(depth > 100){
  377.                         locationExits[i][j] = -1;
  378.                         goodToGo    = true;
  379.                         tryAgain    = true;
  380.                     }
  381.                
  382.                 }while(!goodToGo);
  383.             }
  384.            
  385.            
  386.         }
  387.        
  388.         if(tryAgain){
  389.             //burrow generation failed (stupid random thingie) so we will try again
  390.             generateBurrow();
  391.         }
  392.     }
  393.    
  394.    
  395.     private void printMap(){
  396.         for(int i = 0; i < 20; ++i){
  397.             System.out.println("Location[" + i + "] = " + locationExits[i][0] + " " + locationExits[i][1] + " " + locationExits[i][2]);
  398.         }
  399.        
  400.         /*
  401.         System.out.println("------------");
  402.        
  403.         for(int i = 0; i < 20; ++i){
  404.             int wyst = 0;
  405.            
  406.             for(int j = 0; j < 20; j++){
  407.                 for(int k = 0; k < 3; ++k){
  408.                     if(locationExits[j][k] == i){
  409.                         wyst++;
  410.                     }
  411.                 }
  412.             }
  413.            
  414.             System.out.print("Location[" + i + "] = ");
  415.             System.out.println(wyst);
  416.         }
  417.         */
  418.     }
  419.    
  420.    
  421.     public void printHalp(){
  422.         System.out.println("\nINSTRUKCJE DLA NOWOGRACZY");
  423.         System.out.println("Witaj, oto nora, w ktorej zyje Erise. Musisz go zabic.");
  424.         System.out.println("W tym celu masz nieskonczony wor magicznych harpunow.");
  425.         System.out.println("\nAby przechodzic miedzy lokacjami pisz \"go numer_lokacji\".");
  426.         System.out.println("Mozesz rzucac harpunami do sasiednich lokacji \"shoot numer_lokacji\"");
  427.         System.out.println("\nErise spi w jednej z komnat. Budza go tylko harpuny i ludzie wchodzacy");
  428.         System.out.println("do jego lokacji. Jak sie obudzi zaczyna spacerowac i zjada wszystko co zobaczy.");
  429.         System.out.println("\nW komnatach sa tez dziury, w ktore mozna wpasc i zubaty, ktore przenosza");
  430.         System.out.println("do losowych komnat jesli sie na nie trafi. Gra zakomunikuje odpowiednio,");
  431.         System.out.println("jesli jedna z tych rzeczy bedzie w ktorejs z sasiednich lokacji. To samo");
  432.         System.out.println("tyczy sie Erise. Sam Erise jest za gruby zeby wpasc do dziury (nie miesci sie)");
  433.         System.out.println("i za ciezki zeby zubaty mogly go uniesc.");
  434.         System.out.println("\nInne komendy to: look, map, halp, reset, guit");
  435.         System.out.println("Powodzenia xD");
  436.     }
  437.    
  438.    
  439.     public static void main(String args[]){
  440.         wumpus Game = new wumpus();
  441.         //Game.printHalp();
  442.        
  443.         System.out.println("\n\n------------------\nERISE HUNT v1.0");
  444.         System.out.println("by Anonykuc\n------------------");
  445.         System.out.println("Wpisz \"halp\" jesli ten rowerek jest ci obcy.");
  446.        
  447.         Game.init();
  448.         while(Game.run());
  449.     }
  450.    
  451.    
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement