Download it here: http://www.mediafire.com/?4hkgr94rmegu3eb
Download the source code here: http://www.mediafire.com/?rfgdtll8hkiag62
Yes, I’ve finally finished my first game: Dice 8.
Development has been a little bit quiet since my computer got wiped. I was forced to bring it back to factory state and do a system restore. However, the good news is that I was able to start programming again after I re-downloaded the Code::Blocks IDE. I find Code::Blocks the ONLY IDE I was able to set up, and so, it has become my only IDE. I couldn’t find out how to get Eclipse to work, and how to get NetBeans to extend its functionality to C++. I am currently using the MinGW compiler that optionally comes with Code::Blocks.
Dice 8 is a simple game that uses:
- iostream
- cstdlib (The C standard library, I’m assuming?)
- ctime (Part of cstdlib?)
- conio.h (Have no idea about this)
- process.h (Have no idea about this too)
I have imposed no copyright on it; how could I anyways? It’s a pathetic little program that pits you against another person to roll the same number as the computer. You can only choose the numbers 1 – 8.
I figured I should use a string to store all input information–after all, you can’t go wrong if you can store both characters and numbers. When the user is required to input a number, even inputting “c” or “%623498″ will not crash the program. The code used to get input from the user is here:
int getInt() {
unsigned int number = 0;
while ((number != 1) || (number !=2) || (number !=3) || (number !=4) || (number !=5) || (number !=6) || (number !=7) ||(number != 8)) {
cin >> linein;
number = atoi(linein.c_str());
if ((number == 1) || (number == 2) || (number == 3) || (number == 4) || (number == 5) || (number == 6) || (number == 7) || (number == 8)) {
break;
}
else {
system("cls");
cout << "You have input an invalid string of characters." << endl;
cout << "Please input a number from 1 to 8: ";
}
}
return number;
}
As you can see, I still use the std::in for getting input, but it puts the input in a global variable named “linein,” which is my shorthand for “the input line/string.” linein is then converted to an integer by the function atoi(). If the value stored in linein is not an integer, because the int number cannot hold character data, any letters or special characters are not accepted. This function, getInt(), then compares the integer number to the integer constants 1 through 8, and if int number is equal to 1, 2, 3, 4, 5, 6, 7, or 8, then the while-if-else statement is broken, and getInt() function then returns the current value of the integer number, which is limited to 1 through 8 (because you checked number to see if it was a number between 1 or 8).
This entire function ensures that the user’s input is guaranteed converted to a valid integer.
You may look at the source code yourself. It has already been uploaded to MediaFire.