Example #1
0
 /// <summary>
 /// Update teh booleans of this class
 /// </summary>
 /// <param name="m">Move to take cues from</param>
 public void UpdateCastlingStatus(Move m)
 {
     if (this[m.From]._Color == PlayerColor.White)
     {
         if (m.From == _WhiteKing)
         {
             _WhiteCanCastleShort = false;
             _WhiteCanCastleLong = false;
         }
         if (m.From == _WhiteRookShort)
             _WhiteCanCastleShort = false;
         if (m.From == _WhiteRookLong)
             _WhiteCanCastleLong = false;
     }
     else
     {
         if (m.From == _BlackKing)
         {
             _BlackCanCastleShort = false;
             _BlackCanCastleLong = false;
         }
         if (m.From == _BlackRookShort)
             _BlackCanCastleShort = false;
         if (m.From == _BlackRookLong)
             _BlackCanCastleLong = false;
     }
 }
Example #2
0
        /// <summary>
        /// Read method to be executed by threads
        /// </summary>
        public void Read()
        {
            string Line = "";
            while (true)
            {
                // read line, write some debug output
                Line = _ChessStreamReader.ReadLine();
                Console.WriteLine("ChessProtocol.Read(" + Line + ")");

                // split line
                char[] Delimiter = { ' ' };
                string[] Params = Line.Split(Delimiter);

                // handle command
                switch (Params[0])
                {
                    case "move":
                        {
                            string Promotion = null;
                            if (Params.Length == 4)
                                Promotion = Params[3];
                            Move m = new Move(Params[1], Params[2], Promotion);
                            _ChessGame.PeerMove(m);
                            break;
                        }
                    case "stalemate":
                        {
                            MessageBox.Show("Stalemate!", "Stalemate! Did you feel lucky?");
                            _ChessGame.Dispose();
                            break;
                        }
                    case "check_mate":
                        {
                            MessageBox.Show("Checkmate!", "Checkmate! Did you feel lucky?");
                            _ChessGame.Dispose();
                            break;
                        }
                    case "message":
                        {
                            MessageBox.Show(Params[1] + ": " + Line.Substring(Line.IndexOf(Params[2])), "Message from user " + Params[1]);
                            break;
                        }
                    case "surrender":
                        {
                            MessageBox.Show("You have won!", "Remote user surrendered");
                            _ChessGame.Dispose();
                            break;
                        }
                    case "endgame":
                        {
                            MessageBox.Show("You have won!", "Remote user ended the game");
                            _ChessGame.Dispose();
                            break;
                        }
                    case "offer_draw":
                        {
                            DialogResult dr = MessageBox.Show("You are offered a draw, do you accept?", "Draw offer", MessageBoxButtons.YesNo);
                            if (dr == DialogResult.Yes)
                            {
                                DrawOk();
                                _ChessGame.Dispose();
                            }
                            else
                                DrawDeny();
                            break;
                        }
                    case "draw_ok":
                        {
                            MessageBox.Show("Draw accepted", "Your draw offer has been accepted");
                            _ChessGame.Dispose();
                            break;
                        }
                    case "draw_deny":
                        {
                            MessageBox.Show("Draw rejected", "Your draw offer has been rejected");
                            break;
                        }
                }
            }
        }
Example #3
0
 /// <summary>
 /// Undo a move. Sherlock says: "This may require that the move was previously applied."
 /// </summary>
 /// <param name="m">The move</param>
 public void UndoMove(Move m)
 {
     this[m.From] = this[m.To];
     this[m.To] = m.Capture;
 }
Example #4
0
        /// <summary>
        /// If the move is a castling move, find the tower and move it too
        /// </summary>
        /// <param name="m">The move</param>
        public void DoCastling(Move m)
        {
            if (this[m.From].Type == PieceType.King)
            {
                int XDelta, YDelta;
                int XDir, YDir;

                m.AbsDelta(out XDelta, out YDelta);
                m.Direction(out XDir, out YDir);

                if (XDelta == 2 && YDelta == 0) // is a good castling move
                {
                    int TowerColumn = (XDir == 1 ? 7 : (XDir == -1 ? 0 : 666));
                    Move TowerMove = new Move(TowerColumn, m.From.Y, m.To.X - XDir, m.From.Y, "");
                    ApplyMove(TowerMove);
                }
            }
        }
Example #5
0
 /// <summary>
 /// Apply a move to the board
 /// </summary>
 /// <param name="m">Move to execute</param>
 public void ApplyMove(Move m)
 {
     m.Capture = this[m.To];
     this[m.To] = this[m.From];
     this[m.From] = null;
 }
Example #6
0
        /// <summary>
        /// Do a move that was initiated by the user
        /// </summary>
        /// <param name="FromX">X coordinate</param>
        /// <param name="FromY">Y coordinate</param>
        /// <param name="ToX">X coordinate</param>
        /// <param name="ToY">Y coordinate</param>
        public void UserMove(int FromX, int FromY, int ToX, int ToY)
        {
            Move m = new Move(FromX, FromY, ToX, ToY, "");
            if (_MoveChecker.IsLegalMove(m))
            {
                HistoryDelegate hd = new HistoryDelegate(_ChessInterface.AddToHistory);
                _Board.DoCastling(m);
                _Board.UpdateCastlingStatus(m);
                _Board.ApplyMove(m);
                _ChessProtocol.SendMove(m.ToString());
                string Line = "You: " + Coordinate.IndexToChess(FromX, FromY) + " > " + Coordinate.IndexToChess(ToX, ToY);

                ChangeColor();

                _ChessInterface.Invoke(hd, new object[] { Line });
                _ChessInterface.DrawPieces();
            }
            //else
            //    throw new ChessException("Illegal move!");
        }
Example #7
0
        /// <summary>
        /// Execute a move that was initiated by a peer
        /// </summary>
        /// <param name="m">Remote opponent sends a move</param>
        public void PeerMove(Move m)
        {
            HistoryDelegate hd = new HistoryDelegate(_ChessInterface.AddToHistory);
            string Line = "Opponent: " + Coordinate.IndexToChess(m.From.X, m.From.Y) + " > " + Coordinate.IndexToChess(m.To.X, m.To.Y);
            _Board.DoCastling(m);
            _Board.UpdateCastlingStatus(m);
            _Board.ApplyMove(m);

            ChangeColor();

            _ChessInterface.Invoke(hd, new object[] { Line });
            _ChessInterface.DrawPieces();
        }