- 2
How to make a game?
14 odpowiedzi
+ 3
Here's a little game ;-)
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
int guess;
int toGuess;
std::srand(std::time(0));
while(true) {
std::cout << "Guess a number between 0 and " << RAND_MAX << "!\n";
toGuess = std::rand();
do {
std::cout << "Your guess:";
std::cin >> guess ;
std::cout << "\n";
if (guess == toGuess) {
std::cout << "Great job! It was " << toGuess << ", indeed!\n";
} else {
std::cout << "Not the number, pls try again.\n";
}
} while ( guess != toGuess);
}
}
+ 2
// Here's another little game.
// As I felt that my last game was
// lacking excitement, I tried to add some.
// I call my new game "Type or die" :-)
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
int main() {
unsigned kTimeoutInSecs = 2;
std::string words[] = {
"hello", "challenge",
"d1ff1cul+",
"can you write this fast enough?",
"3v3n m0re difficult",
};
std::string wordTyped;
std::srand(std::time(0));
std::cout << "Type the words you see in less than " << kTimeoutInSecs << " secs." << std::endl;
do {
int index = std::rand() % 5;
int before = std::time(0);
std::cout << words[index] << ": ";
std::cin >> wordTyped;
int after = std::time(0);
unsigned timeSpentInSecs = after - before;
if (wordTyped != words[index]) {
std::cout << "Didn't get the word." << std::endl;
}
if (timeSpentInSecs >= kTimeoutInSecs) {
std::cout << "Typing took longer than " << kTimeoutInSecs << " seconds." << std::endl;
}
if (std::rand() % 10 == 0) {
std::cout << "Illegal memory access" << std::endl;
int* selfDestruct = reinterpret_cast<int*>(std::rand());
delete selfDestruct;
// it should crash before this line
exit(0);
}
} while(true);
}
+ 1
Sardor, you got my joke, hehe ;-) Finally someone who understands the weird jokes I make. :-)
0
any other short game coding?
0
it shows the compilation error.
0
My last edit should fix it. Whitespace was not as the compiler wanted (weird error).
I'm not entirely sure if the program works as expected here on SoloLearn C++.
0
Did anyone understand what game2 does? >:)
0
to type a word what we see in limited time?
0
the words*
0
Jep and if you don't succeed... there is about a 10% chance that something happens...
0
but Idk why is it showing the compilation error
0
Oh, this was because of the whitespace problem. I can share my code as I resolved the whitespace problem in the SoloLearn C++ Code Playground: http://www.sololearn.com/app/cplusplus/playground/cmfdnP9wfQWe/
0
ah okay thanks!
0
Nevertheless, the code does not work very well in SoloLearn C++... The basic idea was to write a "typing trainer" that is bullying the user a bit because for every wrong or too slow answer there is a 10% probability that the program will crash.
Therefore, if the user realizes that wrong or too slow answers crash the program he or she might be inclined to answer better and faster ;-)