Projektdateien hinzufügen.
This commit is contained in:
parent
2a94e96895
commit
368cb29eaf
22
ChessPanel.sln
Normal file
22
ChessPanel.sln
Normal file
@ -0,0 +1,22 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 14
|
||||
VisualStudioVersion = 14.0.25420.1
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chess", "ChessPanel\Chess.csproj", "{4F06559F-7350-4124-8CE9-05EEC4768EB0}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{4F06559F-7350-4124-8CE9-05EEC4768EB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{4F06559F-7350-4124-8CE9-05EEC4768EB0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4F06559F-7350-4124-8CE9-05EEC4768EB0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4F06559F-7350-4124-8CE9-05EEC4768EB0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
6
ChessPanel/App.config
Normal file
6
ChessPanel/App.config
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
|
||||
</startup>
|
||||
</configuration>
|
133
ChessPanel/Chess.csproj
Normal file
133
ChessPanel/Chess.csproj
Normal file
@ -0,0 +1,133 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{4F06559F-7350-4124-8CE9-05EEC4768EB0}</ProjectGuid>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>ChessPanel</RootNamespace>
|
||||
<AssemblyName>ChessPanel</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Deployment" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="WindowsBase" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ChessGame.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="ChessBoard.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Field.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="FieldColorSet.cs" />
|
||||
<Compile Include="MainForm.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
<Compile Include="MainForm.Designer.cs">
|
||||
<DependentUpon>MainForm.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Piece.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Properties\Resources.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DesignTime>True</DesignTime>
|
||||
<DependentUpon>Resources.resx</DependentUpon>
|
||||
</Compile>
|
||||
<EmbeddedResource Include="MainForm.resx">
|
||||
<DependentUpon>MainForm.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Include="Properties\Resources.resx">
|
||||
<Generator>PublicResXFileCodeGenerator</Generator>
|
||||
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
||||
</EmbeddedResource>
|
||||
<None Include="ClassDiagram.cd" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\BishopBlack.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\BishopWhite.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\Empty.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\KingBlack.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\KingWhite.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\KnightBlack.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\KnightWhite.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\PawnBlack.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\PawnWhite.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\QueenBlack.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\QueenWhite.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\RookBlack.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\RookWhite.png" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
202
ChessPanel/ChessBoard.cs
Normal file
202
ChessPanel/ChessBoard.cs
Normal file
@ -0,0 +1,202 @@
|
||||
using Chess;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Forms;
|
||||
using static Chess.Piece;
|
||||
|
||||
namespace ChessPanel
|
||||
{
|
||||
public partial class ChessBoard : Panel
|
||||
{
|
||||
public Field[,] fields;
|
||||
public PieceColor activePlayer;
|
||||
private int gridSize = 8;
|
||||
public bool isSaved = true;
|
||||
Dictionary<Vector, PieceTeam> validTargetSpots = new Dictionary<Vector, PieceTeam>();
|
||||
private Vector? selectedField;
|
||||
private Vector? SelectedField
|
||||
{
|
||||
get { return selectedField; }
|
||||
set { selectedField = value; HighlightValidTargetSpots(); }
|
||||
}
|
||||
private void HighlightValidTargetSpots()
|
||||
{
|
||||
if (selectedField.HasValue && fields[(int)selectedField.Value.X, (int)selectedField.Value.Y].CurrentPiece != null) // Wenn das neu ausgewählte Feld eine Figur enthält, wird diese ausgewählt
|
||||
{
|
||||
fields[(int)selectedField.Value.X, (int)selectedField.Value.Y].Select();
|
||||
}
|
||||
ResetValidTargetSpots();
|
||||
if (selectedField.HasValue)
|
||||
{
|
||||
if (fields[(int)selectedField.Value.X, (int)selectedField.Value.Y].CurrentPiece != null)
|
||||
{
|
||||
validTargetSpots = fields[(int)selectedField.Value.X, (int)selectedField.Value.Y].CurrentPiece.GetValidTargetFields(fields, new Vector(selectedField.Value.X, selectedField.Value.Y));
|
||||
}
|
||||
foreach (var item in validTargetSpots)
|
||||
{
|
||||
fields[(int)item.Key.X, (int)item.Key.Y].Highlight(item.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Setzt die Markierungen sowie die Liste der möglichen Zielfelder zurück
|
||||
/// </summary>
|
||||
private void ResetValidTargetSpots()
|
||||
{
|
||||
foreach (var item in validTargetSpots)
|
||||
{
|
||||
fields[(int)item.Key.X, (int)item.Key.Y].Deselect();
|
||||
}
|
||||
validTargetSpots.Clear(); // Markierung der Zielfelder zurücksetzen
|
||||
}
|
||||
public int GridSize
|
||||
{
|
||||
get { return gridSize; }
|
||||
set { gridSize = value; }
|
||||
}
|
||||
public ChessBoard()
|
||||
{
|
||||
this.DoubleBuffered = true;
|
||||
this.SuspendLayout();
|
||||
this.Resize += new System.EventHandler(this.ChessBoard_Resize);
|
||||
InitializeGrid();
|
||||
InitializePieces();
|
||||
activePlayer = PieceColor.White;
|
||||
this.ResumeLayout(false);
|
||||
}
|
||||
private void ToggleActivePlayer() => activePlayer = activePlayer == PieceColor.White ? PieceColor.Black : PieceColor.White;
|
||||
private void DoPieceMove(Vector location, Vector newLocation, bool togglePlayer = true)
|
||||
{
|
||||
isSaved = false;
|
||||
#region Castling
|
||||
if (fields[(int)location.X, (int)location.Y].CurrentPiece.type == PieceType.King && Math.Abs(newLocation.X - location.X) == 2)
|
||||
DoPieceMove(new Vector(((newLocation.X - location.X == -2) ? 0 : 7), location.Y), new Vector(location.X + (newLocation.X - location.X) / 2, location.Y), false /*Dont Toggle Player*/); // Move Rook
|
||||
#endregion
|
||||
#region EnPassant
|
||||
foreach (var item in fields) // Reset Enpassant Possible states each move
|
||||
{
|
||||
if (item.CurrentPiece != null)
|
||||
if (item.CurrentPiece.type == PieceType.Pawn)
|
||||
{
|
||||
item.CurrentPiece.EnPassantPossible = false;
|
||||
}
|
||||
}
|
||||
if (fields[(int)location.X, (int)location.Y].CurrentPiece.type == PieceType.Pawn && !fields[(int)location.X, (int)location.Y].CurrentPiece.HasMoved)
|
||||
fields[(int)location.X, (int)location.Y].CurrentPiece.EnPassantPossible = true;
|
||||
if (fields[(int)location.X, (int)location.Y].CurrentPiece.type == PieceType.Pawn && newLocation.X != location.X && fields[(int)newLocation.X, (int)newLocation.Y].IsEmpty)
|
||||
fields[(int)newLocation.X, (int)newLocation.Y + (fields[(int)location.X, (int)location.Y].CurrentPiece.color == PieceColor.White ? 1 : -1)].CurrentPiece = null;
|
||||
#endregion
|
||||
fields[(int)location.X, (int)location.Y].CurrentPiece.HasMoved = true; // Figur Bereits bewegt Status aktualisieren
|
||||
fields[(int)newLocation.X, (int)newLocation.Y].CurrentPiece = fields[(int)location.X, (int)location.Y].CurrentPiece; // Figur an neue Position kopieren
|
||||
fields[(int)location.X, (int)location.Y].CurrentPiece = null; // Alte Kopie der Figur löschen
|
||||
if (togglePlayer)
|
||||
ToggleActivePlayer();
|
||||
}
|
||||
private void InitializePieces()
|
||||
{
|
||||
fields[0, 0].CurrentPiece = new Rook(PieceColor.Black);
|
||||
fields[7, 0].CurrentPiece = new Rook(PieceColor.Black);
|
||||
fields[0, 7].CurrentPiece = new Rook(PieceColor.White);
|
||||
fields[7, 7].CurrentPiece = new Rook(PieceColor.White);
|
||||
|
||||
fields[1, 0].CurrentPiece = new Knight(PieceColor.Black);
|
||||
fields[6, 0].CurrentPiece = new Knight(PieceColor.Black);
|
||||
fields[1, 7].CurrentPiece = new Knight(PieceColor.White);
|
||||
fields[6, 7].CurrentPiece = new Knight(PieceColor.White);
|
||||
|
||||
fields[2, 0].CurrentPiece = new Bishop(PieceColor.Black);
|
||||
fields[5, 0].CurrentPiece = new Bishop(PieceColor.Black);
|
||||
fields[2, 7].CurrentPiece = new Bishop(PieceColor.White);
|
||||
fields[5, 7].CurrentPiece = new Bishop(PieceColor.White);
|
||||
|
||||
fields[4, 0].CurrentPiece = new King(PieceColor.Black);
|
||||
fields[3, 0].CurrentPiece = new Queen(PieceColor.Black);
|
||||
|
||||
fields[3, 7].CurrentPiece = new Queen(PieceColor.White);
|
||||
fields[4, 7].CurrentPiece = new King(PieceColor.White);
|
||||
for (int x = 0; x < 8; x++)
|
||||
{
|
||||
fields[x, 1].CurrentPiece = new Pawn(PieceColor.Black);
|
||||
fields[x, 6].CurrentPiece = new Pawn(PieceColor.White);
|
||||
}
|
||||
}
|
||||
private void InitializeGrid()
|
||||
{
|
||||
this.fields = new Field[gridSize, gridSize];
|
||||
int subPanelEdgeLength = Math.Min(this.Size.Width, this.Size.Height) / gridSize;
|
||||
System.Drawing.Size subPanelSize = new System.Drawing.Size(subPanelEdgeLength, subPanelEdgeLength);
|
||||
for (int x = 0; x < this.gridSize; x++)
|
||||
{
|
||||
for (int y = 0; y < this.gridSize; y++)
|
||||
{
|
||||
Field field = new Field(x, y);
|
||||
field.Size = subPanelSize;
|
||||
field.Location = new System.Drawing.Point(x * subPanelSize.Width, y * subPanelSize.Height);
|
||||
field.FieldClick += FieldClicked;
|
||||
fields[x, y] = field;
|
||||
this.Controls.Add(fields[x, y]);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void FieldClicked(Vector coordinates)
|
||||
{
|
||||
this.SuspendLayout();
|
||||
if (SelectedField.HasValue && validTargetSpots.ContainsKey(coordinates)) // Ein erlaubtes Zielfeld wurde angeklickt
|
||||
{
|
||||
DoPieceMove(SelectedField.Value, coordinates); // Bewege Figur
|
||||
ResetSelectedField(); // Auswahl zurücksetzen
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SelectedField == coordinates) // Erneutes Anklicken des Auswahlfeldes
|
||||
ResetSelectedField(); // Auswahl zurücksetzen
|
||||
else // Tritt ein, wenn ein Feld angeklickt wurde, dass nicht das Auswahlfeld ist
|
||||
{
|
||||
if (SelectedField.HasValue) // Wenn ein Feld ausgewählt ist wird die Auswahl gelöscht falls nicht das Auswahlfeld angeklickt wurde
|
||||
ResetSelectedField(); // Auswahl zurücksetzen
|
||||
if (!fields[(int)coordinates.X, (int)coordinates.Y].IsEmpty && fields[(int)coordinates.X, (int)coordinates.Y].CurrentPiece.color == activePlayer) // Feld wird nur ausgewählt, wenn die Farbe der Figur mit der des aktiven Spielers übereinstimmt
|
||||
SelectedField = coordinates; // Kein Feld ist ausgewählt: Angeklicktes Feld auswählen (!) AKTUALISIERT AUTOMATISCH DIE MARKIERUNGEN
|
||||
}
|
||||
}
|
||||
this.ResumeLayout(false);
|
||||
}
|
||||
/// <summary>
|
||||
/// Löscht die Auswahl des aktuell ausgewählten Feldes
|
||||
/// </summary>
|
||||
private void ResetSelectedField()
|
||||
{
|
||||
fields[(int)SelectedField.Value.X, (int)SelectedField.Value.Y].Deselect(); // Auswahl zurücksetzen
|
||||
SelectedField = null; // Erlaubte Zielfelder zurücksetzen
|
||||
}
|
||||
#region Helper Can be deleted
|
||||
static readonly string[] Columns = new[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ", "BA", "BB", "BC", "BD", "BE", "BF", "BG", "BH" };
|
||||
public static string IndexToColumn(int index)
|
||||
{
|
||||
if (index <= 0)
|
||||
throw new IndexOutOfRangeException("index must be a positive number");
|
||||
|
||||
return Columns[index - 1];
|
||||
}
|
||||
#endregion
|
||||
private void ChessBoard_Resize(object sender, EventArgs e)
|
||||
{
|
||||
RefreshSize();
|
||||
}
|
||||
private void RefreshSize()
|
||||
{
|
||||
this.SuspendLayout();
|
||||
int subPanelEdgeLength = Math.Min(this.Size.Width, this.Size.Height) / gridSize;
|
||||
System.Drawing.Size subPanelSize = new System.Drawing.Size(subPanelEdgeLength, subPanelEdgeLength);
|
||||
for (int x = 0; x < this.gridSize; x++)
|
||||
{
|
||||
for (int y = 0; y < this.gridSize; y++)
|
||||
{
|
||||
fields[x, y].Size = subPanelSize;
|
||||
fields[x, y].Location = new System.Drawing.Point(x * subPanelSize.Width, y * subPanelSize.Height);
|
||||
}
|
||||
}
|
||||
this.ResumeLayout(false);
|
||||
}
|
||||
}
|
||||
}
|
81
ChessPanel/ChessGame.cs
Normal file
81
ChessPanel/ChessGame.cs
Normal file
@ -0,0 +1,81 @@
|
||||
using ChessPanel;
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Chess
|
||||
{
|
||||
public class ChessGame : Panel
|
||||
{
|
||||
ChessBoard board;
|
||||
public ChessGame()
|
||||
{
|
||||
InitializeComponent();
|
||||
RefreshSize();
|
||||
}
|
||||
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.board = new ChessBoard();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// board
|
||||
//
|
||||
this.board.GridSize = 8;
|
||||
this.board.Cursor = Cursors.Hand;
|
||||
this.board.Location = new System.Drawing.Point(0, 0);
|
||||
this.board.Name = "board";
|
||||
int size = Math.Min(this.Size.Width, this.Size.Height);
|
||||
board.Size = new System.Drawing.Size(size, size);
|
||||
this.board.TabIndex = 0;
|
||||
//
|
||||
// ChessGame
|
||||
//
|
||||
this.Controls.Add(this.board);
|
||||
this.Resize += new System.EventHandler(this.ResizeEvent);
|
||||
this.ResumeLayout(false);
|
||||
}
|
||||
|
||||
private void ResizeEvent(object sender, EventArgs e)
|
||||
{
|
||||
RefreshSize();
|
||||
}
|
||||
|
||||
internal void FormClosing(object sender, FormClosingEventArgs e)
|
||||
{
|
||||
if (board.isSaved)
|
||||
{
|
||||
e.Cancel = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (MessageBox.Show("Das aktuelle Spiel ist noch nicht gespeichert!\nMöchtest du es jetzt speichern?", "Speichern?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation))
|
||||
{
|
||||
case DialogResult.Cancel:
|
||||
e.Cancel = true;
|
||||
break;
|
||||
case DialogResult.Yes:
|
||||
SaveGame();
|
||||
e.Cancel = false;
|
||||
break;
|
||||
case DialogResult.No:
|
||||
e.Cancel = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveGame()
|
||||
{
|
||||
MessageBox.Show("TODO: SaveGame here!");
|
||||
}
|
||||
|
||||
private void RefreshSize()
|
||||
{
|
||||
int size = Math.Min(this.Size.Width, this.Size.Height);
|
||||
board.Size = new System.Drawing.Size(size, size);
|
||||
board.Location = this.Size.Width > this.Size.Height ? new System.Drawing.Point((this.Size.Width - size) / 2, 0) : new System.Drawing.Point(0, (this.Size.Height - size) / 2);
|
||||
}
|
||||
}
|
||||
}
|
107
ChessPanel/ClassDiagram.cd
Normal file
107
ChessPanel/ClassDiagram.cd
Normal file
@ -0,0 +1,107 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ClassDiagram MajorVersion="1" MinorVersion="1">
|
||||
<Class Name="ChessPanel.ChessBoard" Collapsed="true">
|
||||
<Position X="19" Y="0.5" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AICAAAAAAAAAAAAQAQAACCAEAIAgJAAACgAAAEoABII=</HashCode>
|
||||
<FileName>ChessBoard.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="Chess.ChessGame" Collapsed="true">
|
||||
<Position X="20.75" Y="0.5" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAAAAAAAAAAIAAAAEAAAAACAAAABAAAIAAAAAAAAAA=</HashCode>
|
||||
<FileName>ChessGame.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="Chess.Field" Collapsed="true">
|
||||
<Position X="22.5" Y="0.5" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AABBgAEIEIYEIBAACAAAIAACADAAAEACAAAgQAAAABA=</HashCode>
|
||||
<FileName>Field.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="Chess.Form1" Collapsed="true">
|
||||
<Position X="19" Y="1.5" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAAEAAAACAAAAAAAACAAAACAAAAAAAAAAAAAAAAAAA=</HashCode>
|
||||
<FileName>Form1.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="Chess.Piece" Collapsed="true">
|
||||
<Position X="10.75" Y="0.5" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAABAAAAAAAAAAAAAAAAAAAAEAABAACAAAAAAAgAAAA=</HashCode>
|
||||
<FileName>Piece.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="Chess.Rook" Collapsed="true">
|
||||
<Position X="9.75" Y="1.75" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAgAAAA=</HashCode>
|
||||
<FileName>Piece.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="Chess.Pawn" Collapsed="true">
|
||||
<Position X="14.25" Y="1.75" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAgAAAA=</HashCode>
|
||||
<FileName>Piece.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="Chess.Knight" Collapsed="true">
|
||||
<Position X="5.25" Y="1.75" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAA=</HashCode>
|
||||
<FileName>Piece.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="Chess.Bishop" Collapsed="true">
|
||||
<Position X="7.5" Y="1.75" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAgAAAA=</HashCode>
|
||||
<FileName>Piece.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="Chess.Queen" Collapsed="true">
|
||||
<Position X="12" Y="1.75" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAA=</HashCode>
|
||||
<FileName>Piece.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="Chess.King" Collapsed="true">
|
||||
<Position X="16.5" Y="1.75" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAgAAAA=</HashCode>
|
||||
<FileName>Piece.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="Chess.Program" Collapsed="true">
|
||||
<Position X="20.75" Y="1.5" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAA=</HashCode>
|
||||
<FileName>Program.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="Chess.Properties.Resources" Collapsed="true">
|
||||
<Position X="22.5" Y="1.5" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAAAADAgQAQABAAAAIBEAAAIUAAIAAABAACAAAAIIA=</HashCode>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Class Name="Chess.Properties.Settings" Collapsed="true">
|
||||
<Position X="19" Y="2.5" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAAAAAAAAAAAAAAAAAAIAAAAAABAAAAAAAAAAAAAAA=</HashCode>
|
||||
</TypeIdentifier>
|
||||
</Class>
|
||||
<Struct Name="Chess.FieldColorSet" Collapsed="true">
|
||||
<Position X="19" Y="3.75" Width="1.5" />
|
||||
<TypeIdentifier>
|
||||
<HashCode>AAAQAQAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
|
||||
<FileName>FieldColorSet.cs</FileName>
|
||||
</TypeIdentifier>
|
||||
</Struct>
|
||||
<Font Name="Segoe UI" Size="9" />
|
||||
</ClassDiagram>
|
101
ChessPanel/Field.cs
Normal file
101
ChessPanel/Field.cs
Normal file
@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
using System.Drawing;
|
||||
using static Chess.Piece;
|
||||
using System.Windows;
|
||||
|
||||
namespace Chess
|
||||
{
|
||||
public class Field : Button
|
||||
{
|
||||
static readonly FieldColorSet primaryNormalColorSet = new FieldColorSet(Color.FromArgb(204, 204, 204), Color.FromArgb(153, 153, 153), Color.White);
|
||||
static readonly FieldColorSet secondaryNormalColorSet = new FieldColorSet(Color.FromArgb(51, 51, 51), Color.FromArgb(102, 102, 102), Color.Black);
|
||||
static readonly FieldColorSet primarySelectedColorSet = new FieldColorSet(Color.FromArgb(204, 204, 102), Color.FromArgb(153, 153, 102), Color.FromArgb(255, 255, 102));
|
||||
static readonly FieldColorSet secondarySelectedColorSet = new FieldColorSet(Color.FromArgb(204, 204, 51), Color.FromArgb(153, 153, 51), Color.FromArgb(255, 255, 51));
|
||||
static readonly FieldColorSet primaryValidFriendlyTargetFieldSet = new FieldColorSet(Color.FromArgb(102, 204, 102), Color.FromArgb(102, 153, 102), Color.FromArgb(102, 255, 102));
|
||||
static readonly FieldColorSet secondaryValidFriendlyTargetFieldSet = new FieldColorSet(Color.FromArgb(51, 204, 51), Color.FromArgb(51, 153, 51), Color.FromArgb(51, 255, 51));
|
||||
static readonly FieldColorSet primaryValidEnemyTargetFieldSet = new FieldColorSet(Color.FromArgb(204, 102, 102), Color.FromArgb(153, 102, 102), Color.FromArgb(255, 102, 102));
|
||||
static readonly FieldColorSet secondaryValidEnemyTargetFieldSet = new FieldColorSet(Color.FromArgb(204, 51, 51), Color.FromArgb(153, 51, 51), Color.FromArgb(255, 51, 51));
|
||||
public Vector location;
|
||||
private FieldColorSet currentNormalColorSet;
|
||||
private FieldColorSet currentEnemyColorSet;
|
||||
private FieldColorSet currentSelectedColorSet;
|
||||
private FieldColorSet currentAllyColorSet;
|
||||
public delegate void ClickEventHandler(Vector coordinates);
|
||||
public event ClickEventHandler FieldClick;
|
||||
public bool IsEmpty { get { return currentPiece == null; } }
|
||||
public Piece currentPiece;
|
||||
public Piece CurrentPiece
|
||||
{
|
||||
get { return currentPiece; }
|
||||
set { currentPiece = value; RefreshPiece(); }
|
||||
}
|
||||
|
||||
private void RefreshPiece()
|
||||
{
|
||||
if (currentPiece == null)
|
||||
this.BackgroundImage = ChessPanel.Properties.Resources.Empty;
|
||||
else
|
||||
this.BackgroundImage = CurrentPiece.image;
|
||||
}
|
||||
public void Deselect()
|
||||
{
|
||||
setActiveColorSet(currentNormalColorSet);
|
||||
}
|
||||
public void Highlight(PieceTeam team) // validTargetField
|
||||
{
|
||||
switch (team)
|
||||
{
|
||||
case PieceTeam.Enemy:
|
||||
setActiveColorSet(currentEnemyColorSet);
|
||||
break;
|
||||
case PieceTeam.Ally:
|
||||
setActiveColorSet(currentAllyColorSet);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
public new void Select() // selectedField
|
||||
{
|
||||
setActiveColorSet(currentSelectedColorSet);
|
||||
}
|
||||
private void setActiveColorSet(FieldColorSet set)
|
||||
{
|
||||
this.BackColor = set.NormalColor;
|
||||
this.FlatAppearance.MouseDownBackColor = set.MouseDownColor;
|
||||
this.FlatAppearance.MouseOverBackColor = set.MouseOverColor;
|
||||
}
|
||||
public Field(int x, int y)
|
||||
{
|
||||
this.currentNormalColorSet = (x + y) % 2 == 0 ? primaryNormalColorSet : secondaryNormalColorSet;
|
||||
this.currentAllyColorSet = (x + y) % 2 == 0 ? primaryValidFriendlyTargetFieldSet : secondaryValidFriendlyTargetFieldSet;
|
||||
this.currentEnemyColorSet = (x + y) % 2 == 0 ? primaryValidEnemyTargetFieldSet : secondaryValidEnemyTargetFieldSet;
|
||||
this.currentSelectedColorSet = (x + y) % 2 == 0 ? primarySelectedColorSet : secondarySelectedColorSet;
|
||||
|
||||
|
||||
this.FlatStyle = FlatStyle.Flat;
|
||||
this.FlatAppearance.BorderSize = 0;
|
||||
this.setActiveColorSet(this.currentNormalColorSet);
|
||||
this.Cursor = Cursors.Arrow;
|
||||
|
||||
this.location = new Vector(x, y); // NICHT L(!)ocation, der gibt die Position des Buttons in Pixel auf dem Control an!
|
||||
this.Name = string.Format($"field[{x},{y}]");
|
||||
this.Text = "";
|
||||
|
||||
this.BackgroundImageLayout = ImageLayout.Zoom;
|
||||
this.BackgroundImage = ChessPanel.Properties.Resources.Empty;
|
||||
|
||||
this.Click += clicked;
|
||||
}
|
||||
|
||||
private void clicked(object sender, EventArgs e)
|
||||
{
|
||||
FieldClick(this.location);
|
||||
}
|
||||
public override string ToString()
|
||||
{
|
||||
return this.currentPiece.type.ToString();
|
||||
}
|
||||
}
|
||||
}
|
17
ChessPanel/FieldColorSet.cs
Normal file
17
ChessPanel/FieldColorSet.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System.Drawing;
|
||||
namespace Chess
|
||||
{
|
||||
struct FieldColorSet
|
||||
{
|
||||
public FieldColorSet(Color MOC, Color MDC, Color NC)
|
||||
{
|
||||
MouseOverColor = MOC;
|
||||
MouseDownColor = MDC;
|
||||
NormalColor = NC;
|
||||
}
|
||||
public Color MouseOverColor { get; set; }
|
||||
public Color MouseDownColor { get; set; }
|
||||
public Color NormalColor { get; set; }
|
||||
}
|
||||
|
||||
}
|
69
ChessPanel/MainForm.Designer.cs
generated
Normal file
69
ChessPanel/MainForm.Designer.cs
generated
Normal file
@ -0,0 +1,69 @@
|
||||
namespace Chess
|
||||
{
|
||||
partial class MainForm
|
||||
{
|
||||
/// <summary>
|
||||
/// Erforderliche Designervariable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Verwendete Ressourcen bereinigen.
|
||||
/// </summary>
|
||||
/// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Vom Windows Form-Designer generierter Code
|
||||
|
||||
/// <summary>
|
||||
/// Erforderliche Methode für die Designerunterstützung.
|
||||
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.chessGame = new Chess.ChessGame();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// chessGame
|
||||
//
|
||||
this.chessGame.BackColor = System.Drawing.Color.Black;
|
||||
this.chessGame.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.chessGame.Location = new System.Drawing.Point(0, 0);
|
||||
this.chessGame.Name = "chessGame";
|
||||
this.chessGame.Size = new System.Drawing.Size(1896, 1016);
|
||||
this.chessGame.TabIndex = 0;
|
||||
this.chessGame.UseWaitCursor = true;
|
||||
//
|
||||
// MainForm
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(1896, 1016);
|
||||
this.Controls.Add(this.chessGame);
|
||||
this.DoubleBuffered = true;
|
||||
this.KeyPreview = true;
|
||||
this.Name = "MainForm";
|
||||
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
|
||||
this.Text = "Color Board";
|
||||
this.UseWaitCursor = true;
|
||||
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
|
||||
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
|
||||
this.Load += new System.EventHandler(this.MainForm_Load);
|
||||
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.MainForm_KeyUp);
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private ChessGame chessGame;
|
||||
}
|
||||
}
|
||||
|
52
ChessPanel/MainForm.cs
Normal file
52
ChessPanel/MainForm.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Chess
|
||||
{
|
||||
public partial class MainForm : Form
|
||||
{
|
||||
bool Fullscreen = false;
|
||||
bool fullscreen
|
||||
{
|
||||
get { return Fullscreen; }
|
||||
set { Fullscreen = value; RefreshFullScreen(); }
|
||||
}
|
||||
public MainForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void RefreshFullScreen()
|
||||
{
|
||||
this.SuspendLayout();
|
||||
if (fullscreen)
|
||||
{
|
||||
this.WindowState = FormWindowState.Normal;
|
||||
this.FormBorderStyle = FormBorderStyle.None;
|
||||
this.Bounds = Screen.PrimaryScreen.Bounds;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.FormBorderStyle = FormBorderStyle.Sizable;
|
||||
this.WindowState = FormWindowState.Maximized;
|
||||
}
|
||||
this.ResumeLayout(true);
|
||||
}
|
||||
|
||||
private void MainForm_KeyUp(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.KeyCode == Keys.F11)
|
||||
fullscreen = !fullscreen;
|
||||
}
|
||||
|
||||
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
|
||||
{
|
||||
chessGame.FormClosing(sender, e);
|
||||
}
|
||||
|
||||
private void MainForm_Load(object sender, EventArgs e)
|
||||
{
|
||||
fullscreen = true;
|
||||
}
|
||||
}
|
||||
}
|
120
ChessPanel/MainForm.resx
Normal file
120
ChessPanel/MainForm.resx
Normal file
@ -0,0 +1,120 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<!--
|
||||
Microsoft ResX Schema
|
||||
|
||||
Version 2.0
|
||||
|
||||
The primary goals of this format is to allow a simple XML format
|
||||
that is mostly human readable. The generation and parsing of the
|
||||
various data types are done through the TypeConverter classes
|
||||
associated with the data types.
|
||||
|
||||
Example:
|
||||
|
||||
... ado.net/XML headers & schema ...
|
||||
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||
<resheader name="version">2.0</resheader>
|
||||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||
</data>
|
||||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||
<comment>This is a comment</comment>
|
||||
</data>
|
||||
|
||||
There are any number of "resheader" rows that contain simple
|
||||
name/value pairs.
|
||||
|
||||
Each data row contains a name, and value. The row also contains a
|
||||
type or mimetype. Type corresponds to a .NET class that support
|
||||
text/value conversion through the TypeConverter architecture.
|
||||
Classes that don't support this are serialized and stored with the
|
||||
mimetype set.
|
||||
|
||||
The mimetype is used for serialized objects, and tells the
|
||||
ResXResourceReader how to depersist the object. This is currently not
|
||||
extensible. For a given mimetype the value must be set accordingly:
|
||||
|
||||
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||
that the ResXResourceWriter will generate, however the reader can
|
||||
read any of the formats listed below.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.binary.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.soap.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||
value : The object must be serialized into a byte array
|
||||
: using a System.ComponentModel.TypeConverter
|
||||
: and then encoded with base64 encoding.
|
||||
-->
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
461
ChessPanel/Piece.cs
Normal file
461
ChessPanel/Piece.cs
Normal file
@ -0,0 +1,461 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using ChessPanel;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
|
||||
namespace Chess
|
||||
{
|
||||
public abstract class Piece
|
||||
{
|
||||
public enum PieceColor { White, Black };
|
||||
public enum PieceTeam { Enemy, Ally };
|
||||
public enum PieceType { King, Queen, Bishop, Rook, Knight, Pawn };
|
||||
public PieceColor color;
|
||||
public PieceType type;
|
||||
public Image image;
|
||||
public bool HasMoved = false;
|
||||
public bool EnPassantPossible = false; // Only in Pawn Class. Signs that this Piece has virtually not jumped two pieces, but one
|
||||
internal static bool IsEndangeredLocationForPiece(Field[,] fields, Vector pieceLocation, Vector testLocation, PieceColor color)
|
||||
{
|
||||
bool returnValue = false;
|
||||
Piece p = fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece;
|
||||
fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece = null;
|
||||
foreach (var field in fields)
|
||||
{
|
||||
if (field.currentPiece != null) if (field.currentPiece.color != color) if (field.currentPiece.GetValidTargetFields(fields, field.location, true).ContainsKey(testLocation))
|
||||
{
|
||||
returnValue = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece = p;
|
||||
return returnValue;
|
||||
}
|
||||
internal static bool MoveWillEndangerKing(Field[,] fields, Vector pieceLocation, Vector pieceMovedLocation, PieceColor color) // This does not test if it is a valid move spot
|
||||
{
|
||||
bool returnValue = false;
|
||||
Piece p1 = fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece;
|
||||
Piece p2 = fields[(int)pieceMovedLocation.X, (int)pieceMovedLocation.Y].currentPiece;
|
||||
fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece = null;
|
||||
fields[(int)pieceMovedLocation.X, (int)pieceMovedLocation.Y].currentPiece = p1;
|
||||
returnValue = KingIsEndangered(fields, color);
|
||||
fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece = p1;
|
||||
fields[(int)pieceMovedLocation.X, (int)pieceMovedLocation.Y].currentPiece = p2;
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
private static bool KingIsEndangered(Field[,] fields, PieceColor color)
|
||||
{
|
||||
Vector kingLocation = GetKingLocation(fields, color);
|
||||
foreach (var field in fields)
|
||||
{
|
||||
if (field.currentPiece == null || field.currentPiece.color != color)
|
||||
continue;
|
||||
if (field.currentPiece.GetValidTargetFields(fields, field.location, testKingDanger: true).ContainsKey(kingLocation))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static Vector GetKingLocation(Field[,] fields, PieceColor color)
|
||||
{
|
||||
foreach (var field in fields)
|
||||
if (!field.IsEmpty && field.currentPiece.type == PieceType.King && field.currentPiece.color == color)
|
||||
return field.location;
|
||||
throw new Exception("King not found!");
|
||||
}
|
||||
|
||||
internal abstract Dictionary<Vector, PieceTeam> GetValidTargetFields(Field[,] fields, Vector currentLocation, bool onlyAttackFields = false, bool testKingDanger = false);
|
||||
}
|
||||
public class Rook : Piece
|
||||
{
|
||||
public Rook(PieceColor color)
|
||||
{
|
||||
type = (PieceType)Enum.Parse(typeof(PieceType), this.GetType().Name);
|
||||
this.color = color;
|
||||
this.image = (Image)ChessPanel.Properties.Resources.ResourceManager.GetObject($"{this.type.ToString()}{this.color.ToString()}");
|
||||
}
|
||||
|
||||
internal override Dictionary<Vector, PieceTeam> GetValidTargetFields(Field[,] fields, Vector location, bool ignoreKingCheck = false, bool testKingDanger = false)
|
||||
{
|
||||
var targetFields = GetTargetFields(fields, location, this.color);
|
||||
/*if (!testKingDanger)
|
||||
foreach (var item in targetFields)
|
||||
if (MoveWillEndangerKing(fields, location, item.Key, this.color))
|
||||
targetFields.Remove(item.Key);*/
|
||||
return targetFields;
|
||||
}
|
||||
|
||||
internal static Dictionary<Vector, PieceTeam> GetTargetFields(Field[,] fields, Vector location, PieceColor color)
|
||||
{
|
||||
Dictionary<Vector, PieceTeam> targetFields = new Dictionary<Vector, PieceTeam>();
|
||||
|
||||
for (int x = (int)location.X - 1; x >= 0; x--) // nach Links
|
||||
{
|
||||
if (fields[x, (int)location.Y].IsEmpty)
|
||||
{
|
||||
targetFields.Add(new Vector(x, (int)location.Y), PieceTeam.Ally);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fields[x, (int)location.Y].currentPiece.color != color)
|
||||
targetFields.Add(new Vector(x, (int)location.Y), PieceTeam.Enemy);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (int x = (int)location.X + 1; x <= 7; x++) // nach Rechts
|
||||
{
|
||||
if (fields[x, (int)location.Y].IsEmpty)
|
||||
{
|
||||
targetFields.Add(new Vector(x, (int)location.Y), PieceTeam.Ally);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fields[x, (int)location.Y].currentPiece.color != color)
|
||||
targetFields.Add(new Vector(x, (int)location.Y), PieceTeam.Enemy);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int y = (int)location.Y - 1; y >= 0; y--) // nach Unten
|
||||
{
|
||||
if (fields[(int)(int)location.X, y].IsEmpty)
|
||||
{
|
||||
targetFields.Add(new Vector((int)location.X, y), PieceTeam.Ally);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fields[(int)(int)location.X, y].currentPiece.color != color)
|
||||
targetFields.Add(new Vector((int)location.X, y), PieceTeam.Enemy);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (int y = (int)location.Y + 1; y <= 7; y++) // nach Oben
|
||||
{
|
||||
if (fields[(int)(int)location.X, y].IsEmpty)
|
||||
{
|
||||
targetFields.Add(new Vector((int)location.X, y), PieceTeam.Ally);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fields[(int)(int)location.X, y].currentPiece.color != color)
|
||||
targetFields.Add(new Vector((int)location.X, y), PieceTeam.Enemy);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return targetFields;
|
||||
}
|
||||
} // FINALIZED
|
||||
public class Pawn : Piece
|
||||
{
|
||||
public Pawn(PieceColor color)
|
||||
{
|
||||
type = (PieceType)Enum.Parse(typeof(PieceType), this.GetType().Name);
|
||||
this.color = color;
|
||||
this.image = (Image)ChessPanel.Properties.Resources.ResourceManager.GetObject($"{this.type.ToString()}{this.color.ToString()}");
|
||||
}
|
||||
|
||||
internal override Dictionary<Vector, PieceTeam> GetValidTargetFields(Field[,] fields, Vector location, bool onlyAttackFields = false, bool testKingDanger = false)
|
||||
{
|
||||
var targetFields = GetTargetFields(fields, location, this.color, onlyAttackFields);
|
||||
/*if (!testKingDanger)
|
||||
foreach (var item in targetFields)
|
||||
if (MoveWillEndangerKing(fields, location, item.Key, this.color))
|
||||
targetFields.Remove(item.Key);*/
|
||||
return targetFields;
|
||||
}
|
||||
|
||||
private Dictionary<Vector, PieceTeam> GetTargetFields(Field[,] fields, Vector location, PieceColor color, bool onlyAttackFields = false)
|
||||
{
|
||||
Dictionary<Vector, PieceTeam> targetFields = new Dictionary<Vector, PieceTeam>();
|
||||
int direction = this.color == PieceColor.White ? -1 : 1;
|
||||
#region EnPassant
|
||||
if (location.Y == (this.color == PieceColor.White ? 3 : 4))
|
||||
for (int x = -1; x < 2; x += 2)
|
||||
if (location.X + x <= 7 && location.X + x >= 0)
|
||||
if (fields[(int)location.X + x, (int)location.Y].currentPiece != null)
|
||||
if (fields[(int)location.X + x, (int)location.Y].currentPiece.type == PieceType.Pawn)
|
||||
if (fields[(int)location.X + x, (int)location.Y].currentPiece.color != this.color)
|
||||
if (fields[(int)location.X + x, (int)location.Y].currentPiece.EnPassantPossible)
|
||||
targetFields.Add(new Vector(location.X + x, location.Y + direction), PieceTeam.Enemy);
|
||||
#endregion
|
||||
#region Movement
|
||||
if (!onlyAttackFields)
|
||||
for (int y = 1; y < 1 + (HasMoved ? 1 : 2); y++)
|
||||
{
|
||||
if (location.Y + (direction * y) > 7 || location.Y + (direction * y) < 0)
|
||||
break;
|
||||
if (fields[(int)location.X, (int)location.Y + (direction * y)].IsEmpty)
|
||||
targetFields.Add(new Vector(location.X, (int)location.Y + (direction * y)), PieceTeam.Ally);
|
||||
else
|
||||
break;
|
||||
}
|
||||
#endregion
|
||||
#region Attack
|
||||
for (int x = -1; x < 2; x += 2)
|
||||
{
|
||||
if (location.Y + (direction) > 7 || location.Y + (direction) < 0 || location.X + x > 7 || location.X + x < 0)
|
||||
continue;
|
||||
if (onlyAttackFields || (fields[(int)location.X + x, (int)location.Y + (direction)].currentPiece != null && fields[(int)location.X + x, (int)location.Y + (direction)].currentPiece.color != this.color))
|
||||
{
|
||||
targetFields.Add(new Vector((int)location.X + x, (int)location.Y + (direction)), PieceTeam.Enemy);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
return targetFields;
|
||||
}
|
||||
} // EN-PASSANT MISSING
|
||||
public class Knight : Piece
|
||||
{
|
||||
public Knight(PieceColor color)
|
||||
{
|
||||
type = (PieceType)Enum.Parse(typeof(PieceType), this.GetType().Name);
|
||||
this.color = color;
|
||||
this.image = (Image)ChessPanel.Properties.Resources.ResourceManager.GetObject($"{this.type.ToString()}{this.color.ToString()}");
|
||||
}
|
||||
|
||||
internal override Dictionary<Vector, PieceTeam> GetValidTargetFields(Field[,] fields, Vector location, bool ignoreKingCheck = false, bool testKingDanger = false)
|
||||
{
|
||||
var targetFields = GetTargetFields(fields, location, this.color);
|
||||
/* if (!testKingDanger)
|
||||
foreach (var item in targetFields)
|
||||
if (MoveWillEndangerKing(fields, location, item.Key, this.color))
|
||||
targetFields.Remove(item.Key);*/
|
||||
return targetFields;
|
||||
}
|
||||
|
||||
private Dictionary<Vector, PieceTeam> GetTargetFields(Field[,] fields, Vector location, PieceColor color)
|
||||
{
|
||||
Dictionary<Vector, PieceTeam> targetFields = new Dictionary<Vector, PieceTeam>();
|
||||
|
||||
for (int a = -1; a < 2; a += 2)
|
||||
{
|
||||
for (int b = -2; b < 3; b += 4)
|
||||
{
|
||||
if (!(a + (int)location.X > 7 || b + (int)location.Y < 0 || a + (int)location.X < 0 || b + (int)location.Y > 7))
|
||||
{
|
||||
if (fields[a + (int)location.X, b + (int)location.Y].IsEmpty)
|
||||
{
|
||||
targetFields.Add(new Vector(a + (int)location.X, b + (int)location.Y), PieceTeam.Ally);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fields[a + (int)location.X, b + (int)location.Y].currentPiece.color != color)
|
||||
targetFields.Add(new Vector(a + (int)location.X, b + (int)location.Y), PieceTeam.Enemy);
|
||||
}
|
||||
}
|
||||
if (!(b + (int)location.X > 7 || a + (int)location.Y < 0 || b + (int)location.X < 0 || a + (int)location.Y > 7))
|
||||
{
|
||||
if (fields[b + (int)location.X, a + (int)location.Y].IsEmpty)
|
||||
{
|
||||
targetFields.Add(new Vector(b + (int)location.X, a + (int)location.Y), PieceTeam.Ally);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fields[b + (int)location.X, a + (int)location.Y].currentPiece.color != color)
|
||||
targetFields.Add(new Vector(b + (int)location.X, a + (int)location.Y), PieceTeam.Enemy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return targetFields;
|
||||
}
|
||||
} // FINALIZED
|
||||
public class Bishop : Piece
|
||||
{
|
||||
public Bishop(PieceColor color)
|
||||
{
|
||||
type = (PieceType)Enum.Parse(typeof(PieceType), this.GetType().Name);
|
||||
this.color = color;
|
||||
this.image = (Image)ChessPanel.Properties.Resources.ResourceManager.GetObject($"{this.type.ToString()}{this.color.ToString()}");
|
||||
}
|
||||
|
||||
internal override Dictionary<Vector, PieceTeam> GetValidTargetFields(Field[,] fields, Vector location, bool onlyAttackFields = false, bool testKingDanger = false)
|
||||
{
|
||||
var targetFields = GetTargetFields(fields, location, this.color, onlyAttackFields);
|
||||
/* if (!testKingDanger)
|
||||
foreach (var item in targetFields)
|
||||
if (MoveWillEndangerKing(fields, location, item.Key, this.color))
|
||||
targetFields.Remove(item.Key);*/
|
||||
return targetFields;
|
||||
}
|
||||
internal static Dictionary<Vector, PieceTeam> GetTargetFields(Field[,] fields, Vector location, PieceColor color, bool onlyAttackFields = true)
|
||||
{
|
||||
Dictionary<Vector, PieceTeam> targetFields = new Dictionary<Vector, PieceTeam>();
|
||||
|
||||
for (int xy = 1; xy <= 7; xy++) // nach RechtsOben
|
||||
{
|
||||
if (location.X + xy > 7 || (int)location.Y + xy > 7)
|
||||
break;
|
||||
if (fields[xy + (int)location.X, xy + (int)location.Y].IsEmpty)
|
||||
{
|
||||
targetFields.Add(new Vector(xy + (int)location.X, xy + (int)location.Y), PieceTeam.Ally);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fields[xy + (int)location.X, xy + (int)location.Y].currentPiece.color != color)
|
||||
targetFields.Add(new Vector(xy + (int)location.X, xy + (int)location.Y), PieceTeam.Enemy);
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int xy = 1; xy <= 7; xy++) // nach LinksOben
|
||||
{
|
||||
if (location.X - xy < 0 || (int)location.Y + xy > 7)
|
||||
break;
|
||||
if (fields[(int)location.X - xy, (int)location.Y + xy].IsEmpty)
|
||||
{
|
||||
targetFields.Add(new Vector(location.X - xy, (int)location.Y + xy), PieceTeam.Ally);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fields[(int)location.X - xy, (int)location.Y + xy].currentPiece.color != color)
|
||||
targetFields.Add(new Vector(location.X - xy, (int)location.Y + xy), PieceTeam.Enemy);
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int xy = 1; xy <= 7; xy++) // nach RechtsUnten
|
||||
{
|
||||
if (location.X + xy > 7 || (int)location.Y - xy < 0)
|
||||
break;
|
||||
if (fields[xy + (int)location.X, (int)location.Y - xy].IsEmpty)
|
||||
{
|
||||
targetFields.Add(new Vector(xy + (int)location.X, (int)location.Y - xy), PieceTeam.Ally);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fields[xy + (int)location.X, (int)location.Y - xy].currentPiece.color != color)
|
||||
targetFields.Add(new Vector(xy + (int)location.X, (int)location.Y - xy), PieceTeam.Enemy);
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int xy = 1; xy <= 7; xy++) // nach LinksUnten
|
||||
{
|
||||
if (location.X - xy < 0 || (int)location.Y - xy < 0)
|
||||
break;
|
||||
if (fields[(int)location.X - xy, (int)location.Y - xy].IsEmpty)
|
||||
{
|
||||
targetFields.Add(new Vector(location.X - xy, (int)location.Y - xy), PieceTeam.Ally);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fields[(int)location.X - xy, (int)location.Y - xy].currentPiece.color != color)
|
||||
targetFields.Add(new Vector(location.X - xy, (int)location.Y - xy), PieceTeam.Enemy);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return targetFields;
|
||||
}
|
||||
} // FINALIZED
|
||||
public class Queen : Piece
|
||||
{
|
||||
public Queen(PieceColor color)
|
||||
{
|
||||
type = (PieceType)Enum.Parse(typeof(PieceType), this.GetType().Name);
|
||||
this.color = color;
|
||||
this.image = (Image)ChessPanel.Properties.Resources.ResourceManager.GetObject($"{this.type.ToString()}{this.color.ToString()}");
|
||||
}
|
||||
|
||||
internal override Dictionary<Vector, PieceTeam> GetValidTargetFields(Field[,] fields, Vector location, bool ignoreKingCheck = false, bool testKingDanger = false)
|
||||
{
|
||||
Dictionary<Vector, PieceTeam> targetFields = Rook.GetTargetFields(fields, location, this.color);
|
||||
targetFields = targetFields.Concat(Bishop.GetTargetFields(fields, location, this.color)
|
||||
.Where(kvp => !targetFields.ContainsKey(kvp.Key)))
|
||||
.OrderBy(c => c.Value)
|
||||
.ToDictionary(c => c.Key, c => c.Value);
|
||||
/*if (!testKingDanger)
|
||||
foreach (var item in targetFields)
|
||||
if (MoveWillEndangerKing(fields, location, item.Key, this.color))
|
||||
targetFields.Remove(item.Key);*/
|
||||
return targetFields;
|
||||
}
|
||||
} // FINALIZED
|
||||
public class King : Piece
|
||||
{
|
||||
public King(PieceColor color)
|
||||
{
|
||||
type = (PieceType)Enum.Parse(typeof(PieceType), this.GetType().Name);
|
||||
this.color = color;
|
||||
this.image = (Image)ChessPanel.Properties.Resources.ResourceManager.GetObject($"{this.type.ToString()}{this.color.ToString()}");
|
||||
}
|
||||
|
||||
internal override Dictionary<Vector, PieceTeam> GetValidTargetFields(Field[,] fields, Vector location, bool ignoreKingCheck = false, bool testKingDanger = false) // ignoreKingCheck hat keinen Einfluss
|
||||
{
|
||||
return GetTargetFields(fields, location, this.color);
|
||||
}
|
||||
|
||||
private Dictionary<Vector, PieceTeam> GetTargetFields(Field[,] fields, Vector location, PieceColor color)
|
||||
{
|
||||
Dictionary<Vector, PieceTeam> targetFields = new Dictionary<Vector, PieceTeam>();
|
||||
#region Castling
|
||||
if (!this.HasMoved) // Only Castle if King did not move
|
||||
foreach (var item in fields)
|
||||
{
|
||||
var itemLocation = item.location;
|
||||
// Sucht nach Ally Turm und prüft, ob er sich schon bewegt hat
|
||||
if (item.currentPiece != null && item.currentPiece.type == PieceType.Rook && !item.currentPiece.HasMoved && item.currentPiece.color == this.color)
|
||||
{
|
||||
if (itemLocation.X == 0 || itemLocation.X == 7)
|
||||
{
|
||||
bool castling = true;
|
||||
for (int x = (itemLocation.X == 0 ? 1 : 5); (itemLocation.X == 0 ? x < 4 : x < 7); x++)
|
||||
{
|
||||
if (!fields[x, (int)location.Y].IsEmpty)
|
||||
{
|
||||
castling = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (castling) // Only Check endangered Fields if castling is generally possible
|
||||
{
|
||||
for (int x = (itemLocation.X == 0 ? 2 : 4); x < (itemLocation.X == 0 ? 5 : 7); x++) // König steht im Schach oder überschreitet bedrohtes Feld
|
||||
{
|
||||
if (IsEndangeredLocationForPiece(fields, location, new Vector(x, location.Y), color))
|
||||
{
|
||||
castling = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (castling)
|
||||
{
|
||||
targetFields.Add(new Vector((itemLocation.X == 0 ? 2 : 6), itemLocation.Y), PieceTeam.Ally);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region Movement: 1 in every direction
|
||||
for (int x = -1; x < 2; x++)
|
||||
{
|
||||
for (int y = -1; y < 2; y++)
|
||||
{
|
||||
if ((x == 0 && y == 0) || location.X + x > 7 || location.Y + y > 7 || location.X + x < 0 || location.Y + y < 0)
|
||||
continue;
|
||||
if (!IsEndangeredLocationForPiece(fields, location, new Vector(location.X + x, location.Y + y), color))
|
||||
if (fields[(int)location.X + x, (int)location.Y + y].IsEmpty)
|
||||
{
|
||||
targetFields.Add(new Vector(location.X + x, (int)location.Y + y), PieceTeam.Ally);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fields[(int)location.X + x, (int)location.Y + y].currentPiece.color != color)
|
||||
targetFields.Add(new Vector(location.X + x, (int)location.Y + y), PieceTeam.Enemy);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
return targetFields;
|
||||
}
|
||||
} // CHECKMATE/CHECK-TEST MISSING
|
||||
}
|
19
ChessPanel/Program.cs
Normal file
19
ChessPanel/Program.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Chess
|
||||
{
|
||||
static class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// Der Haupteinstiegspunkt für die Anwendung.
|
||||