king = 900
}
-local function get_possible_moves(inv, from_idx)
- local piece, color = inv:get_stack("board", from_idx):get_name():match(":(%w+)_(%w+)")
+local function get_possible_moves(board, from_idx)
+ local piece, color = board[from_idx]:match(":(%w+)_(%w+)")
if not piece then return end
local moves = {}
local from_x, from_y = index_to_xy(from_idx)
for i = 1, 64 do
- local stack = inv:get_stack("board", i)
- local stack_name = stack:get_name()
-
+ local stack_name = board[i]
if stack_name:find((color == "black" and "white" or "black")) or
- stack:is_empty() then
+ stack_name == "" then
moves[i] = 0
end
end
for to_idx in pairs(moves) do
- local pieceTo = inv:get_stack("board", to_idx):get_name()
+ local pieceTo = board[to_idx]
local to_x, to_y = index_to_xy(to_idx)
-- PAWN
if piece == "pawn" then
if color == "white" then
- local pawnWhiteMove = inv:get_stack("board", xy_to_index(from_x, from_y - 1)):get_name()
+ local pawnWhiteMove = board[xy_to_index(from_x, from_y - 1)]
-- white pawns can go up only
if from_y - 1 == to_y then
if from_x == to_x then
end
elseif color == "black" then
- local pawnBlackMove = inv:get_stack("board", xy_to_index(from_x, from_y + 1)):get_name()
+ local pawnBlackMove = board[xy_to_index(from_x, from_y + 1)]
-- black pawns can go down only
if from_y + 1 == to_y then
if from_x == to_x then
-- Moving down
-- Ensure that no piece disturbs the way
for i = from_y + 1, to_y - 1 do
- if inv:get_stack("board", xy_to_index(from_x, i)):get_name() ~= "" then
+ if board[xy_to_index(from_x, i)] ~= "" then
moves[to_idx] = nil
end
end
-- Mocing up
-- Ensure that no piece disturbs the way
for i = to_y + 1, from_y - 1 do
- if inv:get_stack("board", xy_to_index(from_x, i)):get_name() ~= "" then
+ if board[xy_to_index(from_x, i)] ~= "" then
moves[to_idx] = nil
end
end
-- mocing right
-- ensure that no piece disturbs the way
for i = from_x + 1, to_x - 1 do
- if inv:get_stack("board", xy_to_index(i, from_y)):get_name() ~= "" then
+ if board[xy_to_index(i, from_y)] ~= "" then
moves[to_idx] = nil
end
end
-- Mocing left
-- Ensure that no piece disturbs the way
for i = to_x + 1, from_x - 1 do
- if inv:get_stack("board", xy_to_index(i, from_y)):get_name() ~= "" then
+ if board[xy_to_index(i, from_y)] ~= "" then
moves[to_idx] = nil
end
end
-- Moving right-down
-- Ensure that no piece disturbs the way
for i = 1, dx - 1 do
- if inv:get_stack(
- "board", xy_to_index(from_x + i, from_y + i)):get_name() ~= "" then
+ if board[xy_to_index(from_x + i, from_y + i)] ~= "" then
moves[to_idx] = nil
end
end
-- Moving right-up
-- Ensure that no piece disturbs the way
for i = 1, dx - 1 do
- if inv:get_stack(
- "board", xy_to_index(from_x + i, from_y - i)):get_name() ~= "" then
+ if board[xy_to_index(from_x + i, from_y - i)] ~= "" then
moves[to_idx] = nil
end
end
-- Moving left-down
-- Ensure that no piece disturbs the way
for i = 1, dx - 1 do
- if inv:get_stack(
- "board", xy_to_index(from_x - i, from_y + i)):get_name() ~= "" then
+ if board[xy_to_index(from_x - i, from_y + i)] ~= "" then
moves[to_idx] = nil
end
end
-- Moving left-up
-- ensure that no piece disturbs the way
for i = 1, dx - 1 do
- if inv:get_stack(
- "board", xy_to_index(from_x - i, from_y - i)):get_name() ~= "" then
+ if board[xy_to_index(from_x - i, from_y - i)] ~= "" then
moves[to_idx] = nil
end
end
-- Moving down
-- Ensure that no piece disturbs the way
for i = from_y + 1, to_y - 1 do
- if inv:get_stack("board", xy_to_index(from_x, i)):get_name() ~= "" then
+ if board[xy_to_index(from_x, i)] ~= "" then
moves[to_idx] = nil
end
end
-- Mocing up
-- Ensure that no piece disturbs the way
for i = to_y + 1, from_y - 1 do
- if inv:get_stack("board", xy_to_index(from_x, i)):get_name() ~= "" then
+ if board[xy_to_index(from_x, i)] ~= "" then
moves[to_idx] = nil
end
end
-- Goes right
-- Ensure that no piece disturbs the way
for i = 1, dx - 1 do
- if inv:get_stack(
- "board", xy_to_index(from_x + i, from_y)):get_name() ~= "" then
+ if board[xy_to_index(from_x + i, from_y)] ~= "" then
moves[to_idx] = nil
end
end
-- Goes right-down
-- Ensure that no piece disturbs the way
for i = 1, dx - 1 do
- if inv:get_stack(
- "board", xy_to_index(from_x + i, from_y + i)):get_name() ~= "" then
+ if board[xy_to_index(from_x + i, from_y + i)] ~= "" then
moves[to_idx] = nil
end
end
-- Goes right-up
-- Ensure that no piece disturbs the way
for i = 1, dx - 1 do
- if inv:get_stack(
- "board", xy_to_index(from_x + i, from_y - i)):get_name() ~= "" then
+ if board[xy_to_index(from_x + i, from_y - i)] ~= "" then
moves[to_idx] = nil
end
end
-- mocing right
-- ensure that no piece disturbs the way
for i = from_x + 1, to_x - 1 do
- if inv:get_stack("board", xy_to_index(i, from_y)):get_name() ~= "" then
+ if board[xy_to_index(i, from_y)] ~= "" then
moves[to_idx] = nil
end
end
-- Mocing left
-- Ensure that no piece disturbs the way
for i = to_x + 1, from_x - 1 do
- if inv:get_stack("board", xy_to_index(i, from_y)):get_name() ~= "" then
+ if board[xy_to_index(i, from_y)] ~= "" then
moves[to_idx] = nil
end
end
-- Goes left-down
-- Ensure that no piece disturbs the way
for i = 1, dx - 1 do
- if inv:get_stack(
- "board", xy_to_index(from_x - i, from_y + i)):get_name() ~= "" then
+ if board[xy_to_index(from_x - i, from_y + i)] ~= "" then
moves[to_idx] = nil
end
end
-- Goes left-up
-- Ensure that no piece disturbs the way
for i = 1, dx - 1 do
- if inv:get_stack(
- "board", xy_to_index(from_x - i, from_y - i)):get_name() ~= "" then
+ if board[xy_to_index(from_x - i, from_y - i)] ~= "" then
moves[to_idx] = nil
end
end
if not next(moves) then return end
for i in pairs(moves) do
- local stack = inv:get_stack("board", tonumber(i))
- local stack_name = stack:get_name()
-
+ local stack_name = board[tonumber(i)]
if stack_name ~= "" then
for p, value in pairs(piece_values) do
if stack_name:find(p) then
end
pieces_str = pieces_str .. "69=mailbox_blank16.png"
+local fs_init = [[
+ size[4,1.2;]
+ no_prepend[]
+ label[0,0;Select a mode:]
+ button[0,0.5;2,1;single;Singleplayer]
+ button[2,0.5;2,1;multi;Multiplayer]
+]]
+
local fs = [[
size[14.7,10;]
no_prepend[]
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
- meta:set_string("formspec", fs)
+ meta:set_string("formspec", fs_init)
meta:set_string("infotext", "Chess Board")
meta:set_string("playerBlack", "")
meta:set_string("playerWhite", "")
meta:set_string("moves", "")
meta:set_string("eaten", "")
+ meta:set_string("mode", "")
inv:set_list("board", pieces)
inv:set_size("board", 64)
get_moves_list(meta, pieceFrom, pieceTo, pieceTo_s, from_index, to_index)
get_eaten_list(meta, pieceTo, pieceTo_s)
- --print("from_index: " .. from_index)
- --print("to_index: " .. to_index)
-
return 1
end
-function realchess.on_move(pos, from_list, from_index)
- local meta = minetest.get_meta(pos)
- local inv = meta:get_inventory()
- inv:set_stack(from_list, from_index, "")
-
+local function ai_move(inv, meta)
+ local board_t = board_to_table(inv)
local lastMove = meta:get_string("lastMove")
+
if lastMove == "white" then
update_formspec(meta)
local moves = {}
for i = 1, 64 do
- local possibleMoves = get_possible_moves(inv, i)
+ local possibleMoves = get_possible_moves(board_t, i)
local stack_name = inv:get_stack("board", i):get_name()
if stack_name:find("black") then
end
end
- --minetest.log("warning", "moves: " .. dump(moves))
-
local choice_from, choice_to = best_move(moves)
+
local pieceFrom = inv:get_stack("board", choice_from):get_name()
local pieceTo = inv:get_stack("board", choice_to):get_name()
local pieceTo_s = pieceTo ~= "" and pieceTo:match(":(%w+_%w+)") or ""
return
end
else
- inv:set_stack("board", choice_to, pieceFrom)
+ if pieceFrom:find("pawn") and choice_to >= 57 and choice_to <= 64 then
+ inv:set_stack("board", choice_to, "realchess:queen_black")
+ else
+ inv:set_stack("board", choice_to, pieceFrom)
+ end
+
inv:set_stack("board", choice_from, "")
end
else
update_formspec(meta)
end
+end
+
+function realchess.on_move(pos, from_list, from_index)
+ local meta = minetest.get_meta(pos)
+ local inv = meta:get_inventory()
+ inv:set_stack(from_list, from_index, "")
+
+ if meta:get_string("mode") == "single" then
+ ai_move(inv, meta)
+ end
return false
end
local lastMoveTime = meta:get_int("lastMoveTime")
if fields.quit then return end
+ if fields.single or fields.multi then
+ meta:set_string("mode", (fields.single and "single" or "multi"))
+ update_formspec(meta)
+ return
+ end
+
-- Timeout is 5 min. by default for resetting the game (non-players only)
if fields.new then
if (playerWhite == playerName or playerBlack == playerName) then