botgob

Untitled

Jan 26th, 2021
716
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import javax.swing.*;
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. import static java.lang.System.*;
  7.  
  8. /*
  9.  *  The Mexico dice game
  10.  *  See https://en.wikipedia.org/wiki/Mexico_(game)
  11.  *
  12.  */
  13. public class Mexico {
  14.  
  15.     public static void main(String[] args) {
  16.         new Mexico().program();
  17.     }
  18.  
  19.     final Random rand = new Random();
  20.     final Scanner sc = new Scanner(in);
  21.     final int maxRolls = 3;      // No player may exceed this
  22.     final int startAmount = 3;   // Money for a player. Select any
  23.     final int mexico = 1000;     // A value greater than any other
  24.  
  25.     void program() {
  26.         //test();            // <----------------- UNCOMMENT to test
  27.  
  28.         int pot = 0;         // What the winner will get
  29.         Player[] players;    // The players (array of Player objects)
  30.         Player current;      // Current player for round
  31.         Player leader;       // Player starting the round
  32.  
  33.         players = getPlayers();
  34.         current = getRandomPlayer(players);
  35.         leader = current;
  36.  
  37.         out.println("Mexico Game Started");
  38.         statusMsg(players);
  39.  
  40.         boolean lastPlayerDone = false;
  41.         while (players.length > 1) {   // Game over when only one player left
  42.  
  43.             // ----- In ----------
  44.             String cmd = getPlayerChoice(current);
  45.             if ("r".equals(cmd)) {
  46.  
  47.                     // --- Process ------
  48.                 if (current == leader && current.nRolls < maxRolls){
  49.                     current = rollDice(current);
  50.                     roundMsg(current);
  51.                 } else if (current.nRolls < leader.nRolls && current != leader){
  52.                     current  = rollDice(current);
  53.                     roundMsg(current);
  54.                 } else  {
  55.                     if (indexOf(players,leader) == 0 && indexOf(players, current) == players.length - 1){
  56.                         lastPlayerDone = true;
  57.                     } else if (indexOf(players, leader) - 1 == indexOf(players, current)) {
  58.                         lastPlayerDone = true;
  59.                     } else {
  60.                         current = next(players, current);
  61.                     }
  62.                 }
  63.                     // ---- Out --------
  64.  
  65.             } else if ("n".equals(cmd) && current.nRolls > 0) {
  66.                  // Process
  67.                 if (indexOf(players,leader) == 0 && indexOf(players, current) == players.length - 1){
  68.                     lastPlayerDone = true;
  69.                 } else if (indexOf(players, leader) - 1 == indexOf(players, current)) {
  70.                     lastPlayerDone = true;
  71.                 }
  72.                 current = next(players, current);
  73.             } else {
  74.                 out.println("?");
  75.             }
  76.  
  77.             if ( allRolled(players, leader) && lastPlayerDone) {
  78.                 // --- Process -----
  79.                 Player loser = getLoser(players);
  80.                 loser.amount--;
  81.                 pot++;
  82.                 if (loser.amount <= 0){
  83.                     current = next(players, loser);
  84.                     leader = next(players, loser);
  85.                     players = removeLoser(players,loser);
  86.                 } else {
  87.                     current = loser;
  88.                     leader = loser;
  89.                 }
  90.                 players = clearRoundResults(players);
  91.  
  92.                 lastPlayerDone = false;
  93.                 // ----- Out --------------------
  94.                 out.println("Round done " + loser.name + " lost!");
  95.                 out.println("Next to roll is " + current.name);
  96.  
  97.                 statusMsg(players);
  98.             }
  99.         }
  100.         out.println("Game Over, winner is " + players[0].name + ". Will get " + pot + " from pot");
  101.     }
  102.  
  103.  
  104.     // ---- Game logic methods --------------
  105.  
  106.     // TODO implement and test methods (one at the time)
  107.  
  108.  
  109.     int indexOf(Player[] players, Player player) {
  110.         for (int i = 0; i < players.length; i++) {
  111.             if (players[i] == player) {
  112.                 return i;
  113.             }
  114.         }
  115.         return -1;
  116.     }
  117.     // Gets the next player.
  118.     public Player next(Player[] players, Player player){
  119.         int temp = indexOf(players, player);
  120.         if (temp == players.length - 1) {
  121.             return players[0];
  122.         } else return players[temp + 1];
  123.     }
  124.     Player getRandomPlayer(Player[] players) {
  125.         return players[rand.nextInt(players.length)];
  126.     }
  127.  
  128.     // Gets score of player
  129.     int getScore(Player player){
  130.         int score;
  131.         if (player.fstDice > player.secDice){
  132.             score = player.fstDice * 10 + player.secDice;
  133.         } else if (player.fstDice == player.secDice) {
  134.             score = (player.fstDice * 10 + player.secDice) * 10;
  135.         } else {
  136.             score = player.secDice * 10 + player.fstDice;
  137.         }
  138.         if (score == 21) {
  139.             return mexico;
  140.         } else  {
  141.             return score;
  142.         }
  143.     }
  144.     // Gets the player with lowest score
  145.     Player getLoser(Player[] players){
  146.         Player tempPlayer = players[0];
  147.         for (int i = 0; i < players.length; i++){
  148.             if (getScore(tempPlayer) > getScore(players[i])){
  149.                 tempPlayer = players[i];
  150.             }
  151.         }
  152.         return tempPlayer;
  153.     }
  154.  
  155.     Player[] removeLoser(Player[] players ,Player player){
  156.         int index = indexOf(players, player);
  157.         Player[] newList = new Player[players.length - 1];
  158.         int temp = 0;
  159.         for (int i = 0; i < players.length; i++){
  160.             if (i == index) {
  161.                 continue;
  162.             } else {
  163.                 newList[temp] = players[i];
  164.                 temp++;
  165.             }
  166.         }
  167.         return newList;
  168.     }
  169.     Player[] clearRoundResults(Player[] players){
  170.         for (int i = 0; i < players.length; i++){
  171.             players[i].nRolls = 0;
  172.             players[i].secDice = 0;
  173.             players[i].fstDice = 0;
  174.         }
  175.         return players;
  176.     }
  177.     Player rollDice(Player player){
  178.         player.fstDice = rand.nextInt(6) + 1;
  179.         player.secDice = rand.nextInt(6) + 1;
  180.         player.nRolls++;
  181.         return player;
  182.     }
  183.  
  184.     boolean allRolled(Player[] players, Player leader){
  185.         for (int i = 0; i < players.length; i++){
  186.             if (players[i].nRolls == 0){
  187.                 return false;
  188.             }
  189.         }
  190.         return true;
  191.     }
  192.     // ---------- IO methods (nothing to do here) -----------------------
  193.  
  194.     Player[] getPlayers() {
  195.         // Ugly for now. If using a constructor this may
  196.         // be cleaned up.
  197.         Player[] players = new Player[3];
  198.         Player p1 = new Player();
  199.         p1.name = "Olle";
  200.         p1.amount = startAmount;
  201.         Player p2 = new Player();
  202.         p2.name = "Fia";
  203.         p2.amount = startAmount;
  204.         Player p3 = new Player();
  205.         p3.name = "Lisa";
  206.         p3.amount = startAmount;
  207.         players[0] = p1;
  208.         players[1] = p2;
  209.         players[2] = p3;
  210.         return players;
  211.     }
  212.  
  213.     void statusMsg(Player[] players) {
  214.         out.print("Status: ");
  215.         for (int i = 0; i < players.length; i++) {
  216.             out.print(players[i].name + " " + players[i].amount + " ");
  217.         }
  218.         out.println();
  219.     }
  220.  
  221.     void roundMsg(Player current) {
  222.         out.println(current.name + " got " +
  223.                 current.fstDice + " and " + current.secDice);
  224.     }
  225.  
  226.     String getPlayerChoice(Player player) {
  227.         out.print("Player is " + player.name + " > ");
  228.         return sc.nextLine();
  229.     }
  230.  
  231.     // Possibly useful utility during development
  232.     String toString(Player p){
  233.         return p.name + ", " + p.amount + ", " + p.fstDice + ", "
  234.                 + p.secDice + ", " + p.nRolls;
  235.     }
  236.  
  237.     // Class for a player
  238.     class Player {
  239.         String name;
  240.         int amount;   // Start amount (money)
  241.         int fstDice;  // Result of first dice
  242.         int secDice;  // Result of second dice
  243.         int nRolls;   // Current number of rolls
  244.     }
  245.  
  246.  
  247.  
  248.     /**************************************************
  249.      *  Testing
  250.      *
  251.      *  Test are logical expressions that should
  252.      *  evaluate to true (and then be written out)
  253.      *  No testing of IO methods
  254.      *  Uncomment in program() to run test (only)
  255.      ***************************************************/
  256.     void test() {
  257.         // A few hard coded player to use for test
  258.         // NOTE: Possible to debug tests from here, very efficient!
  259.         Player[] ps = {new Player(), new Player(), new Player()};
  260.         ps[0].fstDice = 2;
  261.         ps[0].secDice = 6;
  262.         ps[1].fstDice = 6;
  263.         ps[1].secDice = 5;
  264.         ps[2].fstDice = 1;
  265.         ps[2].secDice = 1;
  266.  
  267.         for (int i = 0; i < 100; i++){
  268.             out.println(rand.nextInt(6) + 1);
  269.         }
  270.  
  271.         out.println(getScore(ps[0]) == 62);
  272.         out.println(getScore(ps[1]) == 65);
  273.         out.println(next(ps, ps[0]) == ps[1]);
  274.         out.println(getLoser(ps) == ps[0]);
  275.  
  276.         exit(0);
  277.     }
  278.  
  279.  
  280. }
  281.  
RAW Paste Data