Final
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
Fork of Lab4-Reversi-v2 by
Diff: Reversi.cpp
- Revision:
- 3:b1fe86f61f2f
- Parent:
- 2:1f7c6cc19a9a
diff -r 1f7c6cc19a9a -r b1fe86f61f2f Reversi.cpp --- a/Reversi.cpp Tue Nov 01 13:59:02 2016 +0000 +++ b/Reversi.cpp Tue Nov 01 23:30:44 2016 +0000 @@ -8,7 +8,7 @@ PwmOut Speaker(p21); AnalogOut DACout(p18); -DigitalIn pb(p10); +DigitalIn pb(p15); wave_player waver(&DACout); @@ -238,8 +238,8 @@ for (int i = 0; i < 8; i++) { // check for opponent around emtpy space and fill if (i == 0) { // right if (x + 1 < 7 && gb[x + 1][y] == opp) { //check to see if not beyond board and has to be next to the opponent - for (int j = x + 2; j < 8; j++) { - if (gb[j][y] == pl && !flipped) { + for (int j = x + 2; j < 8 && !flipped; j++) { + if (gb[j][y] == pl) { for (int k = x + 1; k < j; k++) { flippingPiece(k, y); } @@ -258,8 +258,8 @@ } } else if (i == 1) { // down-right if (x + 1 < 7 && y + 1 < 7 && gb[x + 1][y + 1] == opp) { // check if within margin and next to other color - for (int j = 2; j < 8 - x && j < 8 - y; j++) { // start finding next same color - if (gb[x + j][y + j] == pl && !flipped) { // if same color and not already been flipping + for (int j = 2; j < 8 - x && j < 8 - y && !flipped; j++) { // start finding next same color + if (gb[x + j][y + j] == pl) { // if same color and not already been flipping for (int k = 1; k < j; k++) { // between other color to this same color flippingPiece(x + k, y + k); // flip } @@ -278,8 +278,8 @@ } } else if (i == 2) { // down if (y + 1 < 7 && gb[x][y + 1] == opp) { - for (int j = y + 2; j < 8; j++) { - if (gb[x][j] == pl && !flipped) { + for (int j = y + 2; j < 8 && !flipped; j++) { + if (gb[x][j] == pl) { for (int k = y + 1; k < j; k++) { flippingPiece(x, k); } @@ -298,8 +298,8 @@ } } else if (i == 3) { // down-left if (x - 1 > 0 && y + 1 < 7 && gb[x - 1][y + 1] == opp) { - for (int j = 2; j < x + 1 && j < 8 - y; j++) { - if (gb[x - j][y + j] == pl && !flipped) { + for (int j = 2; j < x + 1 && j < 8 - y && !flipped; j++) { + if (gb[x - j][y + j] == pl) { for (int k = 1; k < j; k++) { flippingPiece(x - k, y + k); } @@ -318,8 +318,8 @@ } } else if (i == 4) { // left if (x - 1 > 0 && gb[x - 1][y] == opp) { - for (int j = x - 2; j > -1; j--) { - if (gb[j][y] == pl && !flipped) { + for (int j = x - 2; j > -1 && !flipped; j--) { + if (gb[j][y] == pl) { for (int k = x - 1; k > j; k--) { flippingPiece(k, y); } @@ -338,14 +338,14 @@ } } else if (i == 5) { // up-left if (x - 1 > 0 && y - 1 > 0 && gb[x - 1][y - 1] == opp) { - for (int j = 2; j < x + 1 && j < y + 1; j++) { - if (gb[x - j][y - j] == pl && !flipped) { + for (int j = 2; j < x + 1 && j < y + 1 && !flipped; j++) { + if (gb[x - j][y - j] == pl) { for (int k = 1; k < j; k++) { flippingPiece(x - k, y - k); } flipped = true; } else if (gb[x - j][y - j] == 0) { - j = 8; + j = x + 1; } } if (flipped) { @@ -358,8 +358,8 @@ } } else if (i == 6) { // up if (y - 1 > 0 && gb[x][y - 1] == opp) { - for (int j = y - 2; j > -1; j--) { - if (gb[x][j] == pl && !flipped) { + for (int j = y - 2; j > -1 && !flipped; j--) { + if (gb[x][j] == pl) { for (int k = y - 1; k > j; k--) { flippingPiece(x, k); } @@ -378,14 +378,14 @@ } } else if (i == 7) { // up-right if (x + 1 < 7 && y - 1 > 0 && gb[x + 1][y - 1] == opp) { - for (int j = 2; j < 8 - x && j < y + 1; j++) { - if (gb[x + j][y - j] == pl && !flipped) { + for (int j = 2; j < 8 - x && j < y + 1 && !flipped; j++) { + if (gb[x + j][y - j] == pl) { for (int k = 1; k < j; k++) { flippingPiece(x + k, y - k); } flipped = true; } else if (gb[x + j][y - j] == 0) { - j = 8; + j = 8 - x; } } if (flipped) { @@ -445,17 +445,18 @@ uLCD.filled_rectangle(0,0,127,127,0x007f00); FILE *fp1 = fopen("/sd/wavfiles/Victory.wav", "r"); - if(fp1 == NULL) { + if(fp1 == NULL) { error("Could not open file for read\n"); - } - Speaker.period(1.0/400000.0); - waver.play(fp1); //Plays *.wav audio file - fclose(fp1); + } + Speaker.period(1.0/400000.0); + waver.play(fp1); //Plays *.wav audio file + fclose(fp1); countPieces(); + uLCD.textbackground_color(0x007f00); uLCD.color(RED); uLCD.printf("Score\n"); uLCD.color(BLACK); - uLCD.printf("%D, numBlack"); + uLCD.printf("%d", numBlack); uLCD.color(RED); uLCD.printf(" - "); uLCD.color(WHITE); @@ -463,57 +464,30 @@ uLCD.color(RED); uLCD.printf("\n Play again? \n"); - uLCD.text_char('Y', 4, 9, BLUE); - uLCD.text_char('E', 5, 9, BLUE); - uLCD.text_char('S', 6, 9, BLUE); - uLCD.text_char('N', 11, 9, RED); - uLCD.text_char('O', 12, 9, RED); + uLCD.text_char('Y', 8, 9, BLUE); + uLCD.text_char('E', 9, 9, BLUE); + uLCD.text_char('S', 10, 9, BLUE); - uLCD.rectangle(20,90, 40, 110, 0xffff00); //Default over Yes bool optionSelected = false; - int choice = 1; //Default choice Yes - while(optionSelected ==false){ - if(myNav[2] == 0 ){ - uLCD.rectangle(20,90, 40, 110, 0xffff00); - uLCD.rectangle(80,90, 100, 110, 0x007F00); - choice = 1; - } - if(myNav[3] == 0 ){ - uLCD.rectangle(20,90, 40, 110, 0x007F00); - uLCD.rectangle(80,90, 100, 110, 0xffff00); - choice = 2; - } + while(optionSelected == false){ if(myNav[4] == 0){ //Joystick pressed to Select - if(choice == 1) { - //Restart and play again - initialize_game_board(); - xCoor = 3; - yCoor = 4; - } - else{ //choice == 0 - //Quit? - } + //Restart and play again + initialize_game_board(); + drawPieces(); + xCoor = 3; + yCoor = 4; + optionSelected = true; } } } -//For Testing purposes -void instantEnd(){ - result(); -} - int main() { pb.mode(PullUp); - while (1) { initialize_game_board(); drawPieces(); while(!gameover) { //active game, checks game over - if(pb == 1){ - instantEnd(); - } - // check inputs while (!moved) { drawCursor(xCoor, yCoor); wait(0.02); @@ -526,11 +500,14 @@ if(myNav[3] == 0 && yCoor != 0){yCoor --;} if(myNav.fire()) {valid_move(xCoor, yCoor);} // Press Down Joystick for Select //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this + if (pb == 0) { + result(); + } } moved = false; // check game over countPieces(); - gameover = numBlack + numWhite == 64; + gameover = (numBlack + numWhite == 64 || gameover); } result(); }