Dart Programming Examples with Documentation
1. Number Guessing Game
A game where the user guesses a randomly generated number from 0 to 99.
import 'dart:math';
import 'dart:io';
/// A simple number guessing game where the user tries to guess a number between 0 and
99.
void main() {
int? guess;
Random rand = Random();
int answer = rand.nextInt(100); // Random number between 0-99
do {
stdout.write("Enter your guess: ");
String? input = stdin.readLineSync();
guess = int.tryParse(input ?? '');
if (guess == null) {
print("Invalid input. Please enter an integer.");
continue;
}
if (guess < answer) {
print("Too low!");
} else if (guess > answer) {
print("Too high!");
}
} while (guess != answer);
print("You got it!");
}
2. Monty Hall Problem Simulation
Simulation to validate that switching doors gives ~66.6% winning chance.
import 'dart:math';
/// Simulates the Monty Hall problem to show the advantage of switching doors.
void main() {
const int TRIALS = 1000000;
int correct = 0;
Random rand = Random();
for (int i = 0; i < TRIALS; i++) {
int winningDoor = rand.nextInt(3) + 1;
int guess = 1; // Always pick Door 1
int eliminated;
if (winningDoor == 2) {
eliminated = 3;
} else if (winningDoor == 3) {
eliminated = 2;
} else {
eliminated = rand.nextInt(2) + 2;
}
guess = (eliminated == 2) ? 3 : 2;
if (guess == winningDoor) {
correct++;
}
}
print("The percentage of correct guesses was ${(correct / TRIALS) * 100}%");
}
3. Pi Calculator
Approximates pi using the Leibniz formula with 100,000 iterations.
import 'dart:math';
/// Approximates pi using the Leibniz series: pi/4 = 1 - 1/3 + 1/5 - 1/7 + ...
void main() {
const int ITERATIONS = 100000;
double series = 1.0;
double denominator = 3.0;
double negate = -1.0;
for (int i = 0; i < ITERATIONS; i++) {
series += negate * (1 / denominator);
denominator += 2.0;
negate *= -1.0;
}
double pi = 4 * series;
print("We calculated pi as \$pi");
print("The real pi is \$PI");
print("We were off by \${PI - pi}");
}
4. Math Test
A simple arithmetic test using addition, subtraction, and multiplication.
import 'dart:io';
import 'dart:math';
/// A simple math test with random addition, subtraction, and multiplication questions.
void main() {
Random rand = Random();
int correctAnswer, userAnswer, operand1, operand2, operation;
int questionsAttempted = 0, numCorrect = 0;
while (true) {
operation = rand.nextInt(3);
operand1 = rand.nextInt(11);
operand2 = rand.nextInt(11);
switch (operation) {
case 0:
print("\$operand1 + \$operand2 = ");
correctAnswer = operand1 + operand2;
break;
case 1:
print("\$operand1 - \$operand2 = ");
correctAnswer = operand1 - operand2;
break;
case 2:
print("\$operand1 * \$operand2 = ");
correctAnswer = operand1 * operand2;
break;
default:
continue;
}
String? input = stdin.readLineSync();
try {
userAnswer = int.parse(input ?? '');
} on FormatException {
print("Invalid input. Ending game.");
print("You got \$numCorrect out of \$questionsAttempted correct.");
break;
}
if (userAnswer == correctAnswer) {
print("Correct!");
numCorrect++;
} else {
print("Wrong!");
}
questionsAttempted++;
}
}