delphi - Connect 4: Check for winner -


in delphi, have connect 4 board representation (7 columns x 6 lines) in form of array:

tboard = array[1..7, 1..6] of smallint; board: tboard; // instance ob tboard 

each element can have 3 different states:

  • 1 = player 1's pieces
  • 0 = empty
  • -1 = player 2's pieces

now need function checks if there's winner or draw:

function checkforwinner(): smallint; 

... 1 player 1's win, 0 draw, -1 player 2's win , "nil" game has not ended yet.

my draft follows - split 2 single functions:

function checkforwinner(): smallint; var playertocheck: shortint;     s, z: byte;     draw: boolean; begin   draw := true;   s := 1 7 begin     z := 1 6 begin       if board[s, z] = 0 draw := false; // if there empty fields no draw     end;   end;   if draw begin     result := 0;   end   else begin     playertocheck := board[lastpiecex, lastpiecey]; // last-moving player     if searchrow(playertocheck, +1, 0, lastpiecex, lastpiecey) // search right/left       result := playertocheck     else if searchrow(playertocheck, 0, +1, lastpiecex, lastpiecey) // search up/down       result := playertocheck     else if searchrow(playertocheck, +1, +1, lastpiecex, lastpiecey) // search right-down/left-up       result := playertocheck     else if searchrow(playertocheck, +1, -1, lastpiecex, lastpiecey) // search right-up/left-down       result := playertocheck;     else       result := nil;     end;   end; end;  function searchrow(player: smallint; schange, zchange: shortint; starts, startz: byte): boolean; var inrow, s, z: smallint; begin   inrow := 0;   s := starts;   z := startz;   while (board[s, z] = player) , (inrow < 4) , (s >= 1) , (s <= 7) , (z >= 1) , (z <= 6) begin     s := s+schange;     z := z+zchange;     inrow := inrow+1;   end;   s := starts-schange;   z := startz-zchange;   while (board[s, z] = player) , (inrow < 4) , (s >= 1) , (s <= 7) , (z >= 1) , (z <= 6) begin     s := s-schange;     z := z-zchange;     inrow := inrow+1;   end;   if inrow = 4     result := true   else     result := false; end; 

what think of approach? have better (faster / shorter) solution?

thank much!

checking winner in same way do, little less code. think wouldn't need check fields determine if game done. increase counter when drop piece in game. game draw if counter reaches 42 , there no winner yet.

function checkrow(x, y, xd, yd: integer): boolean; var   c: integer;    function rowlength(x, y, xd, yd: integer): integer;   begin     result := 0;     repeat       inc(result);       inc(x, xd);       inc(y, yd);     until not ((x in [1..7]) , (y in [1..6]) , (board[x, y] = c));   end;  begin   c := board[x, y];    result := 4 <= rowlength(x, y, xd, yd) + rowlength(x, y, xd*-1, yd*-1) - 1; end;  function checkforwinner(x, y: integer): integer; begin   result := 0;   if checkrow(x, y, 0, 1) or checkrow(x, y, 1, 1) or      checkrow(x, y, 1, 0) or checkrow(x, y, 1, -1)     result := board[x,y]; end; 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -