gpt4 book ai didi

delphi - 连接4 : Check for winner

转载 作者:行者123 更新时间:2023-12-03 15:20:29 25 4
gpt4 key购买 nike

在 Delphi 中,我有一个数组形式的 Connect 4 板表示(7 列 x 6 行):

TBoard = Array[1..7, 1..6] of SmallInt;
Board: TBoard; // instance ob TBoard

每个元素可以有三种不同的状态:

  • 1 = 玩家 1 的棋子
  • 0 = 空
  • -1 = 玩家 2 的棋子

现在我需要一个函数来检查是否有获胜者或平局:

function CheckForWinner(): SmallInt;

...其中 1 表示玩家 1 获胜,0 表示平局,-1 表示玩家 2 获胜,“nil”表示游戏尚未结束。

我的草稿如下 - 分为两个单一功能:

function CheckForWinner(): SmallInt;
var playerToCheck: ShortInt;
s, z: Byte;
draw: Boolean;
begin
draw := TRUE;
for s := 1 to 7 do begin
for z := 1 to 6 do begin
if Board[s, z] = 0 then draw := FALSE; // if there are empty fields then it is no draw
end;
end;
if draw then begin
result := 0;
end
else begin
playerToCheck := Board[lastPieceX, lastPieceY]; // only for last-moving player
if searchRow(playerToCheck, +1, 0, lastPieceX, lastPieceY) then // search right/left
result := playerToCheck
else if searchRow(playerToCheck, 0, +1, lastPieceX, lastPieceY) then // search up/down
result := playerToCheck
else if searchRow(playerToCheck, +1, +1, lastPieceX, lastPieceY) then // search right-down/left-up
result := playerToCheck
else if searchRow(playerToCheck, +1, -1, lastPieceX, lastPieceY) then // 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) AND (inRow < 4) AND (s >= 1) AND (s <= 7) AND (z >= 1) AND (z <= 6) do begin
s := s+sChange;
z := z+zChange;
inRow := inRow+1;
end;
s := startS-sChange;
z := startZ-zChange;
while (Board[s, z] = player) AND (inRow < 4) AND (s >= 1) AND (s <= 7) AND (z >= 1) AND (z <= 6) do begin
s := s-sChange;
z := z-zChange;
inRow := inRow+1;
end;
if inRow = 4 then
result := TRUE
else
result := FALSE;
end;

您对这种方法有何看法?您有更好(更快/更短)的解决方案吗?

非常感谢!

最佳答案

我没有读过你的代码。我只是选择自己用白纸写一些内容。

这是我的版本:

const
RowCount = 6;
ColCount = 7;

type
TState = (stNone, stA, stB);
TBoard = array [1..RowCount] of array [1..ColCount] of TState;

function ValidLocation(Row, Col: Integer): Boolean;
begin
Result := InRange(Row, 1, RowCount) and InRange(Col, 1, ColCount);
end;

procedure Check(
const Board: TBoard;
const StartRow, StartCol: Integer;
const RowDelta, ColDelta: Integer;
out Winner: TState
);
var
Row, Col, Count: Integer;
State: TState;
begin
Winner := stNone;
Row := StartRow;
Col := StartCol;
State := Board[Row, Col];
if State=stNone then
exit;
Count := 0;
while ValidLocation(Row, Col) and (Board[Row, Col]=State) do begin
inc(Count);
if Count=4 then begin
Winner := State;
exit;
end;
inc(Row, RowDelta);
inc(Col, ColDelta);
end;
end;

function Winner(const Board: TBoard): TState;
var
Row, Col: Integer;
begin
for Row := 1 to RowCount do begin
for Col := 1 to ColCount do begin
Check(Board, Row, Col, 0, 1, Result);//check row
if Result<>stNone then
exit;
Check(Board, Row, Col, 1, 0, Result);//check column
if Result<>stNone then
exit;
Check(Board, Row, Col, 1, 1, Result);//check diagonal
if Result<>stNone then
exit;
Check(Board, Row, Col, 1, -1, Result);//check other diagonal
if Result<>stNone then
exit;
end;
end;
Result := stNone;
end;

一大堆代码。使用暴力方法,对于 Connect 4 来说性能并不重要。不喜欢四个相同的 if Result<>stNone then exit;行,但你肯定可以想出一种更简洁的方法。代码尚未运行。可能根本就行不通!!这正是我的大脑试图解决问题的方式。

关于delphi - 连接4 : Check for winner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5108576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com