ECE4180 Mini Project

Dependencies:   4DGL-uLCD-SE mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Reversi.cpp Source File

Reversi.cpp

00001 #include "mbed.h"
00002 #include "uLCD_4DGL.h"
00003 uLCD_4DGL uLCD(p28, p27, p29); // create a global lcd object
00004 
00005 // black <=> 2
00006 // white <=> 1
00007 
00008 class Nav_Switch
00009 {
00010 public:
00011     Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire);
00012     int read();
00013 //boolean functions to test each switch
00014     bool up();
00015     bool down();
00016     bool left();
00017     bool right();
00018     bool fire();
00019 //automatic read on RHS
00020     operator int ();
00021 //index to any switch array style
00022     bool operator[](int index) {
00023         return _pins[index];
00024     };
00025 private:
00026     BusIn _pins;
00027  
00028 };
00029 Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
00030     _pins(up, down, left, right, fire)
00031 {
00032     _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise
00033     wait(0.001); //delays just a bit for pullups to pull inputs high
00034 }
00035 inline bool Nav_Switch::up()
00036 {
00037     return !(_pins[0]);
00038 }
00039 inline bool Nav_Switch::down()
00040 {
00041     return !(_pins[1]);
00042 }
00043 inline bool Nav_Switch::left()
00044 {
00045     return !(_pins[2]);
00046 }
00047 inline bool Nav_Switch::right()
00048 {
00049     return !(_pins[3]);
00050 }
00051 inline bool Nav_Switch::fire()
00052 {
00053     return !(_pins[4]);
00054 }
00055 inline int Nav_Switch::read()
00056 {
00057     return _pins.read();
00058 }
00059 inline Nav_Switch::operator int ()
00060 {
00061     return _pins.read();
00062 }
00063  
00064 Nav_Switch myNav( p14, p11, p12, p10, p13); //pin order on Sparkfun breakout
00065  
00066 int whosTurn = 1;
00067 int gb[8][8];
00068 int NUM_COLS = 8;
00069 int NUM_ROWS = 8;
00070  /*var isEmpty = function(r,c) {
00071     return board[r][c] == 0;
00072 }
00073 var score = function(boardIn) {
00074     var blackScore = 0;
00075     var whiteScore = 0;
00076     for(var r=0; r<8; r++)
00077     {
00078         for(var c=0; c<8; c++)
00079         {
00080             var current = board[r][c];
00081             if(current === 1)
00082             {
00083                 whiteScore++;
00084             }
00085             if(current === -1)
00086             {
00087                 blackScore++;
00088             }
00089         }
00090     }
00091     var score = {black: blackScore, white: whiteScore};
00092     return score;
00093 }
00094 var displayScore = function() {
00095     var currentScore = score(board);
00096     textSize(20);
00097     fill(255, 0, 0);
00098     text(currentScore.black+" - "+currentScore.white,180,390);
00099 }
00100  */
00101 void initialize_game_board()
00102 {
00103     //This is a nested loop to make sure every cell is empty
00104     //Cell Codes: 0 = empty, 1 = white piece, 2 = black piece
00105     for (int i = 0; i < NUM_ROWS; i++)
00106     {
00107         for (int j = 0; j < NUM_COLS; j++)
00108         gb[i][j] = 0;
00109     }
00110     gb[3][3] = 1;//Put down white piece
00111     gb[4][4] = 1;//Put down white piece
00112     gb[3][4] = 2;//Put down black piece
00113     gb[4][3] = 2;//Put down black piece
00114     
00115     uLCD.cls();
00116     uLCD.background_color(0x007f00);
00117     
00118     uLCD.rectangle(7,7,120,120,DGREY);
00119     uLCD.rectangle(8,8,119,119,DGREY);
00120     //uLCD.filled_rectangle(8,20,120,21,DGREY);
00121     
00122     for( int i = 1; i< 9; i++)
00123     {
00124         uLCD.filled_rectangle(8, 7 + 14*i,119,8 + 14*i,DGREY);
00125     }
00126     for( int j = 1; j<9; j++)
00127     {
00128         uLCD.filled_rectangle(7 + 14*j, 8, 8+14*j, 119, DGREY);
00129     }
00130 }
00131 
00132 void drawCursor(int xCoor, int yCoor) //Expecting x ={0,7} and y ={0,7}
00133 {
00134     uLCD.filled_rectangle(7 + 14*xCoor, 7 + 14*yCoor,8 + 14*xCoor,22 + 14*yCoor,RED);
00135     uLCD.filled_rectangle(7 + 14*xCoor, 7 + 14*yCoor,22 + 14*xCoor,8 + 14*yCoor,RED);
00136     uLCD.filled_rectangle(7 + 14*xCoor, 21 + 14*yCoor,22 + 14*xCoor,22 + 14*yCoor,RED);
00137     uLCD.filled_rectangle(21 + 14*xCoor, 7 + 14*yCoor,22 + 14*xCoor,22 + 14*yCoor,RED);
00138 }
00139     
00140 void removeCursor(int xCoor,int yCoor)
00141 {
00142     uLCD.filled_rectangle(7 + 14*xCoor, 7 + 14*yCoor,8 + 14*xCoor,22 + 14*yCoor,DGREY);
00143     uLCD.filled_rectangle(7 + 14*xCoor, 7 + 14*yCoor,22 + 14*xCoor,8 + 14*yCoor,DGREY);
00144     uLCD.filled_rectangle(7 + 14*xCoor, 21 + 14*yCoor,22 + 14*xCoor,22 + 14*yCoor,DGREY);
00145     uLCD.filled_rectangle(21 + 14*xCoor, 7 + 14*yCoor,22 + 14*xCoor,22 + 14*yCoor,DGREY);
00146 }
00147 
00148 /*
00149 var copyBoard = function() {
00150     var boardCopy = [[0,0,0,0,0,0,0,0],
00151              [0,0,0,0,0,0,0,0],
00152              [0,0,0,0,0,0,0,0],
00153              [0,0,0,0,0,0,0,0],
00154              [0,0,0,0,0,0,0,0],
00155              [0,0,0,0,0,0,0,0],
00156              [0,0,0,0,0,0,0,0],
00157              [0,0,0,0,0,0,0,0]];
00158     for(var r=0; r<8; r++)
00159     {
00160         for(var c=0; c<8; c++)
00161         {
00162             boardCopy[r][c] = board[r][c];
00163         }
00164     }
00165     return boardCopy;
00166 }; */
00167 //Removed DrawEmptyBoard function
00168 
00169 /*var drawPiece = function(r,c,player) {
00170     var xPos = 400/8*c+400/16;
00171     var yPos = 400/8*r+400/16;
00172     if(player === 0)
00173     {
00174         return;
00175     }
00176     fill(0, 0, 0);
00177     if(player===1)
00178     {
00179         fill(255, 255, 255);
00180     }
00181     ellipse(xPos,yPos,40,40);
00182 }; */
00183 
00184 void drawPieces(){
00185     for (int i = 0; i < NUM_ROWS; i++)
00186     {
00187         for (int j = 0; j < NUM_COLS; j++)
00188         {
00189             if(gb[i][j] == 1){
00190                 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , WHITE);
00191             }
00192             if(gb[i][j] == 2){
00193                 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , BLACK);
00194             }
00195         }
00196     }
00197 }
00198 
00199 void drawPiece(int i, int j) { //xCoor, yCoor
00200     if(gb[i][j] == 1){
00201         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , WHITE);
00202     }
00203     if(gb[i][j] == 2){
00204         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , BLACK);
00205     }
00206 }
00207 
00208 void flippingPiece(int i, int j) { //xCoor, yCoor, animation
00209     if(gb[i][j] == 1){ //Gradually changes a piece White
00210         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0x4c4c4c);
00211         wait(0.1);
00212         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0xb2b2b2);
00213         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0x4c4c4c);
00214         wait(0.1);
00215         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0xffffff);
00216         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0xb2b2b2);
00217         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0x4c4c4c);
00218         wait(0.1);
00219         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0xffffff);
00220         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0xb2b2b2);
00221         wait(0.1);
00222         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0xffffff);
00223         wait(0.1);
00224     }
00225     if(gb[i][j] == 2){ //Gradually changes a piece Black
00226         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0xb2b2b2);
00227         wait(0.1);
00228         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0x4c4c4c);
00229         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0xb2b2b2);
00230         wait(0.1);
00231         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0x000000);
00232         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0x4c4c4c);
00233         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0xb2b2b2);
00234         wait(0.1);
00235         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0x000000);
00236         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0x4c4c4c);
00237         wait(0.1);
00238         uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0x000000);
00239         wait(0.1);
00240     }  
00241 }
00242 
00243 void Select(int xCoor, int yCoor){ //function should determine what happens when joy stick pressed
00244     if(whosTurn%2 == 0) ( //Whites Turn
00245         //Valid Check, see if move is possible
00246         
00247         //Place Piece
00248         drawPiece(xCoor, yCoor);
00249         //Convert pieces in 2D Array
00250         int counter; //Count number of converted pieces
00251         
00252         
00253         //Flip pieces on screen
00254         for( int m = 0; m<counter; m++) {
00255                 
00256         }
00257     }      
00258 }
00259 
00260 /*var fillBoard = function() {
00261     for(var r=0; r<8; r++)
00262     {
00263         for(var c=0; c<8; c++)
00264         {
00265             drawPiece(r,c,board[r][c]);
00266         }
00267     }
00268 };
00269 var drawBoard = function() {
00270     drawEmptyBoard();
00271     fillBoard();
00272 };  --------------------------------------------WAS NOT USED VVVVV ----------------------------
00273 /*var flipFromInDir = function(r,c,player,rDir,cDir)
00274 {
00275     //debug("Checking direction rDir: "+rDir+"    cDir: "+cDir+" for player "+player);
00276     var currentR = r;
00277     var currentC = c;
00278     var current = board[currentR][currentC];
00279     var toBeFlipped = [];
00280     var withinRBounds = 0<=currentR && currentR<8;
00281     var withinCBounds = 0<=currentC && currentC<8;
00282     var oppositePieces = true;
00283     var samePlayerEncounterred = false;
00284     while(withinRBounds && withinCBounds && oppositePieces)
00285     {
00286         //debug("    oh, sweet! "+currentR+", "+currentC+" looks good!");
00287         toBeFlipped.push({r:currentR, c:currentC});
00288         currentR += rDir;
00289         currentC += cDir;
00290         withinRBounds = 0<=currentR && currentR<8;
00291         withinCBounds = 0<=currentC && currentC<8;
00292         if(withinCBounds && withinRBounds)
00293         {
00294             current = board[currentR][currentC];
00295             oppositePieces = (current===-player);
00296             //debug("    current at "+currentR+", "+currentC+" is "+current);
00297             //debug("    oppositePieces at "+currentR+", "+currentC+" is "+oppositePieces);
00298             if(current===player)
00299             {
00300                 samePlayerEncounterred = true;
00301             }
00302         }
00303     }
00304     //debug("    we found "+toBeFlipped.length);
00305     if(toBeFlipped.length > 1 && samePlayerEncounterred)
00306     {
00307         for(var i=1; i<toBeFlipped.length; i++)
00308         {
00309             var flipMe = toBeFlipped[i];
00310             drawPiece(flipMe.r, flipMe.c, player);
00311             board[flipMe.r][flipMe.c] = player;
00312         }
00313     }
00314 };  -------------------------------WAS NOT USED ^^^^--------------------
00315 var flipFromInDir = function(r,c,player,rDir,cDir) {
00316     var potentialBoard = copyBoard();
00317     
00318     var currentR = r+rDir;
00319     var currentC = c+cDir;
00320     var withinRBounds = 0<=currentR && currentR<8;
00321     var withinCBounds = 0<=currentC && currentC<8;
00322     
00323     while(withinRBounds && withinCBounds)
00324     {
00325         debug("r,c: "+r+", "+c+"   direction: "+rDir+", "+cDir+"   current: "+currentR+", "+currentC);
00326         var current = board[currentR][currentC];
00327         if(current === 0)
00328         {
00329             return board;
00330         }
00331         else if(current === player)
00332         {
00333             //board = potentialBoard;
00334             return potentialBoard;
00335         }
00336         else
00337         {
00338             potentialBoard[currentR][currentC] = player;
00339             currentR = currentR+rDir;
00340             currentC = currentC+cDir;
00341             withinRBounds = 0<=currentR && currentR<8;
00342             withinCBounds = 0<=currentC && currentC<8;
00343         }
00344     }
00345     return board;
00346 };
00347 var flipFrom = function(r,c,player) {
00348     for(var rDir = -1; rDir<2; rDir++)
00349     {
00350         for(var cDir = -1; cDir<2; cDir++)
00351         {
00352             board = flipFromInDir(r, c, player, rDir, cDir);
00353         }
00354     }
00355 };
00356 var placePiece = function(r,c,player) {
00357     //debug("placing piece for player "+player);
00358     drawPiece(r,c,player);
00359     board[r][c] = player;
00360     flipFrom(r,c,player);
00361     drawBoard();
00362 };
00363 var move = function(r,c)
00364 {
00365     placePiece(r,c,whosTurn);
00366     whosTurn *= -1;
00367 };
00368 var isValidInDir = function(r,c,player,rDir,cDir)
00369 {
00370     //debug("Checking direction rDir: "+rDir+"    cDir: "+cDir+" for player "+player);
00371     var currentR = r;
00372     var currentC = c;
00373     var current = board[currentR][currentC];
00374     var toBeFlipped = [];
00375     var withinRBounds = 0<=currentR && currentR<8;
00376     var withinCBounds = 0<=currentC && currentC<8;
00377     var oppositePieces = true;
00378     var samePlayerEncounterred = false;
00379     while(withinRBounds && withinCBounds && oppositePieces)
00380     {
00381         //debug("    oh, sweet! "+currentR+", "+currentC+" looks good!");
00382         toBeFlipped.push({r:currentR, c:currentC});
00383         currentR += rDir;
00384         currentC += cDir;
00385         withinRBounds = 0<=currentR && currentR<8;
00386         withinCBounds = 0<=currentC && currentC<8;
00387         if(withinCBounds && withinRBounds)
00388         {
00389             current = board[currentR][currentC];
00390             oppositePieces = (current===-player);
00391             //debug("    current at "+currentR+", "+currentC+" is "+current);
00392             //debug("    oppositePieces at "+currentR+", "+currentC+" is "+oppositePieces);
00393             if(current===player)
00394             {
00395                 samePlayerEncounterred = true;
00396             }
00397         }
00398     }
00399     //debug("    we found "+toBeFlipped.length);
00400     if(toBeFlipped.length > 1 && samePlayerEncounterred)
00401     {
00402         return true;
00403     }
00404     return false;
00405 };
00406 var isValidMove = function(r,c,player) {
00407     if(!isEmpty(r,c))
00408     {
00409         return false;
00410     }
00411     for(var rDir = -1; rDir<2; rDir++)
00412     {
00413         for(var cDir = -1; cDir<2; cDir++)
00414         {
00415             if(isValidInDir(r, c, player, rDir, cDir))
00416             {
00417                 return true;
00418             }
00419         }
00420     }
00421     return false;
00422 };
00423 var mouseClicked = function() {
00424     var r = floor(mouseY*8/400);
00425     var c = floor(mouseX*8/400);
00426     if(isValidMove(r,c,whosTurn))
00427     {
00428         move(r,c);
00429     }
00430     displayScore();
00431 }; 
00432 */
00433 int main() {
00434     initialize_game_board();
00435     int xCoor = 3;
00436     int yCoor = 4;
00437     
00438     
00439     while(1) {
00440         //with pullups a button hit is a "0" - "~" inverts data to leds
00441         //~(myNav & 0x0F); //update leds with nav switch direction inputs
00442         if(myNav[0] == 0 && xCoor != 0){xCoor --;}
00443         if(myNav[1] == 0 && xCoor != 7){xCoor ++;}
00444         if(myNav[2] == 0 && yCoor != 0){xCoor --;}
00445         if(myNav[3] == 0 && yCoor != 7){xCoor ++;}
00446         if(myNav.fire()) {Select(xCoor, yCoor);} // Press Down Joystick for Select
00447         //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this
00448         wait(0.02);
00449     }
00450 
00451 }
00452 //initializeBoard();
00453 //drawBoard();
00454 //displayScore();