Example #1
0
        /*blic void testData()
         * {
         *  boardGame = new char[3,3]{ { 'X', 'O', ' ' }, { 'X', ' ', 'O' }, { 'X', ' ', ' ' } };
         * }*/

        public void DisplayScore()
        {
            Console.WriteLine("\r\nPlayer X has {0} points so far.", player.Xpoints);
            Console.WriteLine("Player O has {0} points so far.", player.Opoints);
            Console.ReadKey();
            ClearExtension.Clear(Console.CursorTop, 6, 0);
        }
Example #2
0
 public void GameWonMessage(ref int player, char plSign)
 {
     player++;
     DisplayBoard();
     Console.ForegroundColor = ConsoleColor.Green;
     if (plSign != 'X') //Right after each players move, sign is turned to opposite one. Thats why it is used != 'X'
     {
         Console.Write("\n\rPlayer X wins! Player X points: {0}\r\n", player);
     }
     else if (plSign != 'O')
     {
         Console.Write("\n\rPlayer O wins! Player O points: {0}\r\n", player);
     }
     gameOver = true;
     Console.ResetColor();
     Console.ReadKey();
     ClearExtension.Clear(startingLine: Console.CursorTop, endingLine: 6, cursorPosX: 0);
 }
Example #3
0
        public void InsertXandO()
        {
            Console.Write("Player {0} turn! \r\n", player.PlayerSign);
            int rowVal = 0;
            int colVal = 0;

            do
            {
                Console.Write("Insert row: ");
                rowVal = Convert.ToInt32(Console.ReadLine());
                rowVal--;
                Console.Write("Insert column: ");
                colVal = Convert.ToInt32(Console.ReadLine());
                colVal--;
            } while (!IsValidInput(rowVal) || !IsValidInput(colVal) || !SpotFree(rowVal, colVal));
            player.NumberOfMoves++;
            boardGame[rowVal, colVal] = player.PlayerSign;
            ClearExtension.Clear(startingLine: Console.CursorTop, endingLine: 6, cursorPosX: 0);
            DisplayBoard();
            //Console.Write("Press <enter> to continue. \r\n");
            //Console.ReadKey();
            ClearExtension.Clear(startingLine: Console.CursorTop, endingLine: 6, cursorPosX: 0);
        }
Example #4
0
        /*******************************************************/

        //Is it Win, Defeat or Draw
        public void GameOutcome()
        {
            for (int row = 0; row < 3; row++)
            {
                //Checks rows
                if (boardGame[row, 0] == 'X' && boardGame[row, 1] == 'X' && boardGame[row, 2] == 'X')
                {
                    GameWonMessage(ref player.Xpoints, player.PlayerSign);
                }
                else if (boardGame[row, 0] == 'O' && boardGame[row, 1] == 'O' && boardGame[row, 2] == 'O')
                {
                    GameWonMessage(ref player.Opoints, player.PlayerSign);
                }

                //checks columns
                for (int col = 0; col < 3; col++)
                {
                    if (boardGame[0, col] == 'X' && boardGame[1, col] == 'X' && boardGame[2, col] == 'X')
                    {
                        GameWonMessage(ref player.Xpoints, player.PlayerSign);
                    }

                    else if (boardGame[0, col] == 'O' && boardGame[1, col] == 'O' && boardGame[2, col] == 'O')
                    {
                        GameWonMessage(ref player.Opoints, player.PlayerSign);
                    }
                }

                //checks diag lef-right
                string topLeftToBottomRight = string.Concat(boardGame[0, 0], boardGame[1, 1], boardGame[2, 2]);
                if (topLeftToBottomRight == "XXX")
                {
                    diagonalWinner = true;
                    GameWonMessage(ref player.Xpoints, player.PlayerSign);
                }
                else if (topLeftToBottomRight == "OOO")
                {
                    diagonalWinner = true;
                    GameWonMessage(ref player.Opoints, player.PlayerSign);
                }

                //check dig right-left
                string topRightToBottomLeft = string.Concat(boardGame[0, 2], boardGame[1, 1], boardGame[2, 0]);
                if (topRightToBottomLeft == "XXX")
                {
                    diagonalWinner = true;
                    GameWonMessage(ref player.Xpoints, player.PlayerSign);
                }
                else if (topRightToBottomLeft == "OOO")
                {
                    diagonalWinner = true;
                    GameWonMessage(ref player.Opoints, player.PlayerSign);
                }

                //check draw
                if (player.NumberOfMoves == 9 && !diagonalWinner)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    gameOver = true;
                    vsCPU    = false;
                    DisplayBoard();
                    Console.Write("Game is draw!");
                    Console.ReadKey();
                    Console.ResetColor();
                    Console.SetCursorPosition(0, Console.CursorTop);
                    Console.Write("\r" + new string(' ', Console.WindowWidth) + "\r");
                    ClearExtension.Clear(startingLine: Console.CursorTop, endingLine: 6, cursorPosX: 0);
                }
            }
        }