예제 #1
0
        //AI Turn Taking Process
        private void TakeAITurn()
        {
            //Location of the AI's piece
            int[] aIPiece = pL.AITurn();

            //Updating the board based on the AI's move
            Ellipse aIEllipse = (Ellipse)GameGrid.Children[aIPiece[0] + aIPiece[1] * GameGrid.Rows];

            aIEllipse.Fill    = player2;
            aIEllipse.Opacity = 100;
            HookUpBoards();

            //Checks for five-in-a-row wins for the AI
            if (pL.FiveInARow(aIPiece[0], aIPiece[1]) && !didSomeoneWin)
            {
                GameOverWindow gameOverWindow = new GameOverWindow("The AI Won 5 in a row", WithAI, GameGrid.Rows);
                gameOverWindow.Show();
                didSomeoneWin = true;
                Close();
            }

            //Checks for captures for the AI
            List <int> captureLocation = pL.Capture(aIPiece[0], aIPiece[1]);

            if (captureLocation.Count > 0)
            {
                for (int pieceCount = 0; pieceCount < captureLocation.Count; pieceCount = pieceCount + 2)
                {
                    Ellipse ellipse1 = (Ellipse)GameGrid.Children[(captureLocation[pieceCount + 1] + captureLocation[pieceCount] * GameGrid.Rows)];
                    ellipse1.Opacity = 0;
                    ellipse1.Fill    = EmptySpace;
                }
            }

            //Checks for tessera for the AI
            if (pL.Tessera(aIPiece[0], aIPiece[1]))
            {
                lblTessera.Content    = $"{lblPlayer2Name.Content} Tessera";
                lblTessera.Visibility = Visibility.Visible;
            }
            HookUpBoards();

            //Ends the turn
            pL.TurnOver();
        }
예제 #2
0
        //Updates the window when a player clicks the board
        //Valid and invalid moves are handled
        private void MouseLeftClick_Down(object sender, RoutedEventArgs e)
        {
            //Hides the tria label until a tria is formed
            lblTria.Visibility = Visibility.Hidden;
            lblTria.Content    = "";

            //This check is for tournament rules
            if (player1FirstTurn)
            {
                PlayersFirstTurn((Ellipse)sender);
            }
            else if ((isBlackFirst && !pL.isPlayer2Turn) || (!isBlackFirst && pL.isPlayer2Turn) || (!player1FirstTurn && !player1SecondTurn))
            {
                Ellipse ellipse = (Ellipse)sender;
                //The opacity of the piece is what we are checking
                //to see if it has been placed already
                if (ellipse.Opacity == 0)
                {
                    //Changes the piece placed to match the player's color
                    if (pL.isPlayer2Turn)
                    {
                        ellipse.Fill    = player2;
                        ellipse.Opacity = 100;
                        pL.TurnOver();
                    }
                    else
                    {
                        ellipse.Fill    = player1;
                        ellipse.Opacity = 100;
                        pL.TurnOver();
                    }
                    lblTimer.Content = "20";
                    HookUpBoards();
                    int[] pieceLocation = FindPiece(ellipse);
                    //Checks for captures and handles them
                    List <int> captureLocation = pL.Capture(pieceLocation[0], pieceLocation[1]);
                    if (captureLocation.Count > 0)
                    {
                        for (int pieceCount = 0; pieceCount < captureLocation.Count; pieceCount = pieceCount + 2)
                        {
                            Ellipse ellipse1 = (Ellipse)GameGrid.Children[(captureLocation[pieceCount + 1] + captureLocation[pieceCount] * GameGrid.Rows)];
                            ellipse1.Opacity = 0;
                            ellipse1.Fill    = EmptySpace;
                        }
                    }
                    HookUpBoards();
                    //Takes the AI's turn
                    if (WithAI)
                    {
                        HookUpBoards();
                        TakeAITurn();
                    }
                    //Checks the entire board for a tria
                    string triaString;
                    for (int i = 0; i < GameGrid.Rows; i++)
                    {
                        for (int j = 0; j < GameGrid.Columns; j++)
                        {
                            triaString = pL.Tria(j, i);
                            if (!string.IsNullOrEmpty(triaString))
                            {
                                if (triaString == "Player1")
                                {
                                    lblTria.Content = $"{lblPlayer1Name.Content} has a Tria";
                                }
                                else if (triaString == "Player2")
                                {
                                    lblTria.Content = $"{lblPlayer2Name.Content} has a Tria";
                                }
                                lblTria.Visibility = Visibility.Visible;
                            }
                        }
                    }
                    //Checks if the piece placed forms a tessera
                    if (pL.Tessera(pieceLocation[0], pieceLocation[1]))
                    {
                        if (!pL.isPlayer2Turn)
                        {
                            lblTessera.Content    = $"{lblPlayer1Name.Content} Tessera";
                            lblTessera.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            lblTessera.Content    = $"{lblPlayer2Name.Content} Tessera";
                            lblTessera.Visibility = Visibility.Visible;
                        }
                    }
                    else
                    {
                        lblTessera.Visibility = Visibility.Hidden;
                    }
                    HookUpBoards();
                    //Checks for a five-in-a-row win
                    //Both scenarios will open the Game Over Window
                    if (pL.FiveInARow(pieceLocation[0], pieceLocation[1]))
                    {
                        if (!pL.isPlayer2Turn && !didSomeoneWin)
                        {
                            GameOverWindow gameOverWindow = new GameOverWindow($"{tbxPlayer1Name.Text} Wins 5 in a row!", WithAI, GameGrid.Rows);
                            gameOverWindow.Show();
                            didSomeoneWin = true;
                            Close();
                        }
                        else if (!didSomeoneWin)
                        {
                            GameOverWindow gameOverWindow = new GameOverWindow($"{tbxPlayer2Name.Text} Wins 5 in a row!", WithAI, GameGrid.Rows);
                            gameOverWindow.Show();
                            didSomeoneWin = true;
                            Close();
                        }
                    }
                    //Handles tournament rule
                    if (player1FirstTurn && !pL.isPlayer2Turn)
                    {
                        player1FirstTurn  = false;
                        player1SecondTurn = true;
                    }
                }
            }
            else
            {
                //Enforces the second turn rules based
                //on tournament rules
                PlayersSecondTurn((Ellipse)sender);
            }
            //If either player has captured more
            //than 5 pieces, the game ends and that
            //player wins.
            //Both scenarios open the Game Over Window
            if (pL.player1Captures >= 5 && !didSomeoneWin)
            {
                GameOverWindow gameOverWindow = new GameOverWindow($"{tbxPlayer1Name.Text} Wins w/ 5 captures!", WithAI, GameGrid.Rows);
                gameOverWindow.Show();
                didSomeoneWin = true;
                Close();
            }
            else if (pL.player2Captures >= 5 && !didSomeoneWin)
            {
                GameOverWindow gameOverWindow = new GameOverWindow($"{tbxPlayer2Name.Text} Wins w/ 5 captures!", WithAI, GameGrid.Rows);
                gameOverWindow.Show();
                didSomeoneWin = true;
                Close();
            }
            //Toggles the turn indicator
            ChangeEllipseColor();
        }