Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package nim;
- import java.util.Scanner;
- import java.util.Random;
- public class Nim {
- public static void instructions(boolean turn) {
- System.out.println("This game is called Nim...");
- System.out.println("The objective is to force the computer to grab the last stone...");
- System.out.println("You can only take between 1 and 3 stones...");
- System.out.println("You will go first...");
- turn = false;
- }
- public static void game(boolean turn,int stones, int pickedStones) {
- Scanner input = new Scanner(System.in);
- System.out.println("There are "+stones+" stones. ");
- System.out.print("How many stones do you wish to take?(1,2 or 3)");
- pickedStones = input.nextInt();
- if (pickedStones<=3 && pickedStones>=1) {
- stones -= pickedStones;
- turn = false;
- } else if(pickedStones>3){
- System.out.println("C'mon pick a proper number...");
- System.out.print("How many stones do you wish to take?(1,2 or 3)");
- pickedStones = input.nextInt();
- stones -= pickedStones;
- turn = false;
- }}
- public static void isValid(int pickedStones,int stones) {
- if (pickedStones>stones && pickedStones > 3) {
- System.out.print("That is an invalid number");
- }}
- public static void drawStones(int stones,int startStones,int computerPick, boolean turn) {
- Random r = new Random();
- if (stones>=3){
- int computerTurn = r.nextInt(3);
- computerPick = computerTurn;
- turn = true;
- } else if (stones==2){
- int computerTurn = r.nextInt(2);
- computerPick = computerTurn;
- turn = true;
- } else if (stones==1){
- System.out.print("Congrats,you won!");
- } }
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- boolean turn;
- turn = false;
- Random rand = new Random();
- Random r = new Random();
- int pickedStones=0;
- int fifteen = 15;
- int thirty = 30;
- int startStones = r.nextInt(thirty-fifteen)+fifteen;
- int stones = startStones;
- int computerPick = 0;
- instructions(turn);
- System.out.println("There are "+stones+" stones. ");
- System.out.print("How many stones do you wish to take?(1,2 or 3)");
- pickedStones = input.nextInt();
- pickedStones -= stones;
- System.out.print(stones+" stones remain...");
- if (pickedStones<=3 && pickedStones>=1) {
- stones -= pickedStones;
- turn = false;
- } else {
- System.out.println("C'mon pick a proper number...");
- System.out.print("How many stones do you wish to take?(1,2 or 3)");
- pickedStones = input.nextInt();
- stones -= pickedStones;
- turn = false;
- while (stones>0) {
- if (turn == true){
- game(turn,stones,pickedStones);
- isValid(stones,pickedStones);
- }
- if (turn == false ){
- drawStones(stones,startStones,computerPick,turn);
- }}}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement