Skip to content

Added Hotseat Capabilities #25

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

Open
wants to merge 7 commits into
base: Chess-V1-Unity
Choose a base branch
from
Open
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
1,377 changes: 514 additions & 863 deletions Assets/Scenes/Chess.unity

Large diffs are not rendered by default.

2,900 changes: 2,900 additions & 0 deletions Assets/Scenes/StartScreen.unity

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Assets/Scenes/StartScreen.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions Assets/Scripts/Core/AI/AIDifficulty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using UnityEngine;
using Chess;

public class AIDifficulty : MonoBehaviour
{
public AISettings aiSettings;
public int difficulty;

// Call this method to set the AI settings based on the chosen difficulty
public void SetDifficulty()
{
if (difficulty == 1)
{
// Random difficulty aka easy
aiSettings.depth = 1;
aiSettings.useIterativeDeepening = false;
aiSettings.useTranspositionTable = false;
aiSettings.useThreading = false;
aiSettings.useFixedDepthSearch = false;
aiSettings.searchTimeMillis = 100;
}
else if (difficulty == 2)
{
// Weakened difficulty aka medium
aiSettings.depth = 2;
aiSettings.useIterativeDeepening = true;
aiSettings.useTranspositionTable = true;
aiSettings.useThreading = true;
aiSettings.useFixedDepthSearch = true;
aiSettings.searchTimeMillis = 500;
}
else if (difficulty == 3)
{
// Normal difficulty aka hard
aiSettings.depth = 7;
aiSettings.useIterativeDeepening = true;
aiSettings.useTranspositionTable = true;
aiSettings.useThreading = true;
aiSettings.useFixedDepthSearch = true;
aiSettings.searchTimeMillis = 1000;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Core/AI/AIDifficulty.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 92 additions & 17 deletions Assets/Scripts/Core/GameManager.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Chess.Game {
public class GameManager : MonoBehaviour {

public enum Result { Playing, WhiteIsMated, BlackIsMated, Stalemate, Repetition, FiftyMoveRule, InsufficientMaterial }

public event System.Action onPositionLoaded;
public event System.Action<Move> onMoveMade;

public enum PlayerType { Human, AI }

public bool loadCustomPosition;
Expand All @@ -27,6 +24,14 @@ public enum PlayerType { Human, AI }
public TMPro.TMP_Text aiDiagnosticsUI;
public TMPro.TMP_Text resultUI;

//Scene Control
private int playerChoice;
public AIDifficulty AIDifficulty;

//UI Control
public GameObject export;
public GameObject exit;

Result gameResult;

Player whitePlayer;
Expand All @@ -40,21 +45,77 @@ public enum PlayerType { Human, AI }
Board searchBoard; // Duplicate version of board used for ai search

void Start () {
//Application.targetFrameRate = 60;

if (useClocks) {
/*if (useClocks) {
whiteClock.isTurnToMove = false;
blackClock.isTurnToMove = false;
}
}*/

//UI Control
export.SetActive(false);
exit.SetActive(false);

//Board Set Up
boardUI = FindObjectOfType<BoardUI> ();
gameMoves = new List<Move> ();
board = new Board ();
searchBoard = new Board ();
aiSettings.diagnostics = new Search.SearchDiagnostics ();

NewGame (whitePlayerType, blackPlayerType);


//Start Control
playerChoice = PlayerPrefs.GetInt("StartValue", 0);

switch(playerChoice){
case 1:
//Hotseat
HotseatGame();
break;
case 2:
//White Easy
AIDifficulty.difficulty = 1;
AIDifficulty.SetDifficulty();
NewGame(true);
break;
case 3:
//Black
AIDifficulty.difficulty = 1;
AIDifficulty.SetDifficulty();
NewGame(false);
break;
case 4:
//AI Spectate
AIDifficulty.difficulty = 3;
AIDifficulty.SetDifficulty();
NewComputerVersusComputerGame();
break;
case 5:
//White Medium
AIDifficulty.difficulty = 2;
AIDifficulty.SetDifficulty();
NewGame(true);
break;
case 6:
//White Hard
AIDifficulty.difficulty = 3;
AIDifficulty.SetDifficulty();
NewGame(true);
break;
case 7:
//Black Medium
AIDifficulty.difficulty = 2;
AIDifficulty.SetDifficulty();
NewGame(false);
break;
case 8:
//Black Hard
AIDifficulty.difficulty = 3;
AIDifficulty.SetDifficulty();
NewGame(false);
break;
default:
Debug.LogError("Accessed Impossible Choice");
break;
}
}

void Update () {
Expand Down Expand Up @@ -91,11 +152,20 @@ void OnMoveChosen (Move move) {

public void NewGame (bool humanPlaysWhite) {
boardUI.SetPerspective (humanPlaysWhite);
aiDiagnosticsUI.enabled = true;
NewGame ((humanPlaysWhite) ? PlayerType.Human : PlayerType.AI, (humanPlaysWhite) ? PlayerType.AI : PlayerType.Human);
}

public void HotseatGame (){
boardUI.SetPerspective(true);
aiDiagnosticsUI.enabled = false;
NewGame(PlayerType.Human, PlayerType.Human);
}

public void NewComputerVersusComputerGame () {
boardUI.SetPerspective (true);
AIDifficulty.SetDifficulty();
aiDiagnosticsUI.enabled = true;
NewGame (PlayerType.AI, PlayerType.AI);
}

Expand All @@ -111,22 +181,17 @@ void NewGame (PlayerType whitePlayerType, PlayerType blackPlayerType) {
onPositionLoaded?.Invoke ();
boardUI.UpdatePosition (board);
boardUI.ResetSquareColours ();

CreatePlayer (ref whitePlayer, whitePlayerType);
CreatePlayer (ref blackPlayer, blackPlayerType);

gameResult = Result.Playing;
PrintGameResult (gameResult);

NotifyPlayerToMove ();

}

void LogAIDiagnostics () {
string text = "";
var d = aiSettings.diagnostics;
//text += "AI Diagnostics";
text += $"<color=#{ColorUtility.ToHtmlStringRGB(colors[3])}>Version 1.0\n";
text += $"<color=#{ColorUtility.ToHtmlStringRGB(colors[0])}>Depth Searched: {d.lastCompletedDepth}";
//text += $"\nPositions evaluated: {d.numPositionsEvaluated}";

Expand All @@ -140,7 +205,7 @@ void LogAIDiagnostics () {
}
evalString = ($"{displayEval:00.00}").Replace (",", ".");
if (Search.IsMateScore (d.eval)) {
evalString = $"mate in {Search.NumPlyToMateFromScore(d.eval)} ply";
evalString = $"Mate in {Search.NumPlyToMateFromScore(d.eval)} Play";
}
}
text += $"\n<color=#{ColorUtility.ToHtmlStringRGB(colors[1])}>Eval: {evalString}";
Expand Down Expand Up @@ -169,7 +234,6 @@ public void QuitGame () {
void NotifyPlayerToMove () {
gameResult = GetGameState ();
PrintGameResult (gameResult);

if (gameResult == Result.Playing) {
playerToMove = (board.WhiteToMove) ? whitePlayer : blackPlayer;
playerToMove.NotifyTurnToMove ();
Expand All @@ -185,23 +249,34 @@ void PrintGameResult (Result result) {

if (result == Result.Playing) {
resultUI.text = "";
resultUI.text += subtitleSettings + "";
} else if (result == Result.WhiteIsMated || result == Result.BlackIsMated) {
resultUI.text = "Checkmate!";
EndToggle();
} else if (result == Result.FiftyMoveRule) {
resultUI.text = "Draw";
resultUI.text += subtitleSettings + "\n(50 move rule)";
EndToggle();
} else if (result == Result.Repetition) {
resultUI.text = "Draw";
resultUI.text += subtitleSettings + "\n(3-fold repetition)";
EndToggle();
} else if (result == Result.Stalemate) {
resultUI.text = "Draw";
resultUI.text += subtitleSettings + "\n(Stalemate)";
EndToggle();
} else if (result == Result.InsufficientMaterial) {
resultUI.text = "Draw";
resultUI.text += subtitleSettings + "\n(Insufficient material)";
EndToggle();
}
}

void EndToggle(){
export.SetActive(true);
exit.SetActive(true);
}

Result GetGameState () {
MoveGenerator moveGenerator = new MoveGenerator ();
var moves = moveGenerator.GenerateMoves (board);
Expand Down
15 changes: 15 additions & 0 deletions Assets/Scripts/Core/Scene Control/StartScreenHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using UnityEngine;
using UnityEngine.SceneManagement;

public class StartScreenHandler : MonoBehaviour
{
public void OnPlayButton(int playerChoice)
{
SceneManager.LoadScene("Chess");
PlayerPrefs.SetInt("StartValue", playerChoice);
}
public void onQuitButton()
{
Application.Quit();
}
}
20 changes: 20 additions & 0 deletions Assets/Scripts/Core/StartScreenHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using UnityEngine;
using UnityEngine.SceneManagement;

public class StartScreenHandler : MonoBehaviour
{
public void OnPlayButton(int playerChoice)
{
//Player Choice Int Controls Both Difficulty, Gamemode and Color
SceneManager.LoadScene("Chess");
PlayerPrefs.SetInt("StartValue", playerChoice);
}
public void onQuitButton()
{
Application.Quit();
}
public void onHomeScreenButton()
{
SceneManager.LoadScene("StartScreen");
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Core/StartScreenHandler.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Assets/Settings/Settings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 6ea222b713a9e459caf3ca327745aa40, type: 3}
m_Name: Settings
m_EditorClassIdentifier:
depth: 5
depth: 4
useIterativeDeepening: 1
useTranspositionTable: 1
useThreading: 1
useFixedDepthSearch: 0
searchTimeMillis: 1250
useFixedDepthSearch: 1
searchTimeMillis: 1000
endlessSearchMode: 0
clearTTEachMove: 1
useBook: 1
Expand All @@ -26,8 +26,8 @@ MonoBehaviour:
promotionsToSearch: 2
diagnostics:
lastCompletedDepth: 0
moveVal:
moveVal: Bb4
move:
eval: 0
isBook: 0
isBook: 1
numPositionsEvaluated: 0
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Chess-AI
A fairly simple chess program, implementing in C# in the Unity game engine.
A fairly simple chess program, implementing in C# in the Unity game engine. Designed to be an upgrade of the Main Branches focused around making the program more of a gamelike experience.

[Watch video](https://www.youtube.com/watch?v=U4ogK0MIzqk)

[Download and play](https://sebastian.itch.io/chess-ai)
Expand Down