still having problems with "Nim". using python 2.6.4

Jordan Uchima fuzzy.666.chaos at gmail.com
Sun Feb 7 10:22:55 EST 2010


I have attached the file that the game is on. feel free to modify it to make
it better. all suggestions are welcome. if you don't want to download the
file then here's the code:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Start with thirteen pieces and two players.
# Each player takes 1 - 4 pieces each turn.
# Player who takes the last piece loses.


# Declare constants.
NUMBER_OF_PLAYERS = 2
TOTAL_PIECES_AT_START = 13
namePlayer1 = raw_input("Hello Player 1, what is your name?\n")
namePlayer2 = raw_input("Hello Player 2, what is your name?\n")
# Declare functions.

def rules():
    """Print the rules of the game."""
    print 'The player that takes the last piece loses!'\
          'You can only take 1 - 4 pieces!\n\n'

def get_players():
    """Get the names of the players."""
    # Let's put the player's names in a list.
    players = []
    # Looping like this saves us repeating lines of code for each player.
    for player in range(NUMBER_OF_PLAYERS):

        players.append(player)
        break
    return players

def take_turn(player):
    """Handle a player's turn."""
    # Turn logic goes here.

player1Choice = int(raw_input(namePlayer1 + " how many pieces would you like
to take?\n")),
player2Choice = int(raw_input(namePlayer2 + " how many pieces would you like
to take?\n"))
playerChoice = player1Choice and player2Choice
x = 13 - playerChoice
loss = x <=2 and x >= 0,
while player1Choice == loss is True:
    print namePlayer1 + " loses!"
while player2Choice == loss is True:
    print namePlayer2, loss
while player1Choice and player2Choice == loss is True:
    print "It's a draw!"
while player1Choice != loss is True:
    print x
while player2Choice != loss is True:
    print x
while player1Choice and player2Choice != loss is True:
    print ("Keep going! Only ", x, " pieces left!")
validChoice = \
player1Choice == range(1, 4) is True,
print x
player1Choice == range(1, 4) is False,
print validChoice
player2Choice == range(1, 4) is True,
print x
player2Choice == range(1, 4) is False,
print validChoice


def main():
    """Run the game."""
    # Display the rules by calling rules function.
    rules()
    # Get the players by calling the get_players function.
    players = get_players()
    # Set up the game.
    remaining_pieces = TOTAL_PIECES_AT_START
    playing = True
    # Main loop - let's play!
    while playing:
        # Take turns.
        for player in players:
            remaining_pieces = take_turn(player)
            # Check if this player has lost.
            if remaining_pieces <= 1:
                # Player has taken last piece.
                print 'Sorry ', loser, ' you have lost.'
                # Break out of the loop
                playing = False
                break


# Automatically run main function if we're run as a script.
if __name__ == '__main__':
    main()

# End.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

my problem is that i can't get it to make the players have more than 1 turn
each, it accepts any value for playerChoice, (it is only supposed to accept
values from 1 to 4), and "x" resets after each turn. i can get it to
subtract playerChoice from "x", and display the result, which should be "x",
but, then, "x" resets... by the way, i need this program finished by
wednesday, Feb.  10, 2010. Please help me!!!


--

Jordan (fuzzy.666.chaos at gmail.com)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100207/189adcea/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Nim.py
Type: application/octet-stream
Size: 2785 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20100207/189adcea/attachment.obj>


More information about the Python-list mailing list