From 0e6d89933ad030dab7204ebb9cf54a3134a7263c Mon Sep 17 00:00:00 2001 From: hoaianhkhang <46699312+hoaianhkhang@users.noreply.github.com> Date: Fri, 8 Mar 2019 09:45:48 -0500 Subject: [PATCH 1/3] Create BasicGame.c --- BasicGame.c | 510 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 510 insertions(+) create mode 100644 BasicGame.c diff --git a/BasicGame.c b/BasicGame.c new file mode 100644 index 0000000..d02d242 --- /dev/null +++ b/BasicGame.c @@ -0,0 +1,510 @@ + +#include +#include +#include +#include +#include +#define MAX 10 +#define A "P" +#define B "Q" +int getRandom(int low, int high); +int getValidInteger(int low, int high); +unsigned int playerRoll(int low, int high); + +void seed(void) +{ + srand(time(NULL)); +} +void space(unsigned int size) // create space +{ + printf(" "); +} +char getDisplayType(unsigned int index, unsigned int playerPosition, char playerName) // check the index and return character +{ + + if (playerName != '#') + { + if (index == playerPosition) + return playerName; + + if (index == 0) + return ('C'); + + if (index % 3 == 0) + if (index % 5 == 0) + if (index % 7 == 0) + return('G'); + else return ('L'); + else return ('W'); + + if (index % 5 == 0) + if (index % 7 == 0) + return('G'); + else return ('L'); + + if (index % 7 == 0) { + return('G'); + } + else return (' '); + } + + if (playerName == '#') + { + + if (index == 0) + return ('C'); + + if (index % 3 == 0) { + if (index % 5 == 0) { + if (index % 7 == 0) { + return('G'); + } + else return ('L'); + } + else return ('W'); + } + + if (index % 5 == 0) { + if (index % 7 == 0) { + return('G'); + } + else return ('L'); + } + if (index % 7 == 0) { + return('G'); + } + else + return (' '); + + + + } +} +void firstLine(unsigned int size) // create the upper line of the square for the first and last line +{ + int i; + + for (i = 0; i high) + { + printf("Invalid input, please try again: "); + } + + } while (choice < low || choice > high); + return choice; +} + +int getRandom(int low, int high) //get a rando number +{ + int a; + a = rand() % (high - low) + low; + + return a; +} + +unsigned int playerRoll(int low, int high) //prompt and output the roll +{ + int a = 1, b, c, d, e, choice; + do { + printf("\nyour turn, how many dice will you roll : "); + scanf_s("%d", &choice); + if (choice == 1) + { + b = getRandom(low, high); + //printf("b: \n"); + //scanf_s("%d", &b); + printf("You rolled %d\n", b); + printf("Advancing %d space\n", b); + a = 0; + return b; + } + else if (choice == 2) + { + c = getRandom(low, high); + //printf("c: "); + //scanf_s("%d", &c); + d = getRandom(low, high); + //printf("d: "); + //scanf_s("%d", &d); + b = c + d; + printf("You rolled %d %d\n ", c, d); + printf("Advancing %d space\n", b); + a = 0; + return b; + } + else if (choice == 3) { + c = getRandom(low, high); + d = getRandom(low, high); + e = getRandom(low, high); + b = c + d + e; + printf("You rolled %d %d %d\n ", c, d, e); + printf("Advancing %d space\n", b); + a = 0; + return b; + } + else + printf("Try again,"); + } while (a = 1); +} + +void winPrize(int playerPrizes[], unsigned int* prizeCount) //do the winprize function +{ + int i; + unsigned int prize; + prize = getRandom(10, 100); + printf("%d\n", prize); + if (*prizeCount < MAX) + { + playerPrizes[*prizeCount] = prize; + printf("you won a prize of %d\n", prize); + *prizeCount = *prizeCount + 1; + } + else + printf("Your inventory is full \n"); +} +void winGrandPrize(int playerPrizes[], unsigned int* prizeCount) // do the win grand prize function +{ + int i; + unsigned int prize; + prize = getRandom(100, 200); + printf("%d\n", prize); + if (*prizeCount < MAX) + { + playerPrizes[*prizeCount] = prize; + printf("you won a grand prize of %d\n", prize); + *prizeCount = *prizeCount + 1; + } + else + printf("Your inventory is full "); +} +int loseItem(int playerPrizes[], unsigned int *prizeCount) // do the loseitem fuction +{ + int i, j, k, r, ran = 2; + + if (*prizeCount == 0) + { + printf("Nothing happened,Move On\n"); + } + else + { + + ran = getRandom(0, *prizeCount); + playerPrizes[ran] = 0; + *prizeCount = *prizeCount - 1; + prinrf("you lost the prize &d",) + for (i = ran - 1;i < MAX; i++) //arange the array in order + for (j = i;j < MAX; j++) + if (playerPrizes[i] == 0) + { + k = playerPrizes[i]; + playerPrizes[i] = playerPrizes[j]; + playerPrizes[j] = k; + } + + } +} + + +void initPlayer(int *playerScore, int playerPrizes[], unsigned int *prizeCount, char *playerName, int *playerPosition) //do the initplayer function, set everything to 0 +{ + int i; + playerPrizes[MAX] = 0; + + *playerScore = 0; + printf("playerPrizes: %d\n", playerPrizes[MAX]); + *prizeCount = 0; + *playerPosition = 0; + printf("Enter Player ID: "); + scanf_s("%c", playerName); +} + + + + +void displayBoard(unsigned int size, unsigned int playerPosition, char playerName) //display the boardgame +{ + + int k, size1, loop; + float loop2, playerPosition1, size2; + //printf("player name in display board: %c\n", playerName); + //printf("%d\n", r); + //printf("%d", playerPosition); + playerPosition1 = (float)playerPosition; + size1 = (4 * (size - 1)); + + size2 = (float)size1; + //printf("size2: %.2f\n", size2); + // printf("playerPo1: %.2f\n", playerPosition1); + //playerPosition2 = float size; + //printf("playerPos: %d\n", playerPosition); + loop2 = playerPosition1 / size2; + + loop = trunc(loop2); + + + k = playerPosition - (4 * (size - 1))*loop; + + playerPosition = k; + + { + int i = 0; + if (size == 1) + { + printf(" ___ \n"); + printf(" | ? | \n"); + printf(" |___|"); + printf("\n"); + } + else { + + for (i = 0; i < size - 1;i++) + { + + if (i == 0) + { + firstLine(size); + secondLine(size, i, playerPosition, playerName); + thirdLine(size); + } + } + for (i = 1;i= 200) + { + return 1; + } + else + { + return 0; + } + +} + + +void playGame(unsigned int size, int *playerScore, int playerPrizes[], unsigned int *prizeCount, char *playerName, int* playerPosition) //play the game +{ + printf("playerName in playgame %c\n", *playerName); + //printf("%d\n",*prizeCount); + //printf("%d\n", *playerScore); + int i, l = 1; + while (l) + { + displayBoard(size, *playerPosition, *playerName); + + printf("Score: %d inventory (%d items): ", *playerScore, *prizeCount); + for (i = 0; i < *prizeCount; i++) { + printf("%d, ", playerPrizes[i]); + + } + + *playerPosition = *playerPosition + playerRoll(1, 6); + if (*playerPosition >= 4 * (size - 1)) + *playerPosition = *playerPosition - 4 * (size - 1); + //printf("player position in display %d\n", *playerPosition); + //printf("display type in play game: %c\n", getDisplayType(*playerPosition, *playerPosition, '#')); + //displayBoard(boardSize, playerPosition, playerName); + if (getDisplayType(*playerPosition, *playerPosition, '#') == 'G') + { + winGrandPrize(playerPrizes, prizeCount); + } + else if (getDisplayType(*playerPosition, *playerPosition, '#') == 'W') + { + + winPrize(playerPrizes, prizeCount); + } + else if (getDisplayType(*playerPosition, *playerPosition, '#') == 'L') + { + + loseItem(playerPrizes, prizeCount); + } + else if (getDisplayType(*playerPosition, *playerPosition, '#') == 'C') + { + + + if (checkout(playerScore, playerPrizes, prizeCount) == 1) + { + printf("You Win\n"); + l = 0; + } + } + else + printf("nothing happens, go again.\n"); + } +} + + + + + + + +int main(void) +{ + int i,l=1; + char a, choice; + char c = '#'; + int playerScore; + int playerPrizes[MAX]; + unsigned int prizeCount; + char playerName; + unsigned int size; + unsigned int playerPosition; + printf("Welcome to CHECKOUT\n"); + while (l) { + printf("Main Menu\n"); + printf("p-(p)lay q-(q)uit r-inst(r)uctions s-HI(s)core: \n"); + choice = getValidCharacter('P', 'Q'); + if (choice == 'p') { + printf("Number of players is 1\n"); + initPlayer(&playerScore, playerPrizes, &prizeCount, &playerName, &playerPosition); + + printf("Enter board size: "); + scanf_s("%d", &size); + + playGame(size, &playerScore, playerPrizes, &prizeCount, &playerName, &playerPosition); + + + + getchar(); + + } + if (choice == 's') + { + printf("--\n"); + printf(" \\ "); + printf("_______\n"); + printf(" \\++++++|\n"); + printf(" \\=====|\n"); + printf(" 0--- 0\n"); + printf("HI SCORE: %d Player Name: %c \n",playerScore,playerName); + } + if (choice == 'q') + { + printf("dont go, I will miss you :("); + l = 0; + getchar(); + } + } +} From 438d33c1cb738573a3816d23f1cfda32ebaa19bb Mon Sep 17 00:00:00 2001 From: hoaianhkhang <46699312+hoaianhkhang@users.noreply.github.com> Date: Fri, 8 Mar 2019 09:49:40 -0500 Subject: [PATCH 2/3] Update BasicGame.c --- BasicGame.c | 1 - 1 file changed, 1 deletion(-) diff --git a/BasicGame.c b/BasicGame.c index d02d242..8749543 100644 --- a/BasicGame.c +++ b/BasicGame.c @@ -1,4 +1,3 @@ - #include #include #include From 72d2861dcc3d1baf43971536a376e17359e3d1d5 Mon Sep 17 00:00:00 2001 From: hoaianhkhang <46699312+hoaianhkhang@users.noreply.github.com> Date: Fri, 8 Mar 2019 11:24:02 -0500 Subject: [PATCH 3/3] Update BasicGame.c --- BasicGame.c | 102 ++++++++++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/BasicGame.c b/BasicGame.c index 8749543..8dad81b 100644 --- a/BasicGame.c +++ b/BasicGame.c @@ -36,20 +36,20 @@ char getDisplayType(unsigned int index, unsigned int playerPosition, char player else return ('L'); else return ('W'); - if (index % 5 == 0) - if (index % 7 == 0) - return('G'); - else return ('L'); + if (index % 5 == 0) + if (index % 7 == 0) + return('G'); + else return ('L'); - if (index % 7 == 0) { - return('G'); - } - else return (' '); + if (index % 7 == 0) { + return('G'); + } + else return (' '); } - + if (playerName == '#') { - + if (index == 0) return ('C'); @@ -83,7 +83,7 @@ void firstLine(unsigned int size) // create the upper line of the square for th { int i; - for (i = 0; i = 200) { return 1; @@ -411,13 +411,13 @@ void playGame(unsigned int size, int *playerScore, int playerPrizes[], unsigned while (l) { displayBoard(size, *playerPosition, *playerName); - + printf("Score: %d inventory (%d items): ", *playerScore, *prizeCount); for (i = 0; i < *prizeCount; i++) { printf("%d, ", playerPrizes[i]); - + } - + *playerPosition = *playerPosition + playerRoll(1, 6); if (*playerPosition >= 4 * (size - 1)) *playerPosition = *playerPosition - 4 * (size - 1); @@ -430,17 +430,17 @@ void playGame(unsigned int size, int *playerScore, int playerPrizes[], unsigned } else if (getDisplayType(*playerPosition, *playerPosition, '#') == 'W') { - + winPrize(playerPrizes, prizeCount); } else if (getDisplayType(*playerPosition, *playerPosition, '#') == 'L') { - + loseItem(playerPrizes, prizeCount); } else if (getDisplayType(*playerPosition, *playerPosition, '#') == 'C') { - + if (checkout(playerScore, playerPrizes, prizeCount) == 1) { @@ -461,7 +461,7 @@ void playGame(unsigned int size, int *playerScore, int playerPrizes[], unsigned int main(void) { - int i,l=1; + int i, l = 1; char a, choice; char c = '#'; int playerScore; @@ -478,16 +478,16 @@ int main(void) if (choice == 'p') { printf("Number of players is 1\n"); initPlayer(&playerScore, playerPrizes, &prizeCount, &playerName, &playerPosition); - + printf("Enter board size: "); scanf_s("%d", &size); - + playGame(size, &playerScore, playerPrizes, &prizeCount, &playerName, &playerPosition); - + getchar(); - + } if (choice == 's') { @@ -497,7 +497,7 @@ int main(void) printf(" \\++++++|\n"); printf(" \\=====|\n"); printf(" 0--- 0\n"); - printf("HI SCORE: %d Player Name: %c \n",playerScore,playerName); + printf("HI SCORE: %d Player Name: %c \n", playerScore, playerName); } if (choice == 'q') {