Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.concurrent.TimeUnit;
- import java.util.Scanner;
- /**
- * Francesco Di Mise
- * 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.
- * The game randomly chooses who goes first, and the person can take 1, 2 or 3 sticks on every turn.
- * 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.
- */
- public class TwantyWanStoccs {
- /**
- * The main function displays the instructions and calls the doThis function which performs the rest of the program.
- * This is mostly so that if the player decides to the replay the game they will not have to wait through the instructions again
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception{ //the purpose of throwing an exception here is for the pause in between certain
- Scanner in = new Scanner(System.in);
- int sticks = 21;
- int whom = 0;
- int[] toTake = {1, 2, 3};
- int winner = 0;
- String name1 = "null";
- String name2 ="null";
- char replay = 'v';
- System.out.println("Welcome once again everyone to 21 Stiiiiiiicks!");
- TimeUnit.SECONDS.sleep(2);
- System.out.println("The game starts with 21 sticks. After the first person is randomly chosen, " +
- "they will be able to begin picking up sticks.");
- TimeUnit.SECONDS.sleep(2);
- System.out.println("\nEvery turn, you or the opponent will have the option of picking up 1, 2, or 3 sticks.");
- TimeUnit.SECONDS.sleep(2);
- System.out.println("Once you pick up your stick(s), the opponent then moves. This continues until there are no sticks remaining.");
- TimeUnit.SECONDS.sleep(2);
- System.out.println("The person who picks up the last remaining stick is the LOSER!");
- TimeUnit.SECONDS.sleep(2);
- System.out.println("Good luck! And don't forget to have fun!");
- TimeUnit.SECONDS.sleep(2);
- doThis(sticks, toTake, in, name1, name2, winner, whom, replay);
- }
- /**
- * 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
- * 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
- * @param sticks
- * @param toTake
- * @param in
- * @param name1
- * @param name2
- * @param winner
- * @param whom
- * @param replay
- * @throws Exception
- */
- public static void doThis (int sticks, int[] toTake, Scanner in, String name1, String name2, int winner, int whom, char replay) throws Exception{
- winner = 0;
- System.out.println("\nSo, are there 1 or 2 players? Type '1' or '2' to continue:");
- int players = in.nextInt();
- TimeUnit.SECONDS.sleep(2);
- if(players == 1){
- System.out.print("What is the player's name? ");
- name1 = in.next();
- winner = vsCPU(in, sticks, toTake, winner, whom, name1);
- } else if (players == 2){
- System.out.print("What is player one's name? ");
- name1 = in.next();
- System.out.print("What is player two's name? ");
- name2 = in.next();
- winner = vsHumans(in, sticks, winner, whom, name1, name2);
- }
- //name variables can be reused since they will be reset if the player tries to play again
- if (winner == 1){
- System.out.print("\nThe Incredible Computer wins");
- System.out.print("\nPlay again? (Y/N)");
- replay = in.next().charAt(0);
- } else if (winner == 2) {
- System.out.print("\n" + name1 + " is victorious");
- System.out.print("\nPlay again? (Y/N)");
- replay = in.next().charAt(0);
- } else if (winner == 3){
- System.out.print("\n" + name2 + " is victorious");
- System.out.print("\nPlay again? (Y/N)");
- replay = in.next().charAt(0);
- }
- if (replay == 'Y' || replay == 'y'){
- doThis(sticks, toTake, in, name1, name2, winner, whom, replay);
- } else if (replay == 'N' || replay == 'n'){
- System.exit(0);
- }
- }
- /**
- * This is specific for if the player chooses to play the computer. It randomly picks whether the player or computer goes first.
- * 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.
- * 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)
- *
- * @param in
- * @param sticks
- * @param toTake
- * @param winner
- * @param whom
- * @param name1
- * @return
- * @throws Exception
- */
- public static int vsCPU (Scanner in, int sticks, int[] toTake, int winner, int whom, String name1) throws Exception{
- System.out.println(name1 + " is playing against a computer.");
- TimeUnit.SECONDS.sleep(2);
- int take = 0;
- System.out.println("There are 21 sticks.\n");
- if (Math.random() > .5) {
- System.out.println(name1 + " goes first!");
- System.out.print("\nWill you take 1, 2, or 3 sticks?: ");
- take = in.nextInt();
- sticks -= take;
- System.out.println(sticks + " sticks remain.\n");
- while (winner == 0){
- take = 4 - take;
- sticks -= take;
- System.out.println("The incredible computer took " + take + " stick(s).");
- System.out.println(sticks + " sticks remain.");
- if (sticks == 1){
- winner = 1;
- return winner;
- }
- System.out.println("Will " + name1 + " take 1, 2, or 3 sticks?: \n");
- take = in.nextInt();
- if (take < sticks){
- sticks -= take;
- System.out.println(sticks + " sticks remain.");
- if (sticks == 1) {
- winner = 2;
- return winner;
- }
- whom = 2;
- } else {
- System.out.print("Invalid Input. Try again\n");
- System.out.println(sticks + " sticks remain.");
- }
- }
- } else {
- System.out.println("The computer goes first");
- 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
- take = toTake[idx];
- sticks -= take;
- System.out.println("The computer takes " + take + " stick(s).");
- System.out.println(sticks + " sticks remain.");
- whom = 1;
- winner = vComp(in, sticks, toTake, winner, whom, name1, take);
- }
- return winner;
- }
- /**
- * Specifically if there are 2 players. Randomly decides which player goes first
- * @param in
- * @param sticks
- * @param winner
- * @param whom
- * @param name1
- * @param name2
- * @return
- * @throws Exception
- */
- public static int vsHumans (Scanner in, int sticks, int winner, int whom, String name1, String name2)throws Exception{
- System.out.println("\nIts a battle between " + name1 + " and " + name2);
- int take = 0;
- TimeUnit.SECONDS.sleep(2);
- System.out.println("There are 21 sticks.");
- if (Math.random() > .5) {
- System.out.println(name1 + " goes first!");
- System.out.print("Will " + name1+ " take 1, 2, or 3 sticks?: ");
- take = in.nextInt();
- sticks -= take;
- System.out.println(sticks + " sticks remain.\n");
- whom = 2;
- winner = vPeep(in, sticks, winner, whom, name1, name2, take);
- } else {
- System.out.println(name2 + " goes first!");
- System.out.print("Will " + name2 + " take 1, 2, or 3 sticks?: ");
- take = in.nextInt();
- sticks -= take;
- System.out.println(sticks + " sticks remain.\n");
- whom = 1;
- winner = vPeep(in, sticks, winner, whom, name1, name2, take);
- }
- return winner;
- }
- /**
- * 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.
- * 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
- * @param in
- * @param sticks
- * @param toTake
- * @param winner
- * @param whom
- * @param name1
- * @param take
- * @return
- */
- public static int vComp (Scanner in, int sticks, int[] toTake, int winner, int whom, String name1, int take){
- while (winner == 0) {
- while (whom == 2) {
- if (sticks > 4) {
- int idx = new Random().nextInt(toTake.length);
- take = toTake[idx];
- sticks -= take;
- System.out.println("The computer takes " + take + " stick(s). " + sticks + " sticks remain");
- whom = 1;
- } else {
- take = sticks - 1;
- sticks -= take;
- System.out.print("The incredible computer took " + take + " stick(s).");
- System.out.println(sticks + " sticks remain.");
- winner = 1;
- return winner;
- }
- }
- while (whom == 1) {
- System.out.println("Will " + name1 + " take 1, 2, or 3 sticks?: \n");
- take = in.nextInt();
- if (take < sticks){
- sticks -= take;
- System.out.println(sticks + " sticks remain.");
- if (sticks == 1) {
- winner = 2;
- return winner;
- }
- whom = 2;
- } else {
- System.out.print("Invalid Input. Try again\n");
- System.out.println(sticks + " sticks remain.");
- }
- }
- }
- return winner;
- }
- /**
- *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.
- * @param in
- * @param sticks
- * @param winner
- * @param whom
- * @param name1
- * @param name2
- * @param take
- * @return
- */
- public static int vPeep (Scanner in, int sticks, int winner, int whom, String name1, String name2, int take){
- while (winner == 0){
- while(whom == 2) {
- System.out.print("Will " + name2 + " take 1, 2, or 3 sticks?: \n");
- take = in.nextInt();
- if (take < sticks){
- sticks -= take;
- System.out.println(sticks + " sticks remain.");
- if (sticks == 1) {
- winner = 3;
- return winner;
- }
- whom = 1;
- } else {
- System.out.print("Invalid Input. Try again\n ");
- System.out.println(sticks + " sticks remain.");
- }
- }
- while (whom == 1) {
- System.out.print("Will " + name1 + " take 1, 2, or 3 sticks?: \n");
- take = in.nextInt();
- if (take < sticks){
- sticks -= take;
- System.out.println(sticks + " sticks remain.");
- if (sticks == 1) {
- winner = 2;
- return winner;
- }
- whom = 2;
- } else {
- System.out.print("Invalid Input. Try again\n");
- System.out.println(sticks + " sticks remain.");
- }
- }
- }
- return winner;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement