Results 1 to 5 of 5
- 07-07-2012, 06:58 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
Quick question about a game of Pig (Die game) that I'm writing Code for
Hello, I'm writing code for a game of Pig and I can't seem to compile Player.java, something to do with the takeTurn method. If someone could look and see what's wrong, that would be fantastic. I'm also including the rest of my program, and if you want to look through that and offer any improvements/corrections that would be amazing.
Thanks so much!
Player Class:
Java Code:import java.lang.String; public abstract class Player { private int roundPts; private int gamePts; private String name; public Player(String n) { } public void takeTurn(PigDice) { takeTurn(dice); } public String toString() { return ("Points earned this round " + roundPts) + ("Total is" + (roundPts + gamePts)); } public void setGamePts(int Val) { gamePts += Val; } public void setRoundPts(int Val) { roundPts += Val; } public int getGamePts() { return gamePts; } public int getRoundPts() { return roundPts; } public abstract boolean beAPig(); }
Die Class:
Java Code:public class Die { private final int MAX = 6; // maximum face value private int faceValue; // current value showing on the die //----------------------------------------------------------------- // Constructor: Sets the initial face value. //----------------------------------------------------------------- public Die() { faceValue = 1; } //----------------------------------------------------------------- // Rolls the die and returns the result. //----------------------------------------------------------------- public int roll() { faceValue = (int)(Math.random() * MAX) + 1; return faceValue; } //----------------------------------------------------------------- // Face value mutator. //----------------------------------------------------------------- public void setFaceValue (int value) { faceValue = value; } //----------------------------------------------------------------- // Face value accessor. //----------------------------------------------------------------- public int getFaceValue() { return faceValue; } //----------------------------------------------------------------- // Returns a string representation of this die. //----------------------------------------------------------------- public String toString() { String result = Integer.toString(faceValue); return result; } }
Pair of Dice:
Java Code:public class PairOfDice { // instance data private Die die1; private Die die2; //------------------------------------------ // Constructor: Intializes two dice. //------------------------------------------ public void PairOfDice() { die1 = new Die(); die2 = new Die(); } //---------------------------------------------------------- // Rolls the pair by rolling each die. The sum is returned. //---------------------------------------------------------- public int roll() { return die1.roll() + die2.roll(); } public String toString() { return "[ " + die1.getFaceValue() + " : " + die2.getFaceValue() + " ]"; } public void setDie1(int val) { die1.setFaceValue(val); } public void setDie2(int val) { die2.setFaceValue(val); } public int getDie1() { return die1.getFaceValue(); } public int getDie2() { return die2.getFaceValue(); } }
PigDice
Java Code:public class PigDice extends PairOfDice { public PigDice() { super(); } public int roll() { int result = super.roll(); if(super.getDie1()==1||super.getDie2()==1) { result = 3; } if(super.getDie1()==1&&super.getDie2()==1) { result = 2; } return result; } }
Pig Game:
Java Code:import java.util.Scanner; public class PigGame { private Player player1, player2; private PigDice dice; public PigGame(Player p1, Player p2, PigDice pigDice) { System.out.println( "Welcome to the Pig Game"); PigDice dice = pigDice(); player1 = p1; player2 = p2; } public void playGame() { while (player1.getGamePts() < 100&&player2.getGamePts() < 100) player1.takeTurn(dice); if (player1.getGamePts() < 100) player2.takeTurn(dice); } public String toString() { // GUI makes this unnecessary } }
Human Class
Java Code:import java.util.Scanner; public class Human extends Player { public Human(String player) { super(player); } public boolean beAPig() { Scanner Scan = new Scanner(System.in); System.out.println("Would you like to roll again [y/n]? "); String response = Scan.nextLine(); boolean result; if (response.equals("y")) { result = true; } else result = false; return result; } }
Computer Class:
Java Code:public class Computer extends Player { public Computer(String player) { super(player); } public boolean sensible() { if(Player.getRoundPts() < 20) else Player.takeTurn(); } }Last edited by JosAH; 07-07-2012 at 10:21 PM. Reason: added [code] ... [/code] tags
- 07-07-2012, 06:59 PM #2
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
Re: Quick question about a game of Pig (Die game) that I'm writing Code for
This is a bit hard to read, I wasn't sure how to attach it any better though.
- 07-07-2012, 10:21 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: Quick question about a game of Pig (Die game) that I'm writing Code for
I fixed it for you.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-07-2012, 11:52 PM #4
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
Re: Quick question about a game of Pig (Die game) that I'm writing Code for
Haha sorry I can't tell did you fix the readability as well as the actual code or just the readability?
- 07-08-2012, 09:07 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: Quick question about a game of Pig (Die game) that I'm writing Code for
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
RPG game question
By SwEeTAcTioN in forum New To JavaReplies: 17Last Post: 07-08-2012, 05:56 AM -
Writing a Pong game - enhancing my default code.
By DeagleFace in forum New To JavaReplies: 6Last Post: 05-12-2012, 05:30 PM -
Complete Game Engine for beginner and intermediate game programmers
By rdjava in forum Java GamingReplies: 1Last Post: 06-02-2011, 09:29 AM -
game code for any game
By deathnote202 in forum Java GamingReplies: 4Last Post: 06-10-2010, 08:06 AM -
Quick question about this simple code..
By shroomiin in forum New To JavaReplies: 2Last Post: 11-10-2009, 05:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks