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