Skip to content

Commit

Permalink
Chess Challenge Project
Browse files Browse the repository at this point in the history
  • Loading branch information
SebLague committed Jul 21, 2023
1 parent 29e522d commit a268bc3
Show file tree
Hide file tree
Showing 60 changed files with 7,316 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[*.cs]

# CS8602: Dereference of a possibly null reference.
dotnet_diagnostic.CS8602.severity = silent
# CS8618: Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
dotnet_diagnostic.CS8618.severity = silent
30 changes: 30 additions & 0 deletions Chess-Challenge.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32901.215
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Chess-Challenge", "Chess-Challenge\Chess-Challenge.csproj", "{2803E64F-15AC-430B-A5A2-69C00EA7505F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9FC5D849-F0B6-4C89-A541-C36A341AE67C}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2803E64F-15AC-430B-A5A2-69C00EA7505F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2803E64F-15AC-430B-A5A2-69C00EA7505F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2803E64F-15AC-430B-A5A2-69C00EA7505F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2803E64F-15AC-430B-A5A2-69C00EA7505F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DC54E848-1F03-426F-9B00-9CBC391363F6}
EndGlobalSection
EndGlobal
60 changes: 60 additions & 0 deletions Chess-Challenge/Chess-Challenge.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Chess_Challenge</RootNamespace>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Raylib-cs" Version="4.5.0.2" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.CodeAnalysis">
<HintPath>src\Token Counter\Microsoft.CodeAnalysis.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CodeAnalysis.CSharp">
<HintPath>src\Token Counter\Microsoft.CodeAnalysis.CSharp.dll</HintPath>
</Reference>
</ItemGroup>

<ItemGroup>
<Compile Update="src\My Bot\MyBot.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="src\Framework\Application\UI\BotBrainCapacityUI.cs">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Compile>
<Compile Update="src\Evil Bot\EvilBot.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
</ItemGroup>

<ItemGroup>
<None Update="resources\Fens.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="resources\Fonts\OPENSANS-SEMIBOLD.TTF">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="resources\Fonts\sdf.fs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="resources\Pieces.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="src\Framework\Chess\" />
</ItemGroup>

<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup>

</Project>
500 changes: 500 additions & 0 deletions Chess-Challenge/resources/Fens.txt

Large diffs are not rendered by default.

Binary file not shown.
26 changes: 26 additions & 0 deletions Chess-Challenge/resources/Fonts/sdf.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#version 330

// Input vertex attributes (from vertex shader)
in vec2 fragTexCoord;
in vec4 fragColor;

// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;

// Output fragment color
out vec4 finalColor;

// NOTE: Add here your custom variables

void main()
{
// Texel color fetching from texture sampler
// NOTE: Calculate alpha using signed distance field (SDF)
float distanceFromOutline = texture(texture0, fragTexCoord).a - 0.5;
float distanceChangePerFragment = length(vec2(dFdx(distanceFromOutline), dFdy(distanceFromOutline)));
float alpha = smoothstep(-distanceChangePerFragment, distanceChangePerFragment, distanceFromOutline);

// Calculate final fragment color
finalColor = vec4(fragColor.rgb, fragColor.a*alpha);
}
Binary file added Chess-Challenge/resources/Pieces.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
127 changes: 127 additions & 0 deletions Chess-Challenge/src/API/BitboardHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@

namespace ChessChallenge.API
{
using ChessChallenge.Chess;

/// <summary>
/// Helper class for working with bitboards.
/// Bitboards are represented with the ulong type (unsigned 64 bit integer).
/// </summary>
public static class BitboardHelper
{
/// <summary>
/// Set the given square on the bitboard to 1.
/// </summary>
public static void SetSquare(ref ulong bitboard, Square square)
{
bitboard |= 1ul << square.Index;
}
/// <summary>
/// Clear the given square on the bitboard to 0.
/// </summary>
public static void ClearSquare(ref ulong bitboard, Square square)
{
bitboard &= ~(1ul << square.Index);
}

/// <summary>
/// Toggle the given square on the bitboard between 0 and 1.
/// </summary>
public static void ToggleSquare(ref ulong bitboard, Square square)
{
bitboard ^= 1ul << square.Index;
}

/// <summary>
/// Returns true if the given square is set to 1 on the bitboard, otherwise false.
/// </summary>
public static bool SquareIsSet(ulong bitboard, Square square)
{
return ((bitboard >> square.Index) & 1) != 0;
}

public static int ClearAndGetIndexOfLSB(ref ulong bitboard)
{
return BitBoardUtility.PopLSB(ref bitboard);
}

public static int GetNumberOfSetBits(ulong bitboard)
{
return BitBoardUtility.PopCount(bitboard);
}

/// <summary>
/// Returns a bitboard where each bit that is set to 1 represents a square that the given
/// sliding piece type is able to attack. These attacks are calculated from the given square,
/// and take the given board state into account (so attacks will be blocked by pieces that are in the way).
/// Valid only for sliding piece types (queen, rook, and bishop).
/// </summary>
public static ulong GetSliderAttacks(PieceType pieceType, Square square, Board board)
{
return pieceType switch
{
PieceType.Rook => GetRookAttacks(square, board.AllPiecesBitboard),
PieceType.Bishop => GetBishopAttacks(square, board.AllPiecesBitboard),
PieceType.Queen => GetQueenAttacks(square, board.AllPiecesBitboard),
_ => 0
};
}

/// <summary>
/// Returns a bitboard where each bit that is set to 1 represents a square that the given
/// sliding piece type is able to attack. These attacks are calculated from the given square,
/// and take the given blocker bitboard into account (so attacks will be blocked by pieces that are in the way).
/// Valid only for sliding piece types (queen, rook, and bishop).
/// </summary>
public static ulong GetSliderAttacks(PieceType pieceType, Square square, ulong blockers)
{
return pieceType switch
{
PieceType.Rook => GetRookAttacks(square, blockers),
PieceType.Bishop => GetBishopAttacks(square, blockers),
PieceType.Queen => GetQueenAttacks(square, blockers),
_ => 0
};
}
/// <summary>
/// Gets a bitboard of squares that a knight can attack from the given square.
/// </summary>
public static ulong GetKnightAttacks(Square square) => Bits.KnightAttacks[square.Index];
/// <summary>
/// Gets a bitboard of squares that a king can attack from the given square.
/// </summary>
public static ulong GetKingAttacks(Square square) => Bits.KingMoves[square.Index];

/// <summary>
/// Gets a bitboard of squares that a pawn (of the given colour) can attack from the given square.
/// </summary>
public static ulong GetPawnAttacks(Square square, bool isWhite)
{
return isWhite ? Bits.WhitePawnAttacks[square.Index] : Bits.BlackPawnAttacks[square.Index];
}
static ulong GetRookAttacks(Square square, ulong blockers)
{
ulong mask = Magic.RookMask[square.Index];
ulong magic = PrecomputedMagics.RookMagics[square.Index];
int shift = PrecomputedMagics.RookShifts[square.Index];

ulong key = ((blockers & mask) * magic) >> shift;
return Magic.RookAttacks[square.Index][key];
}

static ulong GetBishopAttacks(Square square, ulong blockers)
{
ulong mask = Magic.BishopMask[square.Index];
ulong magic = PrecomputedMagics.BishopMagics[square.Index];
int shift = PrecomputedMagics.BishopShifts[square.Index];

ulong key = ((blockers & mask) * magic) >> shift;
return Magic.BishopAttacks[square.Index][key];
}

static ulong GetQueenAttacks(Square square, ulong blockers)
{
return GetRookAttacks(square, blockers) | GetBishopAttacks(square, blockers);
}
}
}
Loading

0 comments on commit a268bc3

Please sign in to comment.