This topic is locked, you cannot edit posts or make further replies.  [ 36 posts ]  Go to page 1, 2, 3  Next
Back to Java 
Author Message
Occasionally has a life
User avatar

Joined: Mon May 04, 2009 10:16 am
Posts: 130
Location: In between a rock and a hard place
Hi all just wanted to update on the goings of my Java project please see below.

I need help with 2 things.

The first is I need to make a record of all throws and be able to see them when the game is done.

Second I need to make the game a 2 player but I have no clue how to do that.

Also if anyone has the time could you possibly have a look at the code and tell me why on the 4th and 10th dart throw it starts adding to the score instead of taking away lol, just spotted it today and I have no idea why its doing it.



Dart.java
/* Dart() class will ask the user for a dart number and if it's single, double or treble.
* The values returned will be the either the number
* asked for or the neighbour number on a dart board*/

public class Dart {

int requestedScored; //Stores the initial dart number entered by User.
int modifier; //Stores the value of Single, Double or Treble.
int totalScore; //Displays the value of what they have hit on the dartboard.



public Dart(int target){

/* Neighbour Array used to assigning the values left and right
* of a number on the dartboard
*(20 has the neighbour numbers 5 and 1) for each dart.*/


Neighbour[]neighbour = new Neighbour[20];

neighbour[0] = new Neighbour(20, 18);
neighbour[1] = new Neighbour(15, 17);
neighbour[2] = new Neighbour(17, 19);
neighbour[3] = new Neighbour(18, 13);
neighbour[4] = new Neighbour(12, 20);
neighbour[5] = new Neighbour(13, 10);
neighbour[6] = new Neighbour(19, 16);
neighbour[7] = new Neighbour(11, 16);
neighbour[8] = new Neighbour(14, 12);
neighbour[9] = new Neighbour(6, 15);
neighbour[10] = new Neighbour(14, 8);
neighbour[11] = new Neighbour(9, 5);
neighbour[12] = new Neighbour(4, 6);
neighbour[13] = new Neighbour(11, 9);
neighbour[14] = new Neighbour(10, 2);
neighbour[15] = new Neighbour(8, 7);
neighbour[16] = new Neighbour(3, 2);
neighbour[17] = new Neighbour(1, 4);
neighbour[18] = new Neighbour(7, 3);
neighbour[19] = new Neighbour(5, 1);

TextIO.putln("-------------------------------------------------------------------------");
TextIO.putln();

/* If the value entered by the user is within range then it
* will play otherwise it will display and error.
*/

do
{
TextIO.put("Enter you target between <1-20> or Bullseye <25 or 50>: " );
requestedScored = TextIO.getlnInt();

if (!(requestedScored >= 1 && requestedScored <= 20 || requestedScored == 25 || requestedScored == 50))// input is OK
TextIO.put("Invalid Input: " );

} while (!(requestedScored >= 1 && requestedScored <= 20 || requestedScored == 25 || requestedScored == 50));


TextIO.put("Type in <1> for Single, <2> for Double or <3> for Treble: " );
modifier = TextIO.getlnInt();
TextIO.putln();



TextIO.put("You have chosen: " );

switch (modifier) {
case 1: TextIO.put("Single ");
break;
case 2: TextIO.put("Double ");
break;
case 3: TextIO.put("Treble ");
break;
} // End Switch.

TextIO.put(requestedScored);
TextIO.putln();
TextIO.putln();


TextIO.put("You have hit: ");

int modifier = (int)(3*Math.random())+ 1; //Picks random modifier.

switch (modifier) {
case 1: TextIO.put("Single ");
break;
case 2: TextIO.put("Double ");
break;
case 3: TextIO.put("Treble ");
break;
} //End Switch.



int numberScored; //This will store either the neighbour value or the value that was entered by the user, depending on the maths random.

numberScored=getThrow(requestedScored, neighbour[requestedScored-1].getLeft(),neighbour[requestedScored-1].getRight()) ;

TextIO.putln(numberScored);

totalScore = numberScored * modifier;



TextIO.putln("Your total dart score is " + totalScore);
TextIO.putln();
TextIO.putln("Your remaining game score is " + (target-totalScore));




} // End Method.


public int getThrow(int dartThrow, int left, int right) {

int neighbour = (int)(8*Math.random())+ 1;


switch (neighbour) {
case 1: dartThrow = left;
break;
case 2: dartThrow = right;
break;
case 3: dartThrow = dartThrow;
break;

}
return dartThrow ;
}


public int gettotalScore(){
return totalScore;
}


public void display() {


}


}

Game.Java

public class Game {

Turn[] turns;
int totalScore = 501; // Starting value of each dart game.


public Game(){ // Game() will keep playing a game until score is reached 0.

int i=0;
turns=new Turn[300];
while (totalScore > 0){

turns[i]=new Turn(totalScore);
totalScore -= turns[i].gettotalScore();

i++; }

}


public void display(){

for (int i=0;i<turns.length;i++)
turns[i].display();
}


public void displaytotalScore(){

TextIO.putln("Your total score for this turn " + totalScore);

}
}



Javadarts.Java

public class JavaDarts {

/*JavaDarts class is used to play a game of darts, by calling methods from other Classes.
*There is two players which have three throws each turn.
*/


public static void main(String[] args) {

TextIO.putln("Welcome to JDarts!");
TextIO.putln();
TextIO.putln();
TextIO.putln("Enter <1> to Play 1 Player. " );
TextIO.putln("Enter <2> to Play 2 Player. " );
TextIO.putln("Enter <3> to Quit. " );

int modifier = TextIO.getlnInt();

TextIO.putln();


switch (modifier) {
case 1: TextIO.put("");
break;
case 2: TextIO.put("");
break;
case 3: System.exit(0);
break;


}


Game G1=new Game();
G1.display();

/*
*displaytotalScore() will display the results of the number
*you requested or a a neighbour number if you have missed.
*The total throw count will be shown at the end and how many turns you have had.
*You have the options to play again or terminate if entering 0.
*/


G1.displaytotalScore();

} //End Main.

} //End Class.

Neighbour.Java

/* Neighbour() used to assigning the values left and right
* of a number on the dartboard (20 has the neighbour numbers 5 and 1) for each dart.
*This will be used with a maths random, so the user won't always get the value he asked for */


public class Neighbour {

private int left;
private int right;


public Neighbour(int left, int right)


{
this.left = left;
this.right = right;
}

public int getLeft() //Left value of user entered dart number.
{
return this.left;


}

public int getRight()//Right value of user entered dart number.
{
return this.right;

}

}

Turn.Java

/* Turn() is used to play out three turns for each player
* And will keep doing this until one of two players have reached the score of 501
*
*/

public class Turn {

Dart[]Darts;
int totalScore = 0;

public Turn(int target){



Darts=new Dart[3];

for (int i=0;i<3;i++) {
Darts[i]=new Dart(target);
totalScore = Darts[i].gettotalScore();
target -= totalScore;
}


}

public void display(){

for (int i=0;i<3;i++)
Darts[i].display();
}

public void displaytotalScore(){

TextIO.putln("Your total score for this turn " + totalScore);
}

public int gettotalScore() {

return totalScore;

}

}

Any help on this would be fantastic guys thanks!


Kal

_________________
[color=#BF0000][b]Arctic Cooling Freezer 7 Pro
620W Corsair HX Series Modular SLi PSU
Intel Core 2 Quad-Core Q6600 G0 SLACR, 95W, S775, 2.40 GHz
asus maximus formula, iX38, S 775
ASUS GEFORCE EN8800 GT 512MB GDDR3


Fri Jan 15, 2010 7:31 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
OK, before I have a proper look I have to say that this has come on a hell of a long way.

Not read it properly but at least now I can get the gist of it from a quick skim read.

Nice one :D

OK, back to the code...

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Fri Jan 15, 2010 7:52 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
OK.

1. Recording throws.

Erm... I'll come back to this after a further look.

2. ... player (geddit :D)

ATM the main method creates a new game G1.

The first thing you need is a class called player and then the player get's it's own game.

i.e. main creates a player and player creates their own game.

Then you need main to alternate between players and take turns between each one.

When one of the gets to 0 then the main will end the game and announce a winner.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Fri Jan 15, 2010 7:58 pm
Profile WWW
Officially Mrs saspro
User avatar

Joined: Wed Jan 06, 2010 7:55 pm
Posts: 4955
Location: on the naughty step
Can you use [code] tags please to make it a bit more readable? I'll have a read through sometime :)


Fri Jan 15, 2010 8:24 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Fogmeister wrote:
1. Recording throws.

Erm... I'll come back to this after a further look.

List for each player as you can add to it.

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Fri Jan 15, 2010 8:48 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
finlay666 wrote:
Fogmeister wrote:
1. Recording throws.

Erm... I'll come back to this after a further look.

List for each player as you can add to it.

Yeah, I was thinking that but didn't want to go into it before doing the player but :D

But yeah, set up either a comma separated list or even better an ArrayList or something.

Then stick each throw into it.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Fri Jan 15, 2010 11:15 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Fri Apr 24, 2009 7:55 am
Posts: 7935
Location: Manchester.
TheFrenchun wrote:
Can you use code tags please to make it a bit more readable? I'll have a read through sometime :)

Ask and ye shall receive! Hope this helps :D
Code:
[b]Dart.java[/b]
/* Dart() class will ask the user for a dart number and if it's single, double or treble.
 * The values returned will be the either the number
 * asked for or the neighbour number on a dart board*/

public class Dart {
   
   int requestedScored; //Stores the initial dart number entered by User.
   int modifier;     //Stores the value of Single, Double or Treble.
   int totalScore;   //Displays the value of what they have hit on the dartboard.
   
   
   
   public Dart(int target){
      
       /* Neighbour Array used to assigning the values left and right
        * of a number on the dartboard
        *(20 has the neighbour numbers 5 and 1) for each dart.*/
      
      
        Neighbour[]neighbour = new Neighbour[20];

        neighbour[0] = new Neighbour(20, 18);
        neighbour[1] = new Neighbour(15, 17);
        neighbour[2] = new Neighbour(17, 19);
        neighbour[3] = new Neighbour(18, 13);
        neighbour[4] = new Neighbour(12, 20);
        neighbour[5] = new Neighbour(13, 10);
        neighbour[6] = new Neighbour(19, 16);
        neighbour[7] = new Neighbour(11, 16);
        neighbour[8] = new Neighbour(14, 12);
        neighbour[9] = new Neighbour(6, 15);
        neighbour[10] = new Neighbour(14, 8);
        neighbour[11] = new Neighbour(9, 5);
        neighbour[12] = new Neighbour(4, 6);
        neighbour[13] = new Neighbour(11, 9);
        neighbour[14] = new Neighbour(10, 2);
        neighbour[15] = new Neighbour(8, 7);
        neighbour[16] = new Neighbour(3, 2);
        neighbour[17] = new Neighbour(1, 4);
        neighbour[18] = new Neighbour(7, 3);
        neighbour[19] = new Neighbour(5, 1);
      
        TextIO.putln("-------------------------------------------------------------------------");
        TextIO.putln();
       
 /* If the value entered by the user is within range then it
  * will play otherwise it will display and error.
  */
 
           do 
           {
              TextIO.put("Enter you target between <1-20> or Bullseye <25 or 50>: " );
           requestedScored = TextIO.getlnInt();
           
           if (!(requestedScored >= 1 && requestedScored <= 20 || requestedScored == 25 || requestedScored == 50))// input is OK
              TextIO.put("Invalid Input:  " );
           
           } while (!(requestedScored >= 1 && requestedScored <= 20 || requestedScored == 25 || requestedScored == 50));
       
       
         TextIO.put("Type in <1> for Single, <2> for Double or <3> for Treble: " );
         modifier = TextIO.getlnInt();
         TextIO.putln();
         
       
         
         TextIO.put("You have chosen: " );
         
        switch (modifier) {
        case 1:   TextIO.put("Single ");
              break;
        case 2: TextIO.put("Double ");
              break;
        case 3: TextIO.put("Treble ");
              break;      
         } // End Switch.
         
         TextIO.put(requestedScored);   
         TextIO.putln();
         TextIO.putln();
         
         
         TextIO.put("You have hit: ");
       
         int modifier = (int)(3*Math.random())+ 1; //Picks random modifier.
         
       switch (modifier) {
       case 1:   TextIO.put("Single ");
             break;
       case 2: TextIO.put("Double ");
             break;
       case 3: TextIO.put("Treble ");
             break;
       } //End Switch.
         

       
        int numberScored; //This will store either the neighbour value or the value that was entered by the user, depending on the maths random.
        
        numberScored=getThrow(requestedScored, neighbour[requestedScored-1].getLeft(),neighbour[requestedScored-1].getRight()) ; 
          
        TextIO.putln(numberScored);
        
        totalScore = numberScored * modifier;
         
         
         
         TextIO.putln("Your total dart score is " + totalScore);
         TextIO.putln();
         TextIO.putln("Your remaining game score is " + (target-totalScore));
           
         
      
           
} // End Method.

   
   public int getThrow(int dartThrow, int left, int right) {

      int neighbour = (int)(8*Math.random())+ 1;

      
      switch (neighbour) {
         case 1:   dartThrow = left;
               break;
         case 2: dartThrow = right;
               break;
         case 3: dartThrow = dartThrow;
               break;

               }
            return dartThrow ;
                  }
   
   
   public int gettotalScore(){
      return totalScore;
   }


   public void display() {
      
      
   }


}

[b]Game.Java[/b]

public class Game {
   
Turn[] turns;
int totalScore = 501; // Starting value of each dart game.


   public Game(){  // Game() will keep playing a game until score is reached 0.
      
         int i=0;         
      turns=new Turn[300];
         while (totalScore > 0){
              
      turns[i]=new Turn(totalScore);
      totalScore -= turns[i].gettotalScore();
              
      i++;  }
   
         }
   
   
   public void display(){ 
      
      for (int i=0;i<turns.length;i++)
         turns[i].display();
   }
   
   
   public void displaytotalScore(){
      
      TextIO.putln("Your total score for this turn " + totalScore);
      
      }
}
   
   

[b]Javadarts.Java[/b]

public class JavaDarts {
      
 /*JavaDarts class is used to play a game of darts, by calling methods from other Classes.
  *There is two players which have three throws each turn.
  */   
   

    public static void main(String[] args) {
       
       TextIO.putln("Welcome to JDarts!");
        TextIO.putln();
        TextIO.putln();
        TextIO.putln("Enter <1> to Play 1 Player. " );
        TextIO.putln("Enter <2> to Play 2 Player. " );
        TextIO.putln("Enter <3> to Quit. " );

        int modifier = TextIO.getlnInt();
       
        TextIO.putln();
       
 
        switch (modifier) {
       case 1:   TextIO.put("");
             break;
       case 2: TextIO.put("");
             break;
       case 3: System.exit(0);
             break;
       
       
        }
       
       
  Game G1=new Game(); 
  G1.display();
 
 /*
  *displaytotalScore() will display the results of the number
  *you requested or a a neighbour number if you have missed.
  *The total throw count will be shown at the end and how many turns you have had.
  *You have the options to play again or terminate if entering 0.
  */

 
  G1.displaytotalScore();
   
    } //End Main.
   
} //End Class.

[b]Neighbour.Java[/b]

/* Neighbour() used to assigning the values left and right
  * of a number on the dartboard (20 has the neighbour numbers 5 and 1) for each dart.
  *This will be used with a maths random, so the user won't always get the value he asked for */


public class Neighbour {

   private int left;
   private int right;


public Neighbour(int left, int right)


{
   this.left = left;
   this.right = right;
}

public int getLeft() //Left value of user entered dart number.
{
   return this.left;


}   
   
public int getRight()//Right value of user entered dart number.
{
   return this.right;
   
   }

}

[b]Turn.Java[/b]

/* Turn() is used to play out three turns for each player
 * And will keep doing this until one of two players have reached the score of 501
 *
 */

public class Turn {

Dart[]Darts;
int totalScore = 0;
     
public Turn(int target){
   
   
      
   Darts=new Dart[3];
   
   for (int i=0;i<3;i++) {
   Darts[i]=new Dart(target);
   totalScore = Darts[i].gettotalScore();
   target -= totalScore;
   }
   

}

public void display(){
   
   for (int i=0;i<3;i++)
      Darts[i].display();
}

public void displaytotalScore(){
   
   TextIO.putln("Your total score for this turn " + totalScore);
   }

public int gettotalScore() {
   
   return totalScore;
   
}

}

_________________
okenobi wrote:
John's hot. No denying it. But he's hardly Karen now, is he ;)

John Vella BSc (Hons), PGCE - Still the official forum prankster and crude remarker :P
Sorry :roll:
I'll behave now.
Promise ;)


Sat Jan 16, 2010 10:36 am
Profile WWW
Occasionally has a life
User avatar

Joined: Mon May 04, 2009 10:16 am
Posts: 130
Location: In between a rock and a hard place
Hi guys need help with this Urgent!

I asked my lecturer a question as to why the darts game was not calculating the total throw properly and this is the answer I got, Can anyone clear this up for me because I still dont know what he means lol. Thanks.


The clue to the problem with the 4th dart is that it is the first dart of the second turn (similarly the 10th dart is the first dart of the fourth turn; and if you look again you will see that the problem also occurs with the first dart of the third turn, dart 7).

You create a new Turn object for each turn, and the constructor creates an array of three darts which handle the three throws for that turn. The Turn constructor takes the target (points remaining to be scored) as a parameter, and so does each Dart object. The Turn object declares an instance variable called totalscore, but this is a slightly misleading name as it is updated to the score of the last dart thrown each time round the for loop and this is dart score is deducted from the target for the next dart throw. This means that when the Game object calls turns[i].gettotalScore() the value that it gets back is what was scored on the third dart throw of the turn, and not the total of the three dart throws.

_________________
[color=#BF0000][b]Arctic Cooling Freezer 7 Pro
620W Corsair HX Series Modular SLi PSU
Intel Core 2 Quad-Core Q6600 G0 SLACR, 95W, S775, 2.40 GHz
asus maximus formula, iX38, S 775
ASUS GEFORCE EN8800 GT 512MB GDDR3


Sun Jan 17, 2010 2:26 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
kalisclark wrote:
Hi guys need help with this Urgent!

You really should be doing your own work... essentially this is ghosting and frowned upon by universities along with collusion and plagiarism

kalisclark wrote:
You create a new Turn object for each turn, and the constructor creates an array of three darts which handle the three throws for that turn. The Turn constructor takes the target (points remaining to be scored) as a parameter, and so does each Dart object. The Turn object declares an instance variable called totalscore, but this is a slightly misleading name as it is updated to the score of the last dart thrown each time round the for loop and this is dart score is deducted from the target for the next dart throw. This means that when the Game object calls turns[i].gettotalScore() the value that it gets back is what was scored on the third dart throw of the turn, and not the total of the three dart throws.


Why make a new turn object for each turn? you REALLY don't need it, all you need is a list for each player for darts thrown

get total score is obtained from adding all the members in the list, you add a new item to the list for each dart thrown, if valid you add the score, if invalid you add 0 (such as being on 8 left and hitting a 10)

An object for a turn is quite an ugly way to do it... all you needed is a while loop for
Code:
bool finished = false;
while(!finished)
{
 // player 1 turn
if player1 score == 0
finished = true;
break
else
//player 2 turn
if player2 score == 0
finished = true;
break
}


then in each turn you calculate the remaining score (or have an int on hand to calculate it quickly) process the turn for the dart then update the total after each dart. you should calculate the score after each dart and on 0 break from the loop as the game is finished.

In all honesty I don't think you should be passing in the target in the constructor, especially in the dart. You should pass in a reference to the player and get the variable from functions in the player, otherwise you could have issues as IF you have coded defensively you should not be able to modify the player score directly and must use a function to do so.

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Sun Jan 17, 2010 3:51 pm
Profile
Occasionally has a life
User avatar

Joined: Mon May 04, 2009 10:16 am
Posts: 130
Location: In between a rock and a hard place
I am doing my own work!

I just needed someone to explain that passage in plainer english because no matter how I read it I wasnt understanding it. I also Just need some opinions on how the programme is coming along and if there is anything out of place. I am definatly not asking anyone to do the work for me, just asking to shed some light on the way Java itself is done. Thanks for the explanation I appreciate the clarification, but please dont think I am on here to copy other peoples work, thats not what I am trying to do, I just have trouble trying to imagin some of these java references without seeing them.

Thanks

Kal

_________________
[color=#BF0000][b]Arctic Cooling Freezer 7 Pro
620W Corsair HX Series Modular SLi PSU
Intel Core 2 Quad-Core Q6600 G0 SLACR, 95W, S775, 2.40 GHz
asus maximus formula, iX38, S 775
ASUS GEFORCE EN8800 GT 512MB GDDR3


Sun Jan 17, 2010 4:02 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Jun 18, 2009 5:10 pm
Posts: 5836
kalisclark wrote:
You create a new Turn object for each turn, and the constructor creates an array of three darts which handle the three throws for that turn.

The Turn constructor takes the target (points remaining to be scored) as a parameter, and so does each Dart object.
__________________
|
| The Turn object declares an instance variable called totalscore,
|
| this is a slightly misleading name
|
| it is updated to the score of the last dart thrown each time round the for loop
|
| this is deducted from the target for the next dart throw.
___________________________

^^^^^
This
means that when the Game object calls turns[i].gettotalScore() the value that it gets back is what was scored on the third dart throw of the turn, and not the total of the three dart throws.

Edited for clarity

He's given you some whopper clues there buddy. Sorry I can't help more - I program in Ada, VHDL & Assembly not Java. :oops:

_________________
Jim

Image


Sun Jan 17, 2010 10:48 pm
Profile
Occasionally has a life
User avatar

Joined: Mon May 04, 2009 10:16 am
Posts: 130
Location: In between a rock and a hard place
Hi Guys I am about to shoot myself in the brain here, can anyone see why target is not getting passed on to bustScore?

I have tried but when the game goes to negative numbers and issues bust, it should jump back to the previous score before it went below zero and its just not happening!


totalScore = numberScored * modifier;

TextIO.putln("Your total dart score is " + totalScore);
TextIO.putln();

target = target - totalScore;

TextIO.putln("Your remaining game score is " + (target));

if (target < 0){
bustScore = target += totalScore;

TextIO.putln("--------------------------------------------------------------------------------");
TextIO.putln(" YOU HAVE GONE BUST!");
TextIO.putln(" YOU MUST FINISH ON 0 EXACTLY!");
TextIO.putln(" YOU HAVE " + (bustScore) + " REMAINING!");
TextIO.putln("--------------------------------------------------------------------------------");
bustScore = target;
return;

} // End if.


Cheers

Kal

_________________
[color=#BF0000][b]Arctic Cooling Freezer 7 Pro
620W Corsair HX Series Modular SLi PSU
Intel Core 2 Quad-Core Q6600 G0 SLACR, 95W, S775, 2.40 GHz
asus maximus formula, iX38, S 775
ASUS GEFORCE EN8800 GT 512MB GDDR3


Mon Jan 18, 2010 7:58 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
What's with
Code:
bustScore = target += totalScore;
...
bustScore = target;


Why not do something like:

Code:
if((target-totalScore)<0){
    //BUST IO
}
else if ((target-totalScore) == 0) {
    //WIN IO
}
else {
    target -= totalScore;
}


Why change a variable then attempt to change it back?

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Mon Jan 18, 2010 8:07 pm
Profile
Occasionally has a life
User avatar

Joined: Mon May 04, 2009 10:16 am
Posts: 130
Location: In between a rock and a hard place
Thanks EddArmitage!

Unfortunatly it still refuses to work and I cant for the life of me figure out why. I have put the main class and the dart class for you to have a closer look if you dont mind mate.

I appreciate any clues you can pass my way coz this needs to be done asap and I am about to give up :cry:


Main
public class JavaDarts {

/*JavaDarts class is used to play a game of darts, by calling methods from other Classes.
*There is two players which have three throws each turn. You are able to Replay a game that you have played.
*/


public static void main(String[] args) {

TextIO.putln("Welcome to JDarts!");
TextIO.putln();
TextIO.putln();
TextIO.putln("Enter <1> to Play JDarts. " );
TextIO.putln("Enter <2> to Quit. " );

int menu = TextIO.getlnInt(); // Selects options that are given in the Menu.

TextIO.putln();


switch (menu) {
case 1: TextIO.put("");
break;
case 2: TextIO.put("");
break;
case 3: System.exit(0);
break;


} //End switch case.


Game G1=new Game();

TextIO.putln();
TextIO.putln("You Win! Congratulations!");
TextIO.putln();
TextIO.putln("--------------------------------------------------------------------------------");
TextIO.putln("Press <1> to see you Game History.");
TextIO.putln("Press <2> to Quit.");


int menu2 = TextIO.getlnInt(); // Selects options that are given in the Menu.

TextIO.putln();


switch (menu2) {
case 1: TextIO.put("");
break;
case 2: System.exit(0);
break;

}

G1.display();






} //End Main.

} //End Class.

Dart Class


/* Dart() class will ask the user for a dart number and if it's single, double or treble.
* The values returned will be the either the number
* asked for or the neighbour number on a dart board*/

public class Dart {


int modifier = 1; //Stores the value of Single, Double or Treble.
int totalScore; //Displays the value of the the modifier * requestedScore(or numberScored).
int numberScored; //This will store either the neighbour value or the value that was entered by the user, depending on the maths random.
int bustScore = 0;
/* Dart() is used to get the dart information from the user, this data might be changed
* it is then displayed at the end of a throw to show what the user got and how many remaining
* points they have left to complete the game
*/

public Dart(int target){




Neighbour[]neighbour = new Neighbour[20];

neighbour[0] = new Neighbour(20, 18);
neighbour[1] = new Neighbour(15, 17);
neighbour[2] = new Neighbour(17, 19);
neighbour[3] = new Neighbour(18, 13);
neighbour[4] = new Neighbour(12, 20);
neighbour[5] = new Neighbour(13, 10);
neighbour[6] = new Neighbour(19, 16);
neighbour[7] = new Neighbour(11, 16);
neighbour[8] = new Neighbour(14, 12);
neighbour[9] = new Neighbour(6, 15);
neighbour[10] = new Neighbour(14, 8);
neighbour[11] = new Neighbour(9, 5);
neighbour[12] = new Neighbour(4, 6);
neighbour[13] = new Neighbour(11, 9);
neighbour[14] = new Neighbour(10, 2);
neighbour[15] = new Neighbour(8, 7);
neighbour[16] = new Neighbour(3, 2);
neighbour[17] = new Neighbour(1, 4);
neighbour[18] = new Neighbour(7, 3);
neighbour[19] = new Neighbour(5, 1);

TextIO.putln("-------------------------------------------------------------------------");
TextIO.putln();



/* Do this while it's not within the boundaries of the Dartboard, when boundaries have been met then break.*/


do
{
TextIO.put("Enter you target between <1-20> or Bullseye <25 or 50>: " );
numberScored = TextIO.getlnInt();

if (!(numberScored >= 1 && numberScored <= 20 || numberScored == 25 || numberScored == 50)) // Within the boundaries of Dartboard.
TextIO.put("Invalid Input: " );

if (numberScored == 25)
TextIO.putln("You have hit 25!");

if (numberScored == 50)
TextIO.putln("You have hit bullseye!!!");


} while (!(numberScored >= 1 && numberScored <= 20 || numberScored == 25 || numberScored == 50));


if (numberScored != 50 && numberScored != 25) { // If numberScored is not 50 or 25 then carry out this code.



TextIO.put("Type in <1> for Single, <2> for Double or <3> for Treble: " );
modifier = TextIO.getlnInt();
TextIO.putln();



TextIO.put("You have chosen: " );

switch (modifier) {
case 1: TextIO.put("Single ");
break;
case 2: TextIO.put("Double ");
break;
case 3: TextIO.put("Treble ");
break;
} // End Switch.

TextIO.put(numberScored);
TextIO.putln();
TextIO.putln();


TextIO.put("You have hit: ");

// int modifier = (int)(3*Math.random())+ 1; //Picks random modifier.

switch (modifier) {
case 1: TextIO.put("Single ");
break;
case 2: TextIO.put("Double ");
break;
case 3: TextIO.put("Treble ");
break;
} //End Switch.

} // End If Statement.

if (numberScored != 50 && numberScored != 25 ) { // If numberScored is not 50 or 25 then carry out this code.

numberScored=getThrow(numberScored, neighbour[numberScored-1].getLeft(),neighbour[numberScored-1].getRight()) ;

TextIO.putln(numberScored);

} // End If Statement.


totalScore = numberScored * modifier;

TextIO.putln("Your total dart score is " + totalScore);
TextIO.putln();


if ((target-totalScore)<0){

TextIO.putln("--------------------------------------------------------------------------------");
TextIO.putln(" YOU HAVE GONE BUST!");
TextIO.putln(" YOU MUST FINISH ON 0 EXACTLY!");
TextIO.putln(" YOU HAVE " + (target) + " REMAINING!");
TextIO.putln("--------------------------------------------------------------------------------");


} // End if.

else if ((target - totalScore)==0){
TextIO.putln("Good Job!");
}
else {
TextIO.putln("Your remaining game score is " + (target -= totalScore));
}

} // End Dart() method.

/* getThrow() will assign the numberScored either the left, right or initial value on a dartboard, once the numberScored
* is changed this will be returned to the Dart() method so it can be changed by the modifier.
*/

public int getThrow(int dartThrow, int left, int right) {

// int neighbour = (int)(8*Math.random())+ 1;

int neighbour = 3;

switch (neighbour) {
case 1: dartThrow = left;
break;
case 2: dartThrow = right;
break;
case 3: dartThrow = dartThrow;
break;

}
return dartThrow ;
}


/*Returns the totalScore*/

public int gettotalScore(){
return totalScore;
} // end gettotalScore() method.


/*
*display() will display all the results of each dartThrow you have made during the game. this will be displayed at
*the end of the game, so you can see your what darts you have hit to win the game.
*/

public void display() {



TextIO.putln();
TextIO.putln();
TextIO.putln("------------------------------------------------------------------------");
TextIO.putln("------------------------------------------------------------------------");
TextIO.putln();
TextIO.putln(" You hit dart: " + numberScored);
TextIO.putln(" Your modifier was: " + modifier);
TextIO.putln(" Your total dart score for this dartThrow is: " + totalScore);
TextIO.putln();


} // End display() method.


} // End class.

_________________
[color=#BF0000][b]Arctic Cooling Freezer 7 Pro
620W Corsair HX Series Modular SLi PSU
Intel Core 2 Quad-Core Q6600 G0 SLACR, 95W, S775, 2.40 GHz
asus maximus formula, iX38, S 775
ASUS GEFORCE EN8800 GT 512MB GDDR3


Mon Jan 18, 2010 8:31 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
That is why you keep the turn score

If you bust out your score goes back to what it was before the turn started
You must also finish on a double or the bull. you don't have to win on your final dart. Your game clearly doesn't allow this.

Do you know the rules of the game you are making? It doesn't sound like it so your functions and sanity checks do not seem very well planned as they are fuzzier than needed. The more tightly you can specify what something does the easier it is to write as you have identifiable targets. Your code should be SMART
Simple
Manageable
Arranged in a logical way
Reliable
Testable

creating spare variables and setting variables multiple times in a function is not coding to that guideline. Until you have the basics down, you are making this a lot more complicated for yourself than you need to.

I also suggest you comment your functions a LOT to say exactly what you want to do BEFORE you write any code, and use code tags as I really can't be arsed to copy all your text into a notepad++ document just to get indentation. You only comment your code to state the end of the if statement, nothing else. You really should FORCE yourself into good practices now, as learning good practice at a later date is a lot more difficult, it took me at least 6 months working in a business basically having to rewrite anything that didn't comply.

Also you are using the do loop wrong, should be a while. If you don't understand why you need to re-read your notes and on the language as to what each does

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Mon Jan 18, 2010 8:37 pm
Profile
Display posts from previous:  Sort by  
This topic is locked, you cannot edit posts or make further replies.   [ 36 posts ]  Go to page 1, 2, 3  Next

Who is online

Users browsing this forum: No registered users and 28 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.