basic 2 player wordgame

Discussion in 'Python' started by Baba, Sep 21, 2010.

  1. Baba

    Baba Guest

    Baba, Sep 21, 2010
    #1
    1. Advertising

  2. Baba

    Mel Guest

    Baba wrote:
    > I am working on a simple wordgame exercise: 2 players form a word by
    > alternating turns saying a letter, which is added on to the end of the
    > word fragment.
    >
    > I am familiar with loops, iterations etc but i need a hint as to how
    > to approach alternating turns when writing this code?


    One way (not tested):

    thisplayer = Player()
    otherplayer = Player()
    while not_won:
    thisplayer.take_turn()
    thisplayer, otherplayer = otherplayer, thisplayer



    Mel.
    Mel, Sep 21, 2010
    #2
    1. Advertising

  3. On Sep 21, 6:25 am, Baba <> wrote:
    > Hi
    >
    > I am working on a simple wordgame exercise: 2 players form a word by
    > alternating turns saying a letter, which is added on to the end of the
    > word fragment.
    >
    > I am familiar with loops, iterations etc but i need a hint as to how
    > to approach alternating turns when writing this code?
    >
    > exercise source:http://ocw.mit.edu/courses/electrical-engineering-and-computer-scienc...
    >
    > thanks
    > Baba



    Have a look at the SIMPL toolkit (http://www.icanprogram.com/06py/
    lesson1/lesson1.html). This would allow your two players to be split
    into two separate Python modules. It has the "advantage" that once
    completed those two players could be deployed across a network,
    likely without any code changes.

    bob
    bobicanprogram, Sep 21, 2010
    #3
  4. Baba

    Baba Guest

    On Sep 21, 1:39 pm, Mel <> wrote:
    > Baba wrote:
    > > I am working on a simple wordgame exercise: 2 players form a word by
    > > alternating turns saying a letter, which is added on to the end of the
    > > word fragment.

    >
    > > I am familiar with loops, iterations etc but i need a hint as to how
    > > to approach alternating turns when writing this code?

    >
    > One way (not tested):
    >
    > thisplayer = Player()
    > otherplayer = Player()
    > while not_won:
    >     thisplayer.take_turn()
    >     thisplayer, otherplayer = otherplayer, thisplayer
    >
    >         Mel.



    Hi Mel,

    Thank you very much. Your suggestion works like a charm :)

    def alternating_turns():
    hand = []
    thisPlayer = 'player1'
    otherPlayer = 'player2'
    while len(hand) < 3:
    print 'turn %s: ' %(thisPlayer)
    letter = raw_input('enter letter: ')
    hand.append(letter)
    thisPlayer, otherPlayer = otherPlayer, thisPlayer
    print hand

    alternating_turns()

    thanks again! much appreciated. this was a first for me where i have
    learned a practical way to apply mutation.

    Baba
    Baba, Sep 21, 2010
    #4
    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. cyshao
    Replies:
    1
    Views:
    3,303
    Steve C. Orr [MVP, MCSD]
    Apr 12, 2005
  2. steven robinson
    Replies:
    0
    Views:
    639
    steven robinson
    Nov 9, 2003
  3. Engineer
    Replies:
    6
    Views:
    605
    Jeremy Bowers
    May 1, 2005
  4. ary
    Replies:
    1
    Views:
    1,068
    =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=
    Nov 28, 2006
  5. HY Tech
    Replies:
    0
    Views:
    608
    HY Tech
    Sep 7, 2003
Loading...

Share This Page