From 5a045404f3c49022abeb75c27dfe6f82d35928f7 Mon Sep 17 00:00:00 2001 From: "Ramaekers,Aldrik A.N" Date: Thu, 3 Sep 2020 14:40:33 +0200 Subject: board setup and chess piece movement checks --- .vs/Chess/v16/.suo | Bin 71168 -> 83968 bytes BoardTile.cs | 38 ++++- Chess.csproj | 12 ++ ChessBoard.cs | 121 ++++++++++++---- ChessPiece.cs | 157 ++++++++++++++++++--- ImageHelper.cs | 39 +++++ MainForm.Designer.cs | 1 + MainForm.cs | 5 + Pieces/Bishop.cs | 42 ++++++ Pieces/King.cs | 58 ++++++++ Pieces/Knight.cs | 46 ++++++ Pieces/Pawn.cs | 34 +++-- Pieces/Queen.cs | 59 ++++++++ Pieces/Rook.cs | 40 ++++++ Properties/Resources.Designer.cs | 20 +++ Properties/Resources.resx | 6 + Resources/square brown light_png_shadow_256px.png | Bin 0 -> 879 bytes Resources/square gray light _png_shadow_256px.png | Bin 0 -> 876 bytes bin/Debug/Chess.exe | Bin 227328 -> 236544 bytes bin/Debug/Chess.pdb | Bin 56832 -> 89600 bytes obj/Debug/Chess.Properties.Resources.resources | Bin 214315 -> 216549 bytes obj/Debug/Chess.csproj.CoreCompileInputs.cache | 2 +- obj/Debug/Chess.csproj.GenerateResource.cache | Bin 1709 -> 1847 bytes obj/Debug/Chess.csprojAssemblyReference.cache | Bin 424 -> 424 bytes obj/Debug/Chess.exe | Bin 227328 -> 236544 bytes obj/Debug/Chess.pdb | Bin 56832 -> 89600 bytes .../DesignTimeResolveAssemblyReferencesInput.cache | Bin 9376 -> 9718 bytes .../TempPE/Properties.Resources.Designer.cs.dll | Bin 5632 -> 5632 bytes 28 files changed, 618 insertions(+), 62 deletions(-) create mode 100644 ImageHelper.cs create mode 100644 Pieces/Bishop.cs create mode 100644 Pieces/King.cs create mode 100644 Pieces/Knight.cs create mode 100644 Pieces/Queen.cs create mode 100644 Pieces/Rook.cs create mode 100644 Resources/square brown light_png_shadow_256px.png create mode 100644 Resources/square gray light _png_shadow_256px.png diff --git a/.vs/Chess/v16/.suo b/.vs/Chess/v16/.suo index 63c9c46..dc29f3f 100644 Binary files a/.vs/Chess/v16/.suo and b/.vs/Chess/v16/.suo differ diff --git a/BoardTile.cs b/BoardTile.cs index 1bccbec..e58cbc6 100644 --- a/BoardTile.cs +++ b/BoardTile.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Drawing; using System.Linq; using System.Text; @@ -19,20 +20,47 @@ namespace Chess public int Y; public BoardTileColor Color; public ChessPiece OccupyingPiece; - https://git.fhict.nl/I436104/chess - public BoardTile(int x, int y, BoardTileColor color) + + public BoardTile(int x, int y) { X = x; Y = y; - Color = color; + + Color = BoardTileColor.White; + if (y % 2 == 0) + { + if (x % 2 != 0) Color = BoardTileColor.Black; + } + else + { + if (x % 2 == 0) Color = BoardTileColor.Black; + } + OccupyingPiece = null; } - internal void Draw(Graphics graphics, float tileWidth, float tileHeight) + public bool CanMoveTo(ChessBoard board, BoardTile tile) { - graphics.FillRectangle(new SolidBrush(this.Color == BoardTileColor.White ? System.Drawing.Color.White : System.Drawing.Color.Black), new RectangleF(X * tileWidth, Y * tileHeight, tileWidth, tileHeight)); + if (OccupyingPiece != null) return OccupyingPiece.CanMoveTo(board, this, tile); + + return false; + } + + internal void Draw(Graphics graphics, float tileWidth, float tileHeight, bool active) + { + graphics.FillRectangle(new SolidBrush(this.Color == BoardTileColor.White ? System.Drawing.Color.FromArgb(235, 236, 208) : System.Drawing.Color.FromArgb(137, 163, 105)), + new RectangleF(X * tileWidth, Y * tileHeight, tileWidth, tileHeight)); OccupyingPiece?.Draw(graphics, X, Y, tileWidth, tileHeight); + + if (active) + { + float marginw = tileWidth / 3; + float marginh = tileHeight / 3; + int padding = 3; + graphics.FillEllipse(new SolidBrush(System.Drawing.Color.FromArgb(150, 50, 50, 50)), X * tileWidth + marginw, Y * tileHeight + marginh, tileWidth - marginw * 2, tileHeight - marginh * 2); + graphics.FillEllipse(new SolidBrush(System.Drawing.Color.FromArgb(150, 255, 200, 200)), X * tileWidth + marginw + padding, Y * tileHeight + marginh + padding, tileWidth - (marginw + padding) * 2, tileHeight - (marginh + padding) * 2); + } } } } diff --git a/Chess.csproj b/Chess.csproj index 1dc8e7e..86ed7b9 100644 --- a/Chess.csproj +++ b/Chess.csproj @@ -49,13 +49,19 @@ + Form MainForm.cs + + + + + @@ -120,5 +126,11 @@ + + + + + + \ No newline at end of file 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(); } } } diff --git a/ChessPiece.cs b/ChessPiece.cs index 68bc1cf..5a27177 100644 --- a/ChessPiece.cs +++ b/ChessPiece.cs @@ -9,6 +9,13 @@ using System.Drawing.Imaging; namespace Chess { + internal enum MoveCheckResult + { + CanMove, + CantMove, + ContinueCheck, + } + public abstract class ChessPiece { internal Bitmap PieceImage; @@ -19,32 +26,150 @@ namespace Chess IsWhite = isWhite; } - public abstract bool MoveTo(); - public abstract void Draw(Graphics graphics, float x, float y, float tileWidth, float tileHeight); + internal MoveCheckResult CanMoveUpLeft(ChessBoard board, BoardTile currentTile, BoardTile destinationTile, int count = 8) + { + for (int t = 1; t <= count; t++) + { + Point p1 = new Point(currentTile.X - t, currentTile.Y - t); + + var enemy = board.PieceAt(p1.X, p1.Y); + if ((p1.X == destinationTile.X && p1.Y == destinationTile.Y) && (enemy == null)) return MoveCheckResult.CanMove; + if ((p1.X == destinationTile.X && p1.Y == destinationTile.Y) && (enemy != null && enemy.IsWhite != this.IsWhite)) return MoveCheckResult.CanMove; + if (enemy != null && enemy != this) return MoveCheckResult.ContinueCheck; + } + + return MoveCheckResult.ContinueCheck; + } + + internal MoveCheckResult CanMoveUpRight(ChessBoard board, BoardTile currentTile, BoardTile destinationTile, int count = 8) + { + for (int t = 1; t <= count; t++) + { + Point p1 = new Point(currentTile.X + t, currentTile.Y - t); + + var enemy = board.PieceAt(p1.X, p1.Y); + if ((p1.X == destinationTile.X && p1.Y == destinationTile.Y) && (enemy == null)) return MoveCheckResult.CanMove; + if ((p1.X == destinationTile.X && p1.Y == destinationTile.Y) && (enemy != null && enemy.IsWhite != this.IsWhite)) return MoveCheckResult.CanMove; + if (enemy != null && enemy != this) return MoveCheckResult.ContinueCheck; + } + + return MoveCheckResult.ContinueCheck; + } + - public Bitmap ResizeImage(Image image, int width, int height) + internal MoveCheckResult CanMoveDownRight(ChessBoard board, BoardTile currentTile, BoardTile destinationTile, int count = 8) { - var destRect = new Rectangle(0, 0, width, height); - var destImage = new Bitmap(width, height); + for (int t = 1; t <= count; t++) + { + Point p1 = new Point(currentTile.X + t, currentTile.Y + t); + + var enemy = board.PieceAt(p1.X, p1.Y); + if ((p1.X == destinationTile.X && p1.Y == destinationTile.Y) && (enemy == null)) return MoveCheckResult.CanMove; + if ((p1.X == destinationTile.X && p1.Y == destinationTile.Y) && (enemy != null && enemy.IsWhite != this.IsWhite)) return MoveCheckResult.CanMove; + if (enemy != null && enemy != this) return MoveCheckResult.ContinueCheck; + } - destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); + return MoveCheckResult.ContinueCheck; + } - using (var graphics = Graphics.FromImage(destImage)) + internal MoveCheckResult CanMoveDownLeft(ChessBoard board, BoardTile currentTile, BoardTile destinationTile, int count = 8) + { + for (int t = 1; t <= count; t++) { - graphics.CompositingMode = CompositingMode.SourceCopy; - graphics.CompositingQuality = CompositingQuality.HighQuality; - graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; - graphics.SmoothingMode = SmoothingMode.HighQuality; - graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; + Point p1 = new Point(currentTile.X - t, currentTile.Y + t); + + var enemy = board.PieceAt(p1.X, p1.Y); + if ((p1.X == destinationTile.X && p1.Y == destinationTile.Y) && (enemy == null)) return MoveCheckResult.CanMove; + if ((p1.X == destinationTile.X && p1.Y == destinationTile.Y) && (enemy != null && enemy.IsWhite != this.IsWhite)) return MoveCheckResult.CanMove; + if (enemy != null && enemy != this) return MoveCheckResult.ContinueCheck; + } - using (var wrapMode = new ImageAttributes()) + return MoveCheckResult.ContinueCheck; + } + + internal MoveCheckResult CanMoveLeft(ChessBoard board, BoardTile currentTile, BoardTile destinationTile, int count = 8) + { + if (currentTile.Y != destinationTile.Y) return MoveCheckResult.ContinueCheck; + + if (destinationTile.X < currentTile.X) + { + for (int t = 1; t <= count; t++) { - wrapMode.SetWrapMode(WrapMode.TileFlipXY); - graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); + var enemy = board.PieceAt(currentTile.X - t, currentTile.Y); + if (currentTile.X - t == destinationTile.X && (enemy == null)) return MoveCheckResult.CanMove; + if (currentTile.X - t == destinationTile.X && (enemy != null && enemy.IsWhite != this.IsWhite)) return MoveCheckResult.CanMove; + if (enemy != null && enemy != this) return MoveCheckResult.ContinueCheck; } } - return destImage; + return MoveCheckResult.ContinueCheck; + } + + internal MoveCheckResult CanMoveRight(ChessBoard board, BoardTile currentTile, BoardTile destinationTile, int count = 8) + { + if (currentTile.Y != destinationTile.Y) return MoveCheckResult.ContinueCheck; + + if (destinationTile.X > currentTile.X) + { + for (int t = 1; t <= count; t++) + { + var enemy = board.PieceAt(currentTile.X + t, currentTile.Y); + if (currentTile.X + t == destinationTile.X && (enemy == null)) return MoveCheckResult.CanMove; + if (currentTile.X + t == destinationTile.X && (enemy != null && enemy.IsWhite != this.IsWhite)) return MoveCheckResult.CanMove; + if (enemy != null && enemy != this) return MoveCheckResult.ContinueCheck; + } + } + + return MoveCheckResult.ContinueCheck; + } + + internal MoveCheckResult CanMoveDown(ChessBoard board, BoardTile currentTile, BoardTile destinationTile, int count = 8) + { + if (currentTile.X != destinationTile.X) return MoveCheckResult.ContinueCheck; + + if (destinationTile.Y > currentTile.Y) + { + for (int t = 1; t <= count; t++) + { + var enemy = board.PieceAt(currentTile.X, currentTile.Y + t); + if (currentTile.Y + t == destinationTile.Y && (enemy == null)) return MoveCheckResult.CanMove; + if (currentTile.Y + t == destinationTile.Y && (enemy != null && enemy.IsWhite != this.IsWhite)) return MoveCheckResult.CanMove; + if (enemy != null && enemy != this) return MoveCheckResult.ContinueCheck; + } + } + + return MoveCheckResult.ContinueCheck; + } + + internal MoveCheckResult CanMoveUp(ChessBoard board, BoardTile currentTile, BoardTile destinationTile, int count = 8) + { + if (currentTile.X != destinationTile.X) return MoveCheckResult.ContinueCheck; + + if (destinationTile.Y < currentTile.Y) + { + for (int t = 1; t <= count; t++) + { + var enemy = board.PieceAt(currentTile.X, currentTile.Y - t); + if (currentTile.Y - t == destinationTile.Y && (enemy == null)) return MoveCheckResult.CanMove; + if (currentTile.Y - t == destinationTile.Y && (enemy != null && enemy.IsWhite != this.IsWhite)) return MoveCheckResult.CanMove; + if (enemy != null && enemy != this) return MoveCheckResult.ContinueCheck; + } + } + + return MoveCheckResult.ContinueCheck; + } + + public abstract bool CanMoveTo(ChessBoard board, BoardTile currentTile, BoardTile destinationTile); + public void Draw(Graphics graphics, float x, float y, float tileWidth, float tileHeight) + { + if (PieceImage != null) + { + var image = ImageHelper.ResizeImage(PieceImage, (int)tileWidth, (int)tileHeight); + + graphics.DrawImage(image, new PointF(x * tileWidth + (tileWidth/ 40), y * tileHeight + (tileWidth / 40))); + + image.Dispose(); + } } } } diff --git a/ImageHelper.cs b/ImageHelper.cs new file mode 100644 index 0000000..3ad91a6 --- /dev/null +++ b/ImageHelper.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Drawing.Imaging; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Chess +{ + public class ImageHelper + { + public static Bitmap ResizeImage(Image image, int width, int height) + { + var destRect = new Rectangle(0, 0, width, height); + var destImage = new Bitmap(width, height); + + destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); + + using (var graphics = Graphics.FromImage(destImage)) + { + graphics.CompositingMode = CompositingMode.SourceCopy; + graphics.CompositingQuality = CompositingQuality.HighQuality; + graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; + graphics.SmoothingMode = SmoothingMode.HighQuality; + graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; + + using (var wrapMode = new ImageAttributes()) + { + wrapMode.SetWrapMode(WrapMode.TileFlipXY); + graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); + } + } + + return destImage; + } + } +} diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index ec582bc..f8268fc 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -39,6 +39,7 @@ this.chessBoardBitmap.Size = new System.Drawing.Size(173, 146); this.chessBoardBitmap.TabIndex = 0; this.chessBoardBitmap.TabStop = false; + this.chessBoardBitmap.MouseClick += new System.Windows.Forms.MouseEventHandler(this.chessBoardBitmap_MouseClick); // // MainForm // diff --git a/MainForm.cs b/MainForm.cs index 08e2803..724f259 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -36,5 +36,10 @@ namespace Chess FitBoardContainerToScreen(); this.board.HandleResize(); } + + private void chessBoardBitmap_MouseClick(object sender, MouseEventArgs e) + { + this.board.SelectTile(this.board.MouseToTilePosition(e.X, e.Y)); + } } } diff --git a/Pieces/Bishop.cs b/Pieces/Bishop.cs new file mode 100644 index 0000000..10d1537 --- /dev/null +++ b/Pieces/Bishop.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Chess.Pieces +{ + class Bishop : ChessPiece + { + public Bishop(bool isWhite) : base(isWhite) + { + PieceImage = new Bitmap(!isWhite ? Chess.Properties.Resources.b_bishop_png_shadow_256px : Chess.Properties.Resources.w_bishop_png_shadow_256px); + } + + public override bool CanMoveTo(ChessBoard board, BoardTile currentTile, BoardTile destinationTile) + { + MoveCheckResult result; + + result = CanMoveDownLeft(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveDownRight(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveUpLeft(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveUpRight(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + return false; + } + + + } +} diff --git a/Pieces/King.cs b/Pieces/King.cs new file mode 100644 index 0000000..945c476 --- /dev/null +++ b/Pieces/King.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Chess.Pieces +{ + class King : ChessPiece + { + public King(bool isWhite) : base(isWhite) + { + PieceImage = new Bitmap(!isWhite ? Chess.Properties.Resources.b_king_png_shadow_256px : Chess.Properties.Resources.w_king_png_shadow_256px); + } + + public override bool CanMoveTo(ChessBoard board, BoardTile currentTile, BoardTile destinationTile) + { + MoveCheckResult result; + + result = CanMoveDown(board, currentTile, destinationTile, 1); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveUp(board, currentTile, destinationTile, 1); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveLeft(board, currentTile, destinationTile, 1); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveRight(board, currentTile, destinationTile, 1); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + // diagonal + + result = CanMoveDownLeft(board, currentTile, destinationTile, 1); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveDownRight(board, currentTile, destinationTile, 1); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveUpLeft(board, currentTile, destinationTile, 1); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveUpRight(board, currentTile, destinationTile, 1); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + return false; + } + } +} diff --git a/Pieces/Knight.cs b/Pieces/Knight.cs new file mode 100644 index 0000000..5a0aea7 --- /dev/null +++ b/Pieces/Knight.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Chess.Pieces +{ + class Knight : ChessPiece + { + public Knight(bool isWhite) : base(isWhite) + { + PieceImage = new Bitmap(!isWhite ? Chess.Properties.Resources.b_knight_png_shadow_256px : Chess.Properties.Resources.w_knight_png_shadow_256px); + } + + public override bool CanMoveTo(ChessBoard board, BoardTile currentTile, BoardTile destinationTile) + { + Point[] pointsToCheck = { + new Point(currentTile.X - 2, currentTile.Y - 1), + new Point(currentTile.X - 2, currentTile.Y + 1), + + new Point(currentTile.X + 2, currentTile.Y - 1), + new Point(currentTile.X + 2, currentTile.Y + 1), + + new Point(currentTile.X + 1, currentTile.Y - 2), + new Point(currentTile.X - 1, currentTile.Y - 2), + + new Point(currentTile.X + 1, currentTile.Y + 2), + new Point(currentTile.X - 1, currentTile.Y + 2), + }; + + foreach(var point in pointsToCheck) + { + if (!(destinationTile.Y == point.Y && destinationTile.X == point.X)) continue; + + var enemy = board.PieceAt(point.X, point.Y); + if (enemy == null) return true; + if (enemy != null && enemy.IsWhite != this.IsWhite) return true; + if (enemy != null && enemy != this) continue; + } + + return false; + } + } +} diff --git a/Pieces/Pawn.cs b/Pieces/Pawn.cs index d518b8e..93be545 100644 --- a/Pieces/Pawn.cs +++ b/Pieces/Pawn.cs @@ -9,26 +9,34 @@ namespace Chess.Pieces { class Pawn : ChessPiece { - public Pawn() + private bool firstMove = true; + + public Pawn(bool isWhite) : base(isWhite) { - PieceImage = new Bitmap(Chess.Properties.Resources.b_pawn_png_shadow_256px); + PieceImage = new Bitmap(!isWhite ? Chess.Properties.Resources.b_pawn_png_shadow_256px : Chess.Properties.Resources.w_pawn_png_shadow_256px); } - public override void Draw(Graphics graphics, float x, float y, float tileWidth, float tileHeight) + public override bool CanMoveTo(ChessBoard board, BoardTile currentTile, BoardTile destinationTile) { - if (PieceImage != null) - { - var image = this.ResizeImage(PieceImage, (int)tileWidth, (int)tileHeight); + MoveCheckResult result; - graphics.DrawImage(image, new PointF(x*tileWidth, y*tileHeight)); + int space = 2; + if (!firstMove) space = 1; - image.Dispose(); + if (!IsWhite) + { + result = CanMoveDown(board, currentTile, destinationTile, space); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + } + else + { + result = CanMoveUp(board, currentTile, destinationTile, space); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; } - } - public override bool MoveTo() - { - throw new NotImplementedException(); - } + return false; + } } } diff --git a/Pieces/Queen.cs b/Pieces/Queen.cs new file mode 100644 index 0000000..42998c4 --- /dev/null +++ b/Pieces/Queen.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using System.Text; +using System.Threading.Tasks; + +namespace Chess.Pieces +{ + class Queen : ChessPiece + { + public Queen(bool isWhite) : base(isWhite) + { + PieceImage = new Bitmap(!isWhite ? Chess.Properties.Resources.b_queen_png_shadow_256px : Chess.Properties.Resources.w_queen_png_shadow_256px); + } + + public override bool CanMoveTo(ChessBoard board, BoardTile currentTile, BoardTile destinationTile) + { + MoveCheckResult result; + + result = CanMoveDown(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveUp(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveLeft(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveRight(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + // diagonal + + result = CanMoveDownLeft(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveDownRight(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveUpLeft(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveUpRight(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + return false; + } + } +} diff --git a/Pieces/Rook.cs b/Pieces/Rook.cs new file mode 100644 index 0000000..29c17c0 --- /dev/null +++ b/Pieces/Rook.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Chess.Pieces +{ + class Rook : ChessPiece + { + public Rook(bool isWhite) : base(isWhite) + { + PieceImage = new Bitmap(!isWhite ? Chess.Properties.Resources.b_rook_png_shadow_256px : Chess.Properties.Resources.w_rook_png_shadow_256px); + } + + public override bool CanMoveTo(ChessBoard board, BoardTile currentTile, BoardTile destinationTile) + { + MoveCheckResult result; + + result = CanMoveDown(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveUp(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveLeft(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + result = CanMoveRight(board, currentTile, destinationTile); + if (result == MoveCheckResult.CanMove) return true; + if (result == MoveCheckResult.CantMove) return false; + + return false; + } + } +} diff --git a/Properties/Resources.Designer.cs b/Properties/Resources.Designer.cs index 65342f3..eb7ced0 100644 --- a/Properties/Resources.Designer.cs +++ b/Properties/Resources.Designer.cs @@ -120,6 +120,26 @@ namespace Chess.Properties { } } + /// + /// Looks up a localized resource of type System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap square_brown_light_png_shadow_256px { + get { + object obj = ResourceManager.GetObject("square_brown_light_png_shadow_256px", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + + /// + /// Looks up a localized resource of type System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap square_gray_light__png_shadow_256px { + get { + object obj = ResourceManager.GetObject("square_gray_light__png_shadow_256px", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + /// /// Looks up a localized resource of type System.Drawing.Bitmap. /// diff --git a/Properties/Resources.resx b/Properties/Resources.resx index 6281906..e4f3f00 100644 --- a/Properties/Resources.resx +++ b/Properties/Resources.resx @@ -136,6 +136,12 @@ ..\Resources\b_rook_png_shadow_256px.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + ..\Resources\square brown light_png_shadow_256px.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + + ..\Resources\square gray light _png_shadow_256px.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + ..\Resources\w_bishop_png_shadow_256px.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a diff --git a/Resources/square brown light_png_shadow_256px.png b/Resources/square brown light_png_shadow_256px.png new file mode 100644 index 0000000..251f862 Binary files /dev/null and b/Resources/square brown light_png_shadow_256px.png differ diff --git a/Resources/square gray light _png_shadow_256px.png b/Resources/square gray light _png_shadow_256px.png new file mode 100644 index 0000000..a8b527f Binary files /dev/null and b/Resources/square gray light _png_shadow_256px.png differ diff --git a/bin/Debug/Chess.exe b/bin/Debug/Chess.exe index d4cf93c..73c3566 100644 Binary files a/bin/Debug/Chess.exe and b/bin/Debug/Chess.exe differ diff --git a/bin/Debug/Chess.pdb b/bin/Debug/Chess.pdb index 5a0e80f..4cd0333 100644 Binary files a/bin/Debug/Chess.pdb and b/bin/Debug/Chess.pdb differ diff --git a/obj/Debug/Chess.Properties.Resources.resources b/obj/Debug/Chess.Properties.Resources.resources index 928dda4..35371b6 100644 Binary files a/obj/Debug/Chess.Properties.Resources.resources and b/obj/Debug/Chess.Properties.Resources.resources differ diff --git a/obj/Debug/Chess.csproj.CoreCompileInputs.cache b/obj/Debug/Chess.csproj.CoreCompileInputs.cache index 0afc8e6..8da1a1c 100644 --- a/obj/Debug/Chess.csproj.CoreCompileInputs.cache +++ b/obj/Debug/Chess.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -c665b544783ebbad06de451d9c9d8d9603c7e71b +1b1ae2325e85edb2d670644e476ed984350486d2 diff --git a/obj/Debug/Chess.csproj.GenerateResource.cache b/obj/Debug/Chess.csproj.GenerateResource.cache index 15b52cf..62b3a45 100644 Binary files a/obj/Debug/Chess.csproj.GenerateResource.cache and b/obj/Debug/Chess.csproj.GenerateResource.cache differ diff --git a/obj/Debug/Chess.csprojAssemblyReference.cache b/obj/Debug/Chess.csprojAssemblyReference.cache index f81aff4..594fb41 100644 Binary files a/obj/Debug/Chess.csprojAssemblyReference.cache and b/obj/Debug/Chess.csprojAssemblyReference.cache differ diff --git a/obj/Debug/Chess.exe b/obj/Debug/Chess.exe index d4cf93c..73c3566 100644 Binary files a/obj/Debug/Chess.exe and b/obj/Debug/Chess.exe differ diff --git a/obj/Debug/Chess.pdb b/obj/Debug/Chess.pdb index 5a0e80f..4cd0333 100644 Binary files a/obj/Debug/Chess.pdb and b/obj/Debug/Chess.pdb differ diff --git a/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache index 29860f8..3a9edc4 100644 Binary files a/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache and b/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/obj/Debug/TempPE/Properties.Resources.Designer.cs.dll b/obj/Debug/TempPE/Properties.Resources.Designer.cs.dll index 87e591a..22f2a2d 100644 Binary files a/obj/Debug/TempPE/Properties.Resources.Designer.cs.dll and b/obj/Debug/TempPE/Properties.Resources.Designer.cs.dll differ -- cgit v1.2.3-70-g09d2