Results 1 to 15 of 15
  1. #1
    conor147 is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default java program simulating darts

    i need to write a program that does this


    You are required to write a simple program that simulates a game of darts. In this, two (or more)
    players take turns to “throw“ three darts to reduce their score from 501 down to zero. Each dart
    thrown will be simulated by a pair of numbers – the first is the number scored (1-20, 25, 50), the
    second indicates a single double or treble if one is possible (note that 25 or 50 are always singles, so
    the first number will dictate whether the second value is necessary). After three throws, the full score
    will be deducted from 501. The first player to zero wins. However, as with real darts, the final dart
    must be a double or bullseye (50), and must exactly clear the remaining score. If too much is scored,
    the player is out for that turn, and must try again after the other players have had a turn.
    On-screen, each turn should generate:
    1. Each of the three Throws’ scores, with doubles and trebles included
    2. The total score achieved in a Turn, by summing all three
    3. The remaining score, having deducted from the total at each turn
    4. An indication of ‘bust’ if the player ‘s darts total more than the remaining score, or ‘win’ if the
    player has got out with a double or bullseye (note that if the player gets out on the first or
    second dart, this is a win – the remaining dart(s) do not need to be thrown).

    can anyone advise me on what structure to use? or what algorithms would need to be included. has anyone already wrote a program like this?

    PLEASE HELP

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    I don't understand how the second number is produced. Anyhow, I'll help you start it off but like always, will not do your homework.
    Java Code:
    public class Darts {
         .....
    }
    that would start you off with your class header.

    1. Each of the three Throws’ scores, with doubles and trebles included
    You could make it print a toString method from your class after every turn.
    Java Code:
    public String toString() {
    System.out.println(...);
    }
    2. The total score achieved in a Turn, by summing all three
    a method that would get total score.
    You could print out the score for that turn in the method that will be used to throw the darts.

    3. The remaining score, having deducted from the total at each turn
    Java Code:
    public int getScore() {
    ...
    }
    4. An indication of ‘bust’ if the player ‘s darts total more than the remaining score, or ‘win’ if the
    player has got out with a double or bullseye (note that if the player gets out on the first or
    second dart, this is a win – the remaining dart(s) do not need to be thrown).
    You will most likely need a boolean method for this. true if "win" and false if "bust" or vise-versa.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    heh I wrote a dart counter program for my dad a year or two ago; even told you the checkouts you could get. Guess I could try to see how I did it although its quite old.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by conor147 View Post
    i need to write a program that does this

    ....

    can anyone advise me on what structure to use? or what algorithms would need to be included. has anyone already wrote a program like this?

    PLEASE HELP
    What have you done so far and where exactly are you stuck at?

  5. #5
    conor147 is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    i basically just need to convert this (python) into java, but i dont know java much at all

    '''import cgi

    from google.appengine.api import users
    from google.appengine.ext import webapp
    from google.appengine.ext.webapp.util import run_wsgi_app
    from google.appengine.ext import db
    from datetime import datetime, time
    import rest
    '''
    import random

    dartboard = [20,1,18,4,13,6,10,15,2,17,3,19,7,16,8,11,14,9,12,5]

    class Dart():

    def __init__(self, num, mod):
    self.value = num
    self.modifier = mod
    self.not_always_a_hit()
    print self

    def not_always_a_hit(self):
    # accuracy score as a rangom number 0..1
    r = random.random()
    # 70% chance of a hit...
    if r<0.7:
    # no need to modify the score...
    return
    # 20% chance of a miss-by-one...
    if r<0.90:
    self.miss_by_one()
    return
    # 10% chance of a miss-by-two...
    self.miss_by_two()

    def miss_by_one(self):
    dir = random.choice('udlr')
    if dir=='u':
    self.modifier = self.modifier + 1
    if self.modifier==4:
    self.modifier=1
    if dir=='d':
    self.modifier = self.modifier - 1
    if self.modifier==0:
    self.modifier=3
    if dir=='l': # bed to the left...
    pos = dartboard.index(self.value) - 1
    self.value = dartboard[pos]
    if dir=='r': # bed to the right
    pos = dartboard.index(self.value) + 1
    if pos == len(dartboard):
    pos = 0
    self.value = dartboard[pos]

    def miss_by_two(self):
    dir = random.choice('udlr')
    if dir in 'ud':
    # Up or Down - change double or treble to single
    if self.modifier==2:
    self.modifier = 1
    elif self.modifier == 3:
    self.modifier = 1
    else:
    # Aim for a single - randomly hit double or treble...
    self.modifier=int(random.choice('23'))
    if dir=='l': # bed to the left...
    pos = dartboard.index(self.value) - 2
    self.value = dartboard[pos]
    if dir=='r': # bed to the right...
    pos = dartboard.index(self.value) + 2
    if pos >= len(dartboard):
    pos = pos-len(dartboard)
    self.value = dartboard[pos]

    def __str__(self):
    if self.modifier == 1:
    modf = "Single "
    elif self.modifier == 2:
    modf = "Double "
    else:
    modf = "Treble "
    return '%s%d=%d' % (modf, self.value, self.score())

    def score(self):
    return self.modifier*self.value


    class Turn():

    def __init__(self):
    self.darts = []

    def throw(self, dart):
    self.darts.append(dart)
    return dart

    def score(self):
    total=0
    for d in self.darts:
    total = total + d.score()
    return total

    def __str__(self):
    s="\nDarts thrown:\n"
    if len(self.darts) > 0:
    for d in self.darts:
    s = s + str(d) + '\n'
    s = s + "Score = " + str(self.score()) + '\n'
    else:
    s = "\nNo score\n"
    return s

    class PlayerGame():
    def __init__(self):
    self.remaining=501

    def makeDart(self, inline):
    #Is there a space in this?
    if ' ' in inline:
    bits = inline.split(' ')
    if len(bits)==2:
    #suggests this is of form T 20...
    bits[0] = bits[0].upper()
    if bits[0]=='S':
    #print "Single " + bits[1]
    m=1
    elif bits[0]=='D':
    #print "Double " + bits[1]
    m=2
    elif bits[0]=='T':
    #print "Treble " + bits[1]
    m=3
    else:
    dart = Dart(0,0)
    if int(bits[1]) in dartboard:
    dart = Dart(int(bits[1]), m)
    else:
    dart = Dart(0,0)
    else:
    if int(inline)==25 or int(inline)==50:
    dart = Dart(int(inline), 1)
    else:
    dart = Dart(0,0)
    return dart

    def play(self):
    r = self.remaining
    t = Turn()
    for arrow in range(3):
    i = raw_input("Enter throw (e.g. T 20 for treble 20): ")
    th = t.throw(self.makeDart(i))
    if th.score() == 0:
    continue
    if r < th.score():
    #Bust - return with no score...
    return # Turn()
    elif r == th.score():
    #Out - was the last dart a double?...
    if th.modifier == 2:
    #Yes - rack it up...
    self.remaining = 0
    return th
    else:
    #No - return no score...
    return # Turn()
    else:
    #Knock off the score of this dart...
    r = r - th.score()
    #Subtract the turn's score from the remaining total...
    self.remaining = self.remaining - t.score()
    #And return the turn for reporting...
    return t

    def testArrows():
    #General test to check random misses etc...
    for d_no in range(100):
    d = Dart(20, 3)


    def main():
    p = PlayerGame()
    while p.remaining > 0:
    throw = p.play()
    print throw, "Remaining: ", p.remaining


    if __name__ == "__main__":
    main()

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Is this your Python program or have you obtained it from a source, and if a source, can you give us the reference?

  7. #7
    conor147 is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    i have an assignment that is only graded in java. and i know and can write python about 10 times faster than i can write java so i used python and then tried to find a converter so i could save time and i wouldnt have to learn java, (because i tried to learn java before but it started messing with my brain) so i came on here and tried to find help with writing it in java but even with that i still had to learn java, so i thought i would just post the python and ask for a java version of it, otherwise ill have to learn java but i dont have enough time. so im screwed unless someone can convert python or write the java :(

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    1.) We are not going to write the Java code for you so you can submit it as your own.
    2.) If you try to write the Java code and get stuck we will help you out with your code.

    P.S If you don't want to learn Java then why are you taking the Java course in the first place?

  9. #9
    conor147 is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    im taking a course that includes programming and java happens to be the language the course is doing this year, not python unfortunately :(

    i hope to get round to learning java at some point of course, good programmers are only good if they can program in many languages i hear. if i could learn it in 4 days then hell yeah i wanna learn it, but i dont think its possible.

    is it possible to learn java in 4 days to a competent level... say... enough to write the java equivalent of that python?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's all irrelevant because no one here is going to help you cheat. Please just go.

  11. #11
    conor147 is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    well i never asked anyone to help me cheat so does that mean that it is still relevant?

    whats the quickest way to learn java for someone completely new to the language?

  12. #12
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    programming isn't dependent on a programming language. and neither is problem solving
    google returned this
    PythonComparedToJava - Irmen's Python Wiki
    Jython - Wikipedia, the free encyclopedia

    so please, be kind: dont as here to solve your homework.
    Here most people will help with very concrete questions.
    You should read this: How To Ask Questions The Smart Way
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  13. #13
    conor147 is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    im sorry if it came across that way, but i really just want help. i will of course do all the work but just need advice on what would be the best way. this project is actually meant to be done as a pair but im doing it on my own which makes it even more difficult. if this forum turns out to be good then i will use it more and hopefully help people in return once i come to terms with the language.

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

    Default

    Quote Originally Posted by conor147 View Post
    if i could learn it in 4 days then hell yeah i wanna learn it, but i dont think its possible.
    It takes a bit longer, more like ten years or so.

    kind regards,

    Jos

  15. #15
    justin1980 is offline Member
    Join Date
    Jan 2010
    Location
    Wisconsin
    Posts
    20
    Rep Power
    0

    Default

    If you know another programming language i would think you could easily convert that program to Java in four days.
    Last edited by Fubarable; 01-18-2010 at 02:52 AM.

Similar Threads

  1. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 05:32 PM
  2. simulating netstat
    By prashant in forum Networking
    Replies: 1
    Last Post: 03-14-2009, 07:41 AM
  3. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  4. Simulating Brownian Motion
    By ixhabbaba in forum Java Applets
    Replies: 2
    Last Post: 11-11-2007, 08:53 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Tags for this Thread

Posting Permissions

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