Files
@ 71291a0adf6d
Branch filter:
Location: games/Awakening/london_checkers.lua
71291a0adf6d
15.5 KiB
text/x-lua
outro fix
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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | checkers_game = room {
var {
board = {};
selected = 0;
select_lock = false;
};
nam = "checkers_game";
nosave = true;
noautosave = true;
forcedsc = true;
randomness = 15;
depth = 6;
cutoff_lo = -3;
cutoff_hi = 3;
draw_board = function(board)
rval = {
{0,0,0,0,0,0,0,0};
{0,0,0,0,0,0,0,0};
{0,0,0,0,0,0,0,0};
{0,0,0,0,0,0,0,0};
{0,0,0,0,0,0,0,0};
{0,0,0,0,0,0,0,0};
{0,0,0,0,0,0,0,0};
{0,0,0,0,0,0,0,0};
};
for i = 1, #board do
if board[i][3] then
if board[i][4] then
rval[board[i][1]][board[i][2]] = 3;
else
rval[board[i][1]][board[i][2]] = 1;
end;
else
if board[i][4] then
rval[board[i][1]][board[i][2]] = 4;
else
rval[board[i][1]][board[i][2]] = 2;
end;
end;
end;
return rval;
end;
calc_take = function(s, x, y, w, q, map)
local rval = {};
local r = nil;
if w then
-- белый рубит черного - рубит вверх
if (map[x - 1][y - 1] == 2 or map[x - 1][y - 1] == 4)
and map[x - 2][y - 2] == 0 and x - 2 > 0 and y - 2 > 0 then
mcopy = deepcopy(map);
r = s:calc_take(x - 2, y - 2, w, q or y == 2, mcopy);
if #r == 0 then
rval[#rval + 1] = {1, {{x-1, y-1}}, q or y == 2, x-2, y-2};
end;
for i = 1, #r do
r[i][1] = r[i][1] + 1;
r[i][2][#r[i][2] + 1] = {x-1, y-1};
r[i][3] = r[i][3] or q or y == 2;
rval[#rval + 1] = r[i]
end;
end;
if (map[x + 1][y - 1] == 2 or map[x + 1][y - 1] == 4)
and map[x + 2][y - 2] == 0 and x + 2 < 9 and y - 2 > 0 then
mcopy = deepcopy(map);
r = s:calc_take(x + 2, y - 2, w, q or y == 2, mcopy);
if #r == 0 then
rval[#rval + 1] = {1, {{x+1, y-1}}, q or y == 2, x+2, y-2};
end;
for i = 1, #r do
r[i][1] = r[i][1] + 1;
r[i][2][#r[i][2] + 1] = {x+1, y-1};
r[i][3] = r[i][3] or q or y == 2;
rval[#rval + 1] = r[i]
end;
end;
--Дамка может рубить и вниз
if q and (map[x - 1][y + 1] == 2 or map[x - 1][y + 1] == 4)
and map[x - 2][y + 2] == 0 and x - 2 > 0 and y + 2 < 9 then
mcopy = deepcopy(map);
r = s:calc_take(x - 2, y + 2, w, q, mcopy);
if #r == 0 then
rval[#rval + 1] = {1, {{x-1, y+1}}, q, x-2, y+2};
end;
for i = 1, #r do
r[i][1] = r[i][1] + 1;
r[i][2][#r[i][2] + 1] = {x-1, y+1};
r[i][3] = r[i][3] or q;
rval[#rval + 1] = r[i];
end;
end;
if q and (map[x + 1][y + 1] == 2 or map[x + 1][y + 1] == 4)
and map[x + 2][y + 2] == 0 and x + 2 < 9 and y + 2 < 9 then
mcopy = deepcopy(map);
r = s:calc_take(x + 2, y + 2, w, q, mcopy);
if #r == 0 then
rval[#rval + 1] = {1, {{x+1, y+1}}, q, x+2, y+2};
end;
for i = 1, #r do
r[i][1] = r[i][1] + 1;
r[i][2][#r[i][2] + 1] = {x+1, y+1};
r[i][3] = r[i][3] or q;
rval[#rval + 1] = r[i];
end;
end;
else
--черный рубит белого - рубит вниз
if (map[x - 1][y + 1] == 1 or map[x - 1][y + 1] == 3)
and map[x - 2][y + 2] == 0 and x - 2 > 0 and y + 2 < 9 then
mcopy = deepcopy(map);
r = s:calc_take(x - 2, y + 2, w, q or y == 6, mcopy);
if #r == 0 then
rval[#rval + 1] = {1, {{x-1, y+1}}, q or y == 6, x-2, y+2};
end;
for i = 1, #r do
r[i][1] = r[i][1] + 1;
r[i][2][#r[i][2] + 1] = {x-1, y+1};
r[i][3] = r[i][3] or q or y == 6;
rval[#rval + 1] = r[i]
end;
end;
if (map[x + 1][y + 1] == 1 or map[x + 1][y + 1] == 3)
and map[x + 2][y + 2] == 0 and x + 2 < 9 and y + 2 < 9 then
mcopy = deepcopy(map);
r = s:calc_take(x + 2, y + 2, w, q or y == 6, mcopy);
if #r == 0 then
rval[#rval + 1] = {1, {{x+1, y+1}}, q or y == 6, x+2, y+2};
end;
for i = 1, #r do
r[i][1] = r[i][1] + 1;
r[i][2][#r[i][2] + 1] = {x+1, y+1};
r[i][3] = r[i][3] or q or y == 2;
rval[#rval + 1] = r[i]
end;
end;
--Дамка может рубить и вниз
if q and (map[x - 1][y - 1] == 1 or map[x - 1][y - 1] == 3)
and map[x - 2][y - 2] == 0 and x - 2 > 0 and y - 2 > 0 then
mcopy = deepcopy(map);
r = s:calc_take(x - 2, y - 2, w, q, mcopy);
if #r == 0 then
rval[#rval + 1] = {1, {{x-1, y-1}}, q or y == 6, x-2, y-2};
end;
for i = 1, #r do
r[i][1] = r[i][1] + 1;
r[i][2][#r[i][2] + 1] = {x-1, y-1};
r[i][3] = r[i][3] or q;
rval[#rval + 1] = r[i];
end;
end;
if q and (map[x + 1][y - 1] == 1 or map[x + 1][y - 1] == 3)
and map[x + 2][y - 2] == 0 and x + 2 < 9 and y - 2 > 0 then
mcopy = deepcopy(map);
r = s:calc_take(x + 2, y - 2, w, q, mcopy);
if #r == 0 then
rval[#rval + 1] = {1, {{x+1, y-1}}, q or y == 6, x+2, y-2};
end;
for i = 1, #r do
r[i][1] = r[i][1] + 1;
r[i][2][#r[i][2] + 1] = {x+1, y-1};
r[i][3] = r[i][3] or q;
rval[#rval + 1] = r[i];
end;
end;
end;
return rval;
end;
calc_turn = function(s, d, w, board)
if d > s.depth then
return nil, 0;
end;
local cost = 0;
local turns = {};
local bmap = s.draw_board(board);
local takes = false;
for i = 1, #board do
--Рубка обязательна, поэтому сперва проверяем рубки
if board[i][3] == w then
-- своя фигура. Проверяем рубки
local r = s:calc_take(board[i][1], board[i][2], w, board[i][4], bmap);
if #r > 0 then
takes = true;
for j = 1, #r do
--Восстанавливаем состояние поля после рубки и уходим глубже
local bcopy = deepcopy(board);
for k = #bcopy, 1, -1 do
for l = 1, #r[j][2] do
if r[j][2][1] == bcopy[k][2] and r[j][2][1] == bcopy[k][2] then
table.remove(bcopy, k);
break;
end;
end;
end;
local rr, pts = s:calc_turn(d + 1, not w, bcopy);
turns[#turns + 1] = {board[i][1], board[i][2], r[j][4], r[j][5], r[j][2], r[j][1] - pts};
end;
end;
end;
end;
-- Если была рубка, не обрабатываем дальше
if takes then
table.sort(turns, function(a,b)
return a[6] > b[6];
end);
return turns[0], turns[0][6];
end;
for i = 1, #board do
if board[i][3] == w then
--Проверяем ходы
if q or w then
--белая или дамка - ходит вверх
if bmap[board[i][1] - 1][board[i][2] - 1] == 0 then
local bcopy = deepcopy(board);
bcopy[i][1] = bcopy[i][1] - 1;
bcopy[i][2] = bcopy[i][2] - 1;
local rr, pts = s:calc_turn(d + 1, not w, bcopy);
turns[#turns + 1] = {board[i][1], board[i][2], bcopy[i][1], bcopy[i][2], nil, - pts};
end;
if bmap[board[i][1] - 1][board[i][2] + 1] == 0 then
local bcopy = deepcopy(board);
bcopy[i][1] = bcopy[i][1] - 1;
bcopy[i][2] = bcopy[i][2] + 1;
local rr, pts = s:calc_turn(d + 1, not w, bcopy);
turns[#turns + 1] = {board[i][1], board[i][2], bcopy[i][1], bcopy[i][2], nil, - pts};
end;
end;
if q or not w then
--черная или дамка - ходит вниз
if bmap[board[i][1] + 1][board[i][2] - 1] == 0 then
local bcopy = deepcopy(board);
bcopy[i][1] = bcopy[i][1] + 1;
bcopy[i][2] = bcopy[i][2] - 1;
local rr, pts = s:calc_turn(d + 1, not w, bcopy);
turns[#turns + 1] = {board[i][1], board[i][2], bcopy[i][1], bcopy[i][2], nil, - pts};
end;
if bmap[board[i][1] + 1][board[i][2] + 1] == 0 then
local bcopy = deepcopy(board);
bcopy[i][1] = bcopy[i][1] + 1;
bcopy[i][2] = bcopy[i][2] + 1;
local rr, pts = s:calc_turn(d + 1, not w, bcopy);
turns[#turns + 1] = {board[i][1], board[i][2], bcopy[i][1], bcopy[i][2], nil, - pts};
end;
end;
end;
end;
table.sort(turns, function(a,b)
return a[6] > b[6];
end);
return turns[0], turns[0][6];
end;
enemy_turn = function(s)
end;
click = function(s, x, y)
x = math.floor((x - 4) / 24) + 1;
y = math.floor((y - 4) / 24) + 1;
if x > 0 and x < 9 and y > 0 and y < 9 then
for i = 1,#s.board do
if s.board[i][1] == x and s.board[i][2] == y then
if s.board[i][3] then
s.selected = i;
walkin(here());
return;
else
--Может рубим?
if s.selected > 0 then
print("q");
local dx = x - s.board[s.selected][1];
local dy = y - s.board[s.selected][2];
if s.board[s.selected][4] then
if math.abs(dx) == 1 and math.abs(dy) == 1 then
s.board[s.selected][1] = s.board[s.selected][1] + dx * 2;
s.board[s.selected][2] = s.board[s.selected][2] + dy * 2;
s.selected = 0;
walkin(here());
return;
end;
else
if math.abs(dx) == 1 and dy == -1 then
s.board[s.selected][1] = s.board[s.selected][1] + dx * 2;
s.board[s.selected][2] = s.board[s.selected][2] + dy * 2;
s.selected = 0;
walkin(here());
return;
end;
end;
end;
return;
end;
end;
end;
if s.selected > 0 then
local dx = x - s.board[s.selected][1];
local dy = y - s.board[s.selected][2];
print(dx, dy);
if s.board[s.selected][4] then
if math.abs(dx) == 1 and math.abs(dy) == 1 then
s.board[s.selected][1] = x;
s.board[s.selected][2] = y;
s.selected = 0;
walkin(here());
return;
end;
else
if math.abs(dx) == 1 and dy == -1 then
s.board[s.selected][1] = x;
s.board[s.selected][2] = y;
print(s.board[s.selected][1], s.board[s.selected][2])
s.selected = 0;
walkin(here());
return;
end;
end;
end;
s.selected = 0;
end;
walkin(here());
end;
disp = "Шашки";
reset = function(s)
s.board = {
{2, 3, false, false};
{4, 3, false, false};
{6, 3, false, false};
{8, 3, false, false};
--{1, 2, false, false};
--{3, 2, false, false};
--{5, 2, false, false};
--{7, 2, false, false};
{2, 5, false, false};
{4, 5, false, false};
{6, 5, false, false};
{8, 5, false, false};
{1, 6, true, false};
{3, 6, true, false};
{5, 6, true, false};
{7, 6, true, false};
{2, 7, true, false};
{4, 7, true, false};
{6, 7, true, false};
{8, 7, true, false};
{1, 8, true, false};
{3, 8, true, false};
{5, 8, true, false};
{7, 8, true, false};
};
end;
pic = function(s)
local rval = "images/checkerboard.png";
if s.selected > 0 then
rval = rval .. [[;box:24x24,red,32@]] .. tostring(s.board[s.selected][1] * 24 - 20) .. "," .. tostring(s.board[s.selected][2] * 24 - 20);
end;
for i = 1, #s.board do
if s.board[i][3] and s.board[i][4] then
rval = rval .. [[;images/checkerwhiteq.png@]] .. tostring(s.board[i][1] * 24 - 20) .. "," .. tostring(s.board[i][2] * 24 - 20);
elseif not s.board[i][3] and s.board[i][4] then
rval = rval .. [[;images/checkerblackq.png@]] .. tostring(s.board[i][1] * 24 - 20) .. "," .. tostring(s.board[i][2] * 24 - 20);
elseif s.board[i][3] and not s.board[i][4] then
rval = rval .. [[;images/checkerwhite.png@]] .. tostring(s.board[i][1] * 24 - 20) .. "," .. tostring(s.board[i][2] * 24 - 20);
elseif not s.board[i][3] and not s.board[i][4] then
rval = rval .. [[;images/checkerblack.png@]] .. tostring(s.board[i][1] * 24 - 20) .. "," .. tostring(s.board[i][2] * 24 - 20);
end;
end;
return rval;
end;
};
|