swag

Dependencies:   Motor Servo mbed_tictactoe

Revision:
4:6342714f3bc3
Parent:
3:317e06586628
Child:
5:e6f9086e0730
--- a/main.cpp	Tue Oct 20 12:55:55 2015 +0000
+++ b/main.cpp	Mon Oct 26 11:54:33 2015 +0000
@@ -9,7 +9,6 @@
 #include "Servo.h"
 #include "Timer.h"
 
-DigitalOut test(LED1);
 DigitalIn in[]= {p16,p17,p18,p19,p20,p15};
 DigitalOut lights[]= {p5,p6,p7,p8,p11};
 Timer timer;
@@ -27,7 +26,9 @@
 void ai(int chip,int difficulty);   //Ai picks space
 int turn();                         //Determines the turn number(useful for difficulty 4)
 bool tworow();                      //Checks for 2 in a row(for ai)
-void perfectplace();                  //Picks best spot to put a chip for difficulty 4
+void perfectplace();                //Picks best spot to put a chip for difficulty 4
+void playerpc(int chip);            //PC input for easier testing
+void monitorout();                  //Sisplays the board in tera term for testing purposes
 void placechip(int x, int y);       //Moves robot to place chip
 
 int main()
@@ -46,8 +47,10 @@
     }
 
 //User Selects Difficulty
-    printf("Select Difficulty");
+    printf("Select Difficulty\n");
     int difficulty;
+    scanf("%d",&difficulty);
+    /*
     while(in[0]+in[1]+in[2]+in[3]!=1) {
     }
     if(in[0]==1)
@@ -60,7 +63,7 @@
         difficulty=4;
 
     lights[difficulty-1]=1;
-
+    */
     //Random Seed
     timer.stop();
     srand(timer.read_us());
@@ -76,25 +79,22 @@
     }
 
 //Loops of turns until someone wins or a tie
-    while(winner()==-1) {
-        if(playerchip==1) {
-            player(playerchip);
-            if(winner()!=-1)
-                break;
-            ai(aichip,difficulty);
-            if(winner()!=-1)
-                break;
-        } else {
-            ai(aichip,difficulty);
-            if(winner()!=-1)
-                break;
-            player(playerchip);
-            if(winner()!=-1)
-                break;
-        }
+    if(playerchip==1) {
+        printf("You go first\n");
+        //player(playerchip);
+        playerpc(playerchip);
+    }
+    while(true) {
+        ai(aichip,difficulty);
+        if(winner()!=-1)
+            break;
+        //player(playerchip);
+        playerpc(playerchip);
     }
 
-    //Operates LED's based on winner
+//Operates LED's based on winner
+    int w=winner();
+    printf("Winner:%d\n",w);
     lights[difficulty-1]=0;
     lights[4]=0;
     if(winner()==playerchip) { //Light Boucnes back and forth if player wins
@@ -150,7 +150,8 @@
 //Player inputs using the switches until they pick an empty space
 void player(int chip)
 {
-    int x,y;
+    int x=-1;
+    int y=-1;
     do {
         if(in[0]+in[1]+in[2]==1 && in[3]+in[4]+in[5]==1) {
             if(in[0]==1)
@@ -171,7 +172,8 @@
     board[x][y]=chip;
     xprev=x;
     yprev=y;
-    placechip(x,y);
+    monitorout();
+    //placechip(x,y);
 }
 
 
@@ -181,7 +183,7 @@
 int turn()
 {
     int turn=9;
-    for(int i=1; i<=2; i++) {
+    for(int i=0; i<=2; i++) {
         for(int j=0; j<=2; j++) {
             if(board[i][j]==-1)
                 turn--;
@@ -206,194 +208,215 @@
 
         if(difficulty==2 || difficulty==3) {      //Middle difficulties have a random chance of not blocking
             if(tworow()) {
-                if(difficulty==2 && rand()%10<8) {    //Sometimes doesn't notice two in a row
-                    x=xglobal;
-                    y=yglobal;
-                } else if(difficulty==3) {    //Always notices 2 in a row
+                if((difficulty==2 && rand()%10<8) || difficulty==3) {    //Sometimes doesn't notice two in a row
                     x=xglobal;
                     y=yglobal;
                 }
             }
-        } else {
-            perfectplace();
-            x=xglobal;
-            y=yglobal;
         }
-        placechip(x,y);
-        board[x][y]=chip;
+    } else {
+        perfectplace();
+        x=xglobal;
+        y=yglobal;
     }
+    //placechip(x,y);
+    board[x][y]=chip;
+    monitorout();
 }
 
 
 
 bool tworow()
 {
+    int x,y;
     //Vertical and horizontals
     for(int i=0; i<=2; i++) {
         if(board[0][i]==board[1][i] && board[2][i]==-1) {
-            xglobal=2;
-            yglobal=i; //board[2][i]
-        }
-        if(board[1][i]==board[2][i] && board[0][i]==-1) {
-            xglobal=0;
-            yglobal=i; //board[0][i]
-        }
-        if(board[0][i]==board[2][i] && board[1][i]==-1) {
-            xglobal=1;
-            yglobal=i; //board[1][i]
-        }
-
-        if(board[i][0]==board[i][1] && board[i][2]==-1) {
-            xglobal=i;
-            yglobal=2; //board[i][2]
-        }
-        if(board[i][1]==board[i][2] && board[i][0]==-1) {
-            xglobal=i;
-            yglobal=0; //board[i][0]
-        }
-        if(board[i][0]==board[i][2] && board[i][1]==-1) {
-            xglobal=i;
-            yglobal=1; //board[i][1]
+            x=2;
+            y=i; //board[2][i]
+        } else if(board[1][i]==board[2][i] && board[0][i]==-1) {
+            x=0;
+            y=i; //board[0][i]
+        } else if(board[0][i]==board[2][i] && board[1][i]==-1) {
+            x=1;
+            y=i; //board[1][i]
+        } else if(board[i][0]==board[i][1] && board[i][2]==-1) {
+            x=i;
+            y=2; //board[i][2]
+        } else if(board[i][1]==board[i][2] && board[i][0]==-1) {
+            x=i;
+            y=0; //board[i][0]
+        } else if(board[i][0]==board[i][2] && board[i][1]==-1) {
+            x=i;
+            y=1; //board[i][1]
+        } else if(board[0][0]==board[1][1] && board[2][2]==-1) {   //Diagonals
+            x=2;
+            y=2; //board[2][2]
+        } else if(board[1][1]==board[2][2] && board[0][0]==-1) {
+            x=0;
+            y=0; //board[0][0]
+        } else if(board[0][0]==board[2][2] && board[1][1]==-1) {
+            x=1;
+            y=1; //board[1][1]
+        } else if(board[0][2]==board[1][1] && board[2][0]==-1) {
+            x=2;
+            y=0; //board[2][0]
+        } else if(board[1][1]==board[2][0] && board[0][2]==-1) {
+            x=0;
+            y=2; //board[0][2]
+        } else if(board[0][2]==board[2][0] && board[1][1]==-1) {
+            x=1;
+            y=1; //board[1][1]
         }
     }
-    //Diagonal Components
-    if(board[0][0]==board[1][1] && board[2][2]==-1) {
-        xglobal=2;
-        yglobal=2; //board[2][2]
-    }
-    if(board[1][1]==board[2][2] && board[0][0]==-1) {
-        xglobal=0;
-        yglobal=0; //board[0][0]
-    }
-    if(board[0][0]==board[2][2] && board[1][1]==-1) {
-        xglobal=1;
-        yglobal=1; //board[1][1]
+
+    if(x!=-1 && y!=-1) { //Returns true if there are 2 in a row
+        xglobal=x;
+        yglobal=y;
+        return true;
+    } else {
+        return false;
     }
-
-    if(board[0][2]==board[1][1] && board[2][0]==-1) {
-        xglobal=2;
-        yglobal=0; //board[2][0]
-    }
-    if(board[1][1]==board[2][0] && board[0][2]==-1) {
-        xglobal=0;
-        yglobal=2; //board[0][2]
-    }
-    if(board[0][2]==board[2][0] && board[1][1]==-1) {
-        xglobal=1;
-        yglobal=1; //board[1][1]
-    }
-    if(xglobal==-1 && yglobal==-1)  //Returns true if there are 2 in a row
-        return false;
-    else
-        return true;
 }
 
 
 
 void perfectplace()
 {
+    int x,y;
     if(turn()==0) {   //Places in a random corner if first
-        xglobal=(rand()%2)*2; //Picks either 0 or 2
-        yglobal=(rand()%2)*2;
-        xstart=xglobal;
-        ystart=yglobal;
-    }
-    if(turn()==1) {
+        x=(rand()%2)*2; //Picks either 0 or 2
+        y=(rand()%2)*2;
+        xstart=x;
+        ystart=y;
+    } else if(turn()==1) {
         if(xprev!=1 && yprev!=1) {      //If player takes corner, ai takes center
-            xglobal=1;
-            yglobal=1;
+            x=1;
+            y=1;
         } else if(xprev==1 && yprev==1) {       //If player takes center, ai takes a corner
-            xglobal=(rand()%2)*2; //Picks either 0 or 2
-            yglobal=(rand()%2)*2;
+            x=(rand()%2)*2; //Picks either 0 or 2
+            y=(rand()%2)*2;
         } else {                          //If player takes edge, ai takes spot in either same row or col
             if(xprev==1) {
-                xglobal=rand()%3;
-                if(xglobal==xprev) {
+                x=rand()%3;
+                if(x==xprev) {
                     do {
-                        yglobal=rand()%3;
-                    } while(yglobal==yprev);
+                        y=rand()%3;
+                    } while(y==yprev);
                 } else {
-                    yglobal=yprev;
+                    y=yprev;
                 }
             } else {
-                yglobal=rand()%3;
-                if(yglobal==yprev) {
+                y=rand()%3;
+                if(y==yprev) {
                     do {
-                        xglobal=rand()%3;
-                    } while(xglobal==xprev);
+                        x=rand()%3;
+                    } while(x==xprev);
                 } else {
-                    xglobal=xprev;
+                    x=xprev;
                 }
             }
         }
-    }
-    //The ai's second turn if the user goes first
-    if(turn()==2) {
+    } else if(turn()==2) {
         if(xprev==1 && yprev==1) {   //If user places in middle, any open spot leads to tie
             do {
-                xglobal=rand()%3;
-                yglobal=rand()%3;
-            } while(board[xglobal][yglobal]!=-1);
+                x=rand()%3;
+                y=rand()%3;
+            } while(board[x][y]!=-1);
         } else if((xprev==1 ||yprev==1) && (xprev!=xstart && yprev!=ystart)) {   //Opposite edge
             if(xprev-xstart==2 || xprev-xstart==-2) {
-                yglobal=ystart;
+                y=ystart;
                 if(yprev+ystart==1)
-                    xglobal=2;
+                    x=2;
                 else
-                    xglobal=0;
+                    x=0;
             } else {
-                xglobal=xstart;
+                x=xstart;
                 if(xprev+xstart==1)
-                    yglobal=2;
+                    y=2;
                 else
-                    yglobal=0;
+                    y=0;
             }
         } else if((xprev==1 || yprev==1) && (xprev==xstart || yprev==ystart)) {  //Adjascent edge
             if(xprev==xstart) {
                 do {
-                    xglobal=rand()%3;
-                    yglobal=rand()%3;
-                } while(((ystart!=yglobal) && (xstart==xglobal)) || (xglobal==1 && yglobal==1));
+                    x=rand()%3;
+                    y=rand()%3;
+                } while(((ystart!=y) && (xstart==x)) || (x==1 && y==1));
             } else {
                 do {
-                    xglobal=rand()%3;
-                    yglobal=rand()%3;
-                } while(((xstart!=xglobal) && (ystart==yglobal)) || (xglobal==1 && yglobal==1));
+                    x=rand()%3;
+                    y=rand()%3;
+                } while(((xstart!=x) && (ystart==y)) || (x==1 && y==1));
             }
         } else if((xprev-xstart==2 || xprev-xstart==-2) && (yprev-ystart==2 || yprev-ystart==-2)) {  //Opposite Corner
             do {
-                xglobal=(rand()%2)*2;
-                yglobal=(rand()%2)*2;
-            } while(board[xglobal][yglobal]!=-1);
+                x=(rand()%2)*2;
+                y=(rand()%2)*2;
+            } while(board[x][y]!=-1);
         } else {     //Adjascent Corner
             if(xprev==xstart) {
                 do {
-                    xglobal=rand()%3;
-                    yglobal=rand()%3;
-                } while((ystart==yglobal && xstart!=xglobal) || (yprev==yglobal && (yprev-yglobal==2 || yprev-yglobal==-2)));
+                    x=rand()%3;
+                    y=rand()%3;
+                } while((ystart==y && xstart!=x) || (yprev==y && (yprev-y==2 || yprev-y==-2)));
             } else {
                 do {
-                    xglobal=rand()%3;
-                    yglobal=rand()%3;
-                } while((xstart==xglobal && ystart!=yglobal) || (xprev==xglobal && (xprev-xglobal==2 || xprev-xglobal==-2)));
+                    x=rand()%3;
+                    y=rand()%3;
+                } while((xstart==x && ystart!=y) || (xprev==x && (xprev-x==2 || xprev-x==-2)));
             }
         }
-    }
-    if(turn()>=3) {     //If there are not 2 in a row, place in a random place
+    } else {    //For turns 3+ If there are not 2 in a row, place in a random place unless the center is empty
         do {
-            xglobal=rand()%3;
-            yglobal=rand()%3;
-        } while(board[xglobal][yglobal]!=-1);
+            x=rand()%3;
+            y=rand()%3;
+        } while(board[x][y]!=-1);
+        if(board[1][1]==-1) {
+            x=1;
+            y=1;
+        }
+        xglobal=x;
+        yglobal=y;
         tworow();
     }
 }
 
+void monitorout()
+{
+    for(int i = 0; i < 3; i++) {
+        for(int j = 0; j < 3; j++) {
+            printf("%d ", board[i][j]);
+        }
+        printf("\n");
+    }
+    printf("\n\n");
+}
+
+
+
+
+void playerpc(int chip)
+{
+    int x,y;
+    do {
+        printf("Enter x coordinate\n");
+        scanf("%d",&x);
+        printf("Enter y coordinate\n");
+        scanf("%d",&y);
+    } while(board[x][y]!=-1);
+    board[x][y]=chip;
+    xprev=x;
+    yprev=y;
+    monitorout();
+}
+
 
 
 
 void placechip(int x,int y)
 {
-    drop=.8;
+    drop=.8;        //Base position
     twist=.75;
     //Roll to correct x
     if(x!=0) {