Final

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

Fork of Lab4-Reversi-v2 by Timothy Li

Committer:
EvolutionOfWar
Date:
Tue Nov 01 13:59:02 2016 +0000
Revision:
2:1f7c6cc19a9a
Parent:
1:cc72ad58982b
Child:
3:b1fe86f61f2f
Added Sound and instant end

Who changed what in which revision?

UserRevisionLine numberNew contents of line
EvolutionOfWar 0:fce0f9489de3 1 #include "mbed.h"
EvolutionOfWar 0:fce0f9489de3 2 #include "uLCD_4DGL.h"
aolmenki 1:cc72ad58982b 3 #include "SDFileSystem.h"
aolmenki 1:cc72ad58982b 4 #include "wave_player.h"
EvolutionOfWar 2:1f7c6cc19a9a 5 #include "stdio.h"
EvolutionOfWar 0:fce0f9489de3 6 uLCD_4DGL uLCD(p28, p27, p29); // create a global lcd object
aolmenki 1:cc72ad58982b 7 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
aolmenki 1:cc72ad58982b 8 PwmOut Speaker(p21);
aolmenki 1:cc72ad58982b 9 AnalogOut DACout(p18);
EvolutionOfWar 0:fce0f9489de3 10
EvolutionOfWar 2:1f7c6cc19a9a 11 DigitalIn pb(p10);
EvolutionOfWar 2:1f7c6cc19a9a 12
aolmenki 1:cc72ad58982b 13 wave_player waver(&DACout);
EvolutionOfWar 0:fce0f9489de3 14
EvolutionOfWar 0:fce0f9489de3 15 class Nav_Switch
EvolutionOfWar 0:fce0f9489de3 16 {
EvolutionOfWar 0:fce0f9489de3 17 public:
EvolutionOfWar 0:fce0f9489de3 18 Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire);
EvolutionOfWar 0:fce0f9489de3 19 int read();
EvolutionOfWar 0:fce0f9489de3 20 //boolean functions to test each switch
EvolutionOfWar 0:fce0f9489de3 21 bool up();
EvolutionOfWar 0:fce0f9489de3 22 bool down();
EvolutionOfWar 0:fce0f9489de3 23 bool left();
EvolutionOfWar 0:fce0f9489de3 24 bool right();
EvolutionOfWar 0:fce0f9489de3 25 bool fire();
EvolutionOfWar 0:fce0f9489de3 26 //automatic read on RHS
EvolutionOfWar 0:fce0f9489de3 27 operator int ();
EvolutionOfWar 0:fce0f9489de3 28 //index to any switch array style
EvolutionOfWar 0:fce0f9489de3 29 bool operator[](int index) {
EvolutionOfWar 0:fce0f9489de3 30 return _pins[index];
EvolutionOfWar 0:fce0f9489de3 31 };
EvolutionOfWar 0:fce0f9489de3 32 private:
EvolutionOfWar 0:fce0f9489de3 33 BusIn _pins;
EvolutionOfWar 0:fce0f9489de3 34
EvolutionOfWar 0:fce0f9489de3 35 };
EvolutionOfWar 0:fce0f9489de3 36 Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
EvolutionOfWar 0:fce0f9489de3 37 _pins(up, down, left, right, fire)
EvolutionOfWar 0:fce0f9489de3 38 {
EvolutionOfWar 0:fce0f9489de3 39 _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise
EvolutionOfWar 0:fce0f9489de3 40 wait(0.001); //delays just a bit for pullups to pull inputs high
EvolutionOfWar 0:fce0f9489de3 41 }
EvolutionOfWar 0:fce0f9489de3 42 inline bool Nav_Switch::up()
EvolutionOfWar 0:fce0f9489de3 43 {
EvolutionOfWar 0:fce0f9489de3 44 return !(_pins[0]);
EvolutionOfWar 0:fce0f9489de3 45 }
EvolutionOfWar 0:fce0f9489de3 46 inline bool Nav_Switch::down()
EvolutionOfWar 0:fce0f9489de3 47 {
EvolutionOfWar 0:fce0f9489de3 48 return !(_pins[1]);
EvolutionOfWar 0:fce0f9489de3 49 }
EvolutionOfWar 0:fce0f9489de3 50 inline bool Nav_Switch::left()
EvolutionOfWar 0:fce0f9489de3 51 {
EvolutionOfWar 0:fce0f9489de3 52 return !(_pins[2]);
EvolutionOfWar 0:fce0f9489de3 53 }
EvolutionOfWar 0:fce0f9489de3 54 inline bool Nav_Switch::right()
EvolutionOfWar 0:fce0f9489de3 55 {
EvolutionOfWar 0:fce0f9489de3 56 return !(_pins[3]);
EvolutionOfWar 0:fce0f9489de3 57 }
EvolutionOfWar 0:fce0f9489de3 58 inline bool Nav_Switch::fire()
EvolutionOfWar 0:fce0f9489de3 59 {
EvolutionOfWar 0:fce0f9489de3 60 return !(_pins[4]);
EvolutionOfWar 0:fce0f9489de3 61 }
EvolutionOfWar 0:fce0f9489de3 62 inline int Nav_Switch::read()
EvolutionOfWar 0:fce0f9489de3 63 {
EvolutionOfWar 0:fce0f9489de3 64 return _pins.read();
EvolutionOfWar 0:fce0f9489de3 65 }
EvolutionOfWar 0:fce0f9489de3 66 inline Nav_Switch::operator int ()
EvolutionOfWar 0:fce0f9489de3 67 {
EvolutionOfWar 0:fce0f9489de3 68 return _pins.read();
EvolutionOfWar 0:fce0f9489de3 69 }
EvolutionOfWar 0:fce0f9489de3 70
EvolutionOfWar 0:fce0f9489de3 71 Nav_Switch myNav( p14, p11, p12, p10, p13); //pin order on Sparkfun breakout
EvolutionOfWar 0:fce0f9489de3 72
aolmenki 1:cc72ad58982b 73 int turn = 1; // black goes on 1, white goes on 2 (or 0)
EvolutionOfWar 0:fce0f9489de3 74 int gb[8][8];
EvolutionOfWar 0:fce0f9489de3 75 int NUM_COLS = 8;
EvolutionOfWar 0:fce0f9489de3 76 int NUM_ROWS = 8;
aolmenki 1:cc72ad58982b 77 int xCoor = 3;
aolmenki 1:cc72ad58982b 78 int yCoor = 4;
aolmenki 1:cc72ad58982b 79 int numBlack = 0;
aolmenki 1:cc72ad58982b 80 int numWhite = 0;
aolmenki 1:cc72ad58982b 81 bool moved = false;
aolmenki 1:cc72ad58982b 82 bool gameover = false;
aolmenki 1:cc72ad58982b 83
EvolutionOfWar 0:fce0f9489de3 84 void initialize_game_board()
EvolutionOfWar 0:fce0f9489de3 85 {
EvolutionOfWar 0:fce0f9489de3 86 //This is a nested loop to make sure every cell is empty
EvolutionOfWar 0:fce0f9489de3 87 //Cell Codes: 0 = empty, 1 = white piece, 2 = black piece
EvolutionOfWar 0:fce0f9489de3 88 for (int i = 0; i < NUM_ROWS; i++)
EvolutionOfWar 0:fce0f9489de3 89 {
EvolutionOfWar 0:fce0f9489de3 90 for (int j = 0; j < NUM_COLS; j++)
EvolutionOfWar 0:fce0f9489de3 91 gb[i][j] = 0;
EvolutionOfWar 0:fce0f9489de3 92 }
EvolutionOfWar 0:fce0f9489de3 93 gb[3][3] = 1;//Put down white piece
EvolutionOfWar 0:fce0f9489de3 94 gb[4][4] = 1;//Put down white piece
EvolutionOfWar 0:fce0f9489de3 95 gb[3][4] = 2;//Put down black piece
EvolutionOfWar 0:fce0f9489de3 96 gb[4][3] = 2;//Put down black piece
EvolutionOfWar 0:fce0f9489de3 97
EvolutionOfWar 0:fce0f9489de3 98 uLCD.cls();
aolmenki 1:cc72ad58982b 99 uLCD.filled_rectangle(0,0,127,127,0x007f00);
EvolutionOfWar 0:fce0f9489de3 100
EvolutionOfWar 0:fce0f9489de3 101 uLCD.rectangle(7,7,120,120,DGREY);
EvolutionOfWar 0:fce0f9489de3 102 uLCD.rectangle(8,8,119,119,DGREY);
EvolutionOfWar 0:fce0f9489de3 103 //uLCD.filled_rectangle(8,20,120,21,DGREY);
EvolutionOfWar 0:fce0f9489de3 104
EvolutionOfWar 0:fce0f9489de3 105 for( int i = 1; i< 9; i++)
EvolutionOfWar 0:fce0f9489de3 106 {
EvolutionOfWar 0:fce0f9489de3 107 uLCD.filled_rectangle(8, 7 + 14*i,119,8 + 14*i,DGREY);
EvolutionOfWar 0:fce0f9489de3 108 }
EvolutionOfWar 0:fce0f9489de3 109 for( int j = 1; j<9; j++)
EvolutionOfWar 0:fce0f9489de3 110 {
EvolutionOfWar 0:fce0f9489de3 111 uLCD.filled_rectangle(7 + 14*j, 8, 8+14*j, 119, DGREY);
EvolutionOfWar 0:fce0f9489de3 112 }
EvolutionOfWar 0:fce0f9489de3 113 }
EvolutionOfWar 0:fce0f9489de3 114
aolmenki 1:cc72ad58982b 115 void drawCursor(int x, int y) //Expecting x ={0,7} and y ={0,7}
EvolutionOfWar 0:fce0f9489de3 116 {
aolmenki 1:cc72ad58982b 117 uLCD.filled_rectangle(7 + 14*x, 7 + 14*y,8 + 14*x,22 + 14*y,RED);
aolmenki 1:cc72ad58982b 118 uLCD.filled_rectangle(7 + 14*x, 7 + 14*y,22 + 14*x,8 + 14*y,RED);
aolmenki 1:cc72ad58982b 119 uLCD.filled_rectangle(7 + 14*x, 21 + 14*y,22 + 14*x,22 + 14*y,RED);
aolmenki 1:cc72ad58982b 120 uLCD.filled_rectangle(21 + 14*x, 7 + 14*y,22 + 14*x,22 + 14*y,RED);
EvolutionOfWar 0:fce0f9489de3 121 }
EvolutionOfWar 0:fce0f9489de3 122
aolmenki 1:cc72ad58982b 123 void removeCursor(int x,int y)
EvolutionOfWar 0:fce0f9489de3 124 {
aolmenki 1:cc72ad58982b 125 uLCD.filled_rectangle(7 + 14*x, 7 + 14*y,8 + 14*x,22 + 14*y,DGREY);
aolmenki 1:cc72ad58982b 126 uLCD.filled_rectangle(7 + 14*x, 7 + 14*y,22 + 14*x,8 + 14*y,DGREY);
aolmenki 1:cc72ad58982b 127 uLCD.filled_rectangle(7 + 14*x, 21 + 14*y,22 + 14*x,22 + 14*y,DGREY);
aolmenki 1:cc72ad58982b 128 uLCD.filled_rectangle(21 + 14*x, 7 + 14*y,22 + 14*x,22 + 14*y,DGREY);
EvolutionOfWar 0:fce0f9489de3 129 }
EvolutionOfWar 0:fce0f9489de3 130
EvolutionOfWar 0:fce0f9489de3 131 void drawPieces(){
EvolutionOfWar 0:fce0f9489de3 132 for (int i = 0; i < NUM_ROWS; i++)
EvolutionOfWar 0:fce0f9489de3 133 {
EvolutionOfWar 0:fce0f9489de3 134 for (int j = 0; j < NUM_COLS; j++)
EvolutionOfWar 0:fce0f9489de3 135 {
aolmenki 1:cc72ad58982b 136 if(gb[i][j] == 0){
aolmenki 1:cc72ad58982b 137 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0x007f00);
aolmenki 1:cc72ad58982b 138 }
EvolutionOfWar 0:fce0f9489de3 139 if(gb[i][j] == 1){
EvolutionOfWar 0:fce0f9489de3 140 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , WHITE);
EvolutionOfWar 0:fce0f9489de3 141 }
EvolutionOfWar 0:fce0f9489de3 142 if(gb[i][j] == 2){
EvolutionOfWar 0:fce0f9489de3 143 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , BLACK);
EvolutionOfWar 0:fce0f9489de3 144 }
EvolutionOfWar 0:fce0f9489de3 145 }
EvolutionOfWar 0:fce0f9489de3 146 }
EvolutionOfWar 0:fce0f9489de3 147 }
EvolutionOfWar 0:fce0f9489de3 148
EvolutionOfWar 0:fce0f9489de3 149 void drawPiece(int i, int j) { //xCoor, yCoor
aolmenki 1:cc72ad58982b 150 if (turn == 0) {
aolmenki 1:cc72ad58982b 151 gb[i][j] = 1;
aolmenki 1:cc72ad58982b 152 } else {
aolmenki 1:cc72ad58982b 153 gb[i][j] = 2;
aolmenki 1:cc72ad58982b 154 }
EvolutionOfWar 0:fce0f9489de3 155 if(gb[i][j] == 1){
EvolutionOfWar 0:fce0f9489de3 156 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , WHITE);
EvolutionOfWar 0:fce0f9489de3 157 }
EvolutionOfWar 0:fce0f9489de3 158 if(gb[i][j] == 2){
EvolutionOfWar 0:fce0f9489de3 159 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , BLACK);
EvolutionOfWar 0:fce0f9489de3 160 }
EvolutionOfWar 0:fce0f9489de3 161 }
EvolutionOfWar 0:fce0f9489de3 162
EvolutionOfWar 0:fce0f9489de3 163 void flippingPiece(int i, int j) { //xCoor, yCoor, animation
aolmenki 1:cc72ad58982b 164
aolmenki 1:cc72ad58982b 165 if(gb[i][j] == 2){ //Gradually changes a piece White
aolmenki 1:cc72ad58982b 166 gb[i][j] = 1;
EvolutionOfWar 0:fce0f9489de3 167 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0x4c4c4c);
EvolutionOfWar 0:fce0f9489de3 168 wait(0.1);
EvolutionOfWar 0:fce0f9489de3 169 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0xb2b2b2);
EvolutionOfWar 0:fce0f9489de3 170 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0x4c4c4c);
EvolutionOfWar 0:fce0f9489de3 171 wait(0.1);
EvolutionOfWar 0:fce0f9489de3 172 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0xffffff);
EvolutionOfWar 0:fce0f9489de3 173 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0xb2b2b2);
EvolutionOfWar 0:fce0f9489de3 174 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0x4c4c4c);
EvolutionOfWar 0:fce0f9489de3 175 wait(0.1);
EvolutionOfWar 0:fce0f9489de3 176 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0xffffff);
EvolutionOfWar 0:fce0f9489de3 177 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0xb2b2b2);
EvolutionOfWar 0:fce0f9489de3 178 wait(0.1);
EvolutionOfWar 0:fce0f9489de3 179 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0xffffff);
EvolutionOfWar 0:fce0f9489de3 180 wait(0.1);
aolmenki 1:cc72ad58982b 181 } else if(gb[i][j] == 1){ //Gradually changes a piece Black
aolmenki 1:cc72ad58982b 182 gb[i][j] = 2;
EvolutionOfWar 0:fce0f9489de3 183 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0xb2b2b2);
EvolutionOfWar 0:fce0f9489de3 184 wait(0.1);
EvolutionOfWar 0:fce0f9489de3 185 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0x4c4c4c);
EvolutionOfWar 0:fce0f9489de3 186 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0xb2b2b2);
EvolutionOfWar 0:fce0f9489de3 187 wait(0.1);
EvolutionOfWar 0:fce0f9489de3 188 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0x000000);
EvolutionOfWar 0:fce0f9489de3 189 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0x4c4c4c);
EvolutionOfWar 0:fce0f9489de3 190 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0xb2b2b2);
EvolutionOfWar 0:fce0f9489de3 191 wait(0.1);
EvolutionOfWar 0:fce0f9489de3 192 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0x000000);
EvolutionOfWar 0:fce0f9489de3 193 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0x4c4c4c);
EvolutionOfWar 0:fce0f9489de3 194 wait(0.1);
EvolutionOfWar 0:fce0f9489de3 195 uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0x000000);
EvolutionOfWar 0:fce0f9489de3 196 wait(0.1);
EvolutionOfWar 0:fce0f9489de3 197 }
EvolutionOfWar 0:fce0f9489de3 198 }
EvolutionOfWar 0:fce0f9489de3 199
EvolutionOfWar 2:1f7c6cc19a9a 200
aolmenki 1:cc72ad58982b 201 void invalid(int x, int y, int oldColor){
aolmenki 1:cc72ad58982b 202 gb[x][y] = oldColor;
aolmenki 1:cc72ad58982b 203 drawPieces();
aolmenki 1:cc72ad58982b 204 uLCD.line(9 + 14 * xCoor, 11+ 14 * yCoor, 11+ 14 * xCoor, 9+ 14 * yCoor, RED);
aolmenki 1:cc72ad58982b 205 uLCD.line(11+ 14 * xCoor, 9+ 14 * yCoor, 20+ 14 * xCoor, 18+ 14 * yCoor, RED);
aolmenki 1:cc72ad58982b 206 uLCD.line(20+ 14 * xCoor, 18+ 14 * yCoor, 18+ 14 * xCoor, 20+ 14 * yCoor, RED);
aolmenki 1:cc72ad58982b 207 uLCD.line(18+ 14 * xCoor, 20+ 14 * yCoor, 9+ 14 * xCoor, 11+ 14 * yCoor, RED);
aolmenki 1:cc72ad58982b 208 uLCD.line(9+ 14 * xCoor, 18+ 14 * yCoor, 18+ 14 * xCoor, 9+ 14 * yCoor, RED);
aolmenki 1:cc72ad58982b 209 uLCD.line(18+ 14 * xCoor, 9+ 14 * yCoor, 20+ 14 * xCoor, 11+ 14 * yCoor, RED);
aolmenki 1:cc72ad58982b 210 uLCD.line(20+ 14 * xCoor, 11+ 14 * yCoor, 11+ 14 * xCoor, 20+ 14 * yCoor, RED);
aolmenki 1:cc72ad58982b 211 uLCD.line(11+ 14 * xCoor, 20+ 14 * yCoor, 9+ 14 * xCoor, 18+ 14 * yCoor, RED);
aolmenki 1:cc72ad58982b 212 wait(1);
aolmenki 1:cc72ad58982b 213 uLCD.line(9 + 14 * xCoor, 11+ 14 * yCoor, 11+ 14 * xCoor, 9+ 14 * yCoor, 0x007F00);
aolmenki 1:cc72ad58982b 214 uLCD.line(11+ 14 * xCoor, 9+ 14 * yCoor, 20+ 14 * xCoor, 18+ 14 * yCoor, 0x007F00);
aolmenki 1:cc72ad58982b 215 uLCD.line(20+ 14 * xCoor, 18+ 14 * yCoor, 18+ 14 * xCoor, 20+ 14 * yCoor, 0x007F00);
aolmenki 1:cc72ad58982b 216 uLCD.line(18+ 14 * xCoor, 20+ 14 * yCoor, 9+ 14 * xCoor, 11+ 14 * yCoor, 0x007F00);
aolmenki 1:cc72ad58982b 217 uLCD.line(9+ 14 * xCoor, 18+ 14 * yCoor, 18+ 14 * xCoor, 9+ 14 * yCoor, 0x007F00);
aolmenki 1:cc72ad58982b 218 uLCD.line(18+ 14 * xCoor, 9+ 14 * yCoor, 20+ 14 * xCoor, 11+ 14 * yCoor, 0x007F00);
aolmenki 1:cc72ad58982b 219 uLCD.line(20+ 14 * xCoor, 11+ 14 * yCoor, 11+ 14 * xCoor, 20+ 14 * yCoor, 0x007F00);
aolmenki 1:cc72ad58982b 220 uLCD.line(11+ 14 * xCoor, 20+ 14 * yCoor, 9+ 14 * xCoor, 18+ 14 * yCoor, 0x007F00);
aolmenki 1:cc72ad58982b 221 drawPieces();
aolmenki 1:cc72ad58982b 222 }
aolmenki 1:cc72ad58982b 223
aolmenki 1:cc72ad58982b 224 void valid_move(int x, int y) {
aolmenki 1:cc72ad58982b 225 int opp, pl;
aolmenki 1:cc72ad58982b 226 bool invalidD[] = {false, false, false, false, false, false, false, false}; //flip checker
aolmenki 1:cc72ad58982b 227 if (turn == 1) { //pl is black
aolmenki 1:cc72ad58982b 228 opp = 1;
aolmenki 1:cc72ad58982b 229 pl = 2;
aolmenki 1:cc72ad58982b 230 } else { // pl is white
aolmenki 1:cc72ad58982b 231 opp = 2;
aolmenki 1:cc72ad58982b 232 pl = 1;
EvolutionOfWar 0:fce0f9489de3 233 }
aolmenki 1:cc72ad58982b 234 int oldColor = gb[x][y]; //save old color for drawing over X
aolmenki 1:cc72ad58982b 235 bool flipped = false;
aolmenki 1:cc72ad58982b 236 if (gb[x][y] == 0) { // check empty space
aolmenki 1:cc72ad58982b 237 drawPiece(x, y); // emtpy so draw
aolmenki 1:cc72ad58982b 238 for (int i = 0; i < 8; i++) { // check for opponent around emtpy space and fill
aolmenki 1:cc72ad58982b 239 if (i == 0) { // right
aolmenki 1:cc72ad58982b 240 if (x + 1 < 7 && gb[x + 1][y] == opp) { //check to see if not beyond board and has to be next to the opponent
aolmenki 1:cc72ad58982b 241 for (int j = x + 2; j < 8; j++) {
aolmenki 1:cc72ad58982b 242 if (gb[j][y] == pl && !flipped) {
aolmenki 1:cc72ad58982b 243 for (int k = x + 1; k < j; k++) {
aolmenki 1:cc72ad58982b 244 flippingPiece(k, y);
aolmenki 1:cc72ad58982b 245 }
aolmenki 1:cc72ad58982b 246 flipped = true;
aolmenki 1:cc72ad58982b 247 } else if (gb[j][y] == 0) {
aolmenki 1:cc72ad58982b 248 j = 8;
aolmenki 1:cc72ad58982b 249 }
aolmenki 1:cc72ad58982b 250 }
aolmenki 1:cc72ad58982b 251 if (flipped) {
aolmenki 1:cc72ad58982b 252 flipped = false;
aolmenki 1:cc72ad58982b 253 } else {
aolmenki 1:cc72ad58982b 254 invalidD[0] = true;
aolmenki 1:cc72ad58982b 255 }
aolmenki 1:cc72ad58982b 256 } else {
aolmenki 1:cc72ad58982b 257 invalidD[0] = true;
aolmenki 1:cc72ad58982b 258 }
aolmenki 1:cc72ad58982b 259 } else if (i == 1) { // down-right
aolmenki 1:cc72ad58982b 260 if (x + 1 < 7 && y + 1 < 7 && gb[x + 1][y + 1] == opp) { // check if within margin and next to other color
aolmenki 1:cc72ad58982b 261 for (int j = 2; j < 8 - x && j < 8 - y; j++) { // start finding next same color
aolmenki 1:cc72ad58982b 262 if (gb[x + j][y + j] == pl && !flipped) { // if same color and not already been flipping
aolmenki 1:cc72ad58982b 263 for (int k = 1; k < j; k++) { // between other color to this same color
aolmenki 1:cc72ad58982b 264 flippingPiece(x + k, y + k); // flip
aolmenki 1:cc72ad58982b 265 }
aolmenki 1:cc72ad58982b 266 flipped = true; // marked flipped
aolmenki 1:cc72ad58982b 267 } else if (gb[x + j][y + j] == 0) { // if you find an empty instead
aolmenki 1:cc72ad58982b 268 j = 8; // skip for loop
aolmenki 1:cc72ad58982b 269 }
aolmenki 1:cc72ad58982b 270 }
aolmenki 1:cc72ad58982b 271 if (flipped) { // if flipped
aolmenki 1:cc72ad58982b 272 flipped = false; // set variable back to false
aolmenki 1:cc72ad58982b 273 } else {
aolmenki 1:cc72ad58982b 274 invalidD[1] = true; // no flip happened, so set invalid tracker to true
aolmenki 1:cc72ad58982b 275 }
aolmenki 1:cc72ad58982b 276 } else {
aolmenki 1:cc72ad58982b 277 invalidD[1] = true; // not within margin or next to other color
aolmenki 1:cc72ad58982b 278 }
aolmenki 1:cc72ad58982b 279 } else if (i == 2) { // down
aolmenki 1:cc72ad58982b 280 if (y + 1 < 7 && gb[x][y + 1] == opp) {
aolmenki 1:cc72ad58982b 281 for (int j = y + 2; j < 8; j++) {
aolmenki 1:cc72ad58982b 282 if (gb[x][j] == pl && !flipped) {
aolmenki 1:cc72ad58982b 283 for (int k = y + 1; k < j; k++) {
aolmenki 1:cc72ad58982b 284 flippingPiece(x, k);
aolmenki 1:cc72ad58982b 285 }
aolmenki 1:cc72ad58982b 286 flipped = true;
aolmenki 1:cc72ad58982b 287 } else if (gb[x][j] == 0) {
aolmenki 1:cc72ad58982b 288 j = 8;
aolmenki 1:cc72ad58982b 289 }
aolmenki 1:cc72ad58982b 290 }
aolmenki 1:cc72ad58982b 291 if (flipped) {
aolmenki 1:cc72ad58982b 292 flipped = false;
aolmenki 1:cc72ad58982b 293 } else {
aolmenki 1:cc72ad58982b 294 invalidD[2] = true;
aolmenki 1:cc72ad58982b 295 }
aolmenki 1:cc72ad58982b 296 } else {
aolmenki 1:cc72ad58982b 297 invalidD[2] = true;
aolmenki 1:cc72ad58982b 298 }
aolmenki 1:cc72ad58982b 299 } else if (i == 3) { // down-left
aolmenki 1:cc72ad58982b 300 if (x - 1 > 0 && y + 1 < 7 && gb[x - 1][y + 1] == opp) {
aolmenki 1:cc72ad58982b 301 for (int j = 2; j < x + 1 && j < 8 - y; j++) {
aolmenki 1:cc72ad58982b 302 if (gb[x - j][y + j] == pl && !flipped) {
aolmenki 1:cc72ad58982b 303 for (int k = 1; k < j; k++) {
aolmenki 1:cc72ad58982b 304 flippingPiece(x - k, y + k);
aolmenki 1:cc72ad58982b 305 }
aolmenki 1:cc72ad58982b 306 flipped = true;
aolmenki 1:cc72ad58982b 307 } else if (gb[x - j][y + j] == 0) {
aolmenki 1:cc72ad58982b 308 j = 5;
aolmenki 1:cc72ad58982b 309 }
aolmenki 1:cc72ad58982b 310 }
aolmenki 1:cc72ad58982b 311 if (flipped) {
aolmenki 1:cc72ad58982b 312 flipped = false;
aolmenki 1:cc72ad58982b 313 } else {
aolmenki 1:cc72ad58982b 314 invalidD[3] = true;
aolmenki 1:cc72ad58982b 315 }
aolmenki 1:cc72ad58982b 316 } else {
aolmenki 1:cc72ad58982b 317 invalidD[3] = true;
aolmenki 1:cc72ad58982b 318 }
aolmenki 1:cc72ad58982b 319 } else if (i == 4) { // left
aolmenki 1:cc72ad58982b 320 if (x - 1 > 0 && gb[x - 1][y] == opp) {
aolmenki 1:cc72ad58982b 321 for (int j = x - 2; j > -1; j--) {
aolmenki 1:cc72ad58982b 322 if (gb[j][y] == pl && !flipped) {
aolmenki 1:cc72ad58982b 323 for (int k = x - 1; k > j; k--) {
aolmenki 1:cc72ad58982b 324 flippingPiece(k, y);
aolmenki 1:cc72ad58982b 325 }
aolmenki 1:cc72ad58982b 326 flipped = true;
aolmenki 1:cc72ad58982b 327 } else if (gb[j][y] == 0) {
aolmenki 1:cc72ad58982b 328 j = -1;
aolmenki 1:cc72ad58982b 329 }
aolmenki 1:cc72ad58982b 330 }
aolmenki 1:cc72ad58982b 331 if (flipped) {
aolmenki 1:cc72ad58982b 332 flipped = false;
aolmenki 1:cc72ad58982b 333 } else {
aolmenki 1:cc72ad58982b 334 invalidD[4] = true;
aolmenki 1:cc72ad58982b 335 }
aolmenki 1:cc72ad58982b 336 } else {
aolmenki 1:cc72ad58982b 337 invalidD[4] = true;
aolmenki 1:cc72ad58982b 338 }
aolmenki 1:cc72ad58982b 339 } else if (i == 5) { // up-left
aolmenki 1:cc72ad58982b 340 if (x - 1 > 0 && y - 1 > 0 && gb[x - 1][y - 1] == opp) {
aolmenki 1:cc72ad58982b 341 for (int j = 2; j < x + 1 && j < y + 1; j++) {
aolmenki 1:cc72ad58982b 342 if (gb[x - j][y - j] == pl && !flipped) {
aolmenki 1:cc72ad58982b 343 for (int k = 1; k < j; k++) {
aolmenki 1:cc72ad58982b 344 flippingPiece(x - k, y - k);
aolmenki 1:cc72ad58982b 345 }
aolmenki 1:cc72ad58982b 346 flipped = true;
aolmenki 1:cc72ad58982b 347 } else if (gb[x - j][y - j] == 0) {
aolmenki 1:cc72ad58982b 348 j = 8;
aolmenki 1:cc72ad58982b 349 }
aolmenki 1:cc72ad58982b 350 }
aolmenki 1:cc72ad58982b 351 if (flipped) {
aolmenki 1:cc72ad58982b 352 flipped = false;
aolmenki 1:cc72ad58982b 353 } else {
aolmenki 1:cc72ad58982b 354 invalidD[5] = true;
aolmenki 1:cc72ad58982b 355 }
aolmenki 1:cc72ad58982b 356 } else {
aolmenki 1:cc72ad58982b 357 invalidD[5] = true;
aolmenki 1:cc72ad58982b 358 }
aolmenki 1:cc72ad58982b 359 } else if (i == 6) { // up
aolmenki 1:cc72ad58982b 360 if (y - 1 > 0 && gb[x][y - 1] == opp) {
aolmenki 1:cc72ad58982b 361 for (int j = y - 2; j > -1; j--) {
aolmenki 1:cc72ad58982b 362 if (gb[x][j] == pl && !flipped) {
aolmenki 1:cc72ad58982b 363 for (int k = y - 1; k > j; k--) {
aolmenki 1:cc72ad58982b 364 flippingPiece(x, k);
aolmenki 1:cc72ad58982b 365 }
aolmenki 1:cc72ad58982b 366 flipped = true;
aolmenki 1:cc72ad58982b 367 } else if (gb[x][j] == 0) {
aolmenki 1:cc72ad58982b 368 j = -1;
aolmenki 1:cc72ad58982b 369 }
aolmenki 1:cc72ad58982b 370 }
aolmenki 1:cc72ad58982b 371 if (flipped) {
aolmenki 1:cc72ad58982b 372 flipped = false;
aolmenki 1:cc72ad58982b 373 } else {
aolmenki 1:cc72ad58982b 374 invalidD[6] = true;
aolmenki 1:cc72ad58982b 375 }
aolmenki 1:cc72ad58982b 376 } else {
aolmenki 1:cc72ad58982b 377 invalidD[6] = true;
aolmenki 1:cc72ad58982b 378 }
aolmenki 1:cc72ad58982b 379 } else if (i == 7) { // up-right
aolmenki 1:cc72ad58982b 380 if (x + 1 < 7 && y - 1 > 0 && gb[x + 1][y - 1] == opp) {
aolmenki 1:cc72ad58982b 381 for (int j = 2; j < 8 - x && j < y + 1; j++) {
aolmenki 1:cc72ad58982b 382 if (gb[x + j][y - j] == pl && !flipped) {
aolmenki 1:cc72ad58982b 383 for (int k = 1; k < j; k++) {
aolmenki 1:cc72ad58982b 384 flippingPiece(x + k, y - k);
aolmenki 1:cc72ad58982b 385 }
aolmenki 1:cc72ad58982b 386 flipped = true;
aolmenki 1:cc72ad58982b 387 } else if (gb[x + j][y - j] == 0) {
aolmenki 1:cc72ad58982b 388 j = 8;
aolmenki 1:cc72ad58982b 389 }
aolmenki 1:cc72ad58982b 390 }
aolmenki 1:cc72ad58982b 391 if (flipped) {
aolmenki 1:cc72ad58982b 392 flipped = false;
aolmenki 1:cc72ad58982b 393 } else {
aolmenki 1:cc72ad58982b 394 invalidD[7] = true;
aolmenki 1:cc72ad58982b 395 }
aolmenki 1:cc72ad58982b 396 } else {
aolmenki 1:cc72ad58982b 397 invalidD[7] = true;
aolmenki 1:cc72ad58982b 398 }
aolmenki 1:cc72ad58982b 399 }
aolmenki 1:cc72ad58982b 400 } // finish checking around
aolmenki 1:cc72ad58982b 401 flipped = false;
aolmenki 1:cc72ad58982b 402 for (int i = 0; i < 8; i++) {
aolmenki 1:cc72ad58982b 403 if (!invalidD[i] && !flipped) { // if a move is found, and only once
aolmenki 1:cc72ad58982b 404 moved = true; // has moved
aolmenki 1:cc72ad58982b 405 turn = ++turn%2; // increment turn
aolmenki 1:cc72ad58982b 406 flipped = true; // check flip
EvolutionOfWar 2:1f7c6cc19a9a 407
EvolutionOfWar 2:1f7c6cc19a9a 408 FILE *fp2 = fopen("/sd/wavfiles/FlipSound.wav", "r");
EvolutionOfWar 2:1f7c6cc19a9a 409 if(fp2 == NULL) {
EvolutionOfWar 2:1f7c6cc19a9a 410 error("Could not open file for read\n");
EvolutionOfWar 2:1f7c6cc19a9a 411 }
EvolutionOfWar 2:1f7c6cc19a9a 412 Speaker.period(1.0/400000.0);
EvolutionOfWar 2:1f7c6cc19a9a 413 waver.play(fp2); //Plays *.wav audio file
EvolutionOfWar 2:1f7c6cc19a9a 414 fclose(fp2);
aolmenki 1:cc72ad58982b 415 }
aolmenki 1:cc72ad58982b 416 }
aolmenki 1:cc72ad58982b 417 if (!flipped) { // no changes were made
aolmenki 1:cc72ad58982b 418 invalid(x, y, oldColor); // change back to old color and revert changes
aolmenki 1:cc72ad58982b 419 }
aolmenki 1:cc72ad58982b 420 } else { //invalid
aolmenki 1:cc72ad58982b 421 invalid(x, y, oldColor); // not empty space
aolmenki 1:cc72ad58982b 422 }
aolmenki 1:cc72ad58982b 423 }
aolmenki 1:cc72ad58982b 424
aolmenki 1:cc72ad58982b 425
aolmenki 1:cc72ad58982b 426 void countPieces()
aolmenki 1:cc72ad58982b 427 {
aolmenki 1:cc72ad58982b 428 numBlack = 0;
aolmenki 1:cc72ad58982b 429 numWhite = 0;
aolmenki 1:cc72ad58982b 430 for(int row = 0; row < 8; row++){
aolmenki 1:cc72ad58982b 431 for(int col = 0; col < 8; col++){
aolmenki 1:cc72ad58982b 432 if(gb[row][col]==2)
aolmenki 1:cc72ad58982b 433 {
aolmenki 1:cc72ad58982b 434 numBlack++;
aolmenki 1:cc72ad58982b 435 }
aolmenki 1:cc72ad58982b 436 if(gb[row][col]==1)
aolmenki 1:cc72ad58982b 437 {
aolmenki 1:cc72ad58982b 438 numWhite++;
aolmenki 1:cc72ad58982b 439 }
aolmenki 1:cc72ad58982b 440 }
aolmenki 1:cc72ad58982b 441 }
aolmenki 1:cc72ad58982b 442 }
aolmenki 1:cc72ad58982b 443 void result() {
aolmenki 1:cc72ad58982b 444 uLCD.cls(); // THIS CODE ASSUMES BACKGROUND CORRECTLY RESETS TO 0x007F00
EvolutionOfWar 2:1f7c6cc19a9a 445 uLCD.filled_rectangle(0,0,127,127,0x007f00);
aolmenki 1:cc72ad58982b 446
EvolutionOfWar 2:1f7c6cc19a9a 447 FILE *fp1 = fopen("/sd/wavfiles/Victory.wav", "r");
EvolutionOfWar 2:1f7c6cc19a9a 448 if(fp1 == NULL) {
EvolutionOfWar 2:1f7c6cc19a9a 449 error("Could not open file for read\n");
EvolutionOfWar 2:1f7c6cc19a9a 450 }
EvolutionOfWar 2:1f7c6cc19a9a 451 Speaker.period(1.0/400000.0);
EvolutionOfWar 2:1f7c6cc19a9a 452 waver.play(fp1); //Plays *.wav audio file
EvolutionOfWar 2:1f7c6cc19a9a 453 fclose(fp1);
aolmenki 1:cc72ad58982b 454 countPieces();
aolmenki 1:cc72ad58982b 455 uLCD.color(RED);
aolmenki 1:cc72ad58982b 456 uLCD.printf("Score\n");
aolmenki 1:cc72ad58982b 457 uLCD.color(BLACK);
aolmenki 1:cc72ad58982b 458 uLCD.printf("%D, numBlack");
aolmenki 1:cc72ad58982b 459 uLCD.color(RED);
aolmenki 1:cc72ad58982b 460 uLCD.printf(" - ");
aolmenki 1:cc72ad58982b 461 uLCD.color(WHITE);
aolmenki 1:cc72ad58982b 462 uLCD.printf("%D \n", numWhite);
aolmenki 1:cc72ad58982b 463 uLCD.color(RED);
aolmenki 1:cc72ad58982b 464 uLCD.printf("\n Play again? \n");
aolmenki 1:cc72ad58982b 465
aolmenki 1:cc72ad58982b 466 uLCD.text_char('Y', 4, 9, BLUE);
aolmenki 1:cc72ad58982b 467 uLCD.text_char('E', 5, 9, BLUE);
aolmenki 1:cc72ad58982b 468 uLCD.text_char('S', 6, 9, BLUE);
aolmenki 1:cc72ad58982b 469 uLCD.text_char('N', 11, 9, RED);
aolmenki 1:cc72ad58982b 470 uLCD.text_char('O', 12, 9, RED);
aolmenki 1:cc72ad58982b 471
aolmenki 1:cc72ad58982b 472 uLCD.rectangle(20,90, 40, 110, 0xffff00); //Default over Yes
aolmenki 1:cc72ad58982b 473 bool optionSelected = false;
aolmenki 1:cc72ad58982b 474 int choice = 1; //Default choice Yes
aolmenki 1:cc72ad58982b 475 while(optionSelected ==false){
aolmenki 1:cc72ad58982b 476 if(myNav[2] == 0 ){
aolmenki 1:cc72ad58982b 477 uLCD.rectangle(20,90, 40, 110, 0xffff00);
aolmenki 1:cc72ad58982b 478 uLCD.rectangle(80,90, 100, 110, 0x007F00);
aolmenki 1:cc72ad58982b 479 choice = 1;
aolmenki 1:cc72ad58982b 480 }
aolmenki 1:cc72ad58982b 481 if(myNav[3] == 0 ){
aolmenki 1:cc72ad58982b 482 uLCD.rectangle(20,90, 40, 110, 0x007F00);
aolmenki 1:cc72ad58982b 483 uLCD.rectangle(80,90, 100, 110, 0xffff00);
aolmenki 1:cc72ad58982b 484 choice = 2;
aolmenki 1:cc72ad58982b 485 }
aolmenki 1:cc72ad58982b 486 if(myNav[4] == 0){ //Joystick pressed to Select
aolmenki 1:cc72ad58982b 487 if(choice == 1) {
aolmenki 1:cc72ad58982b 488 //Restart and play again
aolmenki 1:cc72ad58982b 489 initialize_game_board();
aolmenki 1:cc72ad58982b 490 xCoor = 3;
aolmenki 1:cc72ad58982b 491 yCoor = 4;
aolmenki 1:cc72ad58982b 492 }
aolmenki 1:cc72ad58982b 493 else{ //choice == 0
aolmenki 1:cc72ad58982b 494 //Quit?
aolmenki 1:cc72ad58982b 495 }
aolmenki 1:cc72ad58982b 496 }
aolmenki 1:cc72ad58982b 497 }
aolmenki 1:cc72ad58982b 498 }
aolmenki 1:cc72ad58982b 499
EvolutionOfWar 2:1f7c6cc19a9a 500 //For Testing purposes
EvolutionOfWar 2:1f7c6cc19a9a 501 void instantEnd(){
EvolutionOfWar 2:1f7c6cc19a9a 502 result();
EvolutionOfWar 2:1f7c6cc19a9a 503 }
EvolutionOfWar 2:1f7c6cc19a9a 504
EvolutionOfWar 0:fce0f9489de3 505 int main() {
EvolutionOfWar 2:1f7c6cc19a9a 506 pb.mode(PullUp);
EvolutionOfWar 2:1f7c6cc19a9a 507
aolmenki 1:cc72ad58982b 508 while (1) {
aolmenki 1:cc72ad58982b 509
aolmenki 1:cc72ad58982b 510 initialize_game_board();
aolmenki 1:cc72ad58982b 511 drawPieces();
aolmenki 1:cc72ad58982b 512 while(!gameover) { //active game, checks game over
EvolutionOfWar 2:1f7c6cc19a9a 513 if(pb == 1){
EvolutionOfWar 2:1f7c6cc19a9a 514 instantEnd();
EvolutionOfWar 2:1f7c6cc19a9a 515 }
aolmenki 1:cc72ad58982b 516 // check inputs
aolmenki 1:cc72ad58982b 517 while (!moved) {
aolmenki 1:cc72ad58982b 518 drawCursor(xCoor, yCoor);
aolmenki 1:cc72ad58982b 519 wait(0.02);
aolmenki 1:cc72ad58982b 520 removeCursor(xCoor, yCoor);
aolmenki 1:cc72ad58982b 521 //with pullups a button hit is a "0" - "~" inverts data to leds
aolmenki 1:cc72ad58982b 522 //~(myNav & 0x0F); //update leds with nav switch direction inputs
aolmenki 1:cc72ad58982b 523 if(myNav[0] == 0 && xCoor != 0){xCoor --;}
aolmenki 1:cc72ad58982b 524 if(myNav[1] == 0 && xCoor != 7){xCoor ++;}
aolmenki 1:cc72ad58982b 525 if(myNav[2] == 0 && yCoor != 7){yCoor ++;}
aolmenki 1:cc72ad58982b 526 if(myNav[3] == 0 && yCoor != 0){yCoor --;}
aolmenki 1:cc72ad58982b 527 if(myNav.fire()) {valid_move(xCoor, yCoor);} // Press Down Joystick for Select
aolmenki 1:cc72ad58982b 528 //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this
aolmenki 1:cc72ad58982b 529 }
aolmenki 1:cc72ad58982b 530 moved = false;
aolmenki 1:cc72ad58982b 531 // check game over
aolmenki 1:cc72ad58982b 532 countPieces();
aolmenki 1:cc72ad58982b 533 gameover = numBlack + numWhite == 64;
aolmenki 1:cc72ad58982b 534 }
aolmenki 1:cc72ad58982b 535 result();
EvolutionOfWar 0:fce0f9489de3 536 }
EvolutionOfWar 0:fce0f9489de3 537 }
EvolutionOfWar 0:fce0f9489de3 538 //initializeBoard();
EvolutionOfWar 0:fce0f9489de3 539 //drawBoard();
EvolutionOfWar 0:fce0f9489de3 540 //displayScore();