Ejemplo n.º 1
0
        internal static void PrintLastMove(CheckersGameStep io_StepExecuted, Player io_PreviousPlayer)
        {
            System.Text.StringBuilder messageToPrint = new System.Text.StringBuilder();
            string        playerName      = io_PreviousPlayer.PlayerName;
            string        playerTeam      = string.Empty;
            StringBuilder lastMove        = new StringBuilder();
            StringBuilder LastMoveMessage = new System.Text.StringBuilder();

            if (io_PreviousPlayer.Team == ePlayerOptions.Player1)
            {
                playerTeam = s_Team1;
            }
            else
            {
                playerTeam = s_Team2;
            }
            lastMove.AppendFormat(
                "{0}{1}{2}{3}{4}",
                (char)(io_StepExecuted.CurrentPosition.XCoord + 'A'),
                (char)(io_StepExecuted.CurrentPosition.YCooord + 'a'),
                k_ArrowSign.ToString(),
                (char)(io_StepExecuted.RequestedPosition.XCoord + 'A'),
                (char)(io_StepExecuted.RequestedPosition.YCooord + 'a'));
            LastMoveMessage.AppendFormat(s_LastMoveMessageFormat, playerTeam);
            messageToPrint.AppendFormat("{0}{1}{2}", playerName, LastMoveMessage, lastMove);
            Console.WriteLine(messageToPrint);
        }
Ejemplo n.º 2
0
        public static CheckersGameStep ReadGameMove(ref string io_UserInput)
        {
            bool             isInputOk = false;
            Point            startPosition;
            Point            requestedPosition;
            CheckersGameStep requestedStep = new CheckersGameStep();
            Player           currentPlayer = SessionData.GetCurrentPlayer();


            string playerNameHolder = currentPlayer.PlayerName;
            string playersTeam      = string.Empty;

            System.Text.StringBuilder messageToPrint   = new System.Text.StringBuilder();
            System.Text.StringBuilder enterMoveMessage = new System.Text.StringBuilder();


            if (currentPlayer.Team == ePlayerOptions.Player1)
            {
                playersTeam = s_Team1;
            }
            else
            {
                playersTeam = s_Team2;
            }
            enterMoveMessage.AppendFormat(s_EnterMoveMessageFormat, playersTeam);
            messageToPrint.AppendFormat("{0}{1}", playerNameHolder, enterMoveMessage);

            while (!isInputOk)
            {
                Console.Write(messageToPrint);
                io_UserInput = Console.ReadLine();
                isInputOk    = CheckStepInputValidity(io_UserInput);
                if (!isInputOk)
                {
                    Console.WriteLine(s_InvalidInputMessage);
                }
            }

            if (io_UserInput.Equals(k_QuitChar))
            {
                if (SessionData.GetCurrentPlayer().NumberOfSoldiers < SessionData.GetOtherPlayer().NumberOfSoldiers)
                {
                    requestedStep.WantsToQuitIndicator = true;
                }
            }
            else
            {
                MakePointsFromString(io_UserInput, out startPosition, out requestedPosition);
                requestedStep.CurrentPosition   = startPosition;
                requestedStep.RequestedPosition = requestedPosition;
            }

            return(requestedStep);
        }
Ejemplo n.º 3
0
        public void RunCheckersGame()
        {
            string userMoveInput = string.Empty;

            // First Part - Get game initial values from the user
            InitialGameSetting GameSettings;

            UI.ReadGameInitialInputFromUser(out GameSettings);

            // Second Part - initialize the checkers game values according to the user's choice
            SessionData.initializeSessionData(GameSettings);
            SessionData.InitializePlayers(GameSettings);
            m_CheckersBoard.InitializeCheckersBoard();
            Ex02.ConsoleUtils.Screen.Clear();
            UI.PrintCheckersBoard(m_CheckersBoard);

            // Third Part - Game Loop
            while (m_gameState == eGameState.KeepGoing || m_gameState == eGameState.StartOver)
            {
                if (m_gameState == eGameState.StartOver)
                {
                    // In case asked to start a new game - initialize again
                    InitializeAnotherGame(GameSettings);
                    Ex02.ConsoleUtils.Screen.Clear();
                    UI.PrintCheckersBoard(m_CheckersBoard);
                }

                m_currentActivePlayer = SessionData.GetCurrentPlayer();
                m_currentActivePlayer.updateArmy(m_CheckersBoard); // update the possible movement for each soldier in the current player's army
                m_isRequestedMoveLegal = false;

                while (!m_isRequestedMoveLegal)
                {
                    // Read a game movement from the user
                    if (m_currentActivePlayer.Team != ePlayerOptions.ComputerPlayer)
                    {
                        m_RequestedMove = UI.ReadGameMove(ref userMoveInput);
                        if (m_RequestedMove.WantsToQuitIndicator)
                        {
                            break;
                        }
                    }
                    else
                    {
                        // choose a step to execute randomly for PC player
                        m_RequestedMove = m_currentActivePlayer.GetRandomMoveForPc();
                    }

                    // Sort the move type - EatMove or RegularMove
                    m_RequestedMove.MoveTypeInfo = m_CheckersBoard.SortMoveType(m_RequestedMove, m_currentActivePlayer);

                    if (m_RequestedMove.MoveTypeInfo.TypeIndicator != eMoveTypes.Undefined || m_RequestedMove.WantsToQuitIndicator)
                    {
                        m_isRequestedMoveLegal = true;
                    }

                    if (!m_isRequestedMoveLegal)
                    {
                        UI.PrintErrorMessage();
                    }
                }

                if (!m_RequestedMove.WantsToQuitIndicator)
                {
                    // user doesn't want to quit - execute a move!
                    m_currentActivePlayer.MakeAMove(m_RequestedMove, m_CheckersBoard); // at the end of this method - we are ready to get the next move in the game
                    Ex02.ConsoleUtils.Screen.Clear();
                    UI.PrintCheckersBoard(m_CheckersBoard);
                    UI.PrintLastMove(m_RequestedMove, m_currentActivePlayer);
                    m_gameState = SessionData.checkGameState();
                }
                else
                {
                    if (SessionData.m_CurrentActivePlayer == ePlayerOptions.Player1)
                    {
                        m_gameState = eGameState.player1Quit;
                    }
                    else
                    {
                        m_gameState = eGameState.player2Quit;
                    }
                }

                if (m_gameState != eGameState.KeepGoing)
                {
                    // calculate and print score in case of finished game
                    SessionData.CalculateScore(m_gameState);
                    UI.PrintGameResult(m_gameState);
                    m_gameState = UI.CheckIfPlayerWantsAnotherGame();
                }
            }
        }
Ejemplo n.º 4
0
        public static CheckersGameStep ReadAndCheckInput()
        {
            string[] inputs     = { "Af>Be", "Hc>Gd", "Be>Cd", "Bc>De", "Ef>Cd", "Dc>Be", "Cf>Ad", "Gd>He", "Gf>Fe", "He>Gf", "Fg>He", "Ab>Bc", "Fe>Gd", "Gb>Hc", "Bg>Cf", "Hc>Fe", "Dg>Ef", "Fe>Dg", "q" };
            bool[]   validation = new bool[3];

            string i_inputFromUser = string.Empty;

            Console.Write("please enter a  legal move: ");
            //goto xy clear and that stuff


            CheckersGameStep result = new CheckersGameStep();

            Point      currentPoint = new Point();
            Point      NextPoint    = new Point();
            bool       valid        = false;
            const bool quit         = true;

            while (!valid)
            {
                //i_inputFromUser = Console.ReadLine();

                validation[0] = false;
                validation[1] = false;
                validation[2] = false;

                i_inputFromUser = inputs[i];



                string processedString = i_inputFromUser.Replace(" ", "");
                if (i_inputFromUser != "q")
                {
                    if (char.IsUpper(processedString[0]) && char.IsUpper(processedString[3]))
                    {
                        currentPoint.XCoord = (int)(processedString[0] - 'A');
                        NextPoint.XCoord    = (int)(processedString[3] - 'A');
                        validation[0]       = true;
                    }
                    if (char.IsLower(processedString[1]) && char.IsLower(processedString[4]))
                    {
                        currentPoint.YCooord = (int)(processedString[1] - 'a');
                        NextPoint.YCooord    = (int)(processedString[4] - 'a');

                        validation[1] = true;
                    }
                    if (processedString[2] == '>')
                    {
                        validation[2] = true;
                    }
                    valid = (validation[0] && validation[1] && validation[2]);

                    if (!valid)
                    {
                        Output.InputException();
                    }
                    i++;
                }
                else
                {
                    result = CheckersGameStep.CreateCheckersGameStep(currentPoint, NextPoint, quit);
                    valid  = true;
                }
            }

            result.CurrentPosition   = currentPoint;
            result.RequestedPosition = NextPoint;
            return(result);
        }