C++课程设计五子棋

C++五子棋

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

人与人之间的对弈
#include
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAX 20
using namespace std;
char p[MAX][MAX * 2];
int x_index = MAX / 2 - 1, y_index = MAX - 2;
int x_human, y_human;
int x_check, y_check;
void Int()
{
int i, j;
for (i = 0; i < MAX; i++)
{
for (j = 0; j < MAX * 2; j += 2)
{
p[i][j] = ‘’;
p[i][j + 1] = ’ ';
}
}
p[x_index][y_index] = ‘0’;
}
void Print()
{
int i, j;
for (i = 0; i < MAX; i++)
{
for (j = 0; j < MAX * 2; j++)
{
cout << p[i][j];
}
cout << “\n”;
}
}
int Check(char c)
{
int x_temp, y_temp;
x_temp = x_check;
y_temp = y_check;
int i;
int left, right, across, vertical;
left = right = across = vertical = 0;
for (i = 0; i < 5; i++) if (p[x_temp][y_temp] == c) { across++; y_temp += 2; }
else { break; }
y_temp = y_check; x_temp = x_check;
for (i = 0; i < 5; i++) if (p[x_temp][y_temp] == c) { across++; y_temp -= 2; }
else { break; }
y_temp = y_check; x_temp = x_check;
if (across > 5) return 1;
for (i = 0; i < 5; i++) if (p[x_temp][y_temp] == c) { vertical++; x_temp += 1; }
else { break; }
y_temp = y_check; x_temp = x_check;
for (i = 0; i < 5; i++) if (p[x_temp][y_temp] == c) { vertical++; x_temp -= 1; }
else { break; }
y_temp = y_check; x_temp = x_check;
if (vertical > 5) return 1;
for (i = 0; i < 5; i++) if (p[x_temp][y_temp] == c) { left++; x_temp -= 1; y_temp -= 2; }
else { break; }
y_temp = y_check; x_temp = x_check;
for (i = 0; i < 5; i++) if (p[x_temp][y_temp] == c) { left++; x_temp += 1; y_temp += 2; }
else { break; }
y_temp = y_check; x_temp = x_check;
if (left > 5) return 1;
for (i = 0; i < 5; i++) if (p[x_temp][y_temp] == c) { right++; x_temp -= 1; y_temp += 2; }
else { break; }
y_temp = y_check; x_temp = x_check;
for (i = 0; i < 5; i++) if (p[x_temp][y_temp] == c) {
right++; x_temp += 1; y_temp -= 2;
}
else { break; }
y_temp = y_check; x_temp = x_check;
if (right > 5) return 1;
return 0;
}
void CheckDot()
{
int min;
int left, right, up, down;
left = right = up = down = 0;
int x_temp = x_index;
int y_temp = y_index;
while (p[x_temp][y_temp + 2] != '
’)
{
y_temp += 2;
right++;
}
x_temp = x_index; y_temp = y_index;
while (p[x_temp][y_temp - 2] != ‘’)
{
y_temp -= 2;
left++;
}
x_temp = x_index; y_temp = y_index;
while (p[x_temp + 1][y_temp] != '
’)
{
x_temp += 1;
up++;
}
x_temp = x_index; y_temp = y_index;
while (p[x_temp - 1][y_temp] != ‘’)
{
x_temp -= 1;
down++;
}
min = up;
if (min > left)
{
min = left;
}
if (min > right)
{
min = right;
}
if (min > down)
{
min = down;
}
if (min == up)
{
x_index -= up;
p[x_index][y_index] = ‘0’;
}
if (min == down)
{
x_index += down;
p[x_index][y_index] = ‘0’;
}
if (min == left)
{
y_index -= (2 * left);
p[x_index][y_index] = ‘0’;
}
if (min == right)
{
y_index += (2 * right);
p[x_index][y_index] = ‘0’;
}
}
void Put(char c)
{
p[x_index][y_index] = c;
while (p[x_index][y_index] != '
’)
{
x_index += 1;
}
p[x_index][y_index] = ‘0’;
}
int Run()
{
int flage = 0;
Int();
Print();
int input;
char human = ‘o’;
char robot = ‘.’;
while (1)
{
input = getch();
if (input == 0x20)
{
x_check = x_index;
y_check = y_index;
x_human = x_index;
y_human = y_index;
if (flage == 0)
{
Put(human);
flage = 1;
if (Check(human) == 1)
{

				return 1;
			}
			system("cls");
			Print();
			continue;
		}
		if (flage == 1)
		{
			Put(robot);
			flage = 0;
			if (Check(robot) == 1)
			{

				return 2;
			}
			system("cls");
			Print();
			continue;
		}


	}
	else if (input == 0xE0)
	{
		int x_temp = x_index;
		int y_temp = y_index;
		input = getch();
		switch (input)
		{
		case 0x4B://left
		{
			if (p[x_index][y_index - 2] != '*')
			{
				while (p[x_index][y_index] != '*')
				{
					y_index -= 2;
				}
			}
			else
			{
				y_index -= 2;
			}
			break;
		}
		case 0x48://up
		{
			if (p[x_index - 1][y_index] != '*')
			{
				while (p[x_index][y_index] != '*')
				{
					x_index -= 1;
				}
			}
			else
			{
				x_index -= 1;
			}
			break;
		}
		case 0x4D://right
		{
			if (p[x_index][y_index + 2] != '*')
			{
				while (p[x_index][y_index] != '*')
				{
					y_index += 2;
				}
			}
			else
			{
				y_index += 2;
			}
			break;
		}
		case 0x50://down
		{
			if (p[x_index + 1][y_index] != '*')
			{
				while (p[x_index][y_index] != '*')
				{
					x_index += 1;
				}
			}
			else
			{
				x_index += 1;
			}
			break;
		}
		}
		if (x_index < 0) x_index = MAX - 2;
		if (y_index < 0) y_index = 2 * MAX - 2;
		if (x_index >= MAX) x_index = 0;
		if (y_index >= MAX * 2) y_index = 0;
		p[x_temp][y_temp] = '*';
		p[x_index][y_index] = '0';
	}
	system("cls");
	Print();
}

}
void show() {
cout << “\n\n\n”;
cout << " --------------------------------- " << endl;
cout << “| |” << endl;
cout << “| 五子棋游戏简介: |” << endl;
cout << “| 黑白双方依次落子. |” << endl;
cout << “| 任一方先在棋盘上 |” << endl;
cout << “| 形成横向、竖向、斜向的 |” << endl;
cout << “| 连续的相同颜色的五个 |” << endl;
cout << “| 棋子的一方为胜 |” << endl;
cout << “| |” << endl;
cout << “| 您是否原因接受以上规则(Y/N)|” << endl;
cout << " --------------------------------- " << endl;
cout << “\n\n”;
char a;
cout << “请您输入您的选择:”; cin >> a;

switch (a)
{
case 'y':
case 'Y':
	return;
case 'n':
case 'N':
	cout << "\n您将退出游戏,谢谢您的使用!" << endl;
	exit(0);
default:
	cout << "您的输入有误!";
	show();	
}

}

int main()
{
system(“mode con cols=50 lines=30”);
system(“color 6e”);
system(“title 五子棋”);
show();

int i = Run();
if (i == 1)
{
	//system("cls");
	cout << " 白棋 are win\n";
}
else if (i == 2)
{
	//ystem("cls");
	cout << " 黑棋 is win\n";
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值