private void Button1_Click_1(object sender, EventArgs e) { Image image = Image.FromFile(@"chess_pieces\w_rook.png"); ChessFigure test_figure = new ChessFigure(ChessColor.white, ChessType.rook, image); chess_field.chess_cells[0, 0].CurrentChess = test_figure; }
private void MoveChessFigure(int x, int y) { if (allowedMoves[x, y]) { ChessFigure c = ChessFigurePositions[x, y]; if (c != null && c.isWhite != isWhiteTurn) { activeFigures.Remove(c.gameObject); Destroy(c.gameObject); if (c.GetType() == typeof(King)) { EndGame(); return; } } ChessFigurePositions[selectedFigure.CurrentX, selectedFigure.CurrentY] = null; selectedFigure.transform.position = GetTileCenter(x, y); selectedFigure.SetPosition(x, y); ChessFigurePositions[x, y] = selectedFigure; isWhiteTurn = !isWhiteTurn; } BoardHighlighting.Instance.HideHighlights(); selectedFigure = null; }
public ChessMoveInfo Check(Square[,] board, Knight from, ChessFigure to) { if (Check_for_color_match(from, to)) { return(new ChessMoveInfo(false)); } bool KingIsInDanger = Check_for_check(from.IsWhite, board); // If my King is in danger this move should save it; if (KingIsInDanger) { if (Check_if_king_is_check_after(board, from, to)) { // We don't care if the move is valid as long as it saves the king. // After that initial check, it is asserted whether the move is valid or not // and the necessary measures are taken. return(new ChessMoveInfo(false)); } } if (Check_if_king_is_check_after(board, from, to)) { //Same idea but this time we check if the move puts //the life of our dear king in danger. return(new ChessMoveInfo(false)); } ///Again we don't care if it's a valid move just yet, ///just checking if the figure were to move to the designated ///position will it endanger the enemy king or not. bool IsEnemyKingCheck = Check_for_check(!from.IsWhite, board, Generate_attack_board(board, new Knight(to.Row, to.Col, from.IsWhite, ""))); ChessMoveInfo MoveInfo = new ChessMoveInfo(true, from.IsWhite); MoveInfo.FromRow = from.Row; MoveInfo.FromCol = from.Col; MoveInfo.ToRow = to.Row; MoveInfo.ToCol = to.Col; if (to.Name != "Empty") { MoveInfo.TakenFigureRow = to.Row; MoveInfo.TakenFigureCol = to.Col; } if (Math.Abs(from.Row - to.Row) == 2) { if (Math.Abs(from.Col - to.Col) == 1) { return(MoveInfo); } } if (Math.Abs(from.Row - to.Row) == 1) { if (Math.Abs(from.Col - to.Col) == 2) { return(MoveInfo); } } return(new ChessMoveInfo(false)); }
public void FigureMouseUp(ChessFigure figure) { dynamic s = selected; if (!rules.CanMove(s, figure)) { return; } var square = this.Squares.First( x => x.Row == figure.Row && x.Col == figure.Col); var selectedSquare = this.Squares.First( x => x.Row == selected.Row && x.Col == selected.Col); var index = this.Squares.IndexOf(square); var selectedIndex = this.Squares.IndexOf(selectedSquare); selected.Row = square.Row; selected.Col = square.Col; Squares[index] = new Square(square.Row, square.Col, square.Color, selected); Squares[selectedIndex] = new Square(selectedSquare.Row, selectedSquare.Col, selectedSquare.Color, new Empty(selectedSquare.Row, selectedSquare.Col)); //Task.Run(() => //{ // MessageBox.Show(figure.Name + " " + figure.Row.ToString() + " " + figure.Col.ToString()); //}); }
void PullToRetreat(ChessFigure chessFigure) { stateManager.AddState(chessFigure); if (chessFigure.team == TypeTeam.white) { int number = defeatedWhiteFigures.Count; Vector3 pos = defeatedWhiteFiguresParent.transform.position; pos.x += (number / 8) * startSize * scale / 100f; pos.y -= (number % 8) * startSize * scale / 100f; //chessFigure.transform.position = pos; chessFigure.AnimateTurn(pos, speedAnimation, TypeAnimation.Defeat); defeatedWhiteFigures.Add(chessFigure); } else { int number = defeatedBlackFigures.Count; Vector3 pos = defeatedBlackFiguresParent.transform.position; pos.x -= (number / 8) * startSize * scale / 100f; pos.y -= (number % 8) * startSize * scale / 100f; //chessFigure.transform.position = pos; chessFigure.AnimateTurn(pos, speedAnimation, TypeAnimation.Defeat); defeatedBlackFigures.Add(chessFigure); } chessFigure.Destroy(); }
public Square[,] Generate() { Square[,] board = new Square[ Constants.BoardRows, Constants.BoardCols]; for (int row = 0; row < Constants.BoardRows; row++) { for (int col = 0; col < Constants.BoardCols; col++) { Square square = null; ChessFigure figure = GetFigure(row, col); if ((row + col) % 2 == 0) { square = new Square(row, col, true, figure); } else { square = new Square(row, col, false, figure); } board[row, col] = square; } } return(board); }
public void MakeRoque(ChessBoard board, ChessFigure king, int moveNumber) { ChessField kingNewField, rookNewField; if (Color == Color.WHITE) { kingNewField = CurrentField.Column == 'A' ? board.GetField(1, 'C') : board.GetField(1, 'G'); rookNewField = CurrentField.Column == 'A' ? board.GetField(1, 'D') : board.GetField(1, 'F'); } else { kingNewField = CurrentField.Column == 'A' ? board.GetField(8, 'C') : board.GetField(8, 'G'); rookNewField = CurrentField.Column == 'A' ? board.GetField(8, 'D') : board.GetField(8, 'F'); } ChessMove kingMove = new ChessMove(Color, moveNumber, king, null, king.CurrentField, kingNewField, true); king.PreviousMoves.Add(kingMove); ChessMove move = new ChessMove(Color, moveNumber, this, null, CurrentField, rookNewField, true); PreviousMoves.Add(move); board.EmptyField(king.CurrentField); board.EmptyField(CurrentField); board.OccupyField(kingNewField.Row, kingNewField.Column, Color); board.OccupyField(rookNewField.Row, rookNewField.Column, Color); king.CurrentField = kingNewField; CurrentField = rookNewField; king.FindFieldsToMove(board); FindFieldsToMove(board); }
void AddToListSaveFigures(ChessFigure temp, SaveInfo saveAsset) { SaveFigure tempSave = new SaveFigure(); tempSave.positionX = temp.transform.position.x; tempSave.positionY = temp.transform.position.y; tempSave.positionZ = temp.transform.position.z; tempSave.posX = temp.pos.x; tempSave.posY = temp.pos.y; tempSave.startPosX = temp.startPos.x; tempSave.startPosY = temp.startPos.y; tempSave.team = temp.team; tempSave.type = temp.type; tempSave.id = temp.id; tempSave.turnCounter = temp.turnCounter; tempSave.enable = temp.enable; tempSave.beforeFigure = temp.beforeFigure; if (!saveAsset.figures.Contains(tempSave)) { saveAsset.figures.Add(tempSave); } else { Debug.Log("Error! save figure was created"); } }
private bool CheckIfAttackerCanBeTaken(Square[,] board, ChessFigure attacker) { //iterate through all squares foreach (var square in board) { //Skip empty squares if (square.Figure.Name != Constants.EmptySquare) { //Skip own figures and the oposite king if (square.Figure.IsWhite != attacker.IsWhite) { //Check is one of our pieces can take the attacker dynamic attackerFigure = attacker; dynamic protectingFigure = square.Figure; if (rulesService.Check(board, protectingFigure, attackerFigure)) { //we return true, attacker could be taken return(true); } } } } //we can`t take the attacker return(false); }
public Vector2 MakeMove(ChessFigure figure) { r = new System.Random(); bool[,] possibleMoves = figure.PossibleMove(); List <Vector2> possibleMovements = new List <Vector2>(); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (possibleMoves[i, j]) { possibleMovements.Add(new Vector2(i, j)); } } } if (possibleMovements.Count > 0) { return(possibleMovements[r.Next(possibleMovements.Count)]); } else { return(new Vector2(-1, -1)); } }
public void DragFigureLeave(ChessFigure figure) { if (board[figure.Row, figure.Col].CursorOver) { board[figure.Row, figure.Col].CursorOver = false; } }
public Square(int row, int col, string color, ChessFigure figure) { this.Row = row; this.Col = col; this.Color = color; this.Figure = figure; }
public async void DropFigure(ChessFigure figure) { string currentMove = parserService.CastPosition( selectedFigure.Row, selectedFigure.Col, figure.Row, figure.Col); DragFigureLeave(figure); dynamic dynamicFigure = selectedFigure; ChessMoveInfo moveInfo = rulesService.Check(board, dynamicFigure, figure); if (GameService.ProcessMove(board, moveInfo)) { if (autoplay) { await Autoplay(currentMove); } Tuple <int, int, int, int> engineMove = parserService.ParseString(await engineService.PlayMove(currentMove)); dynamic fromFigure = board[engineMove.Item1, engineMove.Item2].Figure; dynamic toFigure = board[engineMove.Item3, engineMove.Item4].Figure; moveInfo = rulesService.Check(board, fromFigure, toFigure); GameService.ProcessMove(board, moveInfo); } }
//the class API public bool IsCheckMate(Square[,] board, King attackedKing) { bool canKingMove = CheckForKingPossibleMoves(board, attackedKing); if (canKingMove) { return(false); } List <ChessFigure> attackers = GetAttackers(board, attackedKing); //If there are two attackers and we already checked that the king can`t move //that means it`s checkmate if (attackers.Count == 2) { return(true); } //Only attacker in the list ChessFigure attacker = attackers[0]; bool canAttackerBeTaken = CheckIfAttackerCanBeTaken(board, attacker); if (canAttackerBeTaken) { return(false); } bool canAttackerBeBlocked = CheckIfAttackerCanBeBlocked(board, attackedKing, attacker); return(true); }
void ReInitGame() { presentTeam = TypeTeam.white; gameEnd = false; ChessFigure[,] newFigures = new ChessFigure[height, width]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (figures[i, j] != null) { BackUpFigure(figures[i, j], newFigures); } } } for (int i = 0; i < defeatedWhiteFigures.Count; i++) { BackUpFigure(defeatedWhiteFigures[i], newFigures); } defeatedWhiteFigures.Clear(); for (int i = 0; i < defeatedBlackFigures.Count; i++) { BackUpFigure(defeatedBlackFigures[i], newFigures); } defeatedBlackFigures.Clear(); figures = newFigures; }
private bool Check_for_color_match(ChessFigure A, ChessFigure B) { if (A.Name == "Empty" || B.Name == "Empty") { return(false); } return(A.IsWhite == B.IsWhite); }
public bool positionFreeOffset(ChessFigure f, int x, int y) { if (getPositionOffset(f, x, y) is ChessPlaceholder) { return(true); } return(false); }
public ChessFigure getPositionOffset(ChessFigure f, int x, int y) { Vector2 pos = getArrayPos(f); x = (int)pos.X + x; y = (int)pos.Y + y; return(getPosition(x, y)); }
public void SelectTile(ChessTile tile) { // Make the tile this belongs to the active board SelectBoard(tile.Board); // If there is an active piece and this is a legal move make the move then deactivate the piece if (_activeFigure != null && _potentialMoves.Contains(tile)) { MovePiece(_activeFigure, tile); _activeFigure = null; return; } foreach (ChessTile moveTile in _potentialMoves) { moveTile.Highlighted = false; // Unhighlights the previously highlighted moves } // Deselect the old tile and select the new one if (_activeTile != null) { _activeTile.Highlighted = false; } _activeTile = tile; _activeTile.Highlighted = true; // If there is a piece on this tile select it as well, otherwise deselect the active piece if (_activeTile.Figure != null) { _activeFigure = _activeTile.Figure; bool[,] legalMoves = _activeFigure.LegalMoves(); // Get the tile references for each legal move _potentialMoves = new List <ChessTile>(); for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { if (legalMoves[x, y]) { _potentialMoves.Add(_activeChessBoard.Tiles[x, y]); } } } // Highlight those squares foreach (ChessTile moveTile in _potentialMoves) { moveTile.Highlighted = true; } } else { _activeFigure = null; } _activeChessBoard.ClearThreatArrows(); _activeChessBoard.DrawThreatArrowsToTile(_activeTile); }
public void FigureClicked(ChessFigure figure) { selected = figure; //Task.Run(() => //{ // MessageBox.Show(figure.Name + " " + figure.Row.ToString() + " " + figure.Col.ToString()); //}); }
public void Do(ref ChessGameField.Cell[,] field, ChessFigure figure) { Fig = figure; foreach (Action action in Actions) { action.Do(ref field); } }
public void DragFigureEnter(ChessFigure figure) { dynamic dynamicFigure = selectedFigure; ChessMoveInfo MoveInfo = rulesService.Check(board, dynamicFigure, figure); if (MoveInfo) { board[figure.Row, figure.Col].CursorOver = true; } }
public bool positionEnemyOffset(ChessFigure f, int x, int y) { ChessFigure offset = getPositionOffset(f, x, y); if (offset != null && !(offset is ChessPlaceholder) && offset.whitecolor != f.whitecolor) { return(true); } return(false); }
public IActionResult GetAvailableMoves([FromQuery] string figureName, byte xPosition, byte yPosition) { List <Type> chessFigureTypes = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.BaseType == typeof(ChessFigure)) .ToList(); Type requestedType = chessFigureTypes.Single(t => t.Name.Equals(figureName, StringComparison.OrdinalIgnoreCase)); ChessFigure chessFigure = (ChessFigure)Activator.CreateInstance(requestedType, new object[] { xPosition, yPosition }, null); return(Ok(chessFigure.GetAvailableMoves())); }
public void updateStatus(ChessFigure c) { if (c.isRed) { red--; } else { blue--; } }
public ChessMove(Color color, int moveNumber, ChessFigure figure, ChessFigure killed, ChessField initial, ChessField result, bool isRoque = false) { Color = color; MoveNumber = moveNumber; MovingFigure = figure; KilledFigure = killed; InitialField = initial; ResultField = result; IsRoque = isRoque; }
void ExchangeFigure(int indexOfFigures) { CreateFigure(becomeKingFigure.team == TypeTeam.white ? chessTypeOne[indexOfFigures] : chessTypeTwo[indexOfFigures], becomeKingFigure.pos.x, becomeKingFigure.pos.y, figuresParent.transform, becomeKingFigure.team, becomeKingFigure != null); //Destroy(becomeKingFigure.gameObject); stateManager.AddState(becomeKingFigure); PullToRetreat(becomeKingFigure); pawnReadyBecomeKing = false; becomeKingFigure = null; activeUI = false; }
bool LoadGame() { if (!File.Exists(Application.persistentDataPath + "/GameInfo.dat")) { return(false); } BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/GameInfo.dat", FileMode.Open); SaveInfo saveData = (SaveInfo)bf.Deserialize(file); file.Close(); figures = new ChessFigure[height, width]; figuresParent = new GameObject("Figures"); defeatedWhiteFiguresParent = new GameObject("Defeated white figures"); float x = ((width + 1) / 2f) * startSize * scale / 100f; float y = ((height - 1) / 2f) * startSize * scale / 100f; defeatedWhiteFiguresParent.transform.position = new Vector3(x, y, 0); defeatedBlackFiguresParent = new GameObject("Defeated black figures"); x = ((-width - 1) / 2f) * startSize * scale / 100f; y = ((height - 1) / 2f) * startSize * scale / 100f; defeatedBlackFiguresParent.transform.position = new Vector3(x, y, 0); for (int i = 0; i < saveData.figures.Count; i++) { SaveFigure temp = saveData.figures[i]; if (temp.enable) { figures[temp.posX, temp.posY] = GetFromListSaveFifures(temp, figuresParent.transform); } else if (temp.team == TypeTeam.white) { defeatedWhiteFigures.Add(GetFromListSaveFifures(temp, defeatedWhiteFiguresParent.transform)); } else { defeatedBlackFigures.Add(GetFromListSaveFifures(temp, defeatedBlackFiguresParent.transform)); } } presentTeam = saveData.presentTeam; becomeKingFigure = FindFigure(saveData.idBecomeKingFigure); for (int i = saveData.states.Count - 1; i >= 0; i--) { stateManager.AddState(saveData.states[i]); } return(true); }
public void Spawn(ChessFigure figure, int row, int column, Player player) { var cell = cells[column, row]; var go = Instantiate(figure, figureContainer.transform); cell.Figure = go; figure.GetComponent <Image>().enabled = false; player.AddActiveFigure(go); go.Player = player; go.IsInteractable = false; go.DragStarted += () => Figure_DragStarted(go); go.DragEnded += () => Figure_DragEnded(go); }
public Vector2 getArrayPos(ChessFigure f) //TODO BETTER RUNTIME POSSIBLE { for (int xa = 0; xa < chessfield.GetLength(0); xa++) { for (int ya = 0; ya < chessfield.GetLength(0); ya++) { if (chessfield[xa, ya] == f) { return(new Vector2(xa, ya)); } } } return(new Vector2(0, 0)); }
private List <Square> GetQueenPath(Square[,] board, King attackedKing, ChessFigure attacker) { List <Square> path = new List <Square>(); if (attacker.Col != attackedKing.Col && attacker.Row != attackedKing.Row) { path = GetBishopPath(board, attackedKing, attacker); } else { path = GetRookPath(board, attackedKing, attacker); } return(path); }
public void CreateFigure(ChessFigurePosition position, ChessFigure figure) { if (this[position] == ChessFigure._) { this[position] = figure; } else { throw new InvalidOperationException("There's a chess figure at this position."); } }
public void ReplaceFigure(ChessFigurePosition position, ChessFigure figure) { if (this[position] != ChessFigure.None) { this[position] = figure; } else { throw new InvalidOperationException("There's no figure at this position."); } }
private void AddFigure(string code, ChessFigure figure) { figures[code] = figure; figureCodes[figure] = code; }
public string SaveFigure(ChessFigure figure) { if (figure == ChessFigure.None) return "."; else return figureCodes[figure]; }