From 423c213f2b2a52044d1c5fea0039e06287cea21d Mon Sep 17 00:00:00 2001 From: capitalthree Date: Mon, 20 Nov 2023 09:01:37 -0600 Subject: [PATCH] identify captures and suicide moves --- rules.lua | 61 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/rules.lua b/rules.lua index c3cf046..bff8772 100644 --- a/rules.lua +++ b/rules.lua @@ -41,18 +41,26 @@ function handle_placement(pos) end our_stone = our_stone:sub(2, -1) - -- print_r(neighbor_dirs(pos)) - + local captured = false for i,v in pairs(neighbor_dirs(pos)) do - local node = check_position(pos +directions[v]) + local node = check_position(pos + directions[v]) if node:byte(1) == S then - + if not (node:sub(2, -1) == our_stone) then + local captures = check_captures(pos + directions[v]) + if not (#captures == 0) then + minetest.chat_send_all("You gotcher self a capture right there! size: "..#captures) + captured = true + end + end end - - --neighbors[i] = pos + v end - + if not captured then + local captures = check_captures(pos) + if not (#captures == 0) then + minetest.chat_send_all("You gotcher self a suicide move right there! size: "..#captures) + end + end end --[[ @@ -140,16 +148,45 @@ function _check_position_uncached(pos) end end +--[[ +Scans for a connected group, until enumerating the whole group, or +finding a liberty. Returns list of captured nodes, or empty list if +no captures. +--]] +function check_captures(pos) + local group = {pos} + local checked = {[minetest.hash_node_position(pos)] = true} + local team = check_position(pos):sub(2,-1) + + local probe = 0 + while probe < #group do + probe = probe + 1 + pos = group[probe] + + for i,v in pairs(neighbor_dirs(pos)) do + local newpos = pos + directions[v] + local newhash = minetest.hash_node_position(newpos) + + if not checked[newhash] then + checked[newhash] = true + + local newnode = check_position(newpos) + + if newnode:byte(1) == E then + return {} + elseif newnode:byte(1) == S and + newnode:sub(2, -1) == team then + group[#group+1] = newpos + end + end + end + end -function surrounded_group(pos) - local group = {} - local queue = {} + return group end - - function print_r ( t ) local print_r_cache={} local function sub_print_r(t,indent)