Search code examples
winapitic-tac-toe

Reseting Screen after Finishing Game win32 c++


I am making a Tic-Tac-Toe game as my first ever win32 app, and it is coming along pretty good. I have everything done up until the first round is finished, with each square on the tic tac toe board being a button that when clicked becomes an X or an O based on whos turn it is. When one player wins the game, i made a small window show up underneath the tic tac toe board saying "Player 1 Wins". I also made a "NEW GAME" button at the bottom of the screen that when clicked all of the X's and O's are cleared from the board and the game restarts.

So here is my problem, when i click the "NEW GAME" button, i can't get the window saying "PLAYER 1 WINS" to go away like everything else, it just stays there. I want it to go away. Here is some of my code:

   case WM_COMMAND:
    wmId    = LOWORD(wParam);
    wmEvent = HIWORD(wParam);

    // Parse the menu selections:
    switch (wmId)
    {
    case IDM_LOC1:
        click1++;
        if(click1>1)
        {break;}
        else{
            playerobject++;
        if(playerobject==1|playerobject==3|playerobject==5|playerobject==7|playerobject==9)
        {   SetDlgItemText(hWnd, IDM_LOC1, "X");                
            loc1 = 1;   }
        else
        {   SetDlgItemText(hWnd, IDM_LOC1, "O");                
            loc1 = 2;   }
        winner = determinewinner();
        if(winner==1)
        {               
            hwndb = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,TEXT("STATIC"),TEXT("PLAYER 1 WINS!"),WS_CHILD|SS_CENTER|WS_VISIBLE,20,210,210,18,hWnd,HMENU(NULL),GetModuleHandle(NULL),NULL);
        }
        else if (winner==2)
        {
            hwndb = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,TEXT("STATIC"),TEXT("PLAYER 2 WINS!"),WS_CHILD|SS_CENTER|WS_VISIBLE,20,210,210,18,hWnd,HMENU(NULL),GetModuleHandle(NULL),NULL);
        }
        break;}

That is handling the logic part (I know its pretty messy, its my first go at it). So you can see that when the "determinewinner" function returns 1, the window shows up stating that player 1 has won. Now here is the code handling the "NEW GAME" button:

    case IDM_RESTART:
        SetDlgItemText(hWnd, IDM_LOC1, "");
        SetDlgItemText(hWnd, IDM_LOC2, "");
        SetDlgItemText(hWnd, IDM_LOC3, "");
        SetDlgItemText(hWnd, IDM_LOC4, "");
        SetDlgItemText(hWnd, IDM_LOC5, "");
        SetDlgItemText(hWnd, IDM_LOC6, "");
        SetDlgItemText(hWnd, IDM_LOC7, "");
        SetDlgItemText(hWnd, IDM_LOC8, "");
        SetDlgItemText(hWnd, IDM_LOC9, "");
        playerobject = 0;
        click1=0;
        click2=0;
        click3=0;
        click4=0;
        click5=0;
        click6=0;
        click7=0;   
        click8=0;
        click9=0;
        loc1=0,loc2=0,loc3=0,loc4=0,loc5=0,loc6=0,loc7=0,loc8=0,loc9=0;
    DestroyWindow(hwndb);
        break;

Now i think the problem is the switch statement, as both of them are just cases under the WndProc switch statement (The WM_COMMAND part). I tried moving the hwndb window into the winmain function and then just doing the ShowWindow function in the switch statement but that didn't work either of course.

How can i get the window to close? Thanks!

EDIT* So i forgot to say that when i run it, it does fine until the NEW GAME button is clicked, it says that hwndb hasn't been initialized, which is of course true for the particular part of the switch statement, so i've obviously done it wrong. How can i do it right?


Solution

  • Because of the "hwndb hasn't been initialized"-error I would say you have not declared:

    HWND hwndb; //Global variable
    

    If however you have made this a global variable this error can be occuring because of a call to IDM_RESTART before IDM_LOC1. This also means that should you have double-clicked your refresh button the DestroyWindow function will return false.