Skip to content

Yaser-Alkhayyat #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions quiz_app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
3 changes: 3 additions & 0 deletions quiz_app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version.
2 changes: 2 additions & 0 deletions quiz_app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A sample command-line application with an entrypoint in `bin/`, library code
in `lib/`, and example unit test in `test/`.
30 changes: 30 additions & 0 deletions quiz_app/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml

# Uncomment the following section to specify additional rules.

# linter:
# rules:
# - camel_case_types

# analyzer:
# exclude:
# - path/to/excluded/files/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
12 changes: 12 additions & 0 deletions quiz_app/bin/end_message.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// print suitable message for each category of score
String endMessage({required String name, required int score, required Map<String, Map> quiz}) {
if(score == quiz.length) {
return "Well Done $name 🥳⭐\nYour score is $score/${quiz.length}";
}
else if (score > quiz.length~/2) {
return("Almost there $name 💪\nYour score is $score/${quiz.length}");
}
else {
return("Bad luck today $name 😢\nYour score is $score/${quiz.length}");
}
}
44 changes: 44 additions & 0 deletions quiz_app/bin/get_questions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// function to get shuffled questions
Map<String, Map> getQuestions() {
// initialize quiz
Map<String, Map> quiz = {
'What is git ?': {
'answers': ['Version Control System','Nickname for github','Programming language'],
"correct": 'Version Control System'
},
'What is the command to get the current status of the Git repository ?': {
'answers': ['git config --status', 'git getStatus', 'git status'],
'correct': 'git status'
},
'What is the command to initialize Git on the current repository ?': {
'answers': ['git init', 'git start', 'git begin'],
'correct': 'git init'
},
'What is the command to get the installed version of Git ?': {
'answers': ['git GetVersion', 'git --version', 'git help version'],
'correct': 'git --version'
},
'What is the command to commit with the message "New email" ?': {
'answers': ['git commit message "New email"','git commit -m "New email"','git commit --msg "New email"'],
'correct': 'git commit -m "New email"'
}
};
// initialize shuffled quiz
Map<String, Map> shuffledQuiz = {};

// create questions list and shuffle it
List questions = quiz.keys.toList();
questions.shuffle();

// loop over the questions, get each question answers and shuffle them too
for (int i = 0; i < quiz.length; i++) {
List answers = quiz[questions[i]]!['answers'];
answers.shuffle();
shuffledQuiz[questions[i]] = {
'answers': answers,
'correct': quiz[questions[i]]!['correct']
};
}
// return shuffled questions and answers
return shuffledQuiz;
}
14 changes: 14 additions & 0 deletions quiz_app/bin/menu.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'dart:io'; // library to interact with user

// function to print menu and return user choice
String menu() {
print('-'*36);
print("| ❓❔ WELCOME TO QUIZ APP ❔❓ |");
print('-'*36);
print("| Click 1 to start the quiz |");
print("| Click 0 to EXIT |");
print('-'*36);
stdout.write("Enter a choice : ");
String? choice = stdin.readLineSync();
return choice!;
}
58 changes: 58 additions & 0 deletions quiz_app/bin/play.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// imports
import 'dart:io'; // library to interact with user
import 'get_questions.dart'; // function to get questions
import 'play_again.dart'; // function to ask user for restart
import 'end_message.dart'; // function to print suitable end of quiz message

// function to start the quiz
void play({required String name}) {
// initialize score
int score = 0;

Map<String, Map> quiz = getQuestions();

// quiz starts !!
print("⚔️ Hello $name, Get Ready ⚔️");

for (int i = 0; i < quiz.length; i++) {
// get question, answers, and correct answer
String question = quiz.keys.toList()[i];
List answers = quiz[question]!['answers'];
String correct = quiz[question]!['correct'];

// show question and answers
print('Q${i + 1} : $question\n');
print('1- ${answers[0]}');
print('2- ${answers[1]}');
print('3- ${answers[2]}');

// get user answer
stdout.write('\nchoose answer 1, 2, or 3 : ');
String? answer = stdin.readLineSync();

// validation
while (answer!.isEmpty ||
int.tryParse(answer) == null ||
(int.tryParse(answer) != null && ![1, 2, 3].contains(int.parse(answer)))) {
print("Invalid answer !!");
stdout.write('Please choose an answer 1, 2, or 3 : ');
answer = stdin.readLineSync();
}

// casting to int
int userAnswer = int.parse(answer);

// check answer by comparing it with the index of the correct answer
userAnswer - 1 == answers.indexOf(correct)
? print("${answers[userAnswer-1]} is Correct ✅\nYour score now is ${++score}")
: print('${answers[userAnswer-1]} is Wrong ❌');
print('-' * 30);
}
print("The end 🏁");

// print suitable message after ending the quiz
print(endMessage(name: name, score: score, quiz: quiz));

// ask user to play again
playAgain(name: name, score: score);
}
27 changes: 27 additions & 0 deletions quiz_app/bin/play_again.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'dart:io'; // library to interact with user
import 'play.dart'; // function to start quiz

// function to ask user to play again
void playAgain({required String name, required int score}) {
// ask user to play again
print('-'*30);
print("Play Again ?");
print("0 : No");
print("1 : Sure !");
print('-'*30);
stdout.write("Enter your choice : ");
String? choice = stdin.readLineSync();

// validation
while (choice!.isEmpty || !['0', '1'].contains(choice)) {
print("Invalid choice !!");
print("Play Again ?");
print("0 : No");
print("1 : Sure !");
stdout.write("Enter your choice : ");
choice = stdin.readLineSync();
}

// if yes call play(), otherwise end the program
choice == '0' ? print("Bye bye 🖐️ .....") : play(name: name);
}
36 changes: 36 additions & 0 deletions quiz_app/bin/quiz_app.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// imports
import 'dart:io'; // library to interact with user
import 'menu.dart'; // function to display menu
import 'play.dart'; // function to start the quiz

void main() {
while_quiz:
while (true) {
String choice = menu();
switch (choice) {
// EXIT
case '0':
print("Bye bye 🖐️ .....");
break while_quiz;

// play
case '1':
// get user name
stdout.write('Enter your name : ');
String? name = stdin.readLineSync();

// validation
while (name!.isEmpty || !name.contains(RegExp(r'[A-Za-z]'))) {
print('Invalid name !!');
stdout.write('Please enter your name : ');
name = stdin.readLineSync();
}
print('-'*30);
play(name: name);
break while_quiz;

default:
print("Invalid choice !!");
}
}
}
Loading