/********************************************************************************* * * CONNECT FOUR * Final Project for ECE 476 * Prof. Bruce Land * Cornell University * Submitted on 4/28/2003 * Authors: Matthew Kenney and Patric Lowe * * Written for the Atmel Mega32. This program will output, black and white, * To a television screen with sound (given the appropriate hardware setup. * It will play either a two player game, or a one player game with three levels * of AI. * ***********************************************************************************/ //video gen and sound //D.5 is sync:1000 ohm + diode to 75 ohm resistor //D.6 is video:330 ohm + diode to 75 ohm resistor //C.0 is sound #pragma regalloc- //I allocate the registers myself #pragma optsize- //optimize for speed #include #include #include #include #include //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 #define ScreenBot 230 #define T0reload 256-60 #define maxkeys 11 //define states for the connect four state machine #define promptForUser 0 #define isValid 1 #define draw 2 #define checkFinish 3 #define Finish 4 #define Restart 5 #define waitForUser 6 #define debounce 7 #define initialize 8 #define initDebounce 9 #define promptLevel 10 #define debounceLevel 11 #define drawBoard 12 #define notPushed 13 //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 time; long int rand_seed = 17; //used for generating our random numbers char j; //used for for loops int k; //used to clear screen char GameState; //state machine for connect four char currentPlayer; //what player is up? //animation char x, y, s, vx, vy; char x1, y1, x2, y2, x3, y3; //used for fireworks graphics char screen[1600], t, ts[10]; //keeps track of the pixels written to the screen char plyr[]="PLAYER"; //generic string char win[]="WINS"; //used when a player wins char reset9[] = "9 2 RESET"; //tells user how to reset the game after a win /* char message1[] = "IDIOT"; //the following five messages are trash talking used by the computer char message2[] = "MORON"; // in a one player game. We had to eliminate this since our code was too char message3[] = "FOOL"; // long for the mcu. We left it in to document that we had it, and in case char message4[] = "DUMMY"; // groups in the future would want to see how our code worked. char message5[] = "PUNK"; */ char messageBlank[] = " "; //used to overwrite previously written messages char numPlayers[] = "NUM PLYRS"; //prompt user for number of players char levels[] = "LEVEL 1 2 3"; //prompt user for level char welcome[] = "WELCOME"; //welcome message char to[] = "TO"; char connect4[] = "CONNECT 4"; char tie[] = "TIE "; //used for a tie game char maybe; //used for debouncing the keypad char currentMove; //current column that the user is placing (or wants to place) a checker in char players; // number of players in the current game char currentRow; //when a piece is added, this is the row to which it has been added char debounceCounter; //used for debouncing the keypad char eraseCounter; //used for erasing the board without flicker char drawCounter; //used for drawing the checkers with animation char waittoDraw; //deals with animation timing issues char choice; // determines which column the computer will choose on level 1 int bestMove[7]; //an array of moves for the computer. char level; // current level in a one player game char interlaceflag; //we attempted to use this for interlacing frames. We did not succede char leveltemp; //used while generating fireworks to determine the amount of fireworks to generate. int wait; //variable used throughout the state machine to break states up into small sections which // can then be computed during multiple frames to avoid flicker and image distortion. int temp; char temp2; char m; //tone related variables //set note to a number to play a tone char note, halfnote, notecount, inote, tnote; //define some musical notes //number of video lines per cycle of audio //starting at c3 to c5 //frequency error is less than 1% //The 3 at the end is a "rest" /*flash char notes[16]={120,107,95,90,80,71,64,60, 54,48,45,40,36,32,30,3}; */ flash char notes[16]={240,214,190,180,160,142,128,120, 108,96,90,80,72,64,60,6}; flash char notes2[4]={54, 47, 42, 40}; //specific values used for the connect four theme song. char board[6][7]; // the software representation of the connect four playing board. //Point plot lookup table flash char pos[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; flash unsigned char keytbl[11]={0b01111110, 0xee, 0xed, 0xeb, 0b11011110, 0b11011101, 0b11011011, 0b10111110, 0b10111101, 0b10111011, 0b01110111}; //mapping which will provide which key is being pressed depending on the index. char butnum, key, butnumber; //used to determine which key is pressed //define some character bitmaps //5x7 characters flash char bitmap[41][7]={ //0 0b01110000, 0b10001000, 0b10011000, 0b10101000, 0b11001000, 0b10001000, 0b01110000, //1 0b00100000, 0b01100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b01110000, //2 0b01110000, 0b10001000, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b11111000, //3 0b11111000, 0b00010000, 0b00100000, 0b00010000, 0b00001000, 0b10001000, 0b01110000, //4 0b00010000, 0b00110000, 0b01010000, 0b10010000, 0b11111000, 0b00010000, 0b00010000, //5 0b11111000, 0b10000000, 0b11110000, 0b00001000, 0b00001000, 0b10001000, 0b01110000, //6 0b01000000, 0b10000000, 0b10000000, 0b11110000, 0b10001000, 0b10001000, 0b01110000, //7 0b11111000, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000, 0b10000000, //8 0b01110000, 0b10001000, 0b10001000, 0b01110000, 0b10001000, 0b10001000, 0b01110000, //9 0b01110000, 0b10001000, 0b10001000, 0b01111000, 0b00001000, 0b00001000, 0b00010000, //A 0b01110000, 0b10001000, 0b10001000, 0b10001000, 0b11111000, 0b10001000, 0b10001000, //B 0b11110000, 0b10001000, 0b10001000, 0b11110000, 0b10001000, 0b10001000, 0b11110000, //C 0b01110000, 0b10001000, 0b10000000, 0b10000000, 0b10000000, 0b10001000, 0b01110000, //D 0b11110000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b11110000, //E 0b11111000, 0b10000000, 0b10000000, 0b11111000, 0b10000000, 0b10000000, 0b11111000, //F 0b11111000, 0b10000000, 0b10000000, 0b11111000, 0b10000000, 0b10000000, 0b10000000, //G 0b01110000, 0b10001000, 0b10000000, 0b10011000, 0b10001000, 0b10001000, 0b01110000, //H 0b10001000, 0b10001000, 0b10001000, 0b11111000, 0b10001000, 0b10001000, 0b10001000, //I 0b01110000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b01110000, //J 0b00111000, 0b00010000, 0b00010000, 0b00010000, 0b00010000, 0b10010000, 0b01100000, //K 0b10001000, 0b10010000, 0b10100000, 0b11000000, 0b10100000, 0b10010000, 0b10001000, //L 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b11111000, //M 0b10001000, 0b11011000, 0b10101000, 0b10101000, 0b10001000, 0b10001000, 0b10001000, //N 0b10001000, 0b10001000, 0b11001000, 0b10101000, 0b10011000, 0b10001000, 0b10001000, //O 0b01110000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b01110000, //P 0b11110000, 0b10001000, 0b10001000, 0b11110000, 0b10000000, 0b10000000, 0b10000000, //Q 0b01110000, 0b10001000, 0b10001000, 0b10001000, 0b10101000, 0b10010000, 0b01101000, //R 0b11110000, 0b10001000, 0b10001000, 0b11110000, 0b10100000, 0b10010000, 0b10001000, //S 0b01111000, 0b10000000, 0b10000000, 0b01110000, 0b00001000, 0b00001000, 0b11110000, //T 0b11111000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, //U 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b01110000, //V 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b01010000, 0b00100000, //W 0b10001000, 0b10001000, 0b10001000, 0b10101000, 0b10101000, 0b10101000, 0b01010000, //X 0b10001000, 0b10001000, 0b01010000, 0b00100000, 0b01010000, 0b10001000, 0b10001000, //Y 0b10001000, 0b10001000, 0b10001000, 0b01010000, 0b00100000, 0b00100000, 0b00100000, //Z 0b11111000, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000, 0b11111000, //figure1 0b01110000, 0b00100000, 0b01110000, 0b10101000, 0b00100000, 0b01010000, 0b10001000, //figure2 0b01110000, 0b10101000, 0b01110000, 0b00100000, 0b00100000, 0b01010000, 0b10001000, // player 1 piece 0b00000000, 0b00110000, 0b01001000, 0b01001000, 0b01001000, 0b01001000, 0b00110000, // player 2 piece 0b00000000, 0b00110000, 0b01111100, 0b01111100, 0b01111100, 0b01111100, 0b00110000, // blank 0b00000000, 0b00000000, 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++; //Much of the code that is commented out here details our attempts to get interlaced // frames to work. We weren't successful, but this should provide the graders with some // indication of the paths that we took in attempting to implement this, and should also tell // future groups what NOT to do. //LineCount++; //start code for interlacing frames //LineCount += 2; //laceflag = ~laceflag; //stop code for interlacing frames //begin inverted (Vertical) synch after line 247 //if ((LineCount==248) || (LineCount == 249)) if (LineCount == 248) begin syncON = 0b00100000; syncOFF = 0; end //back to regular sync after line 250 //if ((LineCount==251) || (LineCount == 252)) if (LineCount == 251) begin syncON = 0; syncOFF = 0b00100000; end //start new frame after line 262 //if ((LineCount==263) || (LineCount == 264)) if (LineCount == 263) begin //More failed interlacing stuff /* if (interlaceflag != 1) { LineCount = 1; interlaceflag = 1; } else { LineCount = 2; interlaceflag = 0; } */ LineCount = 1; end //toggle the audio bits // the two branches of the if statment // must take the same number of cycles to aviod jitter on TV if ((notecount=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 end #pragma warn+ int randnum() /* from K&R // obtained from www.howstuffworks.com/c6.htm - produces a random number between 0 and 32767. */ // we had problems using rand() which should have been included in one of the // files that we included above, but, alas, it did not work. We used the function // below instead. { rand_seed = rand_seed * 1103515245 +12345; return (unsigned int)(rand_seed / 65536) % 32768; } char keypad() begin //get lower nibble DDRC = 0x0f; PORTC = 0xf0; delay_us(5); key = PINC; //get upper nibble DDRC = 0xf0; PORTC = 0x0f; delay_us(5); key = key | PINC; //find matching keycode in keytbl if(key!=0xff) { for(butnum=0; butnum>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+ //================================== // draw a firework for when a player wins The variables x and y define the center point of // the firework. This function should take 34/60 seconds to complete. Along with the // fireworks should be a sound. // NOTE: This function too had to be eliminated due to space concerns. It works quite well, and is fully // animated. However, as is quite evident, since the firework here is drawn pixel by pixel, it is rather // verbose code. The bells and whistles of the program were the first to go. Again, we left it in our listing // as documentation of our work. /*void video_firework(char x, char y) { if (wait == 60) //the speed of the animation slows as the firework gets bigger { video_pt(x,y,1); //the centerpoint of the firework note = notes[0]; //sound accompanying the firework } else if (wait == 58) { video_pt(x+1,y-1,1); //draw one pixel at a time, in six directions video_pt(x+1,y+1,1); video_pt(x-1,y-1,1); video_pt(x-1,y+1,1); video_pt(x+1,y,1); video_pt(x-1,y,1); video_pt(x,y-1,1); video_pt(x,y+1,1); note = notes[1]; } else if (wait == 56) { video_pt(x+2,y-2,1); video_pt(x+2,y+2,1); video_pt(x-2,y-2,1); video_pt(x-2,y+2,1); video_pt(x+2,y,1); video_pt(x-2,y,1); video_pt(x,y-2,1); video_pt(x,y+2,1); note = notes[2]; } else if (wait == 53) { video_pt(x+3,y-3,1); video_pt(x+3,y+3,1); video_pt(x-3,y-3,1); video_pt(x-3,y+3,1); video_pt(x+3,y,1); video_pt(x-3,y,1); video_pt(x,y-3,1); video_pt(x,y+3,1); note = notes[3]; } else if (wait == 50) { video_pt(x+4,y-4,1); video_pt(x+4,y+4,1); video_pt(x-4,y-4,1); video_pt(x-4,y+4,1); video_pt(x+4,y,1); video_pt(x-4,y,1); video_pt(x,y-4,1); video_pt(x,y+4,1); note = notes[4]; } else if (wait == 46) { video_pt(x+5,y-5,1); video_pt(x+5,y+5,1); video_pt(x-5,y-5,1); video_pt(x-5,y+5,1); video_pt(x+5,y,1); video_pt(x-5,y,1); video_pt(x,y-5,1); video_pt(x,y+5,1); note = notes[5]; } else if (wait == 42) { video_pt(x+6,y-5,1); //at this point the firework begins to "fall" video_pt(x+5,y+6,1); video_pt(x-6,y-5,1); video_pt(x-5,y+6,1); video_pt(x+6,y+1,1); video_pt(x-6,y+1,1); video_pt(x,y-5,1); video_pt(x,y+6,1); note = notes[6]; } else if (wait == 37) { video_pt(x+7,y-4,1); video_pt(x+6,y+7,1); video_pt(x-7,y-4,1); video_pt(x-6,y+7,1); video_pt(x+7,y+2,1); video_pt(x-7,y+2,1); video_pt(x,y-6,1); video_pt(x,y+7,1); note = notes[7]; } else if (wait == 32) { video_pt(x+8,y-3,1); video_pt(x+6,y+8,1); video_pt(x-8,y-3,1); video_pt(x-6,y+8,1); video_pt(x+7,y+3,1); video_pt(x-7,y+3,1); video_pt(x,y-6,1); video_pt(x,y+9,1); note = notes[8]; } else if (wait == 26) { video_pt(x+8,y-2,1); video_pt(x+6,y+10,1); video_pt(x-8,y-2,1); video_pt(x-6,y+10,1); video_pt(x+7,y+5,1); video_pt(x-7,y+5,1); video_pt(x,y-6,1); video_pt(x,y+11,1); note = notes[9]; } } */ //================================== // 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 string of big characters on the screen void video_puts(char x, char y, char *str) begin char i ; for (i=0; str[i]!=0; i++) begin if (str[i] == ' ') video_putchar(x,y,40); else if (str[i]>=0x30 && str[i]<=0x3a) video_putchar(x,y,str[i]-0x30); else video_putchar(x,y,str[i]-0x40+9); x = x+6; 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 //================================== //return the value of one point //at x,y with color 1=white 0=black 2=invert char video_set(char x, char y) begin //The following construction //detects exactly one bit at the x,y location i=((int)x>>3) + ((int)y<<4) ; return ( screen[i] & 1<<(7-(x & 0x7))); end char check_win() begin //We need to check all four directions for both players - horizontal, veritcal, and // diagonals. On the board, a zero represents a blank space, a one represents a checker // from player one, and a 2 represents a check from player 2. Returns a 0 if no one has won yet. // Return a 3 for a tie. i=0; while ((board[5][i] != 0) && (i <= 6)); i++; if (i==7) return 3; //the board is completely full, so we return a tie for(j = 0; j<6; j++) for(i = 0; i < 4; i++) //check horizontal. The for loop steps though each of the rows. There are four combinations // of columns in each row in which four pieces of one kind may be in line. { if( board [j][i] != 0) if (board [j][i+1] == board[j][i]) if (board [j][i+2] == board [j][i]) if (board[j][i+3] == board[j][i]) return board[j][i]; } //end horizontal for loop for(j = 0; j<7; j++) for(i = 0; i < 3; i++) //check vertical. The for loop steps through the different columns { if( board [i][j] != 0) if (board [i+1][j] == board[i][j]) if (board [i+2][j] == board [i][j]) if (board[i+3][j] == board[i][j]) return board[i][j]; } //end vertical for loop for(j=0; j < 4; j++) for (i = 0; i < 3; i++) //check diagonal up and to the right { if( board [i][j] != 0) if (board [i+1][j+1] == board[i][j]) if (board [i+2][j+2] == board[i][j]) if (board[i+3][j+3] == board[i][j]) return board[i][j]; //check diagonal DOWN and to the right if( board [i+3][j] != 0) if (board [i+2][j+1] == board[i+3][j]) if (board [i+1][j+2] == board[i+3][j]) if (board[i][j+3] == board[i+3][j]) return board[i+3][j]; } //end diagonal for loop return 0; //the game is not yet over end //check_win char add_piece(char col,char player) // adds a checker for the specified player in the given column. If the column is full, return -1, // else, return 0 if successful. Again, 0 is blank, 1 is for player 1, and 2 is player 2. begin if (board[5][col-1] != 0) //that column is full! return -1; else { for (j = 0; j <= 5; j++) //step through each row in the given column if (board[j][col-1] == 0) { board[j][col-1] = player; // we found the first blank space in the given column // so we can now add the piece return j; //return the row where we add the piece. } //end if } //end else end //add_piece char easyComputer() //Represents level 1 (of 3) //randomly picks a column to place the checker. Checks to make sure that the pick is good begin return ( (randnum() %7) + 1 ); //return the column chosen end //easy computer char mediumComputer() //Represents level 2 (of 3) //check for a win by computer or player. If it can block an opponents win, or gain a win for itself, // it returns the appropriate column value. If no such move exists, returns a random value. begin for(i=0;i<6;i++) for(j=0;j<4;j++) { // check for a X| |X|X if(board[i][j]!=0) if((board[i][j+2] == board[i][j]) && (board[i][j+3] == board[i][j])) if((i>0) && (board[i-1][j+1]==0)) {} //we don't want to explicitly set up the opponent for a win //such a move would look like X| |X|X // O| |O|X // X|O|O|O else { if (board[i][j+1] == 0) return (j+2); //return a value between one and seven. } // check for a X|X| |X if(board[i][j]!=0) if((board[i][j+1] ==board[i][j]) && (board[i][j+3]==board[i][j])) if((i>0) && (board[i-1][j+2]==0)) {} //don't set up the opponent else { if (board[i][j+2] == 0) return (j+3); } // check for a X|X|X| | if(board[i][j] != 0) if ((board[i][j+1] == board[i][j]) && (board[i][j+2] == board[i][j])) if((i>0) && (board[i-1][j+3]==0)) {} else { if (board[i][j+3] == 0) return (j+4); } // check for a | |X|X|X if(board[i][j+3] != 0) if ((board[i][j+1] == board[i][j+3]) && (board[i][j+2] == board[i][j+3])) if((i>0) && (board[i-1][j]==0)) {} else { if (board[i][j] == 0) return (j+1); } } //check three in a row vertical for(i=4; i>1; i--) for (j = 0; j < 7; j++) if (board[i][j] !=0) if ((board[i-1][j] == board[i][j]) && (board[i-2][j] == board[i][j])) if ( board[i+1][j] == 0) return(j+1); //right diagonals for(i=0; i<3; i++) for(j=0; j<4; j++) { // b // x // x // x if(board[i][j] != 0) if ((board[i+1][j+1] == board[i][j]) && (board[i+2][j+2] == board[i][j])) if(board[i+2][j+3]==0) {} else { if (board[i+3][j+3] == 0) return (j+4); } // x // b // x // x if(board[i][j] != 0) if ((board[i+1][j+1] == board[i][j]) && (board[i+3][j+3] == board[i][j])) if(board[i+1][j+2]==0) {} else { if (board[i+2][j+2] == 0) return (j+3); } // x // x // b // x if(board[i][j] != 0) if ((board[i+2][j+2] == board[i][j]) && (board[i+3][j+3] == board[i][j])) if(board[i][j+1]==0) {} else { if (board[i+1][j+1] == 0) return (j+2); } // x // x // x // b if(board[i+1][j+1] != 0) if ((board[i+2][j+2] == board[i+1][j+1]) && (board[i+3][j+3] == board[i+1][j+1])) if((i>0) && (board[i-1][j]==0)) {} else { if (board[i][j] == 0) return (j+1); } }//end nested for loop for right diagonals //left diagonals for(i=0; i<3; i++) for(j=6; j>2; j--) { // b // x // x // x if(board[i][j] != 0) if ((board[i+1][j-1] == board[i][j]) && (board[i+2][j-2] == board[i][j])) if(board[i+2][j-3]==0) {} else { if (board[i+3][j-3] == 0) return (j-2); } // x // b // x // x if(board[i][j] != 0) if ((board[i+1][j-1] == board[i][j]) && (board[i+3][j-3] == board[i][j])) if(board[i+1][j-2]==0) {} else { if (board[i+2][j-2] == 0) return (j-1); } // x // x // b // x if(board[i][j] != 0) if ((board[i+2][j-2] == board[i][j]) && (board[i+3][j-3] == board[i][j])) if(board[i][j-1]==0) {} else { if (board[i+1][j-1] == 0) return (j); } // x // x // x // b if(board[i+1][j-1] != 0) if ((board[i+2][j-2] == board[i+1][j-1]) && (board[i+3][j-3] == board[i+1][j-1])) if((i>0) && (board[i-1][j]==0)) {} else { if (board[i][j] == 0) return (j+1); } }//end nested for loop for right diagonals return (randnum()%7 + 1); end char hardComputer() // Represents level 3 (of 3) // returns a value based upon the greatest weight of the columns. Weight for the columns depend upon the particular // arrangements of the checkers. Many cases are checked for. The reader may also notice that in each case, several // weights may be assigned. This is depended upon the arrangement of checkers around the case and upon whether the // case represents an offensive or defensive opportunity. For example, a win is given more weight than a block. // However, care must be taken not to set the opponent up for either a win or a block of a win. The philosophy is to // hold off until the opponent (and hopefully not the computer) is FORCED to make a move that will result in the // computer's victory. Given that this more or less results in a win 50% of the time against an astute opponent, // we rely on human imperfection to hopefully boost our win percentage. (we certainly made many dumb mistakes while // debugging this program. { temp2 = 0; for (j = 0; j < 7; j++) bestMove[j] = 0; //ensure that all the weights start at zero // check for a X| |X|X for(i=0;i<6;i++) for(j=0;j<4;j++) { if(board[i][j]!=0) if((board[i][j+2] == board[i][j]) && (board[i][j+3] == board[i][j])) if((i > 1) && (board [i-2][j+1] != 0) && (board[i-1][j+1]==0)) { //a value of -200 represents the fact that a move would either lead to an opponent's win, // or allow the opponent to block a potential win. bestMove[j+1] -= 200; } else if ((i==1) && (board[0][j+1] == 0)) { bestMove[j+1] -= 200; } else { if (board[i][j+1] == 0) { if ( (i==0) || (board[i-1][j+1] != 0) ) { if (board[i][j] == 1) bestMove[j+1] += 100; //100 represents a block else bestMove[j+1] += 500; //500 represents a WIN!! } else bestMove[j+1] += 50; //50 represents that we can set ourselves up } } } // check for a X|X| |X for(i=0;i<6;i++) for(j=0;j<4;j++) { if(board[i][j]!=0) if((board[i][j+1] ==board[i][j]) && (board[i][j+3]==board[i][j])) if((i>1) && (board [i-2][j+2] != 0) && (board[i-1][j+2]==0)) { bestMove[j+2] -= 200; } else if ((i == 1) && (board[0][j+2] == 0)) { bestMove[j+2] -= 200; } else { if (board[i][j+2] == 0) { if ( (i==0) || (board[i-1][j+1] != 0) ) { if(board[i][j] == 1) bestMove[j+2] += 100; else bestMove[j+2] += 500; } else bestMove[j+2] += 50; } } } // check for a X|X|X| | for(i=0; i<6; i++) for(j=0; j<4; j++) { if(board[i][j] != 0) if ((board[i][j+1] == board[i][j]) && (board[i][j+2] == board[i][j])) if((i>1) && (board [i-2][j+3] != 0) && (board[i-1][j+3]==0)) { bestMove[j+3] -= 200; } else if ((i == 1) && (board[0][j+3] == 0)) { bestMove[j+3] -= 200; } else { if (board[i][j+3] == 0) { if ( (i==0) || (board[i-1][j+1] != 0) ) { if (board[i][j] == 1) bestMove[j+3] += 100; else bestMove[j+3] += 500; } else bestMove[j+3] += 50; } } } // check for a | |X|X|X for(i=0; i<6; i++) { for(j=0; j<4; j++) { if(board[i][j+3] != 0) if ((board[i][j+1] == board[i][j+3]) && (board[i][j+2] == board[i][j+3])) if((i>1) && (board [i-2][j] != 0) && (board[i-1][j]==0)) { bestMove[j] -= 200; } else if ((i == 1) && (board[0][j] == 0)) { bestMove[j] -= 200; } else { if (board[i][j] == 0) { if ( (i==0) || (board[i-1][j+1] != 0) ) { if (board[i][j+3] == 1) bestMove[j] += 100; else bestMove[j] += 500; } else bestMove[j] += 50; } } } } //check three in a row vertical for(i=0; i<3; i++) { for (j = 0; j < 7; j++) if (board[i][j] != 0) if ((board[i+1][j] == board[i][j]) && (board[i+2][j] == board[i][j])) if (board[i+3][j] == 0) { if (board[i][j] == 1) bestMove[j] += 100; else bestMove[j] += 500; } } //right diagonals for(i=0; i<3; i++) for(j=0; j<4; j++) { // b // x // x // x if(board[i][j] != 0) if ((board[i+1][j+1] == board[i][j]) && (board[i+2][j+2] == board[i][j])) { if ((board[i+2][j+3] == 0) && (board[i+1][j+3] != 0)) { bestMove[j+3] -= 200; } else { if (board[i+3][j+3] == 0) { if (board[i+2][j+3] != 0) { if (board[i][j] == 1) bestMove[j+3] += 100; else bestMove[j+3] += 500; } else bestMove[j+3] += 50; } } } // x // b // x // x if(board[i][j] != 0) if ((board[i+1][j+1] == board[i][j]) && (board[i+3][j+3] == board[i][j])) { if ((board[i+1][j+2] == 0) && (board[i][j+2] != 0)) { bestMove[j+2] -= 200; } else { if (board[i+2][j+2] == 0) { if (board[i+1][j+2] != 0) { if (board[i][j] == 1) bestMove[j+2] += 100; else bestMove[j+2] += 500; } else bestMove[j+2] += 50; } } } // x // x // b // x if(board[i][j] != 0) if ((board[i+2][j+2] == board[i][j]) && (board[i+3][j+3] == board[i][j])) { if (((i>0) && (board[i][j+1] == 0) && (board[i-1][j+1] != 0)) || ((i==0) && (board[i][j+1] == 0))) { bestMove[j+1] -= 200; } else { if (board[i+1][j+1] == 0) { if (board[i][j+1] != 0) { if (board[i][j] == 1) bestMove[j+1] += 100; else bestMove[j+1] += 500; } else bestMove[j+1] += 50; } } } // x // x // x // b if(board[i+1][j+1] != 0) if ((board[i+2][j+2] == board[i+1][j+1]) && (board[i+3][j+3] == board[i+1][j+1])) { if( (i>0) && (board[i-1][j] == 0) ) { bestMove[j] -= 200; } else { if (board[i][j] == 0) { if ((i > 0) && (board[i-1][j] != 0)) { if (board[i+1][j+1] == 1) bestMove[j] += 100; else bestMove[j] += 500; } else bestMove[j] += 50; } } } }//end nested for loop for right diagonals //left diagonals for(i=0; i<3; i++) for(j=0; j<4; j++) { // b // x // x // x if(board[i][j+3] != 0) if ((board[i+1][j+2] == board[i][j+3]) && (board[i+2][j+1] == board[i][j+3])) { if ((board[i+2][j] == 0) && (board[i+1][j] != 0)) { bestMove[j] -= 200; } else { if (board[i+3][j] == 0) { if (board[i+1][j] != 0) { if (board[i][j+3] == 1) bestMove[j] += 100; else bestMove[j] += 500; } else bestMove[j] += 50; } } } // check for left diagonal case 2 // x // b // x // x if(board[i][j+3] != 0) if ((board[i+1][j+2] == board[i][j+3]) && (board[i+3][j] == board[i][j+3])) { if ((board[i+1][j+1] == 0) && (board[i][j+1] != 0)) { bestMove[j+1] -= 200; } else { if (board[i+2][j+1] == 0) { if (board[i][j+1] != 0) { if (board[i][j+3] == 1) bestMove[j+1] += 100; else bestMove[j+1] += 500; } else bestMove[j] += 50; } } } // x // x // b // x if(board[i][j+3] != 0) if ((board[i+2][j+1] == board[i][j+3]) && (board[i+3][j] == board[i][j+3])) { if ( ((i>0) && (board[i][j+2] == 0) && (board[i-1][j+2] != 0)) || ((i==0) && (board[i][j+2] == 0)) ) { bestMove[j+2] -= 200; } else { if (board[i+1][j+2] == 0) { if (board[i][j+2] != 0) { if (board[i][j+3] == 1) bestMove[j+2] += 100; else bestMove[j+2] += 500; } else bestMove[j+2] += 50; } } } // x // x // x // b if(board[i+1][j+2] != 0) if ((board[i+2][j+1] == board[i+1][j+2]) && (board[i+3][j] == board[i+1][j+2])) { if ( (i>0) && (board[i-1][j+3] == 0) ) { bestMove[j+3] -= 200; } else { if (board[i][j+3] == 0) { if ((i>0) && (board[i-1][j+3] != 0)) { if (board[i+1][j+2] == 1) bestMove[j+3] += 100; else bestMove[j+3] += 500; } else bestMove[j+3] += 50; } } } }//end nested for loop for left diagonals // check for 2 in horizontal for(i=0; i<6; i++) for(j=0; j<4; j++) { //cases where we have only two checkers set up for a connect four are given // less wieght, for obvious reasons. Offensive and defensive strategies are // also the same here. if(board[i][j] != 0) { // check case X| | |X if ((board[i][j+3] == board[i][j]) && (board[i][j+1] == 0) && (board[i][j+2] == 0)) if(i>0) { if(board[i-1][j+1] != 0) bestMove[j+1] += 8; if (board[i-1][j+2] != 0) bestMove[j+2] += 8; } else if(i==0) { bestMove[j+1] += 8; bestMove[j+2] += 8; } // check case X| |X| | if ((board[i][j+2] == board[i][j]) && (board[i][j+1] == 0) && (board[i][j+3] == 0)) if(i>0) { if(board[i-1][j+1] != 0) bestMove[j+1] += 8; if (board[i-1][j+3] != 0) bestMove[j+3] += 8; } else if(i==0) { bestMove[j+1] += 8; bestMove[j+3] += 8; } // check case X|X| | | if ((board[i][j+1] == board[i][j]) && (board[i][j+2] == 0) && (board[i][j+3] == 0)) if(i>0) { if(board[i-1][j+2] != 0) bestMove[j+2] += 8; if (board[i-1][j+3] != 0) bestMove[j+3] += 8; } else if(i==0) { bestMove[j+2] += 8; bestMove[j+3] += 8; } } // end if board[i][j]!=0 if(board[i][j+3] != 0) { // check case | | |X|X if ((board[i][j] == 0) && (board[i][j+1] == 0) && (board[i][j+2] == board[i][j+3])) if(i>0) { if(board[i-1][j] != 0) bestMove[j] += 8; if (board[i-1][j+1] != 0) bestMove[j+1] += 8; } else if(i==0) { bestMove[j] += 8; bestMove[j+1] += 8; } // check case | |X| |X if ((board[i][j] == 0) && (board[i][j+1] == board[i][j+3]) && (board[i][j+2] == 0)) if(i>0) { if(board[i-1][j] != 0) bestMove[j] += 8; if (board[i-1][j+2] != 0) bestMove[j+2] += 8; } else if(i==0) { bestMove[j] += 8; bestMove[j+2] += 8; } } // end if board[i][j+3] != 0 // check case | |X|X| | // this case is given a greater weight because of it's potential to lead to // a win on the opponents part if not blocked correctly. For example, let's // say the bottom row is: // O| | |X|X| |O // If O were to put a checker in either columns one or seven, then X will win. if((board[i][j]==0) && (board[i][j+1] != 0) && (board[i][j+1] == board[i][j+2]) && (board[i][j+3]==0)) if(i>0) { if(board[i-1][j] !=0) bestMove[j] += 16; if(board[i-1][j+3] !=0) bestMove[j+3] += 16; } else if(i==0) { bestMove[j] += 16; bestMove[j+3] += 16; } } // end nested for loop horizontal lines // check right diagnal for(i=0; i<3; i++) for(j=0; j<4; j++) { if(board[i][j]!=0) { // x // b // b // x if ((board[i+3][j+3] == board[i][j]) && (board[i+1][j+1] == 0) && (board[i+2][j+2] == 0)) { if(board[i][j+1] != 0) bestMove[j+1] += 8; if (board[i+1][j+2] != 0) bestMove[j+2] += 8; } // b // x // b // x else if ((board[i+1][j+1] == 0) && (board[i+2][j+2] == board[i][j]) && (board[i+3][j+3] == 0)) { if(board[i][j+1] != 0) bestMove[j+1] += 8; if (board[i+2][j+3] != 0) bestMove[j+3] += 8; } // b // b // x // x else if ((board[i+1][j+1] == board[i][j]) && (board[i+2][j+2] == 0) && (board[i+3][j+3] == 0)) { if(board[i+1][j+2] != 0) bestMove[j+2] += 8; if (board[i+2][j+3] != 0) bestMove[j+3] += 8; } } if(board[i+3][j+3] != 0) { // x // x // b // b if ((board[i][j] == 0) && (board[i+1][j+1] == 0) && (board[i+2][j+2] == board[i+3][j+3])) if(i>0) { if(board[i-1][j] != 0) bestMove[j] += 8; if (board[i][j+1] != 0) bestMove[j+1] += 8; } else if(i==0) { bestMove[j] += 8; bestMove[j+1] += 8; } // x // b // x // b else if ((board[i][j] == 0) && (board[i+1][j+1] == board[i+3][j+3]) && (board[i+2][j+2] == 0)) if(i>0) { if(board[i-1][j] != 0) bestMove[j] += 8; if (board[i+1][j+2] != 0) bestMove[j+2] += 8; } else if(i==0) { bestMove[j] += 8; bestMove[j+2] += 8; } } // end if board[i][j+3] != 0 // case | |X|X| | // b // x // x // b if((board[i][j]==0) && (board[i+1][j+1]!=0) && (board[i+1][j+1]==board[i+2][j+2]) && (board[i+3][j+3]==0)) { if(i>0) { if(board[i-1][j]!=0) bestMove[j] += 16; if(board[i+2][j+3]!=0) bestMove[j+3] += 16; } else if(i==0) { bestMove[j] += 16; bestMove[j+3] += 16; } } } // end nested loop for 2 in right diagnal //case for left diagonal for(i=0; i<3; i++) for(j=0; j<4; j++) { if(board[i+3][j]!=0) { // check case X|X| | | // x // x // b // b if((board[i+2][j+1]==board[i+3][j]) && (board[i+1][j+2]==0) && (board[i][j+3]==0)) { if(board[i][j+2]!=0) bestMove[j+2] += 8; if(i>0) { if(board[i-1][j+3]!=0) bestMove[j+3] += 8; } else if(i==0) bestMove[j+3] += 8; } // x // b // x // b if((board[i+2][j+1]==0) && (board[i+1][j+2]==board[i+3][j]) && (board[i][j+3]==0)) { if(board[i+1][j+1]!=0) bestMove[j+1] += 8; if(i>0) { if(board[i-1][j+3]!=0) bestMove[j+3] += 8; } else if(i==0) bestMove[j+3] += 8; } // check case X| | |X // x // b // b // x if((board[i+2][j+1]==0) && (board[i+1][j+2]==0) && (board[i+3][j]==board[i][j+3])) { if(board[i+1][j+1]!=0) bestMove[j+1] += 8; if(board[i][j+2]!=0) bestMove[j+1] += 8; } } if((board[i][j+3]!=0) && (board[i+3][j]==0)) { // b // x // b // x if((board[i+2][j+1]==board[i][j+3]) && (board[i+1][j+2]==0)) { if(board[i+2][j]!=0) bestMove[j] += 8; if(board[i][j+2]!=0) bestMove[j+2] += 8; } // b // b // x // x if((board[i+2][j+1]==0) && (board[i+1][j+2]==board[i][j+3])) { if(board[i+2][j]!=0) bestMove[j] += 8; if(board[i+1][j+1]!=0) bestMove[j+1] += 8; } } // b // x // x // b if ((board[i+3][j]==0) && (board[i+2][j+1]!=0) && (board[i+2][j+1]==board[i+1][j+2]) && (board[i][j+3]==0)) { if(board[i+2][j]!=0) bestMove[j] += 16; if((i>0) && (board[i-1][j+3]!=0)) bestMove[j+3] += 16; else if(i==0) bestMove[j+3] += 16; } } // nested for left daiganol temp = -1270; //make sure that temp is really low, so that we don't mistakenly // miss our best move //calculate the column with the highest value for (j = 0; j < 7; j++) { if (bestMove[j] > temp) { temp = bestMove[j];//temp is the highest value seen thus far temp2 = j; //temp 2 is the column associated with that value. } } j = 0; if (temp == 0) //it's possible (although not probable) that temp is zero, // even though we have assigned weights to various columns. // temp could be zero if, say, we added 100 twice, and then // subtract 200, all from the same column. { while ((j <= 6) && (bestMove[j] == 0)) //do we have any weights? j++; if (j==7) return (randnum() % 7 + 1); //we didn't change any values, //thus we can simply choose a random value. } else { while ((board [5][temp2] != 0) && (check_win() != 3)) temp2 = (randnum() % 7); //Is the column we want to place a checker in // already full? (It shouldn't be, sut this is // a fail safe). If so, choose a random value. return (temp2 + 1); } }//end hardComputer //================================== // 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 = 0b00010100; //enable interrupt T1 cmp //init ports DDRD = 0xf0; //video out and switches DDRB = 0xff; //D.5 is sync:1000 ohm + diode to 75 ohm resistor //D.6 is video:330 ohm + diode to 75 ohm resistor //init timer 0 to 1/uSec TCCR0 = 3; //initialize synch constants LineCount = 1; syncON = 0b00000000; syncOFF = 0b00100000; GameState = initialize; //initialize our state machine. //currentPlayer = 1; //video_puts(0,0,plyr); //init software timer //t=0; //time=0; PORTB = 0xff; //set up PORTB for sound. //init the tones srand(1); note = 3; halfnote = note>>1; tnote = 10; wait = 240; //This is placed here so that our welcome screen will last about // four seconds. //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 switch (GameState) { case initialize: { // Initially, we had animation with our introduction to the program // we had to throw this out to fit all of our AI, though. We left it in for documentation // and to show that we at least attempted more animation then is in the final version of // the code. You'll also notice sound in the commented sections. However, we were able to // squeeze some sound into the final version. //#asm("sleep"); /*if (wait >= 210) { if (wait == 240) video_puts(30,0,welcome); note = notes2[2]; halfnote = note>>1; wait--; GameState = initialize; break; } else if ((wait >= 180)&&(wait < 210)) { note = 0; halfnote = note>>1; wait--; GameState = initialize; break; } else if ((wait >= 165)&&(wait < 180)) { note = notes2[1]; halfnote = note>>1; wait--; if (wait == 170) video_puts(45,40,to); GameState = initialize; break; } else if ((wait >= 135)&&(wait < 165)) { note = notes2[0]; halfnote = note>>1; wait--; GameState = initialize; break; } else if ((wait >= 105)&&(wait < 135)) { note = 0; halfnote = note>>1; wait--; GameState = initialize; break; } else if ((wait >= 95)&&(wait < 105)) { note = notes2[2]; halfnote = note>>1; wait--; GameState = initialize; break; } else if ((wait >= 90)&&(wait < 95)) { if (wait == 94) video_puts(20,80, connect4); note = 0; halfnote = note>>1; wait--; GameState = initialize; break; } else if ((wait >= 75)&&(wait < 90)) { note = notes2[2]; halfnote = note>>1; wait--; GameState = initialize; break; } else if ((wait >= 45)&&(wait < 75)) { note = notes2[3]; halfnote = note>>1; wait--; GameState = initialize; break; } else if ((wait >= 8)&&(wait < 45)) { note = 0; halfnote = note>>1; wait--; GameState = initialize; break; } */ if (wait == 210) { // The initial welcome screen stay up for 3.5 seconds (approximately). video_puts(30,0,welcome); video_puts(45,30,to); note = notes2[2]; } else if (wait == 208) { video_puts(20,60,connect4); } // The sound below is an attempt to replicate the theme song from Milton // we didn't get very close, however, and so we don't violate any copyright laws. else if (wait == 195) note = 0; else if (wait == 175) note = notes2[1]; else if (wait == 163) note = 0; else if (wait == 160) note = notes2[0]; else if (wait == 145) note = 0; else if (wait == 85) note = notes2[2]; else if (wait == 73) note = 0; else if (wait == 70) note = notes2[2]; else if (wait == 55) note = 0; else if (wait == 30) note = notes2[3]; else if (wait == 15) note = 0; else if (wait == 9) { // erase the welcome screen // You will see many "GameState" commands commented out throughout the code. These are // not necessary and were elimanted for space. However, we wanted to be explicit about // when to stay within the same state across multiple frames, and thus they were left commented // as opposed to simlpy erased. This should hopefully make the code easier to understand. //GameState = initialize; video_puts(30,0,messageBlank); video_puts(45,30,messageBlank); video_puts(20,60,messageBlank); video_puts(32,60,messageBlank); } else if (wait == 8) { //draw the horizontal lines for the board video_line (0,10, 70, 10, 1); video_line (0,20, 70, 20, 1); video_line (0,30, 70, 30, 1); note = 0; //GameState = initialize; } else if (wait == 7) { video_line (0,40, 70, 40, 1); video_line (0,50, 70, 50, 1); video_line (0,60, 70, 60, 1); video_line (0,70, 70, 70, 1); //GameState = initialize; } else if (wait == 6) { //draw the vertical lines for the board video_line (0, 10, 0, 70, 1); video_line (10, 10, 10, 70, 1); video_line (20, 10, 20, 70, 1); video_line (30, 10, 30, 70, 1); //GameState = initialize; } else if (wait == 5) { video_line (40, 10, 40, 70, 1); video_line (50, 10, 50, 70, 1); video_line (60, 10, 60, 70, 1); video_line (70, 10, 70, 70, 1); //GameState = initialize; } //put numbers at the bottom of the columns to make life easier for the user. else if (wait == 4) { video_putchar(2, 72, 1); video_putchar(12, 72, 2); video_putchar(22, 72, 3); video_putchar(32, 72, 4); //GameState = initialize; } else if (wait == 3) { video_putchar(42, 72, 5); video_putchar(52, 72, 6); video_putchar(62, 72, 7); //GameState = initialize; } else if (wait == 0) { video_puts(0,90,numPlayers); //prompt for the number of users maybe = keypad(); //used for debouncing GameState = initDebounce; //go to the initdebounce state debounceCounter = 2; //used for debouncing (ensure that the button // is pressed for 1/30 seconds m=1; //signifies the first move of the game } wait--; //decrease the weight counter halfnote = note>>1; break; } case initDebounce: //debounce the player choice state { if(keypad() == maybe) { if (debounceCounter == 0) //we're now debounced { if (maybe == 1) //user wants a one player game { players = 1; currentPlayer = 1; GameState = notPushed; //debounce again video_puts(0,90,messageBlank); } else if (maybe == 2) //user wants a two player game { players = 2; currentPlayer = 1; GameState = promptForUser; //don't need to prompt for a leve, //so just start the game wait = 2; video_puts(0,90,messageBlank); //erase the "nmbr of plyrs" message } else { maybe = keypad(); //something got messed up, start the debouncing process // over again. debounceCounter == 2; //GameState = initDebounce; } } else { debounceCounter--; //GameState = initDebounce; } break; } else { maybe = keypad(); //nothing has been pressed yet, so check again //GameState = initDebounce; break; } } //end case initDebounce case notPushed: //This case ensures that the user moves off of the keypad between the players // and levels selection { video_puts(0,0,messageBlank); if (keypad() == 0) //nothing is being pressed, move on and ask for a level. // No need to debounce. { GameState = promptLevel; } else { //GameState = notPushed; } break; } case promptLevel: //For a one-player game, ask what level the user wants. { video_puts(0,90,levels); //put the levels message at the bottom of the screen maybe = keypad(); //let's debounce the button GameState = debounceLevel; //move to the debounce level. debounceCounter = 2; break; }//end case prompt level case debounceLevel: { if(keypad() == maybe) { if (debounceCounter == 0) //we're debounced { if (maybe != 0) //the user has inputted something, so erase the levels // string at the bottom of the screen video_puts(0,90,messageBlank); if (maybe == 1) //level 1 { level = 1; currentPlayer = 1; //user starts GameState = promptForUser; wait = 2; } else if (maybe == 2) //level 2 { level = 2; currentPlayer = 1; //user starts GameState = promptForUser; wait = 2; } else if (maybe == 3) //level 3 { level = 3; currentPlayer = 2; //computer starts GameState = promptForUser; wait = 2; } else GameState = promptLevel; //a value other than 1-3 was seen at the keyboard } else { debounceCounter--; //GameState = debounceLevel; } break; } else { GameState = promptLevel; //we're not debounced break; } } //end case debounceLevel case promptForUser: { if (wait == 2) //commands need to be put on seperate frames to avoid flicker // and artifacts. { video_putchar(40,0,currentPlayer); //Write who's turn it is on the screen //GameState = promptForUser; } else if (wait == 1) { video_puts(0,0,plyr); //write the "player" string to the screen //video_line(0,10,0,20,1); } else { GameState = waitForUser; //the following commented code was used to taunt the user. It was eliminated // to conserve space. /*j = randnum() % 11; if ( ( j < 5) && (players == 1) ) { if ((currentPlayer == 1) && (level < 3)) { if (j == 0) video_puts(0,80,message1); else if (j == 1) video_puts(0,80,message2); else if (j == 2) video_puts(0,80,message3); else if (j == 3) video_puts(0,80,message4); else if (j == 4) video_puts(0,80,message5); } else { video_puts(0,80,messageBlank); } } else { video_puts(0,80,messageBlank); } */ } wait--; break; }// end case promptForUser case waitForUser: //wait for the user to input something to the keypad { if (currentPlayer == 1) //first players turn { if(keypad() == 0) //we're still waiting for the user { //GameState = waitForUser; break; } else { maybe = keypad(); //see what's at the keypad GameState = debounce; //debounce the keypad debounceCounter = 2; //wait for 1/30 seconds to debounce break; } } else //second players turn { if (players == 1) //computers turn { if (level == 1) currentMove = easyComputer(); //get a move for level1 else if (level == 2) currentMove = mediumComputer(); //move for level 2 else { if (m == 1){ //If we're on level 3 and we're at the first move of the game, // always put the first checker at the center of the board. This // is a simple strategic move to help control the center of the board. currentMove = 4; //go into column four m++; //first move is done } else currentMove = hardComputer(); //use the AI to pick the best column } GameState = isValid; //make sure the move chosen is a valid move break; } else //human's turn { if(keypad() == 0) //we're still waiting for the user { //GameState = waitForUser; break; } else { maybe = keypad(); //see what the human has pressed GameState = debounce; //debounce the keypad debounceCounter = 2; break; } } //end human's turn } //end second player's turn break; } //end case waitForUser case debounce: // This case debounces the keypad. { if(keypad() == maybe) { if (debounceCounter == 0) //we're debounced { currentMove = maybe; //the column we want to move into is set to the // value pressed on the keypad. GameState = isValid; //check to make sure the move is valid. } else { debounceCounter--; //GameState = debounce; } break; } else { GameState = waitForUser; //not debounced - try again. break; } } //end case debounce case isValid: { currentRow = add_piece(currentMove, currentPlayer); if((currentMove > 8) || (currentRow == -1) || (currentMove < 1) && (check_win() != 3)) { //The move is not valid, so try again GameState = promptForUser; wait = 2; break; } else { //the move is valid, so we can write the piece onto the board. GameState = draw; drawCounter = 5 - currentRow; //# of times to draw the piece (for animation) waittoDraw = 10; //slow the animation down a bit. break; } } //end case isValid case draw: { //note = notes[5]; //halfnote = note>>1; if(waittoDraw>0) { waittoDraw--; //GameState = draw; } else { waittoDraw = 10; note = notes[drawCounter + 8]; //play a note, value changes depending on // row and how far the piece is to be dropped. halfnote = note>>1; s = currentRow +drawCounter; //useful for determining the y-coord of the piece t = currentMove*10 - 8; // x-coord for the piece if (drawCounter>0) { video_putchar(t, 62 - 10*(s), 37 + currentPlayer); //draw the appropriate //checker if (s < 5) { video_putchar(t, 62 - 10*(s+1), 40); //erase the previous checker // 40 is the blank character. } drawCounter--; //GameState = draw; } else { //draw the final checker. video_putchar(t, 62 - 10*currentRow, 37 + currentPlayer); if (s < 5) { video_putchar(t, 62 - 10*(s+1), 40); } GameState = checkFinish; wait = 3; } } break; } //end case draw case checkFinish: //Check to see if the game is done { note = 0; //turn of the sound generating during the drawing state. halfnote = 0; i = check_win(); if (i != 0) //the game is over { if (i == 3) //the game is tied { video_puts(0,0,tie); video_puts(0,80,reset9); GameState = Finish; break; } else if (wait == 2) //the game is not tied, tell the user(s) who won. { video_puts(50,0,win); } else if (wait == 1) { video_puts(0,80,reset9); GameState = Finish; } wait--; break; } else //the gmae continues... { if (currentPlayer == 1) //switch the players currentPlayer = 2; else currentPlayer = 1; GameState = promptForUser; //prompt the new user to go wait = 2; break; } } //end case checkFinish case Finish: //and now it's time to say goodbye to all our family { if (keypad() == 9) //the user has pressed nine, and wants to reset { GameState = Restart; } else { //GameState = Finish; //the user hasn't reset the game yet, so we'll sit here and wait for them // to get their act together. } break; } //end case finish case Restart: //Start a new game { for (i=0; i<6; i++) for (j = 0; j<7; j++) board[i][j] = 0; //erase the board the mcu sees for (k = 0; k < 1600; k++) screen[k] = 0; //clear the video screen wait = 9; //go to the initialize state, but start after the welcome screen. // Essentially, just re-draw the board. GameState = initialize; //video_puts(0,0,plyr); break; } }//end switch statement end //line 231 end //while end //main