Thank you all for your contribution. However adding to the answer I got that went like this
import 'dart:io';
void main() {
print(getch());
}
int getch() {
stdin.echoMode = false;
stdin.lineMode = false;
return stdin.readByteSync();
}
I decided to add something to make it more like the getch() function in conio.h header file in c language. The code goes like this
import 'dart:io';
void main() {
print(getch());
}
String getch() {
stdin.echoMode = false;
stdin.lineMode = false;
int a = stdin.readByteSync();
return String.fromCharCode(a);
}
Although it only works on cmd, powershell and linux terminal and not on intelliJ, it is better than nothing. The most important thing is to get the foundation of dart for things like flutter and web. And with this little knowledge, I was put it into practice and make a simple and basic typing game in dart. The code is below:
import 'dart:io';
import 'dart:convert';
import 'dart:core';
void main() {
Stopwatch s = Stopwatch();
String sentence = 'In the famous battle of Thermopylae in 480 BC, one of the most famous battles in history, King Leonidas of Sparta said the phrase'
' Molon Labe which means \"come and take them\" in ancient greek to Xerxes I of Persia when the Persians asked the Spartans to lay'
' down their arms and surrender.';
List<String> sentenceSplit = sentence.split(' ');
int wordCount = sentenceSplit.length;
print('Welcome to this typing game. Type the words you see on the screen below\n\n$sentence\n\n');
for (int i=0; i<sentence.length; i++) {
if(i==1) {
s.start(); // start the timer after first letter is clicked
}
if(getch() == sentence[i]) {
stdout.write(sentence[i]);
}
else {
i--;
continue;
}
}
s.stop(); // stop the timer
int typingSpeed = wordCount ~/ (s.elapsed.inSeconds/60);
print('\n\nWord Count:\t$wordCount words');
print('Elapsed time:\t${s.elapsed.inSeconds} seconds');
print('Typing speed:\t$typingSpeed WPM');
}
String getch() {
stdin.echoMode = false;
stdin.lineMode = false;
int a = stdin.readByteSync();
return String.fromCharCode(a);
}
You can go ahead and advance to make it a way that when the user starts the
game again, it should show a different text so they don't get used to it. But anyway, that is it for this question. It is officially closed. Although, if you have anymore to add, feel free to drop it here. Thank you!