Code listing

 

#include <Mega163.h>

#include <delay.h>

#include <string.h>

#include <stdlib.h>

// Keypad constants

#define CHECKKEY   30

#define MAXKEYS     12

 

// LCD Controls (PORTB)

// Pin 0 - reset

// Pin 1 - RD

// Pin 2 - WR

// Pin 3 - nc

// Pin 4 - nc

// Pin 5 - nc

// Pin 6 - A0

// Pin 7 - nc

// http://www.seiko-usa-ecd.com/lcd/products/graphic_mods/pdf/sed1335f.pdf

#define LCDreset        0x00    // 00000000 Reset the Display

#define LCDnop          0x47    // 01000111 No operation

#define CmdSetup        0x47    // 01000111 Set A0 high

#define CmdWrite        0x43    // 01000011 Set WR low

#define DataSetup       0x07    // 00000111 Set A0 low

#define DataWrite       0x03    // 00000011 Set WR low

#define StatusRead      0x05    // 00000101 Set RD low, A0 low

#define DataRead        0x45    // 01000101 Set RD low, A0 high

 

 

// LCD Commands (PORTC)

#define SystemSet        0x40    // Initialize system

#define SleepIn             0x53    // Enter standby mode

#define DispOFF          0x58    // Display Off

#define DispON           0x59    // Display On

#define Scroll               0x44    // Initialize Address & Regions

#define CSRForm        0x5D    // Set cursor type

#define CharAddr         0x5C    // Set address of character RAM

#define CSRRight         0x4C    // Cursor direction = right

#define CSRLeft           0x4D    // Cursor direction = left

#define CSRUp            0x4E    // Cursor direction = up

#define CSRDown       0x4F    // Cursor direction = down

#define HorzScroll        0x5A    // Set horz scroll position

#define Overlay            0x5B    // Set Display Format

#define CSRW             0x46    // Set cursor address

#define CSRR              0x47    // Read cursor address

#define MWRITE         0x42    // Write to display memory

#define MREAD          0x43    // Read from display memory

 

// State Constants

#define INIT                            0

#define NAME                         1

#define ROLL                          2

#define OWNED                     3

#define UNOWNED                4

#define MORTGAGED            5

#define SELL                           6

#define JA                                7

#define BURSAR                     8

#define TUITION                    9

#define BUYH1                       10

#define BUYH2                       11

#define BUYH3                       12

#define SELLH1                      13

#define SELLH2                      14

#define SELLH3                      15

#define SELLP1                       16

#define SELLP2                       17

#define SELLP3                       18

#define UNMORT1                 19

#define UNMORT2                 20

#define UNMORT3                 21

 

void initialize(void);

 

// Keypad input

void checkkey(void);

 

// LCD drivers

void WriteData(unsigned char CommandCode);

void lcdinit(void);

void clearLCD(void);

void WriteCommand(unsigned char CommandCode);

void WriteString(char *input);

void DrawXY(int x, int y);

void MoveCursor(int addr);

void DrawGB(void);

void DrawMenu(void);

void clearSB(void);

void DrawCard(char i);

void DrawPattern(char i);

void clearMB(void);

void WriteStringSB(char line, char *input);

void DrawCar(int x, int y);

void DrawTower(int x, int y);

void DrawQuestion(int bx, int by);

void DrawWave(int bx, int by);

void DrawPattern1(int bx, int by);

void DrawPattern2(int bx, int by);

void DrawPattern3(int bx, int by);

void DrawPattern4(int bx, int by);

void DrawPattern5(int bx, int by);

void DrawPattern6(int bx, int by);

void DrawPattern7(int bx, int by);

void DrawPattern8(int bx, int by);

void DrawPattern3a(int bx, int by);

void DrawPattern4a(int bx, int by);

 

// Property routines

void GetName(char i);

 

// Game routine

void GameInit(void);

void StateMachine(void);

void nextplayer(void);

void UpdateTop(void);

char RollDice(void);

int GetRent(char i);

 

typedef struct {

            char name[10];

            int cash;

            int asset;

            char location;

            char roll;

            int rentpaid;

            int rentdue;

} Player;

 

unsigned char reload;                // timer0 reload to set 1 mSec

unsigned int checkkey_t;

unsigned char keyblock, butnum, key;

unsigned char strbuffer[18];

unsigned char pixelbuffer;

unsigned char numplayer;

unsigned char currentplayer;

unsigned char State[4];

unsigned char Rolled[2];

unsigned char laststate;

unsigned char houseindex;

unsigned char chance, campusevent;

 

Player player[4];

 

flash unsigned char keytbl[MAXKEYS]={0xd7, 0xee, 0xde, 0xbe, 0xed, 0xdd, 0xbd, 0xeb, 0xdb, 0xbb, 0xe7, 0xb7};

flash unsigned char rent[40]={0, 2, 0, 4, 200, 0, 6, 0, 6, 8, 0, 10, 0, 10, 12, 0, 14, 0, 14, 16, 0, 18, 0, 18, 20, 0, 22, 22, 0, 24, 0, 26, 26, 0, 28, 0, 0, 35, 75, 50};

flash unsigned char house1[40]={0, 10, 0, 20, 0, 0, 30, 0, 30, 40, 0, 50, 0, 50, 60, 0, 70, 0, 70, 80, 0, 90, 0, 90, 100, 0, 110, 110, 0, 120, 0, 130, 130, 0, 150, 0, 0, 175, 0, 200};

flash unsigned char house2[40]={0, 30, 0, 60, 0, 0, 90, 0, 90, 100, 0, 150, 0, 150, 180, 0, 200, 0, 200, 220, 0, 250, 0, 250, 300, 0, 330, 330, 0, 360, 0, 390, 390, 0, 450, 0, 0, 500, 0, 600};

flash unsigned int house3[40]={0, 90, 0, 180, 0, 0, 270, 0, 270, 300, 0, 450, 0, 450, 500, 0, 550, 0, 550, 600, 0, 700, 0, 700, 750, 0, 800, 800, 0, 850, 0, 900, 900, 0, 1000, 0, 0, 1100, 0, 1400};

flash unsigned int house4[40]={0, 160, 0, 320, 0, 0, 400, 0, 400, 450, 0, 625, 0, 625, 700, 0, 750, 0, 750, 800, 0, 875, 0, 875, 925, 0, 975, 975, 0, 1025, 0, 1100, 1100, 0, 1200, 0, 0, 1300, 0, 1700};

flash unsigned int hotel[40]={0, 250, 0, 450, 0, 0, 550, 0, 550, 600, 0, 750, 0, 750, 900, 0, 950, 0, 950, 1000, 0, 1050, 0, 1050, 1100, 0, 1150, 1150, 0, 1200, 0, 1275, 1275, 0, 1400, 0, 0, 1500, 0, 2000};

flash unsigned char mortgage[40]={0, 30, 0, 30, 0, 100, 50, 0, 50, 60, 0, 70, 75, 70, 80, 100, 90, 0, 90, 100, 0, 110, 0, 110, 120, 100, 130, 130, 75, 140, 0, 150, 150, 0, 160, 100, 0, 175, 0, 200};

flash unsigned char housecost[40]={0, 50, 0, 50, 0, 0, 50, 0, 50, 50, 0, 100, 0, 100, 100, 0, 100, 0, 100, 100, 0, 150, 0, 150, 150, 0, 150, 150, 0, 150, 0, 200, 200, 0, 200, 0, 0, 200, 0, 200};

flash unsigned char buybuild[40]={0, 2, 0, 2, 0, 1, 2, 0, 2, 2, 0, 2, 1, 2, 2, 1, 2, 0, 2, 2, 0, 2, 0, 2, 2, 1, 2, 2, 1, 2, 0, 2, 2, 0, 2, 1, 0, 2, 0, 2};

flash unsigned char pattern[40]={0, 1, 0, 1, 0, 0, 2, 0, 2, 2, 0, 3, 0, 3, 3, 0, 4, 0, 4, 4, 0, 5, 0, 5, 5, 0, 6, 6, 0, 6, 0, 7, 7, 0, 7, 0, 0, 8, 0, 8};

 

unsigned char building[40];

unsigned char owner[40];

//flash unsigned char cost[40]={0, 60, 0, 60, 0, 200, 100, 0, 100, 120, 0, 140, 150, 140, 160, 200, 180, 0, 180, 200, 0, 220, 0, 220, 240, 200, 260, 260, 150, 280, 0, 300, 300, 0, 320, 200, 0, 350, 0, 400};

// cost is exactly 2 times mortgage price

 

 

//*************************************************************

interrupt [TIM0_OVF] void timer0_overflow(void)

{

            TCNT0 = reload;

 

            // Decrement the times if they are not already zero

            if (checkkey_t > 0) --checkkey_t;

}

 

//*************************************************************

void main(void)

{

            initialize();

    GameInit();

 

            while (1)

            {

                        if (checkkey_t == 0) checkkey();

            }

}

 

//*************************************************************

// Keypad routines

//*************************************************************

void checkkey(void)

{

            checkkey_t = CHECKKEY;

 

            DDRD = 0x0f; // set upper nibble input

            PORTD = 0xf0;

            delay_us(5);

            key = PIND;

 

            DDRD = 0xf0;    // set lower nibble input

            PORTD = 0x0f;

            delay_us(5);

            key = key | PIND;

 

            if (keyblock == 0)

            {

                        if (key != 0xff)

                        {

                                    for (butnum=0; butnum<MAXKEYS; butnum++)

                                    {

                                                if (keytbl[butnum] == key)        break;

                                    }

                                    // convert from butnum to actual character

                                    if (butnum != MAXKEYS)

                                    {

                                                WriteCommand(MWRITE);

                                                WriteData(butnum+48);

                                                StateMachine();

                                                DrawMenu();

                                    }

                                    keyblock = 1;

                        }

            }

            else

            {

                        if (key == 0xff)

                        {

                                    keyblock = 0;

                        }

            }

}

 

//*************************************************************

// LCD routines, from previous project Graphic Calculator

//*************************************************************

// WriteCommand() :  Sends Command to LCD controller

 

void WriteCommand(unsigned char CommandCode) {

  PORTC = CommandCode;

  PORTB = CmdSetup;

  PORTB = CmdWrite;

  PORTB = CmdSetup;

} // end WriteCommand

 

//*************************************************************

// WriteData() :  Sends parameters or data to LCD controller

 

void WriteData(unsigned char CommandCode) {

  PORTC = CommandCode;

  PORTB = DataSetup;

  PORTB = DataWrite;

  PORTB = DataSetup;

} // end WriteData

 

 

//*************************************************************

// Writestring(s) //write string whereever cursor was at

 

void WriteString(char *input){

            char i=0;

            WriteCommand(MWRITE);

            while (input[i]!=0)

            {

                        WriteData(input[i]);

                        i++;

            }

} // end WriteString

 

//*************************************************************

// WriteStringSB(i,s)     //write string to line i of sidebox

 

void WriteStringSB(char line, char *input){

            MoveCursor((int)line*40+25);

            WriteString(input);

} // end WriteString

 

//*************************************************************

// lcdinit() initializes LCD

void lcdinit(void)

{

            // set I/O port directions

            DDRC = 0xff;       //set PORTD to outputs

            PORTB = LCDnop;    //issues nothing to the LCD for about 6 ms.

            delay_ms(2);

 

            PORTB = LCDreset;

            delay_ms(2);

 

            PORTB = LCDnop;

            delay_ms(2);

 

            WriteCommand(SystemSet);     //Set LCD system

            WriteData(0x30);

            WriteData(0x87);

            WriteData(0x07);

            WriteData(0x27);

            WriteData(0x2F);

            WriteData(0xC7);

            WriteData(0x28);

            WriteData(0x00);

 

            WriteCommand(Overlay);

            WriteData(0x00);

 

            WriteCommand(Scroll);

            WriteData(0x00);

            WriteData(0x00);

            WriteData(0xC8);

            WriteData(0xE8);

            WriteData(0x03);

            WriteData(0xC8);

 

            WriteCommand(CSRForm);

            WriteData(0x04);

            WriteData(0x86);

 

            WriteCommand(CSRRight);

 

            WriteCommand(HorzScroll);

            WriteData(0x00);

 

            WriteCommand(DispON);

            WriteData(0x16);

 

            clearLCD();                    //completely clears LCD

} // end lcdinit

 

//*************************************************************

// clearLCD() clears both layers of LCD

 

void clearLCD(void)

{

            unsigned short i;

 

            MoveCursor(0);   //set cursor to the beginning of the character layer

 

            WriteCommand(MWRITE);

            for (i=0; i<1000; i++) {

                        WriteData(0x20);    // write " " to character memory

            }

 

            MoveCursor(1000);   //set cursor to the beginning of the grpahics layer

 

            WriteCommand(MWRITE);

            for (i=0; i<8000; i++) {

                        WriteData(0x00);    // erase graphics memory

            }

} // end clearLCD

 

//*************************************************************

// clearSB() clears text layer of sidebox starting 5th row

 

void clearSB(void)

{

            unsigned char i, j;

            int address;

 

            address = 184;

            for (i=0; i<21; i++) {

                        MoveCursor(address);

                        WriteCommand(MWRITE);

                        for (j=0; j<16; j++) {

                                    WriteData(0x20);    // write " " to character memory

                        }

                        address = address+40;

            }

} // end clearLCD

 

//*************************************************************

// clearSB() clears text layer of middlebox

 

void clearMB(void)

{

            unsigned char i, j;

            int address;

 

            address = 163;

            for (i=0; i<18; i++) {

                        MoveCursor(address);

                        WriteCommand(MWRITE);

                        for (j=0; j<18; j++) {

                                    WriteData(0x20);    // write " " to character memory

                        }

                        address = address+40;

            }

} // end clearLCD

 

//*************************************************************

// DrawXY(x,y) turn on pixel at (x,y) in graphic layer

 

void DrawXY(int x,int y) {

            int address;

            address = 1000 + y*40 + (int)(x>>3);    //graphics starting address is 1000

            MoveCursor(address);                    //since each horizontal lines takes up 40 bytes,

                                                                                                                                    //we should add y*40 to the base address

                                                                                                                                    //then we add x/8 to the address to do the horizontal offset

            WriteCommand(MREAD);                    //Set LCD to be ready to be read

 

            DDRC = 0x00;                              //set PORTD to be inputs

            PORTC=0x00;                               //turn off all pull ups

            PORTB = DataRead;                         //send to LCD to do a data read

            delay_ms(10);                             //wait some time for everything to set(if this delay is too short, flickering increases)

            pixelbuffer = PIND;                       //read in the old data

            PORTB = LCDnop;                           //signal the LCD to stop reading

            delay_ms(2);                              //wait for LCD to stop and set PORTD back to outputs

            DDRC = 0xFF;

            pixelbuffer = pixelbuffer | (0x80>>(x%8));  //OR pixelbuffer with the byte you want to write, 0x80>>(x%8) is where we want to illuminate in that byte

 

            MoveCursor(address);                                                             //set the cursor back to where we were going to write to

            WriteCommand(MWRITE);

            WriteData(pixelbuffer);                                                             //write the pixel buffer which contained old and new pixel information

}

 

//*************************************************************

// MoveCursor(addr) moves cursor to specified address

 

void MoveCursor(int addr) {

            WriteCommand(CSRW);

            WriteData(addr);

            WriteData(addr>>8);

}

 

//*************************************************************

// DrawGB() draws out our basic gameboard on LCD

 

void DrawGB(void)

{

            unsigned char i, j;

            int address;

 

            // Draw Game Board (Graphics)

            address = 1000+40*8+2;

            // draw vertical line for left boxes

            for (i=0; i<191; i++) {

                        MoveCursor(address);

                        WriteCommand(MWRITE);

                        WriteData(0x02);

                        address = address+40;

            }

 

            address = 1000+40*8+20;

            // draw vertical line for right boxes

            for (i=0; i<191; i++) {

                        MoveCursor(address);

                        WriteCommand(MWRITE);

                        WriteData(0x02);

                        address = address+40;

            }

 

            address = 1000+24;

            // draw vertical line for right border

            for (i=0; i<200; i++) {

                        MoveCursor(address);

                        WriteCommand(MWRITE);

                        WriteData(0x80);

                        address = address+40;

            }

 

            address = 1000+40*8+4;

            // draw vertical line for top boxes

            for (i=0; i<23; i++) {

                        MoveCursor(address);

                        WriteCommand(MWRITE);

                        for (j=0; j<8; j++) {

                                    // one pixel thick line

                                    WriteData(0x02);

                                    WriteData(0x00);

                        }

                        address = address+40;

            }

 

            address = 1000+40*176+4;

            // draw vertical line for bottom boxes

            for (i=0; i<23; i++) {

                        MoveCursor(address);

                        WriteCommand(MWRITE);

                        for (j=0; j<8; j++) {

                                    // one pixel thick line

                                    WriteData(0x02);

                                    WriteData(0x00);

                        }

                        address = address+40;

            }

 

            address = 1000+40*7;             // 8 pixels down

            MoveCursor(address);

            WriteCommand(MWRITE);

            // draw line across

            for (i=0; i<24; i++) {

                        WriteData(0xff);

            }

 

            address = 1000+40*31;                       // 32 pixels down

            MoveCursor(address);

            WriteCommand(MWRITE);

            // draw line across

            for (i=0; i<24; i++) {

                        WriteData(0xff);

            }

 

            address = 1000+40*175;                     // 176 pixels down

            MoveCursor(address);

            WriteCommand(MWRITE);

            // draw line across

            for (i=0; i<24; i++) {

                        WriteData(0xff);

            }

 

            address = 1000+40*47;

            // draw horizontal line for left boxes

            for (i=0; i<8; i++) {

                        MoveCursor(address);

                        WriteCommand(MWRITE);

                        // 22 pixels across

                        WriteData(0xff);

                        WriteData(0xff);

                        WriteData(0xfe);

                        // move cursor to next box

                        address = address + (40*16);

            }

 

            address = 1000+40*47+20;

            // draw horizontal line for right boxes

            for (i=0; i<8; i++) {

                        MoveCursor(address);

                        WriteCommand(MWRITE);

                        // 25 pixels across

                        WriteData(0x03);

                        WriteData(0xff);

                        WriteData(0xff);

                        WriteData(0xff);

                        // move cursor to next box

                        address = address + (40*16);

            }

 

            // Draw Game Board (Text)

            MoveCursor(5);

            strcpyf(strbuffer, "CORNELL ");

            WriteString(strbuffer);

            strcpyf(strbuffer, "MONOPOLY");

            WriteString(strbuffer);

 

            // Top boxes

            MoveCursor(62);

            WriteCommand(MWRITE);

            WriteData('G');

            WriteData('O');

 

            MoveCursor(83);

            strcpyf(strbuffer, "MG??MLGSTCKR");

            WriteString(strbuffer);

            strcpyf(strbuffer, "OLWPUL TO");

            WriteString(strbuffer);

 

            MoveCursor(121);

            strcpyf(strbuffer, "FP22  22242026");

            WriteString(strbuffer);

            strcpyf(strbuffer, "261528 JA");

            WriteString(strbuffer);

 

            // Bottom boxes

            MoveCursor(881);

            WriteCommand(MWRITE);

            WriteData('J');

            WriteData('A');

 

            MoveCursor(923);

            strcpyf(strbuffer, "BNBY??LNTC$?");

            WriteString(strbuffer);

            strcpyf(strbuffer, "HNCEBT");

            WriteString(strbuffer);

            WriteData(0x7f);

            WriteData('P');

            WriteData('D');

 

            MoveCursor(963);

            strcpyf(strbuffer, "1210  1020%?");

            WriteString(strbuffer);

            strcpyf(strbuffer, " 6=D 6$20");

            WriteString(strbuffer);

 

            // Left boxes

            MoveCursor(160);

            WriteCommand(MWRITE);

            WriteData('R');

            WriteData('F');

            MoveCursor(200);

            WriteCommand(MWRITE);

            WriteData('2');

            WriteData('0');

 

            MoveCursor(240);

            WriteCommand(MWRITE);

            WriteData('C');

            WriteData('L');

            MoveCursor(280);

            WriteCommand(MWRITE);

            WriteData('1');

            WriteData('8');

           

            MoveCursor(320);

            WriteCommand(MWRITE);

            WriteData('C');

            WriteData('E');

            MoveCursor(360);

            WriteCommand(MWRITE);

            WriteData('=');

            WriteData('D');

 

            MoveCursor(400);

            WriteCommand(MWRITE);

            WriteData('B');

            WriteData('R');

            MoveCursor(440);

            WriteCommand(MWRITE);

            WriteData('1');

            WriteData('8');

 

            MoveCursor(480);

            WriteCommand(MWRITE);

            WriteData('T');

            WriteData('C');

            MoveCursor(520);

            WriteCommand(MWRITE);

            WriteData('2');

            WriteData('0');

 

            MoveCursor(560);

            WriteCommand(MWRITE);

            WriteData('B');

            WriteData('B');

            MoveCursor(600);

            WriteCommand(MWRITE);

            WriteData('1');

            WriteData('6');

 

            MoveCursor(640);

            WriteCommand(MWRITE);

            WriteData('D');

            WriteData('C');

            MoveCursor(680);

            WriteCommand(MWRITE);

            WriteData('1');

            WriteData('4');

 

            MoveCursor(720);

            WriteCommand(MWRITE);

            WriteData('C');

            WriteData('I');

            WriteData('T');

            MoveCursor(760);

            WriteCommand(MWRITE);

            WriteData('1');

            WriteData('5');

 

            MoveCursor(800);

            WriteCommand(MWRITE);

            WriteData('S');

            WriteData('M');

            MoveCursor(840);

            WriteCommand(MWRITE);

            WriteData('1');

            WriteData('4');

 

            // Right boxes

            MoveCursor(182);

            WriteCommand(MWRITE);

            WriteData('H');

            WriteData('O');

            MoveCursor(222);

            WriteCommand(MWRITE);

            WriteData('3');

            WriteData('0');

 

            MoveCursor(262);

            WriteCommand(MWRITE);

            WriteData('O');

            WriteData('P');

            MoveCursor(302);

            WriteCommand(MWRITE);

            WriteData('3');

            WriteData('0');

           

            MoveCursor(342);

            WriteCommand(MWRITE);

            WriteData('C');

            WriteData('E');

            MoveCursor(382);

            WriteCommand(MWRITE);

            WriteData('=');

            WriteData('D');

 

            MoveCursor(422);

            WriteCommand(MWRITE);

            WriteData('P');

            WriteData('H');

            MoveCursor(462);

            WriteCommand(MWRITE);

            WriteData('3');

            WriteData('2');

 

            MoveCursor(502);

            WriteCommand(MWRITE);

            WriteData('T');

            WriteData('C');

            MoveCursor(542);

            WriteCommand(MWRITE);

            WriteData('2');

            WriteData('0');

   

MoveCursor(582);

            WriteCommand(MWRITE);

            WriteData('?');

            MoveCursor(622);

            WriteCommand(MWRITE);

            WriteData('?');

           

            MoveCursor(662);

            WriteCommand(MWRITE);

            WriteData('R');

            WriteData('H');

            MoveCursor(702);

            WriteCommand(MWRITE);

            WriteData('3');

            WriteData('5');

           

            MoveCursor(742);

            WriteCommand(MWRITE);

            WriteData('$');

            WriteData('$');

            MoveCursor(782);

            WriteCommand(MWRITE);

            WriteData('=');

            WriteData('(');

 

            MoveCursor(822);

            WriteCommand(MWRITE);

            WriteData('D');

            WriteData('F');

            MoveCursor(862);

            WriteCommand(MWRITE);

            WriteData('4');

            WriteData('0');

/*

            DrawCar(1,10);

            DrawTower(0,65);

            DrawTower(177,65);

            DrawTower(137,185);

            DrawQuestion(43,16);

            DrawQuestion(59,184);

            DrawQuestion(180,112);

            DrawWave(136,17);

 

            DrawPattern1(152,176);

            DrawPattern1(120,176);

            DrawPattern2(72,176);

            DrawPattern2(40,176);

            DrawPattern2(24,176);

            DrawPattern3(18,160);

            DrawPattern3(18,128);

            DrawPattern3(18,112);

            DrawPattern4(18,80);

            DrawPattern4(18,48);

            DrawPattern4(18,32);

            DrawPattern5(24,8);

            DrawPattern5(56,8);

            DrawPattern5(72,8);

            DrawPattern6(104,8);

            DrawPattern6(120,8);

            DrawPattern6(152,8);

            DrawPattern7(168,80);

            DrawPattern7(168,48);

            DrawPattern7(168,32);

            DrawPattern8(168,128);

            DrawPattern8(168,160);

*/

}

 

//*************************************************************

// DrawMenu() draw on side menu depending on State

 

void DrawMenu(void)

{

            switch (State[currentplayer]) {

 

                        case INIT:        strcpyf(strbuffer, "# of Player");

                                                            WriteStringSB(5, strbuffer);

                                                            strcpyf(strbuffer, "(2 - 4)");

                                                            WriteStringSB(7, strbuffer);

                                                            MoveCursor(385);

                                                            break;

 

                        case NAME:    clearMB();

                                                            clearSB();

                                                            strcpyf(strbuffer, "Enter Name ");

                                                            WriteStringSB(5, strbuffer);

                                                            WriteData(currentplayer+49);

                                                            strcpyf(strbuffer, "(9 char max)");

                                                            WriteStringSB(7, strbuffer);

                                                            MoveCursor(385);

                                                            break;

 

                        case ROLL:      DrawCard(player[currentplayer].location);

                                                            clearSB();

                                                            UpdateTop();

                                                            strcpyf(strbuffer, "Roll");

                                                            WriteStringSB(5, strbuffer);

                                                            strcpyf(strbuffer, "1) Buy Houses");

                                                            WriteStringSB(8, strbuffer);

                                                            strcpyf(strbuffer, "2) Unmortgage");

                                                            WriteStringSB(10, strbuffer);

                                                            strcpyf(strbuffer, "0) Roll Dice");

                                                            WriteStringSB(12, strbuffer);

                                                            MoveCursor(585);

                                                            break;

 

                        case OWNED: clearSB();

                                                            UpdateTop();

                                                            strcpyf(strbuffer, "Rolled ");

                                                            WriteStringSB(4, strbuffer);

                                                            WriteData(Rolled[0]+48);

                                                            WriteData(',');

                                                            WriteData(Rolled[1]+48);

                                                            strcpyf(strbuffer, "Owned by");

                                                            WriteStringSB(5, strbuffer);

                                                            WriteStringSB(6, player[owner[player[currentplayer].location]].name);

                                                            strcpyf(strbuffer, "1) Mortgage");

                                                            WriteStringSB(8, strbuffer);

                                                            strcpyf(strbuffer, "2) Bankrupt");

                                                            WriteStringSB(10, strbuffer);

                                                            strcpyf(strbuffer, "0) End Round");

                                                            WriteStringSB(12, strbuffer);

                                                            strcpyf(strbuffer, "Paid:   ");

                                                            WriteStringSB(16, strbuffer);

                                                            itoa(player[currentplayer].rentpaid, strbuffer);

                                                            WriteString(strbuffer);

                                                            strcpyf(strbuffer, "Remain: ");

                                                            WriteStringSB(18, strbuffer);

                                                            itoa(player[currentplayer].rentdue, strbuffer);

                                                            WriteString(strbuffer);

                                                            MoveCursor(825);

                                                            break;

 

                        case UNOWNED:       clearSB();

                                                                        UpdateTop();

                                                                        strcpyf(strbuffer, "Rolled ");

                                                                        WriteStringSB(4, strbuffer);

                                                                        WriteData(Rolled[0]+48);

                                                                        WriteData(',');

                                                                        WriteData(Rolled[1]+48);

                                                                        strcpyf(strbuffer, "Unowned");

                                                                        WriteStringSB(5, strbuffer);

                                                                        strcpyf(strbuffer, "1) Buy");

                                                                        WriteStringSB(8, strbuffer);

                                                                        strcpyf(strbuffer, "2) Mortgage");

                                                                        WriteStringSB(10, strbuffer);

                                                                        strcpyf(strbuffer, "0) End Round");

                                                                        WriteStringSB(12, strbuffer);

                                                                        MoveCursor(665);

                                                                        break;

 

                        case MORTGAGED:   clearSB();

                                                                        UpdateTop();

                                                                        strcpyf(strbuffer, "Rolled ");

                                                                        WriteStringSB(4, strbuffer);

                                                                        WriteData(Rolled[0]+48);

                                                                        WriteData(',');

                                                                        WriteData(Rolled[1]+48);

                                                                        strcpyf(strbuffer, "Mortgaged by");

                                                                        WriteStringSB(5, strbuffer);

                                                                        WriteStringSB(6, player[owner[player[currentplayer].location]-5].name);

                                                                        strcpyf(strbuffer, "Pay no rent");

                                                                        WriteStringSB(8, strbuffer);

                                                                        strcpyf(strbuffer, "0) End Round");

                                                                        WriteStringSB(10, strbuffer);

                                                                        MoveCursor(505);

                                                                        break;

 

                        case SELL:       clearSB();

                                                            UpdateTop();

                                                            strcpyf(strbuffer, "Mortgage Menu");

                                                            WriteStringSB(5, strbuffer);

                                                            strcpyf(strbuffer, "1) Sell Houses");

                                                            WriteStringSB(8, strbuffer);

                                                            strcpyf(strbuffer, "2) Property");

                                                            WriteStringSB(10, strbuffer);

                                                            strcpyf(strbuffer, "0) Done");

                                                            WriteStringSB(12, strbuffer);

                                                            MoveCursor(585);

                                                            break;

 

                        case JA:           clearSB();

                                                            UpdateTop();

                                                            strcpyf(strbuffer, "JA Menu");

                                                            WriteStringSB(5, strbuffer);

                                                            strcpyf(strbuffer, "1) Pay $50");

                                                            WriteStringSB(8, strbuffer);

                                                            strcpyf(strbuffer, "2) Roll Dice");

                                                            WriteStringSB(10, strbuffer);

                                                            MoveCursor(505);

                                                            break;

 

                        case BURSAR:            clearSB();

                                                                        UpdateTop();

                                                                        strcpyf(strbuffer, "Rolled ");

                                                                        WriteStringSB(4, strbuffer);

                                                                        WriteData(Rolled[0]+48);

                                                                        WriteData(',');

                                                                        WriteData(Rolled[1]+48);

                                                                        strcpyf(strbuffer, "Bursar Menu");

                                                                        WriteStringSB(5, strbuffer);

                                                                        strcpyf(strbuffer, "1) Mortgage");

                                                                        WriteStringSB(8, strbuffer);

                                                                        strcpyf(strbuffer, "2) Bankrupt");

                                                                        WriteStringSB(10, strbuffer);

                                                                        strcpyf(strbuffer, "0) End Round");

                                                                        WriteStringSB(12, strbuffer);

                                                                        strcpyf(strbuffer, "Paid:   ");

                                                                        WriteStringSB(16, strbuffer);

                                                                        itoa(player[currentplayer].rentpaid, strbuffer);

                                                                        WriteString(strbuffer);

                                                                        strcpyf(strbuffer, "Remain: ");

                                                                        WriteStringSB(18, strbuffer);

                                                                        itoa(player[currentplayer].rentdue, strbuffer);

                                                                        WriteString(strbuffer);

                                                                        MoveCursor(825);

                                                                        break;

 

                        case TUITION:            clearSB();

                                                                        UpdateTop();

                                                                        strcpyf(strbuffer, "Rolled ");

                                                                        WriteStringSB(4, strbuffer);

                                                                        WriteData(Rolled[0]+48);

                                                                        WriteData(',');

                                                                        WriteData(Rolled[1]+48);

                                                                        strcpyf(strbuffer, "Tuition Menu");

                                                                        WriteStringSB(5, strbuffer);

                                                                        strcpyf(strbuffer, "1) Mortgage");

                                                                        WriteStringSB(8, strbuffer);

                                                                        strcpyf(strbuffer, "2) Bankrupt");

                                                                        WriteStringSB(10, strbuffer);

                                                                        strcpyf(strbuffer, "0) End Round");

                                                                        WriteStringSB(12, strbuffer);

                                                                        strcpyf(strbuffer, "Paid:   ");

                                                                        WriteStringSB(16, strbuffer);

                                                                        itoa(player[currentplayer].rentpaid, strbuffer);

                                                                        WriteString(strbuffer);

                                                                        strcpyf(strbuffer, "Remain: ");

                                                                        WriteStringSB(18, strbuffer);

                                                                        itoa(player[currentplayer].rentdue, strbuffer);

                                                                        WriteString(strbuffer);

                                                                        MoveCursor(825);

                                                                        break;

                        }

}

 

//*************************************************************

// DrawPattern() draw pattern of card

void DrawPattern(char i)

{

/*         unsigned char a, b;

 

            switch (pattern[i]) {

                        case 1:              for (a=32;a<48;a+4)

                                                            {

                                                                        for (b=24;b<152;b+15) //167

                                                                        {

                                                                                    DrawPattern1(b,a);

                                                                        }

                                                            }

                                                            break;

 

                        case 2:

                                                            for (a=32;a<48;a+4)

                                                            {

                                                                        for (b=24;b<152;b+15)

                                                                        {

                                                                                    DrawPattern2(b,a);

                                                                        }

                                                            }

                                                            break;

 

                        case 3:              for (a=32;a<48;a+15)

                                                            {

                                                                        for (b=24;b<164;b+3)

                                                                        {

                                                                                    DrawPattern3a(b,a);

                                                                        }

                                                            }

                                                            break;

 

                        case 4:              for (a=32;a<48;a+15)

                                                            {

                                                                        for (b=24;b<164;b+3)

                                                                        {

                                                                                    DrawPattern4a(b,a);

                                                                        }

                                                            }

                                                            break;

 

                        case 5:              for (a=32;a<48;a+4)

                                                            {

                                                                        for (b=24;b<152;b+15)

                                                                        {

                                                                                    DrawPattern5(b,a);

                                                                        }

                                                            }

                                                            break;

 

                        case 6:              for (a=24;a<167;a++)

                                                                        DrawPattern6(a,47);

                                                            break;

 

                        case 7:              for (a=32;a<48;a+15)

                                                            {

                                                                        for (b=24;b<163;b+4) //167

                                                                        {

                                                                                    DrawPattern7(b,a);

                                                                        }

                                                            }

                                                            break;

 

                        case 8:              for (a=32;a<48;a+15)

                                                            {

                                                                        for (b=24;b<163;b+4)

                                                                        {

                                                                                    DrawPattern8(b,a);

                                                                        }

                                                            }

                                                            break;

            }

*/

}

 

//*************************************************************

// DrawCard(i) draw card in middle box when landed

 

void DrawCard(char i)

{

            clearMB();

            DrawPattern(i);

            MoveCursor(243);

            GetName(i);

            WriteString(strbuffer);

            if (buybuild[i] != 0) {

                        MoveCursor(283);

                        strcpyf(strbuffer, "Cost $");

                        WriteString(strbuffer);

                        itoa(mortgage[i]*(int)2, strbuffer);

                        WriteString(strbuffer);

                        strcpyf(strbuffer, " Rent $");

                        WriteString(strbuffer);

                        itoa(rent[i], strbuffer);

                        WriteString(strbuffer);

                        if (buybuild[i] == 2) {

                                    MoveCursor(363);

                                    strcpyf(strbuffer, "With 1 House $");

                                    WriteString(strbuffer);

                                    itoa(house1[i], strbuffer);

                                    WriteString(strbuffer);

                                    MoveCursor(407);

                                    strcpyf(strbuffer, "2 Houses $");

                                    WriteString(strbuffer);

                                    itoa(house2[i], strbuffer);

                                    WriteString(strbuffer);

                                    MoveCursor(447);

                                    strcpyf(strbuffer, "3 Houses $");

                                    WriteString(strbuffer);

                                    itoa(house3[i], strbuffer);

                                    WriteString(strbuffer);

                                    MoveCursor(487);

                                    strcpyf(strbuffer, "4 Houses $");

                                    WriteString(strbuffer);

                                    itoa(house4[i], strbuffer);

                                    WriteString(strbuffer);

                                    MoveCursor(529);

                                    strcpyf(strbuffer, "Hotel  $");

                                    WriteString(strbuffer);

                                    itoa(hotel[i], strbuffer);

                                    WriteString(strbuffer);

                                    MoveCursor(644);

                                    strcpyf(strbuffer, "House/Hotel $");

                                    WriteString(strbuffer);

                                    itoa(housecost[i], strbuffer);

                                    WriteString(strbuffer);

                                    MoveCursor(803);

                                    strcpyf(strbuffer, " Houses:");

                                    WriteString(strbuffer);

                                    itoa(building[i]%5, strbuffer);

                                    WriteString(strbuffer);

                                    strcpyf(strbuffer, " Hotel:");

                                    WriteString(strbuffer);

                                    itoa((building[i]==5), strbuffer);

                                    WriteString(strbuffer);

                        }

                        MoveCursor(606);

                        strcpyf(strbuffer, "Mortgage  $");

                        WriteString(strbuffer);

                        itoa(mortgage[i], strbuffer);

                        WriteString(strbuffer);

 

                        MoveCursor(724);

                        if (owner[i] == 4) {

                                    strcpyf(strbuffer, "Unowned");

                                    WriteString(strbuffer);

                        }

                        else if (owner[i] > 4) {

                                    strcpyf(strbuffer, "Mortg: ");

                                    WriteString(strbuffer);

                                    WriteString(player[owner[i]-5].name);

                        }

                        else {

                                    strcpyf(strbuffer, "Owner: ");

                                    WriteString(strbuffer);

                                    WriteString(player[owner[i]].name);

                        }

            }

}

 

 

//*************************************************************

// GetName(i) copy name of property at square i into strbuffer

 

void GetName(char i)

{

            switch(i) {

                        case 0:      strcpyf(strbuffer, "Pay Day");

                                                            break;

                        case 1:     strcpyf(strbuffer, "Barton Hall");

                                                            break;

                        case 2:     strcpyf(strbuffer, "Campus Events");

                                                            break;

                        case 3:     strcpyf(strbuffer, "Helen Newman");

                                                            break;

                        case 4:     strcpyf(strbuffer, "Tuition");

                                                            break;

                        case 5:     strcpyf(strbuffer, "TCAT 1");

                                                            break;

                        case 6:     strcpyf(strbuffer, "Lincoln Hall");

                                                            break;

                        case 7:     strcpyf(strbuffer, "Chance");

                                                            break;

                        case 8:     strcpyf(strbuffer, "Bailey Hall");

                                                            break;

                        case 9:     strcpyf(strbuffer, "Barnes Hall");

                                                            break;

                        case 10:    strcpyf(strbuffer, "JA Office");

                                                            break;

                        case 11:    strcpyf(strbuffer, "Seeley Mudd Hall");

                                                            break;

                        case 12:    strcpyf(strbuffer, "C.I.T");

                                                            break;

                        case 13:    strcpyf(strbuffer, "Dale Corson Hall");

                                                            break;

                        case 14:    strcpyf(strbuffer, "BioTech Building");

                                                            break;

                        case 15:    strcpyf(strbuffer, "TCAT 2");

                                                            break;

                        case 16:    strcpyf(strbuffer, "Baker Lab");

                                                            break;

                        case 17:    strcpyf(strbuffer, "Campus Events");

                                                            break;

                        case 18:    strcpyf(strbuffer, "Clark Hall");

                                                            break;

                        case 19:    strcpyf(strbuffer, "Rockefeller");

                                                            break;

                        case 20:    strcpyf(strbuffer, "Free Parking");

                                                            break;

                        case 21:    strcpyf(strbuffer, "McGraw Hall");

                                                            break;

                        case 22:    strcpyf(strbuffer, "Chance");

                                                            break;

                        case 23:    strcpyf(strbuffer, "Morrill Hall");

                                                            break;

                        case 24:    strcpyf(strbuffer, "Goldwin Smith");

                                                            break;

                        case 25:    strcpyf(strbuffer, "TCAT 3");

                                                            break;

                        case 26:    strcpyf(strbuffer, "Kroch Library");

                                                            break;

                        case 27:    strcpyf(strbuffer, "Olin Library");

                                                            break;

                        case 28:    strcpyf(strbuffer, "Water Plant");

                                                            break;

                        case 29:    strcpyf(strbuffer, "Uris Library");

                                                            break;

                        case 30:    strcpyf(strbuffer, "Go To JA");

                                                            break;

                        case 31:    strcpyf(strbuffer, "Hollister Hall");

                                                            break;

                        case 32:    strcpyf(strbuffer, "Upson Hall");

                                                            break;

                        case 33:    strcpyf(strbuffer, "Campus Events");

                                                            break;

                        case 34:    strcpyf(strbuffer, "Phillips Hall");

                                                            break;

                        case 35:    strcpyf(strbuffer, "TCAT 4");

                                                            break;

                        case 36:    strcpyf(strbuffer, "Chance");

                                                            break;

                        case 37:    strcpyf(strbuffer, "Rhodes Hall");

                                                            break;

                        case 38:    strcpyf(strbuffer, "Bursar Office");

                                                            break;

                        case 39:    strcpyf(strbuffer, "Duffield");

                                                            break;

            }

}

 

//*************************************************************

// GameInit() initializes players at start of game

 

void GameInit(void)

{

            unsigned char i;

 

            for (i=0; i<40; i++) {

                        building[i] = 0;  // all property starts with no building

                        owner[i] = 4;                // owned by nobody

            }

 

            strcpyf(strbuffer, "Welcome to");

            MoveCursor(367);

            WriteString(strbuffer);

            strcpyf(strbuffer, "Cornell Monopoly");

            MoveCursor(444);

            WriteString(strbuffer);

 

            for (i=0; i<4; i++) {

                        player[i].cash = 1500;

                        player[i].asset = 1500;

                        player[i].location = 0;

                        player[i].roll = 0;

                        player[i].rentpaid = 0;

                        player[i].rentdue = 0;

            }

            currentplayer = 0;

            chance = 0;

            campusevent = 0;

            State[0] = INIT;

            State[1] = NAME;

            State[2] = NAME;

            State[3] = NAME;

 

            strcpyf(player[0].name, "Player1");

            strcpyf(player[1].name, "Player2");

            strcpyf(player[2].name, "Player3");

            strcpyf(player[3].name, "Player4");

 

            DrawMenu();

}

 

//*************************************************************

// nextplayer() next player

 

void nextplayer(void)

{

            currentplayer++;

            if (currentplayer == numplayer) {

                        currentplayer = 0;

            }

}

 

 

//*************************************************************

// GetRent(i) returns rent of place i

 

int GetRent(char i) {

 

            unsigned char a[4];

            unsigned char b;

 

            if ((i == 12) || (i == 28)) { // Utilities

                        if (owner[12] == owner[28]) {

                                    return 10*(Rolled[0]+Rolled[1]);

                        }

                        else {

                                    return 4*(Rolled[0]+Rolled[1]);

                        }

            }

            else if ((i == 5) || (i == 15) || (i == 25) || (i == 35)) { // TCAT

                        for (b=0; b<4; b++) {

                                    a[b] = 0;

                        }

 

                        for (b=0; b<4; b++) {

                                    if (owner[b] == 0) {

                                                a[0]++;

                                    }

                                    if (owner[b] == 1) {

                                                a[1]++;

                                    }

                                    if (owner[b] == 2) {

                                                a[2]++;

                                    }

                                    if (owner[b] == 3) {

                                                a[3]++;

                                    }

                        }

                        if (a[i] == 0) {

                                    return 25;

                        }

                        else if (a[i] == 1) {

                                    return 50;

                        }

                        else if (a[i] == 2) {

                                    return 100;

                        }

                        else if (a[i] == 3) {

                                    return 200;

                        }

            }

            else {

                        if (building[i] == 0) {

                                    return rent[i];

                        }

                        else if (building[i] == 1) {

                                    return house1[i];

                        }

                        else if (building[i] == 2) {

                                    return house2[i];

                        }

                        else if (building[i] == 3) {

                                    return house3[i];

                        }

                        else if (building[i] == 4) {

                                    return house4[i];

                        }

                        else if (building[i] == 5) {

                                    return hotel[i];

                        }

            }

}

 

//*************************************************************

// UpdateTop() display cash owned by current player

 

void UpdateTop(void)

{

            unsigned char i, j;

            int address;

 

            address = 24;                                       // clear Top

            for (i=0; i<4; i++) {

                        MoveCursor(address);

                        WriteCommand(MWRITE);

                        for (j=0; j<4; j++) {

                                    WriteData(0x20);    // write " " to character memory

                        }

                        address = address+40;

            }

            // write current player name and cash

            WriteStringSB(1, player[currentplayer].name);

            strcpyf(strbuffer, "$     ");

            WriteStringSB(3, strbuffer);

            itoa(player[currentplayer].cash, strbuffer);

            MoveCursor(146);

            WriteString(strbuffer);

}

 

//*************************************************************

// RollDice() return random number

 

char RollDice(void)

{

            return (rand() % 6) + 1;

            //return 2;

}

 

//*************************************************************

// StateMachine() drive game state machine

 

void StateMachine(void)

{

            unsigned oldloc;

 

            switch (State[currentplayer]) {

                        case INIT:        if ((butnum > 1) && (butnum < 5)) {

                                                                        if (currentplayer == 0) {

                                                                                    numplayer = butnum;

                                                                        }

                                                                        State[0] = NAME;

                                                            }

                                                            else {

                                                                        strcpyf(strbuffer, "Invalid Input");

                                                                        WriteStringSB(11, strbuffer);

                                                                        MoveCursor(385);

                                                            }

                                                            break;

 

                        case NAME:  // implement name input later

                                                            State[currentplayer] = ROLL;

                                                            nextplayer();

                                                            break;

 

                        case ROLL:      if (butnum == 0) {         // Roll

                                                                        Rolled[0] = RollDice();

                                                                        Rolled[1] = RollDice();

                                                                        if (Rolled[0] == Rolled[1]) {

                                                                                    player[currentplayer].roll++;

                                                                        }

                                                                        else {

                                                                                    player[currentplayer].roll = 0;

                                                                        }

                                                                        if (player[currentplayer].roll == 3) {

                                                                                    player[currentplayer].location = 10;       // JA Office

                                                                                    player[currentplayer].roll = 0;

                                                                                    State[currentplayer] = JA;

                                                                                    nextplayer();

                                                                        }

                                                                        else {

                                                                                    oldloc = player[currentplayer].location;

                                                                                    player[currentplayer].location = (player[currentplayer].location+Rolled[0]+Rolled[1])%40;

                                                                                    DrawCard(player[currentplayer].location);

                                                                                    if (oldloc > player[currentplayer].location) {      // past Pay Day

                                                                                                player[currentplayer].cash += 200;

                                                                                    }

                                                                                    if (buybuild[player[currentplayer].location] != 0) {

                                                                                                if (owner[player[currentplayer].location] == 4) {

                                                                                                            State[currentplayer] = UNOWNED;

                                                                                                }

                                                                                                else if (owner[player[currentplayer].location] > 4) {

                                                                                                            State[currentplayer] = MORTGAGED;

                                                                                                }

                                                                                                else {

                                                                                                            if (owner[player[currentplayer].location] != currentplayer) {

                                                                                                                        player[currentplayer].rentdue = GetRent(player[currentplayer].location);

                                                                                                                        if (player[currentplayer].cash >= player[currentplayer].rentdue) {

                                                                                                                                    player[currentplayer].rentpaid = player[currentplayer].rentdue;

                                                                                                                                    player[currentplayer].rentdue = 0;

                                                                                                                                    player[currentplayer].cash -= player[currentplayer].rentpaid;

                                                                                                                        }

                                                                                                                        else {

                                                                                                                                    player[currentplayer].rentpaid = player[currentplayer].cash;

                                                                                                                                    player[currentplayer].rentdue -= player[currentplayer].cash;

                                                                                                                                    player[currentplayer].cash = 0;

                                                                                                                        }

                                                                                                            }

                                                                                                            else {

                                                                                                                        player[currentplayer].rentpaid = 0;

                                                                                                                        player[currentplayer].rentdue = 0;

                                                                                                            }

                                                                                                            State[currentplayer] = OWNED;

                                                                                                }

                                                                                    }

                                                                                    else if (player[currentplayer].location == 30) { // JA

                                                                                                State[currentplayer] = JA;

                                                                                                player[currentplayer].location = 10;

                                                                                                player[currentplayer].roll = 0;

                                                                                                nextplayer();

                                                                                    }

                                                                        }

                                                            }

                                                            else if (butnum == 1) {  // Buy House

                                                                        laststate = ROLL;

                                                                        strcpyf(strbuffer, "Where (00-39)");

                                                                        WriteStringSB(16, strbuffer);

                                                                        MoveCursor(745);

                                                                        State[currentplayer] = BUYH1;

                                                            }

                                                            else if (butnum == 2) {  // Unmortgage

                                                                        laststate = ROLL;

                                                                        strcpyf(strbuffer, "Where (00-39)");

                                                                        WriteStringSB(16, strbuffer);

                                                                        MoveCursor(745);

                                                                        State[currentplayer] = UNMORT1;

                                                            }

                                                            break;

 

                        case OWNED: if (butnum == 0) {        // End Round

                                                                        if (player[currentplayer].rentdue == 0) {

                                                                                    State[currentplayer] = ROLL;

                                                                                    if (player[currentplayer].roll == 0) {

                                                                                                nextplayer();

                                                                                    }

                                                                        }

                                                            }

                                                            else if (butnum == 1) {  // Mortgage

                                                                        laststate = OWNED;

                                                                        State[currentplayer] = SELL;

                                                            }

                                                            else if (butnum == 2) { // Bankrupt

 

                                                            }

                                                            break;

 

                        case UNOWNED:       if (butnum == 0) { // End Round

                                                                                    State[currentplayer] = ROLL;

                                                                                    if (player[currentplayer].roll == 0) {

                                                                                                nextplayer();

                                                                                    }

                                                                        }

                                                                        else if (butnum == 1) { // Buy

                                                                                    if (player[currentplayer].cash >= 2*mortgage[player[currentplayer].location]) {

                                                                                                owner[player[currentplayer].location] = currentplayer;

                                                                                                player[currentplayer].cash -= 2*mortgage[player[currentplayer].location];

                                                                                                State[currentplayer] = ROLL;

                                                                                                if (player[currentplayer].roll == 0) {

                                                                                                            nextplayer();

                                                                                                }

                                                                                    }

                                                                                    else {

                                                                                                strcpyf(strbuffer, "No Money");

                                                                                                WriteStringSB(18, strbuffer);

                                                                                    }

                                                                        }

                                                                        else if (butnum == 2) { // Mortgage

                                                                                    laststate = UNOWNED;

                                                                                    State[currentplayer] = SELL;

                                                                        }

                                                                        break;

 

                        case MORTGAGED:   if (butnum == 0) { // End Round

                                                                                    State[currentplayer] = ROLL;

                                                                                    if (player[currentplayer].roll == 0) {

                                                                                                nextplayer();

                                                                                    }

                                                                        }

                                                                        break;

 

                        case SELL:       if (butnum == 0) { // Done

                                                                        State[currentplayer] = laststate;

                                                            }

                                                            else if (butnum == 1) { // Sell House

                                                                        State[currentplayer] = SELLH1;

                                                                        strcpyf(strbuffer, "Where (00-39)");

                                                                        WriteStringSB(16, strbuffer);

                                                                        MoveCursor(745);

                                                            }

                                                            else if (butnum == 2) { // Mortgage Property

                                                                        State[currentplayer] = SELLP1;

                                                                        strcpyf(strbuffer, "Where (00-39)");

                                                                        WriteStringSB(16, strbuffer);

                                                                        MoveCursor(745);

                                                            }

                                                            break;

 

                        case JA:           if (butnum == 1) { // Pay $5

                                                                        if (player[currentplayer].cash >= 50) {

                                                                                    player[currentplayer].cash -= 50;

                                                                                    State[currentplayer] = ROLL;

                                                                                    player[currentplayer].roll = 0;

                                                                                    nextplayer();

                                                                        }

                                                            }

                                                            else if (butnum == 2) { // Roll Dice

                                                                        Rolled[0] = RollDice();

                                                                        Rolled[1] = RollDice();

                                                                        if (Rolled[0] == Rolled[1]) {

                                                                                    State[currentplayer] = ROLL;

                                                                        }

                                                                        player[currentplayer].roll = 0;

                                                                        nextplayer();

                                                            }

                                                            break;

 

                        case BURSAR:            if (butnum == 0) { // End Round

                                                                                    if (player[currentplayer].rentdue == 0) {

                                                                                                nextplayer();

                                                                                    }

                                                                        }

                                                                        else if (butnum == 1) { // Mortgage

                                                                                    laststate = UNOWNED;

                                                                                    State[currentplayer] = SELL;

                                                                        }

                                                                        else if (butnum == 2) { // Bankrupt

 

                                                                        }

                                                                        break;

 

                        case TUITION:            if (butnum == 0) { // End Round

                                                                                    if (player[currentplayer].rentdue == 0) {

                                                                                                nextplayer();

                                                                                    }

                                                                        }

                                                                        else if (butnum == 1) { // Mortgage

                                                                                    laststate = UNOWNED;

                                                                                    State[currentplayer] = SELL;

                                                                        }

                                                                        else if (butnum == 2) { // Bankrupt

 

                                                                        }

                                                                        break;

 

                        case BUYH1:               if (butnum < 4) {

                                                                                    houseindex = 10*butnum;

                                                                                    State[currentplayer] = BUYH2;

                                                                        }

                                                                        else {

                                                                                    State[currentplayer] = laststate;

                                                                        }

                                                                        break;

 

                        case BUYH2:               houseindex += butnum;

                                                                        if (owner[houseindex] == currentplayer) {

                                                                                    if (buybuild[houseindex] == 2) {

                                                                                                if (player[currentplayer].cash >= housecost[houseindex]) {

                                                                                                            if (building[houseindex] != 5) {

                                                                                                                        building[houseindex]++;

                                                                                                                        player[currentplayer].cash -= housecost[houseindex];

                                                                                                                        strcpyf(strbuffer, "Buy Success");

                                                                                                                        WriteStringSB(20, strbuffer);

                                                                                                            }

                                                                                               

                                                                                                            else {

                                                                                                                        strcpyf(strbuffer, "Buy Fail");

                                                                                                                        WriteStringSB(20, strbuffer);

                                                                                                            }

                                                                                                }

                                                                                                else {

                                                                                                            strcpyf(strbuffer, "No Money");

                                                                                                            WriteStringSB(20, strbuffer);

                                                                                                }

                                                                                    }

                                                                                    else {

                                                                                                strcpyf(strbuffer, "Can't Build");

                                                                                                WriteStringSB(20, strbuffer);

                                                                                    }         

                                                                                    State[currentplayer] = BUYH3;

                                                                        }

                                                                        else {

                                                                                    strcpyf(strbuffer, "Land Not Yours");

                                                                                    WriteStringSB(20, strbuffer);

                                                                                    State[currentplayer] = BUYH3;

                                                                        }

                                                                        break;

 

                        case BUYH3:               State[currentplayer] = laststate;

                        break;

 

                        case SELLH1:  if (butnum < 4) {

                                                                                    houseindex = 10*butnum;

                                                                                    State[currentplayer] = SELLH2;

                                                                        }

                                                                        else {

                                                                                    State[currentplayer] = SELL;

                                                                        }

                                                                        break;

 

                        case SELLH2:  houseindex += butnum;

                                                                        if (owner[houseindex] == currentplayer) {

                                                                                    if (building[houseindex] != 0) {

                                                                                                building[houseindex]--;

                                                                                                player[currentplayer].cash += housecost[houseindex];

                                                                                                strcpyf(strbuffer, "Sell Success");

                                                                                                WriteStringSB(20, strbuffer);

                                                                                    }

                                                                                    else {

                                                                                                strcpyf(strbuffer, "Sell Fail");

                                                                                                WriteStringSB(20, strbuffer);

                                                                                    }

                                                                        }

                                                                        else {

                                                                                    strcpyf(strbuffer, "Land Not Yours");

                                                                                    WriteStringSB(20, strbuffer);

                                                                        }

                                                                        State[currentplayer] = SELLH3;

                                                                        break;

 

                        case SELLH3:  State[currentplayer] = SELL;

                                                                        break;

 

                        case SELLP1:  if (butnum < 4) {

                                                                                    houseindex = 10*butnum;

                                                                                    State[currentplayer] = SELLP2;

                                                                        }

                                                                        else {

                                                                                    State[currentplayer] = SELL;

                                                                        }

                                                                        break;

 

                        case SELLP2:  houseindex += butnum;

                                                                        if (owner[houseindex] == currentplayer) {

                                                                                    if (building[houseindex] == 0) {

                                                                                                owner[houseindex] = currentplayer+5;

                                                                                                player[currentplayer].cash += mortgage[houseindex];

                                                                                                strcpyf(strbuffer, "Sell Success");

                                                                                                WriteStringSB(20, strbuffer);

                                                                                    }

                                                                                    else {

                                                                                                strcpyf(strbuffer, "Have Houses");

                                                                                                WriteStringSB(20, strbuffer);

                                                                                    }

                                                                        }

                                                                        else {

                                                                                    strcpyf(strbuffer, "Land Not Yours");

                                                                                    WriteStringSB(20, strbuffer);

                                                                        }

                                                                        State[currentplayer] = SELLP3;

                                                                        break;

 

                        case SELLP3:  State[currentplayer] = SELL;

                                                                        break;

 

                        case UNMORT1:         if (butnum < 4) {

                                                                                    houseindex = 10*butnum;

                                                                                    State[currentplayer] = UNMORT2;

                                                                        }

                                                                        else {

                                                                                    State[currentplayer] = ROLL;

                                                                        }

                                                                        break;

 

                        case UNMORT2:         houseindex += butnum;

                                                                        if (owner[houseindex] == currentplayer+5) {

                                                                                    if (player[currentplayer].cash >= mortgage[houseindex]) {

                                                                                                            owner[houseindex] -= 5;

                                                                                                           player[currentplayer].cash -= mortgage[houseindex];

                                                                                                            strcpyf(strbuffer, "UnMort Success");

                                                                                                            WriteStringSB(20, strbuffer);

                                                                                    }

                                                                                    else {

                                                                                                strcpyf(strbuffer, "No Money");

                                                                                                WriteStringSB(20, strbuffer);

                                                                                    }

                                                                        }

                                                                        else {

                                                                                    strcpyf(strbuffer, "Not Mortgaged");

                                                                                    WriteStringSB(20, strbuffer);

                                                                        }

                                                                        State[currentplayer] = BUYH3;

                                                                        break;

 

                        case UNMORT3:         State[currentplayer] = ROLL;

                                                                        break;

            }

}

 

 

//*************************************************************

void initialize(void)

{

            DDRB = 0xff;

            DDRC = 0xff;

 

            // setup timer0

            reload = 256 - 125;      // value for 2 millisecond

            TCNT0 = reload;

            TCCR0 = 3;                 // prescalar to 64

            TIMSK = 1;                 // turn on timer0 overflow ISR

 

            // init task timers

            checkkey_t = CHECKKEY;

    keyblock = 0;

 

            lcdinit();

            DrawGB();

 

            // crank up the ISRs

            #asm

                        sei

            #endasm

}

 

//**************************************************************

/*

void DrawCar(int bx, int by)

{

            unsigned char a;

            for (a=6;a<14;a++)

                        DrawXY(bx+a,by+0);

            DrawXY(bx+5,by+1);

            for (a=1;a<6;a++)

                        DrawXY(bx+10,by+a);

            DrawXY(bx+14,by+1);

            DrawXY(bx+4,by+2);

            DrawXY(bx+5,by+2);

            DrawXY(bx+14,by+2);

            DrawXY(bx+15,by+2);

            DrawXY(bx+4,by+3);

            DrawXY(bx+15,by+3);

            DrawXY(bx+3,by+4);

            DrawXY(bx+4,by+4);

            DrawXY(bx+15,by+4);

            DrawXY(bx+16,by+4);

            DrawXY(bx+3,by+5);

            DrawXY(bx+16,by+5);

            for (a=1;a<19;a++)

                        DrawXY(bx+a,by+6);

            for (a=0;a<20;a++)

                        DrawXY(bx+a,by+7);

            for (a=0;a<20;a++)

                        DrawXY(bx+a,by+8);

            for (a=0;a<20;a++)

                        DrawXY(bx+a,by+9);

            for (a=0;a<20;a++)

                        DrawXY(bx+a,by+10);

            for (a=3;a<7;a++)

                        DrawXY(bx+a,by+11);

            for (a=3;a<7;a++)

                        DrawXY(bx+a,by+12);

            for (a=13;a<17;a++)

                        DrawXY(bx+a,by+11);

            for (a=13;a<17;a++)

                        DrawXY(bx+a,by+12);

            DrawXY(bx+4,by+13);

            DrawXY(bx+5,by+13);

            DrawXY(bx+14,by+13);

            DrawXY(bx+15,by+13);

}

 

void DrawTower(int bx, int by)

{

            unsigned char a;

 

            DrawXY(bx+3,by+0);

            for (a=2;a<5;a++)

                        DrawXY(bx+a,by+1);

            for (a=1;a<6;a++)

            {

                        DrawXY(bx+a,by+2);

                        DrawXY(bx+a,by+4);

                        DrawXY(bx+a,by+8);

            }

            for (a=0;a<7;a++)

                        DrawXY(bx+a,by+3);

            for (a=5;a<8;a++)

            {

                        DrawXY(bx+1,by+a);

                        DrawXY(bx+5,by+a);

            }

            for (a=1;a<11;a++)

                        DrawXY(bx+a,by+9);

            for (a=1;a<9;a++)

                        DrawXY(bx+a,by+10);

            for (a=1;a<8;a++)

                        DrawXY(bx+a,by+11);

            for (a=1;a<7;a++)

                        DrawXY(bx+a,by+12);

            for (a=1;a<14;a++)

                        DrawXY(bx+a,by+13);

            DrawXY(bx+10,by+10);

            DrawXY(bx+11,by+10);

            DrawXY(bx+11,by+11);

            DrawXY(bx+12,by+11);

            DrawXY(bx+12,by+12);

            DrawXY(bx+13,by+12);

}

 

void DrawQuestion(int bx, int by)

{

            unsigned char a;

 

            for (a=2;a<7;a++)

                        DrawXY(bx+a,by+0);

            for (a=1;a<8;a++)

            DrawXY(bx+a,by+1);

            DrawXY(bx+2,by+2);

            for (a=2;a<5;a++)

            {

                        DrawXY(bx+0,by+a);

                        DrawXY(bx+1,by+a);

                        DrawXY(bx+7,by+a);

                        DrawXY(bx+8,by+a);

            }

            DrawXY(bx+6,by+5);

            DrawXY(bx+7,by+5);

            DrawXY(bx+5,by+6);

            DrawXY(bx+6,by+6);

            for (a=7;a<11;a++)

            {

                        DrawXY(bx+4,by+a);

                        DrawXY(bx+5,by+a);

            }

            DrawXY(bx+4,by+13);

            DrawXY(bx+5,by+13);

            DrawXY(bx+4,by+14);

            DrawXY(bx+5,by+14);

}

 

void DrawWave(int bx, int by)

{

            DrawXY(bx+1, by+0);

            DrawXY(bx+4, by+0);

            DrawXY(bx+7, by+0);

            DrawXY(bx+10, by+0);

            DrawXY(bx+13, by+0);

 

            DrawXY(bx+0, by+1);

            DrawXY(bx+5, by+1);

            DrawXY(bx+6, by+1);

            DrawXY(bx+11, by+1);

            DrawXY(bx+12, by+1);

 

            DrawXY(bx+2, by+2);

            DrawXY(bx+3, by+2);

            DrawXY(bx+8, by+2);

            DrawXY(bx+9, by+2);

            DrawXY(bx+14, by+2);

 

            DrawXY(bx+1, by+3);

            DrawXY(bx+4, by+3);

            DrawXY(bx+7, by+3);

            DrawXY(bx+10, by+3);

            DrawXY(bx+13, by+3);

 

            DrawXY(bx+0, by+4);

            DrawXY(bx+5, by+4);

            DrawXY(bx+6, by+4);

            DrawXY(bx+11, by+4);

            DrawXY(bx+12, by+4);

 

            DrawXY(bx+2, by+5);

            DrawXY(bx+3, by+5);

            DrawXY(bx+8, by+5);

            DrawXY(bx+9, by+5);

            DrawXY(bx+14, by+5);

 

            DrawXY(bx+1, by+6);

            DrawXY(bx+4, by+6);

            DrawXY(bx+7, by+6);

            DrawXY(bx+10, by+6);

            DrawXY(bx+13, by+6);

 

            DrawXY(bx+0, by+7);

            DrawXY(bx+5, by+7);

            DrawXY(bx+6, by+7);

            DrawXY(bx+11, by+7);

            DrawXY(bx+12, by+7);

}

*/

void DrawPattern1(int bx, int by)

{

            unsigned char a;

 

            for (a=0;a<15;a++)

            {

                        DrawXY(bx+a,by+0);

                        DrawXY(bx+a,by+1);

                        DrawXY(bx+a,by+2);

                        DrawXY(bx+a,by+3);

            }

}

 

void DrawPattern2(int bx, int by)

{

            unsigned char a;

 

            for (a=0;a<15;a++)

            {

                        DrawXY(bx+1,by+a);

                        DrawXY(bx+3,by+a);

            }

}

 

void DrawPattern3(int bx, int by)

{

            DrawXY(bx+0,by+2);

            DrawXY(bx+0,by+5);

            DrawXY(bx+0,by+8);

            DrawXY(bx+0,by+11);

            DrawXY(bx+0,by+14);

            DrawXY(bx+1,by+1);

            DrawXY(bx+1,by+4);

            DrawXY(bx+1,by+7);

            DrawXY(bx+1,by+10);

            DrawXY(bx+1,by+13);

            DrawXY(bx+2,by+0);

            DrawXY(bx+2,by+3);

            DrawXY(bx+2,by+6);

            DrawXY(bx+2,by+9);

            DrawXY(bx+2,by+12);

            DrawXY(bx+3,by+2);

            DrawXY(bx+3,by+5);

            DrawXY(bx+3,by+8);

            DrawXY(bx+3,by+11);

            DrawXY(bx+3,by+14);

}

 

void DrawPattern4(int bx, int by)

{

            DrawXY(bx+2,by+2);

            DrawXY(bx+2,by+5);

            DrawXY(bx+2,by+8);

            DrawXY(bx+2,by+11);

            DrawXY(bx+2,by+14);

            DrawXY(bx+1,by+1);

            DrawXY(bx+1,by+4);

            DrawXY(bx+1,by+7);

            DrawXY(bx+1,by+10);

            DrawXY(bx+1,by+13);

            DrawXY(bx+0,by+0);

            DrawXY(bx+0,by+3);

            DrawXY(bx+0,by+6);

            DrawXY(bx+0,by+9);

            DrawXY(bx+0,by+12);

            DrawXY(bx+3,by+0);

            DrawXY(bx+3,by+3);

            DrawXY(bx+3,by+6);

            DrawXY(bx+3,by+9);

            DrawXY(bx+3,by+12);

}

 

void DrawPattern5(int bx, int by)

{

            unsigned char a;

 

            for (a=0;a<4;a++)

            {

                        DrawXY(bx+1,by+a);

                        DrawXY(bx+3,by+a);

                        DrawXY(bx+5,by+a);

                        DrawXY(bx+7,by+a);

                        DrawXY(bx+9,by+a);

                        DrawXY(bx+11,by+a);

                        DrawXY(bx+13,by+a);

            }

}

 

void DrawPattern6(int bx, int by)

{

            unsigned char a;

 

            for (a=0;a<15;a++)

                        DrawXY(bx+a,by+3);

}

 

void DrawPattern7(int bx, int by)

{

            DrawXY(bx+0,by+1);

            DrawXY(bx+0,by+4);

            DrawXY(bx+0,by+7);

            DrawXY(bx+0,by+10);

            DrawXY(bx+0,by+13);

            DrawXY(bx+1,by+2);

            DrawXY(bx+1,by+5);

            DrawXY(bx+1,by+8);

            DrawXY(bx+1,by+11);

            DrawXY(bx+1,by+14);

            DrawXY(bx+2,by+1);

            DrawXY(bx+2,by+4);

            DrawXY(bx+2,by+7);

            DrawXY(bx+2,by+10);

            DrawXY(bx+2,by+13);

            DrawXY(bx+3,by+2);

            DrawXY(bx+3,by+5);

            DrawXY(bx+3,by+8);

            DrawXY(bx+3,by+11);

            DrawXY(bx+3,by+14);

}

 

void DrawPattern8(int bx, int by)

{

            unsigned char a;

 

            for (a=0;a<15;a+2)

            {

                        DrawXY(bx+0,by+a);

                        DrawXY(bx+2,by+a);

            }

            for (a=1;a<14;a+2)

            {

                        DrawXY(bx+1,by+a);

                        DrawXY(bx+3,by+a);

            }

}

 

void DrawPattern3a(int bx, int by)

{

            DrawXY(bx+2,by+2);

            DrawXY(bx+2,by+5);

            DrawXY(bx+2,by+8);

            DrawXY(bx+2,by+11);

            DrawXY(bx+2,by+14);

            DrawXY(bx+1,by+1);

            DrawXY(bx+1,by+4);

            DrawXY(bx+1,by+7);

            DrawXY(bx+1,by+10);

            DrawXY(bx+1,by+13);

            DrawXY(bx+0,by+0);

            DrawXY(bx+0,by+3);

            DrawXY(bx+0,by+6);

            DrawXY(bx+0,by+9);

            DrawXY(bx+0,by+12);

}

 

void DrawPattern4a(int bx, int by)

{

            DrawXY(bx+0,by+2);

            DrawXY(bx+0,by+5);

            DrawXY(bx+0,by+8);

            DrawXY(bx+0,by+11);

            DrawXY(bx+0,by+14);

            DrawXY(bx+1,by+1);

            DrawXY(bx+1,by+4);

            DrawXY(bx+1,by+7);

            DrawXY(bx+1,by+10);

            DrawXY(bx+1,by+13);

            DrawXY(bx+2,by+0);

            DrawXY(bx+2,by+3);

            DrawXY(bx+2,by+6);

            DrawXY(bx+2,by+9);

            DrawXY(bx+2,by+12);

}