Esempio n. 1
0
 public static void Initialize(Game g)
 {
     board = new Piece[8, 8, 3];
     board[0, 0, 0] = new Piece(Type.Root, true);
     board[1, 0, 0] = new Piece(Type.Knight, true);
     board[2, 0, 0] = new Piece(Type.Bishop, true);
     board[3, 0, 0] = new Piece(Type.Queen, true);
     board[4, 0, 0] = new Piece(Type.King, true);
     board[5, 0, 0] = new Piece(Type.Bishop, true);
     board[6, 0, 0] = new Piece(Type.Knight, true);
     board[7, 0, 0] = new Piece(Type.Root, true);
     for (int i = 0; i < 8; i++)
         board[i, 1, 0] = new Piece(Type.Pawn, true);
     board = new Piece[8, 8, 3];
     board[0, 7, 2] = new Piece(Type.Root, false);
     board[1, 7, 2] = new Piece(Type.Knight, false);
     board[2, 7, 2] = new Piece(Type.Bishop, false);
     board[3, 7, 2] = new Piece(Type.Queen, false);
     board[4, 7, 2] = new Piece(Type.King, false);
     board[5, 7, 2] = new Piece(Type.Bishop, false);
     board[6, 7, 2] = new Piece(Type.Knight, false);
     board[7, 7, 2] = new Piece(Type.Root, false);
     for (int i = 0; i < 8; i++)
         board[i, 6, 2] = new Piece(Type.Pawn, false);
     XBase = new int[3];
     YBase = new int[3];
     XBase[0] = 500;
     XBase[1] = 500;
     XBase[2] = 500;
     YBase[0] = 0;
     YBase[1] = 205;
     YBase[2] = 410;
     game = g;
 }
Esempio n. 2
0
        public static void Update()
        {
            MouseState mouseState = Mouse.GetState();
            _whiteChess = false;
            _blackChess = false;

            if (!_iswhiteToPlay) // noirs jouent
            {
                Piece blackKing = Black.Find(x => x.PieceType == Type.King);
                WhiteDeadCells.Clear();

                board[(int) blackKing.Position.X, (int) blackKing.Position.Y, (int) blackKing.Position.Z].PieceType = Type.Empty;
                White.ForEach(p => WhiteDeadCells.AddRange(p.GetPossibleMoves())); // on liste les cases menacées par les blancs
                board[(int) blackKing.Position.X, (int) blackKing.Position.Y, (int) blackKing.Position.Z].PieceType = Type.King;

                WhiteDeadCells = WhiteDeadCells.Distinct().ToList();
                _blackChess = WhiteDeadCells.Any(v => v == blackKing.Position); // on regarde si notre roi est menacé
                var blackKingMoves = (List<Vector3>)blackKing.GetPossibleMoves(); // on liste les deplacements possibles de notre roi
                blackKingMoves.RemoveAll(move => WhiteDeadCells.Contains(move)); // on supprime tous ceux qui le mettraient en echec

                if (blackKingMoves.Count == 0 && _blackChess)
                    IsRuning = false; //echec & mate
            }
            else // blancs jouent
            {
                Piece whiteKing = White.Find(x => x.PieceType == Type.King);
                BlackDeadCells.Clear();

                board[(int)whiteKing.Position.X, (int)whiteKing.Position.Y, (int)whiteKing.Position.Z].PieceType = Type.Empty;
                Black.ForEach(p => BlackDeadCells.AddRange(p.GetPossibleMoves())); // on liste les cases menacées par les noirs
                board[(int)whiteKing.Position.X, (int)whiteKing.Position.Y, (int)whiteKing.Position.Z].PieceType = Type.King;

                BlackDeadCells = BlackDeadCells.Distinct().ToList();
                _whiteChess = BlackDeadCells.Any(v => v == whiteKing.Position); // on regarde si notre roi est menacé
                var whiteKingMoves = (List<Vector3>)whiteKing.GetPossibleMoves(); // on liste les deplacements possibles de notre roi
                whiteKingMoves.RemoveAll(move => BlackDeadCells.Contains(move)); // on supprime tous ceux qui le mettraient en echec

                if (whiteKingMoves.Count == 0 && _whiteChess)
                    IsRuning = false; //echec & mate
            }

            if (!IsRuning) return;

            if (mouseState.LeftButton != ButtonState.Pressed) return;

            Vector3 selected = ScreenToBoard(mouseState.X, mouseState.Y);
            if (selected.X >= 0 && selected.X < 8 && selected.Y >= 0 && selected.Y < 8 && selected.Z >= 0 && selected.Z < 3)
            {
                selectedCase = board[(int)selected.X, (int)selected.Y, (int)selected.Z];
                if (selectedCase.PieceType != Type.Empty && _iswhiteToPlay == selectedCase.IsWhite /*&& (whiteToPlay ? (!_whiteChess || selectedCase.PieceType == Type.King) : (!_blackChess || selectedCase.PieceType == Type.King))*/)
                    possibleMove = new Tuple<Piece, List<Vector3>>(selectedCase, (List<Vector3>)selectedCase.GetPossibleMoves());
                else
                {
                    if (possibleMove.Item2.Contains(selected))
                    {
                        if (selectedCase.PieceType == Type.King)
                        {
                            board[(int)possibleMove.Item1.Position.X, (int)possibleMove.Item1.Position.Y, (int)possibleMove.Item1.Position.Z] = new Piece(Type.Empty, true);
                            board[(int)selected.X, (int)selected.Y, (int)selected.Z] = possibleMove.Item1;
                            board[(int)selected.X, (int)selected.Y, (int)selected.Z].Position = selected;
                            IsRuning = false;
                        }
                        else
                        {
                            if (_iswhiteToPlay)
                            {
                                if (!BlackDeadCells.Contains(selected))
                                {
                                    Black.Remove(board[(int) selected.X, (int) selected.Y, (int) selected.Z]);
                                    board[(int)possibleMove.Item1.Position.X, (int)possibleMove.Item1.Position.Y, (int)possibleMove.Item1.Position.Z] = new Piece(Type.Empty, true);
                                    board[(int)selected.X, (int)selected.Y, (int)selected.Z] = possibleMove.Item1;
                                    board[(int)selected.X, (int)selected.Y, (int)selected.Z].Position = selected;
                                    _iswhiteToPlay = !_iswhiteToPlay;
                                }
                            }
                            else
                            {
                                if (!WhiteDeadCells.Contains(selected))
                                {
                                    White.Remove(board[(int) selected.X, (int) selected.Y, (int) selected.Z]);
                                    board[(int)possibleMove.Item1.Position.X, (int)possibleMove.Item1.Position.Y, (int)possibleMove.Item1.Position.Z] = new Piece(Type.Empty, true);
                                    board[(int)selected.X, (int)selected.Y, (int)selected.Z] = possibleMove.Item1;
                                    board[(int)selected.X, (int)selected.Y, (int)selected.Z].Position = selected;
                                    _iswhiteToPlay = !_iswhiteToPlay;
                                }
                            }
                        }
                    }
                    possibleMove = new Tuple<Piece, List<Vector3>>(new Piece(Type.Empty), new List<Vector3>());
                }
            }
            else
                selectedCase = new Piece(Type.Empty, true);
        }
Esempio n. 3
0
 public static void Initialize(Game g)
 {
     Black = new List<Piece>();
     White = new List<Piece>();
     board = new Piece[8, 8, 3];
     for (int i = 0; i < 8; i++)
         for (int j = 0; j < 8; j++)
             for (int k = 0; k < 3; k++)
                 board[i, j, k] = new Piece(Type.Empty, true);
     board[0, 0, 0] = new Piece(Type.Rook, true);
     board[1, 0, 0] = new Piece(Type.Knight, true);
     board[2, 0, 0] = new Piece(Type.Bishop, true);
     board[3, 0, 0] = new Piece(Type.Queen, true);
     board[4, 0, 0] = new Piece(Type.King, true);
     board[5, 0, 0] = new Piece(Type.Bishop, true);
     board[6, 0, 0] = new Piece(Type.Knight, true);
     board[7, 0, 0] = new Piece(Type.Rook, true);
     for (int i = 0; i < 8; i++)
         board[i, 1, 0] = new Piece(Type.Pawn, true);
     board[0, 7, 2] = new Piece(Type.Rook, false);
     board[1, 7, 2] = new Piece(Type.Knight, false);
     board[2, 7, 2] = new Piece(Type.Bishop, false);
     board[3, 7, 2] = new Piece(Type.Queen, false);
     board[4, 7, 2] = new Piece(Type.King, false);
     board[5, 7, 2] = new Piece(Type.Bishop, false);
     board[6, 7, 2] = new Piece(Type.Knight, false);
     board[7, 7, 2] = new Piece(Type.Rook, false);
     for (int i = 0; i < 8; i++)
         board[i, 6, 2] = new Piece(Type.Pawn, false);
     for (int i = 0; i < 8; i++)
         for (int j = 0; j < 8; j++)
             for (int k = 0; k < 3; k++)
                 board[i, j, k].Position = new Vector3(i, j, k);
     XBase = new int[3];
     YBase = new int[3];
     for (int i = 0; i < 2; i++)
     {
         for (int j = 0; j < 8; j++)
         {
             Black.Add(board[j, 7 - i , 2]);
             White.Add(board[j, i, 0]);
         }
     }
     XBase[0] = 500;
     XBase[1] = 500;
     XBase[2] = 500;
     YBase[0] = 0;
     YBase[1] = 205;
     YBase[2] = 410;
     game = g;
     IsRuning = true;
 }