So I tried doing my monopoly code and I used a global variable because I'm not that good at using pointers. After analyzing my code and tried to see the next step. I saw that Global variables and structs are not allowed. So I'm trying to convert my code from using a global variable to using pointers since I think it is the only way. As you can see in my code, there is P1_CUR_AMT, P2_CUR_AMT, P1_CUR_POS, P2_CUR_POS and currentPlayer being used as a global variable. Here's my code.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <string.h>
int P1_CUR_AMT = 200;
int P2_CUR_AMT = 200;
int P1_CUR_POS = 0;
int P2_CUR_POS = 0;
int currentPlayer = 1;
int RollDice()
{
return (rand() % 6) + 1;
}
int checkLand(int Money)
{
int choice = 0;
if(currentPlayer == 1)
{
P1_CUR_POS += RollDice();
if(P1_CUR_POS == 10){
P1_CUR_POS = 0;
}
else if(P1_CUR_POS > 10){
P1_CUR_POS = P1_CUR_POS - 10;
}
choice = P1_CUR_POS;
}
if(currentPlayer == 2)
{
P2_CUR_POS += RollDice();
if(P2_CUR_POS == 10){
P2_CUR_POS = 0;
}
else if(P2_CUR_POS > 10)
{
P2_CUR_POS = P2_CUR_POS - 10;
}
choice = P2_CUR_POS;
}
char playerchoice;
int dice = RollDice();
if (choice == 0)
{
printf(" on Go.\n\n");
Money += 200;
printf("You collect $200 from the Bank.");
printf("\nYour New Balance is $%d", Money);
}
else if (choice == 1)
{
printf(" on Tree House.\n\n");
printf("\nWould you like to buy Tree House?");
printf("\n\t [ y or Y ]\t[n or N]\n");
scanf("%c", &playerchoice);
scanf("%c", &playerchoice);
if (playerchoice == 'y' || playerchoice == 'Y')
{
Money -= 20;
printf("\nYou succesfully bought Tree House");
printf("\nYour New Balance is $%d", Money);
}
else if (playerchoice == 'n' || playerchoice == 'N')
printf("Thank You for Staying.");
}
else if (choice == 2)
{
printf(" on Electric Company.\n\n");
printf("\nWould you like to buy Electric Company?");
printf("\n\t [ y or Y ]\t[n or N]\n");
scanf("%c", &playerchoice);
scanf("%c", &playerchoice);
if (playerchoice == 'y' || playerchoice == 'Y')
{
Money -= 150;
printf("\nYou succesfully bought Electric Company\n");
printf("\nYour New Balance is $%d", Money);
}
else if (playerchoice == 'n' || playerchoice == 'N')
printf("Thank You for Staying.");
}
else if (choice == 3)
{
printf(" on Beach House.\n\n");
printf("\nWould you like to buy Beach House?");
printf("\n\t [ y or Y ]\t[n or N]\n");
scanf("%c", &playerchoice);
scanf("%c", &playerchoice);
if (playerchoice == 'y' || playerchoice == 'Y')
{
Money -= 60;
printf("\nYou succesfully bought Beach House");
printf("\nYour New Balance is $%d", Money);
}
else if (playerchoice == 'n' || playerchoice == 'N')
printf("Thank You for Staying.");
}
else if (choice == 4)
{
printf(" on Jail Time.\n\n");
printf("You lose a turn.");
}
else if (choice == 5)
{
printf(" on Castle.\n\n");
printf("\nWould you like to buy Castle? ");
printf("\n\t [ y or Y ]\t[n or N]\n");
scanf("%c", &playerchoice);
scanf("%c", &playerchoice);
if (playerchoice == 'y' || playerchoice == 'Y')
{
Money -= 100;
printf("\nYou succesfully bought Castle");
printf("\nYour New Balance is $%d", Money);
}
else if (playerchoice == 'n' || playerchoice == 'N')
printf("Thank You for Staying.");
}
else if (choice == 6)
{
printf(" on Feeling Lucky.\n\n");
printf("Congratulations! You will roll the dice to determine your luck.\n");
printf("You rolled %d", dice);
// if (RollDice())
}
else if (choice == 7)
{
printf(" on Rail Road.\n\n");
printf("Would you like to buy Rail Road?");
printf("\n\t [ y or Y ]\t[n or N]\n");
scanf("%c", &playerchoice);
scanf("%c", &playerchoice);
if (playerchoice == 'y' || playerchoice == 'Y')
{
Money -= 100;
printf("\nYou succesfully bought Rail Road");
printf("\nYour New Balance is $%d.", Money);
}
else if (playerchoice == 'n' || playerchoice == 'N')
printf("Thank You for Staying.");
}
else if (choice == 8)
{
printf(" on Igloo.\n\n");
printf("Would you like to buy Igloo?");
printf("\n\t [ y or Y ]\t[n or N]\n");
scanf("%c", &playerchoice);
scanf("%c", &playerchoice);
if (playerchoice == 'y' || playerchoice == 'Y')
{
Money -= 160;
printf("\nYou succesfully bought Igloo");
printf("\nYour New Balance is $%d.", Money);
}
else if (playerchoice == 'n' || playerchoice == 'N')
printf("Thank You for Staying.");
}
else if (choice == 9)
{
printf(" on Farm House.\n\n");
printf("Would you like to buy Farm House?");
printf("\n\t [ y or Y ]\t[n or N]\n");
scanf("%c", &playerchoice);
scanf("%c", &playerchoice);
if (playerchoice == 'y' || playerchoice == 'Y')
{
Money -= 180;
printf("\nYou succesfully bought Farm House");
printf("\nYour New Balance is $%d.", Money);
}
else if (playerchoice == 'n' || playerchoice == 'N')
printf("Thank You for Staying.");
}
return Money;
}
/*********************************************
FUNCTION TO RETURN GAME STATUS
1 FOR GAME IS OVER WITH PLAYER 2 WIN
2 FOR GAME IS OVER WITH PLAYER 1 WIN
3 FOR GAME IS IN PROGRESS
**********************************************/
int checkWin()
{
if(P1_CUR_AMT <= 0)
{
return 1;
}
else if(P2_CUR_AMT <= 0)
{
return 2;
}
else
{
return 3;
}
}
char PlayGame(char YesOrNo)
{
printf("Would you like to play? Enter Y or N: ", YesOrNo);
scanf("%c", &YesOrNo);
if (YesOrNo == 'Y' || YesOrNo == 'y')
{
Lines();
printf("\n%40s", "Welcome To A Monopoly Game Called\n");
printf("%30s", "Property Property\n\n");
Lines();
cRules();
system ("cls");
displayBoard();
int player = 1, i;
do
{
Lines();
if(currentPlayer > 2) currentPlayer = 1;
printf("It is Player %d turn.\n", currentPlayer);
printf("Press any key to roll a dice.");
getch();
printf("\nPlayer %d landed", currentPlayer);
if(currentPlayer == 1)
{
P1_CUR_AMT = checkLand(P1_CUR_AMT);
}
else
{
P2_CUR_AMT = checkLand(P2_CUR_AMT);
}
i = checkWin();
currentPlayer++;
}
while (i != 1 || i != 2);
}
}
int main()
{
srand (time(0)); // Initialize random seed
char choice;
Introduction();
getch();
system ("cls");
PlayGame(choice);
getch();
system ("cls");
return 0;
}