AI Example #01 - Rock, Paper, Scissors Game with AI

In our previous lectures, we’ve show you the fundamentals and theories of Artificial intelligence. From now, we’ll show you some practical example how Artificial intelligent can help you learn artificial intelligence very quickly using some practical real life examples. Let’s start with Rock, Paper and Scissors game -

Building a Rock, Paper, Scissors Game with AI: A Tutorial for Students

This project will guide you through building a Rock, Paper, Scissors (RPS) game with an AI opponent. We’ll explore basic AI concepts, decision trees, and coding the logic to play the game.

Understanding the Game

RPS is a classic hand game where two players choose one of three options: rock, paper, or scissors. The winner is determined by the following rules:

  • Rock crushes Scissors
  • Scissors cut Paper
  • Paper covers Rock
  • If both players choose the same, it’s a tie

Project Architecture

This project will be built using:

  • Python: A popular programming language for beginners, easy to learn and understand.
  • Libraries: We’ll use the random library to generate random choices for the AI opponent.

Building the Game

1. Setting Up:

2. Creating the Script:

  • Open your code editor and create a new file named rps_game.py.

3. Importing Libraries:

import random

4. Defining Game Choices:

# Define game choices as a list
choices = ["rock", "paper", "scissors"]

5. AI Decision Making (Decision Tree):

We’ll use a simple decision tree approach for the AI to choose its move. This means the AI will randomly pick a number between 1 and 3, corresponding to the choices list index.

def ai_choice():
  """
  This function generates a random choice for the AI opponent.
  """
  return choices[random.randint(0, 2)]

6. User Input Function:

def user_choice():
  """
  This function prompts the user to enter their choice and validates it.
  """
  while True:
    user_input = input("Enter your choice (rock, paper, scissors): ").lower()
    if user_input in choices:
      return user_input
    else:
      print("Invalid choice. Please try again.")

7. Determining the Winner:

This function takes the user and AI choices and determines the winner based on the RPS rules.

def determine_winner(user_choice, ai_choice):
  """
  This function takes user and AI choices and determines the winner.
  """
  if user_choice == ai_choice:
    return "Tie"
  elif user_choice == "rock":
    if ai_choice == "scissors":
      return "You Win!"
    else:
      return "You Lose!"
  elif user_choice == "paper":
    if ai_choice == "rock":
      return "You Win!"
    else:
      return "You Lose!"
  elif user_choice == "scissors":
    if ai_choice == "paper":
      return "You Win!"
    else:
      return "You Lose!"

8. Running the Game:

# Main game loop
while True:
  # Get AI and user choices
  ai_selection = ai_choice()
  user_selection = user_choice()

  # Print the choices
  print(f"You chose: {user_selection}")
  print(f"AI chose: {ai_selection}")

  # Determine the winner
  winner = determine_winner(user_selection, ai_selection)
  print(winner)

  # Play again prompt
  play_again = input("Play again? (y/n): ").lower()
  if play_again != "y":
    break

print("Thank you for playing!")

Explanation:

  • The ai_choice function uses the random library to pick a random choice from the choices list.
  • The user_choice function ensures the user enters a valid choice using a loop.
  • The determine_winner function checks the choices and applies the RPS rules to determine the winner or tie.
  • The main game loop runs until the user decides not to play again.

Running the Script:

  1. Save the script as rps_game.py.
  2. Open a terminal or command prompt and navigate to the directory where you saved the script.
  3. Run the script using the command python rps_game.py.

More Applications of Artificial intelligence

Artificial intelligence (AI) is rapidly transforming our world, and its applications are becoming increasingly widespread across various industries. Here’s a small list of some of the most notable applications of AI:

  • Healthcare: AI is revolutionizing healthcare by assisting in disease diagnosis, drug discovery, and personalized medicine.
  • Finance: AI is used in fraud detection, risk management, and automated financial advising.
  • Customer Service: AI-powered chatbots provide 24/7 customer support, answer questions, and resolve issues.
  • Manufacturing: AI is used to optimize production processes, predict equipment failures, and improve quality control.
  • Transportation: AI is driving the development of self-driving cars, and is used in traffic management and logistics optimization.
  • Entertainment: AI is used to personalize recommendations for music, movies, and other forms of entertainment.
  • Security: AI is used in facial recognition, anomaly detection, and cyber security threat identification.
  • Climate Change: AI is being used to model climate change, develop sustainable solutions, and predict extreme weather events.

These are just a few examples, and the potential applications of AI continue to grow rapidly. As AI technology continues to evolve, we can expect to see even more innovative and impactful applications emerge in the years to come. These are the very good uses of AI.

Tags: rock paper scissors card game, google snake game, applications of Artificial intelligence, ai applications, applications of ai, artificial intelligence applications, ai apps, ai application, uses of ai

Previous
Exploring AI Future Trends and Challenges with examples
Next
Building a Recommendation System using Artificial Intelligence (AI)