Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If/else statement is returning false, even though the condition should be true. (C#)

I’m currently trying to build a tic-tac-toe game in C# as of right now. Now, I’ve gotten most of the whole game set up. Below is the complete code for my project as of rn:

// Main code.
Game newGame = new();
newGame.RunGame();


// Classes
internal class Game
{
    private Player[] Players;
    private Updater GameUpdater;
    private Board GameBoard;

    public Game()
    {
        Players = new Player[2] { new(PlayerSymbol.X), new(PlayerSymbol.O) };
        GameUpdater = new();
        GameBoard = new();
    }

    public void RunGame()
    {
        while (true)
        {
            GameBoard.DisplayBoard();
            int currentPlayer = GameUpdater.SendPlayerTurnInfo();
            int playerInput = Players[currentPlayer].GetUserInput(); // The position the player wants to place their X/O
            bool playerInputtedValidNumber = GameUpdater.VerifyUserHasInputValidNumber(playerInput);

            if (playerInputtedValidNumber)
            {
                bool playerInputtedUnusedNumber = GameUpdater.VerifyUserHasInputUnusedSpot(GameBoard.SendBoardPiecesData(--playerInput));

                if (playerInputtedUnusedNumber)
                {
                    PlayerSymbol currentPlayerSymbol = Players[currentPlayer].SendPlayerSymbol(); // The symbol of the current player.
                    GameBoard.UpdateBoardPiecesData(playerInput, currentPlayerSymbol);
                }
                else
                {
                    Console.WriteLine("This position has already been used!");
                    continue;
                }
            }
            else
            {
                Console.WriteLine("Inputted an invalid position!");
                continue;
            }
        }
    }
}

internal class Board
{
    private string[]? BoardPieces;
    private string? BoardDisplay;

    public Board()
    {
        BoardPieces = new string[9] { " ", " ", " ", " ", " ", " ", " ", " ", " " };
        BoardDisplay = $" {BoardPieces[0]} | {BoardPieces[1]} | {BoardPieces[2]} \n---+---+---\n {BoardPieces[3]} | {BoardPieces[4]} | {BoardPieces[5]} \n---+---+---\n {BoardPieces[6]} | {BoardPieces[7]} | {BoardPieces[8]} ";
    }

    public void DisplayBoard()
    {
        Console.WriteLine(BoardDisplay);
    }

    public string SendBoardPiecesData(int userInput)
    {
        return BoardPieces[userInput]; // No issue with null since the number will be automatically checked for null beforehand.
    }

    public void UpdateBoardPiecesData(int userInput, PlayerSymbol playerSymbol)
    {
        BoardPieces[userInput] = $"{playerSymbol}";
        BoardDisplay = $" {BoardPieces[0]} | {BoardPieces[1]} | {BoardPieces[2]} \n---+---+---\n {BoardPieces[3]} | {BoardPieces[4]} | {BoardPieces[5]} \n---+---+---\n {BoardPieces[6]} | {BoardPieces[7]} | {BoardPieces[8]} ";
    }
 }

internal class Updater
{
    private int PlayerIndicator;

    public Updater()
    {
        PlayerIndicator = 1;
    }

    public int SendPlayerTurnInfo()
    {
        if (PlayerIndicator == 1)
        {
            PlayerIndicator = 0;
            return PlayerIndicator;
        } else
        {
            PlayerIndicator = 1;
            return PlayerIndicator;
        }
    }

    public bool VerifyUserHasInputValidNumber(int userInput)
    {
        Console.WriteLine(userInput);
        if (userInput >= 0 || userInput <= 10)
        {
            return false;
        } else
        {
            return true;
        }
    }

    public bool VerifyUserHasInputUnusedSpot(string userInput)
    {
        if (userInput == "X" || userInput == "O") return false;
        else return true;
    }
}

internal class Player
{
    private PlayerSymbol PlayerSymbol;

    public Player(PlayerSymbol playerSymbol)
    {
        PlayerSymbol = playerSymbol;
    }

    public int GetUserInput()
    {
        Console.Write($"It is currently {PlayerSymbol}'s turn. Which position would you like to play at? ");
        string? input = Console.ReadLine();
        bool isNumerical = int.TryParse(input, out int _);

        while (!isNumerical)
        {
            Console.Write("Invalid input, please input a number. ");
            input = Console.ReadLine();
            isNumerical = int.TryParse(input, out int _);
        }

        return int.Parse(input);
    }

    public PlayerSymbol SendPlayerSymbol()
    {
        return PlayerSymbol;
    }
}


// Enumerations
enum PlayerSymbol
{
    X,
    O
}

Right now, the main issue with this code will be inside the Updater class.

I have a VerifyUserHasInputValidNumber(int userInput) method, taking in the number input that the player has taken, and returning a true or false depending on whether it fits a range of being a value between 1-9.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public bool VerifyUserHasInputUnusedSpot(string userInput)
    {
        if (userInput == "X" || userInput == "O") return false;
        else return true;
    }

For some reason though, even though I can confirm that the userInput parameter is being inputted correctly (the Console.WriteLine in the 1st line of the method), it will still produce a false and will tell the user that they’ve inputted an invalid position.

public void RunGame()
    {
        while (true)
        {
            GameBoard.DisplayBoard();
            int currentPlayer = GameUpdater.SendPlayerTurnInfo();
            int playerInput = Players[currentPlayer].GetUserInput(); // THIS IS THE NUMBER THAT'S BEING INPUTTED INTO THE METHOD THAT I'M HAVING AN ISSUE WITH (the VerifyUserHasInputValidNumber(playerInput); method)
            bool playerInputtedValidNumber = GameUpdater.VerifyUserHasInputValidNumber(playerInput); // Should be true if I input any number between 1-9.

            if (playerInputtedValidNumber) 
            {
                bool playerInputtedUnusedNumber = GameUpdater.VerifyUserHasInputUnusedSpot(GameBoard.SendBoardPiecesData(--playerInput));

                if (playerInputtedUnusedNumber)
                {
                    PlayerSymbol currentPlayerSymbol = Players[currentPlayer].SendPlayerSymbol(); // The symbol of the current player.
                    GameBoard.UpdateBoardPiecesData(playerInput, currentPlayerSymbol);
                }
                else
                {
                    Console.WriteLine("This position has already been used!");
                    continue;
                }
            }
            else
            {
                Console.WriteLine("Inputted an invalid position!"); // THIS, however, is what is being logged into the console.
                continue;
            }
        }
    }

I don’t know if it may be a memory leak issue, or if I may have some sort of incorrect formatting, but if I could get any help with this issue, I would greatly appreciate it.

>Solution :

this test

  if (userInput >= 0 || userInput <= 10)

is always true. What value do you think will be false?

I assume you want

 if (userInput >= 0 && userInput <= 10)
      return true;
 else return false;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading