Search code examples
c++buttontic-tac-toe

Using a "New Game" button in Tac Tac Toe C++, win32 app beginner


I am making my first ever win32 app, a tic tac toe game, using C++, and thus far i have been able to make a 3 by 3 grid of buttons that when clicked will show X's and O's based on who's turn it is. What i am having trouble doing is figuring out how to clear all the X's and O's once the game is over (I haven't got around to making my program determine if there is a winner or not yet, that will come later).

So i have made a "New Game" button, but i don't know how to get everything to clear when it is clicked... Any help?

    // Tic_Tac_Toe.cpp : Defines the entry point for the application.
    //

    #include "stdafx.h"
    #include "Tic_Tac_Toe.h"




    #define MAX_LOADSTRING 100

    // Global Variables:
    HINSTANCE hInst;                                // current instance
    TCHAR szTitle[MAX_LOADSTRING];                  // The         title bar text
    TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

    // Forward declarations of functions included in this code module:
    ATOM                MyRegisterClass(HINSTANCE hInstance);
    BOOL                InitInstance(HINSTANCE, int);
    LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
    INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

    char playerobject = 0;
    int click1=0;
    int click2=0;
    int click3=0;
    int click4=0;
    int click5=0;
    int click6=0;
    int click7=0;   
    int click8=0;
    int click9=0;



    int APIENTRY _tWinMain(HINSTANCE hInstance,
                 HINSTANCE hPrevInstance,
                 LPTSTR    lpCmdLine,
                 int       nCmdShow)
    {
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: Place code here.



MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TIC_TAC_TOE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
    return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TIC_TAC_TOE));

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
    if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

return (int) msg.wParam;
    }



    //
    //  FUNCTION: MyRegisterClass()
    //
    //  PURPOSE: Registers the window class.
    //
    //  COMMENTS:
    //
    //    This function and its usage are only necessary if you want this code
    //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
    //    function that was added to Windows 95. It is important to call this function
    //    so that the application will get 'well formed' small icons associated
    //    with it.
    //
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style          = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc    = WndProc;
wcex.cbClsExtra     = 0;
wcex.cbWndExtra     = 0;
wcex.hInstance      = hInstance;
wcex.hIcon  = LoadIcon(hInstance,MAKEINTRESOURCE,(IDI_TIC_TAC_TOE));
wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_TIC_TAC_TOE);
wcex.lpszClassName  = szWindowClass;
wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassEx(&wcex);
     }

    //
    //   FUNCTION: InitInstance(HINSTANCE, int)
    //
    //   PURPOSE: Saves instance handle and creates main window
    //
    //   COMMENTS:
    //
    //        In this function, we save the instance handle in a global variable and
    //        create and display the main program window.
    //
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
    HWND hWnd;

       hInst = hInstance; // Store instance handle in our global variable

       hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, 250, 400, NULL, NULL, hInstance, NULL);

       if (!hWnd)
       {
          return FALSE;
       }

       ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);

       return TRUE;
    }







     //
    //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
    //
    //  PURPOSE:  Processes messages for the main window.
    //
    //  WM_COMMAND  - process the application menu
    //  WM_PAINT    - Paint the main window
    //  WM_DESTROY  - post a quit message and return
    //
    //
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;


switch (message)
{
case WM_CREATE:
    CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("BUTTON"), TEXT(""), WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 50,50,50,50,hWnd,HMENU(IDM_LOC1),GetModuleHandle(NULL),NULL);
    CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("BUTTON"), TEXT(""), WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 100,50,50,50,hWnd,HMENU(IDM_LOC2),GetModuleHandle(NULL),NULL);
    CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("BUTTON"), TEXT(""), WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 150,50,50,50,hWnd,HMENU(IDM_LOC3),GetModuleHandle(NULL),NULL);
    CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("BUTTON"), TEXT(""), WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 50,100,50,50,hWnd,HMENU(IDM_LOC4),GetModuleHandle(NULL),NULL);
    CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("BUTTON"), TEXT(""), WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 100,100,50,50,hWnd,HMENU(IDM_LOC5),GetModuleHandle(NULL),NULL);
    CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("BUTTON"), TEXT(""), WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 150,100,50,50,hWnd,HMENU(IDM_LOC6),GetModuleHandle(NULL),NULL);
    CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("BUTTON"), TEXT(""), WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 50,150,50,50,hWnd,HMENU(IDM_LOC7),GetModuleHandle(NULL),NULL);
    CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("BUTTON"), TEXT(""), WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 100,150,50,50,hWnd,HMENU(IDM_LOC8),GetModuleHandle(NULL),NULL);
    CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("BUTTON"), TEXT(""), WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 150,150,50,50,hWnd,HMENU(IDM_LOC9),GetModuleHandle(NULL),NULL);
    CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("BUTTON"), TEXT("NEW GAME"), WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 75,250,100,30,hWnd,HMENU(IDM_RESTART), GetModuleHandle(NULL), NULL);
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");                
            int loc1 = 1;}
        else
            {SetDlgItemText(hWnd, IDM_LOC1, "O");               
            int loc1 = 2;}
        break;}
    case IDM_LOC2:
        click2++;
        if(click2>1){break;}
        else{
        playerobject++;
        if(playerobject==1|playerobject==3|playerobject==5|playerobject==7|playerobject==9){
            SetDlgItemText(hWnd, IDM_LOC2, "X");                
            int loc2=1;}
        else
            {SetDlgItemText(hWnd, IDM_LOC2, "O");               
         int loc2 = 2;}
        break;}
    case IDM_LOC3:
        click3++;
        if(click3>1){break;}
        else{
        playerobject++;
        if(playerobject==1|playerobject==3|playerobject==5|playerobject==7|playerobject==9){
            SetDlgItemText(hWnd, IDM_LOC3, "X");                
            int loc3=1;}
        else
            {SetDlgItemText(hWnd, IDM_LOC3, "O");               
            int loc3 = 2;}
        break;}
    case IDM_LOC4:
        click4++;
        if(click4>1){break;}
        else{
        playerobject++;
        if(playerobject==1|playerobject==3|playerobject==5|playerobject==7|playerobject==9){
            SetDlgItemText(hWnd, IDM_LOC4, "X");                
            int loc4 = 1;}
        else
            {SetDlgItemText(hWnd, IDM_LOC4, "O");               
            int loc4 = 2;}
        break;}
    case IDM_LOC5:
        click5++;
        if(click5>1){break;}
        else{
        playerobject++;
        if(playerobject==1|playerobject==3|playerobject==5|playerobject==7|playerobject==9){
            SetDlgItemText(hWnd, IDM_LOC5, "X");                
            int loc5=1;}
        else
            {SetDlgItemText(hWnd, IDM_LOC5, "O");               
            int loc5=2;}
        break;}
    case IDM_LOC6:
        click6++;
        if(click6>1){break;}
        else{
        playerobject++;
        if(playerobject==1|playerobject==3|playerobject==5|playerobject==7|playerobject==9){
            SetDlgItemText(hWnd, IDM_LOC6, "X");                
            int loc6=1;}
        else
            {SetDlgItemText(hWnd, IDM_LOC6, "O");               
            int loc6=2;}
        break;}
    case IDM_LOC7:
        click7++;
        if(click7>1){break;}
        else{
        playerobject++;
        if(playerobject==1|playerobject==3|playerobject==5|playerobject==7|playerobject==9){
            SetDlgItemText(hWnd, IDM_LOC7, "X");                
            int loc7=1;}
        else
            {SetDlgItemText(hWnd, IDM_LOC7, "O");               
            int loc7=2;}
        break;}
    case IDM_LOC8:
        click8++;
        if(click8>1){break;}
        else{
        playerobject++;
        if(playerobject==1|playerobject==3|playerobject==5|playerobject==7|playerobject==9){
            SetDlgItemText(hWnd, IDM_LOC8, "X");                
            int loc8=1;}
        else
            {SetDlgItemText(hWnd, IDM_LOC8, "O");               
            int loc8=2;}
        break;}
    case IDM_LOC9:
        click9++;
        if(click9>1){break;}
        else{
        playerobject++;
        if(playerobject==1|playerobject==3|playerobject==5|playerobject==7|playerobject==9){
            SetDlgItemText(hWnd, IDM_LOC9, "X");
            int loc9=1;}
        else
            {SetDlgItemText(hWnd, IDM_LOC9, "O");               
            int loc9=2;}
        break;
        }
    case IDM_RESTART:

        break;

    case IDM_ABOUT:
        DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
        break;
    case IDM_EXIT:
        DestroyWindow(hWnd);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    break;
case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    // TODO: Add any drawing code here...
    EndPaint(hWnd, &ps);
    break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
    }

I'm sorry my organization is probably terrible.... But the code compiles and does what i have explained up until now. Also i should add that i do have header files that i am not including that define the variables that you probably see undefined in this code. Thanks for any help you can give me on how to "reset" everything using the "New Game" button


Solution

  • Reset playerobject and all your click globals back to zero, and set the text of each button back to nothing, e.g. SetDlgItemText(hWnd, IDM_LOC1, "");.