(从零到一)五子棋博弈算法——基础框架(不定期更新)

(不定期更新)

2017.12.31日晚更新内容:

1.完善主函数框架。

2.新增界面类(包含人走棋函数,显示函数,检查胜负函数等)。

3.基本的人人对战。


main函数:

#include 
#include "ChessBoard.h"

using namespace std;

int main()
{
	char IsWin;
	ChessBoard cb;
	char FirstGo;
	cout << "请输入您的先手后手![先手0]/[后手1]" << endl;
	cin >> FirstGo;
	if (FirstGo == 0)   //若是选择先手,那么先调用人走子
	{
		cb.Show();   //先显示棋盘,再显示调用用户走子
		cb.PeopleGo(FirstGo);
		//走子
	}
	while (1)
	{
		cb.Show();
		IsWin = cb.CheckWin();
		if (IsWin != -1)
		{
			cout << "恭喜" << (IsWin == '0' ? "黑子" : "白子") << "获胜" << endl;
		}
		cb.PeopleGo(FirstGo);
		cb.Show();
		IsWin = cb.CheckWin();
		if (IsWin != -1)
		{
			cout << "恭喜" << (IsWin == '0' ? "黑子" : "白子") << "获胜" << endl;
		}

		cb.PeopleGo(FirstGo);
		//为了测试,我们首先测试人人对战
		
	}
	return 0;
}


界面类:

包含人走子函数、界面显示函数、检查五子连珠函数。

ChessBoard.h:

#pragma once
#define Cwidth 15    //定义棋盘大小
class ChessBoard
{
public:
	ChessBoard();
	~ChessBoard();
	char CBoard[Cwidth][Cwidth];    //定义棋盘
	char CheckWin();                //检查五子连珠函数
	void Show();                    //界面显示函数
	void PeopleGo(char Color);      //人走子函数   Color为当前人所执子的颜色
};

ChessBoard.cpp:

#include "ChessBoard.h"
#include 
using namespace std;


ChessBoard::ChessBoard()
{
	//memset(CBoard, 0, sizeof(CBoard));
	for (int i = 0; i < Cwidth; i++)             //初始化棋盘
	{
		for (int j = 0; j < Cwidth; j++)
		{
			CBoard[i][j] = -1;
		}
	}
}

ChessBoard::~ChessBoard()
{
}

void ChessBoard::Show()   //界面显示函数
{
	cout << "  ";
	for (int i = 0; i <  Cwidth; i++)
	{
		cout << " " << (char)(i + 'A');
	}
	cout << endl;
	for (int i = 0; i <  Cwidth; i++)
	{
		cout << (char)(i + 'A') << "  ";
		for (int j = 0; j <  Cwidth; j++)
		{
			if (CBoard[i][j] == -1)
			{
				cout << "十";
			}
			else if (CBoard[i][j] == 1)
			{
				cout << "●";
			}
			else
			{
				cout << "○";
			}
		}
		cout << endl;
	}
}

char ChessBoard::CheckWin()   //检查五子连珠
{
	int k = 1;
	for (int i = 0; i < Cwidth; i++)
	{
		for (int j = 0; j < Cwidth; j++)
		{
			while (CBoard[i][j] == CBoard[i][j + k])      //x方向检查
			{
				k++;
				if (k == 5)
					return CBoard[i][j];
			}
			while (CBoard[i][j] == CBoard[i+k][j + k])    //右斜下方向检查
			{
				k++;
				if (k == 5)
					return CBoard[i][j];
			}
			while (CBoard[i][j] == CBoard[i+k][j])       //y方向检查
			{
				k++;
				if (k == 5)
					return CBoard[i][j];
			}
			while (CBoard[i][j] == CBoard[i - k][j])       //左斜下方向检查
			{
				k++;
				if (k == 5)
					return CBoard[i][j];
			}
		}
	}
	return -1;
}

void ChessBoard::PeopleGo(char Color)            //人走子函数
{
	int x, y;
	char NextStep[20];
	cout << "请输入坐标:[横坐标在前,纵坐标在后]" << endl;
	cin >> NextStep;
	while (strlen(NextStep) != 2 || !(NextStep[0] >= 'A' && NextStep[0] <= 'Z' || NextStep[0] >= 'a' && NextStep[0] <= 'z') || !(NextStep[1] >= 'A' && NextStep[1] <= 'Z' || NextStep[1] >= 'a' && NextStep[1] <= 'z'))
	{
		cout << "位置格式有误,请重新输入:" << endl;
		cin >> NextStep;
	}
	y = NextStep[0] > 96 ? NextStep[0] - 97 : NextStep[0] - 65;
	x = NextStep[1] > 96 ? NextStep[1] - 97 : NextStep[1] - 65;
	if (CBoard[y][x] == '-1')
	{
		cout << "该位置已经有子了,请重新落子!" << endl;
	}
	else
	{
		CBoard[x][y] = Color;
	}
	
}

附工程截图:

(2017.12.31晚)

(从零到一)五子棋博弈算法——基础框架(不定期更新)_第1张图片

运行结果图:

(2017.12.31晚)

(从零到一)五子棋博弈算法——基础框架(不定期更新)_第2张图片

(从零到一)五子棋博弈算法——基础框架(不定期更新)_第3张图片




你可能感兴趣的:(C++)