五子棋AI(人机对战)源码-阶段考核作业
//之前发过人人对战源码,这次假期闲来无事,终于将人机对战给完成了,虽然机器"智力"比较低,但也是自己独立完成的、、,特发出来与大家分享,不喜勿喷{:5_91:}//感谢鱼C,感谢小甲鱼的教程
//使用Microsoft visual studio 2015编写编译
#include <windows.h>
#include <winuser.h>
#define bool unsigned short//由于C语言没有bool类型所以这里把它定义为unsigned short
#define true 1
#define false 0
#define CHESSBOARD_NUMBER 15//棋盘规格,注棋盘中黑点不随规格改变而改变
#define BLACK_CHESS_PIECES 1//定义黑色棋子 值为1
#define WHITE_CHESS_PIECES 2//定义白色棋子值为2
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int pr_chess_color;//待下棋子颜色
int z_DrawChessMap(HDC hdc,int extent,int cxClient,int cyClient);//绘制棋盘(设备环境句柄,棋盘规格extent*extent,客户区宽度,客户区高度)
int z_initialise_chessboard(struct st_chess_pieces arr_chessboard, int cxClient, int cyClient, bool is_initialise); // 初始化棋盘, 更新棋盘坐标, 需提供客户区大小, 如果is_initialise值为true, 则初始化棋盘颜色
int z_Draw_chesspieces(struct st_chess_pieces arr_chessboard, HDC hdc,HWND hwnd);//绘制棋子
int z_xiaqi(struct st_chess_pieces arr_chessboard, int x, int y, HWND hwnd);//下棋
int z_is_ok(struct st_chess_pieces arr_chessboard);//判断是否赢棋,黑子赢返回1,白棋赢返回2
int z_calc_score(struct st_value_tab value_tab_st, struct st_value_tab point_tab);//计算指定表分值,须传入评分表,点表
POINT computerAI(struct st_chess_pieces arr_chessboard, intpr_chess_color);//电脑智能走棋算法,传入棋盘数组及要落子颜色,返回落子坐标
struct st_chess_pieces
{
int extra;//预留
int chess_color;//棋子颜色(RGB)
POINT x_y;//棋子坐标(x,y)
};
struct st_value_tab
{
char x1;
int score;
};
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MyWindows");//窗口类名
HWND hwnd;//窗口句柄
MSG msg;//消息
WNDCLASS wndclass;//窗口类
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;//窗口风格
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("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
TEXT("人执白子,电脑执黑子 五子棋-AI算法"), //标题
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
600, //窗口大小
600,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);//显示窗口
UpdateWindow(hwnd);//更新窗口
while (GetMessage(&msg, NULL, 0, 0))//消息循环
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)//处理消息
{
static HDC hdc;
PAINTSTRUCT ps;
RECT rect;
POINT cursor_point;
int iGainer;//胜棋者
static cx_Client;
static cy_Client;
static bool bChessBoard_invalidate;//表示棋盘是否能下棋,true不能下,false能下
static struct st_chess_pieces arr_chessboard;//棋盘数组
POINT ai_xiaqi;//ai下棋坐标
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
z_DrawChessMap(hdc, CHESSBOARD_NUMBER, rect.right, rect.bottom);//绘制棋盘
z_Draw_chesspieces(arr_chessboard, hdc,hwnd);//绘制棋子
EndPaint(hwnd, &ps);
return 0;
case WM_SIZE:
cx_Client = LOWORD(lParam);
cy_Client = HIWORD(lParam);
z_initialise_chessboard(arr_chessboard, cx_Client ,cy_Client, false);
return 0;
case WM_CREATE:
pr_chess_color=WHITE_CHESS_PIECES;//初始化白棋
return 0;
case WM_LBUTTONUP://下棋
if (bChessBoard_invalidate == true)//棋盘不能下棋,直接返回
{
return 0;
}
GetCursorPos(&cursor_point);//获取鼠标在屏幕内坐标
ScreenToClient(hwnd, &cursor_point);//将鼠标在屏幕内坐标转换为窗口客户区坐标
z_xiaqi(arr_chessboard, cursor_point.x, cursor_point.y, hwnd);//将鼠标坐标传入下棋函数
InvalidateRect(hwnd, NULL, true);//发送客户区失效,重绘客户区
iGainer = z_is_ok(arr_chessboard);
if (iGainer == 1)//黑子胜
{
MessageBox(hwnd, TEXT("黑子胜!\n双击鼠标左键重新开盘!"), TEXT("提示:"), MB_OK);
bChessBoard_invalidate = true;
return 0;
}else if(iGainer ==2 )//白子胜
{
MessageBox(hwnd,TEXT("白子胜!\n双击鼠标左键重新开盘!"),TEXT("提示:"),MB_OK);
bChessBoard_invalidate = true;
return 0;
}
if (pr_chess_color != BLACK_CHESS_PIECES)//当前落子不是黑色,结束不继续向下
{
return 0;
}
//以下代码为计算机走棋
Sleep(500);
ai_xiaqi = computerAI(arr_chessboard, pr_chess_color);
z_xiaqi(arr_chessboard, ai_xiaqi.x, ai_xiaqi.y, hwnd);//将鼠标坐标传入下棋函数
InvalidateRect(hwnd, NULL, true);//发送客户区失效,重绘客户区
iGainer = z_is_ok(arr_chessboard);
if (iGainer == 1)//黑子胜
{
MessageBox(hwnd, TEXT("黑子胜!\n双击鼠标左键重新开盘!"), TEXT("提示:"), MB_OK);
bChessBoard_invalidate = true;
}else if(iGainer ==2 )//白子胜
{
MessageBox(hwnd,TEXT("白子胜!\n双击鼠标左键重新开盘!"),TEXT("提示:"),MB_OK);
bChessBoard_invalidate = true;
}
return 0;
case WM_LBUTTONDBLCLK://鼠标左键双击
if (MessageBox(hwnd, TEXT("是否重新开局!"), TEXT("提示:"), MB_YESNO) == IDYES)
{
z_initialise_chessboard(arr_chessboard, cx_Client, cy_Client, true);//初始化棋盘
InvalidateRect(hwnd, NULL, true);//发送客户区失效,重绘客户区
bChessBoard_invalidate = false;//
pr_chess_color = WHITE_CHESS_PIECES;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
**** Hidden Message *****
{:5_91:} bbbbbbbbbbbbbbbbbbbb 太牛掰了 666666666666666666666666 666 感谢楼主分享 牛逼 66666666666 hh 厉害了 感谢楼主无私奉献! 寰堟劅鍏磋叮,鐪嬬湅
1>d:\c练习\test\test\test.cpp(85) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
这个怎么解决 棒棒哒
真不错 niubi 看看 6666666 {:5_106:}