From 4cca36441ae45db4603ba08883b7a83e4cbf1ddc Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Fri, 15 Jul 2011 17:46:57 +1000 Subject: [PATCH] Implement the show_status() and total_value() functions --- src/game.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++- src/game.h | 3 +- src/globals.h | 2 + src/intf.h | 2 + src/system.h | 1 + 5 files changed, 136 insertions(+), 3 deletions(-) diff --git a/src/game.c b/src/game.c index 6b01e80..f376971 100644 --- a/src/game.c +++ b/src/game.c @@ -880,6 +880,7 @@ void get_move (void) { // @@@ To be written show_map(true); + show_status(current_player); } void process_move (void) @@ -1016,9 +1017,135 @@ void show_map (bool closewin) } } -void show_status (int playernum) + +/*----------------------------------------------------------------------- + Function: show_status - Display the player's status + Arguments: num - Player number (0 to number_players - 1) + Returns: (nothing) + + This function displays the financial status of the player num. It uses + the player[num] global variable. The show status window is closed + before returning from this function. +*/ + +void show_status (int num) { - // @@@ To be written + double val; + int i, line; + + + assert(num >= 0 && num < number_players); + + newtxwin(MAX_COMPANIES + 15, 80, LINE_OFFSET + 1, COL_CENTER(80)); + wbkgd(curwin, ATTR_NORMAL_WINDOW); + box(curwin, 0, 0); + + center(curwin, 1, ATTR_WINDOW_TITLE, " Stock Portfolio "); + center2(curwin, 2, ATTR_NORMAL_WINDOW, ATTR_HIGHLIGHT_STR, "Player: ", + "%s", player[num].name); + + val = total_value(num); + if (val == 0.0) { + center(curwin, 11, ATTR_STANDOUT_STR, "* * * B A N K R U P T * * *"); + } else { + char *buf = malloc(GAME_BUFSIZE); + if (buf == NULL) { + err_exit("out of memory"); + } + + // Check to see if any companies are on the map + bool none = true; + for (i = 0; i < MAX_COMPANIES; i++) { + if (company[i].on_map) { + none = false; + break; + } + } + + if (none) { + center(curwin, 8, ATTR_NORMAL_WINDOW, "No companies on the map"); + } else { + // Handle the locale's currency symbol + struct lconv *lc = localeconv(); + assert(lc != NULL); + snprintf(buf, GAME_BUFSIZE, "share (%s)", lc->currency_symbol); + + wattrset(curwin, ATTR_WINDOW_SUBTITLE); + mvwprintw(curwin, 4, 2, " %-22s %12s %10s %10s %10s ", + "", "Price per", "", "Holdings", "Company"); + mvwprintw(curwin, 5, 2, " %-22s %12s %10s %10s %10s ", + "Company", buf, "Return (%)", "(shares)", "owner (%)"); + wattrset(curwin, ATTR_NORMAL_WINDOW); + + for (line = 6, i = 0; i < MAX_COMPANIES; i++) { + if (company[i].on_map) { + strfmon(buf, GAME_BUFSIZE, "%!12n", company[i].share_price); + mvwprintw(curwin, line, 2, + " %-22s %10s %10.2f %'10d %10.2f ", + company[i].name, buf, + company[i].share_return * 100.0, + player[num].stock_owned[i], + (company[i].stock_issued == 0) ? 0.0 : + ((double) player[num].stock_owned[i] * 100.0) / + company[i].stock_issued); + line++; + } + } + } + + line = 15; + strfmon(buf, GAME_BUFSIZE, "%18n", player[num].cash); + center2(curwin, line++, ATTR_NORMAL_WINDOW, ATTR_HIGHLIGHT_STR, + "Current cash: ", " %s ", buf); + if (player[num].debt != 0.0) { + strfmon(buf, GAME_BUFSIZE, "%18n", player[num].debt); + center2(curwin, line++, ATTR_NORMAL_WINDOW, ATTR_HIGHLIGHT_STR, + "Current debt: ", " %s ", buf); + center2(curwin, line++, ATTR_NORMAL_WINDOW, ATTR_HIGHLIGHT_STR, + "Interest rate: ", " %17.2f%% ", interest_rate * 100.0); + } + + strfmon(buf, GAME_BUFSIZE, "%18n", val); + center2(curwin, line + 1, ATTR_HIGHLIGHT_STR, ATTR_WINDOW_TITLE, + "Total value: ", " %s ", buf); + + free(buf); + } + + wait_for_key(curwin, 21, ATTR_WAITNORMAL_STR); + deltxwin(); + txrefresh(); +} + + +/*----------------------------------------------------------------------- + Function: total_value - Calculate a player's total worth + Arguments: num - Player number (0 to number_players - 1) + Returns: (nothing) + + This function calculates the total value (worth) of the player num. +*/ + +double total_value (int num) +{ + double val; + int i; + + + assert(num >= 0 && num < number_players); + + val = player[num].cash - player[num].debt; + for (i = 0; i < MAX_COMPANIES; i++) { + if (company[i].on_map) { + val += player[num].stock_owned[i] * company[i].share_price; + } + } + + if (val < ROUNDING_AMOUNT) { + val = 0.0; + } + + return val; } diff --git a/src/game.h b/src/game.h index f4bfde4..6ec9ac8 100644 --- a/src/game.h +++ b/src/game.h @@ -52,7 +52,8 @@ extern void exchange_stock (void); extern void next_player (void); extern void show_map (bool show_moves); -extern void show_status (int playernum); +extern void show_status (int num); +extern double total_value (int num); #endif /* included_GAME_H */ diff --git a/src/globals.h b/src/globals.h index edca2ff..61e3fa5 100644 --- a/src/globals.h +++ b/src/globals.h @@ -73,6 +73,8 @@ #define INC_INTEREST_RATE (0.30) /* 30% chance of incr./decr. interest rate */ #define MAX_INTEREST_RATE (0.30) /* Maximum interest rate */ +#define ROUNDING_AMOUNT (0.01) /* Round off smaller amounts to zero */ + // Information about each company typedef struct company_info { diff --git a/src/intf.h b/src/intf.h index 91c9cb5..dcd6e33 100644 --- a/src/intf.h +++ b/src/intf.h @@ -126,6 +126,7 @@ enum color_pairs { #define ATTR_STATUS_WINDOW ATTR(COLOR_PAIR(BLACK_ON_WHITE), A_REVERSE) #define ATTR_ERROR_WINDOW ATTR(COLOR_PAIR(WHITE_ON_RED), A_REVERSE) #define ATTR_WINDOW_TITLE ATTR(COLOR_PAIR(YELLOW_ON_BLACK) | A_BOLD, A_REVERSE) +#define ATTR_WINDOW_SUBTITLE ATTR(COLOR_PAIR(WHITE_ON_BLACK), A_REVERSE) #define ATTR_MAP_TITLE ATTR(COLOR_PAIR(WHITE_ON_BLUE), A_NORMAL) #define ATTR_MAP_T_HIGHLIGHT ATTR(COLOR_PAIR(YELLOW_ON_BLUE) | A_BOLD, A_BOLD) #define ATTR_MAP_T_STANDOUT ATTR(COLOR_PAIR(YELLOW_ON_BLUE) | A_BOLD | A_BLINK, A_BOLD | A_BLINK) @@ -133,6 +134,7 @@ enum color_pairs { #define ATTR_INPUT_FIELD ATTR(COLOR_PAIR(WHITE_ON_BLACK), A_BOLD | '_') #define ATTR_KEYCODE_STR ATTR(COLOR_PAIR(YELLOW_ON_BLACK) | A_BOLD, A_REVERSE) #define ATTR_HIGHLIGHT_STR ATTR(COLOR_PAIR(YELLOW_ON_BLUE) | A_BOLD, A_BOLD) +#define ATTR_STANDOUT_STR ATTR(COLOR_PAIR(YELLOW_ON_BLUE) | A_BOLD | A_BLINK, A_BOLD | A_BLINK) #define ATTR_ERROR_STR ATTR(COLOR_PAIR(WHITE_ON_RED) | A_BOLD, A_REVERSE) #define ATTR_WAITNORMAL_STR ATTR(COLOR_PAIR(CYAN_ON_BLUE), A_NORMAL) #define ATTR_WAITERROR_STR ATTR(COLOR_PAIR(WHITE_ON_RED), A_REVERSE) diff --git a/src/system.h b/src/system.h index d210b44..55a0079 100644 --- a/src/system.h +++ b/src/system.h @@ -61,6 +61,7 @@ #include #include #include +#include #include