public override bool[,] PossibleMoves2(PieceBoard board, int x, int y) { bool[,] possible = PossibleMoves3(board, x, y); ChessGame chess = board as ChessGame; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (possible[i, j]) { MobilePiece overwritePiece = board.pieces[i, j]; board.pieces[i, j] = board.pieces[x, y]; board.pieces[x, y] = null; Point kingPos = chess.GetKingPos(white); bool check = chess.InCheck(kingPos.X, kingPos.Y, white); //Console.WriteLine(check); board.pieces[x, y] = board.pieces[i, j]; board.pieces[i, j] = overwritePiece; if (check) { possible[i, j] = false; } } } } return(possible); }
public void Move(PieceBoard board, int x1, int y1, int x2, int y2, Action action = null) { if (!CancelMove(board, x1, y1, x2, y2, action)) { //board.pieces[x1, y1].BeforeMove(board, x1, y1, x2, y2); action?.Invoke(); MobilePiece piece = board.pieces[x1, y1]; piece.moved = true; board.pieces[x1, y1] = null; Vector2 dest = new Vector2(x2, y2) + Vector2.One / 2; board.animations.Add(new MoveAnim(piece.GetTexture, new Vector2(x1, y1) + Vector2.One / 2, dest, () => { moveSound.Play(); Point destPos = dest.ToPoint(); board.pieces[destPos.X, destPos.Y] = piece; AfterMove(board, x1, y1, x2, y2); })); } }
protected override void ResetGame() { playerWhite ^= true; turn = false; base.ResetGame(); capturingPiece = new Point(-1); pieces = new MobilePiece[8, 8]; LoopThroughAll((x, y) => { if (y < 3) { if ((x + y) % 2 == 1) { SetPiece(new Draught(false), x, y); } } else if (y > 4) { if ((x + y) % 2 == 1) { SetPiece(new Draught(true), x, y); } } }); }
public PieceBoard(MainScreen mainScreen, int seed, int _d) : base(mainScreen, seed) { d = _d; scl = wh / d; boardMatrix = Matrix.CreateScale(scl); boardMatrix *= Matrix.CreateTranslation(new Vector3((wh % d) / 2, (wh % d) / 2, 0f)); pieces = new MobilePiece[d, d]; selectedTiles = new bool[d, d]; animations = new AnimManager(); ResetGame(); }
protected override void AfterMove(PieceBoard board, int x1, int y1, int x2, int y2) { if (Math.Abs(x2 - x1) == 2) { board.pieces[(x1 + x2) / 2, (y1 + y2) / 2] = null; (board as DraughtsGame).capturingPiece = new Point(x2, y2); } if (!king && (y2 == 0 || y2 == 7)) { king = true; MobilePiece piece = this; board.pieces[x2, y2] = null; board.animations.Add(new SpriteAnim(white ? whitePieces.flip : blackPieces.flip, new Vector2(x2, y2) + Vector2.One / 2, () => board.pieces[x2, y2] = piece)); } }
public bool InCheck(int x, int y, bool white) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (x == i && y == j) { continue; } MobilePiece piece = pieces[i, j]; if (piece != null && piece.white != white) { if ((piece as ChessPiece).PossibleMoves3(this, i, j)[x, y]) { return(true); } } } } return(false); }
public override void AfterUpdate(GameTime gameTime) { //if (overlay != null && overlay.TakeControl) { } if (animations.Count > 0) { animations.Update(gameTime); /* * for (int i = animations.Count - 1; i >= 0; i--) * { * animations[i].Update(gameTime); * } * * animations.RemoveAll(x => x.Finished); */ if (animations.Count == 0) { if (!doNotUpdate) { AfterMove(); } else { doNotUpdate = false; } } } else { if (Input.LeftMousePressed()) { Point mousePos = Input.MousePositionMatrix(boardMatrix).ToPoint(); if (InBounds(mousePos.X, mousePos.Y)) { //selectedTiles[mousePos.X, mousePos.Y] ^= true; if (selectedTiles[mousePos.X, mousePos.Y]) { selectedTiles = new bool[d, d]; /* * pieces[selectedPiece.X, selectedPiece.Y].BeforeMove * (this, selectedPiece.X, selectedPiece.Y, mousePos.X, mousePos.Y); */ pieces[selectedPiece.X, selectedPiece.Y].Move (this, selectedPiece.X, selectedPiece.Y, mousePos.X, mousePos.Y, new Action(() => pieces[selectedPiece.X, selectedPiece.Y].BeforeMove(this, selectedPiece.X, selectedPiece.Y, mousePos.X, mousePos.Y))); //SendMessage(selectedPiece.X + "," + selectedPiece.Y + "," + mousePos.X + "," + mousePos.Y); } else { MobilePiece piece = pieces[mousePos.X, mousePos.Y]; if (piece != null) { selectedTiles = piece.PossibleMoves(this, mousePos.X, mousePos.Y); selectedPiece = mousePos; } else { selectedTiles = new bool[d, d]; } } } } } }
internal void SetPiece(MobilePiece piece, int x, int y) { //animations.Add(new ZoomAnim(piece.GetTexture, new Vector2(x, y), new Action(() => pieces[x, y] = piece), Vector2.Zero, Vector2.One, (x + y) * 2)); animations.Add(new ZoomAnim(piece.GetTexture, new Vector2(x, y), new Action(() => pieces[x, y] = piece), Vector2.Zero, Vector2.One, (x + y) * 2)); //pieces[x, y] = piece; }
internal bool ComparePieceColours(MobilePiece p1, MobilePiece p2) => p1 != null && p2 != null && p1.white == p2.white;