Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Breaking out the library from the challenge. #496

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,8 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml
.idea/.idea.Chess-Challenge/.idea/.gitignore
.idea/.idea.Chess-Challenge/.idea/.name
.idea/.idea.Chess-Challenge/.idea/encodings.xml
.idea/.idea.Chess-Challenge/.idea/indexLayout.xml
.idea/.idea.Chess-Challenge/.idea/vcs.xml
18 changes: 12 additions & 6 deletions Chess-Challenge.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@ 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
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChessEngine", "ChessEngine\ChessEngine.csproj", "{BB20BD5E-BC39-43AF-AB33-F7DD3139B531}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChessChallenge", "ChessChallenge\ChessChallenge.csproj", "{0B1CE8E2-50C2-4316-8F0D-92119649D6FF}"
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
{BB20BD5E-BC39-43AF-AB33-F7DD3139B531}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB20BD5E-BC39-43AF-AB33-F7DD3139B531}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB20BD5E-BC39-43AF-AB33-F7DD3139B531}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB20BD5E-BC39-43AF-AB33-F7DD3139B531}.Release|Any CPU.Build.0 = Release|Any CPU
{0B1CE8E2-50C2-4316-8F0D-92119649D6FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0B1CE8E2-50C2-4316-8F0D-92119649D6FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B1CE8E2-50C2-4316-8F0D-92119649D6FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B1CE8E2-50C2-4316-8F0D-92119649D6FF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
13 changes: 0 additions & 13 deletions Chess-Challenge/src/API/PieceType.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,28 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Chess_Challenge</RootNamespace>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>ChessChallenge</RootNamespace>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<LangVersion>11</LangVersion>
</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">
<Compile Update="src\Bots\EvilBot.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="src\Bots\MyBot.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
</ItemGroup>
Expand All @@ -50,11 +44,20 @@
</ItemGroup>

<ItemGroup>
<Folder Include="src\Framework\Chess\" />
<None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup>

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

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

</Project>
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ChessChallenge.API;
using System;
using System;
using ChessEngine;

namespace ChessChallenge.Example
namespace ChessChallenge.Bots
{
// A simple bot that can spot mate in one, and always captures the most valuable piece it can.
// Plays randomly otherwise.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using ChessChallenge.API;
using ChessEngine;

public class MyBot : IChessBot
namespace ChessChallenge.Bots;

public class DefaultBot : IChessBot
{
public Move Think(Board board, Timer timer)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
using ChessChallenge.Chess;
using ChessChallenge.Example;
using Raylib_cs;
using System;
using System;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static ChessChallenge.Application.Settings;
using static ChessChallenge.Application.ConsoleHelper;

namespace ChessChallenge.Application
using ChessChallenge.Bots;
using ChessChallenge.Framework.Application.Helpers;
using ChessChallenge.Framework.Application.Players;
using ChessChallenge.Framework.Application.TokenCounting;
using ChessChallenge.Framework.Application.UI;
using Raylib_cs;
using static ChessChallenge.Framework.Application.Core.Settings;
using static ChessChallenge.Framework.Application.Helpers.ConsoleHelper;
using ChessEngine;
using ChessEngine.Internal.Helpers;
using ChessEngine.Internal.MoveGeneration;
using ChessEngine.Internal.Result;
using Board = ChessEngine.Internal.Board.Board;

namespace ChessChallenge.Framework.Application.Core
{
public class ChallengeController
{
Expand All @@ -32,7 +40,7 @@ public enum PlayerType

float lastMoveMadeTime;
bool isWaitingToPlayMove;
Move moveToPlay;
ChessEngine.Internal.Board.Move moveToPlay;
float playMoveTime;
public bool HumanWasWhiteLastGame { get; private set; }

Expand All @@ -58,7 +66,7 @@ public enum PlayerType

public ChallengeController()
{
Log($"Launching Chess-Challenge version {Settings.Version}");
Log($"Launching ChessChallenge version {Settings.Version}");
(tokenCount, debugTokenCount) = GetTokenCount();
Warmer.Warm();

Expand Down Expand Up @@ -141,22 +149,22 @@ void BotThinkerThread()
//Console.WriteLine("Exitting thread: " + threadID);
}

Move GetBotMove()
ChessEngine.Internal.Board.Move GetBotMove()
{
API.Board botBoard = new(board);
ChessEngine.Board botBoard = new(board);
try
{
API.Timer timer = new(PlayerToMove.TimeRemainingMs, PlayerNotOnMove.TimeRemainingMs, GameDurationMilliseconds, IncrementMilliseconds);
API.Move move = PlayerToMove.Bot.Think(botBoard, timer);
return new Move(move.RawValue);
ChessEngine.Timer timer = new(PlayerToMove.TimeRemainingMs, PlayerNotOnMove.TimeRemainingMs, GameDurationMilliseconds, IncrementMilliseconds);
Move move = PlayerToMove.Bot.Think(botBoard, timer);
return new ChessEngine.Internal.Board.Move(move.RawValue);
}
catch (Exception e)
{
Log("An error occurred while bot was thinking.\n" + e.ToString(), true, ConsoleColor.Red);
hasBotTaskException = true;
botExInfo = ExceptionDispatchInfo.Capture(e);
}
return Move.NullMove;
return ChessEngine.Internal.Board.Move.NullMove;
}


Expand Down Expand Up @@ -194,36 +202,36 @@ void SetBoardPerspective()
boardUI.SetPerspective(PlayerWhite.IsHuman);
HumanWasWhiteLastGame = PlayerWhite.IsHuman;
}
else if (PlayerWhite.Bot is MyBot && PlayerBlack.Bot is MyBot)
else if (PlayerWhite.Bot is DefaultBot && PlayerBlack.Bot is DefaultBot)
{
boardUI.SetPerspective(true);
}
else
{
boardUI.SetPerspective(PlayerWhite.Bot is MyBot);
boardUI.SetPerspective(PlayerWhite.Bot is DefaultBot);
}
}

ChessPlayer CreatePlayer(PlayerType type)
{
return type switch
{
PlayerType.MyBot => new ChessPlayer(new MyBot(), type, GameDurationMilliseconds),
PlayerType.MyBot => new ChessPlayer(new DefaultBot(), type, GameDurationMilliseconds),
PlayerType.EvilBot => new ChessPlayer(new EvilBot(), type, GameDurationMilliseconds),
_ => new ChessPlayer(new HumanPlayer(boardUI), type)
};
}

static (int totalTokenCount, int debugTokenCount) GetTokenCount()
{
string path = Path.Combine(Directory.GetCurrentDirectory(), "src", "My Bot", "MyBot.cs");
string path = Path.Combine(Directory.GetCurrentDirectory(), "src", "Bots", "MyBot.cs");

using StreamReader reader = new(path);
string txt = reader.ReadToEnd();
return TokenCounter.CountTokens(txt);
}

void OnMoveChosen(Move chosenMove)
void OnMoveChosen(ChessEngine.Internal.Board.Move chosenMove)
{
if (IsLegal(chosenMove))
{
Expand All @@ -249,7 +257,7 @@ void OnMoveChosen(Move chosenMove)
}
}

void PlayMove(Move move)
void PlayMove(ChessEngine.Internal.Board.Move move)
{
if (isPlaying)
{
Expand Down Expand Up @@ -424,7 +432,7 @@ public void StartNewBotMatch(PlayerType botTypeA, PlayerType botTypeB)
public string AllPGNs => pgns.ToString();


bool IsLegal(Move givenMove)
bool IsLegal(ChessEngine.Internal.Board.Move givenMove)
{
var moves = moveGenerator.GenerateMoves(board);
foreach (var legalMove in moves)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Raylib_cs;
using System.IO;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using ChessChallenge.Framework.Application.Helpers;
using Raylib_cs;

namespace ChessChallenge.Application
namespace ChessChallenge.Framework.Application.Core
{
static class Program
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Numerics;

namespace ChessChallenge.Application
namespace ChessChallenge.Framework.Application.Core
{
public static class Settings
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using static ChessChallenge.Application.Settings;
using static ChessChallenge.Framework.Application.Core.Settings;

namespace ChessChallenge.Application
namespace ChessChallenge.Framework.Application.Helpers
{
public static class ConsoleHelper
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System;

namespace ChessChallenge.Application
namespace ChessChallenge.Framework.Application.Helpers
{
public static class FileHelper
{
Expand Down
Loading