Python Code Snake and Ladder1 | PDF | Function (Mathematics) | Control Flow
100% found this document useful (1 vote)
49 views

Python Code Snake and Ladder1

1. The document provides instructions for programming a Snakes and Ladders game in Python. 2. It outlines 7 steps to design the game, including getting player names, generating random ladder and snake positions, rolling dice, and checking player positions for ladders or snakes. 3. The main program runs the game logic, checking after each turn if a player has won by reaching position 100.

Uploaded by

jahringa123
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
49 views

Python Code Snake and Ladder1

1. The document provides instructions for programming a Snakes and Ladders game in Python. 2. It outlines 7 steps to design the game, including getting player names, generating random ladder and snake positions, rolling dice, and checking player positions for ladders or snakes. 3. The main program runs the game logic, checking after each turn if a player has won by reaching position 100.

Uploaded by

jahringa123
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Instructions

1. Find a partner

2. Unscramble the code below

3. The code currently has two players, change to code to allow at least four players

4. Rewrite the Python program that finds the shortest possible path from 1 to 100 that
requires the smallest possible dice rolls.

5. Choose an algorithm to represent your code.

Programming Project

The objective of this project is to design the Snakes and Ladders game. The project needs to
simulate the game with the ability to roll dice between two players, and then apply the game
logic to decide who the winner is.

The game board has 100 cells starting from 1 to 100. There are two players and each player rolls
the dice and moves cell position accordingly. There are snakes and ladders in different cells with
each cell having either a ladder, a snake or nothing but not both in the same cell. If a player
moves to a cell that contains a ladder, the player jumps up by 15 cells. If the player moves to a
cell including a snake mouth, the player drops off by 10 cells. The rolling of dice continues until
one of the players reaches a cell above 99, that player will be declared as the winner of the game.

The steps to design the game are:

Step 1: get_players_list()

Create a function get_players_list() that reads and stores both player names in a list. The function
prints and returns name_list.

Step 2: generate_ladders_position()

There are 15 ladders in the game. Declare function generate_ladders_position()to generate the
positions of these ladders randomly, and store them in a list. Ladder positions should be in the
range of cells 5 to 85 (both inclusive). You must avoid assigning the same cell to more than one
ladder. The function prints the ladder positions and returns ladders_list.
Step 3: generate_snakes_position()

There are 10 snakes in the game. Declare function generate_snakes_position()to generate the
positions of these snakes mouths randomly, and store them in a list. Snake mouth positions
should be in the range of cells 20 to 95 (both inclusive).

You must avoid assigning the same cell to more than one snake. Moreover, the function should
receive an input argument (ladders_list) to avoid assigning a cell to both a ladder or a snake,
meaning that no cell can host both a ladder or a snake mouth. Nevertheless, for simplicity
reasons, the game allows snakes cells that jump down to a ladder cell, or ladder cells that put the
player in a snake mouth. For example, a player may jump on a ladder and then get bitten by a
snake.

The function prints and returns the snakes_list.

Step 4: roll_dice()

Define a function to roll the dice i.e., generating a random number between 1 and 6 (both
inclusive). The function should receive two arguments, current_position (an integer) of the
player rolling the dice, and player_name, the name of the player rolling the dice. The function
returns the new position of the player. The function also prints the value of the dice, and the new
position of the player. The new position is calculated as current_position + the dice value.

For example, roll_dice(current_position=54, player_name="John") prints:

Step 5: check_for_ladder()

Write a function to accept three input arguments, 1) current_position of a player, 2)ladder_list,


and 3) plyer_name. The function checks if the position contains a ladder. If so, increase the
current position of the player by 15 cells and return the new position.

The function also prints a message explaining what happened, and the player’s new position. For
example:

Step 6: check_for_snake()

Create a function to receive three input arguments, 1) current_position of the player, 2)


snake_list, and 3) player_name, and check if the position contains a snake mouth. If so, decrease
the current position of the player by 10 cells and return the new position.

The function also prints a message explaining what happened, and the player’s new position, for
example:
Step 7: Main Program

The main program is responsible for running the game using the functions defined above, and
identify the winner. An example of the tasks on the Main program can be:

1. Decalre players_positions list to maintain the position of the players, each index shows
the position of a player. Initialize each index of the list by zero meaning that the initial
position of each player is cell 0.
2. Initialize the list of players by calling get_players_list() and store it in players_name
3. Initialize list of ladders and snakes by calling the generate_ladders_position() and
generate_snakes_position() and store them into two lists respectively, ladders_list and
snakes_list.
4. Use a while loop to start playing the game. This loop iterates as long as both of
player_positions are less than 99 (99 exclusive):
o Use an inner for loop that counts twice, once for each player:
1. Get new position of the current player by calling roll_dice().
2. Check whether the player position is more than 99 (99 exclusive). In this
case, the player is the winner of the game so the game should print a
message and exit the program by breaking the loop.

 Otherwise, for the current player, check for ladder and snake positions by calling
check_for_ladder() and check_for_snakes(), and update the current player’s position
respectively, if necessary.

6.
What is happening in Game:
1- Display the welcome message
2- Collect the player’s names
3- Until one of the player wins do the following:
3.1- Roll the dice
3.2- Move the player forward for the value got on the dice roll.
3.3- If the player is on snake’s head, move down to its tail
3.4- If the player is on ladder’s bottom, take it to its top
3.5- else remain there and let the second player roll the dice

Unscramble the Snake and the Ladder Python Code

import time
import random
import sys

check_win(player2_name, player2_current_position)

if __name__ == "__main__":
start()

print("\n" + player_name + " moved from " + str(old_value) + " to " + str(current_value))
if current_value in snakes:
final_value = snakes.get(current_value)
got_snake_bite(current_value, final_value, player_name)

# snake takes you down from 'key' to 'value'


snakes = {
8: 4,
18: 1,
26: 10,
39: 5,
51: 6,
54: 36,
56: 1,
60: 23,
75: 28,
83: 45,
85: 59,
90: 48,
92: 25,
97: 87,
99: 63
}
player_turn_text = [
"Your turn.",
"Go.",
"Please proceed.",
"Lets win this.",
"Are you ready?",
"",
]

snake_bite = [
"boohoo",
"bummer",
"snake bite",
"oh no",
"dang"
]

ladder_jump = [
"woohoo",
"woww",
"nailed it",
"oh my God...",
"yaayyy"
]

def welcome_msg():
msg = """
Welcome to Snake and Ladder Game.
Version: 1.0.0
Developed by: https://www.pythoncircle.com

Rules:
1. Initally both the players are at starting position i.e. 0.
Take it in turns to roll the dice.
Move forward the number of spaces shown on the dice.
2. If you lands at the bottom of a ladder, you can move up to the top of the ladder.
3. If you lands on the head of a snake, you must slide down to the bottom of the snake.
4. The first player to get to the FINAL position is the winner.
5. Hit enter to roll the dice.

"""
print(msg)
def get_player_names():
player1_name = None
while not player1_name:
player1_name = input("Please enter a valid name for first player: ").strip()

player2_name = None
while not player2_name:
player2_name = input("Please enter a valid name for second player: ").strip()

print("\nMatch will be played between '" + player1_name + "' and '" + player2_name + "'\n")
return player1_name, player2_name

def get_dice_value():
def got_snake_bite(old_value, current_value, player_name):
print("\n" + random.choice(snake_bite).upper() + " ~~~~~~~~>")
print("\n" + player_name + " got a snake bite. Down from " + str(old_value) + " to " +
str(current_value))

def got_ladder_jump(old_value, current_value, player_name):


print("\n" + random.choice(ladder_jump).upper() + " ########")
print("\n" + player_name + " climbed the ladder from " + str(old_value) + " to " +
str(current_value))

def snake_ladder(player_name, current_value, dice_value):


time.sleep(SLEEP_BETWEEN_ACTIONS)
old_value = current_value
current_value = current_value + dice_value

if current_value > MAX_VAL:


print("You need " + str(MAX_VAL - old_value) + " to win this game. Keep trying.")
return old_value

# just of effects. add a delay of 1 second before performing any action


SLEEP_BETWEEN_ACTIONS = 1
MAX_VAL = 100
DICE_FACE = 6

time.sleep(SLEEP_BETWEEN_ACTIONS)
dice_value = random.randint(1, DICE_FACE)
print("Its a " + str(dice_value))
return dice_value
elif current_value in ladders:
final_value = ladders.get(current_value)
got_ladder_jump(current_value, final_value, player_name)

else:
final_value = current_value

return final_value

def check_win(player_name, position):


time.sleep(SLEEP_BETWEEN_ACTIONS)
if MAX_VAL == position:
print("\n\n\nThats it.\n\n" + player_name + " won the game.")
print("Congratulations " + player_name)
print("\nThank you for playing the game. Please visit https://www.pythoncircle.com\n\n")
sys.exit(1)

# ladder takes you up from 'key' to 'value'


ladders = {
3: 20,
6: 14,
11: 28,
15: 34,
17: 74,
22: 37,
38: 59,
49: 67,
57: 76,
61: 78,
73: 86,
81: 98,
88: 91
}

def start():
welcome_msg()
time.sleep(SLEEP_BETWEEN_ACTIONS)
player1_name, player2_name = get_player_names()
time.sleep(SLEEP_BETWEEN_ACTIONS)

player1_current_position = 0
player2_current_position = 0

while True:
time.sleep(SLEEP_BETWEEN_ACTIONS)
input_1 = input("\n" + player1_name + ": " + random.choice(player_turn_text) + " Hit the
enter to roll dice: ")
print("\nRolling dice...")
dice_value = get_dice_value()
time.sleep(SLEEP_BETWEEN_ACTIONS)
print(player1_name + " moving....")
player1_current_position = snake_ladder(player1_name, player1_current_position,
dice_value)

check_win(player1_name, player1_current_position)

input_2 = input("\n" + player2_name + ": " + random.choice(player_turn_text) + " Hit the


enter to roll dice: ")
print("\nRolling dice...")
dice_value = get_dice_value()
time.sleep(SLEEP_BETWEEN_ACTIONS)
print(player2_name + " moving....")
player2_current_position = snake_ladder(player2_name, player2_current_position,
dice_value)

You might also like