loops making me loopy!
I have a project to create a game where you have coins and take them away one or two at a time. There is a class (http://www.users.muohio.edu/~wernerla/CSA174/NimPlayer) that I can call for the computer's turn. I just have no idea how to call it and put it into the game itself. I think my loops might be messed up. I was trying so hard to duplicate the given example that it might not work right. Here is the example of the game:
Welcome to the game of Nim!
We will take turns. When it is your turn, you may
remove either one or two coins from the pile.
Whoever removes the last coin from the pile is the winner!
Would you like a smart opponent? Enter yes or no: maybe
Please enter yes or no: yes
How many coins would you like in the starting pile?
Please choose a number from 1 to 50: 200
Please choose a number from 1 to 50: 14
You can go first.
There are 14 coins: OOOOOOOOOOOOOO
How many would you like to take, 1 or 2? 3
You can only take 1 or 2 coins. Try again.
How many would you like to take, 1 or 2? 2
There are 12 coins: OOOOOOOOOOOO
I will take 2 coins.
There are 10 coins: OOOOOOOOOO
How many would you like to take, 1 or 2? 1
There are 9 coins: OOOOOOOOO
I will take 2 coins.
There are 7 coins: OOOOOOO
How many would you like to take, 1 or 2? 1
There are 6 coins: OOOOOO
I will take 2 coins.
There are 4 coins: OOOO
How many would you like to take, 1 or 2? 1
There are 3 coins: OOO
I will take 2 coins.
There is 1 coin: O
How many would you like to take? 2
You can only take 1. Try again.
How many would you like to take? 1
***** YOU WIN! CONGRATULATIONS! *****
And here is my code so far... But I am kind of stuck. I don't really know what to do for the loops or if i need a bunch? I don't know how to make the game end either. Please offer some advice
import java.util.Scanner;
publicclass Program4{
publicstaticvoid main(String[] args){
Scanner keyboard =new Scanner(System.in);
System.out.println("Welcome to the game of Nim! \nWe will take turns. When it is your turn, you may \nremove either one or two coins from the pile. \nWhoever removes the last coin from the pile is the winner!\n");
String computer ="yes";
finalint LOW = 1;
finalint HIGH = 50;
int coins = 0;
int coinsTaken = 0;
do
{
//asks user for difficulty of player
System.out.print("Would you like a smart opponent? Enter yes or no: ");
computer = keyboard.nextLine();
}while (!(computer.equalsIgnoreCase("yes") || computer.equalsIgnoreCase("no")));
if (computer.equalsIgnoreCase("yes")){
}else{
}
finalint MINIMUM_COINS = 1;
finalint MAXIMUM_COINS = 50;
do
{
//asks user amount of coins in starting pile
System.out.print("\nHow many coins would you like in the starting pile? \nPlease choose a number from 1 to 50: ");
coins = keyboard.nextInt();
}while (coins<MINIMUM_COINS || coins>MAXIMUM_COINS);
System.out.println("\nYou can go first.\n");
do
{
System.out.print("There are " + coins +" coins: ");
}while (coins < LOW || coins > HIGH);
//display X's on one line
for (int count = 0; count < coins; count++){
System.out.print('O');
}
System.out.println();
int count2 = 0;
do
{
count2++;
if (count2==1)
{
System.out.print("How many would you like to take, 1 or 2? ");
coinsTaken = keyboard.nextInt();
}else{
System.out.print("You can only take 1 or 2 coins. Try again.\nHow many would you like to take, 1 or 2? ");
coinsTaken=keyboard.nextInt();
}
}while (coinsTaken<1 || coinsTaken>2);
}
}
I also need help making it so they can choose computer difficulty. right now I just assigned it numbers to put something in but I don't know how to do that. And how would I change the tense.. I guess if I could end the loops at 1 and so then i could just make a single thing so that it would say there "is" one coin left but I don't know how to make it either human or computers turn. Please help
Look at the example you gave for the output. You are going to have to put all of that in the final do...while loop.
but that isn't much of my concern yet. i just need the game to function right now
TIP: Break your code up into smaller chunks and place these in seperate methods. Don't put everything in the main method.P.S. I thought the object of NIM was to NOT take the last object.Message was edited by: flounder
> but that isn't much of my concern yet. i just need> the game to function right nowUm... yes it is. You need to do that in order for your game to function. I also highly recommend flounder's advice.
> but that isn't much of my concern yet. i just need
> the game to function right now
Could you explain what you have done please?
Like I don't understand your while loop at the end for starters. Shouldn't the exit condition be when either there are 0 or 1 coins left?
But I really just need the thing to continually ask me and then the computer to take coins first... Step by step... and I have to put it all in the main method we never learned how not to.
> But I really just need the thing to continually ask
When you say the word "continually" you mean a loop.
Now it's a bit distrurbing to me because the code you posted has some
loops and I'm wondering what the problem is here really.
Did you actually write the code you posted?
Please explain what you have written so far and why you wrote what you
wrote.
> me and then the computer to take coins first... Step
> by step... and I have to put it all in the main
> method we never learned how not to.
You must unlearn what you have learned
>Could you explain what you have done please?
>Like I don't understand your while loop at the end for starters. Shouldn't the >exit condition be when either there are 0 or 1 coins left?
My code is what I have done. I have only set it up so that it will run through and ask the human for their number of coins. The exit condition should be 0 or 1 but that is why i am on here asking for help. I am lost because I don't know how to add in the computer player in the loop to make it go back an fourth until the last turn which i don't know how to denote and then how to make it determine if it is the human or computer's turn.
Lol yes i did write the code myself. I pulled it all out of snippits of my previous projects and class exercises but I don't know how to make the logic work so that the loop will be continuous until there is one or two coins left and how to determine if it is the comptuer's turn or not. Or how to put in the right computer difficulty.
There are 14 coins: OOOOOOOOOOOOOO
How many would you like to take, 1 or 2? 3
You can only take 1 or 2 coins. Try again.
How many would you like to take, 1 or 2? 2
There are 12 coins: OOOOOOOOOOOO
I will take 2 coins.
There are 10 coins: OOOOOOOOOO
How many would you like to take, 1 or 2? 1
There are 9 coins: OOOOOOOOO
I will take 2 coins.
All of this needs to happen inside the last do...while loop.
For get the computer difficulty for now. Inside the last do...while loop, you need it to:
1. Ask how many coins the user wants to take.
2. Subtract that number from the total amount of coins.
3. Have the computer determine how many coins it wants to take.
4. Output how many coins the computer is taking.
5. Subtract that number from the total amount of coins.
6. Output the number of coins left (using the for loop).
7. Output if you won or lost if no coins are left.
Then keep looping through all of that.
I really need desperate help now. I am getting endless looping and it ends up saying there are -2 coins and goes on and on until i stop the program. I tried to put the computer's move in and have it loop.
Please tell me what I am doing wrong.
import java.util.Scanner;
public class Program4 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to the game of Nim! \nWe will take turns. When it is your turn, you may \nremove either one or two coins from the pile. \nWhoever removes the last coin from the pile is the winner!\n");
String computer = "yes";
final int LOW = 1;
final int HIGH = 50;
int coins = 0;
int coinsTaken = 0;
int computerMove1=0;
int coin = 0;
int count = 0;
String computerCoin="coin";
final int MINIMUM_COINS = 1;
final int MAXIMUM_COINS = 50;
do
{
//asks user for difficulty of player
System.out.print("Would you like a smart opponent? Enter yes or no: ");
computer = keyboard.nextLine();
}while (!(computer.equalsIgnoreCase("yes") || computer.equalsIgnoreCase("no")));
do
{
//asks user amount of coins in starting pile
System.out.print("\nHow many coins would you like in the starting pile? \nPlease choose a number from 1 to 50: ");
coin = keyboard.nextInt();
}while (coin<MINIMUM_COINS || coin>MAXIMUM_COINS);
System.out.println("\nYou can go first.\n");
//begins game
do
{
System.out.print("There are " + coin + " coins: ");
}while (coin < LOW || coin > HIGH);
//display X's on one line
for (count = 0; count < coin; count++){
System.out.print('O');
}
System.out.println();
do
{
do
{
count++;
if (count==1)
{
System.out.print("How many would you like to take, 1 or 2? ");
coinsTaken = keyboard.nextInt();
}else {
System.out.print("\nYou can only take 1 or 2 coins. Try again.\nHow many would you like to take, 1 or 2? ");
coinsTaken=keyboard.nextInt();
}
}while (coinsTaken<1 || coinsTaken>2);
do
{
coin= coins-coinsTaken;
System.out.print("\nThere are " + (coin) + " coins: ");
}while (coin < LOW || coin > HIGH);
for (count = 0; count < coin; count++){
System.out.print('O');
}
if (computer.equalsIgnoreCase("yes"))
{
NimPlayer computerMove = new NimPlayer();
computerMove1 = computerMove.getSmartMove(coin);
}
else
{
NimPlayer ComputerMove = new NimPlayer();
computerMove1 = ComputerMove.getDumbMove(coin);
}
if (computerMove1 == 1)
{
computerCoin=" coin.";
}
else
{
computerCoin=" coins.";
}
System.out.println("/nI will take " + computerMove1 + computerCoin);
coinsTaken=computerMove1;
do
{
coin= coins-coinsTaken;
System.out.print("\nThere are " + (coin) + " coins: ");
}while (coin < LOW || coin > HIGH);
for (count = 0; count < coin; count++){
System.out.print('O');
}
}while (coin >= 2);
}
}
it also isnt asking for coins anymore. now it just displays You can only take 1 or 2 coins. Try again.How many would you like to take, 1 or 2? and then allows you to enter a number
After combing through my program, I have found my mistakes and have it working. The only thing I need help with is how do I end the game? I don't know exactly where to say you win or you lose, what number to end the loop on and where to start new ones.
import java.util.Scanner;
public class Program4 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to the game of Nim! \nWe will take turns. When it is your turn, you may \nremove either one or two coins from the pile. \nWhoever removes the last coin from the pile is the winner!\n");
String computer = "yes";
final int LOW = 1;
final int HIGH = 50;
int coins = 0;
int coinsTaken = 0;
int computerMove1=0;
int coin = 0;
int count = 0;
int count2 = 0;
int count3 = 0;
String computerCoin="coin";
final int MINIMUM_COINS = 1;
final int MAXIMUM_COINS = 50;
do
{
//asks user for difficulty of player
System.out.print("Would you like a smart opponent? Enter yes or no: ");
computer = keyboard.nextLine();
}while (!(computer.equalsIgnoreCase("yes") || computer.equalsIgnoreCase("no")));
do
{
//asks user amount of coins in starting pile
System.out.print("\nHow many coins would you like in the starting pile? \nPlease choose a number from 1 to 50: ");
coin = keyboard.nextInt();
}while (coin<MINIMUM_COINS || coin>MAXIMUM_COINS);
System.out.println("\nYou can go first.");
//begins game
do
{
//Human Turn
do
{
System.out.print("\nThere are " + coin + " coins: ");
}while (coin < LOW || coin > HIGH);
//display O's on one line
for (count = 0; count < coin; count++){
System.out.print('O');
}
do
{
count2++;
if (count2==1)
{
System.out.print("\nHow many would you like to take, 1 or 2? ");
coinsTaken = keyboard.nextInt();
}else {
System.out.print("You can only take 1 or 2 coins. Try again.\nHow many would you like to take, 1 or 2? ");
coinsTaken=keyboard.nextInt();
}
}while (coinsTaken<1 || coinsTaken>2);
count2=0;
coin=coin-coinsTaken;
//computers turn
do
{
System.out.print("\nThere are " + coin + " coins: ");
}while (coin < LOW || coin > HIGH);
//display O's on one line
for (count = 0; count < coin; count++){
System.out.print('O');
}
System.out.println();
if (computer.equalsIgnoreCase("yes"))
{
NimPlayer computerMove = new NimPlayer();
computerMove1 = computerMove.getSmartMove(coin);
}
else
{
NimPlayer ComputerMove = new NimPlayer();
computerMove1 = ComputerMove.getDumbMove(coin);
}
if (computerMove1 == 1)
{
computerCoin=" coin.";
}
else
{
computerCoin=" coins.";
}
System.out.println("I will take " + computerMove1 + computerCoin);
coinsTaken=computerMove1;
coin=coin-coinsTaken;
}while (coin > 2);
}
}
