Miheer Bavare / Mbed 2 deprecated Chess

Dependencies:   4DGL-uLCD-SE PinDetect SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "uLCD_4DGL.h"
00003 #include "SDFileSystem.h"
00004 #include <algorithm>
00005 #include "PinDetect.h"
00006 #include "Speaker.h"
00007 
00008 uLCD_4DGL uLCD(p28,p27,p29); // serial tx, serial rx, reset pin;
00009 
00010 //bluetooth for the player two controller
00011 Serial blue(p13,p14); //serial tx, rx
00012 
00013 //Using the speaker as a pwm out
00014 Speaker mySpeaker(p21);
00015 
00016 //set up the chess board as an int array
00017 int board[8][8]={0};
00018 
00019 //Set the pieces so we can refer to them by name
00020 const int pawn=1;
00021 const int rook=2;
00022 const int knight=3;
00023 const int bishop=4;
00024 const int queen=5;
00025 const int king=6;
00026 
00027 //global variables to keep track of the current player and the winner
00028 int winner = 0;
00029 int current_player = 1;
00030 
00031 //class declaration for the navswitch
00032 class Nav_Switch
00033 {
00034 public:
00035     Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire);
00036     int read();
00037 //boolean functions to test each switch
00038     bool up();
00039     bool down();
00040     bool left();
00041     bool right();
00042     bool fire();
00043 //automatic read on RHS
00044     operator int ();
00045 //index to any switch array style
00046     bool operator[](int index) {
00047         return _pins[index];
00048     };
00049 private:
00050     BusIn _pins;
00051 
00052 };
00053 Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
00054 _pins(up, down, left, right, fire)
00055 {
00056     _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise
00057     wait(0.001); //delays just a bit for pullups to pull inputs high
00058 }
00059 inline bool Nav_Switch::up()
00060 {
00061     return !(_pins[0]);
00062 }
00063 inline bool Nav_Switch::down()
00064 {
00065     return !(_pins[1]);
00066 }
00067 inline bool Nav_Switch::left()
00068 {
00069     return !(_pins[2]);
00070 }
00071 inline bool Nav_Switch::right()
00072 {
00073     return !(_pins[3]);
00074 }
00075 inline bool Nav_Switch::fire()
00076 {
00077     return !(_pins[4]);
00078 }
00079 inline int Nav_Switch::read()
00080 {
00081     return _pins.read();
00082 }
00083 inline Nav_Switch::operator int ()
00084 {
00085     return _pins.read();
00086 }
00087 
00088 Nav_Switch p1( p9, p6, p7, p5, p8); //pin order on Sparkfun breakout
00089 
00090 //set the MBED leds up to signal information
00091 DigitalOut myLed1(LED1);
00092 DigitalOut myLed2(LED2);
00093 DigitalOut myLed3(LED3);
00094 DigitalOut myLed4(LED4);
00095 
00096 //functions for drawing the different chess pieces using uLCD member functions
00097 void drawPawn(int x, int y, int Color) {
00098     uLCD.filled_circle(x*16+8,y*16+5,2,Color);
00099     uLCD.triangle(x*16+8,y*16+5,x*16+4,y*16+12,x*16+12,y*16+12,Color);    
00100 }
00101 void drawQueen(int x, int y, int Color) {
00102     uLCD.filled_rectangle(x*16+4,y*16+6, x*16+12, y*16+12, Color);
00103     uLCD.triangle(x*16+4,y*16+6,x*16+4,y*16+2,x*16+7,y*16+6,Color);
00104     uLCD.triangle(x*16+7,y*16+6,x*16+8.5,y*16+2,x*16+10,y*16+6,Color);
00105     uLCD.triangle(x*16+10,y*16+6,x*16+12,y*16+6,x*16+12,y*16+2,Color);
00106 }
00107 void drawKing(int x,int y, int Color) {
00108     uLCD.filled_rectangle(x*16+7,y*16+2,x*16+9,y*16+12,Color);
00109     uLCD.filled_rectangle(x*16+4,y*16+6,x*16+12,y*16+8,Color);    
00110 }
00111 void drawKnight(int x, int y, int Color) {
00112     uLCD.triangle(x*16+8,y*16+2,x*16+4,y*16+4,x*16+8,y*16+4,Color);
00113     uLCD.filled_rectangle(x*16+7, y*16+2, x*16+9, y*16+10, Color);
00114     uLCD.filled_rectangle(x*16+4, y*16+10, x*16+12, y*16+ 12, Color);
00115 }
00116 void drawRook(int x, int y, int Color) {
00117     uLCD.filled_rectangle(x*16+2, y*16+4, x*16+ 14, y*16+6,Color);
00118     uLCD.filled_rectangle(x*16+5, y*16+6, x*16+11, y*16+12, Color);
00119     uLCD.filled_rectangle(x*16+2, y*16+12, x*16+14,y*16+14,Color);
00120     uLCD.filled_rectangle(x*16+3, y*16+2, x*16+5, y*16+4,Color);
00121     uLCD.filled_rectangle(x*16+7, y*16+2, x*16+9, y*16+4,Color);
00122     uLCD.filled_rectangle(x*16+11, y*16+2,x*16+13,y*16+4,Color);
00123 }
00124 void drawBishop(int x, int y, int Color) {
00125     uLCD.filled_rectangle(x*16+4,y*16+10, x*16+12, y*16+12,Color);
00126     uLCD.filled_rectangle(x*16+7, y*16+4,x*16+9,y*16+10,Color);
00127     uLCD.triangle(x*16+5,y*16+4,x*16+11,y*16+4,x*16+8,y*16,Color);    
00128 }
00129 
00130 //display_board sets up the board in the beginning of the game
00131 void display_board(void)
00132 {
00133     //this loop draws the board squares, alternating color 
00134     bool alternator = true;
00135     for(int i=0; i<8; i++)//This loop will be executed for each row once
00136     {
00137         for(int j=0; j<8; j++) //This loop will be executed 7 times for each row
00138         {
00139             if (alternator) {
00140                 uLCD.filled_rectangle(j*16,i*16,(j*16)+16,(i*16)+16, 0xbcb6b8); //draws a light tan (white) square
00141                 alternator = false;
00142             } else {
00143                 uLCD.filled_rectangle(j*16,i*16,(j*16)+16,(i*16)+16, 0x645659); //draws a dark brown (black) square
00144                 alternator = true;
00145             }
00146         }
00147         alternator = !alternator;
00148     }
00149     
00150     //draw the pieces in their initial placement and update the board array
00151     for(int i=0;i<8;i++) //set the pawns up in a for loop
00152     {
00153         drawPawn(i,1,BLACK);
00154         board[i][1]= -pawn;
00155         drawPawn(i,6,WHITE);
00156         board[i][6]= pawn;
00157     }
00158     
00159     drawRook(0,0,BLACK);
00160     drawRook(7,0,BLACK);
00161     board[0][0]=-rook;
00162     board[7][0]=-rook;
00163 
00164     drawRook(0,7,WHITE);
00165     drawRook(7,7,WHITE);
00166     board[0][7]=rook;
00167     board[7][7]=rook;
00168 
00169     drawKnight(1,0,BLACK);
00170     drawKnight(6,0,BLACK);
00171     board[1][0]=-knight;
00172     board[6][0]=-knight;
00173     
00174     drawKnight(1,7,WHITE);
00175     drawKnight(6,7,WHITE);
00176     board[1][7]=knight;
00177     board[6][7]=knight;
00178     
00179     drawBishop(2,0,BLACK);
00180     drawBishop(5,0,BLACK);
00181     board[2][0]=-bishop;
00182     board[5][0]=-bishop;
00183     
00184     drawBishop(2,7,WHITE);
00185     drawBishop(5,7,WHITE);
00186     board[2][7]=bishop;
00187     board[5][7]=bishop;
00188     
00189     drawQueen(3,0,BLACK);
00190     board[3][0]=-queen;
00191     
00192     drawQueen(3,7,WHITE);
00193     board[3][7]=queen;
00194     
00195     drawKing(4,0,BLACK);
00196     board[4][0]=-king;
00197     
00198     drawKing(4,7,WHITE);
00199     board[4][7]=king;    
00200 }
00201 //globals for castling - keep track of if the pieces have moved 
00202 //false indicates they have not moved, mark true on a monement
00203 bool king1 = false;
00204 bool king2 = false;
00205 bool rook11 = false;
00206 bool rook12 = false;
00207 bool rook21 = false;
00208 bool rook22 = false;
00209 bool castling = false; //set this flag high in validMove so we know in movePiece
00210 
00211 //movePiece takes in a piece coordinate and moves it to a new spot
00212 void movePiece(int ox, int oy, int dx, int dy, int player) {
00213 
00214     //first clear the piece in the destination if a piece is being captured
00215     if(board[dx][dy] !=0) {
00216         if( (dx+dy)%2==0) { //moving to a white square
00217             uLCD.filled_rectangle(dx*16,dy*16,(dx*16)+16,(dy*16)+16, 0xbcb6b8);
00218 
00219         }
00220         else { //moving to a dark square
00221             uLCD.filled_rectangle(dx*16,dy*16,(dx*16)+16,(dy*16)+16, 0x645659);
00222         }
00223     }
00224 
00225     //blank out the old square
00226     if( (ox+oy)%2==0) { //moving from a white square
00227         uLCD.filled_rectangle(ox*16,oy*16,(ox*16)+16,(oy*16)+16, 0xbcb6b8);
00228 
00229     }
00230     else { //moving from a dark square
00231         uLCD.filled_rectangle(ox*16,oy*16,(ox*16)+16,(oy*16)+16, 0x645659);
00232     }   
00233     
00234     //draw the piece in the new square
00235     switch (abs(board[ox][oy])) {  //switch based on the piece type
00236         case pawn:
00237             //if a pawn reaches the end of the line then promote it to a queen
00238             if(current_player == 1 && dy == 0) { //white pawn reaches end of file
00239                 board[ox][oy] = queen;
00240                 drawQueen(dx,dy,player);
00241             }
00242             
00243             else if(current_player == 2 && dy == 7) { //black pawn reaches end of file
00244                 board[ox][oy] = -queen;
00245                 drawQueen(dx,dy,player);
00246             }
00247             else
00248                 drawPawn(dx,dy,player);
00249         break;
00250         case rook:
00251             drawRook(dx,dy,player);
00252             //this marks if rooks have moved or not (can only castle if false)
00253             if(oy == 0) {
00254                 if(ox == 0)  //black left rook
00255                     rook21 = true;
00256                 else if(ox==7) //black right rook
00257                     rook22 = true;
00258             }
00259             else if(oy == 7) {
00260                 if(ox == 0)  //white rook 1
00261                     rook11 = true;
00262                 else if(ox==7) //white rook 2
00263                     rook12 = true; 
00264             }
00265         break;    
00266         
00267         case knight:
00268             drawKnight(dx,dy,player);
00269         break; 
00270         
00271         case bishop:
00272             drawBishop(dx,dy,player);
00273         break;
00274         
00275         case queen:
00276             drawQueen(dx,dy,player);
00277         break; 
00278         
00279         case king:  //for king we also need to mark the king as moved 
00280             if(current_player == 1) 
00281                 king1 = true; 
00282             else
00283                 king2 = true;
00284             drawKing(dx,dy,player);
00285         break;         
00286     }
00287 
00288     //play two notes for a piece capture or one for a regular move
00289     if(board[dx][dy] !=0)
00290     {
00291         mySpeaker.PlayNote(500.0,0.1,0.05);
00292         mySpeaker.PlayNote(700.0,0.1,0.05);
00293     }
00294     else
00295         mySpeaker.PlayNote(600.0,0.1,0.05);
00296 
00297     //if the king is captured then mark then mark the correct player as winning
00298     if(board[dx][dy] == 6)
00299         winner = 2;
00300     else if(board[dx][dy] == -6)
00301         winner=1;
00302 
00303     //adjust the board array for the piece movement
00304     board[dx][dy] = board[ox][oy];
00305     board[ox][oy] = 0;
00306 
00307     //if we're castling we also need to move the rook
00308     //first mark castling false then make a recursive call to the movePiece function to move the rook
00309     if(castling) { 
00310         castling = false; 
00311         if(dx<ox) { //castling left
00312             movePiece(0,oy,3,oy, player); 
00313         }   
00314         else { //castling right
00315             movePiece(7,oy,5,oy, player);
00316         }
00317     }
00318 }
00319 
00320 //this function takes in the location of a piece and the destination and outputs whether or not it is a valid move
00321 int validMove(int ox, int oy, int dx, int dy)
00322 {
00323   int source = board[ox][oy];
00324   int target = board[dx][dy];
00325   int typePiece = abs(source);
00326   //taking one of your own pieces is not allowed
00327   if (board[ox][oy] > 0 && board[dx][dy]>0 || board[ox][oy] < 0 && board[dx][dy]<0)
00328     return 0;
00329 
00330     //movement rules for pawns
00331     if (typePiece == pawn) {
00332         if(current_player == 2 && dx==ox && dy-oy==1 && target==0) return 1;
00333         if(current_player == 1 && dx==ox && dy-oy==-1 && target==0) return 1;
00334         if(abs(dx-ox)==1 && current_player == 2 && dy-oy==1 && target > 0) return 1;
00335         if(abs(dx-ox)==1 && current_player == 1 && dy-oy==-1 && target < 0) return 1;
00336         if(current_player == 2 && oy==1 && (abs(ox-dx)==0) && abs(oy-dy)==2 && target == 0) return 1;
00337         if(current_player == 1 && oy==6 && (abs(ox-dx)==0) &&(abs(oy-dy)==2) && target == 0) return 1;
00338  
00339     //movement rules for rooks    
00340     } else if (typePiece == rook) {
00341         if(dx==ox){ // Moving vertically
00342             if(dy>oy){ // Downards
00343                 for(size_t row = (unsigned)(oy+1); row < (unsigned)dy; ++row){
00344                     if(board[dx][row] != 0) return 0;
00345                 }
00346             } else { // Upwards
00347                 for(size_t row = (unsigned)dy+1; row < (unsigned)(oy); ++row){
00348                 if(board[dx][row] != 0) return 0;
00349                 } 
00350             } 
00351             return 1;
00352         }
00353         if(dy==oy){ // Moving horizontally
00354             if(dx>ox){ // Rightwards
00355                 for(size_t column = (unsigned)(ox+1); column < (unsigned)dx; ++column){
00356                     if(board[column][dy] != 0) return 0;
00357                 }
00358             }
00359             if(dx<ox){ // Leftwards
00360                 for(size_t column = (unsigned)dx+1; column < (unsigned)(ox); ++column){
00361                     if(board[column][dy] != 0) return 0;
00362                 }
00363             }
00364             return 1;
00365         }
00366         return 0;
00367 
00368     //movement rules for the knight
00369     } else if (typePiece == knight) {
00370 
00371         if((abs(dy-oy)==2 && abs(dx-ox)==1) || (abs(dx-ox)==2 && abs(dy-oy)==1)){
00372             return 1;
00373         }
00374         return 0;
00375 
00376     //movement rules for bishops    
00377     } else if (typePiece == bishop) {
00378         if (abs(dx-ox) != abs(dy-oy)) //must move diagonally
00379             return 0;
00380         int ymult = -1;
00381         int xmult = -1;
00382         if (dx>ox)
00383             xmult = 1;
00384         if (dy>oy)
00385             ymult = 1;
00386         //check all the spots between location and destination on the diagonal for pieces
00387         for(int i=1; i < abs(dx-ox); ++i)
00388         {
00389             if(board[ox + i*xmult][oy+i*ymult] != 0) return 0;
00390         }
00391         return 1;
00392 
00393     //movement rules for queens
00394     } else if (typePiece ==queen) {
00395         // Queen
00396         if(abs(dx-ox) == abs(dy-oy)) { //diagonal movement
00397             int ymult = -1;
00398             int xmult = -1;
00399             if (dx>ox)
00400                 xmult = 1;
00401             if (dy>oy)
00402                 ymult = 1;
00403 
00404             for(int i=1; i < abs(dx-ox); ++i)
00405             {
00406                 if(board[ox + i*xmult][oy+i*ymult] != 0) return 0;
00407             }
00408         return 1;             
00409         }
00410 
00411         
00412         if(dx==ox){ // Moving vertically
00413             if(dy>oy){ // Downards
00414                 for(size_t row = (unsigned)(oy+1); row < (unsigned)dy; ++row){
00415                     if(board[dx][row] != 0) return 0;
00416                 }
00417             } else { // Upwards
00418                 for(size_t row = (unsigned)dy+1; row < (unsigned)(oy); ++row){
00419                     if(board[dx][row] != 0) return 0;
00420                 }
00421             }
00422             return 1;
00423         }
00424         if(dy==oy){ // Moving horizontally
00425             if(dx>ox){ // Rightwards
00426                 for(size_t column = (unsigned)(ox+1); column < (unsigned)dx; ++column){
00427                     if(board[column][dy] != 0) return 0;
00428                 }
00429             }
00430             if(dx<ox){ // Leftwards
00431                 for(size_t column = (unsigned)dx+1; column < (unsigned)(ox); ++column){
00432                     if(board[column][dy] != 0) return 0;
00433                 }
00434             }
00435             return 1;
00436         }
00437         return 0;
00438 
00439     //movement rules for a king
00440     } else if (typePiece ==king) {
00441 
00442         //special exception for castling
00443         if(dy == oy && ox-2 == dx && (!king1 && current_player == 1 && !rook11 || !king2 && current_player==2 && !rook21))
00444         {
00445             if(board[ox-1][oy] ==0 && board[ox-2][oy] ==0 && board[ox-3][oy]==0) {
00446                 castling = true;
00447                 return 1; //king can castle to the left
00448             }
00449         }
00450         else if(dy == oy && ox + 2 == dx && (!king1 && current_player == 1 && !rook12 || !king2 && current_player==2 && !rook22))
00451         {
00452             if(board[ox+1][oy] ==0 && board[ox+2][oy] ==0){
00453                 castling = true;
00454                 return 1; //king can castle to the right
00455             }
00456         }
00457 
00458         if(abs(dy-oy)<=1 && abs(dx-ox)<=1) return 1;
00459         return 0;
00460     }
00461 
00462     return 0; 
00463 }
00464 
00465 //naming the pins for the joystick
00466 PinDetect up(p26);
00467 PinDetect center(p25);
00468 PinDetect left(p24);
00469 PinDetect down(p23);
00470 PinDetect right(p22);
00471 
00472 
00473 //variables for the joystick interrupt functions
00474 volatile int x = 4;
00475 volatile int y = 6;
00476 volatile int x2 = 4;
00477 volatile int y2 = 2;
00478 volatile bool center_hit = false;
00479 
00480 //joystick interrupt functions that control which square is selected
00481 void up_hit_callback(void) {
00482     --y;
00483     if(y<0)
00484         y=7;
00485 }  
00486 void down_hit_callback(void) {
00487     y = (y+1)%8;
00488 }
00489 void left_hit_callback(void) {
00490     --x;
00491     if(x<0)
00492         x=7;
00493 }
00494 void right_hit_callback(void) {
00495     x = (x+1)%8;
00496 }
00497 void center_hit_callback(void) {
00498     center_hit = true;
00499 }
00500 
00501 int main() {
00502 
00503     //set up the interrupt functions for the joystick
00504     center.attach_deasserted(&center_hit_callback);
00505     left.attach_deasserted(&left_hit_callback);
00506     right.attach_deasserted(&right_hit_callback);
00507     up.attach_deasserted(&up_hit_callback);
00508     down.attach_deasserted(&down_hit_callback);
00509     center.setSampleFrequency();
00510     left.setSampleFrequency();
00511     right.setSampleFrequency();
00512     down.setSampleFrequency();
00513     up.setSampleFrequency();
00514 
00515     //setup the board
00516     display_board();
00517 
00518     //set the default cursor locations
00519     int oldx = 4;
00520     int oldx2 = 4;
00521     int oldy = 6;
00522     int oldy2 = 1;
00523 
00524     //control variables for p1 and p2
00525     int selectedx;
00526     int selectedy;
00527     bool selected=false;
00528     int selectedx2;
00529     int selectedy2;
00530     bool selected2 = false;
00531 
00532     //draw the initial cursor
00533     uLCD.rectangle(oldx*16,oldy*16,(oldx*16)+16,(oldy*16)+16, GREEN);
00534     
00535     //the overall game loop
00536     while(1) {
00537         //the loop for player one
00538         while(current_player==1) {
00539             myLed4 = 0; //this led indicates okayer
00540             myLed1 = selected; //this led indicates whether or not a piece has been selected
00541             
00542             //if the selected square has moved
00543             if(x != oldx || y != oldy) {
00544                 mySpeaker.PlayNote(969.0,0.05,0.05); //play a noise for cursor movement
00545 
00546                 //delete the old selector
00547                 if(oldx % 2 == oldy %2)
00548                     uLCD.rectangle(oldx*16,oldy*16,(oldx*16)+16,(oldy*16)+16, 0xbcb6b8); 
00549                 else
00550                     uLCD.rectangle(oldx*16,oldy*16,(oldx*16)+16,(oldy*16)+16, 0x645659); 
00551 
00552                 //draw the new cursor
00553                 uLCD.rectangle(x*16,y*16,(x*16)+16,(y*16)+16, GREEN);
00554                 
00555                 //record the old cursor location
00556                 oldx = x;
00557                 oldy = y;  
00558             }
00559 
00560             if (center_hit) {
00561                 if(board[oldx][oldy] > 0 || selected) {
00562                     if(selected) { //if a piece has already been selected
00563                         //check move, make move and change turn if valid
00564                         if(selectedx == oldx && selectedy==oldy) { //deselect piece by clicking on it again
00565                             selected = false;
00566                         } else { 
00567                             if (validMove(selectedx, selectedy, oldx, oldy) ==1) { //if the move is valid make the move and swith turns
00568                                 movePiece(selectedx, selectedy, oldx, oldy, WHITE); 
00569                                 selected = false;
00570                                 current_player = 2;
00571                                 
00572                                 //erase the player one selector
00573                                 if(oldx % 2 == oldy %2)
00574                                     uLCD.rectangle(oldx*16,oldy*16,(oldx*16)+16,(oldy*16)+16, 0xbcb6b8); 
00575                                 else
00576                                     uLCD.rectangle(oldx*16,oldy*16,(oldx*16)+16,(oldy*16)+16, 0x645659); 
00577                                  
00578                                 //draw the player two selector    
00579                                 uLCD.rectangle(oldx2*16,oldy2*16,(oldx2*16)+16,(oldy2*16)+16, BLUE);
00580                             }
00581                         }
00582                     } else { //if no piece is selected select the piece being clicked on
00583                         selected = true;
00584                         selectedx =  oldx;
00585                         selectedy = oldy;
00586                     }
00587 
00588                 }    
00589                 center_hit =false; //set the center hit flag to false
00590             }    
00591                 
00592         }
00593             //the loop for player two
00594             while(current_player==2 && winner!=1)
00595             {
00596                 myLed4 = 1; //this indicates that it is player twos turn
00597                 myLed1 = selected2;  //shows whether or not a piece is selected
00598                 //variables for the bluetooth 
00599                 char bnum=0;
00600                 char bhit=0;
00601 
00602                 bool one_hit = false; 
00603 
00604                 //this reads input from the bluetooth and adjusts the location of the cursor or registers a selection
00605                 if (blue.getc()=='!') {
00606                     if (blue.getc()=='B') { //button data packet
00607                         bnum = blue.getc(); //button number
00608                         bhit = blue.getc(); //1=hit, 0=release
00609                         if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
00610                             switch (bnum) {
00611                                 case '1': //number button 1
00612                                 if (bhit=='1') {
00613                                     one_hit = true; 
00614                                 } 
00615                                 break;
00616                                 case '5': //button 5 up arrow
00617                                 if (bhit=='1') {
00618                                        //y2 = (y2-1)%8;
00619                                     --y2;
00620                                     if(y2<0)
00621                                         y2=7;
00622                                 }
00623                                 break;
00624                                 case '6': //button 6 down arrow
00625                                 if (bhit=='1') {
00626                                     y2 = (y2+1)%8;
00627                                 } 
00628                                 break;
00629                                 case '7': //button 7 left arrow
00630                                 if (bhit=='1') {
00631                                         //x2 = (x2-1)%8;
00632                                     --x2;
00633                                     if(x2<0)
00634                                         x2=7;
00635                                 } 
00636                                 break;
00637                                 case '8': //button 8 right arrow
00638                                 if (bhit=='1') {
00639                                     x2 = (x2+1)%8;
00640                                 } 
00641                                 break;
00642 
00643                             }
00644 
00645                             //sames as in player 1, play a sound and move the selection box if necessary
00646                             if(x2 != oldx2 || y2 != oldy2)
00647                             {
00648                                 mySpeaker.PlayNote(800.0,0.05,0.05);
00649                                 if(oldx % 2 == oldy2 %2)
00650                                     uLCD.rectangle(oldx2*16,oldy2*16,(oldx2*16)+16,(oldy2*16)+16, 0xbcb6b8); 
00651                                 else
00652                                     uLCD.rectangle(oldx2*16,oldy2*16,(oldx2*16)+16,(oldy2*16)+16, 0x645659); 
00653 
00654                                 uLCD.rectangle(x2*16,y2*16,(x2*16)+16,(y2*16)+16, BLUE);
00655                                 oldx2 = x2;
00656                                 oldy2 = y2;  
00657                             }
00658 
00659                             //if the selection button was pressed select/ deselect the piece or attempt to make a move
00660                             if (one_hit) {
00661                                 if(board[oldx2][oldy2] < 0 || selected2) {
00662                                     if(selected2) {
00663                                         //check move, make move and change turn if valid
00664                                         if(selectedx2 == oldx2 && selectedy2 == oldy2) {
00665                                             selected2 = false; 
00666                                         }
00667                                         else {
00668                                             if(validMove(selectedx2, selectedy2, oldx2, oldy2)) {
00669                                                 movePiece(selectedx2, selectedy2, oldx2, oldy2, BLACK); 
00670                                                 selected2 = false;
00671                                                 current_player = 1;
00672                                                 if(oldx % 2 == oldy2 %2)
00673                                                     uLCD.rectangle(oldx2*16,oldy2*16,(oldx2*16)+16,(oldy2*16)+16, 0xbcb6b8); 
00674                                                 else
00675                                                     uLCD.rectangle(oldx2*16,oldy2*16,(oldx2*16)+16,(oldy2*16)+16, 0x645659); 
00676                                                 uLCD.rectangle(oldx*16,oldy*16,(oldx*16)+16,(oldy*16)+16, GREEN);
00677                                             }
00678                                         }
00679                                     }
00680                                     else
00681                                     {
00682                                         selected2 = true;
00683                                         selectedx2 =  oldx2;
00684                                         selectedy2 = oldy2;
00685                                     }
00686                                     one_hit =false;   
00687                                 }
00688                             } 
00689                         }
00690                     }      
00691                 }
00692             } 
00693             //if someone won the game then display the winner and play the video
00694             if(winner) {
00695                 uLCD.cls();
00696                 uLCD.printf("Player %i wins", winner);
00697                 wait(1);
00698                 while(1) {
00699 
00700                     uLCD.media_init();
00701                     uLCD.set_sector_address(0x0000, 0x00);
00702                     uLCD.display_video(0,0);
00703                 }
00704             }           
00705         }
00706     }
00707