Skip to content

Commit

Permalink
Release v1.2.1 (#23)
Browse files Browse the repository at this point in the history
* Implemented new bot AI

* changes to bot ai
  • Loading branch information
omegaleo authored Jun 26, 2022
1 parent 6e9aeb5 commit 214d7cc
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 57 deletions.
117 changes: 117 additions & 0 deletions Assets/Scripts/Libraries/BotAI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public static class BotAI
{
private static List<PieceValue> values = new List<PieceValue>()
{
new PieceValue() {type = typeof(Pawn), value = 10},
new PieceValue() {type = typeof(Knight), value = 30},
new PieceValue() {type = typeof(Bishop), value = 30},
new PieceValue() {type = typeof(Rook), value = 50},
new PieceValue() {type = typeof(Queen), value = 90},
new PieceValue() {type = typeof(King), value = 100}
};

public static void Move(Color color)
{
bool sacrifice = new System.Random().Next(0, 100) <= 10;

var allPossibleMoves = PieceManager.instance.GetAllPossibleMoves(color).ToList();
var pieces = PieceManager.instance.GetPieces(color);

var bestMove = allPossibleMoves.Random();
var pieceToMove = pieces.FirstOrDefault(x => x.highlightedCells.Any(x => x == bestMove));
var bestMoveValue = GetValue(bestMove, GetMultiplier(color, bestMove, pieceToMove));

foreach (Cell cell in allPossibleMoves)
{
if (IsSameColor(color, cell) && !sacrifice)
{
continue;
}

var piece = pieces.FirstOrDefault(x => x.highlightedCells.Any(x => x == cell));

var value = GetValue(cell, GetMultiplier(color, cell, piece));

if (value > bestMoveValue)
{
bestMove = cell;
bestMoveValue = value;
pieceToMove = piece;
}
}

if (pieceToMove != null)
{
pieceToMove.targetCell = bestMove;
pieceToMove.Move();
}
}

private static int GetMultiplier(Color color, Cell cell, BasePiece piece)
{
return (IsSameColor(color, cell)
? (piece.evolved)
? -1 : 1
: 1);
}

private static bool IsSameColor(Color color, Cell cell)
{
return cell.currentPiece != null && cell.currentPiece.color == color;
}

private static int GetValue(Cell cell, int multiplier)
{
int value = 0;

if (cell.currentPiece != null)
{
var pieceValue = values.FirstOrDefault(x => x.type == cell.currentPiece.GetType());

if (pieceValue != null)
{
value = (cell.currentPiece.evolved) ? pieceValue.evolvedValue : pieceValue.value;
}
}

return value * multiplier;
}

/*void BotMove()
{
// For now, get a random piece that can move and move it to a random position
var teamPieces = (player2Color == Color.black) ? blackPieces : whitePieces;
var pieces = teamPieces.Where(x => x.IsAlive()).ToList();
pieces.ForEach(x => x.CheckPathing());
var piece = pieces.Where(x => x.highlightedCells.Any()).ToList().Random();
if (piece != null)
{
piece.targetCell = piece.highlightedCells.Random();
piece.Move();
}
SwitchSides(player2Color);
}*/
}

public class PieceValue
{
public Type type;
public int value;

public int evolvedValue
{
get
{
return Mathf.CeilToInt(value * 1.5f); // Return 150% of the piece's value if evolved
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Libraries/BotAI.cs.meta

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

63 changes: 32 additions & 31 deletions Assets/Scripts/PieceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,30 +194,11 @@ public void SwitchSides(Color color)

if (((isBlackTurn && player2Color == Color.black) || (!isBlackTurn && player2Color == Color.white)) && GameManager.instance.botGame)
{
BotMove();
BotAI.Move(player2Color);
}
}
}

void BotMove()
{
// For now, get a random piece that can move and move it to a random position
var teamPieces = (player2Color == Color.black) ? blackPieces : whitePieces;

var pieces = teamPieces.Where(x => x.IsAlive()).ToList();

pieces.ForEach(x => x.CheckPathing());
var piece = pieces.Where(x => x.highlightedCells.Any()).ToList().Random();

if (piece != null)
{
piece.targetCell = piece.highlightedCells.Random();
piece.Move();
}

SwitchSides(player2Color);
}

public void ResetGame()
{
foreach (BasePiece piece in promotedPieces)
Expand Down Expand Up @@ -287,16 +268,10 @@ public void PromotePiece(Pawn pawn, Cell cell, Color teamColor, string piece)
promotedPieces.Add(promotedPiece);
}

public void UpdateIsChecked(Color teamColor)
public void UpdatePaths()
{
if (teamColor == Color.black)
{
whitePieces.ForEach(x => x.CheckPathing());
}
else
{
blackPieces.ForEach(x => x.CheckPathing());
}
whitePieces.ForEach(x => x.CheckPathing());
blackPieces.ForEach(x => x.CheckPathing());
}

private List<Cell> IterateCells(int xPos, int yPos, int xIncrement, int yIncrement, int count)
Expand Down Expand Up @@ -343,8 +318,8 @@ public List<Cell> CheckingCellPath(Cell checkingPieceCell, King king)
public IEnumerable<Cell> GetCheckingCells(Color color)
{
return (color == Color.black)
? whitePieces.Where(x => x.isChecking && x.IsAlive() && x.GetType() != typeof(Pawn)).SelectMany(x => CheckingCellPath(x.CurrentCell, GetKing(Color.black)))
: blackPieces.Where(x => x.isChecking && x.IsAlive() && x.GetType() != typeof(Pawn)).SelectMany(x => CheckingCellPath(x.CurrentCell, GetKing(Color.white)));
? whitePieces.Where(x => x.isChecking && x.IsAlive()).SelectMany(x => CheckingCellPath(x.CurrentCell, GetKing(Color.black)))
: blackPieces.Where(x => x.isChecking && x.IsAlive()).SelectMany(x => CheckingCellPath(x.CurrentCell, GetKing(Color.white)));
}

public bool CanCheckmateBePrevented(Color teamColor)
Expand All @@ -358,6 +333,20 @@ public bool CanCheckmateBePrevented(Color teamColor)
return countInterceptions > 0;
}

public IEnumerable<Cell> GetAllPossibleMoves(Color color)
{
UpdatePaths();

return GetPieces(color).SelectMany(x => x.highlightedCells);
}

public IEnumerable<BasePiece> GetPieces(Color color)
{
return (color == Color.black)
? blackPieces.Where(x => x.IsAlive())
: whitePieces.Where(x => x.IsAlive());
}

/// <summary>
/// Get a list of all possible movement that a friendly piece might do
/// </summary>
Expand Down Expand Up @@ -420,4 +409,16 @@ public List<BasePiece> GetRooks(Color teamColor)
return whitePieces.Where(x => x.GetType() == typeof(Rook) && x.IsAlive()).ToList();
}
}

/// <summary>
/// Used to evaluate if there's an evolved queen and she's going for the 2nd move
/// </summary>
/// <param name="teamColor"></param>
/// <returns></returns>
public bool EvolvedQueenSecondMove(Color teamColor)
{
return (teamColor == Color.black)
? blackPieces.Any(x => x.GetType() == typeof(Queen) && x.evolved && x.move < 2)
: whitePieces.Any(x => x.GetType() == typeof(Queen) && x.evolved && x.move < 2);
}
}
45 changes: 21 additions & 24 deletions Assets/Scripts/Pieces/BasePiece.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ public virtual void Evolve()

public virtual void CheckPathing()
{
if (PieceManager.instance.EvolvedQueenSecondMove(color) && (GetType() != typeof(Queen) || !evolved))
{
return;
}

isChecking = false;
CurrentCell.outlineImage.enabled = false;
highlightedCells.Clear();
Expand All @@ -223,7 +228,7 @@ public virtual void CheckPathing()
{
return;
}

// Horizontal
CreateCellPath(1, 0, movement.x);
CreateCellPath(-1, 0, movement.x);
Expand Down Expand Up @@ -306,8 +311,21 @@ protected virtual void ExecuteMovement(Cell cell)
{
if (cell == null)
return;

SFXManager.instance.Play("pieceMoveSFX");

CellState state = CellState.None;
state = Board.instance.ValidateCell(targetCell.boardPosition.x, targetCell.boardPosition.y, this);

if (state == CellState.Friendly)
{
// Sacrifice a piece
var piece = targetCell.currentPiece;

this.level += piece.level;
CheckEvolved();
}


cell.RemovePiece();

Expand All @@ -319,8 +337,7 @@ protected virtual void ExecuteMovement(Cell cell)
transform.position = CurrentCell.transform.position;

// Check if any of the kings is in check
PieceManager.instance.UpdateIsChecked(Color.white);
PieceManager.instance.UpdateIsChecked(Color.black);
PieceManager.instance.UpdatePaths();

targetCell = null;

Expand Down Expand Up @@ -438,27 +455,7 @@ public override void OnEndDrag(PointerEventData eventData)
{
return;
}

CellState state = CellState.None;
state = Board.instance.ValidateCell(targetCell.boardPosition.x, targetCell.boardPosition.y, this);

if (state == CellState.Friendly)
{
// Sacrifice a piece
var piece = targetCell.currentPiece;

if (IsValidMovement(piece))
{
this.level += piece.level;
CheckEvolved();
}
else
{
transform.position = CurrentCell.gameObject.transform.position;
return;
}
}

Move();
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Pieces/King.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void TrySwitchRook()
CurrentCell.checkedImage.enabled = false;
rook.CurrentCell.checkedImage.enabled = false;

PieceManager.instance.UpdateIsChecked(color);
PieceManager.instance.UpdatePaths();
IsChecked();
}
}
Expand Down
5 changes: 5 additions & 0 deletions Assets/Scripts/Pieces/Pawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ private bool MatchesState(int targetX, int targetY, CellState targetState)

public override void CheckPathing()
{
if (PieceManager.instance.EvolvedQueenSecondMove(color))
{
return;
}

highlightedCells.Clear();
if (evolved)
{
Expand Down
3 changes: 3 additions & 0 deletions Assets/Scripts/ScriptableObjects.meta

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

13 changes: 13 additions & 0 deletions Assets/Scripts/ScriptableObjects/Puzzle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class Puzzle:ScriptableObject
{
public Sprite image;
public string puzzleName;
public List<BasePiece> whitePieces;
public List<BasePiece> blackPieces;
public Color playerStartColor;
}
3 changes: 3 additions & 0 deletions Assets/Scripts/ScriptableObjects/Puzzle.cs.meta

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

2 changes: 1 addition & 1 deletion ProjectSettings/RiderScriptEditorPersistedState.asset
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ MonoBehaviour:
m_Script: {fileID: 0}
m_Name:
m_EditorClassIdentifier: Unity.Rider.Editor:Packages.Rider.Editor:RiderScriptEditorPersistedState
lastWriteTicks: -8585453646923732384
lastWriteTicks: -8585453527730719902

0 comments on commit 214d7cc

Please sign in to comment.