- 2

How to make a game?

20th Jun 2016, 6:08 PM
Arham Jain
Arham Jain - avatar
14 ответов
+ 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); } }
20th Jun 2016, 7:49 PM
Stefan
Stefan - avatar
+ 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); }
21st Jun 2016, 8:35 AM
Stefan
Stefan - avatar
+ 1
Sardor, you got my joke, hehe ;-) Finally someone who understands the weird jokes I make. :-)
20th Jun 2016, 8:50 PM
Stefan
Stefan - avatar
0
any other short game coding?
21st Jun 2016, 6:55 AM
Kshitij
Kshitij - avatar
0
it shows the compilation error.
21st Jun 2016, 8:43 AM
Kshitij
Kshitij - avatar
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++.
21st Jun 2016, 9:04 AM
Stefan
Stefan - avatar
0
Did anyone understand what game2 does? >:)
21st Jun 2016, 9:21 AM
Stefan
Stefan - avatar
0
to type a word what we see in limited time?
21st Jun 2016, 9:26 AM
Kshitij
Kshitij - avatar
0
the words*
21st Jun 2016, 9:26 AM
Kshitij
Kshitij - avatar
0
Jep and if you don't succeed... there is about a 10% chance that something happens...
21st Jun 2016, 9:38 AM
Stefan
Stefan - avatar
0
but Idk why is it showing the compilation error
21st Jun 2016, 12:15 PM
Kshitij
Kshitij - avatar
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/
21st Jun 2016, 12:31 PM
Stefan
Stefan - avatar
0
ah okay thanks!
21st Jun 2016, 12:33 PM
Kshitij
Kshitij - avatar
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 ;-)
21st Jun 2016, 12:41 PM
Stefan
Stefan - avatar