Advertisement
Guest User

Untitled

a guest
Jan 15th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 11.61 KB | None | 0 0
  1. unit ChessBoard;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids,
  9.   StdCtrls{, pawnswitch};
  10.  
  11. type
  12.  
  13.   { TFormChessBoard }
  14.  
  15.   TFormChessBoard = class(TForm)
  16.     DGridChess: TDrawGrid;
  17.     BtnResign: TButton;
  18.     BtnDraw: TButton;
  19.     BtnSave: TButton;
  20.     BtnQuit: TButton;
  21.     LblTurn: TLabel;
  22.     LblGuidance: TLabel;
  23.     procedure DGridChessBeforeSelection(Sender: TObject; aCol, aRow: integer);
  24.     procedure DGridChessDrawCell(Sender: TObject; aCol, aRow: integer;
  25.       aRect: TRect; aState: TGridDrawState);
  26.     procedure BtnQuitClick(Sender: TObject);
  27.     procedure FormCreate(Sender: TObject);
  28.  
  29.   private
  30.     { private declarations }
  31.   public
  32.     { public declarations }
  33.   end;
  34.  
  35. type
  36.   TPieces = record
  37.     Name: string[6];
  38.     Colour: string[6];
  39.     Player: integer;
  40.     PNG: TPortableNetworkGraphic;
  41.     CanSelect: boolean;
  42.     FirstPawnMove: boolean
  43.   end;
  44.  
  45.   TFile = record
  46.     Player1Colour: string[6];
  47.     Player2Colour: string[6];
  48.     Player1Name: string[15];
  49.     Player2name: string[15];
  50.     Boardsize: integer;
  51.     CurrentTurn: integer;
  52.     SavePiecesArray: array[0..15, 0..15] of TPieces;
  53.   end;
  54.  
  55. var
  56.   FormChessBoard: TFormChessBoard;
  57.   Pieces: Tpieces;
  58.   PiecesArray: array[0..15, 0..15] of TPieces;
  59.   ONMOVE: boolean;
  60.   CurrentTurn, CurrentCol, CurrentRow: integer;
  61.  
  62. implementation
  63.  
  64. {$R *.lfm}
  65.  
  66. { TFormChessBoard }
  67.  
  68. //Get's the piece files and loads them into the grid
  69. procedure PieceStorage(var DGridChess: TDrawGrid);
  70. var
  71.   Count, Count2: integer;
  72. begin
  73.  
  74.   //Draws each piece onto the chess board
  75.   for Count := 0 to 15 do
  76.     for count2 := 0 to 15 do
  77.     begin
  78.       Pieces := PiecesArray[Count, count2];
  79.       if pieces.Name <> '' then
  80.       begin
  81.         try
  82.           DGridChess.canvas.Draw(64 * Count, 64 * count2, pieces.PNG);
  83.         finally
  84.  
  85.         end;
  86.       end;
  87.     end;
  88.  
  89. end;
  90.  
  91.  
  92. //Fills the cells of the chess board
  93. procedure TFormChessBoard.DGridChessDrawCell(Sender: TObject;
  94.   aCol, aRow: integer; aRect: TRect; aState: TGridDrawState);
  95. var
  96.   RowNumber, ColumnNumber, Count: integer;
  97. begin
  98.  
  99.   //  Colours the cells of the grid black/white
  100.   with TDrawGrid(Sender) do
  101.   begin
  102.  
  103.     // Fills everything with white
  104.     DGridChess.Color := clwhite;
  105.  
  106.     // Fills the correct cells with black
  107.     for RowNumber := 0 to 7 do
  108.     begin
  109.       if rownumber mod 2 = 0 then
  110.         ColumnNumber := 1
  111.       else
  112.         columnnumber := 0;
  113.       for Count := 1 to 4 do
  114.       begin
  115.         Canvas.Brush.Color := clblack;
  116.         DGridChess.Canvas.FillRect(TDrawGrid(Sender).CellRect(
  117.           columnnumber, rownumber));
  118.         ColumnNumber := columnnumber + 2;
  119.       end;
  120.     end;
  121.   end;
  122.   PieceStorage(DGridChess);
  123. end;
  124.  
  125. function ToggleTurn(var CurrentTurn: integer): integer;
  126. begin
  127.   //Toggles who's turn it is
  128.   if CurrentTurn = 1 then
  129.   begin
  130.     ToggleTurn := 2;
  131.     FormChessboard.LblTurn.Caption := ('Player 2''s turn');
  132.   end
  133.   else
  134.   begin
  135.     ToggleTurn := 1;
  136.     FormChessboard.LblTurn.Caption := ('Player 1''s turn');
  137.   end;
  138. end;
  139.  
  140. procedure VisuallyMove(ACol, Arow, CurrentCol, CurrentRow: integer);
  141. begin
  142.   //Loads the data of the piece into the new cell
  143.   PiecesArray[ACol, ARow] := PiecesArray[CurrentCol, CurrentRow];
  144.   with PiecesArray[acol, arow] do
  145.   begin
  146.     FirstPawnMove := False;
  147.     PNG := TPortableNetworkGraphic.Create;
  148.     PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  149.   end;
  150.   //Clears the piece's previous cell
  151.   with PiecesArray[CurrentCol, CurrentRow] do
  152.   begin
  153.     Player := 0;
  154.     Colour := '';
  155.     Name := '';
  156.     PNG.Free;
  157.     FirstPawnMove := False;
  158.   end;
  159.   OnMove := False;
  160. end;
  161.  
  162. //Pawn Piece Logic
  163. procedure PawnCheck(ACol, ARow: integer);
  164. var
  165.   PieceDirection, FirstMoveDirection, CurrentPlayer, EndofBoard, OtherPlayer: integer;
  166. begin
  167.  
  168.   if PiecesArray[Acol, Arow].Player = 1 then
  169.   begin
  170.     PieceDirection := -1;
  171.     FirstMoveDirection := -2;
  172.     CurrentPlayer := 1;
  173.     OtherPlayer := 2;
  174.     EndofBoard := 0;
  175.   end
  176.   else
  177.   begin
  178.     PieceDirection := 1;
  179.     FirstMoveDirection := 2;
  180.     CurrentPlayer := 2;
  181.     OtherPlayer := 1;
  182.     EndofBoard := 7;
  183.   end;
  184.  
  185.   if PiecesArray[Acol, Arow + PieceDirection].Player = 0 then
  186.     PiecesArray[Acol, Arow + PieceDirection].CanSelect := True;
  187.  
  188.   if PiecesArray[Acol + 1, Arow + PieceDirection].Player = OtherPlayer then
  189.     PiecesArray[Acol + 1, Arow + PieceDirection].CanSelect := True;
  190.  
  191.   if PiecesArray[Acol - 1, Arow + PieceDirection].Player = OtherPlayer then
  192.     PiecesArray[Acol - 1, Arow + PieceDirection].CanSelect := True;
  193.  
  194.   if (PiecesArray[Acol, Arow].FirstPawnMove = True) and
  195.     (PiecesArray[Acol, Arow + FirstMoveDirection].Player <> CurrentPlayer) then
  196.     PiecesArray[Acol, Arow + FirstMoveDirection].CanSelect := True;
  197.  
  198. end;
  199.  
  200. //Knight Piece Logic
  201. procedure KnightCheck(ACol, ARow: integer);
  202. begin
  203.  
  204. end;
  205. //Rook Piece Logic
  206. procedure RookCheck(ACol, ARow: integer);
  207. begin
  208.  
  209. end;
  210. //King Piece Logic
  211. procedure KingCheck(ACol, ARow: integer);
  212. begin
  213.  
  214. end;
  215. //Queen Piece Logic
  216. procedure QueenCheck(ACol, ARow: integer);
  217. begin
  218.  
  219. end;
  220. //Bishop Piece Logic
  221. procedure BishopCheck(ACol, ARow: integer);
  222. begin
  223.  
  224. end;
  225.  
  226. //Checks where the selected piece can move to
  227. procedure CheckingPiece(ACol, ARow: integer);
  228. var
  229.   Count, count2: integer;
  230. begin
  231.   //resets the cells where the piece can move to
  232.   for Count := 1 to 15 do
  233.     for count2 := 1 to 15 do
  234.       PiecesArray[Count, count2].CanSelect := False;
  235.  
  236.   //Does the corresponding check on where the piece
  237.   //can move to dependng on what the piece is
  238.   if PiecesArray[ACol, ARow].Name = 'Pawn' then
  239.     PawnCheck(ACol, ARow)
  240.   else if PiecesArray[ACol, ARow].Name = 'Knight' then
  241.     KnightCheck(ACol, ARow)
  242.   else if PiecesArray[ACol, ARow].Name = 'Rook' then
  243.     RookCheck(ACol, ARow)
  244.   else if PiecesArray[ACol, ARow].Name = 'King' then
  245.     KingCheck(ACol, ARow)
  246.   else if PiecesArray[ACol, ARow].Name = 'Queen' then
  247.     QueenCheck(ACol, ARow)
  248.   else if PiecesArray[ACol, ARow].Name = 'Bishop' then
  249.     BishopCheck(ACol, ARow);
  250.  
  251. end;
  252.  
  253. procedure TFormChessBoard.DGridChessBeforeSelection(Sender: TObject;
  254.   aCol, aRow: integer);
  255. begin
  256.  
  257.   //Checks if the player clicks no piece as the piece to move, do nothing
  258.   if (PiecesArray[ACol, ARow].Name = '') and (OnMove = False) then
  259.   begin
  260.   end
  261.   else
  262.   //If selected piece matches who's turn it is, then check where the piece can move to
  263.   if (PiecesArray[acol, arow].Player = CurrentTurn) then
  264.   begin
  265.  
  266.     //Gets the cell coordinates of the piece which needs moving
  267.     CurrentCol := Acol;
  268.     CurrentRow := Arow;
  269.  
  270.     CheckingPiece(Acol, Arow);
  271.     OnMove := True;
  272.   end
  273.   else
  274.   if ((Acol = CurrentCol) and (Arow = CurrentRow)) then
  275.     OnMove := False
  276.   else
  277.  
  278.   //Checks if the player is able to move the piece there
  279.   if PiecesArray[ACol, ARow].CanSelect = True then
  280.   begin
  281.     CurrentTurn := ToggleTurn(CurrentTurn);
  282.     VisuallyMove(Acol, Arow, CurrentCol, CurrentRow);
  283.     //Moves the piece to the new cell
  284.   end;
  285.  
  286. end;
  287.  
  288.  
  289.  
  290.  
  291. //Closes form when the quit button is selected
  292. procedure TFormChessBoard.BtnQuitClick(Sender: TObject);
  293. begin
  294.   Close;
  295. end;
  296.  
  297.  
  298. //IN DEVELOPMENT-Hardcode piece initialisation (will be called from file later in development)
  299. procedure TFormChessBoard.FormCreate(Sender: TObject);
  300.  
  301. var
  302.   Count, Count2: integer;
  303. begin
  304.  
  305.   with Pieces do
  306.   begin
  307.     //Initialisation of the variables
  308.     OnMove := False;
  309.     CurrentTurn := 1;
  310.     CurrentCol := -1;
  311.     CurrentRow := -1;
  312.     LblTurn.Caption := ('Player 1''s turn');
  313.     FirstPawnMove := False;
  314.     Name := '';
  315.     Colour := '';
  316.     Player := 0;
  317.     CanSelect := False;
  318.     for Count := 1 to 15 do
  319.       for Count2 := 1 to 15 do
  320.       begin
  321.         PiecesArray[Count, Count2] := Pieces;
  322.       end;
  323.  
  324.     //TEMPORARY- Hard coded piece initialisation
  325.     Player := 1;
  326.     Colour := 'White';
  327.  
  328.     Name := 'Bishop';
  329.     PiecesArray[2, 7] := Pieces;
  330.     PiecesArray[2, 7].PNG := TPortableNetworkGraphic.Create;
  331.     PiecesArray[2, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  332.     PiecesArray[5, 7] := Pieces;
  333.     PiecesArray[5, 7].PNG := TPortableNetworkGraphic.Create;
  334.     PiecesArray[5, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  335.  
  336.     Name := 'Rook';
  337.     PiecesArray[0, 7] := Pieces;
  338.     PiecesArray[0, 7].PNG := TPortableNetworkGraphic.Create;
  339.     PiecesArray[0, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  340.     PiecesArray[7, 7] := Pieces;
  341.     PiecesArray[7, 7].PNG := TPortableNetworkGraphic.Create;
  342.     PiecesArray[7, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  343.  
  344.     Name := 'Knight';
  345.     PiecesArray[6, 7] := Pieces;
  346.     PiecesArray[6, 7].PNG := TPortableNetworkGraphic.Create;
  347.     PiecesArray[6, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  348.     PiecesArray[1, 7] := Pieces;
  349.     PiecesArray[1, 7].PNG := TPortableNetworkGraphic.Create;
  350.     PiecesArray[1, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  351.  
  352.     Name := 'Queen';
  353.     PiecesArray[3, 7] := Pieces;
  354.     PiecesArray[3, 7].PNG := TPortableNetworkGraphic.Create;
  355.     PiecesArray[3, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  356.  
  357.     Name := 'King';
  358.     PiecesArray[4, 7] := Pieces;
  359.     PiecesArray[4, 7].PNG := TPortableNetworkGraphic.Create;
  360.     PiecesArray[4, 7].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  361.  
  362.     Name := 'Pawn';
  363.     FirstPawnMove := True;
  364.     for Count := 0 to 7 do
  365.     begin
  366.       PiecesArray[Count, 6] := Pieces;
  367.       PiecesArray[Count, 6].PNG := TPortableNetworkGraphic.Create;
  368.       PiecesArray[Count, 6].PNG.LoadFromFile('Pieces\' + Colour +
  369.         '\' + Name + '.png');
  370.     end;
  371.  
  372.     Player := 2;
  373.     Colour := 'Black';
  374.     FirstPawnMove := False;
  375.  
  376.     Name := 'Bishop';
  377.     PiecesArray[2, 0] := Pieces;
  378.     PiecesArray[2, 0].PNG := TPortableNetworkGraphic.Create;
  379.     PiecesArray[2, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  380.     PiecesArray[5, 0] := Pieces;
  381.     PiecesArray[5, 0].PNG := TPortableNetworkGraphic.Create;
  382.     PiecesArray[5, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  383.  
  384.     Name := 'Rook';
  385.     PiecesArray[0, 0] := Pieces;
  386.     PiecesArray[0, 0].PNG := TPortableNetworkGraphic.Create;
  387.     PiecesArray[0, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  388.     PiecesArray[7, 0] := Pieces;
  389.     PiecesArray[7, 0].PNG := TPortableNetworkGraphic.Create;
  390.     PiecesArray[7, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  391.  
  392.     Name := 'Knight';
  393.     PiecesArray[1, 0] := Pieces;
  394.     PiecesArray[1, 0].PNG := TPortableNetworkGraphic.Create;
  395.     PiecesArray[1, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  396.     PiecesArray[6, 0] := Pieces;
  397.     PiecesArray[6, 0].PNG := TPortableNetworkGraphic.Create;
  398.     PiecesArray[6, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  399.  
  400.     Name := 'Queen';
  401.     PiecesArray[3, 0] := Pieces;
  402.     PiecesArray[3, 0].PNG := TPortableNetworkGraphic.Create;
  403.     PiecesArray[3, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  404.  
  405.     Name := 'King';
  406.     PiecesArray[4, 0] := Pieces;
  407.     PiecesArray[4, 0].PNG := TPortableNetworkGraphic.Create;
  408.     PiecesArray[4, 0].PNG.LoadFromFile('Pieces\' + Colour + '\' + Name + '.png');
  409.  
  410.     Name := 'Pawn';
  411.     FirstPawnMove := True;
  412.     for Count := 0 to 7 do
  413.     begin
  414.       PiecesArray[Count, 1] := Pieces;
  415.       PiecesArray[Count, 1].PNG := TPortableNetworkGraphic.Create;
  416.       PiecesArray[Count, 1].PNG.LoadFromFile('Pieces\' + Colour +
  417.         '\' + Name + '.png');
  418.     end;
  419.   end;
  420. end;
  421.  
  422. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement