# "Nim" game being created, no GUI used... Need tips...

Kev Dwyer kevin.p.dwyer at gmail.com
Sat Feb 6 13:10:48 CET 2010

```On Fri, 05 Feb 2010 21:54:48 -0600, Jordan Uchima wrote:

Hello Jordan,

You could try something like this (not tested, and maybe I don't
quite "get" the rules):

"""
Nim game.

In response to question on c.l.p.

"""

# 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

# Declare functions.

def rules():
"""Print the rules of the game."""
print 'Some rules'

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):
name = raw_input("Hello Player %d, what is your name?\n" % player)
players.append(player)
return players

def take_turn(player):
"""Handle a player's turn."""
# Turn logic goes here.
# Left as an exercise :)
the_remaining_number_of_pieces_after_this_turn = 1
return the_remaining_number_of_pieces_after_this_turn

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 == 0:
# Player has taken last piece.
print 'Sorry %s, you have lost.' % player
# Break out of the loop
playing = False
break

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

# End.

The "while" loop in the main function runs the game until a winner
(or loser in this case) is declared.  The "for" loop within the while
loop handles the alternation of turns.  Have a look at the python
tutorial at http://docs.python.org/tutorial/index.html for more on
"while" and "for" statements.

Use functions to break up the code into related sections.  Check out
the tutorial material for functions too.

This skeleton should give you a starting structure for your program.

Have fun :)

Kev

```