droidnoob
droidnoob

Reputation: 335

alternating players and adding user inputs

I came across this java question and I wanted to see how easy it would be to write the code for it.

Write a Java program that enables two people to play the following game. We start with a pile of chips. Your game will allow for numbers that are odd. The first player removes between 1 and (number of chips - 1) / 2 from the pile. From then on the players alternate turns, removing chips from the pile until it is empty. Once the pile becomes empty, the game is over.

It was going well till I had to alternate turns. I did it right but when player 1 puts in a value, and player 2 does too, when the while loop goes again, it starts at 0 and not adding the past inputs.

This is what I mean:

while(chips!=0){
    if(counter%2==0){
        System.out.println(fname + " has " + (one) + " chips"  + "\n" + sname + " has " + (two) + " chips." + "\n" + "It is your turn, " + fname + "\n" + "There are " + chips + " remaining." + "\n" + "You may take any number of chips from 1 to " + (chips-1)/2 + ". How many will you take, " + fname + "?") ;
        one +=input.nextInt();
        chips -=one;
    } else {
        System.out.println(fname + " has " + (one) + " chips"  + "\n" + sname + " has " + (two) + " chips." + "\n" + "It is your turn, " + sname + "\n" + "There are " + chips + " remaining." + "\n" + "You may take any number of chips from 1 to " + (chips-1)/2 + ". How many will you take, " + sname + "?") ;
        two +=input.nextInt();
        chips -=two;
    }
    counter++;
}

Does anyone have a solution for that?

Upvotes: 2

Views: 1830

Answers (2)

Limmen
Limmen

Reputation: 1457

This should work:

import java.io.*;
import java.util.Scanner;

public class Test {

public static Scanner input;
public static void main(String[] args) {

    System.out.println("Enter number of chips to start the game with:");
    input = new Scanner(System.in);
    int chips = input.nextInt();
    turn_one(chips, 0 ,0);              
}


 public static void turn_one(int chips, int playerone, int playertwo){
        int max;
        if(chips <= 2){
            max = 1;               
        }
        else{
            max = (chips-1)/2;
        }
        if(chips <= 0){
            System.out.println("Game over!");
            return;
        }

        System.out.println("Playerone has " + playerone + " chips"  + "\n" + "playertwo" + " has " + playertwo + " chips." + "\n" + "It is your turn, " + "playerone" + "\n" + "There are " + chips + " remaining." + "\n" + "You may take any number of chips from 1 to " + max + ". How many will you take, playerone?") ;

        int taken = input.nextInt();
        if (taken > max){
            System.out.println("You can't take that many chips!");
            turn_one(chips, playerone, playertwo);
        }
        playerone = playerone + taken;
        chips = chips - taken;
        turn_two(chips, playerone, playertwo);
    }


    public static void turn_two(int chips, int playerone, int playertwo){
        int max;
        if(chips <= 2){
            max = 1;               
        }
        else{
            max = (chips-1)/2;
        }
        if(chips <= 0){
            System.out.println("Game over!");
            return;
        }
        System.out.println("Playerone has " + playerone + " chips"  + "\n" + "playertwo" + " has " + playertwo + " chips." + "\n" + "It is your turn, " + "playertwo" + "\n" + "There are " + chips + " remaining." + "\n" + "You may take any number of chips from 1 to " + max + ". How many will you take, playertwo?") ;

        int taken = input.nextInt();
        if (taken > max){
            System.out.println("You can't take that many chips!");
            turn_one(chips, playerone, playertwo);
        }
        playertwo = playertwo + taken;
        chips = chips - taken;
        turn_one(chips, playerone, playertwo);
    }

}

Upvotes: 1

Kurumi
Kurumi

Reputation: 103

You need to increment the players' chips by a value instead of assigning it.

int amount = input.nextInt();
one += amount;
chips -= amount;

Upvotes: 1

Related Questions