C++小项目--五子棋(Linux系统)

本文介绍如何在Linux系统上使用C++编程实现一个五子棋游戏。通过讲解游戏逻辑、用户交互和棋盘显示,深入理解C++的基本语法和控制结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>
int mx,my;//记录当前落子的坐标
int cnt=0,cntt=0;//cnt 记录当前的落子的人 %2为0 是白子,cntt记录五子连珠的个数
char arr[20][20] = 
{
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
};
char xs(void)
{
	for(int i=0;i<20;i++)
	{
		for(int j=0;j<20;j++)
		{
			printf("%c ",arr[i][j]);
		}
		printf("\n");
	}
}
int lz(void)
{
	while(1)
	{
		if(cnt % 2 == 0)
		{
			printf("请落白子\n");
		}
		else
		{
			printf("请落黑子\n");
		}
		while(scanf("%d%d",&mx,&my)==2)
		{
			if(mx >= 20 || mx < 0 || my >= 20 || my < 0)
			{
				printf("请下在正确位置\n");
				continue;
			}
			else if( arr[mx][my] != '*')
			{
				printf("该位置已有棋子请重新落子\n");
				continue;
			}
			if(cnt % 2 == 0)
			{
				arr[mx][my] = '&';
				cnt++;
				return 0;
			}
			else 
			{
				arr[mx][my] = '@';
				cnt++;
				return 0;
			}
		}
	}
}
int tj(int m_x,int m_y)
{
	while(1)
	{
		if(mx+m_x < 0 || mx+m_x >= 20 || my+m_y < 0 || my+m_y >= 20)
		{
			return;
		}
		else
		{
			if(arr[mx+m_x][my+m_y]==arr[mx][my])
			{
				cntt++;
				if(m_x < 0)m_x--;
				else m_x++;
				if(m_y < 0)m_y--;
				else m_y++;
			}
			else return;
		}
	}
}
int pd(void)
{
	tj(1,1);
	tj(-1,-1);
	if(cntt >= 4)
	{
		return 1;
	}
	cntt = 0;
	tj(-1,1);
	tj(1,-1);
	if(cntt >= 4)
	{
		return 1;
	}
	cntt = 0;
	tj(-1,0);
	tj(1,0);
	if(cntt >= 4)
	{
		return 1;
	}
	cntt = 0;
	tj(0,1);
	tj(0,-1);
	if(cntt >= 4)
	{
		return 1;
	}
	cntt = 0;
	return 0;	
}

int main(int argc,const char* argv[])
{
	for(;;)
	{
		system("clear");
		xs();
		lz();
		if(pd()==1)
		{
			system("clear");
			xs();
			if(cnt % 2 == 1)
			{
				printf("白子&胜利");
			}
			else
			{
				printf("黑子@胜利");
			}
			return 0;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值