Objective:
In this assignment, you will implement a Snakes and Ladders game in Python. The game should allow two players to roll dice and move on a 100-square board while encountering snakes and ladders. The objective is to write code that simulates the game with basic rules, including player movement, snakes and ladders, and winning conditions.
Problem Statement:
You are tasked with building a Snakes and Ladders game with the following requirements:
- The game board consists of 100 squares, numbered from 1 to 100.
- Two players take turns rolling a 6-sided dice (numbers 1 to 6).
- After rolling the dice, players move forward by the rolled number.
- If a player lands on a snake, they move backward to the position where the snake ends.
- If a player lands on a ladder, they move forward to the position where the ladder starts.
- The first player to reach square 100 wins the game.
- The game ends as soon as one of the players reaches square 100.
You are required to implement the game using the following components:
- Dice Roll: Simulate a dice roll that generates a random number between 1 and 6.
- Player Movement: Move the player according to the dice roll. If the player encounters a snake or a ladder, adjust their position accordingly.
- Snakes: Implement at least 10 snake positions, where each snake moves the player backward (e.g., snake at position 16 moves the player to position 6).
- Ladders: Implement at least 10 ladder positions, where each ladder moves the player forward (e.g., ladder at position 1 moves the player to position 38).
- Game Logic: Alternate between players, rolling the dice, updating their positions, and checking if any player has won the game.
- Game Output: Display the results after each dice roll, including the player’s current position, whether they encountered a snake or a ladder, and who wins the game.
Requirements:
- Class Design: Create a class SnakesAndLadders to represent the game. The class should have:
- Attributes to store the positions of the snakes and ladders.
- Methods to simulate the dice roll, move the player, check for snakes or ladders, and check for a winner.
- A method to play the game, alternating between players and checking for a win condition after each turn.
- Attributes to store the positions of the snakes and ladders.
- Player Movement: Implement player movement logic such that the players move based on their dice rolls and encounter snakes and ladders. Ensure that if a player exceeds position 100, they stay in their previous position.
- Game End Condition: The game should end when one player reaches square 100, and the winner should be announced.
- Game Output: Print out the progress of the game, including:
-
- The dice roll for each turn.
- The player’s position after each move.
- Whether the player encountered a snake or a ladder.
- Announce the winner when a player reaches square 100.
- The dice roll for each turn.
Example Output:
Player 1 rolls a 4. Moving from 0 to 4.
Player 1 rolls a 3. Moving from 4 to 7.
Player 1 rolls a 5. Moving from 7 to 12.
Player 1 rolls a 2. Moving from 12 to 14. Yay! A ladder! Moving from 14 to 34.
Player 1 rolls a 6. Moving from 34 to 40.
…
Player 2 rolls a 6. Moving from 0 to 6.
Player 2 rolls a 2. Moving from 6 to 8. Oops! A snake! Moving from 8 to 4.
Player 2 rolls a 5. Moving from 4 to 9.
…
Player 1 wins the game!
Deliverables:
- Submit the code that implements the Snakes and Ladders game.
- Provide a brief description of your approach to solving the problem and how the game works in your solution.
Evaluation Criteria:
- Correct implementation of the Snakes and Ladders game mechanics.
- Proper alternation between players and accurate simulation of the dice rolls.
- Correct handling of snakes and ladders, including updates to player positions.
- Clear game output showing the progress of the game and the winner.
Code structure and readability (e.g., proper use of functions, classes, and methods).