c++五子棋来啦
  • 板块灌水区
  • 楼主qyzs1110
  • 当前回复6
  • 已保存回复6
  • 发布时间2021/5/29 11:31
  • 上次更新2023/11/4 22:34:51
查看原帖
c++五子棋来啦
286454
qyzs1110楼主2021/5/29 11:31

这几天和万灭、蓝鲸玩五子棋,咱就想着做个程序出来

这不,我们整出来啦!

这是我和他合作的成果

虽说我只帮他做了判断程序,在优化下子

现在我把这程序公布在网站上,有兴趣的可以看看

有bug尽管提! 程序内很简单,一看就懂

#include <bits/stdc++.h>
using namespace std;

int a[25][25];

int dx[15] = {0, 1, -1, 0, 0, 1, -1, 1, -1};
int dy[15] = {0, 0, 0, 1, -1, -1, 1, 1, -1};

void chess()
{
	system("cls");
	cout << setw(33) << "Îå×ÓÆå";
	cout << endl << "   ";
	for (int i = 1; i <= 19; i++)
		cout << setw(2) << i << " ";
	cout << " x×ø±ê" << endl;
	for (int i = 1; i <= 19; i++)
	{
		cout << setw(2) << i;
		for (int j = 1; j <= 19; j++)
		{
			if (a[i][j] == 0)  cout << setw(3) << "+";
			if (a[i][j] == 1)  cout << setw(3) << "¡ö";
			if (a[i][j] == 2)  cout << setw(3) << "¡õ";
		}
		cout << endl;
	}
	cout <<"y"<< endl << "×ø" << endl << "±ê" << endl << endl;
}

bool check(int x, int y)
{
	int t = a[x][y];
	for (int i = 1; i <= 4; i++)
	{
		int tx = x, ty = y, ans = 1;
		for(int k = 1; k <= 2; k++)
		{
			int ttx = tx;
			int tty = ty;
			for (int j = 1; j <= 4; j++)
			{
				ttx += dx[i * 2 - (2 - k)];
				tty += dy[i * 2 - (2 - k)];
				if(a[ttx][tty] == t) ans = ans + 1;
				else break;
			}
		}
		if(ans == 5) return 1;
		
	}
	return 0;
}

int main()
{
	string now = "Black"; 
	while (1)
	{
		int x, y;
		chess();
		do
		{
			cout << " Now:" << now << endl << "ÇëÊäÈëx£¬y×ø±ê£º"  ;
			cin >> y >> x;
		}
		while(x < 1 || y < 1|| x > 19 || y > 19 || a[x][y] != 0); 
		if (now == "Black")
		{
			a[x][y] = 1;
			now = "White";
			if (check(x, y) == 1)
			{
				chess();
				cout << "Black Win!" << endl;
				exit(0); 
			}
		}
		else
		{
			a[x][y] = 2;
			now = "Black";
			if(check(x, y) == 1)
			{
				chess();
				cout << "White Win" << endl;
				exit(0);
			}
		}
	}
	return 0;
}
2021/5/29 11:31
加载中...