Skip to content

Instantly share code, notes, and snippets.

@ashraf267
Created August 3, 2023 23:32
Show Gist options
  • Select an option

  • Save ashraf267/d25899698cf0664b7bc388bb8b92decd to your computer and use it in GitHub Desktop.

Select an option

Save ashraf267/d25899698cf0664b7bc388bb8b92decd to your computer and use it in GitHub Desktop.
Although needs minor tweak, I managed to write a command-line Rock, Paper & Scissors dart game.
import 'dart:io';
import 'dart:math';
enum Options { r, p, s }
void main() {
// call func
rockPaperScissors();
}
void rockPaperScissors() {
stdout.write('Rock, Paper or Scissors? (r/p/s): ');
final playerInput = stdin.readLineSync();
List<Options> options = Options.values;
final aiInput = options[Random().nextInt(3)].name.toString();
print('You chose: $playerInput');
print('AI chose: $aiInput');
if (!(playerInput == 'q')) {
// 'q' was not pressed
// compare choices
if (playerInput == aiInput) {
// draw
print('Draw!');
} else {
// not equal
if ((playerInput == 'r' && aiInput == 'p') ||
(playerInput == 'p' && aiInput == 'r')) {
// rock vs paper
print('paper wins!');
} else if ((playerInput == 'r' && aiInput == 's') ||
(playerInput == 's' && aiInput == 'r')) {
// rock vs scissors
print('rock wins!');
} else if ((playerInput == 's' && aiInput == 'p') ||
(playerInput == 'p' && aiInput == 's')) {
// scissors vs paper
print('scissors wins!');
} else {
print('Invalid input');
}
}
// recursion
rockPaperScissors();
} else {
// 'q' was pressed
print('Exit!');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment