pw_

Wheel of Fortune v 1.1

pw_
Oct 25th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.27 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Menu menu = new Menu();
  8.         Game game = new Game();
  9.         int option;
  10.         boolean x = false;
  11.  
  12.         do {
  13.             try {
  14.                 menu.printMenu();
  15.                 Scanner scanner = new Scanner(System.in);
  16.                 option = scanner.nextInt();
  17.                 switch (option) {
  18.                     case 1: //dodawanie graczy
  19.                         if (game.playerList.size() > 1) {  //sprawdzanie czy gracze sa juz dodani
  20.                             menu.clearScreen();
  21.                             System.out.println("Dodałeś już graczy.");
  22.                         } else {
  23.                             menu.clearScreen();
  24.                             for (int i = 0; i < 3; i++) {
  25.                                 System.out.println("Podaj imie gracza nr: " + (i + 1));
  26.                                 game.addPlayer(scanner.next());
  27.                             }
  28.                             menu.clearScreen();
  29.                             System.out.println("Gracze to:");
  30.                             for (int j = 0; j < 3; j++) {  //wyswietlanie graczy
  31.                                 game.showPlayers(j);
  32.                             }
  33.                         }
  34.                         break;
  35.                     case 2: //start
  36.                         boolean z = true;
  37.                         menu.clearScreen();
  38.                         if (game.playerList.size() < 1) {  //sprawdzanie czy gracze sa juz dodani
  39.                             System.out.println("Dodaj graczy!");
  40.                         }
  41.                         else {
  42.                             boolean y = true;
  43.                             while (y && z) {
  44.                                 try {
  45.                                     menu.printMenu2();
  46.                                     Scanner scanner1 = new Scanner(System.in);
  47.                                     int option1 = scanner1.nextInt();
  48.                                     switch (option1) {
  49.                                         case 1:
  50.                                             menu.clearScreen();
  51.                                             game.gra(); //własciwa gra
  52.                                             break;
  53.                                         case 2:
  54.                                             menu.clearScreen();
  55.                                             game.gra2(); //Kup Samogłoskę
  56.                                             break;
  57.                                         case 3:
  58.                                             game.guess(); //Odgaduje haslo
  59.                                             break;
  60.                                         case 0:
  61.                                             menu.clearScreen();
  62.                                             z = false;
  63.                                             break;
  64.                                         default:
  65.                                             menu.clearScreen();
  66.                                             System.out.println("Nie ma takiej opcji spróbuj jeszcze raz: ");
  67.                                     }
  68.                                 } catch (Exception e) {
  69.                                     System.out.println("Nie ma takie opcji. Spróbuj jeszcze raz.");
  70.                                 }
  71.                             } while (!y);
  72.                         }
  73.                         break;
  74.                     case 3:  //ranking
  75.                         menu.clearScreen();
  76.                         if (game.playerList.size() < 1) {  //sprawdzanie czy gracze sa juz dodani
  77.                             System.out.println("Dodaj graczy!");
  78.                         } else {
  79.                             for (int i = 0; i < game.playerList.size(); i++) {
  80.                                 System.out.println(game.playerList.get(i).getName() + " " + game.playerList.get(i).getPoints() + " złotych.");
  81.                             }
  82.                         }
  83.                         break;
  84.                     case 0:  //koniec
  85.                         menu.clearScreen();
  86.                         System.exit(0);
  87.                     default:
  88.                         menu.clearScreen();
  89.                         System.out.println("Nie ma takiej opcji spróbuj jeszcze raz.");
  90.                 }
  91.             } catch (Exception e) {
  92.                 System.out.println("Nie ma takie opcji. Spróbuj jeszcze raz.");
  93.             }
  94.         } while (!x);
  95.     }
  96.  
  97. }
  98.  
  99. class Players {
  100.  
  101.     private String name;
  102.     private int points;
  103.  
  104.     void setName(String x) {
  105.         name = x;
  106.     }
  107.     public String getName() {
  108.         return name;
  109.     }
  110.     public int getPoints() {
  111.         return points;
  112.     }
  113.     void addPoints(int a) {
  114.         points = points + a;
  115.     }
  116.     void clearPoints() {
  117.         points = 0;
  118.     }
  119.     void subPoints(int a) {
  120.         points = points - a;
  121.     }
  122. }
  123.  
  124. public class Menu {
  125.  
  126.     void printMenu() {
  127.         System.out.print("\n...:::KOŁO FORTUNY:::...\n1. Podaj graczy \n2. Start \n3. Ranking \n0. Koniec \n\nWybierz: ");
  128.     }
  129.     void printMenu2() {
  130.         System.out.print("\n...:::KOŁO FORTUNY:::...\n1. Zakręć Kołem \n2. Kup Samogłoskę \n3. Odgaduje Hasło \n0. Wróć \n\nWybierz: ");
  131.     }
  132.     public static void clearScreen() {
  133.         System.out.print("\033[H\033[2J");
  134.         System.out.flush();
  135.     }
  136. }
  137.  
  138. import java.util.ArrayList;
  139. import java.util.Scanner;
  140. import java.io.*;
  141.  
  142. public class Game{
  143.  
  144.     ///////////////////////////////////////////////////////////
  145.     Scanner scanner = new Scanner(System.in);
  146.     BufferedReader bufread = new BufferedReader(new InputStreamReader(System.in));
  147.     ArrayList<Players> playerList = new ArrayList<>();  //tworzenie listy graczy z klasy
  148.     Rand rand = new Rand();
  149.     Round round = new Round();
  150.     Menu menu = new Menu();
  151.     int randomPrice;
  152.     int randomCat = rand.randomCategoryNumber();
  153.     boolean flag = true;
  154.     boolean flag2 = true;
  155.     ///////////////////////////////////////////////////////////
  156.     String word1 = rand.randomCategory(randomCat);
  157.     String word2 = word1.replaceAll("[a-z]", "#");
  158.     String word4 = word2.replaceAll("[ą,ć,ę,ł,ń,ś,ó,ż,ź]", "#");
  159.     StringBuilder word3 = new StringBuilder(word4);
  160.     ///////////////////////////////////////////////////////////
  161.  
  162.  
  163.     public void addPlayer(String name){
  164.         Players player = new Players();
  165.         player.setName(name);
  166.         playerList.add(player);
  167.     }
  168.     public void showPlayers(int z){
  169.         System.out.println(playerList.get(z).getName() + " " + playerList.get(z).getPoints());
  170.     }
  171.     public boolean checkPuzzle(String check) {  //sprawdzanie poprawności podanego hasła
  172.         if (check.contains(word1)){
  173.             return true;
  174.         }
  175.         else {
  176.             return false;
  177.         }
  178.     }
  179.  
  180.     public boolean checkRandomPrice(int z) {  //sprawdzanie bankruta
  181.         if (z == 0){
  182.             System.out.println("BANKRUT!!!");
  183.             playerList.get(round.getPlayer()).clearPoints();
  184.             round.nextPlayer();
  185.             return false;
  186.         }
  187.         else {
  188.             return true;
  189.         }
  190.     }
  191.     public boolean checkPts(int points) { //sprawdzanie czy mozna kupic samogłoske
  192.         if (points >= 200) {
  193.             return true;
  194.         }
  195.         else {
  196.             System.out.println("Masz za mało pieniędzy.");
  197.             flag = false;
  198.             return false;
  199.         }
  200.     }
  201.     public boolean checkVowel(char vowel) {  //samogłoski
  202.         for(int j = 0; j < vowel ; j++)
  203.             switch(vowel) {
  204.                 case 'a':
  205.                 case 'e':
  206.                 case 'i':
  207.                 case 'o':
  208.                 case 'u':
  209.                 case 'ó':
  210.                     return true;
  211.             }
  212.         return false;
  213.     }
  214.     public boolean checkConsonant(char consonant) {  //spógłoski
  215.         for(int k = 0; k < consonant ; k++)
  216.             switch(consonant) {
  217.                 case 'ą':
  218.                 case 'b':
  219.                 case 'c':
  220.                 case 'ć':
  221.                 case 'd':
  222.                 case 'f':
  223.                 case 'g':
  224.                 case 'h':
  225.                 case 'j':
  226.                 case 'k':
  227.                 case 'l':
  228.                 case 'ł':
  229.                 case 'm':
  230.                 case 'n':
  231.                 case 'ń':
  232.                 case 'p':
  233.                 case 'r':
  234.                 case 's':
  235.                 case 'ś':
  236.                 case 't':
  237.                 case 'w':
  238.                 case 'y':
  239.                 case 'z':
  240.                 case 'ż':
  241.                 case 'ź':
  242.                     return true;
  243.             }
  244.         return false;
  245.     }
  246.     public void guess(){    //odgadywanie hasła
  247.         menu.clearScreen();
  248.         try {
  249.             System.out.print("Podaj Hasło: ");
  250.             String checkP = bufread.readLine();
  251.             if (checkPuzzle(checkP) == true) {
  252.                 System.out.println("Brawo! Wygrał(a): " + playerList.get(round.getPlayer()).getName() + " Hasło to: " + checkP.toUpperCase() + " wygrałeś " + playerList.get(round.getPlayer()).getPoints() + " złotych.");
  253.             } else {
  254.                 System.out.println("Podane hasło jest nieprawidłowe.");
  255.                 round.nextPlayer();
  256.             }
  257.         } catch (IOException ex){
  258.             ex.printStackTrace();
  259.         }
  260.     }
  261.  
  262.     public void gra() {  //odgadywanie spółgloski
  263.         flag = true;
  264.         menu.clearScreen();
  265.         round.checkPlayer(round.getPlayer());
  266.         randomPrice = rand.randomPrice();
  267.         while(flag) {
  268.             if (checkRandomPrice(randomPrice)) {
  269.                 System.out.println("Kręci: " + playerList.get(round.getPlayer()).getName());
  270.                 System.out.println("Wylosowałes kwote: " + randomPrice);
  271.                 System.out.println("Stan portfela: " + playerList.get(round.getPlayer()).getPoints());
  272.                 System.out.println("Kategoria: " + rand.categoryName(randomCat) + "\n" + word3.toString().toUpperCase());
  273.                 System.out.print("Podaj Spółgłoskę: ");
  274.                 puzzle(scanner.next().charAt(0));  //odkrywanie literek
  275.             }
  276.         }
  277.     }
  278.     public void puzzle(char litera) { //sprawdzanie społgłoski
  279.         if (checkConsonant(litera)) {
  280.             if(word1.indexOf(litera)!=-1){
  281.                 for (int i = 0; i < word1.length(); i++){
  282.                     if (litera == word1.charAt(i)){
  283.                         word3.setCharAt(i, word1.charAt(i));  //zastap index w haslo3
  284.                         playerList.get(round.getPlayer()).addPoints(randomPrice);
  285.                     }
  286.                 }
  287.             }
  288.             else {
  289.                 System.out.println("Hasło nie zawiera spółgłoski: " + litera);
  290.                 round.nextPlayer();
  291.                 randomPrice = 0;
  292.                 flag = !flag;
  293.             }
  294.             System.out.println("\nHasło: " + word3.toString().toUpperCase());
  295.             randomPrice = 0;
  296.             flag = !flag;
  297.         }
  298.         else {
  299.             System.out.println("Wprowadziłeś " + litera + " Musisz podać samogłoskę. Spróbuj jeszcze raz.");
  300.         }
  301.     }
  302.  
  303.     public void gra2(){  //kupowanie samogłoski
  304.         flag2 = true;
  305.         menu.clearScreen();
  306.         round.checkPlayer(round.getPlayer());
  307.         while (flag2) {
  308.             System.out.println("Kupuje: " + playerList.get(round.getPlayer()).getName());
  309.             System.out.println("Stan portfela: " + playerList.get(round.getPlayer()).getPoints());
  310.             if (checkPts(playerList.get(round.getPlayer()).getPoints())) {
  311.                 System.out.println("Kategoria: SPORT \nHasło: " + word3.toString().toUpperCase());
  312.                 System.out.print("Podaj Samogłoskę (Koszt 200zł): ");
  313.                 puzzle2(scanner.next().charAt(0));  //odkrywanie literek
  314.             }
  315.         }
  316.  
  317.     }
  318.     public void puzzle2(char litera) {  //sprawdzanie samogłoski
  319.         if (checkVowel(litera)) {
  320.             if(word1.indexOf(litera)!=-1){
  321.                 for (int i = 0; i < word1.length(); i++){
  322.                     if (litera == word1.charAt(i)){
  323.                         word3.setCharAt(i, word1.charAt(i));  //zastap index w haslo3
  324.                     }
  325.                 }
  326.                 playerList.get(round.getPlayer()).subPoints(200);
  327.             }
  328.             else {
  329.                 System.out.println("Hasło nie zawiera samogłoski: " + litera);
  330.                 round.nextPlayer(); //sprawdzic
  331.                 flag2 = !flag;
  332.             }
  333.             System.out.println("\nHasło: " + word3.toString().toUpperCase());
  334.             flag2 = !flag;
  335.         }
  336.         else {
  337.             System.out.println("Wprowadziłeś '" +  litera + "' Musisz podać samogłoskę. Spróbuj jeszcze raz");
  338.         }
  339.     }
  340.  
  341. }
  342.  
  343. import java.util.Random;
  344.  
  345. public class Rand {
  346.  
  347.     private int[] Price = {0, 50, 50, 50, 50, 50, 100, 100, 100, 100, 150, 150, 150, 200, 200, 250, 0, 250, 500, 750, 1000, 1500, 2000, 3000, 5000};
  348.     private String[] Sport = {"manchester united", "lekkoatletyka", "siatkówka", "cristiano ronaldo", "piłka nożna", "tomasz hajto", "skok o tyczce", "rzut oszczepem", "mistrzostwa świata w piłce noznej"};
  349.     private String[] Cities = {"warszawa", "wrocław", "rio de janeiro", "hong kong", "los angeles"};
  350.     private String[] Powiedzenia = {"kiełbasy do góry i golimy frajerów"};
  351.     private String[] Zawody = {"kierowca autobusu"};
  352.  
  353.     public int randomPrice() {
  354.         int index = new Random().nextInt(Price.length);
  355.         int x = (Price[index]);
  356.         return x;
  357.     }
  358.  
  359.     public static int randomCategoryNumber() {  //losowanie kategorii
  360.         int x = (int) (Math.random()*4);
  361.         return x;
  362.     }
  363.  
  364.     public String randomCategory(int z) {  //losowanie hasła
  365.         if (z == 0) {
  366.             int index = new Random().nextInt(Sport.length);
  367.             String x = (Sport[index]);
  368.             return x;
  369.         }
  370.         else if (z == 1) {
  371.             int index = new Random().nextInt(Cities.length);
  372.             String x = (Cities[index]);
  373.             return x;
  374.         }
  375.         else if (z == 2) {
  376.             int index = new Random().nextInt(Powiedzenia.length);
  377.             String x = (Powiedzenia[index]);
  378.             return x;
  379.         }
  380.         else {
  381.             int index = new Random().nextInt(Zawody.length);
  382.             String x = (Zawody[index]);
  383.             return x;
  384.         }
  385.     }
  386.  
  387.     public String categoryName(int z) {  //wybieranie kategorii
  388.         if (z == 0) {
  389.             String x = "Sport.";
  390.             return x;
  391.         }
  392.         else if (z == 1) {
  393.             String x = "Miasta";
  394.             return x;
  395.         }
  396.         else if (z == 2) {
  397.             String x = "Powiedzenia";
  398.             return x;
  399.         }
  400.         else {
  401.             String x = "Zawody";
  402.             return x;
  403.         }
  404.     }
  405.  
  406. }
  407.  
  408. public class Round {
  409.     private int whichPlayer = 0;
  410.  
  411.     /*
  412.     void setPlayer(int i) {
  413.         whichPlayer = i;
  414.     }
  415.     */
  416.     public int getPlayer() {
  417.         return whichPlayer;
  418.     }
  419.     void checkPlayer(int i) {
  420.         if (i > 2) {
  421.             whichPlayer = 0;
  422.         }
  423.     }
  424.     void nextPlayer() {
  425.         whichPlayer++;
  426.     }
  427. }
Advertisement
Add Comment
Please, Sign In to add comment