Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HOUSE 25
#define HOTEL 50
#define PROPERTY 100
struct property
{
char name[30];
int user;
int num_houses;
int num_hotels;
struct property * next;
};
struct property * buy(struct property *list, char property_name[30], int user);
struct property * add_node(struct property *list, char property_name[30], int user);
struct property * improve(struct property *list, char property_name[30], int user);
struct property * sell(struct property *list, char property_name[30], int user);
struct property * del(struct property *list, char property_name[30]);
void print_properties(struct property *list);
//Buy function (search for name and add to linked list)
//Improve function (Add hotel or house, add to linked list)
//Sell function (Print amount property is worth + hotels and houses, remove property from linked list)
int main () {
int num_players, selection, user;
char property_name[30];
struct property *mylist = NULL;
printf("Welcome to Monopoly!\n");
printf("How many players will there be?\n");
scanf("%d", &num_players);
printf("Enjoy your game!\n");
user = 1;
do
{
if (selection == 5)
user ++;
if (user > num_players)
user = 1;
printf("Player %d what would you like to do?\n", user);
printf("\t1-Buy Property\n");
printf("\t2-Improve Property\n");
printf("\t3-Sell Property\n");
printf("\t4-View Properties\n");
printf("\t5-End Turn\n");
printf("\t6-End Game\n");
scanf("%d", &selection);
if (selection == 1) {
printf("Please enter the name of the property. (No spaces)\n");
scanf("%s", &property_name);
mylist = buy(mylist, property_name, user);
}
if (selection == 2) {
printf("Which property would you like to improve?\n");
scanf("%s", &property_name);
mylist = improve(mylist, property_name, user);
}
if (selection == 3) {
printf("Which property would you like to sell?\n");
scanf("%s", &property_name);
mylist = sell(mylist, property_name, user);
}
if (selection == 4)
print_properties(mylist);
printf("Player %d what would you like to do?\n", user);
printf("\t1-Buy Property\n");
printf("\t2-Improve Property\n");
printf("\t3-Sell Property\n");
printf("\t4-View Properties\n");
printf("\t5-End Turn\n");
printf("\t6-End Game\n");
scanf("%d", &selection);
//Selection Results
}
while (selection != 6);
//if (selection == 6)
//End game function
return 0;
}
struct property * buy(struct property *list, char property_name[30], int user){
struct property *helper = list;
int value;
if(list == NULL)
add_node(list, property_name, user);
while(helper != NULL){
value = strcmp(property_name, helper->name);
printf("%d\n", value);
if (value == 0){
printf("Player %d owns that property!", helper->user);
return;
}
helper = helper->next;
}
return(list);}
struct property * add_node(struct property * list, char property_name[30], int user){
struct property * temp = (struct property *)malloc(sizeof(struct property));
strcpy(temp->name, property_name);
printf("%s\n", temp->name);
temp->user = user;
printf("%d\n", temp->user);
temp->num_houses = 0;
printf("%d\n", temp->num_houses);
temp->num_hotels = 0;
printf("%d\n", temp->num_hotels);
temp->next = NULL;
if(list == NULL){
list = temp;
}
else{
temp->next = list;
list = temp;
}
return list;
}
struct property * improve(struct property *list, char property_name[30], int user){
printf("improve property");
struct property *helper = list;
int value, improve_choice, num_houses_buy, num_hotels_buy;
if(helper == NULL){
return list;}
while(helper != NULL){
value = strcmp(property_name, helper->name);
printf("value = %d", value);
if (value == 0){
if(helper->user == user){
printf("Do you want to buy:\n1:House\n2:Hotel\n");
scanf("5d", &improve_choice);
if(improve_choice == 1){
printf("How many houses would you like to buy?");
scanf("%d", &num_houses_buy);
helper->num_houses = helper->num_houses + num_houses_buy;
}
if(improve_choice == 2){
printf("How amny hotels would you like to buy?");
scanf("%d", &num_hotels_buy);
helper->num_hotels = helper->num_hotels + num_hotels_buy;
}
}
}
else
helper = helper->next;
}
return list;
}
struct property * sell(struct property *list, char property_name[30], int user){
printf("sell_property");
struct property *helper = list, *temp;
int value, property_sold;
while(helper != NULL){
value = strcmp(property_name, helper->name);
helper = helper->next;
if (value == 0){
if(helper->user == user)
property_sold = helper->num_houses*25 + helper->num_hotels*50 + 100;
del(list, property_name);
printf("You sold %s for %d dollars!", helper->name, property_sold);
return;
}
}
return;}
struct property * del(struct property *list, char property_name[30]){
struct property * helper = list, *deleted;
if (helper != NULL) {
if (strcmp(helper->name, property_name) == 0) {
list = helper->next;
free(helper);
return list;
}
while (helper-> next != NULL) {
if (strcmp(helper->next->name, property_name) == 0) {
deleted = helper->next;
helper->next = deleted->next;
free(deleted);
return list;
}
helper = helper->next;
}
}
return list;
}
void print_properties(struct property *list){
printf("print properties");
struct property *helper = list;
while(helper != NULL){
printf("%s is owned by player %d.", helper->name, helper->user);
helper = helper->next;
}
return;
}
Thank you in advance for help