I'm making a Connect 4 game for a school assignment, and my code should work in theory, but in reality it doesn't. Whenever I run the game, the board tries to draw itself, but then fails to do so. Can anyone please give me any advice? Thanks in advance.
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
const
nbrcols=7;
nbrrows=6;
sidewidth:integer=10;
player1color:TColor=clred;
player2color:TColor=clyellow;
boardcolor:TColor=clblue;
lookahead:integer=4;
type
TFormGame = class(TForm)
BtnExitGame: TButton;
PnlGame: TPanel;
Image1: TImage;
NewChip: TShape;
BtnNew: TButton;
BtnRetract: TButton;
procedure TokenMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure TokenMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure TokenMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure BtnExitGameClick(Sender: TObject);
procedure BtnNewClick(Sender: TObject);
procedure BtnRetractClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
board:array [1..nbrcols, 1..nbrrows]of integer;
moves:array[1..nbrcols*nbrrows] of TPoint; player1:boolean;
chipwidth:integer;
Dragchip:boolean;
movecount:integer;
gameover:boolean;
procedure initialize;
Procedure DrawChip(x:integer);
procedure DropChip(x:integer);
function FourInARow(col,row:integer):boolean;
function match(col,row,dc,dr:integer):integer;
procedure changeplayers;
end;
var
FormGame: TFormGame;
implementation
uses Unit1;
{$R *.dfm}
procedure tFormGame.initialize;
procedure DrawNewBoard;
var
i,hinc:integer;
begin
with image1, canvas do
begin
chipwidth:=(PnlGame.width-(nbrcols+1)*sidewidth) div nbrcols;
PnlGame.width:=nbrcols*(chipwidth+sidewidth)+sidewidth+2;
PnlGame.height:=(nbrrows+1)*chipwidth+sidewidth+2;
picture.bitmap.width:=width;
picture.bitmap.height:=height;
brush.color:=clwindow;
fillrect(clientrect);
brush.color:=boardcolor; pen.color:=boardcolor;
rectangle(rect(0,height-sidewidth,width,height));
hinc:=(width-10) div nbrcols;
for i:= 0 to nbrcols do rectangle(rect(i*hinc,chipwidth, i*hinc+sidewidth,height-sidewidth));
end;
newchip.width:=chipwidth;
newchip.height:=chipwidth;
drawchip(chipwidth div 2);
end;{Drawnewboard}
var i,j:integer;
begin
for i:=1 to nbrcols do for j:=1 to nbrrows do board[i,j]:=0;
movecount:=0;
player1:=false;
changeplayers;
drawnewboard;
tag:=1;
gameover:=false;
end;
procedure TFormGame.BtnRetractClick(Sender: TObject);
var
h,v:integer;
begin
if movecount>0 then
begin
with image1.canvas, moves[movecount] do
begin
board[x,y]:=0;
brush.color:=clwindow;
h:=sidewidth+(x-1)*(chipwidth+sidewidth);
v:=y*chipwidth;
fillrect(rect(h,v, h+chipwidth, v+chipwidth));
end;
dec(movecount);
changeplayers;
drawchip(chipwidth div 2);
end;
end;
procedure TFormGame.BtnNewClick(Sender: TObject);
begin
Initialize;
end;
procedure TFormGame.Changeplayers;
begin
newchip.top:=0;
player1:=not player1;
if player1 then
begin
newchip.brush.color:=player1color;
end
else
begin
newchip.brush.color:=player2color;
end;
end;
procedure TFormGame.drawchip(x:integer);
begin
newchip.left:=x-chipwidth div 2;
newchip.visible:=true;
end;
function TFormGame.match(col,row,dc,dr:integer):integer;
var c,r,count, checkplayer:integer;
begin
checkplayer:=board[col,row];
c:=col+dc;
r:=row+dr;
count:=0;
while (c>=1) and (c<=nbrcols) and (r>=1) and (r<=nbrrows) and
(board[c,r]=checkplayer) do
begin
inc(c,dc); inc(r,dr);
inc(count);
end;
result:=count;
end;
function TFormGame.FourInARow(col,row:integer):boolean;
{Chck for 4 tokens in a row}
var n:integer;
begin
n:=1+match(col,row,-1,0)+match(col,row,+1,0);
if n<4 then n:=1+match(col,row,0,-1)+match(col,row,0,+1);
if n<4 then n:=1+match(col,row,-1,-1)+match(col,row,+1,+1);
if n<4 then n:=1+match(col,row,-1,+1)+match(col,row,+1,-1);
if n>=4 then result:=true else result:=false;
end;
procedure TFormGame.dropchip(x:integer);
var
col, row, i:integer;
msg:string;
begin
col:=x div(chipwidth+sidewidth)+1 ;
newchip.left:=sidewidth+(col-1)*(chipwidth+sidewidth);
row:=1;
while (row<=nbrrows) and (board[col,row]=0)
do inc(row);
if row=1 then exit;
dec(row);
with newchip do
for i:=1 to row+1 do
begin
top:=(i-1)*chipwidth;
update;
sleep(100);
end;
if player1 then board[col,row]:=1
else board[col,row]:=2;
with image1.canvas do
begin
brush.color:=newchip.brush.color;
with newchip do ellipse(left,top, left+width, top+height);
end;
inc(movecount);
moves[movecount]:=point(col,row); if (movecount=nbrcols*nbrrows) or fourinarow(col,row) then
begin
if fourinarow(col,row) then
if player1 then msg:='Player 1 is the winner!'
else msg:='Player 2 is the winner!'
else msg:='A draw!';
Gameover:=true;
newchip.visible:=false;
end;
end;
procedure TFormGame.TokenMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if gameover then exit;
DragChip:=true;
newchip.top:=0;
drawchip(newchip.left+x);
end;
procedure TFormGame.TokenMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if dragchip
then drawchip(newchip.left+x);
end;
procedure TFormGame.TokenMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if board[(newchip.left+x) div (chipwidth+sidewidth)+1,1]<>0 then
begin
newchip.left:=0;
exit;
end;
dropchip(newchip.left+x);
dragchip:=false;
if not gameover then
begin
changeplayers;
drawchip(chipwidth div 2);
end
else
begin
newchip.top:=0;
player1:=not player1;
if player1 then newchip.brush.color:=player1color
else newchip.brush.color:=player2color;
end;
end;
procedure TFormGame.BtnExitGameClick(Sender: TObject);
begin
FormGame.close;
FormMain.show;
end;
end.