8 Replies - 437 Views - Last Post: 23 January 2013 - 11:32 AM Rate Topic: -----

#1 PaC-mAn336  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-January 13

Penny Wise PRogram

Posted 22 January 2013 - 10:08 AM

Players: 2-6

Object: To be the last player with any coins.

Setup: Each player starts with four pennies, three nickels, two dimes, and a quarter. The pot is empty at the beginning of the game.

Play: On your turn, put one of your coins into the pot. You may then take change from the pot, up to one cent less than the value of the coin you played. For example, if you put in a dime, you may take out up to nine cents worth of coins.

This is the objective of the program and here is the actual game program I can't figure out how to alternate between the two players and to add to the pot as of right now the program runs but takes the user input and ends the program I been trying to use a while to for the players to take turns but it always comes back as an error

import java.util.Scanner;



public class PennyWiseGame {

	

	private SetCoin pOne;
	
	private SetCoin pTwo;
	
	private SetCoin pot;
	
	private int deposit;
	
	Scanner input;
	
	

	public PennyWiseGame(){
		
		pOne = new SetCoin(4, 3, 2, 1);
		
		pTwo = new SetCoin(4, 3, 2, 1);
		
		pot = new SetCoin(0, 0, 0, 0);

		deposit = 0;
		
		input = new Scanner(System.in);
	
	}
	
	

public void turn(){
		
System.out.print("Player 1\nSelect which coin to add to the pot:\n1. Penny\n2. Nickel\n3.Dime\n4. Quarter\n");
deposit = input.nextInt();

while(deposit > 4 || deposit < 0){
System.out.println("Invalid Choice!");

System.out.print("Select which coin to add to the pot:\n1. Penny\n2. Nickel\n3. Dime\n4. Quarter\n");
deposit = input.nextInt();
		
		}
		
	
	}
public static void main(String[] args){
System.out.println("Welcome to PennyWise!");
PennyWiseGame game = new PennyWiseGame();
game.turn();
}
}



This is the setCoin class

 public class SetCoin {
    
private double pennies;

	private double nickels;

	private double dimes;

	private double quarters;


	

    public SetCoin(double numPen, double numNic, double numDi, double numQu){

			pennies = numPen;

			nickels = numNic;

			dimes = numDi;

			quarters = numQu;

		}


	
	public void addPenny(){

			pennies++;

		}


	
	public void addNickel(){

			nickels++;
	
	}


	
	public void addDime(){

			dimes++;
	
	}


	
	public void addQuarter(){
				
			quarters++;
	
	}


	
	public void subPenny(){

			pennies--;
	
	}

	
	
		public void subNickel(){
		
			nickels--;
	
		}

	
	
		public void subDime(){
	
			dimes--;
	
		}

	
			
		public void subQuarter(){

			quarters--;

		}	
	
	
		public void setPen(double newPen){
	
			pennies = newPen;
	
		}
	

	
		public double getPen(){
	
			return pennies;

		}
	

	
		public void setNic(double newNic){

			nickels = newNic;
	
		}

	
	
		public double getNic(){
	
			return nickels;
	
		}
	

		
		public void setDi(double newDi){
	
			dimes = newDi;

		}

		
	
		public double getDi(){
	
			return dimes;
	
		}
	
	

		public void setQu(double newQu){

			quarters = newQu;

		}


	
	public double getQu(){
		
			return quarters;

		}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Penny Wise PRogram

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7872
  • View blog
  • Posts: 13,354
  • Joined: 19-March 11

Re: Penny Wise PRogram

Posted 22 January 2013 - 10:51 AM

You want to set up a loop: in English, "while the game is not done, the next player takes a turn".

Your turn method, then, should take an argument to tell you which player is moving, and return a boolean value to tell you if the game is finished. For example, you might have player 0 and player 1. The math for flipping players is:


currentPlayer = (currentPlayer+1)%2;


Try setting up a toy game which alternates between players, but the rules are very simple: if a player enters a positive number, the game continues. If a player enters a negative number, the game ends and that player wins.

For the sake of courtesy, use an array of Strings to store player names, so you can address them by name.

Once you've done that, then you can worry about implementing the rules of your real game.
Was This Post Helpful? 0
  • +
  • -

#3 PaC-mAn336  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-January 13

Re: Penny Wise PRogram

Posted 23 January 2013 - 08:47 AM

I ended up using a for look to alternate between the players but when I try to display the current state of the player and the pot it just give me machine code I know I need a toString() method somewhere but when I write it in the the PennyWiseGame class I get an error and when I write it in my SetCoin class I get a error any ideas?

My updated PennyWise class with for loop
import java.util.Scanner;



public class PennyWiseGame {

	

	private SetCoin pOne;
	
	private SetCoin pTwo;
	
	private SetCoin pot;
	
	private int deposit;
	
	Scanner input;
	
        int player;
       
        

	public PennyWiseGame(){
		
		pOne = new SetCoin(4, 3, 2, 1);
		
		pTwo = new SetCoin(4, 3, 2, 1);
		
		pot = new SetCoin(0, 0, 0, 0);

		deposit = 0;
		
		input = new Scanner(System.in);
	
	}
	
	

public void turn(){

		for (player = 1; player < 2; player = player --){

               if(player == 1){
    System.out.print("Player 1 \nSelect which coin to add to the pot:\n1. Penny\n2. Nickel\n3.Dime\n4. Quarter\n");

deposit = input.nextInt();

while(deposit > 4 || deposit < 0){
System.out.println("Invalid Choice!");

System.out.print("Select which coin to add to the pot:\n1. Penny\n2. Nickel\n3. Dime\n4. Quarter\n");
deposit = input.nextInt();

}		
		
if( deposit == 1){
                pOne.subPenny();
                pot.addPenny();
System.out.println(pOne.toString());
System.out.println(pot.toString());

}
	if(deposit == 2){
        pOne.subNickel();
        pot.addNickel();
        System.out.println(pOne.toString());
System.out.println(pot.toString());
        }
	if(deposit == 3){
        pOne.subDime();
        pot.addDime();
        System.out.println(pOne.toString());
System.out.println(pot.toString());
        }
if(deposit == 4){
pOne.subQuarter();
pot.addQuarter();
System.out.println(pOne.toString());
System.out.println(pot.toString());
}
}
if(player == 1){
System.out.print("Player 2 \nSelect which coin to add to the pot:\n1. Penny\n2. Nickel\n3.Dime\n4. Quarter\n");

deposit = input.nextInt();

while(deposit > 4 || deposit < 0){
System.out.println("Invalid Choice!");

System.out.print("Select which coin to add to the pot:\n1. Penny\n2. Nickel\n3. Dime\n4. Quarter\n");
deposit = input.nextInt();
player = player - 2;
}
if( deposit == 1){
                pTwo.subPenny();
                pot.addPenny();
                System.out.println(pTwo);
System.out.println(pot);

}
	if(deposit == 2){
        pTwo.subNickel();
        pot.addNickel();
        System.out.println(pTwo);
System.out.println(pot);

        }
	if(deposit == 3){
        pTwo.subDime();
        pot.addDime();
        System.out.println(pTwo);
System.out.println(pot);

        }
if(deposit == 4){
pTwo.subQuarter();
pot.addQuarter();
System.out.println(pTwo);
System.out.println(pot);

}

}         

                }

}

public static void main(String[] args){
System.out.println("Welcome to PennyWise!");
PennyWiseGame game = new PennyWiseGame();
game.turn();
}
}




My SetCoin class
public class SetCoin {
    
private double pennies;

	private double nickels;

	private double dimes;

	private double quarters;


	

    public SetCoin(double numPen, double numNic, double numDi, double numQu){

			pennies = numPen;

			nickels = numNic;

			dimes = numDi;

			quarters = numQu;

		}


	
	public void addPenny(){

			pennies++;

		}


	
	public void addNickel(){

			nickels++;
	
	}


	
	public void addDime(){

			dimes++;
	
	}


	
	public void addQuarter(){
				
			quarters++;
	
	}


	
	public void subPenny(){

			pennies--;
	
	}

	
	
		public void subNickel(){
		
			nickels--;
	
		}

	
	
		public void subDime(){
	
			dimes--;
	
		}

	
			
		public void subQuarter(){

			quarters--;

		}	
	
	
		public void setPen(double newPen){
	
			pennies = newPen;
	
		}
	

	
		public double getPen(){
	
			return pennies;

		}
	

	
		public void setNic(double newNic){

			nickels = newNic;
	
		}

	
	
		public double getNic(){
	
			return nickels;
	
		}
	

		
		public void setDi(double newDi){
	
			dimes = newDi;

		}

		
	
		public double getDi(){
	
			return dimes;
	
		}
	
	

		public void setQu(double newQu){

			quarters = newQu;

		}


	
	public double getQu(){
		
			return quarters;

		}

        public boolean isEmpty(){
        return pennies == 0 && (nickels == 0)&& (dimes == 0) && (quarters ==0);
        }


}


Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Penny Wise PRogram

Posted 23 January 2013 - 08:58 AM

The 3 pots are all SetCoin objects, so the toString() method should be a SetCoin method. Then, when you print a pot, the object's toString() method will be called. Make sense?
Was This Post Helpful? 0
  • +
  • -

#5 PaC-mAn336  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-January 13

Re: Penny Wise PRogram

Posted 23 January 2013 - 09:14 AM

Yes but when I tried that it would come back as an error I just want to display the current state of the pot and player after each turn
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7872
  • View blog
  • Posts: 13,354
  • Joined: 19-March 11

Re: Penny Wise PRogram

Posted 23 January 2013 - 09:16 AM

What was the error? Can you tell us what exactly the problem was?

Best if you can reproduce the error and show us the compiler output and the code that produced it.
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Penny Wise PRogram

Posted 23 January 2013 - 09:31 AM

Here's a simple, partial toString() method that if added to your current SetCoin class will provide you incomplete but acceptable results. You should be able to expand the toString() method to include the other coins in the current pot.
    public String toString()
    {
        return "Number of nickels = " + getNic();
    }


Was This Post Helpful? 0
  • +
  • -

#8 PaC-mAn336  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-January 13

Re: Penny Wise PRogram

Posted 23 January 2013 - 11:27 AM

It keeps telling that I need to override the default toString() method
Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Penny Wise PRogram

Posted 23 January 2013 - 11:32 AM

What is 'It' and how is it communicating to you? No, I don't think you're hearing voices or seeing dead people.

As Jon mentioned, it's always helpful to actually post your errors. In this case, I think you may be getting a warning or other helpful message, either from your IDE or from a compiler message on the console, because there is no REQUIREMENT to override the default toString() method, and therefore there wouldn't be an error.

Though I don't completely understand what you're seeing, perhaps changing what I posted earlier to as posted below will eliminate whatever you're getting:
    @Override
    public String toString()
    {
        return "Number of nickels = " + getNic();
    }

This post has been edited by GregBrannon: 23 January 2013 - 11:34 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1