Need some simple coding help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mx2k

    #1

    Need some simple coding help

    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 - mx@mx2k.net
    # 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(timeco de)
    f.write('Action times 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_inpu t("Char 1
    Initiative: ")),int(raw_inp ut("Char 1 Reaction Time: "))]
    char2 = [raw_input("Char 2 Name: "),int(raw_inpu t("Char 2
    Initiative: ")),int(raw_inp ut("Char 2 Reaction Time: "))]
    char3 = [raw_input("Char 3 Name: "),int(raw_inpu t("Char 3
    Initiative: ")),int(raw_inp ut("Char 3 Reaction Time: "))]
    char4 = [raw_input("Char 4 Name: "),int(raw_inpu t("Char 4
    Initiative: ")),int(raw_inp ut("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

  • Steven Bethard

    #2
    Re: Need some simple coding help

    mx2k wrote:[color=blue]
    > 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?[/color]

    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(objec t):
    .... def __init__(self, name, initiative, reaction_time):
    .... self.name = name
    .... self.initiative = initiative
    .... self.reaction_t ime = reaction_time
    .... def __repr__(self):
    .... return 'Character(%r, %r, %r)' % (
    .... self.name, self.initiative , self.reaction_t ime)
    ....
    py> def get_char():
    .... return Character(raw_i nput("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('Stev e', 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

    Comment

    • mx2k

      #3
      Re: Need some simple coding help

      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.

      Comment

      • mx2k

        #4
        Re: Need some simple coding help

        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

        Comment

        • Steven Bethard

          #5
          Re: Need some simple coding help

          mx2k wrote:[color=blue]
          > 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.[/color]

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

          py> characters
          [Character('Stev e', 3, 2), Character('mx2k ', 5, 1)]
          py> def get_init(charac ter):
          .... return (character.init iative + character.react ion_time +
          .... int(raw_input(' %s initiative: ' % character.name) ))
          ....
          py> def print_initiativ es(characters, rounds):
          .... for _ in range(rounds):
          .... inits = dict([(character, get_init(charac ter))
          .... for character in characters])
          .... for character in sorted(inits, key=inits.__get item__):
          .... print character, inits[character]
          ....
          py> print_initiativ es(characters, 3)
          [... I type '5', '6' ...]
          Character('Stev e', 3, 2) 10
          Character('mx2k ', 5, 1) 12
          [... I type '9', '1' ...]
          Character('mx2k ', 5, 1) 7
          Character('Stev e', 3, 2) 14
          [... I type '1', '2' ...]
          Character('Stev e', 3, 2) 6
          Character('mx2k ', 5, 1) 8

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

          HTH,

          STeVe

          Comment

          • Steven Bethard

            #6
            Re: Need some simple coding help

            mx2k wrote:[color=blue]
            > 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.[/color]

            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

            Comment

            • Terry Reedy

              #7
              Re: Need some simple coding help


              "mx2k" <mx@mx2k.net> wrote in message
              news:1109762091 .780210.245430@ f14g2000cwb.goo glegroups.com.. .[color=blue]
              > 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.[/color]

              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



              Comment

              • mx2k

                #8
                Re: Need some simple coding help


                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]

                Comment

                Working...