Advertisement
MagnusArias

PO2 | Gra

Mar 9th, 2016
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3. import java.util.InputMismatchException;
  4.  
  5. class ProgsRun{
  6.    
  7.     public static boolean czyGrac(int c){
  8.         Scanner czyt = new Scanner(System.in);
  9.         String wybor = "";
  10.        
  11.         System.out.println("Doskonale, trafiles!\nTwoja liczba prob = " + c );
  12.         System.out.println("Chcesz sprobowac ponownie?[Y/N]");
  13.        
  14.         wybor = czyt.next(); // sprawdznie po zakonczeniu. czy gramy dalej
  15.  
  16.         if ( wybor.equals("n") || wybor.equals("N") ) {
  17.             System.out.println("Zakonczono");
  18.             return false;
  19.         }
  20.         else if ( wybor.equals("y") || wybor.equals("Y") ){
  21.             return true;
  22.         }
  23.         else {
  24.             System.out.println("Prosilem o Y lub N, a nie cos innego.");
  25.             return false;
  26.         }
  27.     }
  28.    
  29.    
  30.     public static boolean sprawdzLiczbe(int wynik, int rand){
  31.         if (wynik > rand) {
  32.             System.out.println("Liczba za duza, Celuj dalej: ");
  33.             return false;
  34.         }
  35.         else if (wynik < rand) {
  36.             System.out.println("Liczba za mala, celuj dalej: ");
  37.             return false;
  38.         }
  39.         else {
  40.             return true;
  41.         }
  42.     }
  43.    
  44.     public static void playGame(String argsy[]){
  45.         try {
  46.             int max = Integer.parseInt(argsy[0]);
  47.            
  48.             Scanner czytaj = new Scanner(System.in);
  49.             Random generator = new Random();
  50.          
  51.             int wynik = 0;
  52.             int licznik = 0;
  53.             int liczba = generator.nextInt(max);                    //random
  54.            
  55.             System.out.println("Wylosowano liczbe od 0 do " + max);
  56.             System.out.println("Zgaduj liczbe: ");
  57.            
  58.             do {
  59.            
  60.                 try {
  61.                     wynik = czytaj.nextInt();
  62.                    
  63.                     if ( sprawdzLiczbe(wynik, liczba) ){
  64.                         licznik++;
  65.                        
  66.                         if ( czyGrac(licznik) ){
  67.                             System.out.println("Wylosowano liczbe od 0 do " + max);
  68.                             System.out.println("Zgaduj liczbe: ");
  69.                             licznik = 0;
  70.                             liczba = generator.nextInt(max);
  71.                         }
  72.                         else {
  73.                             System.exit(1);
  74.                         }
  75.                     }
  76.                     else{
  77.                         licznik++;
  78.                     }
  79.                 }
  80.                 catch (InputMismatchException f){
  81.                     System.out.println("Podany parametr nie jest liczba, probuj ponownie:");
  82.                     czytaj.next();
  83.                 }
  84.                
  85.             } while (true);
  86.         }
  87.         catch (NumberFormatException e){
  88.             System.out.println("Podany argument nie jest liczba, probuj ponownie");
  89.             System.exit(1);
  90.         }
  91.     }
  92.    
  93.    
  94.     public static void main(String[] args) {
  95.         if (args.length == 1){
  96.             playGame(args);    
  97.         }
  98.         else { System.out.println("\n\nPodano za malo lub za duzo argumentow"); }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement