home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume7 / hotel / part01 / human.c < prev    next >
C/C++ Source or Header  |  1989-07-13  |  10KB  |  433 lines

  1. /*
  2.  * This program (called "Hotel") is copyright 1989 to Scott R. Turner,
  3.  * in both source code and executable form.  Permission is given to 
  4.  * copy both the source code and the executable under the following
  5.  * conditions:
  6.  * 
  7.  * COPYING POLICIES
  8.  * 
  9.  *   1. You may copy and distribute verbatim copies of Hotel code as you
  10.  * receive it, in any medium, provided that you conspicuously and
  11.  * appropriately publish on each file a valid copyright notice such as
  12.  * "Copyright (C) 1989 Scott R. Turner", and keep intact the copyright
  13.  * and license notices on all files.  You may charge a distribution fee for the
  14.  * physical act of transferring a copy, but that fee may not exceed
  15.  * your actual costs in creating and delivering the copy.
  16.  * 
  17.  *   2. You may modify your copy or copies of Hotel or any portion of it,
  18.  * and copy and distribute such modifications under the terms of
  19.  * Paragraph 1 above, provided that you also do the following:
  20.  * 
  21.  *     a) cause the modified files to carry prominent notices stating
  22.  *     who last changed such files and the date of any change; and
  23.  * 
  24.  *     b) cause the whole of any work that you distribute or publish,
  25.  *     that in whole or in part contains or is a derivative of Hotel
  26.  *     or any part thereof, to be licensed at no charge to all third
  27.  *     parties on terms identical to those contained in this License
  28.  *     Agreement (except that you may choose to grant more extensive
  29.  *     warranty protection to third parties, at your option).
  30.  *
  31.  *   3. You may not copy, sublicense, distribute or transfer Hotel
  32.  * except as expressly provided under this License Agreement.  Any attempt
  33.  * otherwise to copy, sublicense, distribute or transfer Hotel is void and
  34.  * your rights to use Hotel under this License agreement shall be
  35.  * automatically terminated.  However, parties who have received computer
  36.  * software programs from you with this License Agreement will not have
  37.  * their licenses terminated so long as such parties remain in full compliance.
  38.  * 
  39.  *   4.  Under no circumstances may you charge for copies of Hotel, for copies
  40.  * of any program containing code from Hotel in whole or in part, or for 
  41.  * any software package or collection of programs or code that contains Hotel
  42.  * in whole or part.
  43.  *
  44.  */ 
  45. /*
  46.  *  human.c
  47.  *  Scott R. Turner
  48.  *  9/7/88
  49.  *
  50.  *  This file contains the "play", "buy" and "liquidate" strategies
  51.  *  for human players.
  52.  *
  53.  */
  54. #include "defs.h"
  55. #include "my_wgets.h"
  56. extern int legal_play();
  57. extern void print_board();
  58. extern void update_cash();
  59. extern void getnum_help();
  60. extern void buy_help();
  61. extern void newhotel_help();
  62. extern void save_help();
  63. extern void quit();
  64.  
  65. /*
  66.  *  human_play queries the player to find out which of his tiles
  67.  *  he would like to play.
  68.  *
  69.  */
  70.  
  71. void  human_play(n,px,py)
  72.      int n;
  73.      int *px,*py;
  74. {
  75.   int tile,i,j,legl;
  76.  
  77.   print_board(n);
  78.   move((boardsize+5),0);
  79.   clrtobot();
  80.   printw("Cash: $%d\n",players[n].cash);
  81.   printw("Holdings: ");
  82.   for(i=1;i<=numhotels;i++)
  83.     if(players[n].shares[i] > 0)
  84.       printw("(%c) %d  ",(i + 'A' - 1),players[n].shares[i]);
  85.   printw("\n");
  86.  
  87.   /*  The player may not have a legal play.  */
  88.  
  89.   legl = 0;
  90.     for(i=1;i<=boardsize;i++)
  91.       for(j=1;j<=boardsize;j++)
  92.     if(board[i][j] == -n && legal_play(i,j)) {
  93.       legl=1;
  94.       goto done;
  95.     }
  96.  done:
  97.  
  98.   if (!legl) {
  99.     printw("Sorry, you have no legal moves.\n");
  100.     refresh();
  101.     *px = -1;
  102.     *py = -1;
  103.     return;
  104.   };
  105.  
  106.   /* Else get his play. */
  107.  
  108.   do {
  109.     legl = 0;
  110.     printw("Which tile to play? ");
  111.     refresh();
  112.     tile = getnum(1,numtiles);
  113.  
  114.     /* Is this legitimate? */
  115.  
  116.     if(tile > numtiles || tile < 1) {
  117.       printw("That is not a valid tile number.\n");
  118.     } else {
  119.  
  120.       /* Figure out where it is */
  121.  
  122.       for(i=1;i<=boardsize;i++)
  123.     for(j=1;j<=boardsize;j++)
  124.       if(board[i][j] == -n) {
  125.         tile--;
  126.         if (tile == 0) {
  127.           *px = i;
  128.           *py = j;
  129.           goto finished;
  130.         };
  131.       };
  132.       
  133.     finished:
  134.       
  135.       legl = legal_play(*px,*py);
  136.       if (!legl) {
  137.         move((boardsize+8),0);
  138.     clrtobot();
  139.     printw("You cannot create another hotel.\n");
  140.     refresh();
  141.       };
  142.     };
  143.   } while (!legl);
  144.   
  145. };
  146.  
  147. /*
  148.  *  human_buy queries the player and asks what shares he would
  149.  *  like to purchase, and returns the answer in purchases.
  150.  *
  151.  */
  152. beep2()
  153. {
  154.   printf("%c",7);
  155. };
  156.  
  157. void  human_buy(p,purchases)
  158.      int p, purchases[MAXHOTELS];
  159. {
  160.   extern int share_cost();
  161.   int i,j,tot,cashtot,goahead;
  162.   char ans;
  163.  
  164.   for(i=1;i<=numhotels;i++) purchases[i] = 0;
  165.  
  166.   goahead = 0;
  167.   for(j=1;j<=numhotels;j++)
  168.     if(hotels[j].shares>0 &&
  169.        hotels[j].size>0 && players[p].cash >= share_cost(j))
  170.       goahead = 1;
  171.  
  172.   if (!goahead) return;
  173.  
  174.   /*  Query the user about each available hotel. */
  175.  
  176.   print_board(p);
  177.   move((boardsize+5),0);
  178.   clrtobot();
  179.   printw("What to buy?\n");
  180.   printw("(Hit letter once for each stock, Space to finish.) ");
  181.   refresh();
  182.  
  183.   tot = 0;
  184.   cashtot = players[p].cash;
  185.  
  186.   do {
  187.     ans = getch();
  188.     if (islower(ans)) ans = toupper(ans);
  189.     if (ans == REFRESHKEY) {
  190.       wrefresh(curscr);
  191.       continue;
  192.     }
  193.     if (ans == QUITKEY) {
  194.       quit();
  195.     };
  196.     if (ans == HELPKEY) {
  197.         buy_help();
  198.     continue;
  199.     };
  200.     if (ans == 32) break;
  201.     clrtobot();
  202.     i = (ans - 'A' + 1);
  203.     if (i < 1 || i > numhotels || hotels[i].size == 0 ||
  204.     hotels[i].shares == 0) {
  205.       /* An invalid choice, so beep. */
  206.       beep2();
  207.       continue;
  208.     };
  209.     /* Can he afford this? */
  210.     if (share_cost(i) > cashtot) {
  211.       beep2();
  212.       continue;
  213.     };
  214.     purchases[i]++;
  215.     tot++;
  216.     cashtot -= share_cost(i);
  217.   } while (tot != maxbuy);
  218.   move((boardsize+10),0);
  219.   clrtobot();
  220.   refresh();
  221. };
  222.  
  223. /*
  224.  *  human_liquidate is called when a hotel chain is sunk and a player might
  225.  *  have stock to get rid of.
  226.  *
  227.  */
  228.  
  229. void  human_liquidate(p,maxhot,sunk,sell,trade)
  230.      int p, *sell, *trade, maxhot,sunk;
  231. {
  232.   int i;
  233.  
  234.   print_board(p);
  235.   refresh();
  236.   if(players[p].shares[sunk]) {
  237.       *sell = MAXSHARES + 1;
  238.       while(*sell+*trade > players[p].shares[sunk]) {
  239.     move((boardsize+5),0);
  240.     clrtobot();
  241.     *sell = 0;
  242.     *trade = 0;
  243. again:      
  244.     printw("You have %d shares of (%d) %s.\n",
  245.            players[p].shares[sunk],sunk,hotels[sunk].name);
  246.     if (hotels[maxhot].shares != 0) {
  247.       printw("There are %d shares of %s available.\n",hotels[maxhot].shares,
  248.          hotels[maxhot].name);
  249.       printw("How many shares would you like to trade in (2 for 1)? ");
  250.       refresh();
  251.       echo();
  252. #ifdef UNIX
  253.       nl(); 
  254. #endif UNIX
  255.         /*      scanw("%d",trade);  */
  256.         /*  Trying getint. */
  257.         *trade = getint();
  258. #ifdef UNIX
  259.       nonl(); 
  260. #endif UNIX
  261.       noecho();
  262.       printw("\n");
  263.  
  264.       if (*trade % 2) {
  265.         move((boardsize+5),0);
  266.         clrtobot();
  267.         printw("Trading an odd number of shares is wasteful.\n");
  268.         refresh();
  269.         (void) any_key();
  270.         goto again;
  271.       };
  272.       if ((*trade / 2) > hotels[maxhot].shares) {
  273.         move((boardsize+5),0);
  274.         clrtobot();
  275.         printw("No point in trading for more shares than available.");
  276.         refresh();
  277.         (void) any_key();
  278.         goto again;
  279.       };
  280.     };
  281.     if (players[p].shares[sunk] > *trade) {
  282.       printw("How many shares would you like to sell at %d? ",share_cost(sunk));
  283.       refresh();
  284.       echo();
  285. #ifdef UNIX
  286.       nl(); 
  287. #endif UNIX
  288.       /*  scanw("%d",sell); */
  289.       *sell = getint();
  290. #ifdef UNIX
  291.       nonl(); 
  292. #endif UNIX
  293.       noecho();
  294.       printw("\n");
  295.         };
  296.     if (*sell+*trade > players[p].shares[sunk]) {
  297.       printw("That's too many.  You only have %d shares.\n",
  298.          players[p].shares[sunk]);
  299.           refresh();
  300.           (void) any_key();
  301.     };
  302.      };
  303.    };
  304. };
  305.  
  306. int  human_new(n,x,y)
  307.      int n,x,y;
  308. {
  309.   int i;
  310.   char ans;
  311.  
  312.   print_board(n);
  313.   do {
  314. topofselect:
  315.     move((boardsize + 5), 0);
  316.     clrtobot();
  317.     printw("You have created a new hotel.\n");
  318.     printw("Available hotels:\n");
  319.     for(i=1;i<=numhotels;i++)
  320.       if (hotels[i].size == 0)
  321.         printw("%c ",hotels[i].name[0]);
  322.     printw("\nSelect a hotel: ");
  323.     refresh();
  324. nextinput:
  325.     ans = getch();
  326.     if (islower(ans)) ans = toupper(ans);
  327.     if (ans == REFRESHKEY) {
  328.       wrefresh(curscr);
  329.       goto nextinput;
  330.     }
  331.     if (ans == QUITKEY) {
  332.       quit();
  333.     };
  334.     if (ans == HELPKEY) {
  335.         newhotel_help();
  336.         goto topofselect;
  337.     };
  338.     clrtobot();
  339.     i = (ans - 'A' + 1);
  340.     if (i < 1 || i > numhotels || hotels[i].size != 0) {
  341. #ifdef UNIX
  342.        printw("\n");
  343. #endif UNIX       
  344.        printw("That is not a legal selection.\n");
  345.        refresh();
  346.        goto topofselect;
  347.     };
  348.   } while (0);
  349.   move((boardsize+10),0);
  350.   clrtobot();
  351.   refresh();
  352.   return(i);
  353. };
  354.  
  355. /*
  356.  *  human_save is a routine for asking the player which of N
  357.  *  equal size hotels they'd like to save.
  358.  *
  359.  */
  360.  
  361. int  human_save(p,max,adj)
  362.      int p,adj[MAXHOTELS+1];
  363. {
  364.   int j,i;
  365.   char ans;
  366.  
  367.   print_board(p);
  368.   printw("Save which hotel: ");
  369.   for(j=1;j<=MAXHOTELS;j++)
  370.     if (adj[j] == max) printw(" %c",hotels[j].name[0]);
  371.   printw("?");
  372.   refresh();
  373.   i = 0;
  374.   do {
  375.     ans = getch();
  376.     if (islower(ans)) ans = toupper(ans);
  377.     if (ans == REFRESHKEY) {
  378.       wrefresh(curscr);
  379.       continue;
  380.     }
  381.     if (ans == QUITKEY) {
  382.       quit();
  383.     };
  384.     if (ans == HELPKEY) {
  385.         save_help();
  386.     continue;
  387.     };
  388.     clrtobot();
  389.     i = (ans - 'A' + 1);
  390.   } while (i < 1 || i > numhotels || adj[i] != max);
  391.   printw("\n");
  392.   move((boardsize+10),0);
  393.   clrtobot();
  394.   refresh();
  395.   return(i);
  396.  
  397. };
  398.  
  399. /*
  400.  *  getnum does the input processing to get a number in the
  401.  *  range [low,high] inclusive.
  402.  *
  403.  *  Should put hooks in here for clear and help, etc.
  404.  */
  405.  
  406. getnum(low,high)
  407.      int low,high;
  408. {
  409.   char ch;
  410.   int ans,y,x;
  411.  
  412.   ans = low - 1;
  413.   do {
  414.     ch = getch();
  415.     if (islower(ch)) ch = toupper(ch);
  416.     if (ch == REFRESHKEY) {
  417.       wrefresh(curscr);
  418.       continue;
  419.     };
  420.     if (ch == HELPKEY) {
  421.         getnum_help();
  422.     continue;
  423.     };
  424.     if (ch == QUITKEY) {
  425.       quit();
  426.     };
  427.     clrtobot();
  428.     ans = ch - '0';
  429.   } while (ans < low || ans > high);
  430.   printw("%d\n",ans);
  431.   return(ans);
  432. };
  433.