Final
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
Fork of Lab4-Reversi-v2 by
Reversi.cpp@0:fce0f9489de3, 2016-10-31 (annotated)
- Committer:
- EvolutionOfWar
- Date:
- Mon Oct 31 19:22:51 2016 +0000
- Revision:
- 0:fce0f9489de3
- Child:
- 1:cc72ad58982b
Lab4 Reversi 3:22pm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
EvolutionOfWar | 0:fce0f9489de3 | 1 | #include "mbed.h" |
EvolutionOfWar | 0:fce0f9489de3 | 2 | #include "uLCD_4DGL.h" |
EvolutionOfWar | 0:fce0f9489de3 | 3 | uLCD_4DGL uLCD(p28, p27, p29); // create a global lcd object |
EvolutionOfWar | 0:fce0f9489de3 | 4 | |
EvolutionOfWar | 0:fce0f9489de3 | 5 | // black <=> 2 |
EvolutionOfWar | 0:fce0f9489de3 | 6 | // white <=> 1 |
EvolutionOfWar | 0:fce0f9489de3 | 7 | |
EvolutionOfWar | 0:fce0f9489de3 | 8 | class Nav_Switch |
EvolutionOfWar | 0:fce0f9489de3 | 9 | { |
EvolutionOfWar | 0:fce0f9489de3 | 10 | public: |
EvolutionOfWar | 0:fce0f9489de3 | 11 | Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); |
EvolutionOfWar | 0:fce0f9489de3 | 12 | int read(); |
EvolutionOfWar | 0:fce0f9489de3 | 13 | //boolean functions to test each switch |
EvolutionOfWar | 0:fce0f9489de3 | 14 | bool up(); |
EvolutionOfWar | 0:fce0f9489de3 | 15 | bool down(); |
EvolutionOfWar | 0:fce0f9489de3 | 16 | bool left(); |
EvolutionOfWar | 0:fce0f9489de3 | 17 | bool right(); |
EvolutionOfWar | 0:fce0f9489de3 | 18 | bool fire(); |
EvolutionOfWar | 0:fce0f9489de3 | 19 | //automatic read on RHS |
EvolutionOfWar | 0:fce0f9489de3 | 20 | operator int (); |
EvolutionOfWar | 0:fce0f9489de3 | 21 | //index to any switch array style |
EvolutionOfWar | 0:fce0f9489de3 | 22 | bool operator[](int index) { |
EvolutionOfWar | 0:fce0f9489de3 | 23 | return _pins[index]; |
EvolutionOfWar | 0:fce0f9489de3 | 24 | }; |
EvolutionOfWar | 0:fce0f9489de3 | 25 | private: |
EvolutionOfWar | 0:fce0f9489de3 | 26 | BusIn _pins; |
EvolutionOfWar | 0:fce0f9489de3 | 27 | |
EvolutionOfWar | 0:fce0f9489de3 | 28 | }; |
EvolutionOfWar | 0:fce0f9489de3 | 29 | Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire): |
EvolutionOfWar | 0:fce0f9489de3 | 30 | _pins(up, down, left, right, fire) |
EvolutionOfWar | 0:fce0f9489de3 | 31 | { |
EvolutionOfWar | 0:fce0f9489de3 | 32 | _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise |
EvolutionOfWar | 0:fce0f9489de3 | 33 | wait(0.001); //delays just a bit for pullups to pull inputs high |
EvolutionOfWar | 0:fce0f9489de3 | 34 | } |
EvolutionOfWar | 0:fce0f9489de3 | 35 | inline bool Nav_Switch::up() |
EvolutionOfWar | 0:fce0f9489de3 | 36 | { |
EvolutionOfWar | 0:fce0f9489de3 | 37 | return !(_pins[0]); |
EvolutionOfWar | 0:fce0f9489de3 | 38 | } |
EvolutionOfWar | 0:fce0f9489de3 | 39 | inline bool Nav_Switch::down() |
EvolutionOfWar | 0:fce0f9489de3 | 40 | { |
EvolutionOfWar | 0:fce0f9489de3 | 41 | return !(_pins[1]); |
EvolutionOfWar | 0:fce0f9489de3 | 42 | } |
EvolutionOfWar | 0:fce0f9489de3 | 43 | inline bool Nav_Switch::left() |
EvolutionOfWar | 0:fce0f9489de3 | 44 | { |
EvolutionOfWar | 0:fce0f9489de3 | 45 | return !(_pins[2]); |
EvolutionOfWar | 0:fce0f9489de3 | 46 | } |
EvolutionOfWar | 0:fce0f9489de3 | 47 | inline bool Nav_Switch::right() |
EvolutionOfWar | 0:fce0f9489de3 | 48 | { |
EvolutionOfWar | 0:fce0f9489de3 | 49 | return !(_pins[3]); |
EvolutionOfWar | 0:fce0f9489de3 | 50 | } |
EvolutionOfWar | 0:fce0f9489de3 | 51 | inline bool Nav_Switch::fire() |
EvolutionOfWar | 0:fce0f9489de3 | 52 | { |
EvolutionOfWar | 0:fce0f9489de3 | 53 | return !(_pins[4]); |
EvolutionOfWar | 0:fce0f9489de3 | 54 | } |
EvolutionOfWar | 0:fce0f9489de3 | 55 | inline int Nav_Switch::read() |
EvolutionOfWar | 0:fce0f9489de3 | 56 | { |
EvolutionOfWar | 0:fce0f9489de3 | 57 | return _pins.read(); |
EvolutionOfWar | 0:fce0f9489de3 | 58 | } |
EvolutionOfWar | 0:fce0f9489de3 | 59 | inline Nav_Switch::operator int () |
EvolutionOfWar | 0:fce0f9489de3 | 60 | { |
EvolutionOfWar | 0:fce0f9489de3 | 61 | return _pins.read(); |
EvolutionOfWar | 0:fce0f9489de3 | 62 | } |
EvolutionOfWar | 0:fce0f9489de3 | 63 | |
EvolutionOfWar | 0:fce0f9489de3 | 64 | Nav_Switch myNav( p14, p11, p12, p10, p13); //pin order on Sparkfun breakout |
EvolutionOfWar | 0:fce0f9489de3 | 65 | |
EvolutionOfWar | 0:fce0f9489de3 | 66 | int whosTurn = 1; |
EvolutionOfWar | 0:fce0f9489de3 | 67 | int gb[8][8]; |
EvolutionOfWar | 0:fce0f9489de3 | 68 | int NUM_COLS = 8; |
EvolutionOfWar | 0:fce0f9489de3 | 69 | int NUM_ROWS = 8; |
EvolutionOfWar | 0:fce0f9489de3 | 70 | /*var isEmpty = function(r,c) { |
EvolutionOfWar | 0:fce0f9489de3 | 71 | return board[r][c] == 0; |
EvolutionOfWar | 0:fce0f9489de3 | 72 | } |
EvolutionOfWar | 0:fce0f9489de3 | 73 | var score = function(boardIn) { |
EvolutionOfWar | 0:fce0f9489de3 | 74 | var blackScore = 0; |
EvolutionOfWar | 0:fce0f9489de3 | 75 | var whiteScore = 0; |
EvolutionOfWar | 0:fce0f9489de3 | 76 | for(var r=0; r<8; r++) |
EvolutionOfWar | 0:fce0f9489de3 | 77 | { |
EvolutionOfWar | 0:fce0f9489de3 | 78 | for(var c=0; c<8; c++) |
EvolutionOfWar | 0:fce0f9489de3 | 79 | { |
EvolutionOfWar | 0:fce0f9489de3 | 80 | var current = board[r][c]; |
EvolutionOfWar | 0:fce0f9489de3 | 81 | if(current === 1) |
EvolutionOfWar | 0:fce0f9489de3 | 82 | { |
EvolutionOfWar | 0:fce0f9489de3 | 83 | whiteScore++; |
EvolutionOfWar | 0:fce0f9489de3 | 84 | } |
EvolutionOfWar | 0:fce0f9489de3 | 85 | if(current === -1) |
EvolutionOfWar | 0:fce0f9489de3 | 86 | { |
EvolutionOfWar | 0:fce0f9489de3 | 87 | blackScore++; |
EvolutionOfWar | 0:fce0f9489de3 | 88 | } |
EvolutionOfWar | 0:fce0f9489de3 | 89 | } |
EvolutionOfWar | 0:fce0f9489de3 | 90 | } |
EvolutionOfWar | 0:fce0f9489de3 | 91 | var score = {black: blackScore, white: whiteScore}; |
EvolutionOfWar | 0:fce0f9489de3 | 92 | return score; |
EvolutionOfWar | 0:fce0f9489de3 | 93 | } |
EvolutionOfWar | 0:fce0f9489de3 | 94 | var displayScore = function() { |
EvolutionOfWar | 0:fce0f9489de3 | 95 | var currentScore = score(board); |
EvolutionOfWar | 0:fce0f9489de3 | 96 | textSize(20); |
EvolutionOfWar | 0:fce0f9489de3 | 97 | fill(255, 0, 0); |
EvolutionOfWar | 0:fce0f9489de3 | 98 | text(currentScore.black+" - "+currentScore.white,180,390); |
EvolutionOfWar | 0:fce0f9489de3 | 99 | } |
EvolutionOfWar | 0:fce0f9489de3 | 100 | */ |
EvolutionOfWar | 0:fce0f9489de3 | 101 | void initialize_game_board() |
EvolutionOfWar | 0:fce0f9489de3 | 102 | { |
EvolutionOfWar | 0:fce0f9489de3 | 103 | //This is a nested loop to make sure every cell is empty |
EvolutionOfWar | 0:fce0f9489de3 | 104 | //Cell Codes: 0 = empty, 1 = white piece, 2 = black piece |
EvolutionOfWar | 0:fce0f9489de3 | 105 | for (int i = 0; i < NUM_ROWS; i++) |
EvolutionOfWar | 0:fce0f9489de3 | 106 | { |
EvolutionOfWar | 0:fce0f9489de3 | 107 | for (int j = 0; j < NUM_COLS; j++) |
EvolutionOfWar | 0:fce0f9489de3 | 108 | gb[i][j] = 0; |
EvolutionOfWar | 0:fce0f9489de3 | 109 | } |
EvolutionOfWar | 0:fce0f9489de3 | 110 | gb[3][3] = 1;//Put down white piece |
EvolutionOfWar | 0:fce0f9489de3 | 111 | gb[4][4] = 1;//Put down white piece |
EvolutionOfWar | 0:fce0f9489de3 | 112 | gb[3][4] = 2;//Put down black piece |
EvolutionOfWar | 0:fce0f9489de3 | 113 | gb[4][3] = 2;//Put down black piece |
EvolutionOfWar | 0:fce0f9489de3 | 114 | |
EvolutionOfWar | 0:fce0f9489de3 | 115 | uLCD.cls(); |
EvolutionOfWar | 0:fce0f9489de3 | 116 | uLCD.background_color(0x007f00); |
EvolutionOfWar | 0:fce0f9489de3 | 117 | |
EvolutionOfWar | 0:fce0f9489de3 | 118 | uLCD.rectangle(7,7,120,120,DGREY); |
EvolutionOfWar | 0:fce0f9489de3 | 119 | uLCD.rectangle(8,8,119,119,DGREY); |
EvolutionOfWar | 0:fce0f9489de3 | 120 | //uLCD.filled_rectangle(8,20,120,21,DGREY); |
EvolutionOfWar | 0:fce0f9489de3 | 121 | |
EvolutionOfWar | 0:fce0f9489de3 | 122 | for( int i = 1; i< 9; i++) |
EvolutionOfWar | 0:fce0f9489de3 | 123 | { |
EvolutionOfWar | 0:fce0f9489de3 | 124 | uLCD.filled_rectangle(8, 7 + 14*i,119,8 + 14*i,DGREY); |
EvolutionOfWar | 0:fce0f9489de3 | 125 | } |
EvolutionOfWar | 0:fce0f9489de3 | 126 | for( int j = 1; j<9; j++) |
EvolutionOfWar | 0:fce0f9489de3 | 127 | { |
EvolutionOfWar | 0:fce0f9489de3 | 128 | uLCD.filled_rectangle(7 + 14*j, 8, 8+14*j, 119, DGREY); |
EvolutionOfWar | 0:fce0f9489de3 | 129 | } |
EvolutionOfWar | 0:fce0f9489de3 | 130 | } |
EvolutionOfWar | 0:fce0f9489de3 | 131 | |
EvolutionOfWar | 0:fce0f9489de3 | 132 | void drawCursor(int xCoor, int yCoor) //Expecting x ={0,7} and y ={0,7} |
EvolutionOfWar | 0:fce0f9489de3 | 133 | { |
EvolutionOfWar | 0:fce0f9489de3 | 134 | uLCD.filled_rectangle(7 + 14*xCoor, 7 + 14*yCoor,8 + 14*xCoor,22 + 14*yCoor,RED); |
EvolutionOfWar | 0:fce0f9489de3 | 135 | uLCD.filled_rectangle(7 + 14*xCoor, 7 + 14*yCoor,22 + 14*xCoor,8 + 14*yCoor,RED); |
EvolutionOfWar | 0:fce0f9489de3 | 136 | uLCD.filled_rectangle(7 + 14*xCoor, 21 + 14*yCoor,22 + 14*xCoor,22 + 14*yCoor,RED); |
EvolutionOfWar | 0:fce0f9489de3 | 137 | uLCD.filled_rectangle(21 + 14*xCoor, 7 + 14*yCoor,22 + 14*xCoor,22 + 14*yCoor,RED); |
EvolutionOfWar | 0:fce0f9489de3 | 138 | } |
EvolutionOfWar | 0:fce0f9489de3 | 139 | |
EvolutionOfWar | 0:fce0f9489de3 | 140 | void removeCursor(int xCoor,int yCoor) |
EvolutionOfWar | 0:fce0f9489de3 | 141 | { |
EvolutionOfWar | 0:fce0f9489de3 | 142 | uLCD.filled_rectangle(7 + 14*xCoor, 7 + 14*yCoor,8 + 14*xCoor,22 + 14*yCoor,DGREY); |
EvolutionOfWar | 0:fce0f9489de3 | 143 | uLCD.filled_rectangle(7 + 14*xCoor, 7 + 14*yCoor,22 + 14*xCoor,8 + 14*yCoor,DGREY); |
EvolutionOfWar | 0:fce0f9489de3 | 144 | uLCD.filled_rectangle(7 + 14*xCoor, 21 + 14*yCoor,22 + 14*xCoor,22 + 14*yCoor,DGREY); |
EvolutionOfWar | 0:fce0f9489de3 | 145 | uLCD.filled_rectangle(21 + 14*xCoor, 7 + 14*yCoor,22 + 14*xCoor,22 + 14*yCoor,DGREY); |
EvolutionOfWar | 0:fce0f9489de3 | 146 | } |
EvolutionOfWar | 0:fce0f9489de3 | 147 | |
EvolutionOfWar | 0:fce0f9489de3 | 148 | /* |
EvolutionOfWar | 0:fce0f9489de3 | 149 | var copyBoard = function() { |
EvolutionOfWar | 0:fce0f9489de3 | 150 | var boardCopy = [[0,0,0,0,0,0,0,0], |
EvolutionOfWar | 0:fce0f9489de3 | 151 | [0,0,0,0,0,0,0,0], |
EvolutionOfWar | 0:fce0f9489de3 | 152 | [0,0,0,0,0,0,0,0], |
EvolutionOfWar | 0:fce0f9489de3 | 153 | [0,0,0,0,0,0,0,0], |
EvolutionOfWar | 0:fce0f9489de3 | 154 | [0,0,0,0,0,0,0,0], |
EvolutionOfWar | 0:fce0f9489de3 | 155 | [0,0,0,0,0,0,0,0], |
EvolutionOfWar | 0:fce0f9489de3 | 156 | [0,0,0,0,0,0,0,0], |
EvolutionOfWar | 0:fce0f9489de3 | 157 | [0,0,0,0,0,0,0,0]]; |
EvolutionOfWar | 0:fce0f9489de3 | 158 | for(var r=0; r<8; r++) |
EvolutionOfWar | 0:fce0f9489de3 | 159 | { |
EvolutionOfWar | 0:fce0f9489de3 | 160 | for(var c=0; c<8; c++) |
EvolutionOfWar | 0:fce0f9489de3 | 161 | { |
EvolutionOfWar | 0:fce0f9489de3 | 162 | boardCopy[r][c] = board[r][c]; |
EvolutionOfWar | 0:fce0f9489de3 | 163 | } |
EvolutionOfWar | 0:fce0f9489de3 | 164 | } |
EvolutionOfWar | 0:fce0f9489de3 | 165 | return boardCopy; |
EvolutionOfWar | 0:fce0f9489de3 | 166 | }; */ |
EvolutionOfWar | 0:fce0f9489de3 | 167 | //Removed DrawEmptyBoard function |
EvolutionOfWar | 0:fce0f9489de3 | 168 | |
EvolutionOfWar | 0:fce0f9489de3 | 169 | /*var drawPiece = function(r,c,player) { |
EvolutionOfWar | 0:fce0f9489de3 | 170 | var xPos = 400/8*c+400/16; |
EvolutionOfWar | 0:fce0f9489de3 | 171 | var yPos = 400/8*r+400/16; |
EvolutionOfWar | 0:fce0f9489de3 | 172 | if(player === 0) |
EvolutionOfWar | 0:fce0f9489de3 | 173 | { |
EvolutionOfWar | 0:fce0f9489de3 | 174 | return; |
EvolutionOfWar | 0:fce0f9489de3 | 175 | } |
EvolutionOfWar | 0:fce0f9489de3 | 176 | fill(0, 0, 0); |
EvolutionOfWar | 0:fce0f9489de3 | 177 | if(player===1) |
EvolutionOfWar | 0:fce0f9489de3 | 178 | { |
EvolutionOfWar | 0:fce0f9489de3 | 179 | fill(255, 255, 255); |
EvolutionOfWar | 0:fce0f9489de3 | 180 | } |
EvolutionOfWar | 0:fce0f9489de3 | 181 | ellipse(xPos,yPos,40,40); |
EvolutionOfWar | 0:fce0f9489de3 | 182 | }; */ |
EvolutionOfWar | 0:fce0f9489de3 | 183 | |
EvolutionOfWar | 0:fce0f9489de3 | 184 | void drawPieces(){ |
EvolutionOfWar | 0:fce0f9489de3 | 185 | for (int i = 0; i < NUM_ROWS; i++) |
EvolutionOfWar | 0:fce0f9489de3 | 186 | { |
EvolutionOfWar | 0:fce0f9489de3 | 187 | for (int j = 0; j < NUM_COLS; j++) |
EvolutionOfWar | 0:fce0f9489de3 | 188 | { |
EvolutionOfWar | 0:fce0f9489de3 | 189 | if(gb[i][j] == 1){ |
EvolutionOfWar | 0:fce0f9489de3 | 190 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , WHITE); |
EvolutionOfWar | 0:fce0f9489de3 | 191 | } |
EvolutionOfWar | 0:fce0f9489de3 | 192 | if(gb[i][j] == 2){ |
EvolutionOfWar | 0:fce0f9489de3 | 193 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , BLACK); |
EvolutionOfWar | 0:fce0f9489de3 | 194 | } |
EvolutionOfWar | 0:fce0f9489de3 | 195 | } |
EvolutionOfWar | 0:fce0f9489de3 | 196 | } |
EvolutionOfWar | 0:fce0f9489de3 | 197 | } |
EvolutionOfWar | 0:fce0f9489de3 | 198 | |
EvolutionOfWar | 0:fce0f9489de3 | 199 | void drawPiece(int i, int j) { //xCoor, yCoor |
EvolutionOfWar | 0:fce0f9489de3 | 200 | if(gb[i][j] == 1){ |
EvolutionOfWar | 0:fce0f9489de3 | 201 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , WHITE); |
EvolutionOfWar | 0:fce0f9489de3 | 202 | } |
EvolutionOfWar | 0:fce0f9489de3 | 203 | if(gb[i][j] == 2){ |
EvolutionOfWar | 0:fce0f9489de3 | 204 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , BLACK); |
EvolutionOfWar | 0:fce0f9489de3 | 205 | } |
EvolutionOfWar | 0:fce0f9489de3 | 206 | } |
EvolutionOfWar | 0:fce0f9489de3 | 207 | |
EvolutionOfWar | 0:fce0f9489de3 | 208 | void flippingPiece(int i, int j) { //xCoor, yCoor, animation |
EvolutionOfWar | 0:fce0f9489de3 | 209 | if(gb[i][j] == 1){ //Gradually changes a piece White |
EvolutionOfWar | 0:fce0f9489de3 | 210 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0x4c4c4c); |
EvolutionOfWar | 0:fce0f9489de3 | 211 | wait(0.1); |
EvolutionOfWar | 0:fce0f9489de3 | 212 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0xb2b2b2); |
EvolutionOfWar | 0:fce0f9489de3 | 213 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0x4c4c4c); |
EvolutionOfWar | 0:fce0f9489de3 | 214 | wait(0.1); |
EvolutionOfWar | 0:fce0f9489de3 | 215 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0xffffff); |
EvolutionOfWar | 0:fce0f9489de3 | 216 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0xb2b2b2); |
EvolutionOfWar | 0:fce0f9489de3 | 217 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0x4c4c4c); |
EvolutionOfWar | 0:fce0f9489de3 | 218 | wait(0.1); |
EvolutionOfWar | 0:fce0f9489de3 | 219 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0xffffff); |
EvolutionOfWar | 0:fce0f9489de3 | 220 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0xb2b2b2); |
EvolutionOfWar | 0:fce0f9489de3 | 221 | wait(0.1); |
EvolutionOfWar | 0:fce0f9489de3 | 222 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0xffffff); |
EvolutionOfWar | 0:fce0f9489de3 | 223 | wait(0.1); |
EvolutionOfWar | 0:fce0f9489de3 | 224 | } |
EvolutionOfWar | 0:fce0f9489de3 | 225 | if(gb[i][j] == 2){ //Gradually changes a piece Black |
EvolutionOfWar | 0:fce0f9489de3 | 226 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0xb2b2b2); |
EvolutionOfWar | 0:fce0f9489de3 | 227 | wait(0.1); |
EvolutionOfWar | 0:fce0f9489de3 | 228 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0x4c4c4c); |
EvolutionOfWar | 0:fce0f9489de3 | 229 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0xb2b2b2); |
EvolutionOfWar | 0:fce0f9489de3 | 230 | wait(0.1); |
EvolutionOfWar | 0:fce0f9489de3 | 231 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 1 , 0x000000); |
EvolutionOfWar | 0:fce0f9489de3 | 232 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0x4c4c4c); |
EvolutionOfWar | 0:fce0f9489de3 | 233 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0xb2b2b2); |
EvolutionOfWar | 0:fce0f9489de3 | 234 | wait(0.1); |
EvolutionOfWar | 0:fce0f9489de3 | 235 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 3 , 0x000000); |
EvolutionOfWar | 0:fce0f9489de3 | 236 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0x4c4c4c); |
EvolutionOfWar | 0:fce0f9489de3 | 237 | wait(0.1); |
EvolutionOfWar | 0:fce0f9489de3 | 238 | uLCD.filled_circle(14 * (i + 1), 14 * (j + 1), 5 , 0x000000); |
EvolutionOfWar | 0:fce0f9489de3 | 239 | wait(0.1); |
EvolutionOfWar | 0:fce0f9489de3 | 240 | } |
EvolutionOfWar | 0:fce0f9489de3 | 241 | } |
EvolutionOfWar | 0:fce0f9489de3 | 242 | |
EvolutionOfWar | 0:fce0f9489de3 | 243 | void Select(int xCoor, int yCoor){ //function should determine what happens when joy stick pressed |
EvolutionOfWar | 0:fce0f9489de3 | 244 | if(whosTurn%2 == 0) ( //Whites Turn |
EvolutionOfWar | 0:fce0f9489de3 | 245 | //Valid Check, see if move is possible |
EvolutionOfWar | 0:fce0f9489de3 | 246 | |
EvolutionOfWar | 0:fce0f9489de3 | 247 | //Place Piece |
EvolutionOfWar | 0:fce0f9489de3 | 248 | drawPiece(xCoor, yCoor); |
EvolutionOfWar | 0:fce0f9489de3 | 249 | //Convert pieces in 2D Array |
EvolutionOfWar | 0:fce0f9489de3 | 250 | int counter; //Count number of converted pieces |
EvolutionOfWar | 0:fce0f9489de3 | 251 | |
EvolutionOfWar | 0:fce0f9489de3 | 252 | |
EvolutionOfWar | 0:fce0f9489de3 | 253 | //Flip pieces on screen |
EvolutionOfWar | 0:fce0f9489de3 | 254 | for( int m = 0; m<counter; m++) { |
EvolutionOfWar | 0:fce0f9489de3 | 255 | |
EvolutionOfWar | 0:fce0f9489de3 | 256 | } |
EvolutionOfWar | 0:fce0f9489de3 | 257 | } |
EvolutionOfWar | 0:fce0f9489de3 | 258 | } |
EvolutionOfWar | 0:fce0f9489de3 | 259 | |
EvolutionOfWar | 0:fce0f9489de3 | 260 | /*var fillBoard = function() { |
EvolutionOfWar | 0:fce0f9489de3 | 261 | for(var r=0; r<8; r++) |
EvolutionOfWar | 0:fce0f9489de3 | 262 | { |
EvolutionOfWar | 0:fce0f9489de3 | 263 | for(var c=0; c<8; c++) |
EvolutionOfWar | 0:fce0f9489de3 | 264 | { |
EvolutionOfWar | 0:fce0f9489de3 | 265 | drawPiece(r,c,board[r][c]); |
EvolutionOfWar | 0:fce0f9489de3 | 266 | } |
EvolutionOfWar | 0:fce0f9489de3 | 267 | } |
EvolutionOfWar | 0:fce0f9489de3 | 268 | }; |
EvolutionOfWar | 0:fce0f9489de3 | 269 | var drawBoard = function() { |
EvolutionOfWar | 0:fce0f9489de3 | 270 | drawEmptyBoard(); |
EvolutionOfWar | 0:fce0f9489de3 | 271 | fillBoard(); |
EvolutionOfWar | 0:fce0f9489de3 | 272 | }; --------------------------------------------WAS NOT USED VVVVV ---------------------------- |
EvolutionOfWar | 0:fce0f9489de3 | 273 | /*var flipFromInDir = function(r,c,player,rDir,cDir) |
EvolutionOfWar | 0:fce0f9489de3 | 274 | { |
EvolutionOfWar | 0:fce0f9489de3 | 275 | //debug("Checking direction rDir: "+rDir+" cDir: "+cDir+" for player "+player); |
EvolutionOfWar | 0:fce0f9489de3 | 276 | var currentR = r; |
EvolutionOfWar | 0:fce0f9489de3 | 277 | var currentC = c; |
EvolutionOfWar | 0:fce0f9489de3 | 278 | var current = board[currentR][currentC]; |
EvolutionOfWar | 0:fce0f9489de3 | 279 | var toBeFlipped = []; |
EvolutionOfWar | 0:fce0f9489de3 | 280 | var withinRBounds = 0<=currentR && currentR<8; |
EvolutionOfWar | 0:fce0f9489de3 | 281 | var withinCBounds = 0<=currentC && currentC<8; |
EvolutionOfWar | 0:fce0f9489de3 | 282 | var oppositePieces = true; |
EvolutionOfWar | 0:fce0f9489de3 | 283 | var samePlayerEncounterred = false; |
EvolutionOfWar | 0:fce0f9489de3 | 284 | while(withinRBounds && withinCBounds && oppositePieces) |
EvolutionOfWar | 0:fce0f9489de3 | 285 | { |
EvolutionOfWar | 0:fce0f9489de3 | 286 | //debug(" oh, sweet! "+currentR+", "+currentC+" looks good!"); |
EvolutionOfWar | 0:fce0f9489de3 | 287 | toBeFlipped.push({r:currentR, c:currentC}); |
EvolutionOfWar | 0:fce0f9489de3 | 288 | currentR += rDir; |
EvolutionOfWar | 0:fce0f9489de3 | 289 | currentC += cDir; |
EvolutionOfWar | 0:fce0f9489de3 | 290 | withinRBounds = 0<=currentR && currentR<8; |
EvolutionOfWar | 0:fce0f9489de3 | 291 | withinCBounds = 0<=currentC && currentC<8; |
EvolutionOfWar | 0:fce0f9489de3 | 292 | if(withinCBounds && withinRBounds) |
EvolutionOfWar | 0:fce0f9489de3 | 293 | { |
EvolutionOfWar | 0:fce0f9489de3 | 294 | current = board[currentR][currentC]; |
EvolutionOfWar | 0:fce0f9489de3 | 295 | oppositePieces = (current===-player); |
EvolutionOfWar | 0:fce0f9489de3 | 296 | //debug(" current at "+currentR+", "+currentC+" is "+current); |
EvolutionOfWar | 0:fce0f9489de3 | 297 | //debug(" oppositePieces at "+currentR+", "+currentC+" is "+oppositePieces); |
EvolutionOfWar | 0:fce0f9489de3 | 298 | if(current===player) |
EvolutionOfWar | 0:fce0f9489de3 | 299 | { |
EvolutionOfWar | 0:fce0f9489de3 | 300 | samePlayerEncounterred = true; |
EvolutionOfWar | 0:fce0f9489de3 | 301 | } |
EvolutionOfWar | 0:fce0f9489de3 | 302 | } |
EvolutionOfWar | 0:fce0f9489de3 | 303 | } |
EvolutionOfWar | 0:fce0f9489de3 | 304 | //debug(" we found "+toBeFlipped.length); |
EvolutionOfWar | 0:fce0f9489de3 | 305 | if(toBeFlipped.length > 1 && samePlayerEncounterred) |
EvolutionOfWar | 0:fce0f9489de3 | 306 | { |
EvolutionOfWar | 0:fce0f9489de3 | 307 | for(var i=1; i<toBeFlipped.length; i++) |
EvolutionOfWar | 0:fce0f9489de3 | 308 | { |
EvolutionOfWar | 0:fce0f9489de3 | 309 | var flipMe = toBeFlipped[i]; |
EvolutionOfWar | 0:fce0f9489de3 | 310 | drawPiece(flipMe.r, flipMe.c, player); |
EvolutionOfWar | 0:fce0f9489de3 | 311 | board[flipMe.r][flipMe.c] = player; |
EvolutionOfWar | 0:fce0f9489de3 | 312 | } |
EvolutionOfWar | 0:fce0f9489de3 | 313 | } |
EvolutionOfWar | 0:fce0f9489de3 | 314 | }; -------------------------------WAS NOT USED ^^^^-------------------- |
EvolutionOfWar | 0:fce0f9489de3 | 315 | var flipFromInDir = function(r,c,player,rDir,cDir) { |
EvolutionOfWar | 0:fce0f9489de3 | 316 | var potentialBoard = copyBoard(); |
EvolutionOfWar | 0:fce0f9489de3 | 317 | |
EvolutionOfWar | 0:fce0f9489de3 | 318 | var currentR = r+rDir; |
EvolutionOfWar | 0:fce0f9489de3 | 319 | var currentC = c+cDir; |
EvolutionOfWar | 0:fce0f9489de3 | 320 | var withinRBounds = 0<=currentR && currentR<8; |
EvolutionOfWar | 0:fce0f9489de3 | 321 | var withinCBounds = 0<=currentC && currentC<8; |
EvolutionOfWar | 0:fce0f9489de3 | 322 | |
EvolutionOfWar | 0:fce0f9489de3 | 323 | while(withinRBounds && withinCBounds) |
EvolutionOfWar | 0:fce0f9489de3 | 324 | { |
EvolutionOfWar | 0:fce0f9489de3 | 325 | debug("r,c: "+r+", "+c+" direction: "+rDir+", "+cDir+" current: "+currentR+", "+currentC); |
EvolutionOfWar | 0:fce0f9489de3 | 326 | var current = board[currentR][currentC]; |
EvolutionOfWar | 0:fce0f9489de3 | 327 | if(current === 0) |
EvolutionOfWar | 0:fce0f9489de3 | 328 | { |
EvolutionOfWar | 0:fce0f9489de3 | 329 | return board; |
EvolutionOfWar | 0:fce0f9489de3 | 330 | } |
EvolutionOfWar | 0:fce0f9489de3 | 331 | else if(current === player) |
EvolutionOfWar | 0:fce0f9489de3 | 332 | { |
EvolutionOfWar | 0:fce0f9489de3 | 333 | //board = potentialBoard; |
EvolutionOfWar | 0:fce0f9489de3 | 334 | return potentialBoard; |
EvolutionOfWar | 0:fce0f9489de3 | 335 | } |
EvolutionOfWar | 0:fce0f9489de3 | 336 | else |
EvolutionOfWar | 0:fce0f9489de3 | 337 | { |
EvolutionOfWar | 0:fce0f9489de3 | 338 | potentialBoard[currentR][currentC] = player; |
EvolutionOfWar | 0:fce0f9489de3 | 339 | currentR = currentR+rDir; |
EvolutionOfWar | 0:fce0f9489de3 | 340 | currentC = currentC+cDir; |
EvolutionOfWar | 0:fce0f9489de3 | 341 | withinRBounds = 0<=currentR && currentR<8; |
EvolutionOfWar | 0:fce0f9489de3 | 342 | withinCBounds = 0<=currentC && currentC<8; |
EvolutionOfWar | 0:fce0f9489de3 | 343 | } |
EvolutionOfWar | 0:fce0f9489de3 | 344 | } |
EvolutionOfWar | 0:fce0f9489de3 | 345 | return board; |
EvolutionOfWar | 0:fce0f9489de3 | 346 | }; |
EvolutionOfWar | 0:fce0f9489de3 | 347 | var flipFrom = function(r,c,player) { |
EvolutionOfWar | 0:fce0f9489de3 | 348 | for(var rDir = -1; rDir<2; rDir++) |
EvolutionOfWar | 0:fce0f9489de3 | 349 | { |
EvolutionOfWar | 0:fce0f9489de3 | 350 | for(var cDir = -1; cDir<2; cDir++) |
EvolutionOfWar | 0:fce0f9489de3 | 351 | { |
EvolutionOfWar | 0:fce0f9489de3 | 352 | board = flipFromInDir(r, c, player, rDir, cDir); |
EvolutionOfWar | 0:fce0f9489de3 | 353 | } |
EvolutionOfWar | 0:fce0f9489de3 | 354 | } |
EvolutionOfWar | 0:fce0f9489de3 | 355 | }; |
EvolutionOfWar | 0:fce0f9489de3 | 356 | var placePiece = function(r,c,player) { |
EvolutionOfWar | 0:fce0f9489de3 | 357 | //debug("placing piece for player "+player); |
EvolutionOfWar | 0:fce0f9489de3 | 358 | drawPiece(r,c,player); |
EvolutionOfWar | 0:fce0f9489de3 | 359 | board[r][c] = player; |
EvolutionOfWar | 0:fce0f9489de3 | 360 | flipFrom(r,c,player); |
EvolutionOfWar | 0:fce0f9489de3 | 361 | drawBoard(); |
EvolutionOfWar | 0:fce0f9489de3 | 362 | }; |
EvolutionOfWar | 0:fce0f9489de3 | 363 | var move = function(r,c) |
EvolutionOfWar | 0:fce0f9489de3 | 364 | { |
EvolutionOfWar | 0:fce0f9489de3 | 365 | placePiece(r,c,whosTurn); |
EvolutionOfWar | 0:fce0f9489de3 | 366 | whosTurn *= -1; |
EvolutionOfWar | 0:fce0f9489de3 | 367 | }; |
EvolutionOfWar | 0:fce0f9489de3 | 368 | var isValidInDir = function(r,c,player,rDir,cDir) |
EvolutionOfWar | 0:fce0f9489de3 | 369 | { |
EvolutionOfWar | 0:fce0f9489de3 | 370 | //debug("Checking direction rDir: "+rDir+" cDir: "+cDir+" for player "+player); |
EvolutionOfWar | 0:fce0f9489de3 | 371 | var currentR = r; |
EvolutionOfWar | 0:fce0f9489de3 | 372 | var currentC = c; |
EvolutionOfWar | 0:fce0f9489de3 | 373 | var current = board[currentR][currentC]; |
EvolutionOfWar | 0:fce0f9489de3 | 374 | var toBeFlipped = []; |
EvolutionOfWar | 0:fce0f9489de3 | 375 | var withinRBounds = 0<=currentR && currentR<8; |
EvolutionOfWar | 0:fce0f9489de3 | 376 | var withinCBounds = 0<=currentC && currentC<8; |
EvolutionOfWar | 0:fce0f9489de3 | 377 | var oppositePieces = true; |
EvolutionOfWar | 0:fce0f9489de3 | 378 | var samePlayerEncounterred = false; |
EvolutionOfWar | 0:fce0f9489de3 | 379 | while(withinRBounds && withinCBounds && oppositePieces) |
EvolutionOfWar | 0:fce0f9489de3 | 380 | { |
EvolutionOfWar | 0:fce0f9489de3 | 381 | //debug(" oh, sweet! "+currentR+", "+currentC+" looks good!"); |
EvolutionOfWar | 0:fce0f9489de3 | 382 | toBeFlipped.push({r:currentR, c:currentC}); |
EvolutionOfWar | 0:fce0f9489de3 | 383 | currentR += rDir; |
EvolutionOfWar | 0:fce0f9489de3 | 384 | currentC += cDir; |
EvolutionOfWar | 0:fce0f9489de3 | 385 | withinRBounds = 0<=currentR && currentR<8; |
EvolutionOfWar | 0:fce0f9489de3 | 386 | withinCBounds = 0<=currentC && currentC<8; |
EvolutionOfWar | 0:fce0f9489de3 | 387 | if(withinCBounds && withinRBounds) |
EvolutionOfWar | 0:fce0f9489de3 | 388 | { |
EvolutionOfWar | 0:fce0f9489de3 | 389 | current = board[currentR][currentC]; |
EvolutionOfWar | 0:fce0f9489de3 | 390 | oppositePieces = (current===-player); |
EvolutionOfWar | 0:fce0f9489de3 | 391 | //debug(" current at "+currentR+", "+currentC+" is "+current); |
EvolutionOfWar | 0:fce0f9489de3 | 392 | //debug(" oppositePieces at "+currentR+", "+currentC+" is "+oppositePieces); |
EvolutionOfWar | 0:fce0f9489de3 | 393 | if(current===player) |
EvolutionOfWar | 0:fce0f9489de3 | 394 | { |
EvolutionOfWar | 0:fce0f9489de3 | 395 | samePlayerEncounterred = true; |
EvolutionOfWar | 0:fce0f9489de3 | 396 | } |
EvolutionOfWar | 0:fce0f9489de3 | 397 | } |
EvolutionOfWar | 0:fce0f9489de3 | 398 | } |
EvolutionOfWar | 0:fce0f9489de3 | 399 | //debug(" we found "+toBeFlipped.length); |
EvolutionOfWar | 0:fce0f9489de3 | 400 | if(toBeFlipped.length > 1 && samePlayerEncounterred) |
EvolutionOfWar | 0:fce0f9489de3 | 401 | { |
EvolutionOfWar | 0:fce0f9489de3 | 402 | return true; |
EvolutionOfWar | 0:fce0f9489de3 | 403 | } |
EvolutionOfWar | 0:fce0f9489de3 | 404 | return false; |
EvolutionOfWar | 0:fce0f9489de3 | 405 | }; |
EvolutionOfWar | 0:fce0f9489de3 | 406 | var isValidMove = function(r,c,player) { |
EvolutionOfWar | 0:fce0f9489de3 | 407 | if(!isEmpty(r,c)) |
EvolutionOfWar | 0:fce0f9489de3 | 408 | { |
EvolutionOfWar | 0:fce0f9489de3 | 409 | return false; |
EvolutionOfWar | 0:fce0f9489de3 | 410 | } |
EvolutionOfWar | 0:fce0f9489de3 | 411 | for(var rDir = -1; rDir<2; rDir++) |
EvolutionOfWar | 0:fce0f9489de3 | 412 | { |
EvolutionOfWar | 0:fce0f9489de3 | 413 | for(var cDir = -1; cDir<2; cDir++) |
EvolutionOfWar | 0:fce0f9489de3 | 414 | { |
EvolutionOfWar | 0:fce0f9489de3 | 415 | if(isValidInDir(r, c, player, rDir, cDir)) |
EvolutionOfWar | 0:fce0f9489de3 | 416 | { |
EvolutionOfWar | 0:fce0f9489de3 | 417 | return true; |
EvolutionOfWar | 0:fce0f9489de3 | 418 | } |
EvolutionOfWar | 0:fce0f9489de3 | 419 | } |
EvolutionOfWar | 0:fce0f9489de3 | 420 | } |
EvolutionOfWar | 0:fce0f9489de3 | 421 | return false; |
EvolutionOfWar | 0:fce0f9489de3 | 422 | }; |
EvolutionOfWar | 0:fce0f9489de3 | 423 | var mouseClicked = function() { |
EvolutionOfWar | 0:fce0f9489de3 | 424 | var r = floor(mouseY*8/400); |
EvolutionOfWar | 0:fce0f9489de3 | 425 | var c = floor(mouseX*8/400); |
EvolutionOfWar | 0:fce0f9489de3 | 426 | if(isValidMove(r,c,whosTurn)) |
EvolutionOfWar | 0:fce0f9489de3 | 427 | { |
EvolutionOfWar | 0:fce0f9489de3 | 428 | move(r,c); |
EvolutionOfWar | 0:fce0f9489de3 | 429 | } |
EvolutionOfWar | 0:fce0f9489de3 | 430 | displayScore(); |
EvolutionOfWar | 0:fce0f9489de3 | 431 | }; |
EvolutionOfWar | 0:fce0f9489de3 | 432 | */ |
EvolutionOfWar | 0:fce0f9489de3 | 433 | int main() { |
EvolutionOfWar | 0:fce0f9489de3 | 434 | initialize_game_board(); |
EvolutionOfWar | 0:fce0f9489de3 | 435 | int xCoor = 3; |
EvolutionOfWar | 0:fce0f9489de3 | 436 | int yCoor = 4; |
EvolutionOfWar | 0:fce0f9489de3 | 437 | |
EvolutionOfWar | 0:fce0f9489de3 | 438 | |
EvolutionOfWar | 0:fce0f9489de3 | 439 | while(1) { |
EvolutionOfWar | 0:fce0f9489de3 | 440 | //with pullups a button hit is a "0" - "~" inverts data to leds |
EvolutionOfWar | 0:fce0f9489de3 | 441 | //~(myNav & 0x0F); //update leds with nav switch direction inputs |
EvolutionOfWar | 0:fce0f9489de3 | 442 | if(myNav[0] == 0 && xCoor != 0){xCoor --;} |
EvolutionOfWar | 0:fce0f9489de3 | 443 | if(myNav[1] == 0 && xCoor != 7){xCoor ++;} |
EvolutionOfWar | 0:fce0f9489de3 | 444 | if(myNav[2] == 0 && yCoor != 0){xCoor --;} |
EvolutionOfWar | 0:fce0f9489de3 | 445 | if(myNav[3] == 0 && yCoor != 7){xCoor ++;} |
EvolutionOfWar | 0:fce0f9489de3 | 446 | if(myNav.fire()) {Select(xCoor, yCoor);} // Press Down Joystick for Select |
EvolutionOfWar | 0:fce0f9489de3 | 447 | //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this |
EvolutionOfWar | 0:fce0f9489de3 | 448 | wait(0.02); |
EvolutionOfWar | 0:fce0f9489de3 | 449 | } |
EvolutionOfWar | 0:fce0f9489de3 | 450 | |
EvolutionOfWar | 0:fce0f9489de3 | 451 | } |
EvolutionOfWar | 0:fce0f9489de3 | 452 | //initializeBoard(); |
EvolutionOfWar | 0:fce0f9489de3 | 453 | //drawBoard(); |
EvolutionOfWar | 0:fce0f9489de3 | 454 | //displayScore(); |