Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.71 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.concurrent.TimeUnit;
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  * Francesco Di Mise
  7.  * This is a program which executes a game of 21 sticks. The two options are to play a computer or for two people to play against each other.
  8.  * The game randomly chooses who goes first, and the person can take 1, 2 or 3 sticks on every turn.
  9.  * The program will not allow for a person to lose, so they cannot take more sticks than are left, and cannot take the last stick.
  10.  */
  11. public class TwantyWanStoccs {
  12.  
  13.     /**
  14.      * The main function displays the instructions and calls the doThis function which performs the rest of the program.
  15.      * This is mostly so that if the player decides to the replay the game they will not have to wait through the instructions again
  16.      * @param args
  17.      * @throws Exception
  18.      */
  19.     public static void main(String[] args) throws Exception{    //the purpose of throwing an exception here is for the pause in between certain
  20.  
  21.         Scanner in = new Scanner(System.in);
  22.  
  23.         int sticks = 21;
  24.         int whom = 0;
  25.         int[] toTake = {1, 2, 3};
  26.         int winner = 0;
  27.         String name1 = "null";
  28.         String name2 ="null";
  29.         char replay = 'v';
  30.  
  31.         System.out.println("Welcome once again everyone to 21 Stiiiiiiicks!");
  32.         TimeUnit.SECONDS.sleep(2);
  33.         System.out.println("The game starts with 21 sticks. After the first person is randomly chosen, " +
  34.                 "they will be able to begin picking up sticks.");
  35.         TimeUnit.SECONDS.sleep(2);
  36.  
  37.         System.out.println("\nEvery turn, you or the opponent will have the option of picking up 1, 2, or 3 sticks.");
  38.         TimeUnit.SECONDS.sleep(2);
  39.  
  40.         System.out.println("Once you pick up your stick(s), the opponent then moves. This continues until there are no sticks remaining.");
  41.         TimeUnit.SECONDS.sleep(2);
  42.  
  43.         System.out.println("The person who picks up the last remaining stick is the LOSER!");
  44.         TimeUnit.SECONDS.sleep(2);
  45.  
  46.         System.out.println("Good luck! And don't forget to have fun!");
  47.         TimeUnit.SECONDS.sleep(2);
  48.  
  49.         doThis(sticks, toTake, in, name1, name2, winner, whom, replay);
  50.     }
  51.  
  52.     /**
  53.      * This function is what contains most of the main parts that would occur before the actual game, such as choosing if one person is playing a computer, or
  54.      * if two people are playing against each other. It also prompts the player(s) for their name. Finally, after the game is finished, it will ask if they want to play again
  55.      * @param sticks
  56.      * @param toTake
  57.      * @param in
  58.      * @param name1
  59.      * @param name2
  60.      * @param winner
  61.      * @param whom
  62.      * @param replay
  63.      * @throws Exception
  64.      */
  65.     public static void doThis (int sticks, int[] toTake, Scanner in, String name1, String name2, int winner, int whom, char replay) throws Exception{
  66.         winner = 0;
  67.         System.out.println("\nSo, are there 1 or 2 players?       Type '1' or '2' to continue:");
  68.         int players = in.nextInt();
  69.         TimeUnit.SECONDS.sleep(2);
  70.  
  71.         if(players == 1){
  72.             System.out.print("What is the player's name? ");
  73.             name1 = in.next();
  74.             winner = vsCPU(in, sticks, toTake, winner, whom, name1);
  75.         } else if (players == 2){
  76.             System.out.print("What is player one's name? ");
  77.             name1 = in.next();
  78.             System.out.print("What is player two's name? ");
  79.             name2 = in.next();
  80.  
  81.             winner = vsHumans(in, sticks, winner, whom, name1, name2);
  82.         }
  83.  
  84.         //name variables can be reused since they will be reset if the player tries to play again
  85.         if (winner == 1){
  86.             System.out.print("\nThe Incredible Computer wins");
  87.             System.out.print("\nPlay again?     (Y/N)");
  88.             replay = in.next().charAt(0);
  89.         } else if (winner == 2) {
  90.             System.out.print("\n" + name1 + " is victorious");
  91.             System.out.print("\nPlay again?     (Y/N)");
  92.             replay = in.next().charAt(0);
  93.         } else if (winner == 3){
  94.             System.out.print("\n" + name2 + " is victorious");
  95.             System.out.print("\nPlay again?     (Y/N)");
  96.             replay = in.next().charAt(0);
  97.         }
  98.  
  99.         if (replay == 'Y' || replay == 'y'){
  100.             doThis(sticks, toTake, in, name1, name2, winner, whom, replay);
  101.         } else if (replay == 'N' || replay == 'n'){
  102.             System.exit(0);
  103.         }
  104.     }
  105.  
  106.     /**
  107.      * This is specific for if the player chooses to play the computer. It randomly picks whether the player or computer goes first.
  108.      * However, if the player is chosen to go first, rather than activate the vCPU function to play against the computer, you will have a slightly rigged game.
  109.      * Due to the nature of the game, mathematically, the person who goes first should always lose. The second person simply has to take the remainder of the number of sticks taken by the opponent from one more than the maximum number of sticks that can be taken byt a single person (3)
  110.      *
  111.      * @param in
  112.      * @param sticks
  113.      * @param toTake
  114.      * @param winner
  115.      * @param whom
  116.      * @param name1
  117.      * @return
  118.      * @throws Exception
  119.      */
  120.     public static int vsCPU (Scanner in, int sticks, int[] toTake, int winner, int whom, String name1) throws Exception{
  121.  
  122.         System.out.println(name1 + " is playing against a computer.");
  123.         TimeUnit.SECONDS.sleep(2);
  124.         int take = 0;
  125.         System.out.println("There are 21 sticks.\n");
  126.  
  127.         if (Math.random() > .5) {
  128.             System.out.println(name1 + " goes first!");
  129.             System.out.print("\nWill you take 1, 2, or 3 sticks?: ");
  130.             take = in.nextInt();
  131.             sticks -= take;
  132.             System.out.println(sticks + " sticks remain.\n");
  133.  
  134.             while (winner == 0){
  135.                 take = 4 - take;
  136.                 sticks -= take;
  137.                 System.out.println("The incredible computer took " + take + " stick(s).");
  138.                 System.out.println(sticks + " sticks remain.");
  139.                 if (sticks == 1){
  140.                     winner = 1;
  141.                     return winner;
  142.                 }
  143.                 System.out.println("Will " + name1 + " take 1, 2, or 3 sticks?: \n");
  144.                 take = in.nextInt();
  145.                 if (take < sticks){
  146.                     sticks -= take;
  147.                     System.out.println(sticks + " sticks remain.");
  148.  
  149.                     if (sticks == 1) {
  150.                         winner = 2;
  151.                         return winner;
  152.                     }
  153.                     whom = 2;
  154.                 } else {
  155.                     System.out.print("Invalid Input. Try again\n");
  156.                     System.out.println(sticks + " sticks remain.");
  157.                 }
  158.             }
  159.  
  160.         } else {
  161.             System.out.println("The computer goes first");
  162.  
  163.             int idx = new Random().nextInt(toTake.length); //this is the first instance of a true randomizer. This series of lines takes a random value from 1 to 3 and takes that from sticks
  164.             take = toTake[idx];
  165.             sticks -= take;
  166.             System.out.println("The computer takes " + take + " stick(s).");
  167.             System.out.println(sticks + " sticks remain.");
  168.  
  169.             whom = 1;
  170.             winner = vComp(in, sticks, toTake, winner, whom, name1, take);
  171.         }
  172.         return winner;
  173.     }
  174.  
  175.     /**
  176.      * Specifically if there are 2 players. Randomly decides which player goes first
  177.      * @param in
  178.      * @param sticks
  179.      * @param winner
  180.      * @param whom
  181.      * @param name1
  182.      * @param name2
  183.      * @return
  184.      * @throws Exception
  185.      */
  186.     public static int vsHumans (Scanner in, int sticks, int winner, int whom, String name1, String name2)throws Exception{
  187.         System.out.println("\nIts a battle between " + name1 + " and " + name2);
  188.         int take = 0;
  189.         TimeUnit.SECONDS.sleep(2);
  190.         System.out.println("There are 21 sticks.");
  191.  
  192.         if (Math.random() > .5) {
  193.             System.out.println(name1 + " goes first!");
  194.             System.out.print("Will " + name1+ " take 1, 2, or 3 sticks?: ");
  195.             take = in.nextInt();
  196.             sticks -= take;
  197.             System.out.println(sticks + " sticks remain.\n");
  198.  
  199.             whom = 2;
  200.             winner = vPeep(in, sticks, winner, whom, name1, name2, take);
  201.  
  202.         } else {
  203.             System.out.println(name2 + " goes first!");
  204.             System.out.print("Will " + name2 + " take 1, 2, or 3 sticks?: ");
  205.             take = in.nextInt();
  206.             sticks -= take;
  207.             System.out.println(sticks + " sticks remain.\n");
  208.  
  209.             whom = 1;
  210.             winner = vPeep(in, sticks, winner, whom, name1, name2, take);
  211.         }
  212.         return winner;
  213.     }
  214.  
  215.     /**
  216.      * This is for after the first move is made when playing against the computer. It randomizes however many sticks will be taken by the computer rather than making the computer win every time.
  217.      * However, once a certain amount of sticks are left then the computer will not mess up and will win. This only runs if the computer goes first, as the player could always mess up
  218.      * @param in
  219.      * @param sticks
  220.      * @param toTake
  221.      * @param winner
  222.      * @param whom
  223.      * @param name1
  224.      * @param take
  225.      * @return
  226.      */
  227.    public static int vComp (Scanner in, int sticks, int[] toTake, int winner, int whom, String name1, int take){
  228.        while (winner == 0) {
  229.            while (whom == 2) {
  230.                if (sticks > 4) {
  231.                    int idx = new Random().nextInt(toTake.length);
  232.                    take = toTake[idx];
  233.                    sticks -= take;
  234.                    System.out.println("The computer takes " + take + " stick(s). " + sticks + " sticks remain");
  235.                    whom = 1;
  236.                } else {
  237.                    take = sticks - 1;
  238.                    sticks -= take;
  239.                    System.out.print("The incredible computer took " + take + " stick(s).");
  240.                    System.out.println(sticks + " sticks remain.");
  241.  
  242.                    winner = 1;
  243.                    return winner;
  244.                }
  245.            }
  246.  
  247.            while (whom == 1) {
  248.                System.out.println("Will " + name1 + " take 1, 2, or 3 sticks?: \n");
  249.                take = in.nextInt();
  250.                if (take < sticks){
  251.                    sticks -= take;
  252.                    System.out.println(sticks + " sticks remain.");
  253.  
  254.                    if (sticks == 1) {
  255.                        winner = 2;
  256.                        return winner;
  257.                    }
  258.                    whom = 2;
  259.                } else {
  260.                    System.out.print("Invalid Input. Try again\n");
  261.                    System.out.println(sticks + " sticks remain.");
  262.                }
  263.            }
  264.        }
  265.        return winner;
  266.    }
  267.  
  268.     /**
  269.      *Runs after the first move when two humans are playing each other. The only limitation is that a person cannot lose if they have a winning move, and you cannot take more sticks than exist.
  270.      * @param in
  271.      * @param sticks
  272.      * @param winner
  273.      * @param whom
  274.      * @param name1
  275.      * @param name2
  276.      * @param take
  277.      * @return
  278.      */
  279.    public static int vPeep (Scanner in, int sticks, int winner, int whom, String name1, String name2, int take){
  280.         while (winner == 0){
  281.             while(whom == 2) {
  282.                 System.out.print("Will " + name2 + " take 1, 2, or 3 sticks?: \n");
  283.                 take = in.nextInt();
  284.                 if (take < sticks){
  285.                     sticks -= take;
  286.                     System.out.println(sticks + " sticks remain.");
  287.  
  288.                     if (sticks == 1) {
  289.                         winner = 3;
  290.                         return winner;
  291.                     }
  292.                     whom = 1;
  293.                 } else {
  294.                     System.out.print("Invalid Input. Try again\n ");
  295.                     System.out.println(sticks + " sticks remain.");
  296.                 }
  297.             }
  298.  
  299.             while (whom == 1) {
  300.                 System.out.print("Will " + name1 + " take 1, 2, or 3 sticks?: \n");
  301.                 take = in.nextInt();
  302.                 if (take < sticks){
  303.                     sticks -= take;
  304.                     System.out.println(sticks + " sticks remain.");
  305.  
  306.                     if (sticks == 1) {
  307.                         winner = 2;
  308.                         return winner;
  309.                     }
  310.                     whom = 2;
  311.                 } else {
  312.                     System.out.print("Invalid Input. Try again\n");
  313.                     System.out.println(sticks + " sticks remain.");
  314.                 }
  315.             }
  316.         }
  317.         return winner;
  318.    }
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement