Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit ChessBoard;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids,
- StdCtrls{, pawnswitch};
- type
- { TFormChessBoard }
- TFormChessBoard = class(TForm)
- DGridChess: TDrawGrid;
- BtnResign: TButton;
- BtnDraw: TButton;
- BtnSave: TButton;
- BtnQuit: TButton;
- LblTurn: TLabel;
- LblGuidance: TLabel;
- procedure DGridChessBeforeSelection(Sender: TObject; aCol, aRow: integer);
- procedure DGridChessDrawCell(Sender: TObject; aCol, aRow: integer;
- aRect: TRect; aState: TGridDrawState);
- procedure BtnQuitClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- private
- { private declarations }
- public
- { public declarations }
- end;
- type
- TPieces = record
- Name: string[6];
- Colour: string[6];
- Player: integer;
- PNG: TPortableNetworkGraphic;
- CanSelect: boolean;
- FirstPawnMove: boolean
- end;
- TFile = record
- Player1Colour: string[6];
- Player2Colour: string[6];
- Player1Name: string[15];
- Player2name: string[15];
- Boardsize: integer;
- CurrentTurn: integer;
- SavePiecesArray: array[0..15, 0..15] of TPieces;
- end;
- var
- FormChessBoard: TFormChessBoard;
- Pieces: Tpieces;
- PiecesArray: array[0..15, 0..15] of TPieces;
- ONMOVE: boolean;
- CurrentTurn, CurrentCol, CurrentRow: integer;
- implementation
- {$R *.lfm}
- { TFormChessBoard }
- //Get's the piece files and loads them into the grid
- procedure PieceStorage(var DGridChess: TDrawGrid);
- var
- Count, Count2: integer;
- begin
- //Draws each piece onto the chess board
- for Count := 0 to 15 do
- for count2 := 0 to 15 do
- begin
- Pieces := PiecesArray[Count, count2];
- if pieces.Name <> '' then
- begin
- try
- DGridChess.canvas.Draw(64 * Count, 64 * count2, pieces.PNG);
- finally
- end;
- end;
- end;
- end;
- //Fills the cells of the chess board
- procedure TFormChessBoard.DGridChessDrawCell(Sender: TObject;
- aCol, aRow: integer; aRect: TRect; aState: TGridDrawState);
- var
- RowNumber, ColumnNumber, Count: integer;
- begin
- // Colours the cells of the grid black/white
- with TDrawGrid(Sender) do
- begin
- // Fills everything with white
- DGridChess.Color := clwhite;
- // Fills the correct cells with black
- for RowNumber := 0 to 7 do
- begin
- if rownumber mod 2 = 0 then
- ColumnNumber := 1
- else
- columnnumber := 0;
- for Count := 1 to 4 do
- begin
- Canvas.Brush.Color := clblack;
- DGridChess.Canvas.FillRect(TDrawGrid(Sender).CellRect(
- columnnumber, rownumber));
- ColumnNumber := columnnumber + 2;
- end;
- end;
- end;
- PieceStorage(DGridChess);
- end;
- function ToggleTurn(var CurrentTurn: integer): integer;
- begin
- //Toggles who's turn it is
- if CurrentTurn = 1 then
- begin
- ToggleTurn := 2;
- FormChessboard.LblTurn.Caption := ('Player 2''s turn');
- end
- else
- begin
- ToggleTurn := 1;
- FormChessboard.LblTurn.Caption := ('Player 1''s turn');
- end;
- end;
- procedure VisuallyMove(ACol, Arow, CurrentCol, CurrentRow: integer);
- begin
- //Loads the data of the piece into the new cell
- PiecesArray[ACol, ARow] := PiecesArray[CurrentCol, CurrentRow];
- with PiecesArray[acol, arow] do
- begin
- FirstPawnMove := False;
- PNG := TPortableNetworkGraphic.Create;
- PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- end;
- //Clears the piece's previous cell
- with PiecesArray[CurrentCol, CurrentRow] do
- begin
- Player := 0;
- Colour := '';
- Name := '';
- PNG.Free;
- FirstPawnMove := False;
- end;
- OnMove := False;
- end;
- //Pawn Piece Logic
- procedure PawnCheck(ACol, ARow: integer);
- var
- PieceDirection, FirstMoveDirection, CurrentPlayer, EndofBoard, OtherPlayer: integer;
- begin
- if PiecesArray[Acol, Arow].Player = 1 then
- begin
- PieceDirection := -1;
- FirstMoveDirection := -2;
- CurrentPlayer := 1;
- OtherPlayer := 2;
- EndofBoard := 0;
- end
- else
- begin
- PieceDirection := 1;
- FirstMoveDirection := 2;
- CurrentPlayer := 2;
- OtherPlayer := 1;
- EndofBoard := 7;
- end;
- if PiecesArray[Acol, Arow + PieceDirection].Player = 0 then
- PiecesArray[Acol, Arow + PieceDirection].CanSelect := True;
- if PiecesArray[Acol + 1, Arow + PieceDirection].Player = OtherPlayer then
- PiecesArray[Acol + 1, Arow + PieceDirection].CanSelect := True;
- if PiecesArray[Acol - 1, Arow + PieceDirection].Player = OtherPlayer then
- PiecesArray[Acol - 1, Arow + PieceDirection].CanSelect := True;
- if (PiecesArray[Acol, Arow].FirstPawnMove = True) and
- (PiecesArray[Acol, Arow + FirstMoveDirection].Player <> CurrentPlayer) then
- PiecesArray[Acol, Arow + FirstMoveDirection].CanSelect := True;
- end;
- //Knight Piece Logic
- procedure KnightCheck(ACol, ARow: integer);
- begin
- end;
- //Rook Piece Logic
- procedure RookCheck(ACol, ARow: integer);
- begin
- end;
- //King Piece Logic
- procedure KingCheck(ACol, ARow: integer);
- begin
- end;
- //Queen Piece Logic
- procedure QueenCheck(ACol, ARow: integer);
- begin
- end;
- //Bishop Piece Logic
- procedure BishopCheck(ACol, ARow: integer);
- begin
- end;
- //Checks where the selected piece can move to
- procedure CheckingPiece(ACol, ARow: integer);
- var
- Count, count2: integer;
- begin
- //resets the cells where the piece can move to
- for Count := 1 to 15 do
- for count2 := 1 to 15 do
- PiecesArray[Count, count2].CanSelect := False;
- //Does the corresponding check on where the piece
- //can move to dependng on what the piece is
- if PiecesArray[ACol, ARow].Name = 'Pawn' then
- PawnCheck(ACol, ARow)
- else if PiecesArray[ACol, ARow].Name = 'Knight' then
- KnightCheck(ACol, ARow)
- else if PiecesArray[ACol, ARow].Name = 'Rook' then
- RookCheck(ACol, ARow)
- else if PiecesArray[ACol, ARow].Name = 'King' then
- KingCheck(ACol, ARow)
- else if PiecesArray[ACol, ARow].Name = 'Queen' then
- QueenCheck(ACol, ARow)
- else if PiecesArray[ACol, ARow].Name = 'Bishop' then
- BishopCheck(ACol, ARow);
- end;
- procedure TFormChessBoard.DGridChessBeforeSelection(Sender: TObject;
- aCol, aRow: integer);
- begin
- //Checks if the player clicks no piece as the piece to move, do nothing
- if (PiecesArray[ACol, ARow].Name = '') and (OnMove = False) then
- begin
- end
- else
- //If selected piece matches who's turn it is, then check where the piece can move to
- if (PiecesArray[acol, arow].Player = CurrentTurn) then
- begin
- //Gets the cell coordinates of the piece which needs moving
- CurrentCol := Acol;
- CurrentRow := Arow;
- CheckingPiece(Acol, Arow);
- OnMove := True;
- end
- else
- if ((Acol = CurrentCol) and (Arow = CurrentRow)) then
- OnMove := False
- else
- //Checks if the player is able to move the piece there
- if PiecesArray[ACol, ARow].CanSelect = True then
- begin
- CurrentTurn := ToggleTurn(CurrentTurn);
- VisuallyMove(Acol, Arow, CurrentCol, CurrentRow);
- //Moves the piece to the new cell
- end;
- end;
- //Closes form when the quit button is selected
- procedure TFormChessBoard.BtnQuitClick(Sender: TObject);
- begin
- Close;
- end;
- //IN DEVELOPMENT-Hardcode piece initialisation (will be called from file later in development)
- procedure TFormChessBoard.FormCreate(Sender: TObject);
- var
- Count, Count2: integer;
- begin
- with Pieces do
- begin
- //Initialisation of the variables
- OnMove := False;
- CurrentTurn := 1;
- CurrentCol := -1;
- CurrentRow := -1;
- LblTurn.Caption := ('Player 1''s turn');
- FirstPawnMove := False;
- Name := '';
- Colour := '';
- Player := 0;
- CanSelect := False;
- for Count := 1 to 15 do
- for Count2 := 1 to 15 do
- begin
- PiecesArray[Count, Count2] := Pieces;
- end;
- //TEMPORARY- Hard coded piece initialisation
- Player := 1;
- Colour := 'White';
- Name := 'Bishop';
- PiecesArray[2, 7] := Pieces;
- PiecesArray[2, 7].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[2, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- PiecesArray[5, 7] := Pieces;
- PiecesArray[5, 7].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[5, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- Name := 'Rook';
- PiecesArray[0, 7] := Pieces;
- PiecesArray[0, 7].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[0, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- PiecesArray[7, 7] := Pieces;
- PiecesArray[7, 7].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[7, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- Name := 'Knight';
- PiecesArray[6, 7] := Pieces;
- PiecesArray[6, 7].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[6, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- PiecesArray[1, 7] := Pieces;
- PiecesArray[1, 7].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[1, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- Name := 'Queen';
- PiecesArray[3, 7] := Pieces;
- PiecesArray[3, 7].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[3, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- Name := 'King';
- PiecesArray[4, 7] := Pieces;
- PiecesArray[4, 7].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[4, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- Name := 'Pawn';
- FirstPawnMove := True;
- for Count := 0 to 7 do
- begin
- PiecesArray[Count, 6] := Pieces;
- PiecesArray[Count, 6].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[Count, 6].PNG.LoadFromFile('Pieces\' + Colour +
- '\' + Name + '.png');
- end;
- Player := 2;
- Colour := 'Black';
- FirstPawnMove := False;
- Name := 'Bishop';
- PiecesArray[2, 0] := Pieces;
- PiecesArray[2, 0].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[2, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- PiecesArray[5, 0] := Pieces;
- PiecesArray[5, 0].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[5, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- Name := 'Rook';
- PiecesArray[0, 0] := Pieces;
- PiecesArray[0, 0].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[0, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- PiecesArray[7, 0] := Pieces;
- PiecesArray[7, 0].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[7, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- Name := 'Knight';
- PiecesArray[1, 0] := Pieces;
- PiecesArray[1, 0].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[1, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- PiecesArray[6, 0] := Pieces;
- PiecesArray[6, 0].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[6, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- Name := 'Queen';
- PiecesArray[3, 0] := Pieces;
- PiecesArray[3, 0].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[3, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- Name := 'King';
- PiecesArray[4, 0] := Pieces;
- PiecesArray[4, 0].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[4, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
- Name := 'Pawn';
- FirstPawnMove := True;
- for Count := 0 to 7 do
- begin
- PiecesArray[Count, 1] := Pieces;
- PiecesArray[Count, 1].PNG := TPortableNetworkGraphic.Create;
- PiecesArray[Count, 1].PNG.LoadFromFile('Pieces\' + Colour +
- '\' + Name + '.png');
- end;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement