Example #1
0
        //check the rule to finish the game
        public bool finishGame(int x, int y)
        {
            GomokuRule r             = new GomokuRule();
            CommandR   ruleOnCommand = new GomokuRuleOnCommand(r);
            Rule       rule          = new Rule();

            rule.setCommand(ruleOnCommand);

            //check the rule
            //can finish
            if (rule.checkFinishCondition())
            {
                //display the winner
                WriteLine("The winner is " + Piece.Instance.currentPlayer);
                Piece.Instance.winner = Piece.Instance.currentPlayer;
                //display the score
                displayScore();
                return(true);
            }
            //continue the game
            else
            {
                return(false);
            }
        }
Example #2
0
        //move position
        public int[] movePosition()
        {
            int x, y;

            int[]      position      = new int[2];
            GomokuRule r             = new GomokuRule();
            CommandR   ruleOnCommand = new GomokuRuleOnCommand(r);
            Rule       rule          = new Rule();

            rule.setCommand(ruleOnCommand);

            // if play with human player(object diagram and sequence diagram part2)
            if (PlayerData.Instance.playerType == 1)
            {
                while (true)
                {
                    //input x position
                    WriteLine("Enter the position x>>");
                    x = Convert.ToInt32(ReadLine()) - 1;
                    //check the value
                    while (x > Piece.Instance.boardSize || x < 0)
                    {
                        WriteLine("invalid position");
                        WriteLine("Enter the position x>>");
                        x = Convert.ToInt32(ReadLine()) - 1;
                    }
                    //input y position
                    WriteLine("Enter the position y>>");
                    y = Convert.ToInt32(ReadLine()) - 1;
                    //check the value
                    while (y > Piece.Instance.boardSize || y < 0)
                    {
                        WriteLine("invalid position");
                        WriteLine("Enter the position y>>");
                        y = Convert.ToInt32(ReadLine()) - 1;
                    }
                    // check the position. if it already used player should be select another position
                    if (checkPosition(x, y))
                    {
                        break;
                    }
                }

                // store the current position information
                if (Piece.Instance.currentPlayer == 1)
                {
                    Piece.Instance.position[x, y] = 1;
                }

                else if (Piece.Instance.currentPlayer == 2)
                {
                    Piece.Instance.position[x, y] = 2;
                }

                position[0] = x;
                position[1] = y;

                //save the current player, poistion x, y and mark for redo and undo
                GameData.Instance.redo[0, 0] = Piece.Instance.currentPlayer;
                GameData.Instance.redo[0, 1] = x;
                GameData.Instance.redo[0, 2] = y;
                GameData.Instance.redo[0, 3] = Piece.Instance.position[x, y];
            }
            //if play with computer player
            else
            {
                while (true)
                {
                    //palyer select the position
                    if (Piece.Instance.currentPlayer == 1)
                    {
                        WriteLine("Enter the position x>>");
                        x = Convert.ToInt32(ReadLine()) - 1;

                        while (x > Piece.Instance.boardSize || x < 0)
                        {
                            WriteLine("invalid position");
                            WriteLine("Enter the position x>>");
                            x = Convert.ToInt32(ReadLine()) - 1;
                        }

                        WriteLine("Enter the position y>>");
                        y = Convert.ToInt32(ReadLine()) - 1;
                        while (y > Piece.Instance.boardSize || y < 0)
                        {
                            WriteLine("invalid position");
                            WriteLine("Enter the position y>>");
                            y = Convert.ToInt32(ReadLine()) - 1;
                        }

                        if (checkPosition(x, y))
                        {
                            break;
                        }
                    }
                    // if computer's turn
                    else
                    {
                        //get position from rule
                        int[] point = rule.computerMove();
                        x = point[0];
                        y = point[1];
                        if (checkPosition(x, y))
                        {
                            break;
                        }
                    }
                }
            }

            //save the position
            if (Piece.Instance.currentPlayer == 1)
            {
                Piece.Instance.position[x, y] = 1;
            }

            else if (Piece.Instance.currentPlayer == 2)
            {
                Piece.Instance.position[x, y] = 2;
            }

            //save the game data for redo and undo
            GameData.Instance.redo[0, 0] = Piece.Instance.currentPlayer;
            GameData.Instance.redo[0, 1] = x;
            GameData.Instance.redo[0, 2] = y;
            GameData.Instance.redo[0, 3] = Piece.Instance.position[x, y];

            position[0] = x;
            position[1] = y;

            GameData.Instance.x = position[0] + 1;
            GameData.Instance.y = position[1] + 1;

            return(position);
        }