Anyone want to critique this program?

John Salerno johnjsal at gmail.com
Sat Jul 2 22:19:43 EDT 2011


Just thought I'd post this in the event anyone has a few spare minutes
and feels like tearing apart a fairly simple attempt to write a
game. :)

I'll paste the exercise I was working on first, although I think it
was meant to be an exercise in how to use lists. I went way beyond
that, so maybe my program is overly complicated. It works, though, so
that's a plus! The main questions I had about it are at the bottom,
after the code.

----------
Snakes and Ladders can be a fun game for kids to play but you realize
when you get older that the players actually have no choices at all.
To illustrate just how little input the players have I want you to
make a computer program that allows two players to play snakes and
ladders on the board given.

	The rules of the game are simple. On each player's turn they roll one
six sided die and move ahead that many squares.  If they end their
move at the base of a ladder then they automatically climb to the top
of the ladder.  If they end their move at the end of a snake they
automatically slide down to its head. To win the game you must be the
first player to land on the last square.  If you are near the end and
your roll would cause you to go past the end square you don't move for
that turn.

	Your computerized version will look something like:
Player 1  hit enter to roll

You rolled: 3
You are at spot 3
Player 2  hit enter to roll

You rolled: 6
You climbed a ladder
You are at spot 17
although you can write this program without using lists, you should
ask yourself how you can use lists to encode where the snakes and
ladders are.
-----------------
(I changed snakes to chutes, and I didn't paste in the game board
picture. Just trust that the dictionary of lists in the beginning of
the program is accurate, and that each set of numbers represents first
the space you land on, and second the space you slide or climb to,
e.g. [30, 35] means you landed on 30, and as a result of landing on a
ladder space, you climbed up to space 35.)

-------------------------------
import random

game_information = '***Chutes and Ladders***\nUp to four (4) players
may play.\n'\
                   'There are 90 spaces on the board. '\
                   'The player to reach space 90 first wins.'
separator = '-' * 20
spaces = [None] * 90
c_l_spaces = {r'slid down a chute \\': [[14, 3], [20, 15], [39, 33],
[66, 53],
                                    [69, 58], [79, 67], [84, 71], [88,
36]],
              'climbed up a ladder |=|': [[6, 17], [24, 26], [30, 44],
[49, 62], [82, 86]]}

class Player:

    def __init__(self, number):
        self.number = number
        self.space = 0

    def move(self, roll):
        global spaces
        if (self.space + roll) > 90:
            return (1, 'Your roll would move you off the game board.
'\
                    'You remain at space {0}.'.format(self.space))
        elif (self.space + roll) == 90:
            return (0, 'You moved to the last space. You won the
game!')
        else:
            self.space += roll
            try:
                space_type = spaces[self.space - 1][0]
                self.space = spaces[self.space - 1][1]
            except TypeError:
                return (1, 'You moved to space
{0}.'.format(self.space))
            else:
                return (1, 'You {0} to space {1}!'.format(space_type,
self.space))

def roll_die():
    roll = random.randint(1, 6)
    print('You rolled {0}.'.format(roll))
    return roll

def populate_game_board():
    global spaces
    for key, values in c_l_spaces.items():
        for space in values:
            spaces[space[0] - 1] = [key, space[1]]

def initialize_players():
    while True:
        try:
            num_players = int(input('Enter the number of players (0 to
exit): '))
        except ValueError:
            print('You have entered an invalid response.\n')
        else:
            if num_players == 0:
                print('You have quit the game.')
                return
            elif 1 <= num_players <= 4:
                break
            elif (num_players < 0) or (num_players > 4):
                print('You have entered an invalid number of players.
\n')

    players = []
    for num in range(num_players):
        players.append(Player(num + 1))

    return players

def start_game(players):
    game = 1
    while game:
        for player in players:
            print('\n***Player {0}***'.format(player.number))
            print('You are currently on space
{0}.'.format(player.space))
            game_update = player.move(roll_die())
            print(game_update[1])
            print(separator, end='')
            if game_update[0] == 0:
                game = 0
                break
        if game:
            input('\nPress Enter for the next turn.')

def initialize_game():
    global game_information
    print(game_information)
    players = initialize_players()
    if players:
        populate_game_board()
        start_game(players)

if __name__ == '__main__':
    initialize_game()
--------------------------

My questions:

1. Are the functions I made necessary? Do they do enough or too much?
2. Is there a better way to implement the players than as a class?
3. Is there a better way to handle all the print calls that the
program makes?
4. Are my try/except blocks used appropriately?

Any other comments would be appreciated too!
Thanks!



More information about the Python-list mailing list