delphi - Connect 4: Check for winner

Delphi - Connect 4: Check for winner

To check for a winner in a Connect Four game implemented in Delphi, you'll need to verify if there are four consecutive discs of the same player in any direction: horizontal, vertical, or diagonal. Here's a step-by-step guide and sample code to achieve this:

1. Define the Game Board

Assume you have a 6x7 board (6 rows, 7 columns). You'll use a 2D array to represent the board.

const ROWS = 6; COLS = 7; type TBoard = array[0..ROWS-1, 0..COLS-1] of Integer; var Board: TBoard; 

2. Initialize the Board

You can initialize the board with zeros (or any other default value) to indicate empty cells.

procedure InitializeBoard(var Board: TBoard); var Row, Col: Integer; begin for Row := 0 to ROWS - 1 do for Col := 0 to COLS - 1 do Board[Row, Col] := 0; // 0 indicates an empty cell end; 

3. Check for a Winner

You need to check for a sequence of four discs in horizontal, vertical, and diagonal directions.

3.1 Horizontal Check

function CheckHorizontal(Board: TBoard; Player: Integer): Boolean; var Row, Col, Count: Integer; begin Result := False; for Row := 0 to ROWS - 1 do begin Count := 0; for Col := 0 to COLS - 1 do begin if Board[Row, Col] = Player then Inc(Count) else Count := 0; if Count = 4 then begin Result := True; Exit; end; end; end; end; 

3.2 Vertical Check

function CheckVertical(Board: TBoard; Player: Integer): Boolean; var Row, Col, Count: Integer; begin Result := False; for Col := 0 to COLS - 1 do begin Count := 0; for Row := 0 to ROWS - 1 do begin if Board[Row, Col] = Player then Inc(Count) else Count := 0; if Count = 4 then begin Result := True; Exit; end; end; end; end; 

3.3 Diagonal Check

You need to check both diagonal directions: from bottom-left to top-right and from bottom-right to top-left.

Bottom-left to Top-right:

function CheckDiagonalBLTR(Board: TBoard; Player: Integer): Boolean; var Row, Col, Count: Integer; begin Result := False; for Row := 0 to ROWS - 1 do begin for Col := 0 to COLS - 1 do begin Count := 0; // Check diagonally down-right for var i := 0 to 3 do if (Row + i < ROWS) and (Col + i < COLS) and (Board[Row + i, Col + i] = Player) then Inc(Count) else Break; if Count = 4 then begin Result := True; Exit; end; end; end; end; 

Bottom-right to Top-left:

function CheckDiagonalBRTL(Board: TBoard; Player: Integer): Boolean; var Row, Col, Count: Integer; begin Result := False; for Row := 0 to ROWS - 1 do begin for Col := 0 to COLS - 1 do begin Count := 0; // Check diagonally down-left for var i := 0 to 3 do if (Row + i < ROWS) and (Col - i >= 0) and (Board[Row + i, Col - i] = Player) then Inc(Count) else Break; if Count = 4 then begin Result := True; Exit; end; end; end; end; 

4. Check for a Winner

Combine all checks to determine if a player has won.

function CheckForWinner(Board: TBoard; Player: Integer): Boolean; begin Result := CheckHorizontal(Board, Player) or CheckVertical(Board, Player) or CheckDiagonalBLTR(Board, Player) or CheckDiagonalBRTL(Board, Player); end; 

5. Example Usage

Here's how you might use these functions:

begin InitializeBoard(Board); // Assume Player 1 has made moves and you want to check if Player 1 has won if CheckForWinner(Board, 1) then ShowMessage('Player 1 wins!') else ShowMessage('No winner yet.'); end; 

Summary

  1. Initialize the Board: Set up your board with default values.
  2. Check Horizontal, Vertical, and Diagonal Lines: Implement functions to check for four consecutive discs in each direction.
  3. Determine the Winner: Combine the checks to find out if a player has won.

This code assumes that Player is an integer representing the player's ID or disc type. Adjust the constants and logic based on your specific game implementation.

Examples

  1. How to check for a Connect 4 winner in Delphi?

    Description: Implements a function to check for a winner in Connect 4 by scanning rows, columns, and diagonals.

    Code:

    function CheckWinner(Board: array of array of Integer): Integer; var i, j: Integer; begin Result := 0; // Check rows for i := 0 to High(Board) do for j := 0 to High(Board[i]) - 3 do if (Board[i][j] = Board[i][j+1]) and (Board[i][j] = Board[i][j+2]) and (Board[i][j] = Board[i][j+3]) and (Board[i][j] <> 0) then Exit(Board[i][j]); // Check columns for j := 0 to High(Board[0]) do for i := 0 to High(Board) - 3 do if (Board[i][j] = Board[i+1][j]) and (Board[i][j] = Board[i+2][j]) and (Board[i][j] = Board[i+3][j]) and (Board[i][j] <> 0) then Exit(Board[i][j]); // Check diagonals (left-top to right-bottom) for i := 0 to High(Board) - 3 do for j := 0 to High(Board[i]) - 3 do if (Board[i][j] = Board[i+1][j+1]) and (Board[i][j] = Board[i+2][j+2]) and (Board[i][j] = Board[i+3][j+3]) and (Board[i][j] <> 0) then Exit(Board[i][j]); // Check diagonals (right-top to left-bottom) for i := 0 to High(Board) - 3 do for j := 3 to High(Board[i]) do if (Board[i][j] = Board[i+1][j-1]) and (Board[i][j] = Board[i+2][j-2]) and (Board[i][j] = Board[i+3][j-3]) and (Board[i][j] <> 0) then Exit(Board[i][j]); end; 
  2. How to implement Connect 4 winner check for a specific player in Delphi?

    Description: Checks if a specific player has won the Connect 4 game.

    Code:

    function IsPlayerWinner(Board: array of array of Integer; Player: Integer): Boolean; var i, j: Integer; begin Result := False; // Check rows for i := 0 to High(Board) do for j := 0 to High(Board[i]) - 3 do if (Board[i][j] = Player) and (Board[i][j] = Board[i][j+1]) and (Board[i][j] = Board[i][j+2]) and (Board[i][j] = Board[i][j+3]) then Exit(True); // Check columns for j := 0 to High(Board[0]) do for i := 0 to High(Board) - 3 do if (Board[i][j] = Player) and (Board[i][j] = Board[i+1][j]) and (Board[i][j] = Board[i+2][j]) and (Board[i][j] = Board[i+3][j]) then Exit(True); // Check diagonals (left-top to right-bottom) for i := 0 to High(Board) - 3 do for j := 0 to High(Board[i]) - 3 do if (Board[i][j] = Player) and (Board[i][j] = Board[i+1][j+1]) and (Board[i][j] = Board[i+2][j+2]) and (Board[i][j] = Board[i+3][j+3]) then Exit(True); // Check diagonals (right-top to left-bottom) for i := 0 to High(Board) - 3 do for j := 3 to High(Board[i]) do if (Board[i][j] = Player) and (Board[i][j] = Board[i+1][j-1]) and (Board[i][j] = Board[i+2][j-2]) and (Board[i][j] = Board[i+3][j-3]) then Exit(True); end; 
  3. How to detect a draw in Connect 4 using Delphi?

    Description: Determines if the game is a draw by checking if the board is full and no winner is found.

    Code:

    function IsDraw(Board: array of array of Integer): Boolean; var i, j: Integer; begin Result := True; // Check if the board is full for i := 0 to High(Board) do for j := 0 to High(Board[i]) do if Board[i][j] = 0 then Exit(False); // There is still an empty spot // Check if there is no winner if (CheckWinner(Board) = 0) then Exit(True); // It's a draw end; 
  4. How to handle invalid moves in Connect 4 in Delphi?

    Description: Validates and handles invalid moves such as placing a disc in a full column.

    Code:

    function IsMoveValid(Board: array of array of Integer; Column: Integer): Boolean; begin Result := (Column >= 0) and (Column <= High(Board[0])) and (Board[0][Column] = 0); end; 
  5. How to initialize a Connect 4 board in Delphi?

    Description: Initializes a Connect 4 board with zeros representing empty spaces.

    Code:

    procedure InitializeBoard(var Board: array of array of Integer); var i, j: Integer; begin for i := 0 to High(Board) do for j := 0 to High(Board[i]) do Board[i][j] := 0; // 0 represents an empty cell end; 
  6. How to place a disc in Connect 4 in Delphi?

    Description: Places a disc in the specified column and updates the board accordingly.

    Code:

    procedure PlaceDisc(var Board: array of array of Integer; Column: Integer; Player: Integer); var i: Integer; begin for i := High(Board) downto 0 do if Board[i][Column] = 0 then begin Board[i][Column] := Player; Break; end; end; 
  7. How to check if a column is full in Connect 4 using Delphi?

    Description: Checks if a column is full, preventing further discs from being placed.

    Code:

    function IsColumnFull(Board: array of array of Integer; Column: Integer): Boolean; begin Result := Board[0][Column] <> 0; end; 
  8. How to update the game status after a move in Delphi?

    Description: Updates the game status (winner, draw) after a player makes a move.

    Code:

    procedure UpdateGameStatus(Board: array of array of Integer; var GameStatus: string); var Winner: Integer; begin Winner := CheckWinner(Board); if Winner <> 0 then GameStatus := Format('Player %d wins!', [Winner]) else if IsDraw(Board) then GameStatus := 'It''s a draw!' else GameStatus := 'Game in progress...'; end; 
  9. How to highlight the winning line in Connect 4 using Delphi?

    Description: Highlights the winning line when a player wins.

    Code:

    procedure HighlightWinningLine(Board: array of array of Integer; Winner: Integer; var HighlightedCells: TArray<TPoint>); var i, j: Integer; begin // Example for row highlight, similar logic can be applied for columns and diagonals for i := 0 to High(Board) do for j := 0 to High(Board[i]) - 3 do if (Board[i][j] = Winner) and (Board[i][j] = Board[i][j+1]) and (Board[i][j] = Board[i][j+2]) and (Board[i][j] = Board[i][j+3]) then begin HighlightedCells := [TPoint.Create(i, j), TPoint.Create(i, j+1), TPoint.Create(i, j+2), TPoint.Create(i, j+3)]; Exit; end; end; 
  10. How to implement undo functionality in Connect 4 with Delphi?

    Description: Implements undo functionality to revert the last move made in the game.

    Code:

    procedure UndoLastMove(var Board: array of array of Integer; var MovesHistory: TList<TPoint>); var LastMove: TPoint; i: Integer; begin if MovesHistory.Count = 0 then Exit; // No moves to undo LastMove := MovesHistory.Last; MovesHistory.Delete(MovesHistory.Count - 1); // Remove the last disc placed for i := 0 to High(Board) do if Board[i][LastMove.X] = 1 then begin Board[i][LastMove.X] := 0; Exit; end; end; 

More Tags

placeholder 2d illegalargumentexception django-1.9 gradle-task self-extracting preact media-player sqlxml entity

More Programming Questions

More Biochemistry Calculators

More Chemical thermodynamics Calculators

More Bio laboratory Calculators

More Internet Calculators