basic 2 player wordgame

M

Mel

Baba said:
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.
 
B

bobicanprogram

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
 
B

Baba

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top