1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
// Simon test
#include <stdio.h>
#include <stdlib.h> // For system
#include <unistd.h> // For sleep
#define MAXLEN 1024
// why is this looping twice on first iteration
int guess(int val[], int indexMax) {
int i = 0;
int guessVal = 0;
for (i = 0; i <= indexMax; i++) {
printf("i (low) val is %d\n", i);
printf("max is %d\n", indexMax);
printf("Guess %d >> ", i);
scanf("%d", &guessVal);
putc('\n', stdout);
if (guessVal != val[i])
return 0;
}
return 1;
}
void setVal(int val[], int index) {
val[index] = rand() / (double)RAND_MAX * 4 + 1;
}
void playValueReel(int val[], double speed, int indexMax) {
int j = 0;
for (j = 0; j <= indexMax; j++) {
printf("i (high) value is %d\n", indexMax);
printf("Value %d is %d\n", j, val[j]);
sleep(speed);
// system("cls");
}
int playGame(double speed) {
int i = 0; // for setting val
int val[1024];
for (i = 0; i < MAXLEN; i++) {
setVal(val, i);
playValueReel(val, speed, i);
if (!(guess(val, j)))
return i;
}
}
}
double diffSelect() {
int dif = 0;
printf("Select difficulty (speed):\n");
puts("1. Easy (1.0 sec)");
puts("2. Medium (0.5 sec)");
puts("3. Hard (0.25 sec)");
printf("Enter difficulty >> ");
scanf("%d", &dif);
system("cls");
if (dif == 1)
return 1;
else if (dif == 2)
return .5;
else if (dif == 3)
return .25;
else
return -1;
}
int main(void) {
puts("Simon Game test V.1\n");
printf("Score: %d", playGame(diffSelect())); //playGame() call here
return EXIT_SUCCESS;
}
|