示例#1
0
        public void repaint()
        {
            Dispatcher.Invoke(DispatcherPriority.Input, new ThreadStart(() =>
            {
                String turn  = Chess.WhosTurn();
                Turn.Content = char.ToUpper(turn[0]) + turn.Substring(1) + "'s turn";
                bool black   = false;
                for (int y = 0; y < 8; ++y)
                {
                    for (int x = 0; x < 8; ++x)
                    {
                        Label l = FindChild <Label>(ChessBoard, "board_" + x + y);
                        if (l != null)
                        {
                            l.MouseLeftButtonDown -= showMoves;
                            l.MouseLeftButtonDown -= move;
                            l.MouseLeftButtonDown -= repaint;


                            Piece current = Chess.board.at(new Position(x, y));
                            String type   = current.visual();
                            l.Content     = type;
                            l.Background  = black ? new SolidColorBrush(Color.FromArgb(150, 0, 0, 0)) : new SolidColorBrush(Color.FromArgb(205, 255, 255, 255));

                            if (current.getColor() == Chess.WhosTurn())
                            {
                                if (current.getType() == "king")
                                {
                                    if (Chess.IsChecked(Chess.WhosTurn()))
                                    {
                                        l.Background = Brushes.LightCoral;
                                    }
                                }

                                l.Cursor = Cursors.Hand;
                                l.MouseLeftButtonDown += showMoves;
                            }
                            else
                            {
                                l.Cursor = Cursors.Arrow;
                            }
                        }
                        black = !black;
                    }
                    black = !black;
                }

                ChessMove latestMove = Chess.computerMove;
                if (latestMove != null)
                {
                    Label last      = FindChild <Label>(ChessBoard, "board_" + latestMove.from.x + latestMove.from.y);
                    last.Background = Brushes.GreenYellow;

                    last            = FindChild <Label>(ChessBoard, "board_" + latestMove.to.x + latestMove.to.y);
                    last.Background = Brushes.GreenYellow;
                }
            }));
        }
示例#2
0
        public static void writeToJSON()
        {
            var query = from piece in Chess.board.getBoard()
                        where piece.Value.getType() != "blank"
                        select piece.Value;

            StringBuilder sb = new StringBuilder();

            sb.Append("{\"chess\": {\"pieces\": [");
            foreach (var piece in query)
            {
                sb.Append("{\"position\": {\"x\": " + piece.getPosition().x + ", \"y\": " + piece.getPosition().y + "}, \"type\": \"" + piece.getType() + "\", \"moves\": " + piece.getNumberOfMoves() + ", \"color\": \"" + piece.getColor() + "\"}, ");
            }
            sb.Remove(sb.Length - 2, 2);
            sb.Append("], \"turn\": \"" + Chess.WhosTurn() + "\"}}");

            DateTime dt = DateTime.Now;

            if (Chess.currentGameFile == "")
            {
                Chess.currentGameFile = Chess.gamePath + "\\chess_" + dt.ToString("yyyy_MM_dd_HH_mm") + ".json";
            }

            try
            {
                using (StreamWriter sw = File.CreateText(Chess.currentGameFile))
                {
                    sw.Write(sb.ToString());
                    failedToWrite = false;
                }
            }
            catch
            {
                failedToWrite = true;
            }

            lastFile = sb.ToString();
        }