Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By SurfMan

Thread: hi guys! help!

  1. #1
    mikelmb is offline Member
    Join Date
    May 2014
    Posts
    7
    Rep Power
    0

    Smile hi guys! help!

    hi everyone! I had this programming project about a dart game and I did what I could but I didn't work.
    my code didn't work. can you guys help me out? the code is attached.
    thanks
    Attached Files Attached Files

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: hi guys! help!

    Few things before we start:
    1) Nobody here is going to download some zipfile with code. Copy/paste it here. This forum has excellent features for displaying indented and formatted code. Hint: use [code][/code] tags.
    2) Your title is not really descriptive of your problem.
    3) "Didn't work" is not good enough. Show the actual error message, stacktrace, and whatever you need to describe your problem in detail.
    4) We love to work with SSCCE's. If you make your problem small enough to fit in a SSCCE, then it's even easier to solve.

    Now try again.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    mikelmb is offline Member
    Join Date
    May 2014
    Posts
    7
    Rep Power
    0

    Default Re: hi guys! help!

    i know, the code is too long. but anyway, I'll post it. I'm new to java, so bare with me.
    here is my code:


    public class Darts {

    // data structure for recording the score of throwing a single dart
    static class Throw implements Serializable{
    public int numberScored;
    public String modifier="";
    public int totalScore;
    }

    // turn stores a final score
    // and holds 3 throws
    static class Turn implements Serializable
    {
    public int totalThrows;
    public Throw[] ts = new Throw[3];

    Turn()
    {
    ts[0] = new Throw();
    ts[1] = new Throw();
    ts[2] = new Throw();
    }
    }

    // player class stores turns
    static class Player implements Serializable
    {
    public Turn[] turns = new Turn[100];
    public int numTurns = 0;
    }


    // simulate the throwing of a single dart.
    // receives Target throw
    public static Throw simulateThrow(String target)
    {
    String[] fields = target.split("\\s+");

    Throw t = new Throw();
    try{


    if(fields.length > 0)
    t.numberScored = Integer.parseInt(fields[0]);
    if(fields.length > 1)
    {
    String modifier = fields[1].toUpperCase();

    if(modifier.equals("D"))
    t.modifier = "DOUBLE";
    else if(modifier.equals("S"))
    t.modifier = "SINGLE";
    else if(modifier.equals("T"))
    t.modifier = "TREBLE";
    }

    // calculate total score
    if("DOUBLE".equals(t.modifier))
    t.totalScore = t.numberScored * 2;
    else if("TREBLE".equals(t.modifier))
    t.totalScore = t.numberScored * 3;
    else t.totalScore = t.numberScored;

    return t;

    }
    catch (Exception ex)
    {
    TextIO.putln("bad turn entrerred");
    return null;
    }

    }

    // display the result of a single Throw.
    public static void displayThrow(Throw t)
    {

    TextIO.putln("Number Scored: " + t.numberScored);
    TextIO.putln("Modifier: " + t.modifier);
    TextIO.putln("Total Score: " + t.totalScore);
    TextIO.putln("");


    }

    // display all throws in a turn
    public static void displayThrows(Turn turn)
    {
    for(int i=0;i<turn.ts.length;i++)
    {
    if(turn.ts[i] != null)
    {
    displayThrow(turn.ts[i]);
    }
    }
    }

    // gives a user a throw by asking them to enter a target for
    // the dart to be thrown. For example, the user could enter “20 D“ to aim for a
    // double 20.
    // call's the function simulateThrow to perform the throw and then calls
    // displayThrow to display the result, and it should return what was hit (e.g. {20, DOUBLE}).
    // return total score
    public static Throw enterThrow()
    {
    Throw t = null;
    String target = "";

    do{

    TextIO.putln("Enter Target score modifier: ");

    // String target = TextIO.getln();
    target = strategyThrow();

    t = simulateThrow(target);

    }while(t == null);

    displayThrow(t);

    return t;

    }

    // simulate a complete turn
    public static int completeTurn (Turn turn,int remainingScore)
    {

    Throw t = enterThrow();
    turn.totalThrows += t.totalScore;
    turn.ts[0] = t;

    remainingScore = remainingScore - t.totalScore;
    if(remainingScore <= 0) return remainingScore;

    t = enterThrow();

    turn.totalThrows += t.totalScore;
    turn.ts[1] = t;

    remainingScore = remainingScore - t.totalScore;;
    if(remainingScore <= 0) return remainingScore;

    t = enterThrow();

    turn.totalThrows += t.totalScore;
    turn.ts[1] = t;

    remainingScore = remainingScore - t.totalScore;
    if(remainingScore <= 0) return remainingScore;

    return remainingScore;
    }

    // display result of turn
    public static void displayTurn(Turn turn, int remainingScore)
    {
    TextIO.putln("Total value of throws: " + turn.totalThrows);
    if (remainingScore==0){
    TextIO.putln("Player wins");}
    else if (remainingScore < 0){
    TextIO.putln("BUST");}
    }

    // simulate throwing of a real dart
    public static String strategyThrow()
    {
    // Numbers on dartboard clockwise from 19, outer bull and inner bull.
    int dartThrowTarget [] = {19,7,16,8,11,14,9,12,5,20,1,18,4,13,6,10,15,2,17, 3,25,50};
    int result = -1;
    int index = 0;
    int actualHit = (int)(Math.random() * dartThrowTarget.length);
    while (index < dartThrowTarget.length){
    if (actualHit==(dartThrowTarget[index])){
    result = index;
    break;
    }
    index++;
    }

    String modifier = "S";
    if(Math.random() < .5) modifier = "D";

    if(index >= dartThrowTarget.length)
    return "0";
    else
    return dartThrowTarget [index] + " " + modifier;

    }

    /************************************************** **********************************************/

    // players
    private static Player player1 = new Player();
    private static Player player2 = new Player();


    public static void main(String[] args) {



    boolean valid = false;

    do {
    menuDisplay();
    int option = TextIO.getlnInt();
    switch (option) {
    case 1: TextIO.putln("1 was chosen");
    playGame();
    break;
    case 2: TextIO.putln("2 was chosen");
    replayGame();
    break;
    case 3: TextIO.putln("3 was chosen");
    saveGame();
    break;
    case 4: TextIO.putln("4 was chosen");
    readGame();
    replayGame();
    break;
    case 5: valid = true; TextIO.putln("Goodbye.");
    break;
    default: TextIO.putln("Invalid option.");
    break;
    }
    } while (!valid);

    } // end main ()


    // Displays a menu of 5 options.
    public static void menuDisplay () {
    TextIO.putln("1 - Start a new game");
    TextIO.putln("2 - Replay the last game");
    TextIO.putln("3 - Save the game just played");
    TextIO.putln("4 - Replay the last saved game");
    TextIO.putln("5 - Quit");
    TextIO.putln("Please enter an option 1-5: ");

    } // end menuDisplay

    // play game
    public static void playGame () {

    player1 = new Player();
    player2 = new Player();

    TextIO.putln("Player 1 plays");
    int score1 = playPlayer(player1);

    TextIO.putln("Player 2 plays");
    int score2 = playPlayer(player2);

    System.out.println("Score player 1 is: " + score1);
    System.out.println("Score player 2 is: " + score2);
    }

    public static int playPlayer (Player player) {

    int score = 501;
    player.turns[player.numTurns] = new Turn();
    score = completeTurn (player.turns[player.numTurns],score);
    displayTurn(player.turns[player.numTurns],score);
    player.numTurns++;

    while(score > 0)
    {
    player.turns[player.numTurns] = new Turn();
    score = completeTurn (player.turns[player.numTurns],score);
    displayTurn(player.turns[player.numTurns],score);
    player.numTurns++;
    }

    return score;
    }



    // replay game
    public static void replayGame()
    {

    TextIO.putln("Player 1 re-plays");
    int score1 = replayPlayer(player1);
    TextIO.putln("Player 2 re-plays");
    int score2 = replayPlayer(player2);

    System.out.println("Score player 1 is: " + score1);
    System.out.println("Score player 2 is: " + score2);

    }


    // replay player
    public static int replayPlayer(Player player)
    {
    int score = 501;

    for(int i=0;i<player.numTurns;i++)
    {
    Turn turn = player.turns[i];
    displayThrows(turn);
    score -= turn.totalThrows;
    displayTurn(turn,score);

    }
    return score;
    }

    // write game to file
    public static void saveGame()
    {

    try{

    FileOutputStream fout = new FileOutputStream("players.bin");
    ObjectOutputStream oos = new ObjectOutputStream(fout);
    oos.writeObject(player1);
    oos.writeObject(player2);
    oos.close();
    TextIO.putln("Done");

    }catch(Exception ex){
    TextIO.putln(ex.getMessage());
    }
    }

    // read game from file
    public static void readGame()
    {


    try{

    FileInputStream fin = new FileInputStream("players.bin");
    ObjectInputStream ois = new ObjectInputStream(fin);
    player1 = (Player) ois.readObject();
    player2 = (Player) ois.readObject();
    ois.close();

    TextIO.putln("Done");

    }catch(Exception ex){
    TextIO.putln(ex.getMessage());
    }
    }

    } // end class

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: hi guys! help!

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    Also:
    the import statements are missing
    the class TextIO is not defined
    Last edited by Norm; 07-10-2014 at 10:31 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: hi guys! help!

    Also: you still didn't describe what the problem exactly is. Again, I don't care about "doesn't work". And I am not going to read through 6 screens of unformatted code, looking for a "<------------- error here".

    You don't tell your doctor "it hurts" and then say nothing else. The doctor is not going to guess where it hurts, you have to tell him...
    gimbal2 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: hi guys! help!

    Quote Originally Posted by SurfMan View Post
    You don't tell your doctor "it hurts" and then say nothing else. The doctor is not going to guess where it hurts, you have to tell him...
    Nice one.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: hi guys! help!

    Quote Originally Posted by SurfMan View Post
    You don't tell your doctor "it hurts" and then say nothing else.
    Except when your mouth hurts; or your vocal chords ...

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: hi guys! help!

    That's it: Python activated.

    https://www.youtube.com/watch?v=evlrs5Bi_6E
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: hi guys! help!

    Quote Originally Posted by JosAH View Post
    Except when your mouth hurts; or your vocal chords ...

    kindest regards,

    Jos
    You can always text him...

    hi guys! help!-doc_screenie.png
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: hi guys! help!

    Quote Originally Posted by SurfMan View Post
    You can always text him...
    But what if you also ate your fingers? A cell phone is way too small to operate with your toes or your nose ...

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: hi guys! help!

    Quote Originally Posted by JosAH View Post
    But what if you also ate your fingers? A cell phone is way too small to operate with your toes or your nose ...

    kindest regards,

    Jos
    Then you would't be able to properly program anymore, so problem solved!
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: hi guys! help!

    Quote Originally Posted by SurfMan View Post
    Then you would't be able to properly program anymore, so problem solved!
    I could've been programming by banging a certain body part on my keyboard ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: hi guys! help!

    Quote Originally Posted by JosAH View Post
    I could've been programming by banging a certain body part on my keyboard ...
    Clever dicky.

    Btw, there are people who do programming without any kind of typing at, but rather with special software controlled by the eyes or even the brain. We've even had someone like that visit this forum. These are crazy times of technological possibilities.
    Last edited by gimbal2; 07-11-2014 at 01:19 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: hi guys! help!

    Quote Originally Posted by JosAH View Post
    I could've been programming by banging a certain body part on my keyboard ...

    kind regards,

    Jos
    That's why female programmers have a one key advantage...
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Hey Guys!
    By 6footGeek in forum Introductions
    Replies: 1
    Last Post: 01-26-2014, 04:04 PM
  2. Hey guys!
    By Rewl in forum Introductions
    Replies: 0
    Last Post: 01-06-2011, 02:33 AM
  3. Please help me guys. You must know how.
    By I Want To Kill Java in forum Java Applets
    Replies: 4
    Last Post: 12-30-2010, 03:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •