ref: 28c05ee3266fbdac9c6a89457ee62f6b21f93721
parent: c25c807edfd4d1947581f89d95774a5e553dc371
author: mkf <mkf@d510>
date: Wed Apr 19 09:59:09 EDT 2023
fixed main(), digit(), compere() and made getguess to accept (ignore) spaces. still needs some more checks.
--- a/game.cpp
+++ b/game.cpp
@@ -2,7 +2,12 @@
Mastermind impl.
todo:
- fix XXXs in code,
+ fix main()
+ ask for "Do you dare to leave this great game?"
+ check harder for length differs in either main or compere or getguess
+ write proper comments and strings (printrules?)
+ check digit, etc? errors
+
*/
#include <iostream>
@@ -11,14 +16,14 @@
// io functions
void printrules();
-uint getlen();
-uint getguess();
-void compere(uint goal, uint guess);
+ulong getlen();
+ulong getguess();
+void compere(long goal, long guess);
// aux functions
-uint length(uint n);
-uint gengoal(uint n);
-int digit(int number, int index);
+uint length(ulong n);
+ulong gengoal(ulong n);
+int digit(long number, uint index);
void printrules()
{
@@ -25,50 +30,50 @@
cout << "Hi," << endl;
}
-uint getlen()
+ulong getlen()
{
- int len;
- /* hopefully not long */
+ ulong len;
+ /* shouldn't be longer than sizeof(len) */
cout << "how long? " << endl;
cin >> len;
- while(len < 1)
+ if(len < 1 || len > 19)
{
- cerr << "Sorry, please enter a 1+ number" << endl;
+ cerr << "Sorry, please enter a 0 < x < 20 number" << endl;
len = getlen();
}
return len;
}
-uint getguess()
+ulong getguess()
{
/* "sang bozorg neshane nazadan ast" */
char guess[512];
- int i = 0, temp = 0;
+ int i = 0;
+ ulong temp = 0;
cout << "guess? " << endl;
- cin >> guess;
- while(guess[i] != '\0')
+ cin.getline(guess, 512);
+ while(guess[i] != 0)
{
- /* see if it's either a number or space,
- SCREAM loudly otherwise! */
+ /* see if it's either a number or space, SCREAM otherwise! */
if((guess[i] >= '0') && (guess[i] <= '9'))
{
/* because we can't put ascii into int directly */
- temp = (guess[i] - '0') + temp;
temp *= 10;
+ temp += (guess[i] - '0');
}
-
else if(guess[i] != ' ')
{
- cerr << "That's not a number" << endl;
+ cerr << "That's not a number." << endl;
return getguess();
- }
+ }
+ i++;
}
return temp;
}
-uint length(uint n)
+uint length(ulong n)
{
- uint t = 0;
+ ulong t = 0;
while(n > 0)
{
n /= 10;
@@ -77,21 +82,21 @@
return t;
}
-/* generates an uint, with n digits,
- used to make a goal */
-uint gengoal(uint n)
+/* generates a goal, n digits long */
+ulong gengoal(ulong n)
{
- uint num = 0;
+ ulong num = 0;
/* it's technically legal, but
we get a int that's shorter than n */
do
num = rand() % 10;
- while (num == 0)
+ while (num == 0);
- while(n > 0)
+ /* we have already generated one digit */
+ while(n > 1)
{
num *= 10;
- num += rand()%10;
+ num += rand() % 10;
n--;
}
return num;
@@ -100,40 +105,60 @@
/* we could also use arrays for this,
alas, that would a can of worms
digit(number, index) = number[index] */
-int digit(int number, int index)
+int digit(long number, uint index)
{
if((index > length(number)) || (index < 0))
return -1;
int i = 0, j;
- while(i < index)
+ while(i <= index)
{
j = number % 10;
- number /= 10;
+ number /= 10;
i++;
}
return j;
}
-void compere(uint goal, uint guess)
+void compere(ulong goal, ulong guess)
{
- int i = 0;
- while(i < length(goal))
+ int i = length(goal) - 1;
+
+ while(i+1 > 0)
{
if(digit(goal, i) == digit(guess, i))
cout << '#';
else
cout << 'X';
- i++;
+ i--;
}
+ cout << endl;
}
int main()
{
- uint len, goal;
+ int lives, len, flag = 1;
+ ulong goal, guess;
printrules();
len = getlen();
+ /* you may try all digits in 9+, we don't want that*/
+ (len > 9) ? lives = 9 : lives = len;
goal = gengoal(len);
- cout << len << endl;
- cout << goal << endl;
-}
\ No newline at end of file
+ // cout << "psst. here's a hint: goal = " << goal << endl;
+ /* flush the buffer */
+ cin.ignore();
+ while(lives > 0 && flag)
+ {
+ guess = getguess();
+ compere(goal, guess);
+ if(goal != guess)
+ lives--;
+ else
+ {
+ cout << "You win, d(o.o)" << endl;
+ flag = 0;
+ }
+ }
+ if(lives == 0 && flag)
+ cout << "you lose" << endl;
+}