Exemplo n.º 1
0
        private void onClick(object obj, RoutedEventArgs e)
        {
            Button tmp = (Button)obj;

            int row    = (int)tmp.GetValue(Grid.RowProperty) - index;
            int column = (int)tmp.GetValue(Grid.ColumnProperty) - index;

            Piece tmpPiece = this.logicalCDC.array[row, column];

            //POSISI YANG DI KLIK BUKAN MERUPAKAN TEMPAT KOSONG
            if (!this.logicalCDC.isPositionEmpty(row, column))
            {
                //PIECE TERSEBUT BELUM DIBUKA
                if (!this.logicalCDC.isFlipped(row, column))
                {
                    tmp.Content = createContentBidak(tmpPiece.number);

                    if (tmpPiece.number > 0)
                    {
                        tmp.Foreground = Brushes.Black;
                        if (currentTurn == PLAYER.COMPUTER)
                        {
                            if (this.btnComputer.Content.ToString() == "?")
                            {
                                this.btnComputer.Content    = "    ";
                                this.btnComputer.Background = Brushes.Black;
                                this.btnHuman.Content       = "    ";
                                this.btnHuman.Background    = Brushes.Red;
                            }
                        }
                        else
                        {
                            if (this.btnHuman.Content.ToString() == "?")
                            {
                                this.btnComputer.Content    = "    ";
                                this.btnComputer.Background = Brushes.Red;
                                this.btnHuman.Content       = "    ";
                                this.btnHuman.Background    = Brushes.Black;
                            }
                        }
                    }
                    else
                    {
                        tmp.Foreground = Brushes.Red;
                        if (currentTurn == PLAYER.COMPUTER)
                        {
                            if (this.btnComputer.Content.ToString() == "?")
                            {
                                this.btnComputer.Content    = "    ";
                                this.btnComputer.Background = Brushes.Red;
                                this.btnHuman.Content       = "    ";
                                this.btnHuman.Background    = Brushes.Black;
                            }
                        }
                        else
                        {
                            if (this.btnHuman.Content.ToString() == "?")
                            {
                                this.btnComputer.Content    = "    ";
                                this.btnComputer.Background = Brushes.Black;
                                this.btnHuman.Content       = "    ";
                                this.btnHuman.Background    = Brushes.Red;
                            }
                        }
                    }
                    //setting state menjadi flip
                    this.logicalCDC.flip(row, column);
                    //MAINKAN SUARANYA
                    if (Properties.Settings.Default["Sound"].ToString() == "Enable")
                    {
                        Player.Open(new Uri(@"PindahPiece\" + Properties.Settings.Default["MovingSound"].ToString() + ".wav", UriKind.RelativeOrAbsolute));
                        Player.Play();
                    }
                    //hilangkan current focus
                    destroyCurrentFocus();

                    main.WriteLine(currentTurn + " => Flip : " + new Position(row, column).ToString());

                    switchTurn();
                    writeStatusBar();
                }
                //PIECE TERSEBUT SUDAH DIBUKA
                else
                {
                    switchFocus(new Focus(rectanglesFocus[row, column], new Position(row, column)));
                    if (this.currentFocus != null)
                    {
                        if (!this.logicalCDC.isSameSide(tmpPiece, this.logicalCDC.sideToMove))
                        {
                            int turn;
                            //check apakah memang giliran dia
                            if (currentTurn == PLAYER.HUMAN)
                            {
                                if (this.btnHuman.Background == Brushes.Red)
                                {
                                    turn = Constant.RED_SIDE;
                                }
                                else
                                {
                                    turn = Constant.BLACK_SIDE;
                                }
                            }
                            else
                            {
                                if (this.btnComputer.Background == Brushes.Red)
                                {
                                    turn = Constant.RED_SIDE;
                                }
                                else
                                {
                                    turn = Constant.BLACK_SIDE;
                                }
                            }
                            if (turn == this.logicalCDC.sideToMove)
                            {
                                if (this.rectanglesFocus[row, column].Stroke != null)
                                {
                                    this.logicalCDC.move(this.currentFocus.posisi.row, this.currentFocus.posisi.column, row, column);

                                    //MAINKAN SUARANYA
                                    if (Properties.Settings.Default["Sound"].ToString() == "Enable")
                                    {
                                        Player.Open(new Uri(@"PindahPiece\" + Properties.Settings.Default["MovingSound"].ToString() + ".wav", UriKind.RelativeOrAbsolute));
                                        Player.Play();
                                    }
                                    move(this.currentFocus.posisi, new Position(row, column));

                                    destroyCurrentFocus();

                                    switchTurn();
                                    writeStatusBar();
                                }
                            }
                        }
                    }
                }
            }
            //MERUPAKAN TEMPAT KOSONG
            else
            {
                //check apakah ada posisi yang diclick mempunyai focus
                if (this.rectanglesFocus[row, column].Stroke != null)
                {
                    this.logicalCDC.move(this.currentFocus.posisi.row, this.currentFocus.posisi.column, row, column);

                    //MAINKAN SUARANYA
                    if (Properties.Settings.Default["Sound"].ToString() == "Enable")
                    {
                        Player.Open(new Uri(@"PindahPiece\" + Properties.Settings.Default["MovingSound"].ToString() + ".wav", UriKind.RelativeOrAbsolute));
                        Player.Play();
                    }
                    move(this.currentFocus.posisi, new Position(row, column));
                    destroyCurrentFocus();

                    switchTurn();
                    writeStatusBar();
                }
            }
        }