Rock, Paper And Scissor Game In Dart Programming Language With AI.

Rock, Paper And Scissor Game In Dart Programming Language With AI.

In this tutorial, you will learn how to create a rock , paper and scissor game in pure Dart language and play against an AI. You read that right with AI. This is an easy project to practice and understand Dart programming concepts. I believe anyone with previous programming experience has encountered this type of project in other languages, and well it's a common game among normal people. First of all, lets understand how the game will work for this project:

  • ask for players input
  • compare the input with the computer's choice
  • give an output depending on who won, lost or if its a tie. That's it.

I will divide the project into 5 parts :

  • importing libraries
  • rules for the game
  • initial score
  • computer options
  • game logic

Import libraries

On our first step, we are importing some inbuilt libraries which we would use later in the project.

import 'dart:io';
import 'dart:math';

Rules of the game

In this we are defining the rules of the game and writing the main function.

void main() {
  print("welcome to Rock, Paper, Scissors\nType 'exit' to stop the game");
  final random = Random();
  //Rules of the game
  Map<String, String> rules = {
    "rock": "scissors",
    "scissors": "paper",
    "paper": "rock"
  };

The first step here, I have a print() function with an introductory message which will be shown in the console. The next line I have defined a variable random assigned to Random() which is called from the math library 'dart:math' .

Remember we are using final keyword at the begin of the variable because it can't be modified.

This is the important part of this section, here we have the rules of the game stored in a map data type named rules.

The reason for doing this is for the computer to understand how the game works and how each value pairs to each other.

Initial score

This part stores our score points and outputs them when exiting the game to know who had the highest score.

int user = 0;
int comp = 0;

Computer options

In this section, the computer has its options stored in a list.

List<String> options = ['rock', 'paper', 'scissors'];

Main Game Logic

while (true) {
    String computerChoice = options[random.nextInt(options.length)];
    stdout.write("\nPlease choose Rock, paper or Scissors:");
    String? userChoice = stdin.readLineSync()?.toLowerCase();

    if (userChoice == "exit") {
      print("\nYou: $user Computer: $comp\nBye Bye!");
      break;
    }
    if (!options.contains(userChoice)) {
      print('Incorrect choice');
      continue;
    } else if (computerChoice == userChoice) {
      print('We have a tie: $computerChoice vs $userChoice');
    } else if (rules[computerChoice] == userChoice) {
      print('Computer wins: $computerChoice vs $userChoice');
      comp += 1;
    } else if (rules[userChoice] == computerChoice) {
      print('You win: $userChoice vs $computerChoice');
      user += 1;
    }
  }

In this part, our main logic is stored inside a while loop to avoid recompiling of the project after every game session. On the first line We have a String Variable computerChoice, assigned to options the list from our last section. Its main aim is to get random options for the computerChoice which are used against users options. On the next line, we have stdout.write() function which basically writes an output on the console. The following line, userChoice string is defined and assigned to stdin.readLineSync() which reads user input.

Its followed by an if() function which terminates the game after a user types 'exit'. If you look closely, we have two referenced variables from our initial score section which means, as said earlier all our score points we be shown here as we exit the game.

In our last part, we have if else() statements which shows what happens when the game is played and how each participant option affects their output.

That's it.

Here is the full source code.

//import dart inbuilt libraries.
import 'dart:io';
import 'dart:math';

void main() {
  print("welcome to Rock, Paper, Scissors\nType 'exit' to stop the game");
  final random = Random();
  //Rules of the game
  Map<String, String> rules = {
    "rock": "scissors",
    "scissors": "paper",
    "paper": "rock"
  };
  //initial score

  int user = 0;
  int comp = 0;
  //options for computer to choose
  List<String> options = ['rock', 'paper', 'scissors'];

  //actual game

  while (true) {
    String computerChoice = options[random.nextInt(options.length)];
    stdout.write("\nPlease choose Rock, paper or Scissors:");
    String? userChoice = stdin.readLineSync()?.toLowerCase();

    if (userChoice == "exit") {
      print("\nYou: $user Computer: $comp\nBye Bye!");
      break;
    }
    if (!options.contains(userChoice)) {
      print('Incorrect choice');
      continue;
    } else if (computerChoice == userChoice) {
      print('We have a tie: $computerChoice vs $userChoice');
    } else if (rules[computerChoice] == userChoice) {
      print('Computer wins: $computerChoice vs $userChoice');
      comp += 1;
    } else if (rules[userChoice] == computerChoice) {
      print('You win: $userChoice vs $computerChoice');
      user += 1;
    }
  }
}

Thank you for reading my article, if you find it useful please leave us feedback or anything to keep us going..