Facebook
From Trivial Pudu, 6 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 263
  1. package Model;
  2.  
  3. import java.util.Collections;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6.  
  7. /**
  8.  * Created by Jędrzej Hodor on 01.01.2018.
  9.  */
  10.  
  11. public class Supervisor {
  12.  
  13.     private List<Card> hip;
  14.     private List<Player> players;
  15.     private Integer players_quant; //powinno być ustawione 1 mniej niż w rzeczyiwtsości, zeby te zabawy z modulo działały
  16.  
  17.     private Integer whose_move;
  18.     private Colour given_colour;
  19.     private Type given_type;
  20.     private Integer red = 0;
  21.     private Integer orc = 0;
  22.     private Integer green = 0;
  23.     private Integer demand = 0;
  24.     private Integer duel = 0;
  25.  
  26.     private Card demanded_card;
  27.     private Integer player_who_has_demanded;
  28.     private Card duel_card;
  29.  
  30. // setting
  31.  
  32.     public Supervisor(Integer whose_move, Colour given_colour, Type given_type, Integer players_quant, Integer red, Integer orc, Integer green, Integer demand, Card demanded_card, Integer player_who_has_demanded, Integer duel, Card duel_card) {
  33.         this.whose_move = whose_move;
  34.         this.given_colour = given_colour;
  35.         this.given_type = given_type;
  36.         this.players_quant = players_quant;
  37.         this.red = red;
  38.         this.orc = orc;
  39.         this.green = green;
  40.         this.demand = demand;
  41.         this.demanded_card = demanded_card;
  42.         this.player_who_has_demanded = player_who_has_demanded;
  43.         this.duel = duel;
  44.         this.duel_card = duel_card;
  45.     }
  46.  
  47.     public Integer getWhoseMove() {
  48.         return whose_move;
  49.     }
  50.  
  51.     public void setWhoseMove(Integer whose_move) {
  52.         this.whose_move = whose_move;
  53.     }
  54.  
  55.     public Integer getPlayersQuant() {
  56.         return players_quant;
  57.     }
  58.  
  59.     public void setPlayersQuant(Integer players_quant) {
  60.         this.players_quant = players_quant;
  61.     }
  62.  
  63.     public void setDeck() {
  64.         //trzeba by pewnie wstawić karty jakoś
  65.     }
  66.  
  67.     public void setPlayers() {
  68.         //a tu graczy
  69.     }
  70.  
  71. // game
  72.  
  73.     public void shuffleDeck() {
  74.         Collections.shuffle(hip);
  75.     }
  76.  
  77.     public void gameBegin() {
  78.         this.whose_move = 0;
  79.         for (Integer i = 0; i < players_quant; i++) {
  80.             Player player = players.get(i);
  81.             this.draw(5, player);
  82.         }
  83.     }
  84.  
  85.     public Integer game() {
  86.         this.gameBegin();
  87.         Boolean no_winner = true;
  88.         Player winner = null;
  89.         while (no_winner) {
  90.             if (!this.playTurn()) {
  91.                 this.nextTurn();
  92.                 //Jakiś komunikat, że źle i spróbuj jeszcze raz. Myślę, że należy zakładać znajomość zasad.
  93.                 //Komunikaty czemu źle to można dopisać kiedyś ewentualnie.
  94.             }
  95.             //Funkcja, która sprawdza, czy gracz, który grał ma jeszcze karty, jeśli nie to przypisuje go do winnera.
  96.         }
  97.         return winner.getNumber();
  98.     }
  99.  
  100.     public Boolean playTurn() {
  101.         Player player = players.get(this.whose_move);
  102.         Integer type = player.whatMove();
  103.         if (this.checkIfForced()) {
  104.             return this.forcedMove(player);
  105.         } else {
  106.             if (type == 1) {
  107.                 return this.ordinaryMove(player);
  108.             } else if (type == 2) {
  109.                 return this.multipleMove(player);
  110.             } else if (type == 3) {
  111.                 this.draw(1, player);
  112.                 return true;
  113.             }
  114.         }
  115.         return false;
  116.         //W każdym segmencie powinien być return tzn powinno sprawdzać, czy udało się succesful ruch, czy nie
  117.     }
  118.  
  119.     public void checkIfTheHipEmpty() {
  120.         //jak sama nazwa wskazuje
  121.     }
  122.  
  123.     public void newCardOnTheHip(Card card) {
  124.         this.given_type = card.getType();
  125.         this.given_colour = card.getColour();
  126.     }
  127.  
  128.     public void nextTurn() {
  129.         this.whose_move += 1;
  130.         this.whose_move = this.whose_move % this.players_quant;
  131.     }
  132.  
  133. // moves
  134.  
  135.     public void draw(Integer quantity, Player player) {
  136.         for (Integer i = 0; i < quantity; i++) {
  137.             player.draw(hip.get(0));
  138.             hip.remove(0);
  139.         }
  140.     }
  141.  
  142.     public Boolean checkIfForced() {
  143.         if (this.red == 0 && this.orc == 0 && this.green == 0 && this.demand == 0) {
  144.             return false;
  145.         } else {
  146.             return true;
  147.         }
  148.     }
  149.  
  150.     public Boolean forcedMove(Player player) {
  151.         //zakładamy, że nie zdaży się opcja dwóch rodzajów punktów niezerowych (nie powinna xd)
  152.  
  153.         Integer what_move = player.whatForcedMove();
  154.         Integer what_kind = null;
  155.         if (what_move == 2) {
  156.             what_kind = player.whatKindOfForcedMove();
  157.         }
  158.  
  159.         if (this.red != 0) {
  160.             if (what_move == 1) {
  161.                 draw(this.red, player);
  162.                 this.resetRed();
  163.                 return true;
  164.             } else if (what_move == 2) {
  165.                 if (what_kind == 1) {
  166.                     return this.ordinaryDemandedFunction(player, Function.Red);
  167.                 } else if (what_kind == 2) {
  168.                     return this.multipleDemandedFunction(player, Function.Red);
  169.                 }
  170.             } else if (what_move == 3) {
  171.                 if (what_kind == 1) {
  172.                     return this.ordinaryDemandedFunction(player, Function.All);
  173.                 } else if (what_kind == 2) {
  174.                     return this.multipleDemandedFunction(player, Function.All);
  175.                 }
  176.             }
  177.         } else if (this.orc != 0) {
  178.             if (what_move == 1) {
  179.                 draw(this.orc, player);
  180.                 this.resetOrc();
  181.                 return true;
  182.             } else if (what_move == 2) {
  183.                 if (what_kind == 1) {
  184.                     return this.ordinaryDemandedFunction(player, Function.Orc);
  185.                 } else if (what_kind == 2) {
  186.                     return this.multipleDemandedFunction(player, Function.Orc);
  187.                 }
  188.             }
  189.         } else if (this.green != 0) {
  190.             if (what_move == 1) {
  191.                 // Funkcja tracenia kolejek.
  192.                 this.resetGreen();
  193.                 return true;
  194.             } else if (what_move == 2) {
  195.                 if (what_kind == 1) {
  196.                     return this.ordinaryDemandedFunction(player, Function.Stp);
  197.                 } else if (what_kind == 2) {
  198.                     return this.multipleDemandedFunction(player, Function.Stp);
  199.                 }
  200.             }
  201.         } else if (this.demand != 0) {
  202.             if (what_move == 1) {
  203.                 draw(1, player);
  204.                 return true;
  205.             } else if (what_move == 2) {
  206.                 if (what_kind == 1) {
  207.                     return this.ordinaryDemandedFunction(player, Function.Dem);
  208.                 } else if (what_kind == 2) {
  209.                     return this.multipleDemandedFunction(player, Function.Dem);
  210.                 }
  211.             } else if (what_move == 3) {
  212.                 if (what_kind == 1) {
  213.                     return this.ordinaryDemandedType(player, demanded_card.getType());
  214.                 } else if (what_kind == 2) {
  215.                     return this.multipleDemandedType(player, demanded_card.getType());
  216.                 }
  217.             }
  218.  
  219.             if (whose_move == player_who_has_demanded) {
  220.                 this.resetDemand();
  221.             }
  222.         }
  223.         return false;
  224.     }
  225.  
  226.     public Boolean duelMove(Player player, Function function) {
  227.         Integer what_move = player.whatDuelMove();
  228.  
  229.         if (what_move == 1) {
  230.             player.setIfFolded(true);
  231.             return true;
  232.         } else if (what_move == 2) {
  233.             Card card = player.ordinaryMove();
  234.             Function f = card.getFunction();
  235.             if (f == Function.Red) {
  236.                 this.duel += 2;
  237.                 this.duel_card = card;
  238.                 return true;
  239.             } else if (f == function) {
  240.                 this.duel += 1;
  241.                 this.duel_card = card;
  242.                 return true;
  243.             } else {
  244.                 return false;
  245.             }
  246.         } else if (what_move == 3) {
  247.             if (duel_card.getFunction() == Function.Red) {
  248.                 Card card = player.ordinaryMove();
  249.                 Function f = card.getFunction();
  250.                 if (f == Function.All || f == Function.Cyr) {
  251.                     this.resetDuel();
  252.                     this.duel_card = card;
  253.                 } else {
  254.                     return false;
  255.                 }
  256.             } else {
  257.                 return false;
  258.             }
  259.         }
  260.         return false;
  261.     }
  262.  
  263.     // ordinary
  264.  
  265.     public Boolean checkIfOrdinaryAllowed(Card card) {
  266.         return (card.getColour() == given_colour || card.getType() == given_type
  267.                 || card.getColour() == Colour.ALL || card.getType() == Type.all);
  268.     }
  269.  
  270.     public Boolean ordinaryMove(Player player) {
  271.         Card pl_card = player.ordinaryMove();
  272.         if (this.checkIfOrdinaryAllowed(pl_card)) {
  273.             player.playOneCard(pl_card);
  274.             this.newCardOnTheHip(pl_card);
  275.             // Wykonanie funkcji.
  276.             return true;
  277.         } else {
  278.             return false;
  279.         }
  280.     }
  281.  
  282.     public Boolean checkDemandedFunction(Card card, Function function) {
  283.         return (card.getFunction() == function);
  284.     }
  285.  
  286.     public Boolean ordinaryDemandedFunction(Player player, Function function) {
  287.         Card pl_card = player.ordinaryMove();
  288.         if (this.checkDemandedFunction(pl_card, function)) {
  289.             player.playOneCard(pl_card);
  290.             this.newCardOnTheHip(pl_card);
  291.             // Wykonanie funkcji.
  292.             return true;
  293.         } else {
  294.             return false;
  295.         }
  296.     }
  297.  
  298.     public Boolean checkDemandedType(Card card, Type type) {
  299.         return (card.getType() == type);
  300.     }
  301.  
  302.     public Boolean ordinaryDemandedType(Player player, Type type) {
  303.         Card pl_card = player.ordinaryMove();
  304.         if (this.checkDemandedType(pl_card, type)) {
  305.             player.playOneCard(pl_card);
  306.             this.newCardOnTheHip(pl_card);
  307.             // Wykonanie funkcji.
  308.             return true;
  309.         } else {
  310.             return false;
  311.         }
  312.     }
  313.  
  314.     //multiple
  315.  
  316.     public Boolean multipleMove(Player player) {
  317.         Card pl_card = player.ordinaryMove();
  318.         Integer how_many;
  319.         if (this.checkIfOrdinaryAllowed(pl_card)) {
  320.             player.playOneCard(pl_card);
  321.             how_many = player.multipleMove();
  322.             if (how_many == 2) {
  323.                 Card extra_card = player.getThirdCardToThePair();
  324.                 player.playOneCard(extra_card);
  325.             } else if (how_many == 3) {
  326.                 // Play Polak.
  327.             } else if (how_many == 4) {
  328.                 // Play Kazuar.
  329.             } else if (how_many == 5) {
  330.                 // cośtam
  331.             }
  332.             player.playFewCards(how_many, pl_card);
  333.             this.newCardOnTheHip(pl_card);
  334.             // Wykonanie funkcji odp ilość razy.
  335.             return true;
  336.         } else {
  337.             return false;
  338.         }
  339.     }
  340.  
  341.     public Boolean multipleDemandedFunction(Player player, Function function) {
  342.         Card pl_card = player.ordinaryMove();
  343.         Integer how_many;
  344.         if (this.checkDemandedFunction(pl_card, function)) {
  345.             player.playOneCard(pl_card);
  346.             how_many = player.multipleMove();
  347.             if (how_many == 2) {
  348.                 Card extra_card = player.getThirdCardToThePair();
  349.                 player.playOneCard(extra_card);
  350.             } else if (how_many == 3) {
  351.                 // Play Polak.
  352.             } else if (how_many == 4) {
  353.                 // Play Kazuar.
  354.             } else if (how_many == 5) {
  355.                 // cośtam
  356.             }
  357.             player.playFewCards(how_many, pl_card);
  358.             this.newCardOnTheHip(pl_card);
  359.             // Wykonanie funkcji odp ilość razy
  360.             return true;
  361.         } else {
  362.             return false;
  363.         }
  364.     }
  365.  
  366.     public Boolean multipleDemandedType(Player player, Type type) {
  367.         Card pl_card = player.ordinaryMove();
  368.         Integer how_many;
  369.         if (this.checkDemandedType(pl_card, type)) {
  370.             player.playOneCard(pl_card);
  371.             how_many = player.multipleMove();
  372.             if (how_many == 2) {
  373.                 Card extra_card = player.getThirdCardToThePair();
  374.                 player.playOneCard(extra_card);
  375.             } else if (how_many == 3) {
  376.                 // Play Polak.
  377.             } else if (how_many == 4) {
  378.                 // Play Kazuar.
  379.             } else if (how_many == 5) {
  380.                 // cośtam
  381.             }
  382.             player.playFewCards(how_many, pl_card);
  383.             this.newCardOnTheHip(pl_card);
  384.             // Wykonanie funkcji odp ilość razy.
  385.             return true;
  386.         } else {
  387.             return false;
  388.         }
  389.     }
  390.  
  391.  
  392. // special
  393.  
  394.     public void waranTransposition(Player player1, Player player2) {
  395.         List<Card> aux = new LinkedList<>();
  396.  
  397.         for (Integer i = 0; i < player2.getQuant_of_cards(); i++) {
  398.             aux.add(player2.showACard(0));
  399.             player2.playOneCard(player2.showACard(0));
  400.         }
  401.         for (Integer i = 0; i < player1.getQuant_of_cards(); i++) {
  402.             player2.draw(player1.showACard(0));
  403.             player1.playOneCard(player1.showACard(0));
  404.         }
  405.         for (Integer i = 0; i < player2.getQuant_of_cards(); i++) {
  406.             player1.draw(aux.get(i));
  407.         }
  408.     }
  409.  
  410.     public void waranPermutation(Player master, Player giver, Player receiver) {
  411.         Boolean stop = true;
  412.         while (stop) {
  413.             //pytamy gracza master, czy chce wykonać transpozycje
  414.             if (stop) {
  415.                 //pytamy jakich graczy, on nam wpisuje, że player1 i player2.
  416.                 Player player1 = null, player2 = null;
  417.                 this.waranTransposition(player1, player2);
  418.             }
  419.         }
  420.     }
  421.  
  422.     public void giveCards(Player giver, Player receiver, Integer quant_given) {
  423.         giver.shuffleHand();
  424.         for (Integer i = 0; i < quant_given; i++) {
  425.             receiver.draw(giver.showACard(0));
  426.             giver.playOneCard(giver.showACard(0));
  427.         }
  428.     }
  429.  
  430.     public void addRed(Integer how_many) {
  431.         this.red += how_many;
  432.     }
  433.  
  434.     public void addOrc(Integer how_many) {
  435.         this.orc += how_many;
  436.     }
  437.  
  438.     public void addGreen(Integer how_many) {
  439.         this.green += how_many;
  440.     }
  441.  
  442.     public void addDemand(Integer how_many) {
  443.         this.demand += how_many;
  444.     }
  445.  
  446.     public void resetRed() {
  447.         this.red = 0;
  448.     }
  449.  
  450.     public void resetOrc() {
  451.         this.orc = 0;
  452.     }
  453.  
  454.     public void resetGreen() {
  455.         this.green = 0;
  456.     }
  457.  
  458.     public void resetDemand() {
  459.         this.demand = 0;
  460.     }
  461.  
  462.     // duel
  463.  
  464.     public void addDuel(Integer how_many) {
  465.         this.duel += how_many;
  466.     }
  467.  
  468.     public void resetDuel() {
  469.         this.duel = 0;
  470.         for (Integer i = 0; i < players_quant; i++) {
  471.             Player player = players.get(i);
  472.             player.setIfFolded(false);
  473.         }
  474.     }
  475.  
  476.     public Boolean checkIfWinner() {
  477.         Boolean stop = false;
  478.         for (Integer i = 0; i < players_quant; i++) {
  479.             Player player = players.get(i);
  480.             if (!player.getIfFolded() && !stop) {
  481.                 stop = true;
  482.             } else if (!player.getIfFolded() && stop) {
  483.                 return false;
  484.             }
  485.         }
  486.         return true;
  487.     }
  488.  
  489.     public Player findWinner() {
  490.         for (Integer i = 0; i < players_quant; i++) {
  491.             Player player = players.get(i);
  492.             if (!player.getIfFolded()) {
  493.                 return player;
  494.             }
  495.         }
  496.         return null;
  497.     }
  498.  
  499.     public Integer duel(Function function, Player triggering_player) {
  500.         Integer index = triggering_player.getNumber();
  501.         Boolean end = false;
  502.         Player winner = null;
  503.         Boolean if_winner = false;
  504.  
  505.         while (!end) {
  506.             Player current_player = players.get(index);
  507.             this.duelMove(current_player, function);
  508.  
  509.             if (this.duel == 0) {
  510.                 end = true;
  511.             } else if (this.checkIfWinner()) {
  512.                 end = true;
  513.                 winner = this.findWinner();
  514.                 if_winner = true;
  515.             }
  516.  
  517.             index = (index + 1) % players_quant;
  518.         }
  519.         this.whose_move = (index + 1) % players_quant;
  520.         this.resetDuel();
  521.         this.newCardOnTheHip(this.duel_card);
  522.  
  523.         if (if_winner) {
  524.             return winner.getNumber();
  525.         } else {
  526.             return -1;
  527.         }
  528.     } // Zwraca winnera, jeśli został przerwany to -1
  529.  
  530. }
  531.