-- goofy3.wz - An example Lua script for Tick5 -- Copyright (C) 2006 XMartin Yao -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with this program; if not, write to the Free Software -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ---------------------------------------------------------------------------- -- This is a simple example of AI engine built using Lua script for Tick5. -- It is to introduce users the Tick5's API (AI Programming Interface.) -- -- See the example in script goofy.wz for the very basics of the AI -- function tick5think(board). -- -- See the example in script goofy2.wz, which is simpler than this one. -- -- In this example, the AI does more "thinking". Other than picking up -- a vacant spot which is the same as the example in goofy2.wz, it goofs -- around to explore the game board. So it can be defeated without any -- effort. :-) ----------------------------------------------------------------------------- function tick5think( board ) print( "----- start thinking -----" ) -- mycolor() if mycolor() == 1 then print( "I play first, 'cause I play black stone." ) else print( "I play white stone. My favorite color." ) end -- check for last move last_move( board ) -- board API goof_around( board ) -- randomly pick a vacant spot return pick_a_vacant_spot( board ) end ----------------------------------------------- -- Check for the last move the opponent made ----------------------------------------------- function last_move( board ) -- validate global variable if previousboard == nil then return end -- the upper bound of coordinates local bound = board:dimension() - 1 -- search the difference local x, y for x = 0, bound, 1 do for y = 0, bound, 1 do if not board:isempty(x, y) and previousboard:isempty(x, y) then print( "The guy put a stone on (", x, ",", y, ")" ) return end end end end ----------------------------------------------- -- Introduce API of board. ----------------------------------------------- function goof_around( board ) -- clone() local mycopy = board:clone() print( "Make a copy, in case I mess it up." ) -- dimension() local dim = mycopy:dimension() print( "A ", dim, "x", dim, "board. That is too big!" ) -- isempty() if mycopy:isempty() then print( "Why the board is empty?" ) else print( "Why the board is not empty?" ) end -- isfull() if mycopy:isfull() then print( "Why the board is full?" ) else print( "The board is not full. There's a lot to do." ) end -- getplies() print( "There're ", mycopy:getplies(), " stones" ) -- pcik a spot local x = 9 local y = 9 print( "Pick up spot (", x, ",", y, ")" ) -- isempty(x, y) if mycopy:isempty(x, y) then print( "(", x, ",", y, ") is empty" ) else print( "(", x, ",", y, ") is not empty" ) end -- set(x,y) if mycopy:set(x, y, mycolor()) then print( "Put a stone on (", x, ",", y, ")" ) else print( "Can't put a stone on (", x, ",", y, ")" ) end -- getstone(x,y) if mycopy:getstone(x,y) == mycolor() then print( "The stone on (", x, ",", y, ") is mine" ) else print( "The stone on (", x, ",", y, ") is not mine" ) end -- reset(x,y) if mycopy:reset(x,y) then print( "Remove the stone from (", x, ",", y, ")" ) else print( "Can't remove the stone from (", x, ",", y, ")" ) end -- clean() mycopy:clean() if mycopy:isempty() then print( "Oops, wipe out all stones. Lucky, I already made a copy." ) else print( "Why I can't wipe all stones?" ) end end ------------------------------------------------------------------------------ -- Randomly pick up a vacant spot, the same as the example in script goofy2.wz ------------------------------------------------------------------------------ function pick_a_vacant_spot( board ) print( "Got to make a move." ) -- set random number seed math.randomseed( os.time() ) -- query the game board's dimension local dim = board:dimension() -- the upper bound of coordinates local bound = dim - 1 -- x and y are random numbers from 0 to bound local x = math.random( 0, bound ) local y = math.random( 0, bound ) -- generate coordinates until a vacant spot found while not board:isempty( x, y ) do x = math.random( 0, bound ) y = math.random( 0, bound ) end -- put a stone board:set( x, y, mycolor() ) print( "Got an empty spot (", x, " ", y, ")." ) -- cache the board using a global variable previousboard = board:clone(); -- return the chosen coordinates return x, y end