1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-12-04 14:46:45 -05:00

Implement the show_status() and total_value() functions

This commit is contained in:
John Zaitseff 2011-07-15 17:46:57 +10:00
parent 717fc28339
commit 4cca36441a
5 changed files with 136 additions and 3 deletions

View File

@ -880,6 +880,7 @@ void get_move (void)
{ {
// @@@ To be written // @@@ To be written
show_map(true); show_map(true);
show_status(current_player);
} }
void process_move (void) 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;
} }

View File

@ -52,7 +52,8 @@ extern void exchange_stock (void);
extern void next_player (void); extern void next_player (void);
extern void show_map (bool show_moves); 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 */ #endif /* included_GAME_H */

View File

@ -73,6 +73,8 @@
#define INC_INTEREST_RATE (0.30) /* 30% chance of incr./decr. interest rate */ #define INC_INTEREST_RATE (0.30) /* 30% chance of incr./decr. interest rate */
#define MAX_INTEREST_RATE (0.30) /* Maximum 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 // Information about each company
typedef struct company_info { typedef struct company_info {

View File

@ -126,6 +126,7 @@ enum color_pairs {
#define ATTR_STATUS_WINDOW ATTR(COLOR_PAIR(BLACK_ON_WHITE), A_REVERSE) #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_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_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_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_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) #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_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_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_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_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_WAITNORMAL_STR ATTR(COLOR_PAIR(CYAN_ON_BLUE), A_NORMAL)
#define ATTR_WAITERROR_STR ATTR(COLOR_PAIR(WHITE_ON_RED), A_REVERSE) #define ATTR_WAITERROR_STR ATTR(COLOR_PAIR(WHITE_ON_RED), A_REVERSE)

View File

@ -61,6 +61,7 @@
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <monetary.h>
#include <getopt.h> #include <getopt.h>