C++小游戏——五子棋 v1.0.0

加入洛谷团队98418获取更多游戏资源!

五子棋 v1.0.0

代码见下,或前往团队下载

#include<bits/stdc++.h>
#include<windows.h>
#include <stdlib.h>
#include<conio.h>

using namespace std;

int node[1000][1000][2], nxt, vis[1000][1000][2];

void SetSize(unsigned uCol,unsigned uLine)
{
 char cmd[64];
 sprintf(cmd,"mode con cols=%d lines=%d",uCol,uLine);
 system(cmd);
}

void printStr(int startX, int startY, char str[])
{
	HANDLE hd;
	COORD pos;
	pos.X = startX;
	pos.Y = startY;
	hd = GetStdHandle(STD_OUTPUT_HANDLE);	/*获取标准输出的句柄*/ 
	SetConsoleCursorPosition(hd, pos);		/*设置控制台光标输出的位置*/
	printf("%s", str);
}

int Check(int x, int y, int kind)
{
	int num[10] = {0}, ans = 0;
	int dx[10] = {0, -2, -2, -2, 0, 0, 2, 2, 2};
	int dy[10] = {0, -1, 0, 1, -1, 1, -1, 0, 1};
	for (int i = 1; i <= 8; i++)
	{
		int tx = x + dx[i], ty = y + dy[i];
		while (ty >= 0 && tx <= 54 && ty >= 0 && ty <= 27 && node[tx][ty][kind] == 1)
		{
			num[i]++;
			tx += dx[i];
			ty += dy[i];
		}
	}
	for (int i = 1; i <= 4; i++)
	{
		ans = max(ans, num[i] + num[9 - i] + 1);
	}
	return ans;
}

int move(int x, int y, int kind)
{
	char ch;
	while ( ( ch = _getch() ) != '\r' )
	{
		if (ch == 'w') y--; //上 
		else if (ch == 's') y++; //下 
		else if (ch == 'a') x -= 2; //左 
		else if (ch == 'd') x += 2; //右 
		if (x < 0) x += 2; //边界处理 
		if (x >= 54) x -= 2;
		if (y < 0) y++;
		if (y >= 27) y--;
		if (node[x][y][0]) //移动位置 
		{
			printStr(x, y, "@");
		}
		else if (node[x][y][1])
		{
			printStr(x, y, "O");
		}
		else printStr(x, y, "+");
	}
	if (node[x][y][(kind + 1) % 2]) //判断落子位置是否合法 
	{
		nxt++;
		return 0;
	}
	else if (node[x][y][kind])
	{
		nxt++;
		return 0;
	}
	else
	{
		if (kind) printStr(x, y, "O");
		else printStr(x, y, "@");
	}
	node[x][y][kind]++; //标记位置 
	return Check(x, y, kind);
}

void Color()
{
	for (int i = 1; i <= 20; i++) 
	{
		system("color 10");
		system("color 20");
		system("color 30");
		system("color 40");
		system("color 50");
		system("color 60");
		system("color 70");
		system("color 80");
		system("color 90");
		system("color a0");
		system("color b0");
		system("color c0");
		system("color d0");
		system("color f0");
		system("color e0");
	}
}

int main(int argc, char *argv[])
{
	SetSize(54, 29);
	for (int i = 0; i < 27; i++) //建棋盘 
		for (int j = 0; j < 27; j++)
			printStr(i * 2, j, "+");
	int i = 0, last = 0, time = 0;
	while (1)
	{
//		printStr(0, 0, "+"); //光标移动到 0,0 
		last = i;
		time = move(0, 0, i);
		i = ((i + 1) + nxt) % 2;
		printStr(0, 28, "");
		if (time == 5)
		{
			system("CLS");
			printStr(15, 14, "");
			printf("恭喜持%s的选手获胜!", (last == 0)? "黑棋": "白棋");
			Color();
			printStr(15, 16, "");
			system("pause");
			return 0;
		}
		if (last == i)
		{
			printf("落子不合法,请%s重下!", (i == 0)? "黑棋": "白棋");
			printStr(0, 28, "");
		}
		else
		{
			printf("落子合法。                     ");
			printStr(0, 28, "");
			Sleep(1000);
			printf("请%s下!                       ", (i == 0)? "黑棋": "白棋");
			printStr(0, 28, "");
		}
		nxt = 0;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值