summaryrefslogtreecommitdiff
path: root/ChessBoard.cs
diff options
context:
space:
mode:
authorRamaekers,Aldrik A.N <a.ramaekers@student.fontys.nl>2020-09-03 14:40:33 +0200
committerRamaekers,Aldrik A.N <a.ramaekers@student.fontys.nl>2020-09-03 14:40:33 +0200
commit5a045404f3c49022abeb75c27dfe6f82d35928f7 (patch)
treea1dd8aa4091e9bf8d4377dc6d6f26cc9ca07ce47 /ChessBoard.cs
parent4811afb52c511565d2b13f36ed645243c7557803 (diff)
board setup and chess piece movement checks
Diffstat (limited to 'ChessBoard.cs')
-rw-r--r--ChessBoard.cs121
1 files changed, 94 insertions, 27 deletions
diff --git a/ChessBoard.cs b/ChessBoard.cs
index 81939bc..487ec29 100644
--- a/ChessBoard.cs
+++ b/ChessBoard.cs
@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
+using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -16,9 +17,13 @@ namespace Chess
private float tileWidth;
private float tileHeight;
- private BoardTile[,] tiles;
+ internal BoardTile[,] tiles;
private PictureBox container;
+ private BoardTile selectedTile = null;
+
+ private bool screenInvalidated = true;
+
public ChessBoard(PictureBox container)
{
this.container = container;
@@ -36,11 +41,39 @@ namespace Chess
public void GeneratePieces()
{
+ tiles[0, 0].OccupyingPiece = new Rook(false);
+ tiles[0, 1].OccupyingPiece = new Knight(false);
+ tiles[0, 2].OccupyingPiece = new Bishop(false);
+ tiles[4,4].OccupyingPiece = new Queen(false);
+ tiles[0, 4].OccupyingPiece = new King(false);
+ tiles[0, 5].OccupyingPiece = new Bishop(false);
+ tiles[0, 6].OccupyingPiece = new Knight(false);
+ tiles[0, 7].OccupyingPiece = new Rook(false);
+
for (int x = 0; x < boardSize; x++)
- tiles[1, x].OccupyingPiece = new Pawn();
+ tiles[1, x].OccupyingPiece = new Pawn(false);
for (int x = 0; x < boardSize; x++)
- tiles[6, x].OccupyingPiece = new Pawn();
+ tiles[6, x].OccupyingPiece = new Pawn(true);
+
+ tiles[7, 0].OccupyingPiece = new Rook(true);
+ tiles[7, 1].OccupyingPiece = new Knight(true);
+ tiles[7, 2].OccupyingPiece = new Bishop(true);
+ tiles[4,5].OccupyingPiece = new Queen(true);
+ tiles[3, 3].OccupyingPiece = new King(true);
+ tiles[7, 5].OccupyingPiece = new Bishop(true);
+ tiles[7, 6].OccupyingPiece = new Knight(true);
+ tiles[7, 7].OccupyingPiece = new Rook(true);
+ }
+
+ public ChessPiece PieceAt(int x, int y)
+ {
+ if (x >= 0 && y >= 0 && x < boardSize && y < boardSize)
+ {
+ return tiles[y, x].OccupyingPiece;
+ }
+
+ return null;
}
public void GenerateBoard()
@@ -51,46 +84,80 @@ namespace Chess
{
for (int x = 0; x < boardSize; x++)
{
- BoardTileColor color = BoardTileColor.White;
-
- if (y % 2 == 0)
- {
- if (x % 2 != 0) color = BoardTileColor.Black;
- }
- else
- {
- if (x % 2 == 0) color = BoardTileColor.Black;
- }
-
- tiles[y, x] = new BoardTile(x, y, color);
+ tiles[y, x] = new BoardTile(x, y);
+ }
+ }
+ }
+
+ public void DrawTile(Point point)
+ {
+ if (point.X >= 0 && point.X < boardSize && point.Y >= 0 && point.Y < boardSize)
+ {
+ var tile = tiles[point.Y, point.X];
+
+ using (Graphics g = (screenInvalidated ? Graphics.FromImage(container.Image) : container.CreateGraphics()))
+ {
+ tile.Draw(g, tileWidth, tileHeight, (selectedTile != null && selectedTile.CanMoveTo(this, tile)));
}
}
}
public void DrawGame()
{
- Bitmap temp = new Bitmap(container.Size.Width, container.Size.Height);
+ screenInvalidated = true;
+
+ if (container.Image != null)
+ container.Image.Dispose();
+
+ container.Image = new Bitmap(container.Size.Width, container.Size.Height);
tileWidth = container.Size.Width / (float)boardSize;
tileHeight = container.Size.Height / (float)boardSize;
- using (Graphics g = Graphics.FromImage(temp))
+ for (int y = 0; y < boardSize; y++)
{
- for (int y = 0; y < boardSize; y++)
+ for (int x = 0; x < boardSize; x++)
{
- for (int x = 0; x < boardSize; x++)
- {
- BoardTile tile = tiles[y, x];
-
- tile.Draw(g, tileWidth, tileHeight);
- }
+ DrawTile(new Point(x, y));
}
}
- if (container.Image != null)
- container.Image.Dispose();
+ screenInvalidated = false;
+ }
+
+ public Point MouseToTilePosition(int x, int y)
+ {
+ return new Point((int)(x / tileWidth), (int)(y / tileHeight));
+ }
+
+ private void MoveSelectedPieceTo(BoardTile tile)
+ {
+ if (selectedTile.CanMoveTo(this, tile))
+ {
+ tile.OccupyingPiece = selectedTile.OccupyingPiece;
+ selectedTile.OccupyingPiece = null;
+ }
+ }
+
+ public void SelectTile(Point point)
+ {
+ var clickedTile = tiles[point.Y, point.X];
- container.Image = temp;
+ if (selectedTile != clickedTile)
+ {
+ if (selectedTile != null)
+ {
+ MoveSelectedPieceTo(clickedTile);
+ }
+
+ selectedTile = clickedTile;
+ }
+ else
+ {
+ selectedTile = null;
+ }
+
+ DrawGame();
}
}
}