swag
Dependencies: Motor Servo mbed_tictactoe
main.cpp@4:6342714f3bc3, 2015-10-26 (annotated)
- Committer:
- mattsims12
- Date:
- Mon Oct 26 11:54:33 2015 +0000
- Revision:
- 4:6342714f3bc3
- Parent:
- 3:317e06586628
- Child:
- 5:e6f9086e0730
swag
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mattsims12 | 3:317e06586628 | 1 | /* |
mattsims12 | 3:317e06586628 | 2 | Matthew Sims and Ryan Limjoco |
mattsims12 | 3:317e06586628 | 3 | Robot that plays you in tic tac toe |
mattsims12 | 3:317e06586628 | 4 | Shorr 1001 |
mattsims12 | 3:317e06586628 | 5 | */ |
mattsims12 | 3:317e06586628 | 6 | |
mattsims12 | 0:233cd31a07a8 | 7 | #include "mbed.h" |
mattsims12 | 0:233cd31a07a8 | 8 | #include "Motor.h" |
mattsims12 | 0:233cd31a07a8 | 9 | #include "Servo.h" |
mattsims12 | 0:233cd31a07a8 | 10 | #include "Timer.h" |
mattsims12 | 0:233cd31a07a8 | 11 | |
mattsims12 | 0:233cd31a07a8 | 12 | DigitalIn in[]= {p16,p17,p18,p19,p20,p15}; |
mattsims12 | 0:233cd31a07a8 | 13 | DigitalOut lights[]= {p5,p6,p7,p8,p11}; |
mattsims12 | 0:233cd31a07a8 | 14 | Timer timer; |
mattsims12 | 0:233cd31a07a8 | 15 | Motor m(p25,p27,p28); |
mattsims12 | 0:233cd31a07a8 | 16 | Servo twist(p22); |
mattsims12 | 0:233cd31a07a8 | 17 | Servo drop(p21); |
mattsims12 | 0:233cd31a07a8 | 18 | |
mattsims12 | 0:233cd31a07a8 | 19 | int board[3][3]; |
mattsims12 | 0:233cd31a07a8 | 20 | int xglobal,yglobal; //Used to get 2 ints out of tworow |
mattsims12 | 0:233cd31a07a8 | 21 | int xprev,yprev,xstart,ystart; //Record Player's Previous move and the starting move of the ai |
mattsims12 | 0:233cd31a07a8 | 22 | |
mattsims12 | 0:233cd31a07a8 | 23 | //Function Prototypes |
mattsims12 | 0:233cd31a07a8 | 24 | int winner(); //Checks winner and returns who won |
mattsims12 | 0:233cd31a07a8 | 25 | void player(int chip); //Player picks space |
mattsims12 | 0:233cd31a07a8 | 26 | void ai(int chip,int difficulty); //Ai picks space |
mattsims12 | 0:233cd31a07a8 | 27 | int turn(); //Determines the turn number(useful for difficulty 4) |
mattsims12 | 0:233cd31a07a8 | 28 | bool tworow(); //Checks for 2 in a row(for ai) |
mattsims12 | 4:6342714f3bc3 | 29 | void perfectplace(); //Picks best spot to put a chip for difficulty 4 |
mattsims12 | 4:6342714f3bc3 | 30 | void playerpc(int chip); //PC input for easier testing |
mattsims12 | 4:6342714f3bc3 | 31 | void monitorout(); //Sisplays the board in tera term for testing purposes |
mattsims12 | 0:233cd31a07a8 | 32 | void placechip(int x, int y); //Moves robot to place chip |
mattsims12 | 0:233cd31a07a8 | 33 | |
mattsims12 | 0:233cd31a07a8 | 34 | int main() |
mattsims12 | 0:233cd31a07a8 | 35 | { |
mattsims12 | 0:233cd31a07a8 | 36 | timer.start(); //Starts timer for the random seed |
mattsims12 | 0:233cd31a07a8 | 37 | twist.calibrate(.0009,90); |
mattsims12 | 0:233cd31a07a8 | 38 | drop.calibrate(.0009,90); |
mattsims12 | 0:233cd31a07a8 | 39 | twist=.75; |
mattsims12 | 0:233cd31a07a8 | 40 | drop=.8; |
mattsims12 | 0:233cd31a07a8 | 41 | |
mattsims12 | 0:233cd31a07a8 | 42 | //Initialize board to -1 |
mattsims12 | 0:233cd31a07a8 | 43 | for(int i=0; i<=2; i++) { |
mattsims12 | 0:233cd31a07a8 | 44 | for(int j=0; j<=2; j++) { |
mattsims12 | 0:233cd31a07a8 | 45 | board[i][j]=-1; |
mattsims12 | 0:233cd31a07a8 | 46 | } |
mattsims12 | 0:233cd31a07a8 | 47 | } |
mattsims12 | 0:233cd31a07a8 | 48 | |
mattsims12 | 0:233cd31a07a8 | 49 | //User Selects Difficulty |
mattsims12 | 4:6342714f3bc3 | 50 | printf("Select Difficulty\n"); |
mattsims12 | 0:233cd31a07a8 | 51 | int difficulty; |
mattsims12 | 4:6342714f3bc3 | 52 | scanf("%d",&difficulty); |
mattsims12 | 4:6342714f3bc3 | 53 | /* |
mattsims12 | 0:233cd31a07a8 | 54 | while(in[0]+in[1]+in[2]+in[3]!=1) { |
mattsims12 | 0:233cd31a07a8 | 55 | } |
mattsims12 | 0:233cd31a07a8 | 56 | if(in[0]==1) |
mattsims12 | 0:233cd31a07a8 | 57 | difficulty=1; |
mattsims12 | 0:233cd31a07a8 | 58 | else if(in[1]==1) |
mattsims12 | 0:233cd31a07a8 | 59 | difficulty=2; |
mattsims12 | 0:233cd31a07a8 | 60 | else if(in[2]==1) |
mattsims12 | 0:233cd31a07a8 | 61 | difficulty=3; |
mattsims12 | 0:233cd31a07a8 | 62 | else if(in[3]==1) |
mattsims12 | 0:233cd31a07a8 | 63 | difficulty=4; |
mattsims12 | 0:233cd31a07a8 | 64 | |
mattsims12 | 0:233cd31a07a8 | 65 | lights[difficulty-1]=1; |
mattsims12 | 4:6342714f3bc3 | 66 | */ |
mattsims12 | 0:233cd31a07a8 | 67 | //Random Seed |
mattsims12 | 0:233cd31a07a8 | 68 | timer.stop(); |
mattsims12 | 0:233cd31a07a8 | 69 | srand(timer.read_us()); |
mattsims12 | 0:233cd31a07a8 | 70 | |
mattsims12 | 0:233cd31a07a8 | 71 | //Pick who is x's and who is o's (0=o,1=x) |
mattsims12 | 0:233cd31a07a8 | 72 | int playerchip,aichip; |
mattsims12 | 0:233cd31a07a8 | 73 | playerchip=rand()%2; |
mattsims12 | 0:233cd31a07a8 | 74 | if(playerchip==0) { |
mattsims12 | 0:233cd31a07a8 | 75 | aichip=1; |
mattsims12 | 0:233cd31a07a8 | 76 | lights[4]=1; |
mattsims12 | 0:233cd31a07a8 | 77 | } else { |
mattsims12 | 0:233cd31a07a8 | 78 | aichip=0; |
mattsims12 | 0:233cd31a07a8 | 79 | } |
mattsims12 | 0:233cd31a07a8 | 80 | |
mattsims12 | 0:233cd31a07a8 | 81 | //Loops of turns until someone wins or a tie |
mattsims12 | 4:6342714f3bc3 | 82 | if(playerchip==1) { |
mattsims12 | 4:6342714f3bc3 | 83 | printf("You go first\n"); |
mattsims12 | 4:6342714f3bc3 | 84 | //player(playerchip); |
mattsims12 | 4:6342714f3bc3 | 85 | playerpc(playerchip); |
mattsims12 | 4:6342714f3bc3 | 86 | } |
mattsims12 | 4:6342714f3bc3 | 87 | while(true) { |
mattsims12 | 4:6342714f3bc3 | 88 | ai(aichip,difficulty); |
mattsims12 | 4:6342714f3bc3 | 89 | if(winner()!=-1) |
mattsims12 | 4:6342714f3bc3 | 90 | break; |
mattsims12 | 4:6342714f3bc3 | 91 | //player(playerchip); |
mattsims12 | 4:6342714f3bc3 | 92 | playerpc(playerchip); |
mattsims12 | 0:233cd31a07a8 | 93 | } |
mattsims12 | 0:233cd31a07a8 | 94 | |
mattsims12 | 4:6342714f3bc3 | 95 | //Operates LED's based on winner |
mattsims12 | 4:6342714f3bc3 | 96 | int w=winner(); |
mattsims12 | 4:6342714f3bc3 | 97 | printf("Winner:%d\n",w); |
mattsims12 | 3:317e06586628 | 98 | lights[difficulty-1]=0; |
mattsims12 | 3:317e06586628 | 99 | lights[4]=0; |
mattsims12 | 0:233cd31a07a8 | 100 | if(winner()==playerchip) { //Light Boucnes back and forth if player wins |
mattsims12 | 0:233cd31a07a8 | 101 | while(1) { |
mattsims12 | 0:233cd31a07a8 | 102 | for(int i=0; i<=4; i++) { |
mattsims12 | 0:233cd31a07a8 | 103 | lights[i]=1; |
mattsims12 | 0:233cd31a07a8 | 104 | wait(.2); |
mattsims12 | 0:233cd31a07a8 | 105 | } |
mattsims12 | 0:233cd31a07a8 | 106 | for(int i=4; i>=0; i--) { |
mattsims12 | 0:233cd31a07a8 | 107 | lights[i]=0; |
mattsims12 | 0:233cd31a07a8 | 108 | wait(.2); |
mattsims12 | 0:233cd31a07a8 | 109 | } |
mattsims12 | 0:233cd31a07a8 | 110 | } |
mattsims12 | 0:233cd31a07a8 | 111 | } else if(winner()==aichip) { |
mattsims12 | 0:233cd31a07a8 | 112 | while(1) { //All lights blink if ai wins |
mattsims12 | 0:233cd31a07a8 | 113 | for(int i=0; i<=4; i++) { |
mattsims12 | 0:233cd31a07a8 | 114 | lights[i]=!lights[i]; |
mattsims12 | 0:233cd31a07a8 | 115 | } |
mattsims12 | 0:233cd31a07a8 | 116 | wait(.25); |
mattsims12 | 0:233cd31a07a8 | 117 | } |
mattsims12 | 0:233cd31a07a8 | 118 | } else { |
mattsims12 | 0:233cd31a07a8 | 119 | for(int i=0; i<=4; i++) { //Lights turn on solid if tue |
mattsims12 | 0:233cd31a07a8 | 120 | lights[i]=1; |
mattsims12 | 0:233cd31a07a8 | 121 | } |
mattsims12 | 0:233cd31a07a8 | 122 | } |
mattsims12 | 0:233cd31a07a8 | 123 | } |
mattsims12 | 0:233cd31a07a8 | 124 | |
mattsims12 | 0:233cd31a07a8 | 125 | |
mattsims12 | 0:233cd31a07a8 | 126 | |
mattsims12 | 0:233cd31a07a8 | 127 | |
mattsims12 | 0:233cd31a07a8 | 128 | //Checks to see if someone won |
mattsims12 | 0:233cd31a07a8 | 129 | int winner() |
mattsims12 | 0:233cd31a07a8 | 130 | { |
mattsims12 | 0:233cd31a07a8 | 131 | int winner=-1; |
mattsims12 | 0:233cd31a07a8 | 132 | //Horizontal and Vertical check |
mattsims12 | 0:233cd31a07a8 | 133 | for(int i=0; i<=2; i++) { |
mattsims12 | 0:233cd31a07a8 | 134 | if(board[i][0]==board[i][1] && board[i][0]==board[i][2] && board[i][0]!=-1) |
mattsims12 | 0:233cd31a07a8 | 135 | winner=board[i][0]; |
mattsims12 | 0:233cd31a07a8 | 136 | if(board[0][i]==board[1][i] && board[0][i]==board[2][i] && board[0][i]!=-1) |
mattsims12 | 0:233cd31a07a8 | 137 | winner=board[0][i]; |
mattsims12 | 0:233cd31a07a8 | 138 | } |
mattsims12 | 0:233cd31a07a8 | 139 | //Diagonal Checks |
mattsims12 | 0:233cd31a07a8 | 140 | if(board[0][0]==board[1][1] && board[0][0]==board[2][2] && board[0][0]!=-1) |
mattsims12 | 0:233cd31a07a8 | 141 | winner=board[0][0]; |
mattsims12 | 0:233cd31a07a8 | 142 | if(board[0][2]==board[1][1] && board[0][2]==board[2][0] && board[0][2]!=-1) |
mattsims12 | 0:233cd31a07a8 | 143 | winner=board[0][2]; |
mattsims12 | 0:233cd31a07a8 | 144 | |
mattsims12 | 0:233cd31a07a8 | 145 | return(winner); |
mattsims12 | 0:233cd31a07a8 | 146 | } |
mattsims12 | 0:233cd31a07a8 | 147 | |
mattsims12 | 0:233cd31a07a8 | 148 | |
mattsims12 | 0:233cd31a07a8 | 149 | |
mattsims12 | 1:c9f7302eac4f | 150 | //Player inputs using the switches until they pick an empty space |
mattsims12 | 0:233cd31a07a8 | 151 | void player(int chip) |
mattsims12 | 0:233cd31a07a8 | 152 | { |
mattsims12 | 4:6342714f3bc3 | 153 | int x=-1; |
mattsims12 | 4:6342714f3bc3 | 154 | int y=-1; |
mattsims12 | 0:233cd31a07a8 | 155 | do { |
mattsims12 | 0:233cd31a07a8 | 156 | if(in[0]+in[1]+in[2]==1 && in[3]+in[4]+in[5]==1) { |
mattsims12 | 0:233cd31a07a8 | 157 | if(in[0]==1) |
mattsims12 | 0:233cd31a07a8 | 158 | x=0; |
mattsims12 | 0:233cd31a07a8 | 159 | else if(in[1]==1) |
mattsims12 | 0:233cd31a07a8 | 160 | x=1; |
mattsims12 | 0:233cd31a07a8 | 161 | else if(in[2]==1) |
mattsims12 | 0:233cd31a07a8 | 162 | x=2; |
mattsims12 | 0:233cd31a07a8 | 163 | |
mattsims12 | 0:233cd31a07a8 | 164 | if(in[3]==1) |
mattsims12 | 0:233cd31a07a8 | 165 | y=0; |
mattsims12 | 0:233cd31a07a8 | 166 | else if(in[4]==1) |
mattsims12 | 0:233cd31a07a8 | 167 | y=1; |
mattsims12 | 0:233cd31a07a8 | 168 | else if(in[5]==1) |
mattsims12 | 0:233cd31a07a8 | 169 | y=2; |
mattsims12 | 0:233cd31a07a8 | 170 | } |
mattsims12 | 0:233cd31a07a8 | 171 | } while(board[x][y]!=-1); |
mattsims12 | 0:233cd31a07a8 | 172 | board[x][y]=chip; |
mattsims12 | 0:233cd31a07a8 | 173 | xprev=x; |
mattsims12 | 0:233cd31a07a8 | 174 | yprev=y; |
mattsims12 | 4:6342714f3bc3 | 175 | monitorout(); |
mattsims12 | 4:6342714f3bc3 | 176 | //placechip(x,y); |
mattsims12 | 0:233cd31a07a8 | 177 | } |
mattsims12 | 0:233cd31a07a8 | 178 | |
mattsims12 | 0:233cd31a07a8 | 179 | |
mattsims12 | 0:233cd31a07a8 | 180 | |
mattsims12 | 0:233cd31a07a8 | 181 | |
mattsims12 | 0:233cd31a07a8 | 182 | //Counts filled spaces to determine how many turns have passed |
mattsims12 | 0:233cd31a07a8 | 183 | int turn() |
mattsims12 | 0:233cd31a07a8 | 184 | { |
mattsims12 | 0:233cd31a07a8 | 185 | int turn=9; |
mattsims12 | 4:6342714f3bc3 | 186 | for(int i=0; i<=2; i++) { |
mattsims12 | 0:233cd31a07a8 | 187 | for(int j=0; j<=2; j++) { |
mattsims12 | 0:233cd31a07a8 | 188 | if(board[i][j]==-1) |
mattsims12 | 0:233cd31a07a8 | 189 | turn--; |
mattsims12 | 0:233cd31a07a8 | 190 | } |
mattsims12 | 0:233cd31a07a8 | 191 | } |
mattsims12 | 0:233cd31a07a8 | 192 | return turn; |
mattsims12 | 0:233cd31a07a8 | 193 | } |
mattsims12 | 0:233cd31a07a8 | 194 | |
mattsims12 | 0:233cd31a07a8 | 195 | |
mattsims12 | 0:233cd31a07a8 | 196 | |
mattsims12 | 0:233cd31a07a8 | 197 | |
mattsims12 | 0:233cd31a07a8 | 198 | //Easy randomly picks any empty space on the board |
mattsims12 | 0:233cd31a07a8 | 199 | void ai(int chip,int difficulty) |
mattsims12 | 0:233cd31a07a8 | 200 | { |
mattsims12 | 0:233cd31a07a8 | 201 | int x,y; |
mattsims12 | 0:233cd31a07a8 | 202 | |
mattsims12 | 0:233cd31a07a8 | 203 | if(difficulty!=4) { |
mattsims12 | 0:233cd31a07a8 | 204 | do { |
mattsims12 | 0:233cd31a07a8 | 205 | x=rand()%2; |
mattsims12 | 0:233cd31a07a8 | 206 | y=rand()%2; |
mattsims12 | 0:233cd31a07a8 | 207 | } while(board[x][y]!=-1); |
mattsims12 | 0:233cd31a07a8 | 208 | |
mattsims12 | 0:233cd31a07a8 | 209 | if(difficulty==2 || difficulty==3) { //Middle difficulties have a random chance of not blocking |
mattsims12 | 0:233cd31a07a8 | 210 | if(tworow()) { |
mattsims12 | 4:6342714f3bc3 | 211 | if((difficulty==2 && rand()%10<8) || difficulty==3) { //Sometimes doesn't notice two in a row |
mattsims12 | 0:233cd31a07a8 | 212 | x=xglobal; |
mattsims12 | 0:233cd31a07a8 | 213 | y=yglobal; |
mattsims12 | 0:233cd31a07a8 | 214 | } |
mattsims12 | 0:233cd31a07a8 | 215 | } |
mattsims12 | 0:233cd31a07a8 | 216 | } |
mattsims12 | 4:6342714f3bc3 | 217 | } else { |
mattsims12 | 4:6342714f3bc3 | 218 | perfectplace(); |
mattsims12 | 4:6342714f3bc3 | 219 | x=xglobal; |
mattsims12 | 4:6342714f3bc3 | 220 | y=yglobal; |
mattsims12 | 0:233cd31a07a8 | 221 | } |
mattsims12 | 4:6342714f3bc3 | 222 | //placechip(x,y); |
mattsims12 | 4:6342714f3bc3 | 223 | board[x][y]=chip; |
mattsims12 | 4:6342714f3bc3 | 224 | monitorout(); |
mattsims12 | 0:233cd31a07a8 | 225 | } |
mattsims12 | 0:233cd31a07a8 | 226 | |
mattsims12 | 0:233cd31a07a8 | 227 | |
mattsims12 | 0:233cd31a07a8 | 228 | |
mattsims12 | 0:233cd31a07a8 | 229 | bool tworow() |
mattsims12 | 0:233cd31a07a8 | 230 | { |
mattsims12 | 4:6342714f3bc3 | 231 | int x,y; |
mattsims12 | 0:233cd31a07a8 | 232 | //Vertical and horizontals |
mattsims12 | 0:233cd31a07a8 | 233 | for(int i=0; i<=2; i++) { |
mattsims12 | 0:233cd31a07a8 | 234 | if(board[0][i]==board[1][i] && board[2][i]==-1) { |
mattsims12 | 4:6342714f3bc3 | 235 | x=2; |
mattsims12 | 4:6342714f3bc3 | 236 | y=i; //board[2][i] |
mattsims12 | 4:6342714f3bc3 | 237 | } else if(board[1][i]==board[2][i] && board[0][i]==-1) { |
mattsims12 | 4:6342714f3bc3 | 238 | x=0; |
mattsims12 | 4:6342714f3bc3 | 239 | y=i; //board[0][i] |
mattsims12 | 4:6342714f3bc3 | 240 | } else if(board[0][i]==board[2][i] && board[1][i]==-1) { |
mattsims12 | 4:6342714f3bc3 | 241 | x=1; |
mattsims12 | 4:6342714f3bc3 | 242 | y=i; //board[1][i] |
mattsims12 | 4:6342714f3bc3 | 243 | } else if(board[i][0]==board[i][1] && board[i][2]==-1) { |
mattsims12 | 4:6342714f3bc3 | 244 | x=i; |
mattsims12 | 4:6342714f3bc3 | 245 | y=2; //board[i][2] |
mattsims12 | 4:6342714f3bc3 | 246 | } else if(board[i][1]==board[i][2] && board[i][0]==-1) { |
mattsims12 | 4:6342714f3bc3 | 247 | x=i; |
mattsims12 | 4:6342714f3bc3 | 248 | y=0; //board[i][0] |
mattsims12 | 4:6342714f3bc3 | 249 | } else if(board[i][0]==board[i][2] && board[i][1]==-1) { |
mattsims12 | 4:6342714f3bc3 | 250 | x=i; |
mattsims12 | 4:6342714f3bc3 | 251 | y=1; //board[i][1] |
mattsims12 | 4:6342714f3bc3 | 252 | } else if(board[0][0]==board[1][1] && board[2][2]==-1) { //Diagonals |
mattsims12 | 4:6342714f3bc3 | 253 | x=2; |
mattsims12 | 4:6342714f3bc3 | 254 | y=2; //board[2][2] |
mattsims12 | 4:6342714f3bc3 | 255 | } else if(board[1][1]==board[2][2] && board[0][0]==-1) { |
mattsims12 | 4:6342714f3bc3 | 256 | x=0; |
mattsims12 | 4:6342714f3bc3 | 257 | y=0; //board[0][0] |
mattsims12 | 4:6342714f3bc3 | 258 | } else if(board[0][0]==board[2][2] && board[1][1]==-1) { |
mattsims12 | 4:6342714f3bc3 | 259 | x=1; |
mattsims12 | 4:6342714f3bc3 | 260 | y=1; //board[1][1] |
mattsims12 | 4:6342714f3bc3 | 261 | } else if(board[0][2]==board[1][1] && board[2][0]==-1) { |
mattsims12 | 4:6342714f3bc3 | 262 | x=2; |
mattsims12 | 4:6342714f3bc3 | 263 | y=0; //board[2][0] |
mattsims12 | 4:6342714f3bc3 | 264 | } else if(board[1][1]==board[2][0] && board[0][2]==-1) { |
mattsims12 | 4:6342714f3bc3 | 265 | x=0; |
mattsims12 | 4:6342714f3bc3 | 266 | y=2; //board[0][2] |
mattsims12 | 4:6342714f3bc3 | 267 | } else if(board[0][2]==board[2][0] && board[1][1]==-1) { |
mattsims12 | 4:6342714f3bc3 | 268 | x=1; |
mattsims12 | 4:6342714f3bc3 | 269 | y=1; //board[1][1] |
mattsims12 | 0:233cd31a07a8 | 270 | } |
mattsims12 | 0:233cd31a07a8 | 271 | } |
mattsims12 | 4:6342714f3bc3 | 272 | |
mattsims12 | 4:6342714f3bc3 | 273 | if(x!=-1 && y!=-1) { //Returns true if there are 2 in a row |
mattsims12 | 4:6342714f3bc3 | 274 | xglobal=x; |
mattsims12 | 4:6342714f3bc3 | 275 | yglobal=y; |
mattsims12 | 4:6342714f3bc3 | 276 | return true; |
mattsims12 | 4:6342714f3bc3 | 277 | } else { |
mattsims12 | 4:6342714f3bc3 | 278 | return false; |
mattsims12 | 0:233cd31a07a8 | 279 | } |
mattsims12 | 0:233cd31a07a8 | 280 | } |
mattsims12 | 0:233cd31a07a8 | 281 | |
mattsims12 | 0:233cd31a07a8 | 282 | |
mattsims12 | 0:233cd31a07a8 | 283 | |
mattsims12 | 0:233cd31a07a8 | 284 | void perfectplace() |
mattsims12 | 0:233cd31a07a8 | 285 | { |
mattsims12 | 4:6342714f3bc3 | 286 | int x,y; |
mattsims12 | 0:233cd31a07a8 | 287 | if(turn()==0) { //Places in a random corner if first |
mattsims12 | 4:6342714f3bc3 | 288 | x=(rand()%2)*2; //Picks either 0 or 2 |
mattsims12 | 4:6342714f3bc3 | 289 | y=(rand()%2)*2; |
mattsims12 | 4:6342714f3bc3 | 290 | xstart=x; |
mattsims12 | 4:6342714f3bc3 | 291 | ystart=y; |
mattsims12 | 4:6342714f3bc3 | 292 | } else if(turn()==1) { |
mattsims12 | 0:233cd31a07a8 | 293 | if(xprev!=1 && yprev!=1) { //If player takes corner, ai takes center |
mattsims12 | 4:6342714f3bc3 | 294 | x=1; |
mattsims12 | 4:6342714f3bc3 | 295 | y=1; |
mattsims12 | 0:233cd31a07a8 | 296 | } else if(xprev==1 && yprev==1) { //If player takes center, ai takes a corner |
mattsims12 | 4:6342714f3bc3 | 297 | x=(rand()%2)*2; //Picks either 0 or 2 |
mattsims12 | 4:6342714f3bc3 | 298 | y=(rand()%2)*2; |
mattsims12 | 0:233cd31a07a8 | 299 | } else { //If player takes edge, ai takes spot in either same row or col |
mattsims12 | 0:233cd31a07a8 | 300 | if(xprev==1) { |
mattsims12 | 4:6342714f3bc3 | 301 | x=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 302 | if(x==xprev) { |
mattsims12 | 0:233cd31a07a8 | 303 | do { |
mattsims12 | 4:6342714f3bc3 | 304 | y=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 305 | } while(y==yprev); |
mattsims12 | 0:233cd31a07a8 | 306 | } else { |
mattsims12 | 4:6342714f3bc3 | 307 | y=yprev; |
mattsims12 | 0:233cd31a07a8 | 308 | } |
mattsims12 | 0:233cd31a07a8 | 309 | } else { |
mattsims12 | 4:6342714f3bc3 | 310 | y=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 311 | if(y==yprev) { |
mattsims12 | 0:233cd31a07a8 | 312 | do { |
mattsims12 | 4:6342714f3bc3 | 313 | x=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 314 | } while(x==xprev); |
mattsims12 | 0:233cd31a07a8 | 315 | } else { |
mattsims12 | 4:6342714f3bc3 | 316 | x=xprev; |
mattsims12 | 0:233cd31a07a8 | 317 | } |
mattsims12 | 0:233cd31a07a8 | 318 | } |
mattsims12 | 0:233cd31a07a8 | 319 | } |
mattsims12 | 4:6342714f3bc3 | 320 | } else if(turn()==2) { |
mattsims12 | 0:233cd31a07a8 | 321 | if(xprev==1 && yprev==1) { //If user places in middle, any open spot leads to tie |
mattsims12 | 0:233cd31a07a8 | 322 | do { |
mattsims12 | 4:6342714f3bc3 | 323 | x=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 324 | y=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 325 | } while(board[x][y]!=-1); |
mattsims12 | 0:233cd31a07a8 | 326 | } else if((xprev==1 ||yprev==1) && (xprev!=xstart && yprev!=ystart)) { //Opposite edge |
mattsims12 | 0:233cd31a07a8 | 327 | if(xprev-xstart==2 || xprev-xstart==-2) { |
mattsims12 | 4:6342714f3bc3 | 328 | y=ystart; |
mattsims12 | 0:233cd31a07a8 | 329 | if(yprev+ystart==1) |
mattsims12 | 4:6342714f3bc3 | 330 | x=2; |
mattsims12 | 0:233cd31a07a8 | 331 | else |
mattsims12 | 4:6342714f3bc3 | 332 | x=0; |
mattsims12 | 0:233cd31a07a8 | 333 | } else { |
mattsims12 | 4:6342714f3bc3 | 334 | x=xstart; |
mattsims12 | 0:233cd31a07a8 | 335 | if(xprev+xstart==1) |
mattsims12 | 4:6342714f3bc3 | 336 | y=2; |
mattsims12 | 0:233cd31a07a8 | 337 | else |
mattsims12 | 4:6342714f3bc3 | 338 | y=0; |
mattsims12 | 0:233cd31a07a8 | 339 | } |
mattsims12 | 0:233cd31a07a8 | 340 | } else if((xprev==1 || yprev==1) && (xprev==xstart || yprev==ystart)) { //Adjascent edge |
mattsims12 | 0:233cd31a07a8 | 341 | if(xprev==xstart) { |
mattsims12 | 0:233cd31a07a8 | 342 | do { |
mattsims12 | 4:6342714f3bc3 | 343 | x=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 344 | y=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 345 | } while(((ystart!=y) && (xstart==x)) || (x==1 && y==1)); |
mattsims12 | 0:233cd31a07a8 | 346 | } else { |
mattsims12 | 0:233cd31a07a8 | 347 | do { |
mattsims12 | 4:6342714f3bc3 | 348 | x=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 349 | y=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 350 | } while(((xstart!=x) && (ystart==y)) || (x==1 && y==1)); |
mattsims12 | 0:233cd31a07a8 | 351 | } |
mattsims12 | 0:233cd31a07a8 | 352 | } else if((xprev-xstart==2 || xprev-xstart==-2) && (yprev-ystart==2 || yprev-ystart==-2)) { //Opposite Corner |
mattsims12 | 0:233cd31a07a8 | 353 | do { |
mattsims12 | 4:6342714f3bc3 | 354 | x=(rand()%2)*2; |
mattsims12 | 4:6342714f3bc3 | 355 | y=(rand()%2)*2; |
mattsims12 | 4:6342714f3bc3 | 356 | } while(board[x][y]!=-1); |
mattsims12 | 0:233cd31a07a8 | 357 | } else { //Adjascent Corner |
mattsims12 | 0:233cd31a07a8 | 358 | if(xprev==xstart) { |
mattsims12 | 0:233cd31a07a8 | 359 | do { |
mattsims12 | 4:6342714f3bc3 | 360 | x=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 361 | y=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 362 | } while((ystart==y && xstart!=x) || (yprev==y && (yprev-y==2 || yprev-y==-2))); |
mattsims12 | 0:233cd31a07a8 | 363 | } else { |
mattsims12 | 0:233cd31a07a8 | 364 | do { |
mattsims12 | 4:6342714f3bc3 | 365 | x=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 366 | y=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 367 | } while((xstart==x && ystart!=y) || (xprev==x && (xprev-x==2 || xprev-x==-2))); |
mattsims12 | 0:233cd31a07a8 | 368 | } |
mattsims12 | 0:233cd31a07a8 | 369 | } |
mattsims12 | 4:6342714f3bc3 | 370 | } else { //For turns 3+ If there are not 2 in a row, place in a random place unless the center is empty |
mattsims12 | 0:233cd31a07a8 | 371 | do { |
mattsims12 | 4:6342714f3bc3 | 372 | x=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 373 | y=rand()%3; |
mattsims12 | 4:6342714f3bc3 | 374 | } while(board[x][y]!=-1); |
mattsims12 | 4:6342714f3bc3 | 375 | if(board[1][1]==-1) { |
mattsims12 | 4:6342714f3bc3 | 376 | x=1; |
mattsims12 | 4:6342714f3bc3 | 377 | y=1; |
mattsims12 | 4:6342714f3bc3 | 378 | } |
mattsims12 | 4:6342714f3bc3 | 379 | xglobal=x; |
mattsims12 | 4:6342714f3bc3 | 380 | yglobal=y; |
mattsims12 | 0:233cd31a07a8 | 381 | tworow(); |
mattsims12 | 0:233cd31a07a8 | 382 | } |
mattsims12 | 0:233cd31a07a8 | 383 | } |
mattsims12 | 0:233cd31a07a8 | 384 | |
mattsims12 | 4:6342714f3bc3 | 385 | void monitorout() |
mattsims12 | 4:6342714f3bc3 | 386 | { |
mattsims12 | 4:6342714f3bc3 | 387 | for(int i = 0; i < 3; i++) { |
mattsims12 | 4:6342714f3bc3 | 388 | for(int j = 0; j < 3; j++) { |
mattsims12 | 4:6342714f3bc3 | 389 | printf("%d ", board[i][j]); |
mattsims12 | 4:6342714f3bc3 | 390 | } |
mattsims12 | 4:6342714f3bc3 | 391 | printf("\n"); |
mattsims12 | 4:6342714f3bc3 | 392 | } |
mattsims12 | 4:6342714f3bc3 | 393 | printf("\n\n"); |
mattsims12 | 4:6342714f3bc3 | 394 | } |
mattsims12 | 4:6342714f3bc3 | 395 | |
mattsims12 | 4:6342714f3bc3 | 396 | |
mattsims12 | 4:6342714f3bc3 | 397 | |
mattsims12 | 4:6342714f3bc3 | 398 | |
mattsims12 | 4:6342714f3bc3 | 399 | void playerpc(int chip) |
mattsims12 | 4:6342714f3bc3 | 400 | { |
mattsims12 | 4:6342714f3bc3 | 401 | int x,y; |
mattsims12 | 4:6342714f3bc3 | 402 | do { |
mattsims12 | 4:6342714f3bc3 | 403 | printf("Enter x coordinate\n"); |
mattsims12 | 4:6342714f3bc3 | 404 | scanf("%d",&x); |
mattsims12 | 4:6342714f3bc3 | 405 | printf("Enter y coordinate\n"); |
mattsims12 | 4:6342714f3bc3 | 406 | scanf("%d",&y); |
mattsims12 | 4:6342714f3bc3 | 407 | } while(board[x][y]!=-1); |
mattsims12 | 4:6342714f3bc3 | 408 | board[x][y]=chip; |
mattsims12 | 4:6342714f3bc3 | 409 | xprev=x; |
mattsims12 | 4:6342714f3bc3 | 410 | yprev=y; |
mattsims12 | 4:6342714f3bc3 | 411 | monitorout(); |
mattsims12 | 4:6342714f3bc3 | 412 | } |
mattsims12 | 4:6342714f3bc3 | 413 | |
mattsims12 | 0:233cd31a07a8 | 414 | |
mattsims12 | 0:233cd31a07a8 | 415 | |
mattsims12 | 0:233cd31a07a8 | 416 | |
mattsims12 | 0:233cd31a07a8 | 417 | void placechip(int x,int y) |
mattsims12 | 0:233cd31a07a8 | 418 | { |
mattsims12 | 4:6342714f3bc3 | 419 | drop=.8; //Base position |
mattsims12 | 0:233cd31a07a8 | 420 | twist=.75; |
mattsims12 | 0:233cd31a07a8 | 421 | //Roll to correct x |
mattsims12 | 0:233cd31a07a8 | 422 | if(x!=0) { |
mattsims12 | 0:233cd31a07a8 | 423 | m.speed(-.4); |
mattsims12 | 0:233cd31a07a8 | 424 | wait(.25); |
mattsims12 | 0:233cd31a07a8 | 425 | if(x==2) |
mattsims12 | 0:233cd31a07a8 | 426 | wait(.15); |
mattsims12 | 0:233cd31a07a8 | 427 | } |
mattsims12 | 0:233cd31a07a8 | 428 | m.speed(0.0); |
mattsims12 | 0:233cd31a07a8 | 429 | //Twist to correct y |
mattsims12 | 0:233cd31a07a8 | 430 | if(y==0) |
mattsims12 | 0:233cd31a07a8 | 431 | twist=1.0; |
mattsims12 | 0:233cd31a07a8 | 432 | else if(y==1) |
mattsims12 | 0:233cd31a07a8 | 433 | twist=.75; |
mattsims12 | 0:233cd31a07a8 | 434 | else |
mattsims12 | 0:233cd31a07a8 | 435 | twist=.5; |
mattsims12 | 0:233cd31a07a8 | 436 | wait(.25); |
mattsims12 | 0:233cd31a07a8 | 437 | //Drop a Chip |
mattsims12 | 0:233cd31a07a8 | 438 | drop=.5; |
mattsims12 | 0:233cd31a07a8 | 439 | wait(.1); |
mattsims12 | 0:233cd31a07a8 | 440 | drop=.8; |
mattsims12 | 0:233cd31a07a8 | 441 | wait(.25); |
mattsims12 | 0:233cd31a07a8 | 442 | twist=.75; |
mattsims12 | 0:233cd31a07a8 | 443 | //Roll back to origin |
mattsims12 | 0:233cd31a07a8 | 444 | if(x!=0) { |
mattsims12 | 0:233cd31a07a8 | 445 | m.speed(.5); |
mattsims12 | 0:233cd31a07a8 | 446 | wait(.25); |
mattsims12 | 0:233cd31a07a8 | 447 | if(x==2) |
mattsims12 | 0:233cd31a07a8 | 448 | wait(.15); |
mattsims12 | 0:233cd31a07a8 | 449 | } |
mattsims12 | 0:233cd31a07a8 | 450 | m.speed(0.0); |
mattsims12 | 0:233cd31a07a8 | 451 | wait(5); |
mattsims12 | 0:233cd31a07a8 | 452 | } |