Last active
May 9, 2019 18:21
-
-
Save msaisushma/d34a494fb2425b794d06 to your computer and use it in GitHub Desktop.
Algorithm and Python code for a typical two players Snakes and Ladders game.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Algorithm for snakes and Ladders game in python | |
1)There are two players and they are given a dice | |
2)Typically the game board has 100 cells starting from 1 to 100 | |
3)There are snakes and ladders in different cells. And each has either a ladder or a snake or nothing but not both snake and ladder in the same cell. | |
4)I use two python dictionaries namely 'snakes' and 'ladders' to declare the positions of snakes and ladders. | |
5)I make starting cell number of snake or ladder as 'key' and ending cell number as 'value' of the dictionary. | |
6)Define a function to roll the dice i.e., accepting the input from Player1 and Player2 with a checking condition of dice number to be between 1 & 6. | |
7)Define a function to check if Player1 or Player2 found ladder or a snake mouth. | |
8)Check for the 'key's in both dictionaries & proceed accordingly. | |
9)The rolling of dice continues until any of the player reaches above 99, if any of the players reach 100 they will be declared as winner of the game. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
player1 = 0 | |
player2 = 0 | |
a = range(1,11);b = range(11,21)[::-1];c = range(21,31);d = range(31,41)[::-1];e = range(41,51);f = range(51,61)[::-1];g = range(61,71); | |
h = range(71,81)[::-1];i = range(81,91);j = range(91,101)[::-1] | |
print "Snakes and Ladders game" | |
print j,'\n',i,'\n',h,'\n',g,'\n',f,'\n',e,'\n',d,'\n',c,'\n',b,'\n',a | |
def check_for_snakes_and_ladders(n): | |
"""This method checks for the presence of snakes or ladders in the board""" | |
ladders = {1:38,4:14,9:31,21:42,28:84,36:44,51:67,71:91,80:100} | |
snakes = {98:78,95:75,93:73,87:24,64:60,62:19,56:53,49:11,48:26,16:6} | |
if ladders.has_key(n): | |
print "Its a ladder,Climb up" | |
n = ladders[n] | |
elif snakes.has_key(n): | |
print "Its a snake!!,Come down" | |
n = snakes[n] | |
return n | |
def roll_dice(r): | |
"""This method takes input from each of the players, prints the current position of the players and checks for the | |
winner of the game""" | |
d = int(raw_input("roll the dice: ")) | |
while d < 1 or d > 6: | |
d = int(raw_input("dice should show 1 to 6,Roll the dice again: ")) | |
d = r + d | |
return d | |
while player1 < 100 or player2 < 100: | |
print "Its turn of player1\n" | |
player1 = roll_dice(player1) | |
player1 = check_for_snakes_and_ladders(player1) | |
print "Current status of Player1:",player1,"and Player2:",player2 | |
if player1 > 99: | |
print "Winner of the game is player1" | |
break | |
print "Its turn of player2\n" | |
player2 = roll_dice(player2) | |
player2 = check_for_snakes_and_ladders(player2) | |
print "Current status of Player1:",player1," and Player2:",player2 | |
if player2 > 99: | |
print "Winner of the game is player2" | |
break |
@mali29 no :)
It's a question:so are we running the game in the console or terminal?
It's a question:so are we running the game in the console or terminal?
If your console doesn't support input, run it in terminal, otherwise, console is generally better
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
Does the above code create a numbered grid in python 1 to 100?