在井字游戏 C++ 中使用“新游戏”按钮,win32 应用程序初学者
Posted
技术标签:
【中文标题】在井字游戏 C++ 中使用“新游戏”按钮,win32 应用程序初学者【英文标题】:Using a "New Game" button in Tac Tac Toe C++, win32 app beginner 【发布时间】:2013-06-26 19:36:43 【问题描述】:我正在使用 C++ 制作我的第一个 win32 应用程序,一个井字游戏,到目前为止,我已经能够制作一个 3 x 3 的按钮网格,当点击这些按钮时,将根据谁打开它来显示 X 和 O是。我遇到的麻烦是弄清楚如何在游戏结束后清除所有的 X 和 O(我还没有时间让我的程序确定是否有赢家,这将在稍后出现)。
所以我制作了一个“新游戏”按钮,但我不知道如何在单击它时清除所有内容...有什么帮助吗?
// 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;
很抱歉,我的组织可能很糟糕......但是代码可以编译并执行我到目前为止所解释的内容。另外我应该补充一点,我确实有头文件,我不包括这些头文件定义了您可能在此代码中看到未定义的变量。感谢您在如何使用“新游戏”按钮“重置”所有内容方面给予我任何帮助
【问题讨论】:
担心TCHAR
s 真的没有意义。只需在周围使用宽字符串。
我从 Visual c++ 2010 中的模板应用程序开始,然后从那里拿走了它,所以我选择不改变他们已经为我做的很多事情。但是,是的,我是全新的,所以谢谢你的建议。
【参考方案1】:
将playerobject
和所有click
全局变量重置为零,并将每个按钮的文本设置为零,例如SetDlgItemText(hWnd, IDM_LOC1, "");
.
【讨论】:
谢谢!我什至没有想到这一点,这几乎太容易了....那太好了!作为记录,是否有另一种方法可以做到这一点,而无需亲自更改所有值? 您可能会考虑使用一些数组和循环来简化编码并消除代码重复,例如int click[9];
(不要忘记索引为 0-8 或忽略条目 0 并使用 int click[10];
)。此外,如果您的资源标识符是连续的(IDM_LOC1
...IDM_LOC9
),您可以使用循环来重置文本,(for (UINT i=IDM_LOC1; i<=IDM_LOC9; i++)
...)等。您还可以为按钮单击创建通用函数...
非常感谢,我将研究其中的一些想法,以使我的代码不那么混乱。以上是关于在井字游戏 C++ 中使用“新游戏”按钮,win32 应用程序初学者的主要内容,如果未能解决你的问题,请参考以下文章