/*Make a game play the slot machine, can * +read save game and read value from file */ #include #include #define MIN_VALUE 1 #define MAX_VALUE 3 #define MONEY_START 10.00 float readValueFormFile(FILE *fptr); int getInt(int, int); int menu(FILE *fptr, float, int); int makeRandom(int, int); void separatingDigitsIntoArray(int, int, int three_digitNum[]); int randomThree_digitNumber(int, int); float playTheSlotMachine(float); void saveMoney(FILE *fptr, float); int main() { int isContinue = 1, times = 1; int choose; //declare pointer of file FILE *fptr; //read value of money form file float money = readValueFormFile(fptr); printf("You have $%.2f.\n", money); do { //call function menu, input choice choose = menu(fptr, money, times); switch (choose) { case 1: //play game the slot machine money = playTheSlotMachine(money); break; case 2: //Save money to file saveMoney(fptr, money); break; case 3: // print out money & break out of loop printf("Thank you for playing! You end with $%.2f!", money); isContinue = 0; } // number of time add 1 times++; } while (isContinue == 1); return 0; } int getInt(int minValue, int maxValue) { int value; char check; //42a do { int rc = scanf("%d%c", &value, &check); fpurge(stdin); if (rc != 2 || check != '\n') { printf("Invalid value!\n"); } else if (value < minValue || value > maxValue) { printf("Value out of range!\n"); } else { // rc == 2 && check == '\n' return value; //break out of function } } while (1); } float readValueFormFile(FILE *fptr) { //file savegame.txt if it was made fptr = fopen("D:\\Aaa Subjects\\FU2\\LAB101\\CLAB\\C.L.P0003.cfffff\\savegame.txt", "r+"); //check the existence of the file if (fptr == NULL) { //file not yet made --> make new file savegame.txt fptr = fopen("D:\\Aaa Subjects\\FU2\\LAB101\\CLAB\\C.L.P0003.cfffff\\savegame.txt", "w"); } float money; //declare variable check to check the existence of file's data int check = fscanf(fptr, "%f", &money); if (check == -1) { // empty file -- > can't get value from file //if empty file , print $MONEY_START to file fprintf(fptr, "%f", MONEY_START); //assign value for variable money money = MONEY_START; } //close file fptr fclose(fptr); return money; } int menu(FILE *fptr, float money, int times) { int choose; if (money <= 0) {//check : if money <= 0,print MONEY_START to file for next time & end game saveMoney(fptr, MONEY_START); choose = 3; } else { printf("[%d]-Chose one of the following menu options: \n", times); printf("1) Play the slot machine.\n"); printf("2) Save game.\n"); printf("3) Cash out.\n"); choose = getInt(MIN_VALUE, MAX_VALUE); } //return user's choice return choose; } void saveMoney(FILE *fptr, float money) { //open file savegame.txt to write value of money to this file fptr = fopen("D:\\Aaa Subjects\\FU2\\LAB101\\CLAB\\C.L.P0003.cfffff\\savegame.txt", "w"); fprintf(fptr, "%f", money); fclose(fptr); printf("Your money have saved.\n"); } int makeRandom(int minRand, int maxRand) { //make a random number by time srand((unsigned) time(NULL)); //make a random number from minRand to maxRand int numRand = minRand + rand() % (maxRand + 1 - minRand); return numRand; } void separatingDigitsIntoArray(int intNum, int sizeOfArray, int three_digitNumber[]) { //separate digits of number to array three_digitNumber[] for (int i = sizeOfArray - 1; i >= 0; i--) { three_digitNumber[i] = intNum % 10; intNum = intNum / 10; } } int randomThree_digitNumber(int minRand, int maxRand) { int three_digitNumber[3]; //make random a number from (0, 999) int numRand = makeRandom(minRand, maxRand); //Separating digits into Array three_digitsNumber[3] separatingDigitsIntoArray(numRand, 3, three_digitNumber); printf("The slot machine shows "); for (int i = 0; i < 3; i++) { printf("%d", three_digitNumber[i]); } printf("\n"); //Count Combination--> 0, 1, 2 int countCombination; if (three_digitNumber[0] == three_digitNumber[1] && three_digitNumber[0] == three_digitNumber[2]) { countCombination = 2; } else if (three_digitNumber[0] == three_digitNumber[1] || three_digitNumber[0] == three_digitNumber[2] || three_digitNumber[1] == three_digitNumber[2]) { countCombination = 1; } else countCombination = 0; return countCombination; } float playTheSlotMachine(float money) { //start play game -- > money - $0.25 money -= 0.25; //assign countCombination = a number form 0--> 999 by function radomThree_digitNumber(); int countCombination = randomThree_digitNumber(0, 999); if (countCombination == 2) { printf("You win the big prize, $10.00!\n"); money += 10; } else if (countCombination == 1) { printf("You win 50 cents!\n"); money += 0.5; } else { printf("Sorry you don't win anything.\n"); } printf("You have $%.2f\n", money); printf("---------------------------------------\n"); //return money for next time return money; }