有趣的是,问题并不总是存在,即不同的代码运行可能具有相同的选择策略不同的行为。在这段代码中,我试图检查玩家选择的单元格旁边是否还有该玩家或他的塔的另一个筹码,如果有塔,那么我尝试在链条旁边找到至少一个筹码塔,但是当尝试在距离常规芯片 3 个单元的距离处创建塔时,递归函数会出错,而且这个东西在某个时刻可能会崩溃。
private bool IsAdjacentToCurrentPlayer(Vector2 currentPosition) {
// Check adjacent cells for the current player's chip
return
checkCell(currentPosition + Vector2.up) ||
checkCell(currentPosition + Vector2.down) ||
checkCell(currentPosition + Vector2.left) ||
checkCell(currentPosition + Vector2.right);
}
private bool checkCell(Vector2 position) {
Vector2 currentTowerPosition;
// Check the state of the cell at the given position
RaycastHit2D hit = Physics2D.Raycast(position, Vector2.zero);
if (hit.collider != null) {
TurnScript ts = hit.collider.GetComponent<TurnScript>();
if (ts != null && ts.spriteRenderer.sprite == currentPlayerSet[0]) {
return true;
} else if (ts != null && ts.spriteRenderer.sprite == currentPlayerSet[1]) {
currentTowerPosition = ts.spriteRenderer.transform.position;
// trying to find a normal chip
if (IsAdjacentToCurrentPlayer(currentTowerPosition)){
return true;
}
}
}
return false;
}
private bool IsAdjacentToPlayerRegularChip(Vector2 currentPosition) {
RaycastHit2D[] hits = Physics2D.RaycastAll(currentPosition, Vector2.zero);
foreach (RaycastHit2D hit in hits) {
TurnScript ts = hit.collider.GetComponent<TurnScript>();
if (ts != null && ts.spriteRenderer.sprite == currentPlayerSet[0]) {
return true;
}
}
return false;
}
private bool IsAdjacentToTowers(Vector2 currentPosition) {
if (currentPosition.x == -3 && currentPosition.y == 3) {
// Check if there is a chip of the same player adjacent to the row of towers
if (checkCell(currentPosition + Vector2.right)) {
return true;
}
} else if (currentPosition.x == 3 && currentPosition.y == 3) {
// Check if there is a chip of the same player adjacent to the row of towers
if (checkCell(currentPosition + Vector2.left)) {
return true;
}
} else if (currentPosition.x == -3 && currentPosition.y == -3) {
// Check if there is a chip of the same player adjacent to the row of towers
if (checkCell(currentPosition + Vector2.up)) {
return true;
}
} else if (currentPosition.x == 3 && currentPosition.y == -3) {
// Check if there is a chip of the same player adjacent to the row of towers
if (checkCell(currentPosition + Vector2.down)) {
return true;
}
}
return false;
}
private bool checkCell(Vector2 position) {
RaycastHit2D hit = Physics2D.Raycast(position, Vector2.zero);
if (hit.collider != null) {
TurnScript ts = hit.collider.GetComponent<TurnScript>();
if (ts != null && ts.spriteRenderer.sprite == currentPlayerSet[0]) {
return true;
} else if (ts != null && ts.spriteRenderer.sprite == currentPlayerSet[1]) {
isChainSecured = true;
return true;
}
}
return false;
}
和另一次尝试从另一边接近
private bool IsAdjacentToCurrentPlayer(Vector2 currentPosition) {
// Check adjacent cells for the current player's chip
if (checkCell(currentPosition + Vector2.up) ||
checkCell(currentPosition + Vector2.down) ||
checkCell(currentPosition + Vector2.left) ||
checkCell(currentPosition + Vector2.right)) {
return true;
}
// Check if there is at least one regular chip adjacent to the row of towers
return CheckAdjacentRegularChip(currentPosition);
}
private bool CheckAdjacentRegularChip(Vector2 currentPosition) {
RaycastHit2D[] hits = Physics2D.RaycastAll(currentPosition, Vector2.zero);
foreach (RaycastHit2D hit in hits) {
TurnScript ts = hit.collider.GetComponent<TurnScript>();
if (ts != null && ts.spriteRenderer.sprite == currentPlayerSet[0]) {
return true;
}
}
return false;
}