//Chan Lee
 //Dennis Majewski
 //ECE 476 Group 7 Monday Night
 //Electronic Dartboard with Video Display Code 
   //D.5 is sync:1000 ohm + diode to 75 ohm resistor
   //D.6 is video:330 ohm + diode to 75 ohm resistor 
#pragma regalloc- //I allocate the registers myself
   #pragma optsize- //optimize for speed
   
   #include <Mega32.h> 
   #include <stdio.h>
   #include <stdlib.h> 
   #include <math.h> 
   #include <delay.h> 
//cycles = 63.625 * 16 Note NTSC is 63.55 
   //but this line duration makes each frame exactly 1/60 sec
   //which is nice for keeping a realtime clock 
   #define lineTime 1018 
   #define begin {
   #define end }
   #define ScreenTop 30 //Screen top defination
   #define ScreenBot 230 //Screen bottom defination
   #define T0reload 256-60 //Define interrupt trigger time
   #define polldelay 9 //Control to charge pins for dart read
#define no 0 //Game play states, and dartboard read states
#define yes 1
   #define wait 2
   #define top 1
   #define bottom 2
   #define bull 3
#define error 4
   #define notpushed 0 //Push button states
   #define pushed 1 
//Game Play States
   #define Menu_Draw 1 //Draw Menu
   #define Menu 2 //Track button push on menu
   #define Player_Draw 3 //Draw Player menu
   #define Player 4 // Track button pushes on player menu
   #define Game301_Draw 5 // Draw 301 Game
   #define Game301_Play 6 // State for playing 301 game
   #define Cricket_Draw 7 // Draw Cricket Game
   #define CRCK_play 8 // State for playing Cricket Game
   #define Erase1 10 //Erase entire Screen
   #define Erase2 11 //Draw vertical lines
   #define Erase3 12 //Draw Horizontal Lines
   #define Grade_Draw 13 //Draw Grade Game 
   #define Grade_Play 14 //State for Grade Game Play
   #define Drink_Draw 15 //Draw Drink Game 
   #define Drink_Play 16 //State for Drink Game PLay
   #define Erase0 17 //Erase Game Characters without flicker
   #define NewPlayer 1 //State each game at NewPlayer
   #define ThreeHit 2
   #define ThreeOut 3
#define NotBounced 0
   #define Bounced 1 //Debounce States
   #define BustBounced 2

//NOTE that v1 to v8 and i must be in registers! 
   register char v1 @4; 
   register char v2 @5;
   register char v3 @6;
   register char v4 @7;
   register char v5 @8;
   register char v6 @9;
register char v7 @10;
   register char v8 @11; 
   register int i @12;
#pragma regalloc+ 
char syncON, syncOFF; 
   int LineCount;
   int debcounter=0;
   char DartHit=no;

char arrow_top=0;
   char arrow_bottom=0;
char game=1; //Initialize menu variables
   char players=1;
int q=0;
char State_Game301=0; //Game Variables
   char hit_pos=0;
   char butnum=0; 
   char winner1=0; //Winner State
   char State=Menu; //Initialize state machine to Menu
   char screen[1600], t, ts[10]; 
//Game menu definitions for video generation
char menu_select[]="SELECT[GAME";
   char menu_game1[]="301";
   char menu_game2[]="CRICKET";
   char menu_game3[]="DRINKING[GAME";
   char menu_game4[]="GRADE[GAME";
   char arrow_small[]="[]"; //Draw arrow
   char arrow_small_erase[]="[[";
   //Player menu definitions for video generation
   char player_select[]="SELECT[NUMBER[OF[PLAYERS";
   char player_1[]="1[PLAYER";
   char player_2[]="2[PLAYERS";
   char player_3[]="3[PLAYERS[";
   char player_4[]="4[PLAYERS["; 
   //Player number generation
   char p1[]="P1";
   char p2[]="P2";
   char p3[]="P3";
   char p4[]="P4";
//Drinking Game Definations
   char minus[]="?";
   char plus[]="@";
   char aaa[]="A";
   char bbb[]="B"; 
   char ccc[]="C";
   char ddd[]="D"; 
   char fff[]="F"; 
   //Round Defination
   char RND[]="RND"; 
   
   //Message function generation
   char bust[] ="BUST[[";
   char bullz[] ="BULL[["; 
   char bu[] ="BU";
   char lz[] ="LL";
   char dbullz[]="DBULL[";
   char doub[] ="DOUBLE";
   char triple[]="TRIPLE";
   char winner[]="WINNER";
   char hit[]="HIT";
   //Erase defination
   char hit_erase[]="[[[[[[";
   //Cricket symbol defination
   char first_hit[]="^";
   char second_hit[]="X";
   char third_hit[]=":";
   //Drinking Game symbol
   char drink_sy[]="Z";
char symbol_pos =0;
   int disp_total=0;
   int total[4]={0,0,0,0};
   char player_number=0;
//---------------------------------
   // Game Variables
char dart=0;
   char bounce=NotBounced;
   int darttemp=0; 
   int darttemp2=0;
   int disptemp=0; 
   //Cricket
   char pl1=0; 
   char pl2=0;
   char pl3=0;
   char pl4=0;
   char ps1=0; 
   char totalcr=0;
   char win1 =0; 
   char round=0;
   char playz1[7]={2,2,2,2,2,2,2}; 
   char playz2[7]={2,2,2,2,2,2,2};
   char playz3[7]={2,2,2,2,2,2,2};
   char playz4[7]={2,2,2,2,2,2,2}; 
   char posit=0;
   //---------------------------------
   //Dart region detection and scoring variables
char Button=notpushed; 
   char arrow_pos=0;
   char Update=no;
   char Hemisphere=top;
   char temp=0;
   char dartvalue=0;
   char dartregion=0;
   char dartscore=0;
   char pollcounter=0;
   char current_pollresult=0;
   char previous_pollresult=0;
   char previous_pollcounter=0;
   char polloutput[10]={0b01111111,0b10111111,0b11011111,0b11101111,0b11110111,0b11111011,0b11111101,0b11111110,0b00001101,0b00001110};
   //char pollresult[10];
   flash char top_decode[11]={0, 4, 13, 10, 6, 5, 20, 9, 12, 18, 1};
   flash char bottom_decode[11]={0, 3, 17, 15, 2, 8, 16, 14, 11, 19, 7};
   flash char bull_decode[11]={0, 0, 0, 0, 0, 0, 0, 50, 25, 0, 0};
//Point plot lookup table
   flash char pos[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
//define some character bitmaps
   //5x7 characters
   flash char bitmap[1][7]={ 
   //0
   0b01110000,
   0b10001000,
   0b10011000,
   0b10101000,
   0b11001000,
   0b10001000,
   0b01110000
 };
//================================ 
   //3x5 font numbers, then letters
   //packed two per definition for fast 
   //copy to the screen at x-position divisible by 4
   flash char smallbitmap[44][5]={ 
   //0
   0b11101110,
   0b10101010,
   0b10101010,
   0b10101010,
   0b11101110,
   //1
   0b01000100,
   0b11001100,
   0b01000100,
   0b01000100,
   0b11101110,
   //2
   0b11101110,
   0b00100010,
   0b11101110,
   0b10001000,
   0b11101110,
   //3
   0b11101110,
   0b00100010,
   0b11101110,
   0b00100010,
   0b11101110,
   //4
   0b10101010,
   0b10101010,
   0b11101110,
   0b00100010,
   0b00100010,
   //5
   0b11101110,
   0b10001000,
   0b11101110,
   0b00100010,
   0b11101110,
   //6
   0b11001100,
   0b10001000,
   0b11101110,
   0b10101010,
   0b11101110,
   //7
   0b11101110,
   0b00100010,
   0b01000100,
   0b10001000,
   0b10001000,
   //8
   0b11101110,
   0b10101010,
   0b11101110,
   0b10101010,
   0b11101110,
   //9
   0b11101110,
   0b10101010,
   0b11101110,
   0b00100010,
   0b01100110,
   //third hit(input is ":")
   0b01000100,
   0b10101010,
   0b10011001,
   0b10101010,
   0b01000100,
   //- (input is ?)
   0b00000000,
   0b00000000,
   0b11101110,
   0b00000000,
   0b00000000,
   //+ (input is @)
   0b00000000,
   0b01000100,
   0b11101110,
   0b01000100,
   0b00000000,
   //A
   0b11101110,
   0b10101010,
   0b11101110,
   0b10101010,
   0b10101010,
   //B
   0b11001100,
   0b10101010,
   0b11101110,
   0b10101010,
   0b11001100,
   //C
   0b11101110,
   0b10001000,
   0b10001000,
   0b10001000,
   0b11101110,
   //D
   0b11001100,
   0b10101010,
   0b10101010,
   0b10101010,
   0b11001100,
   //E
   0b11101110,
   0b10001000,
   0b11101110,
   0b10001000,
   0b11101110,
   //F
   0b11101110,
   0b10001000,
   0b11101110,
   0b10001000,
   0b10001000,
   //G
   0b11101110,
   0b10001000,
   0b10001000,
   0b10101010,
   0b11101110,
   //H
   0b10101010,
   0b10101010,
   0b11101110,
   0b10101010,
   0b10101010,
   //I
   0b11101110,
   0b01000100,
   0b01000100,
   0b01000100,
   0b11101110,
   //J
   0b00100010,
   0b00100010,
   0b00100010,
   0b10101010,
   0b11101110,
   //K
   0b10001000,
   0b10101010,
   0b11001100,
   0b11001100,
   0b10101010,
   //L
   0b10001000,
   0b10001000,
   0b10001000,
   0b10001000,
   0b11101110,
   //M
   0b10101010,
   0b11101110,
   0b11101110,
   0b10101010,
   0b10101010,
   //N
   0b00000000,
   0b11001100,
   0b10101010,
   0b10101010,
   0b10101010,
   //O
   0b01000100,
   0b10101010,
   0b10101010,
   0b10101010,
   0b01000100,
   //P
   0b11101110,
   0b10101010,
   0b11101110,
   0b10001000,
   0b10001000,
   //Q
   0b01000100,
   0b10101010,
   0b10101010,
   0b11101110,
   0b01100110,
   //R
   0b11101110,
   0b10101010,
   0b11001100,
   0b11101110,
   0b10101010,
   //S
   0b11101110,
   0b10001000,
   0b11101110,
   0b00100010,
   0b11101110,
   //T
   0b11101110,
   0b01000100,
   0b01000100,
   0b01000100,
   0b01000100, 
   //U
   0b10101010,
   0b10101010,
   0b10101010,
   0b10101010,
   0b11101110, 
   //V
   0b10101010,
   0b10101010,
   0b10101010,
   0b10101010,
   0b01000100,
   //W
   0b10101010,
   0b10101010,
   0b11101110,
   0b11101110,
   0b10101010,
   //X
   0b00000000,
   0b10101010,
   0b01000100,
   0b01000100,
   0b10101010,
   //Y
   0b10101010,
   0b10101010,
   0b01000100,
   0b01000100,
   0b01000100,
   //Drink Game symbol (input is Z)
   0b11111111,
   0b10011001,
   0b10011001,
   0b01100110,
   0b00000000,
   //blank(input is "[")
   0b00000000,
   0b00000000,
   0b00000000,
   0b00000000,
   0b00000000,
   //not used
   0b00000000,
   0b00000000,
   0b00000000,
   0b00000000,
   0b00000000,
   //arrow(input is "]")
   0b00001000,
   0b00001100,
   0b00001110,
   0b00001100,
   0b00001000,
   //First Hit "/"(input is "^")
   0b00010001,
   0b00100010,
   0b01000100,
   0b10001000,
   0b00000000,
   //blank(input is "blank")
   0b00000000,
   0b00000000,
   0b00000000,
   0b00000000,
   0b00000000
   };
   
   //==================================
   //This is the sync generator and raster generator. It MUST be entered from 
   //sleep mode to get accurate timing of the sync pulses
   #pragma warn-
   interrupt [TIM1_COMPA] void t1_cmpA(void) 
   begin 
   //start the Horizontal sync pulse 
   PORTD = syncON; 
   //count timer 0 at 1/usec
   TCNT0=0;
   //update the curent scanline number
   LineCount ++ ; 
   //begin inverted (Vertical) synch after line 247
   if (LineCount==248)
   begin 
   syncON = 0b00100000;
   syncOFF = 0;
   end
   //back to regular sync after line 250
   if (LineCount==251) 
   begin
   syncON = 0;
   syncOFF = 0b00100000;
   end 
   //start new frame after line 262
   if (LineCount==263) 
   begin
   LineCount = 1;
   end 
   
   delay_us(2); //adjust to make 5 us pulses
   //end sync pulse
   PORTD = syncOFF; 
 
   //********************************************************* 
   //first dartboard polling here!!! 
   //delay_us(4);
   PORTB=0xff;
   PORTC=0x0f;
   
   if(pollcounter<8)
   {
   PORTB=polloutput[pollcounter];
   }
 else
   {
   PORTC=polloutput[pollcounter];
   }
 #asm 
   __DELAY_USB 9 ;5 is 1us delay, 11 is 2us, 21 is 4us, etc.
   #endasm
 
 previous_pollresult=~PINA;
   previous_pollcounter=pollcounter;
   
   pollcounter++;
   
   PORTB=0xff;
   PORTC=0x0f;
   //*********************************************************
   
   if (LineCount<ScreenBot && LineCount>=ScreenTop) 
   begin 
   
   //compute byte index for beginning of the next line
   //left-shift 4 would be individual lines
   // <<3 means line-double the pixels 
   //The 0xfff8 truncates the odd line bit
   //i=(LineCount-ScreenTop)<<3 & 0xfff8; //
   
   #asm
   push r16
   lds r12, _LineCount
   lds r13, _Linecount+1
   ldi r16, 30
   sub r12, r16 
   ldi r16,0
   sbc r13, r16 
   lsl r12
   rol r13
   lsl r12
   rol r13
   lsl r12 
   rol r13
   mov r16,r12
   andi r16,0xf0
   mov r12,r16
   pop r16 
   #endasm
   
   //load 16 registers with screen info
   #asm
   push r14
   push r15
   push r16
   push r17
   push r18 
   push r19 
   push r26
   push r27
   
   ldi r26,low(_screen) ;base address of screen
   ldi r27,high(_screen) 
   add r26,r12 ;offset into screen (add i)
   adc r27,r13
   ld r4,x+ ;load 16 registers and inc pointer
   ld r5,x+
   ld r6,x+ 
   ld r7,x+
   ld r8,x+ 
   ld r9,x+
   ld r10,x+ 
   ld r11,x+
   ld r12,x+ 
   ld r13,x+
   ld r14,x+ 
   ld r15,x+
   ld r16,x+ 
   ld r17,x+ 
   ld r18,x+
   ld r19,x 
   
   pop r27
   pop r26
   #endasm 
 delay_us(4); //adjust to center image on screen
   
   
   //blast 16 bytes to the screen 
   #asm
   ;but first a macro to make the code shorter 
   ;the macro takes a register number as a parameter
   ;and dumps its bits serially to portD.6 
   ;the nop can be eliminated to make the display narrower
   .macro videobits ;regnum
   BST @0,7
   IN R30,0x12
   BLD R30,6
   
   ;nop
   OUT 0x12,R30 
   
   BST @0,6
   IN R30,0x12
   BLD R30,6
   ;nop
   OUT 0x12,R30 
   
   BST @0,5
   IN R30,0x12
   BLD R30,6 
   ;nop
   OUT 0x12,R30 
   
   BST @0,4
   IN R30,0x12
   BLD R30,6
   ;nop
   OUT 0x12,R30 
   
   BST @0,3
   IN R30,0x12
   BLD R30,6
   ;nop
   OUT 0x12,R30 
   
   BST @0,2
   IN R30,0x12
   BLD R30,6
   ;nop
   OUT 0x12,R30 
   
   BST @0,1
   IN R30,0x12
   BLD R30,6 
   ;nop
   OUT 0x12,R30 
   
   BST @0,0
   IN R30,0x12
   BLD R30,6
   ;nop
   OUT 0x12,R30 
   .endm 
   
   videobits r4 ;video line -- byte 1
   videobits r5 ;byte 2 
   videobits r6 ;byte 3
   videobits r7 ;byte 4
   videobits r8 ;byte 5
   videobits r9 ;byte 6
   videobits r10 ;byte 7
   videobits r11 ;byte 8 
   videobits r12 ;byte 9
   videobits r13 ;byte 10 
   videobits r14 ;byte 11
   videobits r15 ;byte 12
   videobits r16 ;byte 13
   videobits r17 ;byte 14
   videobits r18 ;byte 15
   videobits r19 ;byte 16
   clt ;clear video after the last pixel on the line
 IN R30,0x12
   BLD R30,6
   OUT 0x12,R30
   
   pop r19
   pop r18
   pop r17 
   pop r16 
   pop r15
   pop r14
   #endasm
   end
 //*********************************************************
   //second dartboard polling here!!! 
   //delay_us(4);
   
   PORTB=0xff;
   PORTC=0x0f;
   
   if(pollcounter<8)
   {
   PORTB=polloutput[pollcounter];
   }
   else
   {
   PORTC=polloutput[pollcounter];
   }
   
   #asm 
   __DELAY_USB 9 ;5 is 1us delay, 11 is 2us, 21 is 4us, etc.
   #endasm
 current_pollresult=~PINA;
   
   if(DartHit==no)
   {
   if(previous_pollresult!=0)
   {
   DartHit=yes;
   }
   else if(current_pollresult!=0)
   {
   DartHit=yes;
   }
   }
   else if(DartHit==yes)
   {
   if(previous_pollresult!=0)
   {
   dartvalue=previous_pollresult;
   dartregion=previous_pollcounter+1;
   }
   else if(current_pollresult!=0)
   {
   dartvalue=current_pollresult;
   dartregion=pollcounter+1;
   }
   }
 
   
   if(pollcounter==9) //If poll all 10 input lines, go back to input line 0
   {
   pollcounter=0;
   
   if(DartHit==yes)
   {
   if(debcounter==2)
   {
   debcounter=0;
   DartHit=wait;
   Update=yes;
   }
   else
   {
   dartvalue=0;
   dartregion=0;
   debcounter++;
   }
   }
   else if(DartHit==wait)
   {
   if(debcounter==2000)
   {
   debcounter=0;
   DartHit=no;
   }
   else
   {
   debcounter++;
   }
   }
   else
   {
   debcounter=0;
   }
   }
   else
   {
   pollcounter++;
   }
   
   PORTB=0xff;
   PORTC=0x0f;
   
   //********************************************************* 
   end 
   #pragma warn+
//==================================
   //plot one point 
   //at x,y with color 1=white 0=black 2=invert 
   #pragma warn-
   void video_pt(char x, char y, char c)
   begin 
   
   #asm
   ; i=(x>>3) + ((int)y<<4) ; the byte with the pixel in it
 push r16
   ldd r30,y+2 ;get x
   lsr r30
   lsr r30
   lsr r30 ;divide x by 8
   ldd r12,y+1 ;get y
   lsl r12 ;mult y by 16
   clr r13
   lsl r12
   rol r13
   lsl r12
   rol r13
   lsl r12
   rol r13
   add r12, r30 ;add in x/8
   
   ;v2 = screen[i]; r5
   ;v3 = pos[x & 7]; r6
   ;v4 = c r7
   ldi r30,low(_screen)
   ldi r31,high(_screen)
   add r30, r12
   adc r31, r13
   ld r5,Z ;get screen byte
   ldd r26,y+2 ;get x
   ldi r27,0
   andi r26,0x07 ;form x & 7 
   ldi r30,low(_pos*2) 
   ldi r31,high(_pos*2)
   add r30,r26
   adc r31,r27
   lpm r6,Z
   ld r16,y ;get c 
   
   ;if (v4==1) screen[i] = v2 | v3 ; 
   ;if (v4==0) screen[i] = v2 & ~v3; 
   ;if (v4==2) screen[i] = v2 ^ v3 ; 
   
   cpi r16,1
   brne tst0
   or r5,r6
   tst0:
   cpi r16,0
   brne tst2 
   com r6
   and r5,r6
   tst2:
   cpi r16,2
 brne writescrn
   eor r5,r6
   writescrn:
   ldi r30,low(_screen)
   ldi r31,high(_screen)
   add r30, r12
   adc r31, r13
   st Z, r5 ;write the byte back to the screen
   
   pop r16
   #endasm
   
   end
   #pragma warn+
//==================================
   // put a big character on the screen
   // c is index into bitmap
   void video_putchar(char x, char y, char c) 
   begin 
   v7 = x;
   for (v6=0;v6<7;v6++) 
   begin
   v1 = bitmap[c][v6]; 
   v8 = y+v6;
   video_pt(v7, v8, (v1 & 0x80)==0x80); 
   video_pt(v7+1, v8, (v1 & 0x40)==0x40); 
   video_pt(v7+2, v8, (v1 & 0x20)==0x20);
   video_pt(v7+3, v8, (v1 & 0x10)==0x10);
   video_pt(v7+4, v8, (v1 & 0x08)==0x08);
   end
   end
//==================================
 
   //==================================
   // put a small character on the screen
   // x-cood must be on divisible by 4 
   // c is index into bitmap
   void video_smallchar(char x, char y, char c) 
   begin 
   char mask;
   i=((int)x>>3) + ((int)y<<4) ;
   if (x == (x & 0xf8)) mask = 0x0f; //f8
   else mask = 0xf0;
   
   screen[i] = (screen[i] & mask) | (smallbitmap[c][0] & ~mask); 
   screen[i+16] = (screen[i+16] & mask) | (smallbitmap[c][1] & ~mask);
   screen[i+32] = (screen[i+32] & mask) | (smallbitmap[c][2] & ~mask);
   screen[i+48] = (screen[i+48] & mask) | (smallbitmap[c][3] & ~mask);
   screen[i+64] = (screen[i+64] & mask) | (smallbitmap[c][4] & ~mask);    
   end 
//==================================
   // put a string of small characters on the screen
   // x-cood must be on divisible by 4 
   void video_putsmalls(char x, char y, char *str)
   begin
   char i ;
   for (i=0; str[i]!=0; i++)
   begin 
   if (str[i]>=0x30 && str[i]<=0x3a) 
   video_smallchar(x,y,str[i]-0x30);
   else video_smallchar(x,y,str[i]-0x40+12);
   x = x+4; 
   end
   end
   
   //==================================
   //plot a line 
   //at x1,y1 to x2,y2 with color 1=white 0=black 2=invert 
   //NOTE: this function requires signed chars 
   //Code is from David Rodgers,
   //"Procedural Elements of Computer Graphics",1985
   void video_line(char x1, char y1, char x2, char y2, char c)
   begin 
   int e;
   signed char dx,dy,j, temp;
   signed char s1,s2, xchange;
   signed char x,y;
   
   x = x1;
   y = y1;
   dx = cabs(x2-x1);
   dy = cabs(y2-y1);
   s1 = csign(x2-x1);
   s2 = csign(y2-y1);
   xchange = 0; 
   if (dy>dx)
   begin
   temp = dx;
   dx = dy;
   dy = temp;
   xchange = 1;
   end 
   e = ((int)dy<<1) - dx; 
   for (j=0; j<=dx; j++)
   begin
   video_pt(x,y,c) ; 
   if (e>=0)
   begin
   if (xchange==1) x = x + s1;
   else y = y + s2;
   e = e - ((int)dx<<1);
   end
   if (xchange==1) y = y + s2;
   else x = x + s1;
   e = e + ((int)dy<<1);
   end
   end
//==================================
//==================================
   //Erase Screen Completely
void erase(void)
   begin
   for(q=0;q<1600;q++)
   {
   screen[q]=0;
   }
   end
   //==================================
   //Drawing Menu Screen
void draw_menu(void)
   begin
   video_putsmalls(40,3,menu_select);
   
   video_putsmalls(36,30,menu_game1); //Draw menu game names
   video_putsmalls(36,40,menu_game2);
   video_putsmalls(36,50,menu_game3);
   video_putsmalls(36,60,menu_game4);
   
   arrow_top=30;
   arrow_bottom=60;
   
   game=1;
   
   arrow_pos=30;
   video_putsmalls(24,arrow_pos,arrow_small);
end
   //==================================
   //Drawing Player Select Screen
void draw_player(void)
   begin
   video_putsmalls(16,3,player_select);
   
   video_putsmalls(36,30,player_1); //Draw player selection choices
   video_putsmalls(36,40,player_2);
   video_putsmalls(36,50,player_3);
   video_putsmalls(76,50,hit_erase);
   video_putsmalls(36,60,player_4);
   
   video_putsmalls(24,arrow_pos,arrow_small_erase);
   
   arrow_top=30;
   arrow_bottom=60;
   
   players=1;
   
   arrow_pos=30;
   video_putsmalls(24,arrow_pos,arrow_small);
end
   //==================================
   //Draw 301 Game
void draw_game301(void)
   begin
   video_putsmalls(56,3,menu_game1);
   //Define total score variables
   disp_total=301;
   total[0]=301;
   total[1]=301;
   total[2]=301;
   total[3]=301;
   hit_pos=30;
   player_number=0;
   round=1;
   State_Game301=NewPlayer;
   //Set up Players according to number of players entered
   if(players>=1)
   {
   video_putsmalls(20,30,p1);
   sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(36,30,ts);
   }
   if(players>=2)
   {
   video_putsmalls(20,40,p2);
   sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(36,40,ts);
   }
   if(players>=3)
   {
   video_putsmalls(20,50,p3);
   sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(36,50,ts);
   }
   if(players>=4)
   {
   video_putsmalls(20,60,p4);
   sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(36,60,ts);
   }
   //Define arrow position
   arrow_pos=30;
   arrow_top=30;
   arrow_bottom=50;
 video_putsmalls(8,arrow_pos,arrow_small);
 //Initialize Game Variables
   dart=1;
   bounce=Bounced;
   hit_pos=30;
   darttemp=0;
   disptemp=0;
   Update=no;
end 
   //-----------------------------------------------------------------------------------
   void draw_gameDrink(void)
   begin
   video_putsmalls(36,3,menu_game3);
   player_number=0; 
   disp_total=0; 
   //Set up Players according to muber of players entered
   if(players>=1)
   {
   video_putsmalls(8,30,p1);
   sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(20,30,ts);
   
   }
   if(players>=2)
   {
   video_putsmalls(8,40,p2);
   sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(20,40,ts);
   
   }
   if(players>=3)
   {
   video_putsmalls(8,50,p3);
   sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(20,50,ts);
   
   }
   if(players>=4)
   {
   video_putsmalls(8,60,p4);
   sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(20,60,ts);
   
   }
   //Draw Round Numbers
   sprintf(ts,"%01d",(int) 1); //Round 1
   video_putsmalls(40,22,ts); 
   
   
   sprintf(ts,"%01d",(int) 2); //2
   video_putsmalls(48,22,ts); 
   
   
   sprintf(ts,"%01d",(int) 3); //3
   video_putsmalls(56,22,ts); 
   
   
   sprintf(ts,"%01d",(int) 4); //4
   video_putsmalls(64,22,ts); 
 
 
   sprintf(ts,"%01d",(int) 5); //5
   video_putsmalls(72,22,ts); 
   
   
   
   sprintf(ts,"%01d",(int) 6); //6
   video_putsmalls(80,22,ts); 
   
   sprintf(ts,"%01d",(int) 7); //7
   video_putsmalls(88,22,ts);
   
   sprintf(ts,"%01d",(int) 8); //8
   video_putsmalls(96,22,ts); 
   
   sprintf(ts,"%01d",(int) 9); //9
   video_putsmalls(104,22,ts);
   
   //Initialize Game Variables
   disp_total=40; 
   State_Game301=NewPlayer;
   arrow_pos=30;
   arrow_top=30;
   arrow_bottom=50;
   video_putsmalls(2,arrow_pos,arrow_small);
   temp=0; 
   total[0]=0;
   total[1]=0;
   total[2]=0;
   total[3]=0;
   
   dart=1; //Initialize game variables
   bounce=Bounced;
   round=0;
   hit_pos=70;
   darttemp=0;
   disptemp=0;
   Update=no; 
   darttemp2=0;
   end 
   //-------------------------------------------------------------------------------
   void draw_gameGrade(void)
   begin
   
   //Draw game menu
   video_putsmalls(44,3,menu_game4);
   player_number=0; 
   disp_total=0; 
   video_putsmalls(40,20,menu_game4);
   video_putsmalls(60,20,hit_erase);
   //Set up Players according to muber of players entered
   if(players>=1)
   {
   video_putsmalls(8,30,p1);
   sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(20,30,ts);
   
   }
   if(players>=2)
   {
   video_putsmalls(8,40,p2);
   sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(20,40,ts);
   
   }
   if(players>=3)
   {
   video_putsmalls(8,50,p3);
   sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(20,50,ts);
   
   }
   if(players>=4)
   {
   video_putsmalls(8,60,p4);
   sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(20,60,ts);
   
   }
   
   
   
   //Initialize game variables
   State_Game301=NewPlayer;
   arrow_pos=30;
   arrow_top=30;
   arrow_bottom=50;
   video_putsmalls(2,arrow_pos,arrow_small);
 dart=1;
   bounce=Bounced;
   hit_pos=50;
   darttemp=0;
   disptemp=0;
   Update=no; 
   darttemp2=0; 
   total[0]=0;
   total[1]=0;
   total[2]=0;
   total[3]=0;
end
   //-------------------------------------------------------
   void draw_gameCRKT(void)
   begin 
   video_putsmalls(48,3,menu_game2);
   disp_total=0;
   temp=0;
   round=1;
   player_number=0; 
   
   //Display cricket regions to hit
   
   sprintf(ts,"%01d",(int) 5); //15
   video_putsmalls(40,22,ts); 
   
   sprintf(ts,"%01d",(int) 1);
   video_putsmalls(38,18,ts); 
   
   sprintf(ts,"%01d",(int) 6); //16
   video_putsmalls(52,22,ts); 
   
   sprintf(ts,"%01d",(int) 1);
   video_putsmalls(48,18,ts); 
   
   sprintf(ts,"%01d",(int) 7); //17
   video_putsmalls(64,22,ts); 
   
   sprintf(ts,"%01d",(int) 1);
   video_putsmalls(58,18,ts); 
   
   sprintf(ts,"%01d",(int) 8); //18
   video_putsmalls(76,22,ts); 
   
   sprintf(ts,"%01d",(int) 1);
   video_putsmalls(72,18,ts);
   
   sprintf(ts,"%01d",(int) 9); //19
   video_putsmalls(88,22,ts); 
   
   sprintf(ts,"%01d",(int) 1);
   video_putsmalls(84,18,ts); 
   
   sprintf(ts,"%01d",(int) 0); //20
   video_putsmalls(98,22,ts); 
   
   sprintf(ts,"%01d",(int) 2);
   video_putsmalls(96,18,ts); 
   
   //Bull
   video_putsmalls(108,18,bu);
   video_putsmalls(116,22,lz); 
   
   
   //Set up Players according to muber of players entered 
   
   if(players>=1)
   {
   video_putsmalls(8,30,p1);
   sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(20,30,ts); 
   
   playz1[0]=5;
   playz1[1]=5;
   playz1[2]=5;
   playz1[3]=5;
   playz1[4]=5;
   playz1[5]=5;
   playz1[6]=5;
 }
   if(players>=2)
   {
   video_putsmalls(8,40,p2);
   sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(20,40,ts); 
   playz2[0]=5;
   playz2[1]=5;
   playz2[2]=5;
   playz2[3]=5;
   playz2[4]=5;
   playz2[5]=5;
   playz2[6]=5;
 }
   if(players>=3)
   {
   video_putsmalls(8,50,p3);
 sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(20,50,ts);
   playz3[0]=5;
   playz3[1]=5;
   playz3[2]=5;
   playz3[3]=5;
   playz3[4]=5;
   playz3[5]=5;
   playz3[6]=5;
   
   }
   if(players>=4)
   {
   video_putsmalls(8,60,p4);
   sprintf(ts,"%03d",(int) disp_total);
   video_putsmalls(20,60,ts); 
   playz4[0]=5;
   playz4[1]=5;
   playz4[2]=5;
   playz4[3]=5;
   playz4[4]=5;
   playz4[5]=5;
   playz4[6]=5;
 }
   //Initialize Game Variables
   arrow_pos=30; //Arrow Initialize
   arrow_top=30;
   arrow_bottom=50;
   video_putsmalls(2,arrow_pos,arrow_small);
 total[0]=0;
   total[1]=0;
   total[2]=0;
   total[3]=0;
   dart=1;
   bounce=Bounced;
   hit_pos=70;
   darttemp=0;
   disptemp=0;
   Update=no;
end
//==================================
   //Initial Conditions
void init(void)
   begin
   
   //side lines 
   #define width 126
   video_line(0,0,0,99,1);
   video_line(width,0,width,99,1);
   
   //top line & bottom lines
   video_line(0,0,width,0,1);
   video_line(0,99,width,99,1);
   video_line(0,11,width,11,1);
   
   State=Menu_Draw;
   State_Game301=0;
   
   end
   //==================================
   //Button Polling for 4 push buttons
void button_polling(void)
   begin
   if(Button==notpushed)
   {
   if(PINC.4==1)
   {
   Button=pushed; //De-bounce push button
   butnum=1;
   }
   else if(PINC.5==1)
   {
   Button=pushed;
   butnum=2;
   }
   else if(PINC.6==1)
   {
   Button=pushed;
   butnum=3;
   }
   else if(PINC.7==1)
   {
   Button=pushed;
   butnum=4;
   }
   }
   else
   {
   if(PINC==0x0f)
   {
   Button=notpushed;
   butnum=0;
   }
   }
   end

//==================================
   //Dart Hit Update
void dart_hit_update(void)
   begin
   if(Update==yes)
   {
   Update=no;
   
   switch(dartvalue)
   { //Input Line 0 hit, 1st double region
   case 1:
   dartvalue=2;
   Hemisphere=top;
   dartregion=top_decode[dartregion];
   break;
   case 2: //Input Line 1 hit, 1st triple region
   dartvalue=3;
   Hemisphere=top;
   dartregion=top_decode[dartregion];
   break;
   case 8:
   dartvalue=1; //Input Line 3 hit 1st single region
   Hemisphere=top;
   dartregion=top_decode[dartregion];
   break;
 case 16: //Input Line 4 hit 2nd double region
   dartvalue=2;
   Hemisphere=bottom;
   dartregion=bottom_decode[dartregion];
   break;
   case 128:
   dartvalue=3; //Input Line 6 hit 2nd triple region
   Hemisphere=bottom;
   dartregion=bottom_decode[dartregion];
   break;
   case 32:
   dartvalue=1; //Input Line 5 hit 2nd single region
   Hemisphere=bottom;
   dartregion=bottom_decode[dartregion];
   break;
   
   case 4: //Input Line 2 hit Bull region
   dartvalue=1;
   Hemisphere=bull;
   dartregion=bull_decode[dartregion];
   break;
   
   default: //Default to output 0
   dartvalue=0;
   Hemisphere=error;
   dartregion=0;
   break;
   }
   
   switch(dartvalue)
   {
   case 1:
   dartscore=dartregion; //Single hit
   break;
   case 2:
   dartscore=dartregion+dartregion; //Double Region hit
   break;
   case 3:
   dartscore=dartregion+dartregion+dartregion; 
   //Triple Region Hit
   break;
   
   default:
   dartscore=0; //If unable to read hit output 0
   break;
   }
 
 
   }
   end
   //==================================
   //Message Check
void message(void)
   begin
 if(dartvalue ==2) video_putsmalls(40,80,doub); //Display Double hit
   if(dartvalue ==3) video_putsmalls(40,80,triple); //Display triple hit
   if(dartvalue ==1) video_putsmalls(40,80,hit_erase);//If single hit erase
   if(dartscore ==25) video_putsmalls(40,80,bullz); 
   //Display Bulls eye hit
   if(dartscore ==50) video_putsmalls(40,80,dbullz); 
   //Display Double Bullseye hit
   
   end 
   //=========================================================================================================
   void player1check(void) //Cricket function for game play
   begin
   if(pl1==2) 
   //Already Closed Out, check to see if other players closed out, if not add dart    hit
   {
   
   if(pl2>2) 
   {
   totalcr= totalcr+(dartregion*dartvalue); 
   }
   else if(pl3>2) 
   {totalcr=totalcr+(dartregion*dartvalue); 
   }
   else if(pl4>2) 
   {totalcr=totalcr+dartregion*dartvalue; 
   } 
   else
   {
   
   } 
   
   } 
   else if(pl1>2)
   {
   
   if(dartregion==50) //Special case for double bull
   {
   pl1=(int) pl1-2;
   }
   else
   {
   pl1=pl1-dartvalue;
   }
   
   } 
   
   
   
   if(pl1==4) video_putsmalls(symbol_pos,ps1,first_hit); //Display 1 hit
   else if(pl1==3) video_putsmalls(symbol_pos,ps1,second_hit); //Display 2 hit
   else if(pl1==2) video_putsmalls(symbol_pos,ps1,third_hit); //Display 3 hit
   else if(pl1==1) //One Score
   {
   if(pl2>2) //Score if other players have not closed out
   { 
   if(dartregion==50) totalcr=totalcr+25;
   else totalcr=totalcr+dartregion; 
   } 
   else if(pl3>2)
   {
   if(dartregion==50) totalcr=totalcr+25;
   else totalcr=totalcr+dartregion;
   }
   else if(pl4>2) 
   { 
   if(dartregion==50) totalcr=totalcr+25;
   else totalcr=totalcr+dartregion;
   } 
   else
   {
   }
   pl1=2;
   video_putsmalls(symbol_pos,ps1,third_hit); //Display 3 hit
   } 
   else if(pl1==0) // 2 score
   {
   if(pl2>2) totalcr=totalcr+dartregion+dartregion;
   else if(pl3>2) totalcr=totalcr+dartregion+dartregion;
   else if(pl4>2) totalcr=totalcr+dartregion+dartregion; 
   else
   {
   }
   pl1=2; 
   video_putsmalls(symbol_pos,ps1,third_hit); //Display 3 hit 
   } 
   end
   //=========================================================================================================
 
 
   //=====================================================================================================    
   
   //=====================================================================================================
   //==================================
   // set up the ports and timers
   void main(void)
   begin 
   //init timer 1 to generate sync
   OCR1A = lineTime; //One NTSC line
   TCCR1B = 9; //full speed; clear-on-match
   TCCR1A = 0x00; //turn off pwm and oc lines
   TIMSK = 0x10; //enable interrupt T1 cmp 
   
   
   //init ports
   DDRD = 0xf0; //video out and switches
   //D.5 is sync:1000 ohm + diode to 75 ohm resistor
   //D.6 is video:330 ohm + diode to 75 ohm resistor
   
   DDRA = 0x00;
   DDRB = 0xff;
   DDRC = 0xff;
   PORTA = 0xff;
   
   //init timer 0 to 1/uSec 
   TCCR0 = 2;
   
   //initialize synch constants 
   LineCount = 1;
   syncON = 0b00000000;
   syncOFF = 0b00100000; 
   // Intializing all the stuff
   init();
   
   //enable sleep mode
   MCUCR = 0b10000000;
   #asm ("sei");
   
   //The following loop executes once/video line during lines
   //1-230, then does all of the frame-end processing
   while(1)
   begin
   
   //stall here until next line starts
   //sleep enable; mode=idle 
   //use sleep to make entry into sync ISR uniform time 
   
   #asm ("sleep"); 
   
   //The following code executes during the vertical blanking
   //Code here can be as long as 
   //a total of 60 lines x 63.5 uSec/line x 8 cycles/uSec 
   
   
   
   if (LineCount==231)
   begin
   //============================================================ 
   
   //a clock
   /*if (++t>59)
   begin
   t=0; 
   time = time + 1;
   sprintf(ts,"%05d",time);
   video_putsmalls(92,93,ts);
   end*/

   button_polling(); //Check for push button
   
   switch(State)
   {
   case Menu_Draw:
   //draw the Menu Screen
   draw_menu();
   State=Menu;
   break; 
   
   //-------------------------------------------------------------------- 
   case Erase0: //Erase Game or menu text 
   if(temp==0) //Saves from redrawing lines
   {
   temp=1;
   video_putsmalls(24,3,hit_erase); //1st screen Update
   video_putsmalls(12,3,hit_erase);
   
   video_putsmalls(40,30,hit_erase);
   video_putsmalls(64,40,hit_erase);
   video_putsmalls(24,18,hit_erase);
   video_putsmalls(40,18,hit_erase);
   video_putsmalls(60,18,hit_erase);
   video_putsmalls(80,18,hit_erase);
   video_putsmalls(100,18,hit_erase);
   video_putsmalls(2,30,hit_erase);
   video_putsmalls(80,30,hit_erase);
   video_putsmalls(24,30,hit_erase);
   video_putsmalls(60,30,hit_erase); 
 
 }
   else if(temp==1) //2nd Screen Update
   { 
   temp=2;
   
   video_putsmalls(80,50,hit_erase);
   video_putsmalls(24,50,hit_erase);
   video_putsmalls(60,50,hit_erase); 
   video_putsmalls(72,3,hit_erase);
   video_putsmalls(88,3,hit_erase);
   
   video_putsmalls(2,60,hit_erase);
   video_putsmalls(80,60,hit_erase);
   video_putsmalls(24,60,hit_erase);
   video_putsmalls(60,60,hit_erase); 
   video_putsmalls(80,50,hit_erase);
   video_putsmalls(100,50,hit_erase);
   video_putsmalls(80,60,hit_erase);
   video_putsmalls(100,60,hit_erase);
   }
   else if(temp==2)
   { //3rd Screen Update
   video_putsmalls(4,90,hit_erase); 
 video_putsmalls(40,80,hit_erase);
   video_putsmalls(24,90,hit_erase);
   video_putsmalls(90,30,hit_erase);
   video_putsmalls(78,30,hit_erase);
   video_putsmalls(40,22,hit_erase);
   video_putsmalls(60,22,hit_erase);
   video_putsmalls(80,22,hit_erase);
   video_putsmalls(100,22,hit_erase);
   video_putsmalls(80,40,hit_erase);
   video_putsmalls(100,40,hit_erase);
   video_putsmalls(80,30,hit_erase);
   video_putsmalls(100,30,hit_erase);
   
   temp=3; 
   }
   else if(temp==3) //4th Screen Update
   {
   
   video_putsmalls(2,40,hit_erase); 
   video_putsmalls(4,30,hit_erase);
   video_putsmalls(4,50,hit_erase);
   video_putsmalls(4,40,hit_erase); 
   video_putsmalls(4,60,hit_erase); 
   video_putsmalls(20,40,hit_erase);
   video_putsmalls(76,70,hit_erase);
   video_putsmalls(92,70,hit_erase); 
   video_putsmalls(60,80,hit_erase);
   video_putsmalls(76,80,hit_erase);
   video_putsmalls(92,80,hit_erase);
   video_putsmalls(76,90,hit_erase);
   video_putsmalls(92,90,hit_erase);
   temp=0;
   State= Menu_Draw;
   
   }
   
   break;
   //--------------------------------------------------------------------------------------------------------	   
   case Menu: //Track button push in Game menu
   button_polling();
 if(butnum==1) //Scroll Down
   {
   if(arrow_pos<arrow_bottom)
   {
   video_putsmalls(24,arrow_pos,arrow_small_erase); //Erase arrow
   arrow_pos=arrow_pos+10;
   game++;
   video_putsmalls(24,arrow_pos,arrow_small); 
   //Move and draw arrow
   butnum=0;
   }
   }
   else if(butnum==2) //Scroll Up
   {
   if(arrow_pos>arrow_top)
   {
   video_putsmalls(24,arrow_pos,arrow_small_erase); //Erase arrow
   arrow_pos=arrow_pos-10;
   game--;
   video_putsmalls(24,arrow_pos,arrow_small); 
   //Move and draw arrow
   butnum=0;
   }
   }
   else if(butnum==3) //Enter selection go to Player menu
   {
   State=Player_Draw;
   butnum=0;
   }
 break;
   //-----------------------------------------------------------------------------------------------------------	   
   case Player_Draw: //Draw player menu
   
   draw_player();
   State=Player; 
 break;
   //-----------------------------------------------------------------------------------------------------------------	   
   case Player: //Player button push state
   
   if(butnum==1)
   {
   if(arrow_pos<arrow_bottom) //scroll arrow down
   {
   video_putsmalls(24,arrow_pos,arrow_small_erase);
   arrow_pos=arrow_pos+10;
   players++;
   video_putsmalls(24,arrow_pos,arrow_small);
   butnum=0;
   }
   }
   else if(butnum==2) // scroll arrow up
   {
   if(arrow_pos>arrow_top)
   {
   video_putsmalls(24,arrow_pos,arrow_small_erase);
   arrow_pos=arrow_pos-10;
   players--;
   video_putsmalls(24,arrow_pos,arrow_small);
   butnum=0;
   }
   }
   else if(butnum==3) //Enter selection
   {
   State=Erase1;
   butnum=0;
   }
   else if(butnum==4) State=Erase0;
 break;
   //------------------------------------------------------------------------------------------------
   case Erase1: //Erase entire screen for game set up
   
   erase();
   State=Erase2;
   break;
//------------------------------------------------------------------------------------------------
   case Erase2: // Draw vertical line
   video_line(0,0,0,99,1);
   video_line(width,0,width,99,1); 
   State=Erase3;
   break;
//--------------------------------------------------------
   case Erase3: 
   //Draw horizontal lines
   video_line(0,0,width,0,1);
   video_line(0,99,width,99,1);
   video_line(0,11,width,11,1);
   if(game==1) State=Game301_Draw; //Select game state
   if(game==2) State=Cricket_Draw;
   if(game==3) State=Drink_Draw;
   if(game==4) State=Grade_Draw; 
   break;
   
   //-----------------------------------------------------------------------------------------------------	   
   case Game301_Draw:
   
   draw_game301();
   State=Game301_Play;
   
   break; 
   //----------------------------------------------------------------------------------------	   
   case Cricket_Draw:
   draw_gameCRKT();
   State=CRCK_play;
 
   break; 
   //----------------------------------------------------------------------
   case Grade_Draw:
   draw_gameGrade();
   State=Grade_Play;
   
   break;
   //---------------------------------------------------------------------- 
   case Drink_Draw:
   
   draw_gameDrink();
   State=Drink_Play;
   
   break;
//----------------------------------------------------------------------------
   case CRCK_play: //Cricket Play Game State
   darttemp2=dart;
   if(winner1==1) //Winner Function
   {
   dart=4;
   video_putsmalls(40,80,winner); 
   //In case Debounce Winner
   winner1=0;
   } 
   video_putsmalls(12,90,RND); //Display round number 
   sprintf(ts,"%02d",(int) round);
   video_putsmalls(28,90,ts);
   if(dart<4)
   begin 
   
   if(Update==yes)
   {
   dart_hit_update(); //Display Hit
   video_putsmalls(76,hit_pos,hit);
   sprintf(ts,"%02d",(int) dartregion);
   video_putsmalls(92,hit_pos,ts); 
   // Displace hit number
   dart++;
   bounce=NotBounced;
   hit_pos=hit_pos+10; 
   video_putsmalls(40,80,hit_erase);
   darttemp=0;
   if(dartregion>14)
   { 
   
   message();
   posit=dartregion-15; 
   //set array postion 
   
   if(dartregion>23) posit=6;
   symbol_pos= 40+ posit*12;
   disp_total=total[player_number]; 
   
   // 5 vars,totalcr, pl1-pl4
   if(player_number==0) 
   //Player 1 conditions
   {
   win1=0; 
   totalcr=total[0];
   pl1=playz1[posit];
   pl2=playz2[posit];
   pl3=playz3[posit];
   pl4=playz4[posit];
   ps1=30;
   darttemp=pl1; 
   player1check(); //Update Player 1
   sprintf(ts,"%03d",(int) total[0]);
   video_putsmalls(20,30,ts); 
   // Displace hit number 
   total[0]=totalcr;
   playz1[posit]=pl1;
   playz2[posit]=pl2;
   playz3[posit]=pl3;
   playz4[posit]=pl4; 
   
   if(playz1[0]==2) win1++; 
   //Check for close out of regions
   if(playz1[1]==2) win1++;
   if(playz1[2]==2) win1++;
   if(playz1[3]==2) win1++;
   if(playz1[4]==2) win1++;
   if(playz1[5]==2) win1++;
   if(playz1[6]==2) win1++;
   if(win1==7)
   { 
   //Check for score greater then other players
   if(total[0]>=total[1]) win1++;
   if(total[0]>=total[2]) win1++;
   if(total[0]>=total[3]) win1++;
   }
   if(win1==10) winner1=1; 
   //If all is true, then winner
   
   
   } 
   else if(player_number==1) //If player 2
   {
   win1=0;
   totalcr=total[1];
   pl1=playz2[posit];
   pl2=playz1[posit];
   pl3=playz3[posit];
   pl4=playz4[posit]; 
   ps1=40;
   darttemp=pl1;
   player1check(); 
   total[1]=totalcr;
   playz2[posit]=pl1;
   playz1[posit]=pl2;
   playz3[posit]=pl3;
   playz4[posit]=pl4;
   sprintf(ts,"%03d",(int) total[1]);
   video_putsmalls(20,40,ts); 
   // Display hit number 
   
   if(playz2[0]==2) win1++;
   if(playz2[1]==2) win1++;
   if(playz2[2]==2) win1++;
   if(playz2[3]==2) win1++;
   if(playz2[4]==2) win1++;
   if(playz2[5]==2) win1++;
   if(playz2[6]==2) win1++;
   if(win1==7)
   { 
   if(total[1]>=total[0]) win1++;
   if(total[1]>=total[2]) win1++;
   if(total[1]>=total[3]) win1++;
   }
   if(win1==10) winner1=1;
   
   
   }
   else if(player_number==2) //If player 3
   {
   win1=0;
   totalcr=total[2];
   pl1=playz3[posit];
   pl2=playz1[posit];
   pl3=playz2[posit];
   pl4=playz4[posit]; 
   ps1=50;
   darttemp=pl1;
   player1check(); 
   total[2]=totalcr;
   sprintf(ts,"%03d",(int) total[2]);
   video_putsmalls(20,50,ts); 
   // Display hit number 
   
   playz3[posit]=pl1;
   playz1[posit]=pl2;
   playz2[posit]=pl3;
   playz4[posit]=pl4;
   
   if(playz3[0]==2) win1++;
   if(playz3[1]==2) win1++;
   if(playz3[2]==2) win1++;
   if(playz3[3]==2) win1++;
   if(playz3[4]==2) win1++;
   if(playz3[5]==2) win1++;
   if(playz3[6]==2) win1++;
   if(win1==7)
   { 
   if(total[2]>=total[0]) win1++;
   if(total[2]>=total[1]) win1++;
   if(total[2]>=total[3]) win1++;
   }
   if(win1==10) winner1=1; 
   //Check player 3 winner
   
   
   
   }
   else if(player_number==3) //If player 4
   {
   win1=0;
   totalcr=total[3];
   pl1=playz4[posit];
   pl2=playz1[posit];
   pl3=playz2[posit];
   pl4=playz3[posit]; 
   ps1=60; 
   darttemp=pl1;
   player1check(); 
   total[3]=totalcr;
   sprintf(ts,"%03d",(int) total[3]);
   video_putsmalls(20,60,ts); 
   // Display hit number
   
   playz4[posit]=pl1;
   playz1[posit]=pl2;
   playz2[posit]=pl3;
   playz3[posit]=pl4;
   
   if(playz4[0]==2) win1++;
   if(playz4[1]==2) win1++;
   if(playz4[2]==2) win1++;
   if(playz4[3]==2) win1++;
   if(playz4[4]==2) win1++;
   if(playz4[5]==2) win1++;
   if(playz4[6]==2) win1++;
   if(win1==7)
   { 
   if(total[3]>=total[0]) win1++;
   if(total[3]>=total[1]) win1++;
   if(total[3]>=total[2]) win1++;
   }
   if(win1==10) winner1=1; 
   //Check player 4 winner
   } 
   else
   {
   } 
   
   
   
   sprintf(ts,"%03d",(int) total[0]);
   video_putsmalls(20,30,ts); 
   // Display hit number
   }
 
 
   }
   end 
 button_polling(); //Poll for button push during game
   if(butnum==1) //Change Player button
   {
   Update=no; //Updates flags
   butnum=0;
   dart=1;
   bounce=NotBounced;
 
 
   video_putsmalls(2,arrow_pos,arrow_small_erase); //Erase arrow and message area
   video_putsmalls(40,80,hit_erase);
   arrow_pos=arrow_pos+10;
   player_number++;
   
   if(player_number==players) 
   //If on last player go to beginning
   {
   player_number=0;
   round++;
   arrow_pos=arrow_top;
   }
   hit_pos=70;
   disp_total=total[player_number];
   video_putsmalls(2,arrow_pos,arrow_small); //Display Arrow
   video_putsmalls(76,70,hit_erase); 
   // Erase 3 hit messages
   video_putsmalls(76,80,hit_erase);
   video_putsmalls(76,90,hit_erase);
 }
   else if(butnum==2) 
   // Bounce Out button push
   {
   Update=no;
   butnum=0;
 if(bounce==NotBounced) 
   {
   if(dart>1) 
   // Enter only if Not Bounced before and 1 dart throw
   {
   bounce=Bounced; 
   
   dart=darttemp2;
   dart--;
   
   hit_pos=hit_pos-10;
   video_putsmalls(76,hit_pos,hit_erase);
   video_putsmalls(40,80,hit_erase);
   total[player_number]=disp_total;
   sprintf(ts,"%03d",(int) disp_total); //Display Total
   video_putsmalls(20,arrow_pos,ts); 
   //Erase last hit on video if bounce out
   if(player_number==0) 
   {
   pl1=darttemp;
   if(darttemp>0) playz1[posit]=pl1;
   if(pl1==3) video_putsmalls(symbol_pos,ps1,second_hit);
   if(pl1==4) video_putsmalls(symbol_pos,ps1,first_hit);
   if(pl1==5) video_putsmalls(symbol_pos,ps1,arrow_small_erase);
   }
   else if(player_number==1)
   {
   pl2=darttemp;
   if(darttemp>0) playz2[posit]=pl2;
   if(pl2==3) video_putsmalls(symbol_pos,ps1,second_hit);
   if(pl2==4) video_putsmalls(symbol_pos,ps1,first_hit);
   if(pl2==5) video_putsmalls(symbol_pos,ps1,arrow_small_erase);
   }
   else if(player_number==2)
   {
   pl3=darttemp; 
   if(darttemp>0) playz3[posit]=pl3;
   if(pl3==3) video_putsmalls(symbol_pos,ps1,second_hit);
   if(pl3==4) video_putsmalls(symbol_pos,ps1,first_hit);
   if(pl3==5) video_putsmalls(symbol_pos,ps1,arrow_small_erase);
   }
   else if(player_number==3)
   {
   pl4=darttemp; 
   if(darttemp>0) playz4[posit]=pl4;
   if(pl4==3) video_putsmalls(symbol_pos,ps1,second_hit);
   if(pl4==4) video_putsmalls(symbol_pos,ps1,first_hit);
   if(pl4==5) video_putsmalls(symbol_pos,ps1,arrow_small_erase);
   }
   else
   {
   }
   
   }
   }
 
 }
   else if(butnum==4) State=Erase0; //Reset to main menu

 
   break;
 
   //------------------------------------------------------------------------------------------------------
   case Game301_Play:
   if(dart<4)
   {
   video_putsmalls(12,90,RND);
   sprintf(ts,"%02d",(int) round);
   video_putsmalls(28,90,ts);
   if(Update==yes)
   {
   dart_hit_update(); //Display Hit
   video_putsmalls(76,hit_pos,hit);
   sprintf(ts,"%02d",(int) dartregion);
   video_putsmalls(92,hit_pos,ts); // Displace hit number
 if(disp_total==dartscore) // Check for winner
   {
   disp_total=0;
   winner1=1; //Declare State winner 
   darttemp=dart;
   sprintf(ts,"%03d",(int) disp_total); //Display Total
   video_putsmalls(36,arrow_pos,ts);
   
   }
   else if(disp_total>dartscore)
   { //Normal decrement state
   
   hit_pos=hit_pos+10;
   
   disp_total=disp_total-dartscore; 
   darttemp=dart;
   sprintf(ts,"%03d",(int) disp_total); //Display Total
   video_putsmalls(36,arrow_pos,ts);
   message();
   dart++;
   bounce=NotBounced; //Declare Bounce Var 
   }
   else // Bust state
   {
   video_putsmalls(40,80,bust);
   bounce=BustBounced;
   darttemp=dart; //declare darttemp
   dart=4;
   disptemp = disp_total; //declare
   disp_total=total[player_number]; 
   sprintf(ts,"%03d",(int) disp_total); //Display Total
   video_putsmalls(36,arrow_pos,ts);
   hit_pos=hit_pos+10;
   } 
   }
   }
   if(winner1==1) //Winner State
   {
   dart=4;
   video_putsmalls(40,80,winner); 
   hit_pos=hit_pos+10; //In case Debounce Winner
   winner1=0;
   }
   
   button_polling();
   if(butnum==1) //Change Player
   {
   Update=no; //Updates for flags
   butnum=0;
   dart=1;
   bounce=NotBounced;
   total[player_number]=disp_total; 
   
   video_putsmalls(8,arrow_pos,arrow_small_erase); //Erase arrow and message area
   video_putsmalls(40,80,hit_erase);
   arrow_pos=arrow_pos+10;
   player_number++;
   
   if(player_number==players) //If on last player go to beginning
   {
   player_number=0;
   round++;
   arrow_pos=arrow_top;
   }
   hit_pos=30;
   disp_total=total[player_number];
   video_putsmalls(8,arrow_pos,arrow_small); //Display Arrow
   video_putsmalls(76,30,hit_erase); // Erase 3 hit messages
   video_putsmalls(76,40,hit_erase);
   video_putsmalls(76,50,hit_erase);
 }
   else if(butnum==2) // Bounce Out
   {
   Update=no;
   butnum=0;
 if(bounce==NotBounced) 
   {
   if(dart>1) // Enter only if Not Bounced before and 1 dart throw
   {
   bounce=Bounced;
   dart= darttemp; 
   disp_total=disp_total+dartscore;
   sprintf(ts,"%03d",(int) disp_total); //Display Total
   video_putsmalls(36,arrow_pos,ts);
   hit_pos=hit_pos-10;
   video_putsmalls(76,hit_pos,hit_erase);
   video_putsmalls(40,80,hit_erase);
   }
   }
   else if(bounce==BustBounced) //Bounce out Bust Condition
   {
   bounce=Bounced;
   dart=darttemp;
   disp_total=disptemp;
   sprintf(ts,"%03d",(int) disp_total); //Display Total
   video_putsmalls(36,arrow_pos,ts);
   hit_pos=hit_pos-10;
   video_putsmalls(76,hit_pos,hit_erase); //Erase last throw
   video_putsmalls(40,80,hit_erase);
   }
   }
   else if(butnum==4) State=Erase0;
 break;
 
   //------------------------------------------------------------------------------------------------------------
   
   case Grade_Play:
   if(dart<6)
   begin 
   
   if(Update==yes)
   { 
   if(darttemp2==0)
   {
   dart_hit_update(); //Display Hit
   video_putsmalls(76,hit_pos,hit);
   sprintf(ts,"%02d",(int) dartregion);
   video_putsmalls(92,hit_pos,ts); // Displace hit number
   dart++;
   bounce=NotBounced;
   hit_pos=hit_pos+10; 
   video_putsmalls(40,80,hit_erase);
   message();
   total[player_number]=total[player_number]+dartscore;
   sprintf(ts,"%03d",(int) total[player_number]);
   video_putsmalls(20,arrow_pos,ts);
   }
   
   
   
   }
   end 
   
   button_polling();
   if(butnum==1) //Change Player
   {
   Update=no; //Updates for flags
   butnum=0;
   dart=1;
   bounce=NotBounced;
   if(darttemp2==0)
   { 
   
   //Calculate grade for score
   if(total[player_number]<50) video_putsmalls(40,arrow_pos,fff);//Display F
   else if(total[player_number]<=55)
   {
   video_putsmalls(40,arrow_pos,ddd);
   video_putsmalls(44,arrow_pos,minus);
   } //Display D-
   else if(total[player_number]<=60) video_putsmalls(40,arrow_pos,ddd); //Display    D
   else if(total[player_number]<=65) 
   {
   video_putsmalls(40,arrow_pos,ddd);
   video_putsmalls(44,arrow_pos,plus);
   } //Display D+
   else if(total[player_number]<=70)
   {
   video_putsmalls(40,arrow_pos,ccc);
   video_putsmalls(44,arrow_pos,minus);
   } //Display C-
   else if(total[player_number]<=75) video_putsmalls(40,arrow_pos,ccc); //Display    C
   else if(total[player_number]<=80)
   {
   video_putsmalls(40,arrow_pos,ccc);
   video_putsmalls(44,arrow_pos,plus);
   } //Display C+
   else if(total[player_number]<=85)
   {
   video_putsmalls(40,arrow_pos,bbb);
   video_putsmalls(44,arrow_pos,minus);
   } //Display B-
   else if(total[player_number]<=90) video_putsmalls(40,arrow_pos,bbb);//Display    B
   else if(total[player_number]<=95)
   {
   video_putsmalls(40,arrow_pos,bbb);
   video_putsmalls(44,arrow_pos,plus);
   } //Display B+
   else if(total[player_number]<=100)
   {
   video_putsmalls(40,arrow_pos,aaa);
   video_putsmalls(44,arrow_pos,minus);
   } //Display A-
   else if(total[player_number]<=110) video_putsmalls(40,arrow_pos,aaa);//Display    A
   else if(total[player_number]>110)
   {
   video_putsmalls(40,arrow_pos,aaa);
   video_putsmalls(44,arrow_pos,plus);
   } //Display A+
   else 
   {
   } 
   
   
   video_putsmalls(2,arrow_pos,arrow_small_erase); //Erase arrow and message area
   video_putsmalls(40,80,hit_erase);
   arrow_pos=arrow_pos+10;
   video_putsmalls(2,arrow_pos,arrow_small); //Display Arrow
   player_number++;
   } 
   
   if(player_number==players) //If on last player go to beginning
   {
   
   darttemp2=1; 
   video_putsmalls(2,arrow_pos,arrow_small_erase); 
   } 
   
   hit_pos=50;
   disp_total=total[player_number];
   
   video_putsmalls(76,50,hit_erase);
   video_putsmalls(76,60,hit_erase);
   video_putsmalls(76,70,hit_erase); // Erase 5 hit messages
   video_putsmalls(76,80,hit_erase);
   video_putsmalls(76,90,hit_erase);
 }
   else if(butnum==4) State=Erase0; //If reset button hit, go back to select game    screen 
   
   
   
   
   
   break; 
   //-------------------------------------------------------------------------------
   case Drink_Play: //Drinking Game Play State
   if(dart<4)
   begin 
   
   if(Update==yes)
   {
   if(round<10)
   {
   video_putsmalls(48,80,hit_erase);
   dart_hit_update(); //Display Hit
   video_putsmalls(76,hit_pos,hit);
   sprintf(ts,"%02d",(int) dartregion);
   video_putsmalls(92,hit_pos,ts); // Displace hit number
   dart++;
   bounce=NotBounced;
   hit_pos=hit_pos+10; 
   video_putsmalls(40,80,hit_erase);
   message();
   total[player_number]=total[player_number]+dartscore;
   sprintf(ts,"%03d",(int) total[player_number]);
   video_putsmalls(20,arrow_pos,ts);
   
   } 
   }
   end
   
   button_polling();
   if(butnum==1) //Change Player
   {
   Update=no; //Updates for flags
   butnum=0;
   dart=1;
   bounce=NotBounced;
   
   
   
   
   video_putsmalls(2,arrow_pos,arrow_small_erase); //Erase arrow and message area
   video_putsmalls(40,80,hit_erase);
   arrow_pos=arrow_pos+10;
   //Check for lowest score, if tie, last person to throw darts drinks
   if(player_number==0) 
   {
   darttemp2=total[player_number]; 
   darttemp=0;
   }
   else if(player_number==1)
   {
   if(darttemp2>=total[player_number])
   {
   darttemp2=total[player_number];
   darttemp=1;
   }
   }
   else if(player_number==2)
   {
   if(darttemp2>=total[player_number])
   {
   darttemp2=total[player_number];
   darttemp=2;
   }
   }
   else if(player_number==3)
   {
   if(darttemp2>=total[player_number])
   {
   darttemp2=total[player_number];
   darttemp=3;
   }
   }
   else
   {
   } 
   
   
   player_number++; 
   if(player_number==players) //If on last player go to beginning
   {
   player_number=0;
   arrow_pos=arrow_top; 
   
   //Display shot glass at who drinks 
   if(round<9)
   {
   if(darttemp==0)
   {
   video_putsmalls(40,80,p1);
   video_putsmalls(disp_total,30,drink_sy);
   }
   if(darttemp==1) 
   {
   video_putsmalls(40,80,p2);
   video_putsmalls(disp_total,40,drink_sy);
   }
   if(darttemp==2) 
   {
   video_putsmalls(40,80,p3);
   video_putsmalls(disp_total,50,drink_sy);
   }
   if(darttemp==3) 
   {
   video_putsmalls(40,80,p4);
   video_putsmalls(disp_total,60,drink_sy);
   }
   }
   //Display player who drinks
   video_putsmalls(52,80,menu_game3);
   video_putsmalls(72,80,hit_erase);
   video_putsmalls(84,80,hit_erase); 
   disp_total=disp_total+8;
   //Rest totals for next round
   total[0]=0;
   total[1]=0;
   total[2]=0;
   total[3]=0;
   round++;
   //Display reset totals
   sprintf(ts,"%03d",(int) total[0]);
   video_putsmalls(20,30,ts);
   if (players>=2)
   {
   sprintf(ts,"%03d",(int) total[1]);
   video_putsmalls(20,40,ts);
   }
   if(players>=3)
   {
   sprintf(ts,"%03d",(int) total[2]);
   video_putsmalls(20,50,ts);
   }
   if(players>=4)
   {
   sprintf(ts,"%03d",(int) total[3]);
   video_putsmalls(20,60,ts);
   }
   
   }
   
   hit_pos=70;
   video_putsmalls(2,arrow_pos,arrow_small); //Display Arrow
   video_putsmalls(76,70,hit_erase); // Erase 3 hit messages
   video_putsmalls(76,80,hit_erase);
   video_putsmalls(76,90,hit_erase);
 }
   else if(butnum==4) State=Erase0; 
   
 

break;
   //----------------------------------------------------------------------------------------
   default: //Default Case
   State=Menu;
   break;
   }
 //============================================================
   
   end //line 231
   end //while
   end //main