c++ 项目练习二 五子棋游戏

在这里插入图片描述

主函数

#include <iostream>
#include "ChessGame.h"

int main(void)
{
	Man man;
	Chess chess(13,44,43,67.3);
	AI ai;
	ChessGame game(&man, &ai, &chess);

	game.play();
	return 0;
}

棋盘类

#pragma once
#include<graphics.h>
#include<vector>
#include <wincrypt.h>
#include <wincrypt.h>
#include <conio.h>

using namespace std;

//表示落子的位置,这个struct是一种特殊的类,只不过作用类似c语言的结构体
//所以后面调用的时候,是按照类的调用来调用,而不是结构体调用格式
struct ChessPos {
	int row;
	int col;

	ChessPos(int r = 0,int c = 0) :row(r),col(c) {}
};


typedef enum {
	CHESS_WHITE = -1, //白棋
	CHESS_BLACK = 1

}chess_kind_t;

//棋盘类
class Chess
{
public:
	Chess(int gradeSize, int margin_x, int margin_y, float chessSize);
	void chessDwon(ChessPos* pos, chess_kind_t kind);
	bool clickBroad(int x, int y, ChessPos* pos); //判断落子地方是不是有效
	void init();
	int getGradeSize(); //获取棋盘的大小 (13,15,19线)
	bool checkOver();  //检查期盼是否被用完了
	int getChessData(int row,int col);
	int getChessData(ChessPos* pos);
private:
	IMAGE chessBlackImg;  //黑棋
	IMAGE chessWhiteImg; //白棋

	int gradeSize;  //棋盘的大小(多少行,13,15,17,19)
	int margin_x;  //棋盘的左侧边界
	int margin_y;   //棋盘的上边界
	float chessSize;  //棋子的大小,也即是1个棋盘格的大小
	vector<vector<int>> chessMap;  //存储棋局分布 0:空白; 1:黑子 ; -1:白子
	bool playerFlag;  //true:黑子走; false:白子走
	ChessPos lastPos; //记录最近的一次落子的位置

	bool checkWin();  //检查输赢,如果胜负已分,返回ture
	void updateGameMap(ChessPos* pos);  //更新棋盘数据
};

#include "Chess.h"
#include<mmsystem.h>
#include<math.h>
#pragma comment(lib,"winmm.lib")

void putimagePNG(int x, int y, IMAGE* picture) //x为载入图片的X坐标,y为Y坐标
{
	// 变量初始化
	DWORD* dst = GetImageBuffer();    // GetImageBuffer()函数,用于获取绘图设备的显存指针,EASYX自带
	DWORD* draw = GetImageBuffer();
	DWORD* src = GetImageBuffer(picture); //获取picture的显存指针
	int picture_width = picture->getwidth(); //获取picture的宽度,EASYX自带
	int picture_height = picture->getheight(); //获取picture的高度,EASYX自带
	int graphWidth = getwidth();       //获取绘图区的宽度,EASYX自带
	int graphHeight = getheight();     //获取绘图区的高度,EASYX自带
	int dstX = 0;    //在显存里像素的角标

	// 实现透明贴图 公式: Cp=αp*FP+(1-αp)*BP , 贝叶斯定理来进行点颜色的概率计算
	for (int iy = 0; iy < picture_height; iy++)
	{
		for (int ix = 0; ix < picture_width; ix++)
		{
			int srcX = ix + iy * picture_width; //在显存里像素的角标
			int sa = ((src[srcX] & 0xff000000) >> 24); //0xAArrggbb;AA是透明度
			int sr = ((src[srcX] & 0xff0000) >> 16); //获取RGB里的R
			int sg = ((src[srcX] & 0xff00) >> 8);   //G
			int sb = src[srcX] & 0xff;              //B
			if (ix >= 0 && ix <= graphWidth && iy >= 0 && iy <= graphHeight && dstX <= graphWidth * graphHeight)
			{
				dstX = (ix + x) + (iy + y) * graphWidth; //在显存里像素的角标
				int dr = ((dst[dstX] & 0xff0000) >> 16);
				int dg = ((dst[dstX] & 0xff00) >> 8);
				int db = dst[dstX] & 0xff;
				draw[dstX] = ((sr * sa / 255 + dr * (255 - sa) / 255) << 16)  //公式: Cp=αp*FP+(1-αp)*BP  ; αp=sa/255 , FP=sr , BP=dr
					| ((sg * sa / 255 + dg * (255 - sa) / 255) << 8)         //αp=sa/255 , FP=sg , BP=dg
					| (sb * sa / 255 + db * (255 - sa) / 255);              //αp=sa/255 , FP=sb , BP=db
			}
		}
	}
}

Chess::Chess(int gradeSize, int margin_x, int margin_y, float chessSize)
{
	this->gradeSize = gradeSize;
	this->margin_x = margin_x;
	this->margin_y = margin_y;
	this->chessSize = chessSize;
	playerFlag = CHESS_BLACK;

	for (int i = 0; i < gradeSize; i++)
	{
		vector<int>row;  //创建一个一维数组
		for (int j = 0; j < gradeSize; j++)
		{
			row.push_back(0);
		}
		chessMap.push_back(row);
	}
}

void Chess::chessDwon(ChessPos* pos, chess_kind_t kind)
{
	mciSendString("play down7.WAV", 0, 0, 0);
	//这是图片的左上角的位置坐标,-0.5*chessSize,可以把棋子画在交叉点上
	int x = margin_x + chessSize * pos->col - 0.5 * chessSize;
	int y = margin_y + chessSize * pos->row - 0.5 * chessSize;

	//chessMap[pos->row][pos->col] = 1;

	if (kind == CHESS_WHITE) {
		putimagePNG(x,y,&chessWhiteImg);
	}
	else {
		putimagePNG(x,y, &chessBlackImg);
	}

	updateGameMap(pos);

}

bool Chess::clickBroad(int x, int y, ChessPos* pos)
{
	//落子无法精确的落在交差点上,所以就依次判断落点到格子的四个点的距离,是否小于某个阈值,如果小于,
	//认为落在在当下的交叉点的位置,
	int col = (x - margin_x) / chessSize;
	int row = (y - margin_y) / chessSize;
	int leftTopPosX = margin_x + col * chessSize;
	int leftTopPosY = margin_y + row * chessSize;
	int offset = chessSize * 0.4;

	float len;
	bool ret = false;
	

	do {
		//左上角
		len = sqrt((leftTopPosX - x) * (leftTopPosX - x) + (leftTopPosY - y) * (leftTopPosY - y));
		if (len < offset) {
			pos->row = row;
			pos->col = col;
			if (chessMap[pos->row][pos->col] == 0) {
				ret = true;
			}
			break;  //条件不满足,结束当前循环,后面不再判断,while只执行一次
		}

		//右上角判断
		int x2 = leftTopPosX + chessSize;
		int y2 = leftTopPosY;
		len = sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));
		if (len < offset) {
			pos->row = row;
			pos->col = col+1;
			if (chessMap[pos->row][pos->col] == 0) {
				ret = true;
			}
			break;  //条件不满足,结束当前循环,后面不再判断,while只执行一次
		}

		//右下角判断
		x2 = leftTopPosX + chessSize;
		y2 = leftTopPosY + chessSize;
		len = sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));
		if (len < offset) {
			pos->row = row+1;
			pos->col = col+1;
			if (chessMap[pos->row][pos->col] == 0) {
				ret = true;
			}
			break;  //条件不满足,结束当前循环,后面不再判断,while只执行一次
		}

		//左下角判断
		x2 = leftTopPosX ;
		y2 = leftTopPosY + chessSize;
		len = sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));
		if (len < offset) {
			pos->row = row+1;
			pos->col = col;
			if (chessMap[pos->row][pos->col] == 0) {
				ret = true;
			}
			break;  //条件不满足,结束当前循环,后面不再判断,while只执行一次
		}
	} while (0);
	return ret;
}

void Chess::init()
{	
	initgraph(897,895, EX_SHOWCONSOLE);//创建游戏窗口
	loadimage(0, "棋盘2.jpg");
	mciSendString("play start.wav", 0, 0, 0);

	//加载黑棋和白棋图片
	loadimage(&chessBlackImg,"black.png",chessSize, chessSize,true);
	loadimage(&chessWhiteImg, "white.png", chessSize, chessSize, true);

	//棋局结束,又调用了init希望清棋盘,
	for (int i = 0; i < gradeSize; i++)
	{
		for (int j = 0; j < gradeSize; j++)
		{
			chessMap[i][j] = 0;
		}
	}
}

int Chess::getGradeSize()
{
	return gradeSize;
}

bool Chess::checkOver()
{
	if (checkWin()) {
		Sleep(1500);
		if (playerFlag == false) {
			//刚才走棋的是黑子(棋手),赢家为棋手
			mciSendString("play 不错.mp3", 0, 0, 0);
			loadimage(0, "胜利.jpg");
		}
		else {
			mciSendString("play 失败.mp3", 0, 0, 0);
			loadimage(0, "失败.jpg");
		}
		_getch(); //暂停作用
		return true;

	}	//		loadimage(0, "失败.jpg");

	
	return false;
}

int Chess::getChessData(int row, int col)
{
	return chessMap[row][col];
}

int Chess::getChessData(ChessPos* pos)
{
	
	return chessMap[pos->row][pos->col];
}

bool Chess::checkWin()
{
	

	//最近的落子点的位置
	int row = lastPos.row;
	int col = lastPos.col;

	//for (int i = 0; i < gradeSize; i++)
	/*{
		for (int j = 0; j < gradeSize; j++)
		{
			printf("%d ", chessMap[i][j]);
		}
	    printf("\n");

	}*/

	//落子点的水平方向
	for (int i = 0; i < 5; i++)
	{
		//对于第i次
		//五子连珠的最左侧位置
		if (col-i >=0 &&
			col-i +4 < gradeSize &&
			chessMap[row][col - i] == chessMap[row][col - i + 1] &&
			chessMap[row][col - i] == chessMap[row][col - i + 2] &&
			chessMap[row][col - i] == chessMap[row][col - i + 3] &&
			chessMap[row][col - i] == chessMap[row][col - i + 4])
			return true;
	}

	//垂直方向
	for (int i = 0; i < 5; i++)
	{
		//对于第i次
		//五子连珠的最左侧位置
		if (row - i >= 0 &&
			row - i + 4 < gradeSize &&
			chessMap[row-i][col] == chessMap[row - i + 1][col] &&
			chessMap[row-i][col] == chessMap[row - i + 2][col] &&
			chessMap[row-i][col] == chessMap[row - i + 3][col]  &&
			chessMap[row-i][col] == chessMap[row - i + 4][col])
			return true;
	}

	//   ”/" 方向
	for (int i = 0; i < 5; i++)
	{
		//对于第i次
		//五子连珠的最左侧位置
		if (row + i <gradeSize && row+i-4 >=0 &&
			col -i >=0 && col-i+4 < gradeSize &&
			chessMap[row + i][col - i] == chessMap[row + i - 1][col-i+1] &&
			chessMap[row + i][col - i] == chessMap[row + i - 2][col-i+2] &&
			chessMap[row + i][col - i] == chessMap[row + i - 3][col-i+3] &&
			chessMap[row + i][col - i] == chessMap[row + i - 4][col-i+4])
			return true;
	}

	//   ”\" 方向
	for (int i = 0; i < 5; i++)
	{
		//对于第i次
		//五子连珠的最左侧位置
		if (row - i >= 0 && row + i - 4 < gradeSize &&
			col - i >= 0 && col - i + 4 < gradeSize &&
			chessMap[row - i][col - i] == chessMap[row - i + 1][col - i + 1] &&
			chessMap[row - i][col - i] == chessMap[row - i + 2][col - i + 2] &&
			chessMap[row - i][col - i] == chessMap[row - i + 3][col - i + 3] &&
			chessMap[row - i][col - i] == chessMap[row - i + 4][col - i + 4])
			return true;
	}
	return false;
}

void Chess::updateGameMap(ChessPos* pos)
{
	lastPos = *pos;
	chessMap[pos->row][pos->col] = playerFlag ? CHESS_BLACK : CHESS_WHITE;
	playerFlag = !playerFlag;  //黑白方交换行棋子
}

Al棋手类

#pragma once
#include "Chess.h"
class AI
{
public:
	void init(Chess* chess);
	void go();

private:
	Chess* chess;
	vector<vector<int>> scoreMap;  //存放AL计算每个点的权重分数,用于判断下一步走哪里

private:
	void calculateScore();
	ChessPos think();
};


#include "AI.h"

void AI::init(Chess* chess)
{
	this->chess = chess;

	for (int i = 0; i < chess->getGradeSize(); i++)
	{
		vector<int> row;  //创建一个一维数组
		for (int j = 0; j < chess->getGradeSize(); j++)
		{
			row.push_back(0);
		}
		scoreMap.push_back(row);
	}
}

void AI::go()
{
	ChessPos pos = think();
	Sleep(1000);  //假装思考
	chess->chessDwon(&pos, CHESS_WHITE);
}

//void AI::calculateScore()
//{
//	int personNum = 0;  //棋手(黑棋)有多少连续的棋子
//	int aiNum = 0;      //Al(白棋)有多少连续的棋子
//	int emptyNum = 0; //该方向上空白位置的个数
//
//	//每走一步,都要做一次重新的权重计算
//	for (int i = 0; i < scoreMap.size(); i++)
//	{
//		for (int j = 0; j < scoreMap[1].size(); j++)
//		{
//			scoreMap[i][j] = 0;
//			
//			printf("%d ", chess->getChessData(i, j));
//		}
//		printf("\n ");
//
//	}
//	printf("\n ");
//
//	int size = chess->getGradeSize();
//	for (int row = 6; row < size; row++)
//	{
//		for (int col = 6; col < size; col++)
//		{
//			//先判断这个位置有没有棋子,如果有棋子,就判断下一个棋子
//		if (chess->getChessData	(row, col)) continue;
//
//
//			//上下左右9个点,正负理解为方向,计算每个点的周围四个点
//			for (int y = 1; y <= 1; y++) {
//				for (int x = 1; x <= 1; x++) {
//					//中间那个点是本身,就不用算了,算的是以这个点为中心的四周的权重
//					if (y == 0 && x == 0) continue;  
//
//					personNum = 0;
//					aiNum = 0;
//					emptyNum = 0;
//
//					//假设黑子在该位置落子,会构成什么棋形,数这个方向的4个子,加上本身就是5个子
//					for (int i = 1; i <= 4; i++)
//					{
//						int curRow=  row + i * y; //往下为正
//						int curCol = col + i * x;
//
//						if (curRow >=0 && curRow < size && 
//							curCol >= 0 && curCol < size &&
//							chess->getChessData(curRow, curCol)==1) 
//						{
//							//数对手的棋子
//							personNum++;
//						}
//						else if (curRow >= 0 && curRow < size &&
//							     curCol >= 0 && curCol < size &&
//							     chess->getChessData(curRow, curCol) == 0)  
//						{  //没有棋子
//							emptyNum++;
//							break;
//						}
//						else
//						{
//							//遇到白棋(ai)结束
//							break;
//						}
//					}
//					for (int i = 1; i <= 4; i++)
//					{
//						int curRow = row - i * y; //往下为正
//						int curCol = col - i * x;
//
//						if (curRow >= 0 && curRow < size &&
//							curCol >= 0 && curCol < size &&
//							chess->getChessData(curRow, curCol) == 1)
//						{
//							//数对手的棋子
//							personNum++;
//						}
//						else if (curRow >= 0 && curRow < size &&
//							curCol >= 0 && curCol < size &&
//							chess->getChessData(curRow, curCol) == 0)
//						{  //没有棋子
//							emptyNum++;
//							break;
//						}
//						else
//						{
//							//遇到白棋(ai)结束
//							break;
//						}
//					}
//					if (personNum == 1) {//连2
//						scoreMap[row][col] += 10;
//					}
//					else if (personNum == 2){//
//						if (emptyNum == 1) {
//							scoreMap[row][col] += 30;
//						}
//						else if(emptyNum == 2)
//							scoreMap[row][col] += 40;
//						}
//					else if (personNum == 3) {
//						if (emptyNum == 1) {
//							scoreMap[row][col] += 60;
//						}
//						else if (emptyNum == 2) {
//							scoreMap[row][col] += 200;
//						}
//					}
//					else if (personNum == 4) {
//						scoreMap[row][col] += 20000;
//					}
//
//
//					//假设白子在该位置落子,会构成什么棋形,
//					emptyNum = 0;
//					for (int i = 1; i <= 4; i++) {
//						int curRow = row + i * y;
//						int curCol = col + i * x;
//
//						if (curRow > 0 && curRow < size &&
//							curCol > 0 && curCol < size &&
//							chess->getChessData(curRow, curCol) == -1) {
//							aiNum++;
//						}
//						else if (curRow > 0 && curRow < size &&
//							     curCol > 0 && curCol < size &&
//							     chess->getChessData(curRow, curCol) == 0) {
//							emptyNum++;
//							break;
//						}
//						else {
//							break;  //遇到黑子(人)结束
//						}
//					}
//					for (int i = 1; i <= 4; i++) {
//						int curRow = row - i * y;
//						int curCol = col - i * x;
//
//						if (curRow > 0 && curRow < size &&
//							curCol > 0 && curCol < size &&
//							chess->getChessData(curRow, curCol) == -1) {
//							aiNum++;
//						}
//						else if (curRow > 0 && curRow < size &&
//							curCol > 0 && curCol < size &&
//							chess->getChessData(curRow, curCol) == 0) {
//							emptyNum++;
//							break;
//						}
//						else {
//							break;  //遇到黑子(人)结束
//						}
//					}
//
//
//					if (aiNum == 0) {
//						scoreMap[row][col] += 5;
//					}
//					else if (aiNum == 1) {
//						scoreMap[row][col] += 10;
//					}
//					else if (aiNum == 2) {
//						if(emptyNum ==2)  scoreMap[row][col] += 50;
//						else if(emptyNum == 1)  scoreMap[row][col] += 25;
//					}
//					else if (aiNum == 3) {
//						if (emptyNum == 1) scoreMap[row][col] += 55;
//						else if (emptyNum == 2) scoreMap[row][col] += 10000;
//					}
//					else if (aiNum >= 4)  scoreMap[row][col] += 30000;
//				}
//			}
//		}
//	}
//}

void AI::calculateScore()
{
	int personNum = 0; //棋手方(黑棋)多少个连续的棋子
	int aiNum = 0; //AI方(白棋)连续有多少个连续的棋子
	int emptyNum = 0; // 该方向上空白位的个数

	// 评分向量数组清零
	for (int i = 0; i < scoreMap.size(); i++) {
		for (int j = 0; j < scoreMap[i].size(); j++) {
			scoreMap[i][j] = 0;
		}
	}

	int size = chess->getGradeSize();
	for (int row = 0; row < size; row++) {
		for (int col = 0; col < size; col++) {
			//对每个点进行计算
			if (chess->getChessData(row, col)) continue;

			for (int y = -1; y <= 0; y++) {        //Y的范围还是-1, 0
				for (int x = -1; x <= 1; x++) {    //X的范围是 -1,0,1
					if (y == 0 && x == 0) continue;
					if (y == 0 && x != 1) continue; //当y=0时,仅允许x=1

					personNum = 0;
					aiNum = 0;
					emptyNum = 0;

					// 假设黑棋在该位置落子,会构成什么棋型
					for (int i = 1; i <= 4; i++) {
						int curRow = row + i * y;
						int curCol = col + i * x;

						if (curRow >= 0 && curRow < size &&
							curCol >= 0 && curCol < size &&
							chess->getChessData(curRow, curCol) == 1) {
							personNum++;
						}
						else if (curRow >= 0 && curRow < size &&
							curCol >= 0 && curCol < size &&
							chess->getChessData(curRow, curCol) == 0) {
							emptyNum++;
							break;
						}
						else {
							break;
						}
					}

					// 反向继续计算
					for (int i = 1; i <= 4; i++) {
						int curRow = row - i * y;
						int curCol = col - i * x;

						if (curRow >= 0 && curRow < size &&
							curCol >= 0 && curCol < size &&
							chess->getChessData(curRow, curCol) == 1) {
							personNum++;
						}
						else if (curRow >= 0 && curRow < size &&
							curCol >= 0 && curCol < size &&
							chess->getChessData(curRow, curCol) == 0) {
							emptyNum++;
							break;
						}
						else {
							break;
						}
					}

					if (personNum == 1) { //连2
						//CSDN  程序员Rock
						scoreMap[row][col] += 10;
					}
					else if (personNum == 2) {
						if (emptyNum == 1) {
							scoreMap[row][col] += 30;
						}
						else if (emptyNum == 2) {
							scoreMap[row][col] += 40;
						}
					}
					else if (personNum == 3) {
						if (emptyNum == 1) {
							scoreMap[row][col] = 60;
						}
						else if (emptyNum == 2) {
							scoreMap[row][col] = 5000; //200
						}
					}
					else if (personNum == 4) {
						scoreMap[row][col] = 20000;
					}

					// 假设白棋在该位置落子,会构成什么棋型
					emptyNum = 0;

					for (int i = 1; i <= 4; i++) {
						int curRow = row + i * y;
						int curCol = col + i * x;

						if (curRow >= 0 && curRow < size &&
							curCol >= 0 && curCol < size &&
							chess->getChessData(curRow, curCol) == -1) {
							aiNum++;
						}
						else if (curRow >= 0 && curRow < size &&
							curCol >= 0 && curCol < size &&
							chess->getChessData(curRow, curCol) == 0) {
							emptyNum++;
							break;
						}
						else {
							break;
						}
					}

					for (int i = 1; i <= 4; i++) {
						int curRow = row - i * y;
						int curCol = col - i * x;

						if (curRow >= 0 && curRow < size &&
							curCol >= 0 && curCol < size &&
							chess->getChessData(curRow, curCol) == -1) {
							aiNum++;
						}
						else if (curRow >= 0 && curRow < size &&
							curCol >= 0 && curCol < size &&
							chess->getChessData(curRow, curCol) == 0) {
							emptyNum++;
							break;
						}
						else {
							break;
						}
					}

					if (aiNum == 0) {
						scoreMap[row][col] += 5;
					}
					else if (aiNum == 1) {
						scoreMap[row][col] += 10;
					}
					else if (aiNum == 2) {
						if (emptyNum == 1) {
							scoreMap[row][col] += 25;
						}
						else if (emptyNum == 2) {
							scoreMap[row][col] += 50;
						}
					}
					else if (aiNum == 3) {
						if (emptyNum == 1) {
							scoreMap[row][col] += 55;
						}
						else if (emptyNum == 2) {
							scoreMap[row][col] += 10000;
						}
					}
					else if (aiNum >= 4) {
						scoreMap[row][col] += 30000;
					}
				}
			}
		}
	}
}
ChessPos AI::think()
{
	calculateScore();
	vector< ChessPos> maxPoint;   //可能存在多个分数相同的点,存起来
	int maxScore = 0;
	int size = chess->getGradeSize();
	for (int row = 0; row < size; row++){
		for (int col = 0; col < size; col++) {
			//printf("%d ", scoreMap[row][col]);

			//只对空白点进行判断
			if (chess->getChessData(row, col) == 0) {
				if (scoreMap[row][col] > maxScore) {
					maxScore = scoreMap[row][col];
					maxPoint.clear();
					maxPoint.push_back(ChessPos(row, col));
				}
				else if (scoreMap[row][col] == maxScore){
					maxPoint.push_back(ChessPos(row, col));
				}
			}
		}
		//printf("\n");
	}

	int index = rand() % maxPoint.size();
	return maxPoint[index];
}

#pragma once
#include "Chess.h"


class Man
{
public:
	void init(Chess* chess);
	void go();

private:
	Chess *chess;
};


#include "Man.h"

void Man::init(Chess* chess)
{
	this->chess = chess;
}

void Man::go()
{
	MOUSEMSG msg;
	ChessPos pos;  //ChessPos本身就是一个类

	//通过棋盘对象,调用判断落子是否有效,以及落子功能
	//如果是左键单击且点击位置有效,如果无效就一直循环,如果不这么设置,单击位置错误,当次就无法下棋了
	while (1) {
		//获取鼠标点击消息
		msg = GetMouseMsg();
		if (msg.uMsg == WM_LBUTTONDOWN && chess->clickBroad(msg.x, msg.y, &pos))
		{
			break;
		}
		/*if (msg.uMsg == WM_LBUTTONDOWN)
		{
			printf("hello \n");
			break;
		}*/
	}
	//printf("%d %d\n", pos.col, pos.row);
	//printf("%d %d\n", msg.x, msg.y);

	//落子
	chess->chessDwon(&pos, CHESS_BLACK);

}

棋盘控制类

#pragma once
#include "Chess.h"
#include "Man.h"
#include "AI.h"


//游戏控制
class ChessGame
{
public:
	ChessGame(Man* man, AI* ai, Chess* chess);
	void play();  //开始对局

//添加数据成员
private:
	Man *man;
	AI* ai;
	Chess* chess;
};


#include "ChessGame.h"


//对局,开始五子棋游戏
void ChessGame::play()
{
	chess->init();
	
	while (1)
	{
		//先由棋手走
		man->go();
		if (chess->checkOver()) {
			chess->init();
			continue;
		}

		ai->go();
		if (chess->checkOver()) {
			chess->init();
			continue;
		}
	}
}

ChessGame::ChessGame(Man* man, AI* ai, Chess* chess)
{
	this->man = man;
	this->ai = ai;
	this->chess = chess;

	man->init(chess);
	ai->init(chess);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值