示例#1
0
        protected async override void OnNavigatedTo(NavigationEventArgs e) //
        {
            this.InitializeComponent();
            this.currGame = ChessGameManager.initializeGame();
            Session sess = e.Parameter as Session;

            this.username = sess.PlayerName;

            DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait;
            Timer          = new DispatcherTimer();
            Timer.Interval = TimeSpan.FromMilliseconds(10);
            Timer.Tick    += DispatcherTimer_Tick;
            TimeSpaner     = TimeSpan.FromMinutes(0);

            tb_Timer.Text = showTime();
            Timer.Start();
        }
示例#2
0
        private async void DoneClick(object sender, RoutedEventArgs e)
        {
            if (currGame.DoMove(fromInput.Text, toInput.Text) < 0)
            {
                MessageDialog msg = new MessageDialog("Invalid Move! please try again");
                Utils.Show(msg, new List <UICommand> {
                    new UICommand("Close")
                });
                return;
            }

            //updated move in board and all is legal
            setComputerTurn();
            string move_str = ChessGameManager.GetBestMove(currGame);

            if (move_str == "bad move")
            {
                MessageDialog msg = new MessageDialog("Bad Computer move! ");
                Utils.Show(msg, new List <UICommand> {
                    new UICommand("Close")
                });
                Frame.Navigate(typeof(MainMenu));
                return;
            }

            // player have won
            if (currGame.IsCheckMate(currGame.ActivePlay.PlayerSide.type))
            {
                Timer.Stop();
                this.humanWon       = true;
                InputTextBlock.Text = "You have won the match ! Congratulations ! \n Exit by clicking the surrender button";
                Utils.Show(new MessageDialog("Checkmate ! You won !"), new List <UICommand> {
                    new UICommand("Close")
                });
                return;
            }

            string from = move_str.Substring(move_str.Length - 5, 2);
            string to   = move_str.Substring(move_str.Length - 2, 2);

            //send move to board
            ChessGameManager.sendMoveToBoard(from + to + to[1]);

            // execute computer move
            if (currGame.DoMove(from, to) == -1)
            {
                Utils.Show(new MessageDialog("Computer move illegal !"), new List <UICommand> {
                    new UICommand("Close")
                });
                Frame.Navigate(typeof(MainMenu));
                return;
            }

            string msg_str = "Computer Move: " + move_str;

            if (currGame.IsUnderCheck())
            {
                msg_str += "\n" + "Check !";
            }
            if (move_str[move_str.Length - 3] == 'x')
            {
                msg_str += "\n" + "Please remove captured piece " + to;
            }


            Utils.Show(new MessageDialog(msg_str), new List <UICommand> {
                new UICommand("Close")
            });
            // computer have won
            if (currGame.IsCheckMate(currGame.ActivePlay.PlayerSide.type))
            {
                Timer.Stop();
                InputTextBlock.Text = "The computer have won the match ! \n Exit by clicking the surrender button";
                Utils.Show(new MessageDialog("Checkmate ! The computer won !"), new List <UICommand> {
                    new UICommand("Close")
                });
                return;
            }
            // set the turn back to the user
            setHumanTurn();
        }