예제 #1
0
        public static int PlayTurn(PlayBoardForm i_PlayBoardForm, char i_PlayerSign, int i_Place)
        {
            Player currentPlayer;
            int    nextMove = 0;
            bool   legalMove = true, availableMoveOption = !true;
            Random rnd = new Random();
            int    selectedOptionToPlay;

            currentPlayer       = GameRulesAndLogic.GetPlayerFromSign(i_PlayBoardForm.Board, i_PlayerSign);
            availableMoveOption = GameRulesAndLogic.CheckValidMoves(i_PlayBoardForm.Board, i_PlayerSign);
            if (availableMoveOption)
            {
                if (!currentPlayer.IsComputer && availableMoveOption)
                {
                    do
                    {
                        i_PlayBoardForm.Text = "Otello - " + currentPlayer.Name + "'s Turn";
                        nextMove             = i_Place;
                        legalMove            = GameRulesAndLogic.CheckAndMakeMove(i_PlayBoardForm.Board, nextMove, i_PlayerSign);
                        if (legalMove != true)
                        {
                            i_PlayBoardForm.BoardRefresh();
                            MessageBox.Show("Illigal move!");
                        }
                        else
                        {
                            GameRulesAndLogic.TakeOutAvailableMoveSigns(i_PlayBoardForm.Board);
                        }
                    }while (legalMove != true);
                }
                else if (availableMoveOption)
                {
                    selectedOptionToPlay = GameRulesAndLogic.ComputerRandomeMove(i_PlayBoardForm.Board);
                    nextMove             = selectedOptionToPlay;
                    legalMove            = GameRulesAndLogic.CheckAndMakeMove(i_PlayBoardForm.Board, nextMove, i_PlayerSign);
                    if (legalMove)
                    {
                        GameRulesAndLogic.TakeOutAvailableMoveSigns(i_PlayBoardForm.Board);
                    }
                }

                if (i_PlayBoardForm.Board.SecondPlayer.IsComputer)
                {
                }
            }
            else if (GameRulesAndLogic.ThereIsAvailableMovementsOnBoard(i_PlayBoardForm.Board))
            {
                MessageBox.Show(@"
                                    There is no availbale move for {0}
                                    The turn is going to opponent
                                    Press enter to continue", currentPlayer.Name);
            }

            return(nextMove);
        }
예제 #2
0
        public void BoardRefresh()
        {
            int currentRow = 0, currentCol = 0;

            GameRulesAndLogic.CheckValidMoves(m_Board, m_CurrentPlayerSign);

            for (currentRow = 0; currentRow < m_Board.Rows; currentRow++)
            {
                for (currentCol = 0; currentCol < m_Board.Columns; currentCol++)
                {
                    if (m_Board.PlayBoard[currentRow][currentCol] == m_Board.FirstPlayer.Sign)
                    {
                        if (m_Board.FirstPlayer.Name == "Black")
                        {
                            setBlackButton(m_GameBoardButtons[currentRow][currentCol]);
                        }
                        else
                        {
                            setWhiteButton(m_GameBoardButtons[currentRow][currentCol]);
                        }
                    }
                    else if (m_Board.PlayBoard[currentRow][currentCol] == m_Board.SecondPlayer.Sign)
                    {
                        if (m_Board.SecondPlayer.Name == "Black")
                        {
                            setBlackButton(m_GameBoardButtons[currentRow][currentCol]);
                        }
                        else
                        {
                            setWhiteButton(m_GameBoardButtons[currentRow][currentCol]);
                        }
                    }

                    if (m_Board.PlayBoard[currentRow][currentCol] == m_Board.AvilableMoveSign)
                    {
                        m_GameBoardButtons[currentRow][currentCol].BackColor = Color.LawnGreen;
                        m_GameBoardButtons[currentRow][currentCol].Enabled   = true;
                    }
                    else
                    {
                        if (m_GameBoardButtons[currentRow][currentCol].BackColor == Color.LawnGreen)
                        {
                            m_GameBoardButtons[currentRow][currentCol].BackColor = Color.LightGray;
                        }
                        m_GameBoardButtons[currentRow][currentCol].Enabled = false;
                    }
                }
            }
        }
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent(Board i_Board)
        {
            int     currentRow = 0, currentCol = 0, topLocation = 10, leftLocation = 10, buttonWidth, buttonHeight;
            Size    buttonSize;
            Padding buttonPadding = new Padding(5, 5, 5, 5);
            Font    buttonFont;

            m_Board = i_Board;
            GameRulesAndLogic.CheckValidMoves(m_Board, m_Board.FirstPlayer.Sign);
            this.components         = new System.ComponentModel.Container();
            this.AutoScaleMode      = System.Windows.Forms.AutoScaleMode.Font;
            this.Text               = "Otello - " + m_Board.FirstPlayer.Name + "'s Turn";
            this.m_GameBoardButtons = new System.Windows.Forms.Button[m_Board.Rows][];
            boardInitialized();

            buttonFont = m_GameBoardButtons[m_Board.Rows / 2][m_Board.Columns / 2].Font = new Font(m_GameBoardButtons[m_Board.Rows / 2][m_Board.Columns / 2].Font.FontFamily, 15);
            m_GameBoardButtons[m_Board.Rows / 2][m_Board.Columns / 2].Padding      = buttonPadding;
            m_GameBoardButtons[m_Board.Rows / 2][m_Board.Columns / 2].AutoSizeMode = AutoSizeMode.GrowAndShrink;
            buttonWidth  = m_GameBoardButtons[m_Board.Rows / 2][m_Board.Columns / 2].PreferredSize.Width;
            buttonHeight = m_GameBoardButtons[m_Board.Rows / 2][m_Board.Columns / 2].PreferredSize.Height;
            buttonSize   = new Size(buttonWidth, buttonHeight);

            for (currentRow = 0; currentRow < m_Board.Rows; currentRow++)
            {
                for (currentCol = 0; currentCol < m_Board.Columns; currentCol++)
                {
                    m_GameBoardButtons[currentRow][currentCol].Size     = buttonSize;
                    m_GameBoardButtons[currentRow][currentCol].Font     = buttonFont;
                    m_GameBoardButtons[currentRow][currentCol].Location = new System.Drawing.Point(leftLocation + currentCol * buttonWidth, topLocation + currentRow * buttonHeight);
                    m_GameBoardButtons[currentRow][currentCol].Click   += new System.EventHandler(this.buttonPlayTurn_Click);
                    this.Controls.Add(this.m_GameBoardButtons[currentRow][currentCol]);
                }
            }

            this.Size            = new Size(this.PreferredSize.Width + 10 - (this.DefaultMargin.Vertical / 2), this.PreferredSize.Height + 10 - (this.DefaultMargin.Horizontal / 2));
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.CenterToScreen();
            this.SuspendLayout();
        }
        private void boardInitialized()
        {
            int currentRow = 0, currentCol = 0;

            GameRulesAndLogic.CheckValidMoves(m_Board, m_Board.FirstPlayer.Sign);

            for (currentRow = 0; currentRow < m_Board.Rows; currentRow++)
            {
                m_GameBoardButtons[currentRow] = new System.Windows.Forms.Button[m_Board.Columns];
            }

            for (currentRow = 0; currentRow < m_Board.Rows; currentRow++)
            {
                for (currentCol = 0; currentCol < m_Board.Columns; currentCol++)
                {
                    m_GameBoardButtons[currentRow][currentCol]      = new NewButton();
                    m_GameBoardButtons[currentRow][currentCol].Text = m_Board.EmptySign.ToString();
                    if (m_Board.PlayBoard[currentRow][currentCol] == m_Board.AvilableMoveSign)
                    {
                        m_GameBoardButtons[currentRow][currentCol].BackColor = Color.LawnGreen;
                        m_GameBoardButtons[currentRow][currentCol].Enabled   = true;
                    }
                    else
                    {
                        m_GameBoardButtons[currentRow][currentCol].BackColor = Color.LightGray;
                        m_GameBoardButtons[currentRow][currentCol].Enabled   = false;
                    }
                }
            }

            setBlackButton(m_GameBoardButtons[(m_Board.Rows / 2) - 1][(m_Board.Columns / 2) - 1]);
            setWhiteButton(m_GameBoardButtons[(m_Board.Rows / 2) - 1][m_Board.Columns / 2]);
            setBlackButton(m_GameBoardButtons[m_Board.Rows / 2][m_Board.Columns / 2]);
            setWhiteButton(m_GameBoardButtons[m_Board.Rows / 2][(m_Board.Columns / 2) - 1]);
            m_CurrentPlayerSign = m_Board.FirstPlayer.Sign;
        }