In the world of programming, it’s always fascinating to see how much you can achieve with a limited number of lines of code. Python, being a concise and powerful language, allows us to create simple yet engaging games in just a few lines. Here’s an example of a miniature Python game that can be written in ten lines or less.
The game we’ll create is a simple “Rock, Paper, Scissors” game against the computer. The player will choose their move (rock, paper, or scissors) and the computer will randomly choose one as well. The game will then determine the winner based on the rules of Rock, Paper, Scissors.
Here’s the code for the game:
pythonimport random
choices = ["rock", "paper", "scissors"]
player_choice = input("Rock, Paper, Scissors? ").lower()
computer_choice = random.choice(choices)
if (player_choice == computer_choice):
print("Tie!")
elif ((player_choice == "rock" and computer_choice == "scissors") or
(player_choice == "paper" and computer_choice == "rock") or
(player_choice == "scissors" and computer_choice == "paper")):
print("You win!")
else:
print("Computer wins!")
Let’s break down the code:
- We import the
random
module to allow the computer to randomly choose its move.
- We define a list
choices
containing the possible moves: “rock”, “paper”, and “scissors”.
- We prompt the player to enter their choice using the
input()
function and convert it to lowercase for consistency.
- The computer randomly chooses its move using
random.choice(choices)
.
- We use a series of if-elif-else statements to determine the winner. If the player and computer have the same choice, it’s a tie. If the player’s choice beats the computer’s choice according to the rules of Rock, Paper, Scissors, the player wins. Otherwise, the computer wins.
This game is a great example of how Python’s concise syntax and built-in modules can enable us to create engaging experiences with minimal code.