Visual studio的环境用 C++写黑白棋,要求实现绘制6*6棋盘,分玩家对战和人机对战两种模式
时间: 2024-02-03 10:12:41 浏览: 24
好的,首先你需要创建一个 Visual Studio 的 C++ 窗口程序项目,然后根据需求进行编码。
下面是一个简单的黑白棋游戏实现的代码示例:
```c++
#include <Windows.h>
#include <string>
using namespace std;
const int BOARD_SIZE = 6;
const int CELL_SIZE = 50;
const int MARGIN = 20;
const int WINDOW_WIDTH = 2 * MARGIN + BOARD_SIZE * CELL_SIZE;
const int WINDOW_HEIGHT = WINDOW_WIDTH + CELL_SIZE;
const int BLACK = 1;
const int WHITE = -1;
const int EMPTY = 0;
int board[BOARD_SIZE][BOARD_SIZE];
int currentPlayer = BLACK;
HWND hwnd;
void drawBoard(HDC hdc) {
SelectObject(hdc, GetStockObject(BLACK_PEN));
for (int i = 0; i <= BOARD_SIZE; i++) {
MoveToEx(hdc, MARGIN, MARGIN + i * CELL_SIZE, NULL);
LineTo(hdc, MARGIN + BOARD_SIZE * CELL_SIZE, MARGIN + i * CELL_SIZE);
MoveToEx(hdc, MARGIN + i * CELL_SIZE, MARGIN, NULL);
LineTo(hdc, MARGIN + i * CELL_SIZE, MARGIN + BOARD_SIZE * CELL_SIZE);
}
}
void drawPiece(HDC hdc, int x, int y, int color) {
if (color == BLACK) {
SelectObject(hdc, GetStockObject(BLACK_BRUSH));
}
else if (color == WHITE) {
SelectObject(hdc, GetStockObject(WHITE_BRUSH));
}
Ellipse(hdc, MARGIN + x * CELL_SIZE - CELL_SIZE / 2,
MARGIN + y * CELL_SIZE - CELL_SIZE / 2,
MARGIN + x * CELL_SIZE + CELL_SIZE / 2,
MARGIN + y * CELL_SIZE + CELL_SIZE / 2);
}
void putPiece(int x, int y) {
board[x][y] = currentPlayer;
drawPiece(GetDC(hwnd), x, y, currentPlayer);
currentPlayer = -currentPlayer;
}
void initBoard() {
memset(board, 0, sizeof(board));
board[2][2] = board[3][3] = BLACK;
board[2][3] = board[3][2] = WHITE;
drawBoard(GetDC(hwnd));
drawPiece(GetDC(hwnd), 2, 2, BLACK);
drawPiece(GetDC(hwnd), 3, 3, BLACK);
drawPiece(GetDC(hwnd), 2, 3, WHITE);
drawPiece(GetDC(hwnd), 3, 2, WHITE);
currentPlayer = BLACK;
}
void playWithHuman() {
initBoard();
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
if (msg.message == WM_LBUTTONDOWN) {
int x = (GET_X_LPARAM(msg.lParam) - MARGIN + CELL_SIZE / 2) / CELL_SIZE;
int y = (GET_Y_LPARAM(msg.lParam) - MARGIN + CELL_SIZE / 2) / CELL_SIZE;
if (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && board[x][y] == EMPTY) {
putPiece(x, y);
}
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void playWithComputer() {
// TODO: 实现人机对战
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE:
initBoard();
break;
case WM_PAINT:
drawBoard(GetDC(hwnd));
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] != EMPTY) {
drawPiece(GetDC(hwnd), i, j, board[i][j]);
}
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
static TCHAR szAppName[] = TEXT("BlackWhiteChess");
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("BlackWhiteChess"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
WINDOW_WIDTH, WINDOW_HEIGHT,
NULL, NULL, hInstance, NULL);
if (!hwnd) {
MessageBox(NULL, TEXT("CreateWindow failed!"), szAppName, MB_ICONERROR);
return 0;
}
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
// TODO: 根据需要选择玩家对战还是人机对战
playWithHuman();
//playWithComputer();
return 0;
}
```
在这个示例中,我们定义了一个6*6的棋盘,实现了绘制棋盘和棋子的函数,并在窗口程序的消息处理函数中处理了鼠标点击事件,实现了玩家对战模式。你可以根据需要修改代码,实现人机对战模式。