{
Example Pascal program for the CodeCup 2009 game Pillars.
This example illustrates how I/O works with the CodeCup system.
Written by Willem van der Vegt, willem@informaticaolympiade.nl
Feel free to modify or use this code.
}
program pascal_pillars;
{board represents the gameboard. board[x,y] = false if the tile at (x, y) is empty }
var board:array[0..9,0..9] of boolean;
s: string;
procedure readPillars;
{Read the positions of the initial 10 pillars.}
var i,x,y: integer;
begin
for x:=0 to 9 do for y:=0 to 9 do board[x,y]:=false;
for i:=1 to 10 do
begin
readln(s);
y:=ord(s[1])-ord('A');
x:=ord(s[2])-ord('a');
board[x,y]:=true
end
end;
procedure processOpponentsMove(s: string);
var x,y,x1,x2,y1,y2: integer;
{Update the board to account for the opponent's move.}
begin
y1:=ord(s[1]) - ord('A');
x1:=ord(s[2]) - ord('a');
y2:=ord(s[3]) - ord('A');
x2:=ord(s[4]) - ord('a');
for x:=x1 to x2 do for y:=y1 to y2 do board[x,y]:=true
end;
procedure doOwnMove;
var x,y: integer;
{Select the best move for us, write it to the output, and update
the board. This example just selects a random tile.
It is up to you to make this function as smart as possible.}
begin
x:=random(10);
y:=random(10);
while board[x,y] do
begin
inc(y);
if y=10 then
begin
y:=0;
inc(x);
if x=10 then x:=0
end
end;
{ Found an empty spot in position (x, y)}
board[x,y]:=true;
s:=chr(y+ord('A'))+chr(x+ord('a'))+chr(y+ord('A'))+chr(x+ord('a'));
writeln(s);
flush(output)
end;
begin
{Main program.}
readPillars;
readln(s);
if s='Start' then
begin
{ We play with Red.}
doOwnMove;
readln(s)
end;
while s<>'Quit' do
begin
processOpponentsMove(s);
doOwnMove;
readln(s)
end
end.
syntax highlighted by Code2HTML, v. 0.9.1