Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class Nim {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Random r = new Random();
- int pA = 5, pB = 5, pC = 5, turn = 1, pS, n, x;
- String p1, p2, s1 = null, pile, m1, m2, m3;
- //strings used more than once
- m1 = "You must choose at least one and no more than what the stack has";
- m2 = "Selected pile has no more stacks";
- m3 = "Try again: ";
- System.out.println("Player 1 enter your name ");
- p1 = sc.next();
- System.out.println("Player 2 enter your name or type \"computer\" to play against the computer");
- p2 = sc.next();
- while ((pA + pB + pC) > 0) {
- n = 0;
- System.out.print("A: ");
- while (n < pA) {
- System.out.print("*");
- n++;
- }
- n = 0;
- System.out.print("\nB: ");
- while (n < pB) {
- System.out.print("*");
- n++;
- }
- n = 0;
- System.out.print("\nC: ");
- while (n < pC) {
- System.out.print("*");
- n++;
- }
- //turn checker
- if (turn == 1) {
- s1 = p1;
- turn--;
- } else if (turn == 0 && (p2.equals("computer"))) {
- s1 = p1;
- turn++;
- } else {
- s1 = p2;
- turn++;
- }
- System.out.print("\n" + s1 + " choose pile: ");
- pile = sc.next();
- //checks input to see if "A", "B" or "C" were selected
- while (("A".compareTo(pile) + 3 <= 0)||("A".compareTo(pile)+3>3)) {
- System.out.print("Select a valid pile: ");
- pile = sc.next();
- }
- //checks whether the pile is empty or not
- while ((pile.equals("A") && pA == 0) || (pile.equals("B") && pB == 0) || (pile.equals("C") && pC == 0)) {
- System.out.println(m2);
- System.out.print(m3);
- pile = sc.next();
- }
- System.out.print("How many to take from pile: ");
- pS = sc.nextInt();
- //cheat protection
- while (pile.equals("A") && (pS <= 0 || pS > pA)) {
- System.out.println(m1);
- System.out.print(m3);
- pS = sc.nextInt();
- }
- while (pile.equals("B") && (pS <= 0 || pS > pB)) {
- System.out.println(m1);
- System.out.print(m3);
- pS = sc.nextInt();
- }
- while (pile.equals("C") && (pS <= 0 || pS > pC)) {
- System.out.println(m1);
- System.out.print(m3);
- pS = sc.nextInt();
- }
- if (pile.equals("A")) {
- pA -= pS;
- } else if (pile.equals("B")) {
- pB -= pS;
- } else if (pile.equals("C")) {
- pC -= pS;
- }
- //no idea how to make this shorter. Detects when theres 1 stack left to declare a winner before the computer's turn
- if ((pA == 1 && pB == 0 && pC == 0) || (pA == 0 && pB == 1 && pC == 0) || (pA == 0 && pB == 0 && pC == 1)) {
- if (turn == 1 && (p2.equals("computer"))) {
- System.out.println("the computer lost, its pretty dumb.");
- } else if (turn == 1) {
- s1 = p1;
- System.out.println(s1 + " there is only one left, you lose");
- } else {
- s1 = p2;
- System.out.println(s1 + " there is only one left, you lose");
- }
- return;
- }
- //computer opponent, i dont know whats going on in here anymore
- if (p2.equals("computer")) {
- x = 1 + r.nextInt(3);
- if (x == 1 && pA > 0) {
- x = 1 + r.nextInt(5);
- while (x > pA) {
- x = 1 + r.nextInt(5);
- }
- System.out.println("the computer took " + x + " from pile A");
- pA -= x;
- } else if (x == 2 && pB > 0) {
- x = 1 + r.nextInt(5);
- while (x > pB) {
- x = 1 + r.nextInt(5);
- }
- System.out.println("the computer took " + x + " from pile B");
- pB -= x;
- } else if (x == 3 && pC > 0) {
- x = 1 + r.nextInt(5);
- while (x > pC) {
- x = 1 + r.nextInt(5);
- }
- System.out.println("the computer took " + x + " from pile C");
- pC -= x;
- }
- //the more i read, the more confused i get but it works
- if (x == 1 && pA == 0) {
- if (pB > 0) {
- x = 1 + r.nextInt(5);
- while (x > pB) {
- x = 1 + r.nextInt(5);
- }
- System.out.println("the computer took " + x + " from pile B");
- pB -= x;
- } else {
- x = 1 + r.nextInt(5);
- while (x > pC) {
- x = 1 + r.nextInt(5);
- }
- System.out.println("the computer took " + x + " from pile C");
- pC -= x;
- }
- } else if (x == 2 && pB == 0) {
- if (pA > 0) {
- x = 1 + r.nextInt(5);
- while (x > pB) {
- x = 1 + r.nextInt(5);
- }
- System.out.println("the computer took " + x + " from pile A");
- pA -= x;
- } else {
- x = 1 + r.nextInt(5);
- while (x > pC) {
- x = 1 + r.nextInt(5);
- }
- System.out.println("the computer took " + x + " from pile C");
- pC -= x;
- }
- } else if (x == 3 && pC == 0) {
- if (pA > 0) {
- x = 1 + r.nextInt(5);
- while (x > pA) {
- x = 1 + r.nextInt(5);
- }
- System.out.println("the computer took " + x + " from pile A");
- pA -= x;
- } else {
- x = 1 + r.nextInt(5);
- while (x > pB) {
- x = 1 + r.nextInt(5);
- }
- System.out.println("the computer took " + x + " from pile B");
- pB -= x;
- }
- }
- }
- //computer player doesnt break any rules but doesnt really know how to play
- if (pA==0&&pB==0&&pC==0){
- System.out.println("The computer probably lost by taking all remaining uhh things....");
- return;
- }
- //Detects when theres 1 stack left to declare a winner again at the end of the computer's turn in case it wins!
- if ((pA == 1 && pB == 0 && pC == 0) || (pA == 0 && pB == 1 && pC == 0) || (pA == 0 && pB == 0 && pC == 1)) {
- if (turn == 1 && (p2.equals("computer"))) {
- System.out.println("the computer lost, its pretty dumb.");
- } else if (turn == 1) {
- s1 = p1;
- System.out.println(s1 + " there is only one left, you lose");
- } else {
- s1 = p2;
- System.out.println(s1 + " there is only one left, you lose");
- }
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment