跳到正文

休闲·娱乐

四子棋

naoliaok_lovely约 23 分钟
原文
文章信息与版本

文章信息

发布
2024年1月31日
收录
2026年8月10日
更新
2026年11月13日
篇幅
约 23 分钟

版本

  1. v1当前

尝试还原 THUWC Day 2 的工程题。

这里会给出源码,其中我们规定,00 为空格,11 为己方棋子,22 为对方棋子。你需要返回的是你即将下的列。其中,行为 1∼n1\sim n,列为 1∼m1\sim m。注意,在自己的 AI 内,尽量避免定义全局变量,否则可能会与其他程序冲突!

请不要尝试攻击交互库!!!

chess.h:

C++
int get_point1(int n, int m, int nox, int noy, const int a[][20], const int top[]);
int get_point2(int n, int m, int nox, int noy, const int a[][20], const int top[]);
bool judge(int a[][20], int x, int y, int k);

fight.cpp:

C++
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h> 
#include"chess.h"
std::mt19937 rd(time(0));

int n, m, nox, noy, a[20][20], top[20];
int first_player, player = 1;

namespace cursor
{
    void hide()
    {
        CONSOLE_CURSOR_INFO cursor_info = {1, 0};
        SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
    }
}
void setcolor(int fore = 7, int back = 0)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (back << 4) + fore);
}

void write(int x, int y)
{
	system("cls");
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			if(i == nox && j == noy) putchar('X');
			else if(!a[i][j]) putchar('.');
			else if(a[i][j] == 2 ^ first_player == player) setcolor(i == x && j == y ? 15 : 2), putchar('A');
			else setcolor(i == x && j == y ? 15 : 14), putchar('B');
			setcolor(), putchar(' ');
		}
		puts("");
	}
	for(int i = 1; i <= m; i ++ ) printf("__");
	puts("");
	for(int i = 1; i <= m; i ++ ) printf("%d ", i % 10);
	puts("");
	puts(first_player == 1 ? "A: get_point1\nB: get_point2\n" : "A: get_point2\nB: get_point1\n");
}

inline int get(int y)
{
	return top[y] - 1 == nox && y == noy ? top[y] - 2 : top[y] - 1;
}

bool judge(int a[][20], int x, int y, int k)
{
	int cnt = 0;
	for(int j = y; a[x][j] == k; j--) cnt++;
	for(int j = y + 1; a[x][j] == k; j++) cnt++;
	if(cnt >= 4) return 1;
	
	cnt = 0;
	for(int i = x; a[i][y] == k; i--) cnt++;
	for(int i = x + 1; a[i][y] == k; i++) cnt++;
	if(cnt >= 4) return 1;
	
	cnt = 0;
	for(int i = 0; a[x + i][y + i] == k; i--) cnt++;
	for(int i = 1; a[x + i][y + i] == k; i++) cnt++;
	if(cnt >= 4) return 1;
	
	cnt = 0;
	for(int i = 0; a[x + i][y - i] == k; i--) cnt++;
	for(int i = 1; a[x + i][y - i] == k; i++) cnt++;
	if(cnt >= 4) return 1;
	
	return 0;
}

int main()
{
	cursor::hide();
	SetConsoleTitle("四子棋");
	
	n = rd() % 7 + 8, m = rd() % 7 + 8, nox = rd() % n + 1, noy = rd() % m + 1;
	for(int i = 1; i <= m; i++) top[i] = n + 1;
	
	first_player = player = rd() % 2 + 1;
	write(0, 0);
	puts(player == 1 ? "get_point1 先手" : "get_point2 先手");
	Sleep(2000);
	while(1)
	{
		bool flag = 0;
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= m; j++)
				if(!a[i][j] && (i != nox || j != noy))
					flag = 1;
		if(!flag) break;
		
		int t = player == 1 ? get_point1(n, m, nox, noy, a, top) : get_point2(n, m, nox, noy, a, top);
		if(t < 1 || m < t || !get(t))
		{
			puts("行棋错误!");
			puts(player == 1 ? "get_point2 胜利!" : "get_point1 胜利!");
			return getch(), 0;
		}
		a[get(t)][t] = 1, top[t] = get(t);
		write(top[t], t);
		
		if(judge(a, top[t], t, 1))
		{
			puts(player == 1 ? "get_point1 胜利!" : "get_point2 胜利!");
			return getch(), 0;
		}
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= m; j++)
				if(a[i][j] == 1) a[i][j] = 2;
				else if(a[i][j] == 2) a[i][j] = 1;
		
		player = 3 - player;
	}
	
	puts("平局!");
	return getch(), 0;
}

player1.cpp:

C++
#include<bits/stdc++.h>
#include"chess.h"

int get_point1(int n, int m, int nox, int noy, const int a[][20], const int top[])
{
	int x;
	std::cin >> x;
	return x;
}

player2.cpp:

C++
#include<bits/stdc++.h>
#include"chess.h"

int get_point2(int n, int m, int nox, int noy, const int a[][20], const int top[])
{
	int x;
	std::cin >> x;
	return x;
}

make.cpp:(可以用来代替命令行编译,但是请确保电脑已经配备了 g++)

C++
#include<windows.h>
using namespace std;

int main()
{
	system("g++ fight.cpp player1.cpp player2.cpp -o chess.exe -std=c++14 -O2 -Wall && chess.exe");
	return 0;
}

当然了,这里的 AI 都是人工操作的。可以自己写 AI,为了游戏体验,建议在 AI 前加入一行 getchar() 之类的语句。(如果 AI 的行棋过快的话!)

附:刚写了一个十分简单的 AI,可以试玩。

aoliao.cpp

C++
#include<bits/stdc++.h>
#include<conio.h>
#include"chess.h"

inline double max(double a, double b)
{
	return a > b ? a : b;
}

double dfs(int step, int n, int m, int nox, int noy, int a[][20], int top[], int yesrow)
{
	static std::mt19937 rd(296523659);
	static int step1[] = {0,99,99,99,15,12,10, 9, 8, 8, 7, 7, 7, 6, 6},
			   step2[] = {0, 0, 0, 0, 0, 0, 1, 3, 1, 3, 0, 3, 7, 0, 1};
	if(step > step1[yesrow] + !(rd() & step2[yesrow])) return 0.8 * rd() / UINT_MAX - 0.4;
	auto get = [&](int y)
	{
		return top[y] - 1 == nox && y == noy ? top[y] - 2 : top[y] - 1;
	};
	
	double maxn = -1e9;
	int cnt = 0;
	for(int i = 1; i <= m; i++)
		if(get(i))
		{
			cnt++;
			
			int t = top[i];
			top[i] = get(i), a[top[i]][i] = step & 1 ? 1 : 2;
			if(judge(a, top[i], i, step & 1 ? 1 : 2)) maxn = 1;
			else maxn = max(maxn, -dfs(step + 1, n, m, nox, noy, a, top, yesrow));
			a[top[i]][i] = 0, top[i] = t;
		}
	return cnt ? maxn : 0;
}

int get_point1(int n, int m, int nox, int noy, const int a1[][20], const int top1[])
{
	static int a[20][20], top[20];
	int yesrow = m;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			a[i][j] = a1[i][j];
	for(int i = 1; i <= m; i++) top[i] = top1[i], yesrow -= !top[i];
	
	auto get = [&](int y)
	{
		return top[y] - 1 == nox && y == noy ? top[y] - 2 : top[y] - 1;
	};
	
	double maxn = -1e9;
	int id = 0;
	for(int i = 1; i <= m; i++)
		if(get(i))
		{
			int t = top[i];
			top[i] = get(i), a[top[i]][i] = 1;
			if(judge(a, top[i], i, 1))
			{
				maxn = 1, id = i;
				break;
			}
			
			double res = -dfs(2, n, m, nox, noy, a, top, yesrow);
			if(res > maxn) maxn = res, id = i;
			a[top[i]][i] = 0, top[i] = t;
		}
	if(maxn > -0.9) return id;
	
	//自动检测必败,但是由于体育精神,不能摆烂!
	std::vector<int> s;
	for(int i = 1; i <= m; i++)
		if(get(i))
		{
			s.push_back(i);
			int t = top[i];
			top[i] = get(i), a[top[i]][i] = 2;
			if(judge(a, top[i], i, 2)) return i;
			a[top[i]][i] = 0, top[i] = t;
		}
	static std::mt19937 rd(296523659);
	return s[rd() % s.size()];
}

acoipp.cpp

C++
#include<bits/stdc++.h>
#include"chess.h"
#include<conio.h>
#define ll long long
#define N 20

namespace acoipp{
	ll n,m,i,j,nx,ny,top[N],topp[N],b[N][N],bb[N][N],opt,x,res,ans,temp;
	inline void down(ll x,ll col){
		bb[topp[x]][x] = col;
		topp[x]--;
		if(x==ny&&topp[x]==nx) topp[x]--;
	}
	inline ll chc(){
		for(ll i=1;i<=n;i++){
			for(ll j=1;j<=m;j++){
				if(i+3<=n){
					if(bb[i][j]!=3&&bb[i][j]==bb[i+1][j]&&bb[i+1][j]==bb[i+2][j]&&bb[i+2][j]==bb[i+3][j]){
						if(bb[i][j]==2) return 1;
						return -1;
					}
				}
				if(j+3<=m){
					if(bb[i][j]!=3&&bb[i][j]==bb[i][j+1]&&bb[i][j+1]==bb[i][j+2]&&bb[i][j+2]==bb[i][j+3]){
						if(bb[i][j]==2) return 1;
						return -1;
					}
				}
				if(i+3<=n&&j+3<=m){
					if(bb[i][j]!=3&&bb[i][j]==bb[i+1][j+1]&&bb[i+1][j+1]==bb[i+2][j+2]&&bb[i+2][j+2]==bb[i+3][j+3]){
						if(bb[i][j]==2) return 1;
						return -1;
					}
				}
				if(i+3<=n&&j-3>=1){
					if(bb[i][j]!=3&&bb[i][j]==bb[i+1][j-1]&&bb[i+1][j-1]==bb[i+2][j-2]&&bb[i+2][j-2]==bb[i+3][j-3]){
						if(bb[i][j]==2) return 1;
						return -1;
					}
				}
			}
		}
		return 0;
	}
	
	ll check(ll x){
		for(ll i=1;i<=m;i++) topp[i]=top[i];
		for(ll i=1;i<=n;i++) for(ll j=1;j<=m;j++) bb[i][j]=b[i][j];
		down(x,2);
		if(chc()) return 10000;
		for(ll i=1;i<=n;i++){
			for(ll j=1;j<=m;j++){
				if(i+3<=n){
					if(bb[i][j]==1&&bb[i+1][j]==1&&bb[i+2][j]==1&&topp[j]==i+3) return 0;
					if(bb[i][j]==1&&bb[i+1][j]==1&&bb[i+3][j]==1&&topp[j]==i+2) return 0;
					if(bb[i][j]==1&&bb[i+3][j]==1&&bb[i+2][j]==1&&topp[j]==i+1) return 0;
					if(bb[i+3][j]==1&&bb[i+1][j]==1&&bb[i+2][j]==1&&topp[j]==i) return 0;
				}
				if(j+3<=m){
					if(bb[i][j]==1&&bb[i][j+1]==1&&bb[i][j+2]==1&&topp[j+3]==i) return 0;
					if(bb[i][j]==1&&bb[i][j+1]==1&&bb[i][j+3]==1&&topp[j+2]==i) return 0;
					if(bb[i][j]==1&&bb[i][j+3]==1&&bb[i][j+2]==1&&topp[j+1]==i) return 0;
					if(bb[i][j+3]==1&&bb[i][j+1]==1&&bb[i][j+2]==1&&topp[j]==i) return 0;
				}
				if(i+3<=n&&j+3<=m){
					if(bb[i][j]==1&&bb[i+1][j+1]==1&&bb[i+2][j+2]==1&&topp[j+3]==i+3) return 0;
					if(bb[i][j]==1&&bb[i+1][j+1]==1&&bb[i+3][j+3]==1&&topp[j+2]==i+2) return 0;
					if(bb[i][j]==1&&bb[i+3][j+3]==1&&bb[i+2][j+2]==1&&topp[j+1]==i+1) return 0;
					if(bb[i+3][j+3]==1&&bb[i+1][j+1]==1&&bb[i+2][j+2]==1&&topp[j]==i) return 0;
				}
				if(i+3<=n&&j-3>=1){
					if(bb[i][j]==1&&bb[i+1][j-1]==1&&bb[i+2][j-2]==1&&topp[j-3]==i+3) return 0;
					if(bb[i][j]==1&&bb[i+1][j-1]==1&&bb[i+3][j-3]==1&&topp[j-2]==i+2) return 0;
					if(bb[i][j]==1&&bb[i+3][j-3]==1&&bb[i+2][j-2]==1&&topp[j-1]==i+1) return 0;
					if(bb[i+3][j-3]==1&&bb[i+1][j-1]==1&&bb[i+2][j-2]==1&&topp[j]==i) return 0;
				}
			}
		}
		ll p = 0;
		while(1){
			ll res = 0,pos = 0;
			for(ll i=1;i<=m;i++) if(topp[i]!=0) res++;
			if(res==0) return 0;
			res = rand()%res+1;
			for(ll i=1;i<=m;i++){
				if(topp[i]!=0) res--;
				if(res==0){
					pos=i;
					break;
				}
			}
			down(pos,(p==0)?1:2);
			if(chc()) return chc()>0?1:0;
			p=(p+1)%2;
		}
		return 0;
	}
	int solve(){
		res = -1,ans = -1;
		for(i=1;i<=m;i++){
			if(top[i]==0){
				std::cout<<0<<std::endl;
				continue;
			}
			temp = 0;
			for(j=1;j<=1e3;j++) temp += check(i);
			if(temp>res) res=temp,ans=i;
		}
		return ans;
	}
}
int get_point2(int n, int m, int nox, int noy, const int a[][20], const int top[]){
	acoipp::n=n,acoipp::m=m,acoipp::nx=nox,acoipp::ny=noy;
	for(int i=1;i<=m;i++) acoipp::top[i]=top[i]-1==nox&&i==noy?top[i]-2:top[i]-1;
	for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) acoipp::b[i][j]=3-a[i][j];
	for(ll i=1;i<=n;i++){
		for(ll j=1;j<=m;j++){
			if(i+3<=n){
				if(acoipp::b[i][j]==1&&acoipp::b[i+1][j]==1&&acoipp::b[i+2][j]==1&&acoipp::top[j]==i+3) return j;
				if(acoipp::b[i][j]==1&&acoipp::b[i+1][j]==1&&acoipp::b[i+3][j]==1&&acoipp::top[j]==i+2) return j;
				if(acoipp::b[i][j]==1&&acoipp::b[i+3][j]==1&&acoipp::b[i+2][j]==1&&acoipp::top[j]==i+1) return j;
				if(acoipp::b[i+3][j]==1&&acoipp::b[i+1][j]==1&&acoipp::b[i+2][j]==1&&acoipp::top[j]==i) return j;
			}
			if(j+3<=m){
				if(acoipp::b[i][j]==1&&acoipp::b[i][j+1]==1&&acoipp::b[i][j+2]==1&&acoipp::top[j+3]==i) return j+3;
				if(acoipp::b[i][j]==1&&acoipp::b[i][j+1]==1&&acoipp::b[i][j+3]==1&&acoipp::top[j+2]==i) return j+2;
				if(acoipp::b[i][j]==1&&acoipp::b[i][j+3]==1&&acoipp::b[i][j+2]==1&&acoipp::top[j+1]==i) return j+1;
				if(acoipp::b[i][j+3]==1&&acoipp::b[i][j+1]==1&&acoipp::b[i][j+2]==1&&acoipp::top[j]==i) return j;
			}
			if(i+3<=n&&j+3<=m){
				if(acoipp::b[i][j]==1&&acoipp::b[i+1][j+1]==1&&acoipp::b[i+2][j+2]==1&&acoipp::top[j+3]==i+3) return j+3;
				if(acoipp::b[i][j]==1&&acoipp::b[i+1][j+1]==1&&acoipp::b[i+3][j+3]==1&&acoipp::top[j+2]==i+2) return j+2;
				if(acoipp::b[i][j]==1&&acoipp::b[i+3][j+3]==1&&acoipp::b[i+2][j+2]==1&&acoipp::top[j+1]==i+1) return j+1;
				if(acoipp::b[i+3][j+3]==1&&acoipp::b[i+1][j+1]==1&&acoipp::b[i+2][j+2]==1&&acoipp::top[j]==i) return j;
			}
			if(i+3<=n&&j-3>=1){
				if(acoipp::b[i][j]==1&&acoipp::b[i+1][j-1]==1&&acoipp::b[i+2][j-2]==1&&acoipp::top[j-3]==i+3) return j-3;
				if(acoipp::b[i][j]==1&&acoipp::b[i+1][j-1]==1&&acoipp::b[i+3][j-3]==1&&acoipp::top[j-2]==i+2) return j-2;
				if(acoipp::b[i][j]==1&&acoipp::b[i+3][j-3]==1&&acoipp::b[i+2][j-2]==1&&acoipp::top[j-1]==i+1) return j-1;
				if(acoipp::b[i+3][j-3]==1&&acoipp::b[i+1][j-1]==1&&acoipp::b[i+2][j-2]==1&&acoipp::top[j]==i) return j;
			}
		}
	}
	return acoipp::solve();
}