Skip to content

Commit

Permalink
Final update, finished!
Browse files Browse the repository at this point in the history
  • Loading branch information
krestenlaust committed Feb 20, 2021
1 parent 1d594c0 commit 74b7d5d
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 122 deletions.
Binary file modified .vs/ChessGame/v16/.suo
Binary file not shown.
31 changes: 0 additions & 31 deletions ChessBots/SimpletronBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,41 +81,10 @@ private void CheckMovesDeep(Chessboard boardReadonly, int depth, ConcurrentBag<(
Task.WaitAll(rootMoves.ToArray());
}

/*
private void CheckMovesDeep(Chessboard boardReadonly, int depth, ConcurrentBag<(int, Move)> moves, Move baseMove=null)
{
Chessboard board = new Chessboard(boardReadonly);
if (depth == 0)
{
board.SimulateMove(FindBestMoves(board, boardReadonly.CurrentTurn)[0]);
moves.Add((board.MaterialSum, baseMove));
return;
}
foreach (var move in board.GetMoves(board.CurrentTurn))
{
Chessboard newBoard = new Chessboard(board);
newBoard.SimulateMove(move);
if (baseMove is null)
{
CheckMovesDeep(newBoard, depth - 1, moves, move);
}
else
{
CheckMovesDeep(newBoard, depth - 1, moves, baseMove);
}
}
}*/

private Move GenerateMove(Chessboard board)
{
ConcurrentBag<(int, Move)> longerMoves = new ConcurrentBag<(int, Move)>();

//List<(int, Move)> longerMoves = new List<(int, Move)>();

//CheckMovesDeep(board, 3, board.CurrentTurn, longerMoves);
CheckMovesDeep(board, 2, longerMoves);

List<(int, Move)> sortedMoves = longerMoves.OrderByDescending(material => material.Item1).ToList();
Expand Down
1 change: 0 additions & 1 deletion ChessBots/SkakinatorLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ where Math.Round(moveEvaluation.Item1, 1) == bestEvaluation

// Clean-up
transpositionTable.Clear();
//GC.Collect();

return sortedMoves[0];
}
Expand Down
35 changes: 3 additions & 32 deletions ChessForms/BoardDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ public partial class BoardDisplay : Form
private static readonly Color CheckedSquareColor = Color.DarkRed;

private readonly Gamemode gamemode;
private readonly bool whiteLocal, blackLocal;
private TilePictureControl[,] boardcells;
private Coordinate? fromPosition = null;
private Piece selectedPiece;
private Chessboard chessboard;
private readonly bool whiteLocal, blackLocal;
private bool unFlipped = false;
private Coordinate? recentMoveFrom = null;
private Coordinate? recentMoveTo = null;

//Sound
private System.Media.SoundPlayer soundPlayer = new System.Media.SoundPlayer(Properties.Resources.SkakLydfil);

public BoardDisplay(Gamemode gamemode, bool whiteLocal, bool blackLocal)
Expand All @@ -41,9 +38,6 @@ public BoardDisplay(Gamemode gamemode, bool whiteLocal, bool blackLocal)
this.gamemode = gamemode;
this.whiteLocal = whiteLocal;
this.blackLocal = blackLocal;

// flip board if black is only local player
unFlipped = !(blackLocal && !whiteLocal);
}

private void BoardDisplay_Load(object sender, EventArgs e)
Expand Down Expand Up @@ -139,7 +133,6 @@ private void onTurnStarted()
if (MatchMaker.PlaySoundOnMove)
{
soundPlayer.Play();
//Console.Beep();
}

UpdateBoard();
Expand All @@ -158,14 +151,7 @@ private void onTurnStarted()

private TilePictureControl GetCell(int x, int y)
{
if (unFlipped)
{
return boardcells[chessboard.Width - 1 - x, chessboard.Height - 1 - y];
}
else
{
return boardcells[x, y];
}
return boardcells[x, y];
}

/// <summary>
Expand Down Expand Up @@ -228,15 +214,6 @@ public void UpdateBoard()
{
Coordinate pieceCoordinate;

/*if (unFlipped)
{
pieceCoordinate = new Coordinate(chessboard.Width - 1 - x, chessboard.Height - 1 - y); //Det her burde også fikse noget
}
else
{
pieceCoordinate = new Coordinate(x, y);
}*/

pieceCoordinate = new Coordinate(x, y);

Piece cellPiece = chessboard.GetPiece(pieceCoordinate);
Expand Down Expand Up @@ -312,7 +289,7 @@ public void InstantiateUIBoard()
{
Label label = new Label
{
Text = (chessboard.Height - y).ToString(),
Text = (y + 1).ToString(),
TextAlign = ContentAlignment.MiddleCenter,
Dock = DockStyle.Fill,
Font = labelFont
Expand Down Expand Up @@ -427,12 +404,6 @@ private void CellClicked(object sender, EventArgs e)
int cellX = windowX / ((tableLayoutPanel1.Width - CoordinateSystemPixelSize) / chessboard.Width);
int cellY = windowY / ((tableLayoutPanel1.Height - CoordinateSystemPixelSize) / chessboard.Height);

if (unFlipped)
{
cellY = (chessboard.Height - 1) - cellY;
cellX = (chessboard.Width - 1) - cellX;
}

Coordinate clickTarget = new Coordinate(cellX, cellY);

// handle click
Expand Down
35 changes: 0 additions & 35 deletions ChessGame/Coordinate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,6 @@ public Coordinate(string notation)
Rank = (byte)(notation[1] - 49);
}

/* // proposed null functionality
[DebuggerStepThrough]
public static bool operator ==(Coordinate coordinate1, Coordinate coordinate2)
{
// this is way more complicated than I imagined,
// it's supposed to return true if all non-null equals withn their property group
// E.g.
// (null, 5) == (null, 5) == true
// (null, 5) == (3, 5) == true
// (null, null) == (3, 5) == false
// (null, 5) == (5, null) == false
// logic:
// (null, n) == (null, n) == true
// (n1, n2) == (n1, n2) == true
// (n1, null) == (n1, n2) == true
// all are ambigous, no number is the same.
if (coordinate1.File is null && coordinate1.Rank is null || coordinate2.File is null && coordinate2.Rank is null)
{
return false;
}
// file is null, compare rank.
if (coordinate1.File is null)
{
return coordinate1.Rank == coordinate2.Rank;
}
else if (coordinate1.Rank is null) // rank is null, compare file.
{
return coordinate1.File == coordinate1.Rank;
}
return coordinate1.File == coordinate2.File && coordinate1.Rank == coordinate2.Rank;
}*/

[DebuggerStepThrough]
public static bool operator !=(Coordinate coordinate1, Coordinate coordinate2) => coordinate1.Rank != coordinate2.Rank || coordinate1.File != coordinate2.File;

Expand Down
1 change: 0 additions & 1 deletion ChessGame/Gamemodes/PawnTestChess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public override Chessboard GenerateBoard()
board[new Coordinate(5, 3)] = new Pawn { Color = TeamColor.White };
board[new Coordinate(6, 3)] = new Pawn { Color = TeamColor.White };
board[new Coordinate(7, 3)] = new Pawn { Color = TeamColor.White };
//board[new Coordinate(2, 3)] = new Hypnotist { Color = TeamColor.White };

board[new Coordinate(0, 6)] = new Pawn { Color = TeamColor.Black };
board[new Coordinate(1, 6)] = new Pawn { Color = TeamColor.Black };
Expand Down
20 changes: 12 additions & 8 deletions ChessGame/MovementPatterns/CardinalPattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class CardinalPattern : IMovementPattern
{
public IEnumerable<Move> GetMoves(Piece piece, Coordinate position, Chessboard board, bool guardedSquaresOnly = false)
{
for (int n = 0; n < 4; n++) //The 4 directions from the piece
for (int n = 0; n < 4; n++) // The 4 directions from the piece
{
int Xdir = 1;
int Ydir = 1;
switch (n) //Sets a direction for the checker
switch (n) // Sets a direction for the checker
{
case 0:
Xdir = 1;
Expand All @@ -36,18 +36,21 @@ public IEnumerable<Move> GetMoves(Piece piece, Coordinate position, Chessboard b
break;
}

for (int i = 1; i < board.Height * board.Width; i++) //Checker
for (int i = 1; i < board.Height * board.Width; i++) // Checker
{
Coordinate checkPosition = new Coordinate((i * Xdir) + position.File, (i * Ydir) + position.Rank); //Position update
// Position update
Coordinate checkPosition = new Coordinate((i * Xdir) + position.File, (i * Ydir) + position.Rank);

// If the checking position is outside of the board
if (checkPosition.Rank >= board.Height || checkPosition.Rank < 0 ||
checkPosition.File >= board.Width || checkPosition.File < 0) //If the checking position is outside of the board
checkPosition.File >= board.Width || checkPosition.File < 0)
break;

// whether the position is occupied.
Piece occupyingPiece = board.GetPiece(checkPosition);

if (occupyingPiece is null) // is position empty?
// is position empty?
if (occupyingPiece is null)
{
yield return new Move(checkPosition, position, piece, false, piece.Color);
continue;
Expand All @@ -58,9 +61,10 @@ public IEnumerable<Move> GetMoves(Piece piece, Coordinate position, Chessboard b
break;
}

if (occupyingPiece.Color != piece.Color || guardedSquaresOnly) // There is a enemy piece
// There is a enemy piece
if (occupyingPiece.Color != piece.Color || guardedSquaresOnly)
{
yield return new Move(checkPosition, position, piece, true, piece.Color); //Sends the move
yield return new Move(checkPosition, position, piece, true, piece.Color); // Sends the move
}

break;
Expand Down
12 changes: 6 additions & 6 deletions ChessGame/MovementPatterns/DiagonalPattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class DiagonalPattern : IMovementPattern
{
public IEnumerable<Move> GetMoves(Piece piece, Coordinate position, Chessboard board, bool guardedSquaresOnly = false)
{
for (int n = 0; n < 4; n++) //The 4 directions from the piece
for (int n = 0; n < 4; n++) // The 4 directions from the piece
{
int Xdir = 1;
int Ydir = 1;
switch (n) //Sets a direction for the checker
switch (n) // Sets a direction for the checker
{
case 0:
Xdir = 1;
Expand All @@ -36,12 +36,12 @@ public IEnumerable<Move> GetMoves(Piece piece, Coordinate position, Chessboard b
break;
}

for (int i = 1; i < board.Width; i++) //Checker
for (int i = 1; i < board.Width; i++) // Checker
{
Coordinate checkPosition = new Coordinate((i * Xdir) + position.File, (i * Ydir) + position.Rank); //Position update
Coordinate checkPosition = new Coordinate((i * Xdir) + position.File, (i * Ydir) + position.Rank); // Position update

if (checkPosition.Rank >= board.Height || checkPosition.Rank < 0 ||
checkPosition.File >= board.Width || checkPosition.File < 0) //If the checking position is outside of the board
checkPosition.File >= board.Width || checkPosition.File < 0) // If the checking position is outside of the board
continue;

// whether the position is occupied.
Expand All @@ -60,7 +60,7 @@ public IEnumerable<Move> GetMoves(Piece piece, Coordinate position, Chessboard b

if (occupyingPiece.Color != piece.Color) // There is a enemy piece
{
yield return new Move(checkPosition, position, piece, true, piece.Color); //Sends the move
yield return new Move(checkPosition, position, piece, true, piece.Color); // Sends the move
}

break;
Expand Down
16 changes: 8 additions & 8 deletions ChessGame/MovementPatterns/KnightPattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ public class KnightPattern : IMovementPattern
{
public IEnumerable<Move> GetMoves(Piece piece, Coordinate position, Chessboard board, bool guardedSquaresOnly = false)
{
for (int n = 0; n < 4; n++) //The 4 directions from the piece
for (int n = 0; n < 4; n++) // The 4 directions from the piece
{
int Xdir = 1;
int Ydir = 1;
switch (n) //Sets a direction for the checker
switch (n) // Sets a direction for the checker
{
case 0:
Xdir = 2;
Expand All @@ -34,16 +34,16 @@ public IEnumerable<Move> GetMoves(Piece piece, Coordinate position, Chessboard b

for (int i = 0; i < 2; i++)
{
//First check
Coordinate checkPosition = new Coordinate(Xdir + position.File, Ydir + position.Rank); //Position update
// First check
Coordinate checkPosition = new Coordinate(Xdir + position.File, Ydir + position.Rank); // Position update

if (i != 0) //Second check
if (i != 0) // Second check
{
checkPosition = new Coordinate(-Xdir + position.File, -Ydir + position.Rank); //Position update
checkPosition = new Coordinate(-Xdir + position.File, -Ydir + position.Rank); // Position update
}

if (checkPosition.Rank >= board.Height || checkPosition.Rank < 0 ||
checkPosition.File >= board.Width || checkPosition.File < 0) //If the checking position is outside of the board
checkPosition.File >= board.Width || checkPosition.File < 0) // If the checking position is outside of the board
continue;

// whether the position is occupied.
Expand All @@ -57,7 +57,7 @@ public IEnumerable<Move> GetMoves(Piece piece, Coordinate position, Chessboard b

if (occupyingPiece.Color != piece.Color) // There is a enemy piece
{
yield return new Move(checkPosition, position, piece, true, piece.Color); //Sends the move
yield return new Move(checkPosition, position, piece, true, piece.Color); // Sends the move
}
}
}
Expand Down

0 comments on commit 74b7d5d

Please sign in to comment.