Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class GameOfNim{
- public static void main(String[]args){
- Scanner in = new Scanner(System.in);
- System.out.println("The game of Nim. This is a well known game with a number of variants. The following variant has an interesting winning strategy. Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most half of the marbles. Then the other player takes a turn. The player who takes the last marble loses.");
- //GENERATING TURN:
- System.out.println("-----------------------------------------------");
- boolean turn = false;
- double turnNbr = Math.random();
- if(turnNbr>0.5){
- turn = true;
- System.out.println("The user starts");
- }else{
- turn = false;
- System.out.println("The computer starts");
- }
- //GENERATING PILE:
- int pileSize = (int)(Math.random() * ((100 - 10) + 1)) + 10;
- System.out.println("The pile size is: " + pileSize);
- //GENERATING GAMEDIFF:
- boolean difficulty = false;
- double gameDifficulty = Math.random();
- if(gameDifficulty>0.5){
- difficulty = true;
- System.out.println("The computer plays stupid");
- }else{
- difficulty = false;
- // The computer plays "smart"
- System.out.println("The computer plays smart");
- }
- System.out.println("-----------------------------------------------");
- int subComp = 0;
- int counter = 0;
- if(turn){
- while(pileSize>0){
- // The user is playing;
- System.out.println("How many marbles do you wanna take?: ");
- int sub = in.nextInt();
- pileSize = pileSize - sub;
- System.out.println("Marbles left: " + pileSize);
- if(pileSize == 1){
- System.out.println("The user won!");
- }
- else if(pileSize-sub == 1){
- System.out.println("You lost!");
- }
- System.out.println("The computer is playing ...");
- if(difficulty){
- subComp = (int)(Math.random() * ((pileSize/2 - 1) + 1)) + 1;
- pileSize = pileSize - subComp;
- }
- else{
- if(pileSize == Math.pow(subComp,2)-1){
- subComp = (int)(Math.random() * ((pileSize/2 - 1) + 1)) + 1;
- }
- }
- System.out.println("The computer took: " + subComp + " marbles off!");
- System.out.println("Marbles left: " + pileSize);
- System.out.println("Your turn!");
- System.out.println("-----------------------------------------------");
- counter++;
- }
- }
- else{
- while(pileSize>0){
- System.out.println("The computer is playing ...");
- // The computer is playing;
- if(difficulty){
- subComp = (int)(Math.random() * ((pileSize/2 - 1) + 1)) + 1;
- pileSize = pileSize - subComp;
- }
- else{
- if(pileSize == Math.pow(subComp,2)-1){
- subComp = (int)(Math.random() * ((pileSize/2 - 1) + 1)) + 1;
- }
- }
- System.out.println("The computer took: " + subComp + " marbles off!");
- System.out.println("Marbles left: " + pileSize);
- System.out.println("Your turn!");
- }
- // The user is playing;
- System.out.println("How many marbles do you wanna take?: ");
- int sub = in.nextInt();
- pileSize = pileSize - sub;
- System.out.println("Marbles left: " + pileSize);
- if(pileSize == 1){
- System.out.println("The user won!");
- }
- else if(pileSize-sub == 1){
- System.out.println("You lost!");
- }
- System.out.println("-----------------------------------------------");
- counter++;
- }
- }
- /**
- //TESTING SETTINGS:
- for(int i = 1; i<=30;i++){
- //GENERATING TURN:
- boolean turn = false;
- double turnNbr = Math.random();
- if(turnNbr>0.5){
- turn = true;
- System.out.println("The user starts");
- }else{
- turn = false;
- System.out.println("The computer starts");
- }
- //GENERATING PILE:
- int pileSize = (int)(Math.random() * ((100 - 10) + 1)) + 10;
- System.out.println("The pile size is: " + pileSize);
- //GENERATING GAMEDIFF:
- boolean difficulty = false;
- double gameDifficulty = Math.random();
- if(gameDifficulty>0.5){
- difficulty = true;
- System.out.println("The computer plays stupid");
- }else{
- difficulty = false;
- // The computer plays "smart"
- System.out.println("The computer plays smart");
- }
- System.out.println("-----------------------------------------------");
- }
- **/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement