From c22a82167fd095054d407d446ce1a917770d28ec Mon Sep 17 00:00:00 2001 From: Vansh Gupta <131511941+Vansh16GUPTA@users.noreply.github.com> Date: Thu, 3 Jul 2025 15:34:30 +0530 Subject: [PATCH] Complete Snake and Ladder game implementation --- snakes_and_ladders/__init__.py | 0 snakes_and_ladders/board.py | 14 ++++++++++++++ snakes_and_ladders/dice.py | 5 +++++ snakes_and_ladders/game.py | 29 +++++++++++++++++++++++++++++ snakes_and_ladders/main.py | 26 ++++++++++++++++++++++++++ snakes_and_ladders/player.py | 4 ++++ tests/test_game.py | 0 7 files changed, 78 insertions(+) create mode 100644 snakes_and_ladders/__init__.py create mode 100644 snakes_and_ladders/board.py create mode 100644 snakes_and_ladders/dice.py create mode 100644 snakes_and_ladders/game.py create mode 100644 snakes_and_ladders/main.py create mode 100644 snakes_and_ladders/player.py create mode 100644 tests/test_game.py diff --git a/snakes_and_ladders/__init__.py b/snakes_and_ladders/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/snakes_and_ladders/board.py b/snakes_and_ladders/board.py new file mode 100644 index 00000000..b9bc0342 --- /dev/null +++ b/snakes_and_ladders/board.py @@ -0,0 +1,14 @@ +class Board: + def __init__(self, snakes, ladders): + self.snakes = snakes # dict {head: tail} + self.ladders = ladders # dict {start: end} + + def get_next_position(self, position): + prev = -1 + while prev != position: + prev = position + if position in self.snakes: + position = self.snakes[position] + elif position in self.ladders: + position = self.ladders[position] + return position diff --git a/snakes_and_ladders/dice.py b/snakes_and_ladders/dice.py new file mode 100644 index 00000000..2adea70a --- /dev/null +++ b/snakes_and_ladders/dice.py @@ -0,0 +1,5 @@ +import random + +class Dice: + def roll(self): + return random.randint(1, 6) diff --git a/snakes_and_ladders/game.py b/snakes_and_ladders/game.py new file mode 100644 index 00000000..a1858c30 --- /dev/null +++ b/snakes_and_ladders/game.py @@ -0,0 +1,29 @@ +from .dice import Dice +from .board import Board +from .player import Player + +class Game: + def __init__(self, snakes, ladders, player_names): + self.board = Board(snakes, ladders) + self.players = [Player(name) for name in player_names] + self.dice = Dice() + + def play(self): + won = False + while not won: + for player in self.players: + dice_value = self.dice.roll() + initial_pos = player.position + new_pos = initial_pos + dice_value + if new_pos > 100: + new_pos = initial_pos + else: + new_pos = self.board.get_next_position(new_pos) + + player.position = new_pos + print(f"{player.name} rolled a {dice_value} and moved from {initial_pos} to {new_pos}") + + if new_pos == 100: + print(f"{player.name} wins the game") + won = True + break diff --git a/snakes_and_ladders/main.py b/snakes_and_ladders/main.py new file mode 100644 index 00000000..e09d506d --- /dev/null +++ b/snakes_and_ladders/main.py @@ -0,0 +1,26 @@ +from .game import Game + +def read_input(): + s = int(input()) + snakes = {} + for _ in range(s): + head, tail = map(int, input().split()) + snakes[head] = tail + + l = int(input()) + ladders = {} + for _ in range(l): + start, end = map(int, input().split()) + ladders[start] = end + + p = int(input()) + players = [] + for _ in range(p): + players.append(input().strip()) + + return snakes, ladders, players + +if __name__ == "__main__": + snakes, ladders, players = read_input() + game = Game(snakes, ladders, players) + game.play() diff --git a/snakes_and_ladders/player.py b/snakes_and_ladders/player.py new file mode 100644 index 00000000..e0786045 --- /dev/null +++ b/snakes_and_ladders/player.py @@ -0,0 +1,4 @@ +class Player: + def __init__(self, name): + self.name = name + self.position = 0 diff --git a/tests/test_game.py b/tests/test_game.py new file mode 100644 index 00000000..e69de29b