Problem with fortune telling game C #

I keep getting stackOverflow error (no pun intended): "Unhandled exception of type" System.StackOverflowException "occurred in NumberGuessingGame.exe"

I suppose this would help if you saw my code and alswo, where its ocurring:

Source - testClass:

namespace NumberGuessingGame_08029490
{
public class testClass : Form1
{
    public bool _gameWon;
    public bool _gameRunning;
    public int _number;
    public int _guessesRemaining;

    public int guessesRemaining
    {
        get { return _guessesRemaining; }
    }

    public bool gameEnded
    {
        get { return !_gameRunning; }
    }

    public bool gameWon
    {
        get { return _gameWon; }
    }
    public testClass()
    {
        _gameRunning = false;
        _gameWon = false;
    }

    public void saveNewTestGame(int numberGuesses)
    {

        if (numberGuessesComboBox.SelectedIndex == 0)
        {
            numberGuesses = 1;
        }
        if (numberGuessesComboBox.SelectedIndex == 1)
        {
            numberGuesses = 3;
        }
        if (numberGuessesComboBox.SelectedIndex == 2)
        {
            numberGuesses = 5;
        }

        _guessesRemaining = numberGuesses;
        _gameRunning = true;
    }

    public bool makeGuess(int guessNumber)
    {
        if (_gameRunning)
        {
            _guessesRemaining--;
            if (_guessesRemaining <= 0)
            {
                _gameWon = false;
                _gameRunning = false;
                return false;


            }
            if (guessNumber == _number)
            {
                _gameWon = true;
                return true;
            }
            if (guessNumber > _number)
            {
                guessResultTextBox.Text = "Your Guess is too high, try again";
                _gameWon = false;
                return false;
            }
            if (guessNumber < _number)
            {
                guessResultTextBox.Text = "Your Guess is too low, try again";
                _gameWon = false;
                return false;
            }
            else
            {
                return false;
            }
        }

        else
        {
            throw new Exception("The game is not running. Call newGame() before making a guess.");

        }

    }
}
}

Source - gameClass:

namespace NumberGuessingGame_08029490
{
public class gameClass : Form1
{

    public bool _gameWon;
    public bool _gameRunning;
    public static Random randomNum = new Random();
    public int _number;
    public int _guessesRemaining;

    public int guessesRemaining
    {
        get { return _guessesRemaining; }
    }

    public bool gameEnded
    {
        get { return !_gameRunning; }
    }

    public bool gameWon
    {
        get { return _gameWon; }
    }

    public gameClass()
    {
        _gameRunning = false;
        _gameWon = false;
    }
    public void saveNewGame(int numberGuesses)
    {

        if (numberGuessesComboBox.SelectedIndex == 0)
        {
            numberGuesses = 1;
        }
        if (numberGuessesComboBox.SelectedIndex == 1)
        {
            numberGuesses = 3;
        }
        if (numberGuessesComboBox.SelectedIndex == 2)
        {
            numberGuesses = 5;
        }
        if (rangeNumbersComboBox.SelectedIndex == 0)
        {
            int randomNumFive = randomNum.Next(1, 5);
            randomNumFive = _number;
        }
        if (rangeNumbersComboBox.SelectedIndex == 1)
        {
            int randomNumTen = randomNum.Next(1, 10);
            randomNumTen = _number;
        }
        if (rangeNumbersComboBox.SelectedIndex == 2)
        {
            int randomNumTwenty = randomNum.Next(1, 20);
            randomNumTwenty = _number;
        }
        _guessesRemaining = numberGuesses;
        _gameRunning = true;
    }

    public bool makeGuess(int guessNumber)
    {
        if (_gameRunning)
        {
            _guessesRemaining--;
            if (_guessesRemaining <= 0)
            {
                _gameWon = false;
                _gameRunning = false;
                return false;

            }
            if (guessNumber == _number)
            {
                _gameWon = true;
                return true;
            }
            if (guessNumber > _number)
            {
                guessResultTextBox.Text = "Your Guess is too high, try again";
                _gameWon = false;
                return false;
            }
            if (guessNumber < _number)
            {
                guessResultTextBox.Text = "Your Guess is too low, try again";
                _gameWon = false;
                return false;
            }
            else
            {
                return false;
            }

        }

        else
        {
            throw new Exception("The game is not running. Call newGame() before making a guess.");

        }

    }
}
}

Source - Form1:

namespace NumberGuessingGame_08029490
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    testClass newTest = new testClass();
    gameClass newGame = new gameClass();


    private void saveButton_Click(object sender, EventArgs e)
    {

        if (testCheckBox.Checked == true)
        {
            int numberGuesses = Convert.ToInt32(numberGuessesComboBox.SelectedIndex);
            int _number = Convert.ToInt32(testNumberTextBox.Text);

            newTest.saveNewTestGame(numberGuesses);

        }
        if (testCheckBox.Checked == false)
        {
            int numberGuesses = Convert.ToInt32(numberGuessesComboBox.SelectedIndex);
            int _number = Convert.ToInt32(rangeNumbersComboBox.SelectedIndex);

            newGame.saveNewGame(numberGuesses);
        }

    }

    private void guessButton_Click(object sender, EventArgs e)
    {

        if (testCheckBox.Checked == true)
        {
            int guessNumber = Convert.ToInt32(guessNumberTextBox.Text);
            bool correctAnswer = newTest.makeGuess(guessNumber);

            if (correctAnswer)
            {
                MessageBox.Show("Weldone, you Won!!");
            }

            // if (game.GameEnded)
            //  {
            // disable guess button, show loss label
            //  }
        }
        if (testCheckBox.Checked == false)
        {
            int guessNumber = Convert.ToInt32(guessNumberTextBox.Text);
            bool correctAnswer = newGame.makeGuess(guessNumber);

            if (correctAnswer)
            {
                MessageBox.Show("Weldone, you Won!!");
            }

            // if (game.GameEnded)
            //  {
            // disable guess button, show loss label
            //  }
        }
    }

}
}

EDIT: No longer a StackOverflowException, code errors:

Thank you all for your help <3

Removing the “Form 1” from above. He seems to have solved the problem at the moment, but now that the combobox variables, text fields are no longer inherited, I can’t use them in the code, so I can’t check to see if the solution works, any ideas on how they can be used?

Error   1   The name 'numberGuessesComboBox' does not exist in the current context  E:\Projects\NumberGuessingGame\NumberGuessingGame\testClass.cs  46  17  NumberGuessingGame

Error   7   The name 'rangeNumbersComboBox' does not exist in the current context   E:\Projects\NumberGuessingGame_08029490\NumberGuessingGame_08029490\gameClass.cs    58  17  NumberGuessingGame_08029490

Error   10  The name 'guessResultTextBox' does not exist in the current context E:\Projects\NumberGuessingGame_08029490\NumberGuessingGame_08029490\testClass.cs    84  21  NumberGuessingGame_08029490
+3
4

, . , , , . A B, A, . , , , .

EDIT: , , . ? ?

EDIT2: , , , , Form1:

public class testClass : Form1

public class testClass

EDIT3 ( , ): Form1, testClass - Form1 Form1. Form1 testClass ( ) testClass:

// Inside Form1()
private TestClass m_testClass;

Form1()
{
    m_testClass = new testClass(this);
    ....
}

// Inside testClass
private Form1 m_testForm;

testClass(Form1 formToTest)
{
    m_testForm = formToTest;
}

void DoTest()
{
    // use m_testForm here...
}
+4

StackOverflow, :

  • if-statements SelectedIndex, SelectedValue?
  • else if. SelIndex == 0, SelIndex == 1
  • ?
  • ( ..)
  • () (! ) (variable == true) (variable == false)

EDIT: Form1 ( StackOverflowException

Form1 : testClass gameClass.
testClass gameClass Form1.
, testClass : testClass gameClass.
gameClass : testClass gameClass.

. ?

, , , # - , , , . , , , .

+1
  • , , , .
  • , , , , ( )
  • , ? :

    testClass: Form1

: Form1

0

.

: Form1 testClass gameClass.

, Form1, + gameClass - :

testClass newTest = new testClass();
gameClass newGame = new gameClass();

: , : testClass gameClass, Form1, -, newText newGame. msdn #:" ... .

, Form1 , newTest newGame.

, testClass , NewTest newGame.

! .

0

Source: https://habr.com/ru/post/1734300/


All Articles