-- Reversi: put a disc down so it closes a line of the other colour, and -- those turn yours. Play the PC or someone beside you. Dark goes first. local c8 = require "c8" local N = 8 local DARK, LIGHT = 1, 2 local saved = c8.load("reversi") or {} local opponent = saved.opponent or "pc" -- or "person" local win, board, status local cells, turn, over, last local thinking local dirs = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 }, { 0, 1 }, { 1, -1 }, { 1, 0 }, { 1, 1 } } local function other(p) return 3 - p end -- The discs a move at r, c would turn, as a list of { r, c } local function flips(grid, p, r, c) if grid[r][c] ~= 0 then return {} end local out = {} for _, d in ipairs(dirs) do local line = {} local rr, cc = r + d[1], c + d[2] while rr >= 1 and rr <= N and cc >= 1 and cc <= N and grid[rr][cc] == other(p) do line[#line + 1] = { rr, cc } rr, cc = rr + d[1], cc + d[2] end if #line > 0 and rr >= 1 and rr <= N and cc >= 1 and cc <= N and grid[rr][cc] == p then for _, x in ipairs(line) do out[#out + 1] = x end end end return out end local function moves(grid, p) local list = {} for r = 1, N do for c = 1, N do local f = flips(grid, p, r, c) if #f > 0 then list[#list + 1] = { r = r, c = c, flips = f } end end end return list end local function count() local d, l = 0, 0 for r = 1, N do for c = 1, N do if cells[r][c] == DARK then d = d + 1 elseif cells[r][c] == LIGHT then l = l + 1 end end end return d, l end local function name(p) if opponent == "pc" then return p == DARK and "You" or "The PC" end return p == DARK and "Dark" or "Light" end local function show() local d, l = count() local text if over then if d == l then text = "A draw" else local who = d > l and DARK or LIGHT text = opponent == "pc" and (who == DARK and "You win" or "The PC wins") or (name(who) .. " wins") end elseif thinking then text = "The PC is thinking" else text = opponent == "pc" and "Your turn" or (name(turn) .. "'s turn") end status:set(string.format("%s ● %d ○ %d", text, d, l)) board:redraw() end -- Corners are worth most, the squares that hand a corner over least local weights = { { 100, -20, 10, 5, 5, 10, -20, 100 }, { -20, -50, -2, -2, -2, -2, -50, -20 }, { 10, -2, 1, 1, 1, 1, -2, 10 }, { 5, -2, 1, 0, 0, 1, -2, 5 }, { 5, -2, 1, 0, 0, 1, -2, 5 }, { 10, -2, 1, 1, 1, 1, -2, 10 }, { -20, -50, -2, -2, -2, -2, -50, -20 }, { 100, -20, 10, 5, 5, 10, -20, 100 }, } local function play(grid, p, m) grid[m.r][m.c] = p for _, x in ipairs(m.flips) do grid[x[1]][x[2]] = p end end local function copy(grid) local g = {} for r = 1, N do g[r] = { table.unpack(grid[r]) } end return g end local function worth(grid, p) local v = 0 for r = 1, N do for c = 1, N do if grid[r][c] == p then v = v + weights[r][c] elseif grid[r][c] == other(p) then v = v - weights[r][c] end end end return v + 3 * (#moves(grid, p) - #moves(grid, other(p))) end -- Looks two moves ahead: its own, then the best answer to it local function choose(p) local best, best_v for _, m in ipairs(moves(cells, p)) do local g = copy(cells) play(g, p, m) local worst = math.huge local answers = moves(g, other(p)) if #answers == 0 then worst = worth(g, p) end for _, a in ipairs(answers) do local g2 = copy(g) play(g2, other(p), a) worst = math.min(worst, worth(g2, p)) end worst = worst + math.random() -- not the same game every time if not best_v or worst > best_v then best, best_v = m, worst end end return best end local pc_move -- Whose turn it is next: the other side's, unless they can't move local function next_turn() if #moves(cells, other(turn)) > 0 then turn = other(turn) elseif #moves(cells, turn) == 0 then over = true local d, l = count() if opponent == "pc" and d > l then saved.wins = (saved.wins or 0) + 1 c8.save("reversi", saved) c8.tile.update("org.cont8nuum.Reversi", { template = "block", block = tostring(saved.wins), text1 = "wins against the PC" }) end end show() if not over and opponent == "pc" and turn == LIGHT then thinking = true show() c8.after(0.6, pc_move) end end pc_move = function() thinking = false if over or turn ~= LIGHT then return end local m = choose(LIGHT) if m then play(cells, LIGHT, m) last = m end next_turn() end local function new_game() cells = {} for r = 1, N do cells[r] = {} for c = 1, N do cells[r][c] = 0 end end cells[4][4], cells[5][5] = LIGHT, LIGHT cells[4][5], cells[5][4] = DARK, DARK turn, over, last, thinking = DARK, false, nil, false if board then show() end end local function geometry(w, h) local side = math.floor(math.min(w, h, 640)) local cell = side / N return cell, (w - side) / 2, (h - side) / 2 end local function draw(g, x, y, w, h) local cell, ox, oy = geometry(w, h) x, y = x + ox, y + oy g:rect(x, y, cell * N, cell * N, 0x1e7145) for i = 0, N do g:line(x + i * cell, y, x + i * cell, y + N * cell, 0x000000, 0.35, 1) g:line(x, y + i * cell, x + N * cell, y + i * cell, 0x000000, 0.35, 1) end local hints = {} if not over and not thinking then for _, m in ipairs(moves(cells, turn)) do hints[m.r * 10 + m.c] = true end end for r = 1, N do for c = 1, N do local cx = x + (c - 0.5) * cell local cy = y + (r - 0.5) * cell local v = cells[r][c] if v ~= 0 then g:circle(cx, cy, cell * 0.4, v == DARK and 0x1d1d1d or 0xf4f4f4) if last and last.r == r and last.c == c then g:circle(cx, cy, cell * 0.08, 0xe51400) end elseif hints[r * 10 + c] then g:circle(cx, cy, cell * 0.1, 0xffffff, 0.35) end end end end local function on_event(_, ev) if ev.type ~= "release" or over or thinking then return ev.type == "press" end if opponent == "pc" and turn ~= DARK then return true end local cell, ox, oy = geometry(board.width, board.height) local c = math.floor((ev.x - board.x - ox) / cell) + 1 local r = math.floor((ev.y - board.y - oy) / cell) + 1 if r < 1 or r > N or c < 1 or c > N then return true end local f = flips(cells, turn, r, c) if #f > 0 then local m = { r = r, c = c, flips = f } play(cells, turn, m) last = m next_turn() end return true end local function build() status = c8.label { text = "", color = "secondary" } board = c8.canvas { fill = true, expand = true, draw = draw, on_event = on_event } return c8.column { spacing = 16, padding = { 24, 24, 24, 24 }, c8.label { text = "Reversi", style = "header" }, c8.row { spacing = 12, wrap = true, c8.button { label = "New game", primary = true, on_click = function() new_game() end }, c8.combo { items = { "Against the PC", "Two people" }, width = 200, selected = opponent == "pc" and 1 or 2, on_change = function(_, i) opponent = i == 1 and "pc" or "person" saved.opponent = opponent c8.save("reversi", saved) new_game() end }, status, }, board, } end c8.app { id = "org.cont8nuum.Reversi", open = function() if not (win and win.open) then new_game() win = c8.window { title = "Reversi", width = 760, height = 860, build() } show() end win:present() end, }