広告
前回に引き続き、不足していた機能を追加してみた。
完成品
こんな感じのオセロが完成。申し訳程度の対AIもサポート
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
// define constant value var BOARD_SIZE = { 'WIDTH' :8, 'HEIGHT':8, }; var BLOCK_KIND = { 'NONE' : 0, 'BLACK' : 1, 'WHITE' : 2, 'MAX' : 3, }; var SQUARE_SIZE = { 'WIDTH' : 31, 'HEIGHT' : 31, }; var SLEEP_KIND = { 'TURNOVER' : 1000, }; var isComputer = false; var isFirst = false; var stone; var board = []; var player_color; var getCountIsPossibleToTurnOver = function(x, y, dx, dy) { var count = 0; var cx = x + dx; var cy = y + dy; if (( cx < 0 || BOARD_SIZE.WIDTH < cx) || ( cy < 0 || BOARD_SIZE.HEIGHT < cy)) { return 0; } while(board[cx][cy] == BLOCK_KIND.MAX - player_color) { count++; cx += dx; cy += dy; if (( cx < 0 || BOARD_SIZE.WIDTH < cx) || ( cy < 0 || BOARD_SIZE.HEIGHT < cy)) { return 0; } } if (count > 0 && board[cx][cy] == player_color) { return count; } return 0; }; var turnOverStraight = function(x, y, dx, dy) { var cx = x + dx; var cy = y + dy; while(board[cx][cy] == BLOCK_KIND.MAX - player_color) { board[cx][cy] = player_color; cx += dx; cy += dy; } }; var turnOverBlock = function(x, y, flip) { var total = 0; // can not put block if (board[x][y] != BLOCK_KIND.NONE) { return total; } // check for 8 direction whether it is possible to turn over block for (var dx = -1; dx <= 1; dx++) { for(var dy = -1; dy <= 1; dy++) { if (dx == 0 && dy == 0) { continue; } var cnt = getCountIsPossibleToTurnOver(x, y, dx, dy); if (cnt > 0) { total += cnt; if (flip) { turnOverStraight(x, y, dx, dy); } } } } return total; }; var showBoard = function() { var b = document.getElementById("board"); while(b.firstChild) { b.removeChild(b.firstChild); } for(var y = 0; y < BOARD_SIZE.HEIGHT; y++) { for(var x = 0; x < BOARD_SIZE.WIDTH; x++) { var square = stone[board[x][y]].cloneNode(true); square.style.left = ((x) * SQUARE_SIZE.WIDTH) + "px"; square.style.top = ((y) * SQUARE_SIZE.HEIGHT) + "px"; b.appendChild(square); (function() { var _x = x; var _y = y; square.onclick = function() { if (turnOverBlock(_x, _y, true) > 0) { board[_x][_y] = player_color; showBoard(); if (!changePlayer()) { doAiPlayer(); } } }; })(); } } showProgress(); }; var showProgress = function() { var black = 0; var white = 0; for(var y = 0; y < BOARD_SIZE.HEIGHT; y++) { for(var x = 0; x < BOARD_SIZE.WIDTH; x++) { if (board[x][y] == BLOCK_KIND.BLACK) { black++; } else if (board[x][y] == BLOCK_KIND.WHITE) { white++; } else { // no opereation } } } var msg = document.getElementById("msg"); msg.innerHTML = "progress of territory black:"+black+" white:"+white; }; var changePlayer = function() { var pass = false; player_color = BLOCK_KIND.MAX - player_color; if (isPass()) { if(player_color == BLOCK_KIND.BLACK) { alert("Black is pass"); } else if (player_color == BLOCK_KIND.WHITE) { alert("White is pass"); } else { alert("invalid status"); } player_color = BLOCK_KIND.MAX - player_color; if(isPass()) { alert("Game set"); } pass = true; } return pass; }; var doAiPlayer = function() { if(!isComputer) { return false; } if( (isFirst && (player_color == BLOCK_KIND.BLACK)) || (!isFirst && (player_color == BLOCK_KIND.WHITE)) ) { return false; } for(var y = 0; y < BOARD_SIZE.HEIGHT; y++) { for(var x = 0; x < BOARD_SIZE.WIDTH; x++) { if (turnOverBlock(x, y, true) > 0) { board[x][y] = player_color; showBoard(); if(changePlayer()) { doAiPlayer(); } return true; } } } return false; }; var isPass = function() { for(var y = 0; y < BOARD_SIZE.HEIGHT; y++) { for(var x = 0; x < BOARD_SIZE.WIDTH; x++) { if (turnOverBlock(x, y, false) > 0) { return false; } } } return true; }; var msleep = function(ms) { var d1 = new Date().getTime(); var d2 = new Date().getTime(); while ( d2 < (d1 + ms) ) { d2 = new Date().getTime(); } }; var initBoard = function() { player_color = BLOCK_KIND.BLACK; // 0:none, 1:black, 2:white stone = [ document.getElementById("none"), document.getElementById("black"), document.getElementById("white") ]; // init zero value for (var i = 0; i < BOARD_SIZE.HEIGHT+1; i++) { board[i] = []; for (var j = 0; j < BOARD_SIZE.WIDTH+1; j++) { board[i][j] = BLOCK_KIND.NONE; } } // initial position board[3][4] = BLOCK_KIND.BLACK; board[4][3] = BLOCK_KIND.BLACK; board[3][3] = BLOCK_KIND.WHITE; board[4][4] = BLOCK_KIND.WHITE; }; onload = function() { // just in case Object.freeze(BLOCK_KIND); Object.freeze(BOARD_SIZE); Object.freeze(SQUARE_SIZE); // initialize board initBoard(); // start game showBoard(); }; var OnClickButton = function() { if(document.form1.Computer.checked) { isComputer = true; } else { isComputer = false; } if(document.form1.First.checked) { isFirst = true; } else { isFirst = false; } // initialize board initBoard(); // start game showBoard(); if(!isFirst) { doAiPlayer(); } }; |
広告
広告