Final

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player

Fork of Lab4-Reversi-v2 by Timothy Li

Revision:
2:1f7c6cc19a9a
Parent:
1:cc72ad58982b
Child:
3:b1fe86f61f2f
--- a/Reversi.cpp	Tue Nov 01 11:46:09 2016 +0000
+++ b/Reversi.cpp	Tue Nov 01 13:59:02 2016 +0000
@@ -2,11 +2,14 @@
 #include "uLCD_4DGL.h"
 #include "SDFileSystem.h"
 #include "wave_player.h"
+#include "stdio.h"
 uLCD_4DGL uLCD(p28, p27, p29); // create a global lcd object
 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
 PwmOut Speaker(p21);
 AnalogOut DACout(p18);
 
+DigitalIn pb(p10);
+
 wave_player waver(&DACout);
 
 class Nav_Switch
@@ -194,170 +197,7 @@
     }  
 }
 
-/*var fillBoard = function() {
-    for(var r=0; r<8; r++)
-    {
-        for(var c=0; c<8; c++)
-        {
-            drawPiece(r,c,board[r][c]);
-        }
-    }
-};
-var drawBoard = function() {
-    drawEmptyBoard();
-    fillBoard();
-};  --------------------------------------------WAS NOT USED VVVVV ----------------------------
-var flipFromInDir = function(r,c,player,rDir,cDir)
-{
-    //debug("Checking direction rDir: "+rDir+"    cDir: "+cDir+" for player "+player);
-    var currentR = r;
-    var currentC = c;
-    var current = board[currentR][currentC];
-    var toBeFlipped = [];
-    var withinRBounds = 0<=currentR && currentR<8;
-    var withinCBounds = 0<=currentC && currentC<8;
-    var oppositePieces = true;
-    var samePlayerEncounterred = false;
-    while(withinRBounds && withinCBounds && oppositePieces)
-    {
-        //debug("    oh, sweet! "+currentR+", "+currentC+" looks good!");
-        toBeFlipped.push({r:currentR, c:currentC});
-        currentR += rDir;
-        currentC += cDir;
-        withinRBounds = 0<=currentR && currentR<8;
-        withinCBounds = 0<=currentC && currentC<8;
-        if(withinCBounds && withinRBounds)
-        {
-            current = board[currentR][currentC];
-            oppositePieces = (current===-player);
-            //debug("    current at "+currentR+", "+currentC+" is "+current);
-            //debug("    oppositePieces at "+currentR+", "+currentC+" is "+oppositePieces);
-            if(current===player)
-            {
-                samePlayerEncounterred = true;
-            }
-        }
-    }
-    //debug("    we found "+toBeFlipped.length);
-    if(toBeFlipped.length > 1 && samePlayerEncounterred)
-    {
-        for(var i=1; i<toBeFlipped.length; i++)
-        {
-            var flipMe = toBeFlipped[i];
-            drawPiece(flipMe.r, flipMe.c, player);
-            board[flipMe.r][flipMe.c] = player;
-        }
-    }
-};  -------------------------------WAS NOT USED ^^^^--------------------
-var flipFromInDir = function(r,c,player,rDir,cDir) {
-    var potentialBoard = copyBoard();
-    
-    var currentR = r+rDir;
-    var currentC = c+cDir;
-    var withinRBounds = 0<=currentR && currentR<8;
-    var withinCBounds = 0<=currentC && currentC<8;
-    
-    while(withinRBounds && withinCBounds)
-    {
-        debug("r,c: "+r+", "+c+"   direction: "+rDir+", "+cDir+"   current: "+currentR+", "+currentC);
-        var current = board[currentR][currentC];
-        if(current === 0)
-        {
-            return board;
-        }
-        else if(current === player)
-        {
-            //board = potentialBoard;
-            return potentialBoard;
-        }
-        else
-        {
-            potentialBoard[currentR][currentC] = player;
-            currentR = currentR+rDir;
-            currentC = currentC+cDir;
-            withinRBounds = 0<=currentR && currentR<8;
-            withinCBounds = 0<=currentC && currentC<8;
-        }
-    }
-    return board;
-};
-var flipFrom = function(r,c,player) {
-    for(var rDir = -1; rDir<2; rDir++)
-    {
-        for(var cDir = -1; cDir<2; cDir++)
-        {
-            board = flipFromInDir(r, c, player, rDir, cDir);
-        }
-    }
-};
-var placePiece = function(r,c,player) {
-    //debug("placing piece for player "+player);
-    drawPiece(r,c,player);
-    board[r][c] = player;
-    flipFrom(r,c,player);
-    drawBoard();
-};
-var move = function(r,c)
-{
-    placePiece(r,c,whosTurn);
-    whosTurn *= -1;
-};
-var isValidInDir = function(r,c,player,rDir,cDir)
-{
-    //debug("Checking direction rDir: "+rDir+"    cDir: "+cDir+" for player "+player);
-    var currentR = r;
-    var currentC = c;
-    var current = board[currentR][currentC];
-    var toBeFlipped = [];
-    var withinRBounds = 0<=currentR && currentR<8;
-    var withinCBounds = 0<=currentC && currentC<8;
-    var oppositePieces = true;
-    var samePlayerEncounterred = false;
-    while(withinRBounds && withinCBounds && oppositePieces)
-    {
-        //debug("    oh, sweet! "+currentR+", "+currentC+" looks good!");
-        toBeFlipped.push({r:currentR, c:currentC});
-        currentR += rDir;
-        currentC += cDir;
-        withinRBounds = 0<=currentR && currentR<8;
-        withinCBounds = 0<=currentC && currentC<8;
-        if(withinCBounds && withinRBounds)
-        {
-            current = board[currentR][currentC];
-            oppositePieces = (current===-player);
-            //debug("    current at "+currentR+", "+currentC+" is "+current);
-            //debug("    oppositePieces at "+currentR+", "+currentC+" is "+oppositePieces);
-            if(current===player)
-            {
-                samePlayerEncounterred = true;
-            }
-        }
-    }
-    //debug("    we found "+toBeFlipped.length);
-    if(toBeFlipped.length > 1 && samePlayerEncounterred)
-    {
-        return true;
-    }
-    return false;
-};
-var isValidMove = function(r,c,player) {
-    if(!isEmpty(r,c))
-    {
-        return false;
-    }
-    for(var rDir = -1; rDir<2; rDir++)
-    {
-        for(var cDir = -1; cDir<2; cDir++)
-        {
-            if(isValidInDir(r, c, player, rDir, cDir))
-            {
-                return true;
-            }
-        }
-    }
-    return false;
-};
-*/
+
 void invalid(int x, int y, int oldColor){
     gb[x][y] = oldColor;
     drawPieces();
@@ -564,6 +404,14 @@
                 moved = true; // has moved
                 turn = ++turn%2; // increment turn
                 flipped = true; // check flip
+                
+                FILE *fp2 = fopen("/sd/wavfiles/FlipSound.wav", "r");
+                if(fp2 == NULL) {
+                error("Could not open file for read\n");
+                }
+                Speaker.period(1.0/400000.0);
+                waver.play(fp2); //Plays *.wav audio file
+                fclose(fp2);
             }
         }
         if (!flipped) { // no changes were made
@@ -594,7 +442,15 @@
 }
 void result() {
     uLCD.cls(); // THIS CODE ASSUMES BACKGROUND CORRECTLY RESETS TO 0x007F00
+    uLCD.filled_rectangle(0,0,127,127,0x007f00);
     
+    FILE *fp1 = fopen("/sd/wavfiles/Victory.wav", "r");
+        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);
     countPieces();
     uLCD.color(RED);
     uLCD.printf("Score\n");
@@ -641,13 +497,22 @@
     } 
 }
 
+//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);