Esempio n. 1
0
 // starts a new game if the users say so
 private void startNewGame()
 {
     Ex02.ConsoleUtils.Screen.Clear();
     Console.WriteLine("-- We are starting a new game -- GOOD LUCK !");
     m_Table = new CheckersTable(m_TableSize, m_NumOfPlayers);
     m_Table.printTable();
     m_Turn = m_PlayerOne.m_Name + "\'s";
     playGame();
 }
Esempio n. 2
0
 private void initGame()
 {
     m_Table = new CheckersTable(m_TableSize, m_NumOfPlayers);
     m_Table.printTable();
     m_PlayerOne = new Player(m_FirstUserName, 0, 0);
     m_PlayerTwo = new Player(m_SecondUserName, 0, 1);
     m_Turn      = m_FirstUserName + "\'s";
     playGame();
 }
Esempio n. 3
0
        // play a game until someone wins / draw
        private void playGame()
        {
            bool hasNoLegalMoves = false;
            bool surrender       = false;

            // we start with X player
            int    playerIdTurn = 0;
            string moveMessage  = string.Empty;

            CheckerSquare[] cSquare;
            string[]        moveMessages;

            // play the game till its over
            while (true)
            {
                CheckersLogic logic = new CheckersLogic(m_Table, getPlayerById(playerIdTurn));

                //// if first player can eat again, we give him another turn
                if (m_CanEatAgain && playerIdTurn == 0)
                {
                    m_CanEatAgain = false;
                }
                else
                {
                    Console.WriteLine(m_Turn + " turn " + getMoveType(playerIdTurn) + ":");

                    // if we are playing against the computer
                    if (m_Turn.Equals("Computer\'s"))
                    {
                        cSquare         = m_Table.GetCheckerSquares(1);
                        moveMessages    = logic.getPossibleMovesForPlayer(ref cSquare);
                        hasNoLegalMoves = logic.hasNoLegalMoves(moveMessages);
                        playerIdTurn    = 1;
                        if (hasNoLegalMoves)
                        {
                            break;
                        }

                        Random random = new Random();
                        moveMessage = moveMessages[random.Next(moveMessages.Length)];
                        if (m_CanEatAgain)
                        {
                            moveMessage   = m_EatMove;
                            m_CanEatAgain = false;
                        }
                        else
                        {
                            string eat = string.Empty;

                            // filter the non-legal moves
                            while (moveMessage.Equals("Aa>Aa"))
                            {
                                moveMessage = moveMessages[random.Next(moveMessages.Length)];
                            }

                            eat = logic.checkIfCanEat(moveMessage);

                            // prefer the eating move
                            moveMessage = eat.Equals(string.Empty) ? moveMessage : eat;
                        }
                    }
                    else
                    {
                        // player's (non-computer) turn
                        cSquare         = m_Table.GetCheckerSquares(playerIdTurn);
                        moveMessages    = logic.getPossibleMovesForPlayer(ref cSquare);
                        hasNoLegalMoves = logic.hasNoLegalMoves(moveMessages);
                        if (hasNoLegalMoves)
                        {
                            break;
                        }

                        moveMessage = getLegalMoveMessage(playerIdTurn);

                        // if players wants and can quit
                        if (moveMessage.Equals("Q"))
                        {
                            surrender = true;
                            break;
                        }
                    }

                    int moveIndicator = m_Table.move(moveMessage, getPlayerById(playerIdTurn));
                    if (moveIndicator == 0)
                    {
                        Console.WriteLine("Please enter legal move");
                    }
                    else
                    {
                        // made a move, clear the screen
                        Ex02.ConsoleUtils.Screen.Clear();

                        // print state after the move
                        m_Table.printTable();
                        Console.WriteLine(m_Turn + " move was: " + getMoveType(playerIdTurn) + ": " + moveMessage);

                        // checks if move was eatmove
                        if (logic.isEatMove(moveMessage))
                        {
                            logic.m_Player.m_JustAte = true;

                            // checks if the player can eat again, if he can, camEat will hold the new move
                            string canEat = logic.canEatAgain(moveMessage);
                            if (canEat.Equals(string.Empty))
                            {
                                // if he cant eat anymore, just switch turns
                                logic.m_Player.m_JustAte = false;
                                switchTurn(ref playerIdTurn);
                                m_CanEatAgain = false;
                                m_EatMove     = string.Empty;
                            }
                            else
                            {
                                m_CanEatAgain = true;
                                m_EatMove     = canEat;
                            }
                        }
                        else
                        {
                            // if it wasnt eatmove, checks if he could eat someone and didnt do it, if so he lose man
                            switchTurn(ref playerIdTurn);
                        }
                    }
                }

                if (m_Table.m_NumO == 0 || m_Table.m_NumX == 0 || surrender || hasNoLegalMoves)
                {
                    Console.WriteLine("GAME OVER !");
                    break;
                }
            }

            // something got us out of the game, check what happend
            calculatePointsAfterGame();
            printResults();
            checkIfPlayAgain();
        }