private List <Vector2Int> determineMovesNoCheck(Vector2Int movingPos) { List <Vector2Int> correctMoves = new List <Vector2Int>(); GameObject moving = board[movingPos.x, movingPos.y]; Figure figMoving = moving.GetComponent <Figure>(); Vector2Int[] moves = figMoving.Moves; if (moves != null) { for (int i = 0; i < moves.Length; i++) { if ((moves[i] + movingPos).x < 0 || (moves[i] + movingPos).x > 7 || (moves[i] + movingPos).y < 0 || (moves[i] + movingPos).y > 7) { continue; } if (board[(moves[i] + movingPos).x, (moves[i] + movingPos).y] != null) { Figure figtemp = board[(moves[i] + movingPos).x, (moves[i] + movingPos).y].GetComponent <Figure>(); if (figtemp.team == figMoving.team) { continue; } } correctMoves.Add(moves[i] + movingPos); } } if (figMoving.Type == TypeFigure.King && figMoving.team == Team.WHITE && !figMoving.WasMoved) { if (board[7, 0] != null) { Figure rFig = board[7, 0].GetComponent <Figure>(); if (!rFig.WasMoved && board[6, 0] == null && board[5, 0] == null) { correctMoves.Add(new Vector2Int(7, 0)); } } if (board[0, 0] != null) { Figure rFig = board[0, 0].GetComponent <Figure>(); if (!rFig.WasMoved && board[2, 0] == null && board[3, 0] == null && board[1, 0] == null) { correctMoves.Add(new Vector2Int(0, 0)); } } } if (figMoving.Type == TypeFigure.King && figMoving.team == Team.BLACK && !figMoving.WasMoved) { if (board[7, 7] != null) { Figure rFig = board[7, 7].GetComponent <Figure>(); if (!rFig.WasMoved && board[6, 7] == null && board[5, 7] == null) { correctMoves.Add(new Vector2Int(7, 7)); } } if (board[0, 7] != null) { Figure rFig = board[0, 7].GetComponent <Figure>(); if (!rFig.WasMoved && board[2, 7] == null && board[3, 7] == null && board[1, 7] == null) { correctMoves.Add(new Vector2Int(0, 7)); } } } if (figMoving.VectorMoves != null) { Vector2Int[] vMoves = figMoving.VectorMoves; for (int i = 0; i < vMoves.Length; i++) { Vector2Int step = vMoves[i]; if (step.x != 0) { step.x /= Math.Abs(step.x); } if (step.y != 0) { step.y /= Math.Abs(step.y); } int num = 1; while (step * num != vMoves[i]) { if ((movingPos + step * num).x < 0 || (movingPos + step * num).x > 7 || (movingPos + step * num).y < 0 || (movingPos + step * num).y > 7) { break; } if (board[(movingPos + step * num).x, (movingPos + step * num).y] != null) { Figure figtemp = board[(movingPos + step * num).x, (movingPos + step * num).y].GetComponent <Figure>(); if (figtemp.team == figMoving.team || figMoving.Type == TypeFigure.Pawn) { break; } correctMoves.Add((movingPos + step * num)); break; } correctMoves.Add((movingPos + step * num)); num++; } } } if (!figMoving.WasMoved && figMoving.Type == TypeFigure.Pawn) { Vector2Int additional = new Vector2Int(0, 2); if (figMoving.team == Team.BLACK) { additional *= -1; } additional += movingPos; if (board[additional.x, additional.y] == null) { correctMoves.Add(additional); } } if (figMoving.AttackMoves != null) { Vector2Int[] attackMoves = figMoving.AttackMoves; for (int i = 0; i < attackMoves.Length; i++) { if ((attackMoves[i] + movingPos).x < 0 || (attackMoves[i] + movingPos).x > 7 || (attackMoves[i] + movingPos).y < 0 || (attackMoves[i] + movingPos).y > 7) { continue; } if (board[(attackMoves[i] + movingPos).x, (attackMoves[i] + movingPos).y] != null) { Figure figtemp = board[(attackMoves[i] + movingPos).x, (attackMoves[i] + movingPos).y].GetComponent <Figure>(); if (figtemp.team != figMoving.team) { correctMoves.Add(attackMoves[i] + movingPos); } } } } return(correctMoves); }
private void handleUserUpdate() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { Vector3 point = hit.point; Vector2Int cell = BoardHandler.toGamePos(point); selectedCell.SetActive(true); placeCell(selectedCell, cell); selectedCell.transform.position = selectedCell.transform.position + new Vector3(0, 0.1f, 0); if (Input.GetMouseButtonDown(0) && !isSelected) { if (board[cell.x, cell.y] != null) { Figure fig = board[cell.x, cell.y].GetComponent <Figure>(); if (fig.team == makesMove) { isSelected = true; selectedFigure = cell; correctMoves = determineMoves(cell); if (correctMoves.Count != 0) { moveCells = new List <GameObject>(); foreach (Vector2Int move in correctMoves) { GameObject tempMoveCell = Instantiate <GameObject>(moveCellPrefab); placeCell(tempMoveCell, move); moveCells.Add(tempMoveCell); } } else { isSelected = false; } } } } else { if (Input.GetMouseButtonDown(0) && isSelected) { foreach (Vector2Int move in correctMoves) { if (cell.Equals(move)) { moveFigure(selectedFigure, cell); if (isBotPlaying) { imThinking = true; } } } isSelected = false; foreach (GameObject moveCell in moveCells) { Destroy(moveCell); } moveCells.Clear(); } } } else { selectedCell.SetActive(false); } if (isSelected && Input.GetMouseButtonDown(1)) { foreach (GameObject moveCell in moveCells) { Destroy(moveCell); } isSelected = false; moveCells.Clear(); } }
public void moveFigure(Vector2Int oldPos, Vector2Int newPos) { if (newPos.x >= 0 && newPos.x < 8 && newPos.y >= 0 && newPos.y < 8 && board[oldPos.x, oldPos.y] != null) { List <Move> doubleMove = new List <Move>(); Figure fig = board[oldPos.x, oldPos.y].GetComponent <Figure>(); if (board[newPos.x, newPos.y] != null && fig.Type == TypeFigure.King) { Figure fig2 = board[newPos.x, newPos.y].GetComponent <Figure>(); if (fig2.Type == TypeFigure.Rook && fig2.team == fig.team) { if (newPos.x == 7) { doubleMove = new List <Move>(); doubleMove.Add(new Move(board[oldPos.x, oldPos.y], board[6, oldPos.y], oldPos, new Vector2Int(6, oldPos.y), true)); doubleMove.Add(new Move(board[newPos.x, newPos.y], board[5, oldPos.y], newPos, new Vector2Int(5, oldPos.y), true)); moves.Add(doubleMove); fig.WasMoved = true; fig2.WasMoved = true; board[6, oldPos.y] = board[oldPos.x, oldPos.y]; board[oldPos.x, oldPos.y] = null; board[6, oldPos.y].transform.position = BoardHandler.toWorldPos(new Vector2Int(6, oldPos.y)); board[5, oldPos.y] = board[newPos.x, newPos.y]; board[newPos.x, newPos.y] = null; board[5, oldPos.y].transform.position = BoardHandler.toWorldPos(new Vector2Int(5, oldPos.y)); } else { doubleMove = new List <Move>(); doubleMove.Add(new Move(board[oldPos.x, oldPos.y], board[2, oldPos.y], oldPos, new Vector2Int(2, oldPos.y), true)); doubleMove.Add(new Move(board[newPos.x, newPos.y], board[3, oldPos.y], newPos, new Vector2Int(3, oldPos.y), true)); moves.Add(doubleMove); fig.WasMoved = true; fig2.WasMoved = true; board[2, oldPos.y] = board[oldPos.x, oldPos.y]; board[oldPos.x, oldPos.y] = null; board[2, oldPos.y].transform.position = BoardHandler.toWorldPos(new Vector2Int(2, oldPos.y)); board[3, oldPos.y] = board[newPos.x, newPos.y]; board[newPos.x, newPos.y] = null; board[3, oldPos.y].transform.position = BoardHandler.toWorldPos(new Vector2Int(3, oldPos.y)); } } else { Move newMove = new Move(board[oldPos.x, oldPos.y], board[newPos.x, newPos.y], oldPos, newPos, !fig.WasMoved); doubleMove.Add(newMove); moves.Add(doubleMove); fig.WasMoved = true; if (board[newPos.x, newPos.y] != null) { board[newPos.x, newPos.y].SetActive(false); } board[newPos.x, newPos.y] = board[oldPos.x, oldPos.y]; board[oldPos.x, oldPos.y] = null; board[newPos.x, newPos.y].transform.position = BoardHandler.toWorldPos(newPos); } } else { Move newMove = new Move(board[oldPos.x, oldPos.y], board[newPos.x, newPos.y], oldPos, newPos, !fig.WasMoved); doubleMove.Add(newMove); moves.Add(doubleMove); fig.WasMoved = true; if (board[newPos.x, newPos.y] != null) { board[newPos.x, newPos.y].SetActive(false); } board[newPos.x, newPos.y] = board[oldPos.x, oldPos.y]; board[oldPos.x, oldPos.y] = null; board[newPos.x, newPos.y].transform.position = BoardHandler.toWorldPos(newPos); } changeTurn(); } }