Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
116 commits
Select commit Hold shift + click to select a range
28c57d6
Update .gitignore for .DS_Store
CB4747 Sep 20, 2024
05da80f
add textGame.c
CB4747 Sep 21, 2024
16e5884
Update textGame.c
CB4747 Sep 21, 2024
4ac6364
add function to use for randomness
CB4747 Sep 21, 2024
371949c
add params to LCG
CB4747 Sep 21, 2024
1a86c07
do hardcoded values instead of parameters
CB4747 Sep 21, 2024
ea1bd19
Add actual algorithm
CB4747 Sep 21, 2024
7f49e81
prev between 0 and m
CB4747 Sep 21, 2024
06dadc4
add values from chart
CB4747 Sep 21, 2024
e0f0dad
add some starting values
CB4747 Sep 21, 2024
52e2270
update gametest with comment on source
CB4747 Sep 21, 2024
5805e21
main game loop
CB4747 Sep 21, 2024
eb23730
get the bet from user
CB4747 Sep 21, 2024
c216fa0
handle leaving
CB4747 Sep 21, 2024
a20d65f
break when leaving
CB4747 Sep 21, 2024
bbeac3c
create function to check if bet is valid
CB4747 Sep 21, 2024
176f64a
prevent negative bets
CB4747 Sep 21, 2024
e92a4bb
loop over string
CB4747 Sep 21, 2024
3fcd314
check if char is digit
CB4747 Sep 21, 2024
dcbfcf4
simplify the check, to check if not in ascii range of digits
CB4747 Sep 21, 2024
ddeb46c
Add a comment
CB4747 Sep 21, 2024
2534774
return 0 if one of characters not digit
CB4747 Sep 21, 2024
30c7503
keep asking for bet until we get a valid one
CB4747 Sep 21, 2024
c3e2a1f
use uptime as seed
CB4747 Sep 21, 2024
503ef6d
get new random val
CB4747 Sep 21, 2024
6020039
create new roll var
CB4747 Sep 21, 2024
d43fc91
get roll
CB4747 Sep 21, 2024
49d70a6
game logic
CB4747 Sep 21, 2024
94f963a
temp add blank lines
CB4747 Sep 21, 2024
bf174d7
add winning condition
CB4747 Sep 21, 2024
03a1c50
Update textGame.c
CB4747 Sep 21, 2024
38a1be9
lose, and also print out money after roll Beta
CB4747 Sep 21, 2024
067ebe7
move %m to after m defined
CB4747 Sep 21, 2024
7380d6e
fix using str instead of bet
CB4747 Sep 21, 2024
a25fd11
add missing paren around condition
CB4747 Sep 21, 2024
435aae9
replace uses of bet when i wanted betInput
CB4747 Sep 21, 2024
747e3cc
add missing ;
CB4747 Sep 21, 2024
8f7728b
fix using money instead of coins
CB4747 Sep 21, 2024
0ff3d5c
fix using print instead of printf
CB4747 Sep 21, 2024
6f89f1a
add asking about selling
CB4747 Sep 21, 2024
8747a03
add ?
CB4747 Sep 21, 2024
969eba4
response var, plus prompt for response for selling
CB4747 Sep 21, 2024
3fdba54
fix '' instead of "" and start handling selling
CB4747 Sep 21, 2024
49c358a
ask how much to sell
CB4747 Sep 21, 2024
d488a6b
handle getting amount to sell
CB4747 Sep 21, 2024
25c802f
remove the amount
CB4747 Sep 21, 2024
db4eeab
get a new random value, for use in determining selling price
CB4747 Sep 21, 2024
b908ed7
get sale price from value
CB4747 Sep 21, 2024
6058c84
update variable name
CB4747 Sep 21, 2024
0fe24ac
print info to user
CB4747 Sep 21, 2024
3e754fe
finish handling selling, adding money and printing out total
CB4747 Sep 21, 2024
9842e46
allow user to leave after winning roll of 1
CB4747 Sep 21, 2024
03c2572
handling buying stuff
CB4747 Sep 21, 2024
b0da6bd
get amount to buy
CB4747 Sep 21, 2024
f447e89
fix comment from copy and paste
CB4747 Sep 21, 2024
50ccf71
handle actcually buying
CB4747 Sep 21, 2024
477ef47
add a comment for the next part
CB4747 Sep 21, 2024
da7efa7
handling when you can buy
CB4747 Sep 21, 2024
6d1e76e
finish handling selling stuff
CB4747 Sep 21, 2024
9ea3792
allow user to buy stuff
CB4747 Sep 21, 2024
8590de4
fix sell when i meant buy
CB4747 Sep 21, 2024
1a7f680
print out profit at the end
CB4747 Sep 21, 2024
f6dff20
add missing ;
CB4747 Sep 21, 2024
69e3efe
add /n to prints
CB4747 Sep 21, 2024
07f3640
fix strcmps actually checking if not equal
CB4747 Sep 21, 2024
7b37961
formatting
CB4747 Sep 21, 2024
b6488b7
free vars
CB4747 Sep 21, 2024
c1c02ef
add function for removing trailing newlines
CB4747 Sep 21, 2024
04aa244
get string length
CB4747 Sep 21, 2024
46b547c
finish removing trailing newline function
CB4747 Sep 21, 2024
4f6f66e
call remove trailing newline whenever we use gets
CB4747 Sep 21, 2024
d45be24
Merge pull request #1 from my-name-here/dev
ChristopherBrower Sep 23, 2024
5789e65
make sure we have enough coins to make our bet
CB4747 Sep 23, 2024
e16e40b
better invalid bet error message
CB4747 Sep 23, 2024
47d379e
new variable to store starting coins amount
CB4747 Sep 23, 2024
f31f2e6
profit calc with initialCoins
CB4747 Sep 23, 2024
6f710e3
make init coins const, as well as a,c,m const
CB4747 Sep 23, 2024
88f2fcf
print that you. won things when you do
CB4747 Sep 23, 2024
3db3aa8
make stuff start value a constant, and pirnt out how many things you …
CB4747 Sep 23, 2024
ce5a77c
stop if out of money and stuff
CB4747 Sep 23, 2024
24d3c7b
Merge pull request #2 from my-name-here/dev
ChristopherBrower Sep 23, 2024
876ed83
fix message
CB4747 Sep 23, 2024
3ef0960
fix other usage of that message
CB4747 Sep 23, 2024
495a75d
fix being able to buy things when you can't afford
CB4747 Sep 23, 2024
6496fab
stop if both coins and stuff negativve or 0
CB4747 Sep 23, 2024
cf54c0b
change isValidBet name to be more descriptive
CB4747 Sep 23, 2024
e0564f3
rename param to fit better with the new func name
CB4747 Sep 23, 2024
6da9e9f
add some spaces to make room for new function for big changes
CB4747 Sep 23, 2024
609425e
functions that tell us whether user wants to buy and sell stuff
CB4747 Sep 23, 2024
3612305
implement functions to see if user wants to buy and sell
CB4747 Sep 23, 2024
ae6f795
make selling use this new function
CB4747 Sep 23, 2024
22c264c
use the new functions for buying
CB4747 Sep 23, 2024
72600cd
a function we can call to do the buying
CB4747 Sep 23, 2024
7166c77
copy the code used for buying into the function
CB4747 Sep 23, 2024
c2d5b6c
perform the fixes
CB4747 Sep 23, 2024
bca8c0e
actually use the function for buying
CB4747 Sep 23, 2024
2a68851
add function to do the selling
CB4747 Sep 23, 2024
75cfc23
paste in code for running the selling, still needs fixes
CB4747 Sep 23, 2024
06275b6
apply fixes
CB4747 Sep 23, 2024
ee7d764
fix comment to reflect code
CB4747 Sep 23, 2024
5efc93c
use the function for selling
CB4747 Sep 23, 2024
ae339a4
better print of money after selling, add newline char
CB4747 Sep 23, 2024
6d6f3b3
add another function to reduce code duplication
CB4747 Sep 24, 2024
38b7548
fill in actual implementation
CB4747 Sep 24, 2024
1c744a4
use ternary operator to simplify
CB4747 Sep 24, 2024
5c67c0b
test using this func in shouldbuy
CB4747 Sep 24, 2024
9c7e3a0
test simplifying return further
CB4747 Sep 24, 2024
5528db5
remove unneeded comment
CB4747 Sep 24, 2024
85cbdbe
simplify should sell with this func
CB4747 Sep 24, 2024
67e9b26
combine should sell print statements into 1
CB4747 Sep 24, 2024
f6f4c5f
make user input for leaving use this func
CB4747 Sep 24, 2024
aacac33
delete extra blank line
CB4747 Sep 24, 2024
b01fd8f
finish combining them
CB4747 Sep 24, 2024
092533e
remove shouldBuy and ShouldSell
CB4747 Sep 24, 2024
39256fd
replace usage of shouldbuy and shouldsell with new func
CB4747 Sep 24, 2024
d0a7012
Merge pull request #3 from my-name-here/dev
ChristopherBrower Sep 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ mkfs/mkfs
kernel/kernel
user/usys.S
.gdbinit
.DS_Store
161 changes: 161 additions & 0 deletions user/textGame.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#include "kernel/types.h"
#include "kernel/stat.h"
#include "kernel/fs.h"
#include "kernel/fcntl.h"
#include "user/user.h"

int LCG(int prev){// function that generates a psuedorandom number, using the previous value in the sequence, which starts at a seed

//https://www.ams.org/journals/mcom/1999-68-225/S0025-5718-99-00996-5/S0025-5718-99-00996-5.pdf has table of values
const long a = 530877178;
const long c = 0;//c = 0, since it is what the chart has
const long m = 536870909;// 2^29 -3
prev = prev % m;
return (a*prev+c)%m;
}
int isNonNegativeInt(char *inString){
if (inString[0] == '-'){//no negative bets allowed
return 0;
}
int i = 0;
while (inString[i] != '\0') {//loop over every char in string
if (inString[i] < '0' || inString[i] > '9'){// not a digit
return 0;
}
i++;
}
return 1;

}
void removeTrailingNewline(char *string){
int stringLength = strlen(string);
if (stringLength > 0 && string[stringLength-1] == '\n'){//if string has at least one character, and last char is newline
string[stringLength-1] = '\0';
}
}

int getInputAndCompare(char *userInput, char *comparisonString){
gets(userInput, 128);
removeTrailingNewline(userInput);
return (strcmp(userInput, comparisonString) == 0);//1 if they match, 0 otherwise
}

int shouldBuyOrSellStuff(char *responseInput, int isSelling){//isSelling is 1 if selling, 0 if buying
printf("would you like to %s\n", (isSelling ? "sell some of your stuff yes/no" : "buy some things yes/no"));//use ternary operator to change the print depending on if buying or selling
return getInputAndCompare(responseInput, "yes");
}

void performBuying(int *coinAmount, int *stuffAmount, char *responseInput){
printf("how many things would you like to buy for $50 each?\n");
gets(responseInput, 128);// get amount to buy in response var
removeTrailingNewline(responseInput);
while (!isNonNegativeInt(responseInput) || atoi(responseInput)<0 || 50*atoi(responseInput)>*coinAmount){//reuse bet handling to check if we can convert, and check we are buying positive amount, and that we have enough money
printf("invalid input. Make sure you input a positive int, and have enough money to buy that many.\n");
printf("how many things would you like to buy for $50 each?\n");
gets(responseInput, 128);// get amount to buy in response var
removeTrailingNewline(responseInput);
}
*stuffAmount = *stuffAmount + atoi(responseInput);//increase stuff
*coinAmount = *coinAmount - atoi(responseInput)*50;//decrease money

}

void performSelling(int *coinAmount, int *stuffAmount, int *randomNumber, int maxSalePrice, char *responseInput){
printf("how many things would you like to sell?\n");
gets(responseInput, 128);// get amount to sell in response var
removeTrailingNewline(responseInput);

while (!isNonNegativeInt(responseInput) || atoi(responseInput)>*stuffAmount){//reuse bet handling to check if we can convert, and check we have enough stuff
printf("invalid input. Make sure you input a non-negative integer, and you have enough stuff.\n");
printf("how many things would you like to sell?\n");
gets(responseInput, 128);// get amount to sell in response var
removeTrailingNewline(responseInput);

}
*stuffAmount = *stuffAmount - atoi(responseInput);
*randomNumber = LCG(*randomNumber);// get a new random val with LCG
int salePrice = *randomNumber%(maxSalePrice-1)+1;//random int from 1-maxSalePrice
printf("your stuff sold for $%d each\n", salePrice);
printf("you have %d things\n", *stuffAmount);
*coinAmount = *coinAmount + salePrice * atoi(responseInput);
printf("You now have $%d\n", *coinAmount);
}

int main() {// a reimplementation of a simple casino game I wrote in python a while ago
const int initialStuff = 12;// starting stuff amount
const int initialCoins = 100;// starting coins amount
int coins = initialCoins;//set coins var
int stuff = initialStuff;//set stuff var
char *betInput = malloc(128*sizeof(char));// a var for holding the bet
char *responseInput = malloc(128*sizeof(char));//a var for holding various responses
int randVal = uptime();// set seed for lcg as uptime
int roll = 0;//set roll var to 0;
while (coins >= 0){//keep going while coins <= 0, not < because allows you to sell stuff at 0
if (coins <= 0 && stuff <= 0){//stop if you are out of money and stuff(changed from equals zero, since if negative, also want to stop)
break;
}
printf("how much to bet (type leave to leave the casino)\n");
if (getInputAndCompare(betInput, "leave")){//handle input of leave
break;
}
//not leave, so now check if it is a valid bet
while (!isNonNegativeInt(betInput) || atoi(betInput)>coins){//make sure it is valid int input, if it is, also make sure we have enough coins
printf("invalid bet. make sure you give a positive integer, and have enough money.\n");
printf("how much to bet\n");
gets(betInput, 128);
removeTrailingNewline(betInput);
}
randVal = LCG(randVal);// get a new random val with LCG
roll = (randVal % 97) + 1; // get a roll from 1-98
if (roll == 1){// win more coins if roll is one
coins = coins + atoi(betInput)*48;//win 48 times your bet
printf("you won\n");
if (shouldBuyOrSellStuff(responseInput, 1)){//does the user want to sell
printf("you have %d things\n", stuff);// print out how much stuff we have, so the user knows
performSelling(&coins, &stuff, &randVal, 100, responseInput);
}
printf("would you like to leave yes/no\n");
if (getInputAndCompare(responseInput, "yes")){//said yes to leaving
break;
}
}
else if (roll == 2){//win stuff if roll is 2
stuff=stuff+(atoi(betInput));// win your bet in stuff
printf("you won %d things\n", atoi(betInput));
printf("you have %d things\n", stuff);
if (shouldBuyOrSellStuff(responseInput, 0)){//user wants to buy things
performBuying(&coins, &stuff, responseInput);

}
}
else{//you lose
coins=coins-atoi(betInput);// lose your bet
printf("you lose\n");

}
printf("you have $%d\n", coins);//print money after bet
//now allow the user to sell stuff if they are out of money.
if (coins <= 0){//not enough money
if (shouldBuyOrSellStuff(responseInput, 1)){//user wants to sell
if (stuff<=0){//out of money and stuff
printf("not enough stuff\n");
break;
}
else{//have at least some stuff
printf("you have %d things\n", stuff);// print out how much stuff we have, so the user knows
performSelling(&coins, &stuff, &randVal, 20, responseInput);
}
}
}
// allow user to buy stuff
if (shouldBuyOrSellStuff(responseInput, 0)){//user wants to buy things
performBuying(&coins, &stuff, responseInput);
}
}
printf("profit was $%d\n",coins-initialCoins);
printf("you gained %d things\n",stuff-initialStuff);

free(betInput);
free(responseInput);
return 0;
}