Need some simple coding help

Discussion in 'Python' started by mx2k, Mar 1, 2005.

  1. mx2k

    mx2k Guest

    Hello @ all,

    we have written a small program (code below) for our own
    in-developement rpg system, which is getting values for 4
    RPG-Characters and doing some calculations with it.

    now we're trying hard to find out how to get it working with 'n'
    Characters, so you will be asked to enter a number at the beginning,
    asking you how many characters you want.

    anyone out there who could help us?

    also i wonder how i can read the current date/time (we don't understand
    datetime() )and put it into a variable and print that variable?

    [mx] and Tschabo
    doing python since yesterday :)



    Code follows:
    # Timeline Tools for an RPG system - Written in Python 2.4
    # Created by [mx] networkz @ 27.02.05 -
    # Collaboration work of mx2k & Tschabo (von Konsti)

    # import math

    #-------------------------------------------------------------
    #defining functions

    #header text
    def showtitle():
    print ''
    print ' Timeline System - Alpha Version 0.8.15 '
    print '____________________________________________'
    print '* supports 4 characters *'
    print '* coding [mx] networkz & Tschabo *'
    print '* created 2005 *'
    #print '============================================'
    print ''

    #sorting
    def sortfun():
    charlist = [char1[1],char2[1],char3[1],char4[1]]
    charlist.sort()

    #dunno if correct, is spamming move[] lists but working correctly
    if charlist[0] == char1[1]:
    move1[0:0] = [char1[0],char1[1],char1[2]]
    if charlist[0] == char2[1]:
    move1[0:0] = [char2[0],char2[1],char2[2]]
    if charlist[0] == char3[1]:
    move1[0:0] = [char3[0],char3[1],char3[2]]
    if charlist[0] == char4[1]:
    move1[0:0] = [char4[0],char4[1],char4[2]]
    if charlist[1] == char1[1]:
    move2[0:0] = [char1[0],char1[1],char1[2]]
    if charlist[1] == char2[1]:
    move2[0:0] = [char2[0],char2[1],char2[2]]
    if charlist[1] == char3[1]:
    move2[0:0] = [char3[0],char3[1],char3[2]]
    if charlist[1] == char4[1]:
    move2[0:0] = [char4[0],char4[1],char4[2]]
    if charlist[2] == char1[1]:
    move3[0:0] = [char1[0],char1[1],char1[2]]
    if charlist[2] == char2[1]:
    move3[0:0] = [char2[0],char2[1],char2[2]]
    if charlist[2] == char3[1]:
    move3[0:0] = [char3[0],char3[1],char3[2]]
    if charlist[2] == char4[1]:
    move3[0:0] = [char4[0],char4[1],char4[2]]
    if charlist[3] == char1[1]:
    move4[0:0] = [char1[0],char1[1],char1[2]]
    if charlist[3] == char2[1]:
    move4[0:0] = [char2[0],char2[1],char2[2]]
    if charlist[3] == char3[1]:
    move4[0:0] = [char3[0],char3[1],char3[2]]
    if charlist[3] == char4[1]:
    move4[0:0] = [char4[0],char4[1],char4[2]]


    #showing next action times
    # should also be enumerated to 'n'th action
    # but how to put that into string
    def shownextaction():
    print 'Loop Nr. ', run_nr
    print ''
    print '---------------------------------------'
    print 'Next action', move1[0], 'at tick No', move1[1]
    print ' 2nd action', move2[0], 'at tick No', move2[1]
    print ' 3rd action', move3[0], 'at tick No', move3[1]
    print ' 4th action', move4[0], 'at tick No', move4[1]
    print '---------------------------------------'

    #log to textfile
    def logfile():
    outnr = str('Loop Nr. ') + str(run_nr) + str('\n')
    out1 = str(' Next action ') + str(move1[0])+ str(' at tick No ') +
    str(move1[1]) + str('\n')
    out2 = str(' 2nd action ') + str(move2[0])+ str(' at tick No ') +
    str(move2[1]) + str('\n')
    out3 = str(' 3rd action ') + str(move3[0])+ str(' at tick No ') +
    str(move3[1]) + str('\n')
    out4 = str(' 4th action ') + str(move4[0])+ str(' at tick No ') +
    str(move4[1]) + str('\n\n')
    incr_ = str('Entered Number was: ') + str(inc) + str('\n')
    incre = str('Increasing Character ') + str(move1[0]) + str(' by
    Value: ') + str(inc) + str(' + ') + str(inc_out) + str('\n\n\n')
    #timecode= str

    f = file('timeline.txt', 'a')
    #f.write(timecode)
    f.write('Actiontimes of ')
    f.write(outnr)
    f.write(out1)
    f.write(out2)
    f.write(out3)
    f.write(out4)
    f.write(incr_)
    f.write(incre)
    f.close()




    #-----------------------------------------------------------------------

    #defining some stuff, calling title
    showtitle()
    run_nr= 0
    the_end = 'this is the end. exit requested by user.'
    loops = int(raw_input("Number of Loops: "))

    #check if we should run, then asking for input
    # here it should ask for n characters, then enumerating
    # variables, i.e. n=6 creates char1-char6 and filling it
    # like shown below

    if loops == 0:
    the_end
    else:
    char1 = [raw_input("Char 1 Name: "),int(raw_input("Char 1
    Initiative: ")),int(raw_input("Char 1 Reaction Time: "))]
    char2 = [raw_input("Char 2 Name: "),int(raw_input("Char 2
    Initiative: ")),int(raw_input("Char 2 Reaction Time: "))]
    char3 = [raw_input("Char 3 Name: "),int(raw_input("Char 3
    Initiative: ")),int(raw_input("Char 3 Reaction Time: "))]
    char4 = [raw_input("Char 4 Name: "),int(raw_input("Char 4
    Initiative: ")),int(raw_input("Char 4 Reaction Time: "))]
    move1 = [0,0,0]
    move2 = [0,0,0]
    move3 = [0,0,0]
    move4 = [0,0,0]

    #-----------------------------------------------------------------------

    #loop the shiznit
    if loops == 0:
    print the_end
    else:
    rtd = 1
    for rtd in range(loops+1):

    #calling sort/show functions
    sortfun()
    shownextaction()

    #increasing value of the smallest
    smallest = min(char1[1],char2[1],char3[1],char4[1])
    print 'Amount of Actiontime to add for', move1[0]
    inc = int(raw_input('Value: '))
    print '_______________________________________'
    if smallest == char1[1]:
    char1[1] = inc + char1[1] + char1[2]
    inc_out = char1[2]
    if smallest == char2[1]:
    char2[1] = inc + char2[1] + char2[2]
    inc_out = char2[2]
    if smallest == char3[1]:
    char3[1] = inc + char3[1] + char3[2]
    inc_out = char3[2]
    if smallest == char4[1]:
    char4[1] = inc + char4[1] + char4[2]
    inc_out = char4[2]

    #writing to logfile and increasing loop nr
    logfile()
    run_nr = run_nr + 1
     
    mx2k, Mar 1, 2005
    #1
    1. Advertising

  2. mx2k wrote:
    > Hello @ all,
    >
    > we have written a small program (code below) for our own
    > in-developement rpg system, which is getting values for 4
    > RPG-Characters and doing some calculations with it.
    >
    > now we're trying hard to find out how to get it working with 'n'
    > Characters, so you will be asked to enter a number at the beginning,
    > asking you how many characters you want.
    >
    > anyone out there who could help us?


    Why don't you try describing what it is you're trying to do in some more
    detail -- your code is fairly unreadable.

    I gather you want to do something like:

    py> class Character(object):
    .... def __init__(self, name, initiative, reaction_time):
    .... self.name = name
    .... self.initiative = initiative
    .... self.reaction_time = reaction_time
    .... def __repr__(self):
    .... return 'Character(%r, %r, %r)' % (
    .... self.name, self.initiative, self.reaction_time)
    ....
    py> def get_char():
    .... return Character(raw_input("Char 1 Name: "),
    .... int(raw_input("Char 1 Initiative: ")),
    .... int(raw_input("Char 1 Reaction Time: ")))
    ....
    py> n_chars = int(raw_input('Number of Characters: '))
    [...I type '2'...]
    py> characters = [get_char() for _ in xrange(n_chars)]
    [...I type 'Steve', '3', '2', etc....]
    py> characters
    [Character('Steve', 3, 2), Character('mx2k', 5, 1)]

    But it's not clear to me what it is you want done with the characters.
    It looks like you're maybe trying to go through rounds of combat,
    sorting characters by initiative...? What exactly is the process you're
    attempting to follow?

    If you try to describe the problem in words, you'll probably get more
    help. (Code is generally good too, but if it's not clear what you're
    trying to do with the code, no one will be able to make use of it.)

    STeVe
     
    Steven Bethard, Mar 1, 2005
    #2
    1. Advertising

  3. mx2k

    mx2k Guest

    indeed, we're it is our combat timeline. we enter the initiative and
    reaction time, it should find out the lowest then the next, then the
    'n'th etc. and shouw it on the screen like
    Next action 'Character Name1' at tick No 'Initiative1
    2nd action 'Character Name2' at tick No 'Initiative2'
    [...]
    'n'th action 'Character Name n' at tick No 'Initiative n'

    Now you enter a value by which the initiative should be increased,
    then adding reaction time to that value and then adding the result to
    reaction time. Now soring again by lowest,next,'n'th, etc. and then
    again showing the output, asking for another number etc.
     
    mx2k, Mar 2, 2005
    #3
  4. mx2k

    mx2k Guest

    ah, and sorry for that 'unreadable' code, but that was our first try of
    coding ever. we're working through the python documentaion right now -
    and - thanks to the post by STeVe we at last now that we have to
    construct a class for our purpose.

    Thx for any help
    mx2k
     
    mx2k, Mar 2, 2005
    #4
  5. mx2k wrote:
    > indeed, we're it is our combat timeline. we enter the initiative and
    > reaction time, it should find out the lowest then the next, then the
    > 'n'th etc. and shouw it on the screen like
    > Next action 'Character Name1' at tick No 'Initiative1
    > 2nd action 'Character Name2' at tick No 'Initiative2'
    > [...]
    > 'n'th action 'Character Name n' at tick No 'Initiative n'
    >
    > Now you enter a value by which the initiative should be increased,
    > then adding reaction time to that value and then adding the result to
    > reaction time. Now soring again by lowest,next,'n'th, etc. and then
    > again showing the output, asking for another number etc.


    Not sure if I got your calculation right, but something like this should
    work:

    py> characters
    [Character('Steve', 3, 2), Character('mx2k', 5, 1)]
    py> def get_init(character):
    .... return (character.initiative + character.reaction_time +
    .... int(raw_input('%s initiative: ' % character.name)))
    ....
    py> def print_initiatives(characters, rounds):
    .... for _ in range(rounds):
    .... inits = dict([(character, get_init(character))
    .... for character in characters])
    .... for character in sorted(inits, key=inits.__getitem__):
    .... print character, inits[character]
    ....
    py> print_initiatives(characters, 3)
    [... I type '5', '6' ...]
    Character('Steve', 3, 2) 10
    Character('mx2k', 5, 1) 12
    [... I type '9', '1' ...]
    Character('mx2k', 5, 1) 7
    Character('Steve', 3, 2) 14
    [... I type '1', '2' ...]
    Character('Steve', 3, 2) 6
    Character('mx2k', 5, 1) 8

    If you want the characters in the opposite order, simply use
    sorted(inits, key=inits.__getitem__, reverse=True)
    instead of
    sorted(inits, key=inits.__getitem__)

    HTH,

    STeVe
     
    Steven Bethard, Mar 2, 2005
    #5
  6. mx2k wrote:
    > ah, and sorry for that 'unreadable' code, but that was our first try of
    > coding ever. we're working through the python documentaion right now -
    > and - thanks to the post by STeVe we at last now that we have to
    > construct a class for our purpose.


    Well, you don't *have* to -- it could be done all with lists as you had.
    But I think you'll find that it lends itself to a much cleaner
    solution. Also, if you ever find yourself writing a list of 5 or more
    if statements, you're probably not going at the problem from the easiest
    angle. Ask around, tell people what you're trying to do, and what you
    think your if statements are doing now, and they can probably point you
    to the easier way.

    STeVe
     
    Steven Bethard, Mar 2, 2005
    #6
  7. mx2k

    Terry Reedy Guest

    "mx2k" <> wrote in message
    news:...
    > Now you enter a value by which the initiative should be increased,
    > then adding reaction time to that value and then adding the result to
    > reaction time. Now soring again by lowest,next,'n'th, etc. and then
    > again showing the output, asking for another number etc.


    If I correct understand your goal, you might use a priority queue (heap)
    (there is one in the standard library). You insert all chars with the
    initial response time as the 'key'. You remove the one who is going to
    respond first and perform his actions. If he is to get another turn, add
    his next reaction time to the current value and insert him back into the
    queue. The next character to act will then be at the front of the queue.
    Repeat.

    Terry J. Reedy
     
    Terry Reedy, Mar 2, 2005
    #7
  8. mx2k

    mx2k Guest

    thx again, this code is nearly doing as we want and we think to have
    figured out how to change it to meet our requirments. also thx for the
    tip using a heap...

    should there be more problems i'll post again!

    [mx]
     
    mx2k, Mar 3, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brendan Duffy
    Replies:
    0
    Views:
    4,751
    Brendan Duffy
    Jul 25, 2003
  2. Guest
    Replies:
    0
    Views:
    701
    Guest
    Feb 25, 2004
  3. Milsnips

    need some help with coding

    Milsnips, Oct 17, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    417
    Milsnips
    Oct 17, 2005
  4. calmar
    Replies:
    11
    Views:
    937
    calmar
    Feb 21, 2006
  5. gregNelson
    Replies:
    0
    Views:
    302
    gregNelson
    Feb 27, 2009
Loading...

Share This Page