Yam Code
Sign up
Login
New paste
Home
Trending
Archive
English
English
Tiếng Việt
भारत
Sign up
Login
New Paste
Browse
/*Make a game play the slot machine, can * +read save game and read value from file */ #include<stdio.h> #include<stdlib.h> #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_degitNum[]); 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; }
Paste Settings
Paste Title :
[Optional]
Paste Folder :
[Optional]
Select
Syntax Highlighting :
[Optional]
Select
Markup
CSS
JavaScript
Bash
C
C#
C++
Java
JSON
Lua
Plaintext
C-like
ABAP
ActionScript
Ada
Apache Configuration
APL
AppleScript
Arduino
ARFF
AsciiDoc
6502 Assembly
ASP.NET (C#)
AutoHotKey
AutoIt
Basic
Batch
Bison
Brainfuck
Bro
CoffeeScript
Clojure
Crystal
Content-Security-Policy
CSS Extras
D
Dart
Diff
Django/Jinja2
Docker
Eiffel
Elixir
Elm
ERB
Erlang
F#
Flow
Fortran
GEDCOM
Gherkin
Git
GLSL
GameMaker Language
Go
GraphQL
Groovy
Haml
Handlebars
Haskell
Haxe
HTTP
HTTP Public-Key-Pins
HTTP Strict-Transport-Security
IchigoJam
Icon
Inform 7
INI
IO
J
Jolie
Julia
Keyman
Kotlin
LaTeX
Less
Liquid
Lisp
LiveScript
LOLCODE
Makefile
Markdown
Markup templating
MATLAB
MEL
Mizar
Monkey
N4JS
NASM
nginx
Nim
Nix
NSIS
Objective-C
OCaml
OpenCL
Oz
PARI/GP
Parser
Pascal
Perl
PHP
PHP Extras
PL/SQL
PowerShell
Processing
Prolog
.properties
Protocol Buffers
Pug
Puppet
Pure
Python
Q (kdb+ database)
Qore
R
React JSX
React TSX
Ren'py
Reason
reST (reStructuredText)
Rip
Roboconf
Ruby
Rust
SAS
Sass (Sass)
Sass (Scss)
Scala
Scheme
Smalltalk
Smarty
SQL
Soy (Closure Template)
Stylus
Swift
TAP
Tcl
Textile
Template Toolkit 2
Twig
TypeScript
VB.Net
Velocity
Verilog
VHDL
vim
Visual Basic
WebAssembly
Wiki markup
Xeora
Xojo (REALbasic)
XQuery
YAML
HTML
Paste Expiration :
[Optional]
Never
Self Destroy
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Status :
[Optional]
Public
Unlisted
Private (members only)
Password :
[Optional]
Description:
[Optional]
Tags:
[Optional]
Encrypt Paste
(
?
)
Create New Paste
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Site Languages
×
English
Tiếng Việt
भारत