Home > Mobile >  Why does my windows form not load? Is it just my build or does this truly not pop anything up?
Why does my windows form not load? Is it just my build or does this truly not pop anything up?

Time:04-17

I am using this code along with other methods after it, but cannot get the form I've been using in designer to pop up. The only thing that pops up successfully is the MessageBox asking the player if they would like to play as X. I've tried commenting that out to see if that's the issue for why it won't load, but I'm completely lost as to why my form will not load at all.

    namespace mmelichar_Topic6_Activity13
{
    public partial class mmelichar_TicTacToe : Form
    {
        int player = 0;
        int position;
        int turn = 0;
        int playerMove;
        int firstMove;
        int secondMove;
        string[,] location = new string[3, 3] { { "", "", "" }, { "", "", "" }, { "", "", "" } };

        public mmelichar_TicTacToe()
        {
            InitializeComponent();
        }

        private void mmelichar_TicTacToe_Load(object sender, EventArgs e)
        {
            //There's only two moves that have to be hard-coded for AI to be able to
            //play tic-tac-toe near perfectly, each game /should/ result in a tie or
            //a win for the CPU. Other than that, the tryWin and tryBlock methods
            //should be able to win the game if there is an availability for that,
            //or block the opponent from winning if they cannot win quite yet.
            Random rnd = new Random();



            DialogResult dialogResult = MessageBox.Show("Do you want to play as X?", "Player Choice", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (dialogResult == DialogResult.Yes)
            {
                player = 1;
            }
            else if (dialogResult == DialogResult.No)
            {
                player = 0;
            }
            //player is O
            if (player == 0 && turn == 0)
            {
                firstTurnCPU();
                //player turn
                mre.WaitOne();
            }
            //player is X
            if (player == 1 && turn == 0)
            {
                //player turn 1
                mre.WaitOne();

                //cpu turn 1
                firstTurnCPU();

                //player turn 2
                mre.WaitOne();

                //cpu turn 2
                secondTurnCPU();

                //player turn 3
                mre.WaitOne();

                //cpu turn 3
                tryWin();
                tryBlock();

                //player turn 4
                mre.WaitOne();

                //cpu turn 4
                tryWin();
                tryBlock();

                //player turn 

            }
        }
    private readonly ManualResetEvent mre = new ManualResetEvent(false);

    private void playerTurn_EventHandler(object sender, EventArgs e)
    {
        mre.Set();
    }

edit: updated code to remove while loop and include my ManualResetEvent

CodePudding user response:

You have an infinite loop:

while (playing)
{
    // perform a bunch of logic
    // but probably don't do anything async or properly interact with the UI
}

An infinite loop on the UI threat would certainly prevent the UI from ever drawing to the screen. (It may also be running away with the CPU a lot more than you want it to.)

If you really do want a "game loop" style of game construction, there are approaches you can take in Windows Forms. But now would be a good time to really think through the design of the game before going that route.

Windows Forms is highly event-driven. It's idle most of the time, and responds when users interact with the UI. (A click here, a mouse-over there, etc.) If your game fits that structure (turn based, etc.) then use that structure since it's more native to Windows Forms.

You can also combine the two, using Windows Forms events to process user interactions, but when the program launches you can create a separate thread for your game loop to process ongoing events/logic in the background. Just make sure that loop isn't killing the CPU by constantly running. It's reasonable to sleep the thread for a moment on each iteration of the loop.

CodePudding user response:

I had two problems within my code:

First, an infinite while loop that I forgot to remove, increasing CPU usage needlessly.

Second, use of ManualResetEvent instead of just using normal flow within the program to hand control back and forth from player to CPU and vice-versa.

  • Related