本文实例为大家分享了C++实现五子棋小游戏的具体代码,供大家参考,具体内容如下
(这是一个颜色会变化的呦)
#include <iostream>
using namespace std;
int b[20][20];
int n;
int m;
void qipan()
{
system("cls");
int k = 1;
for (int k = 0;k < n;++k)
{
for (int j = 0;j < m;++j)
{
cout << " ---- ";
}
cout << endl;
for (int i = 0;i < n;++i)
{
for (int j = 0;j < m;++j)
{
if (b[i][j] < 10 && b[i][j] >= 0) cout << "| " << 0 << b[i][j] << " |";
else if (b[i][j] == -1) cout << "| " << "█" << " |";
else if (b[i][j] == -2) cout << "|" << " ○ " << "|";
else cout << "| " << b[i][j] << " |";
k++;
}
cout << endl;
for (int j = 0;j < m;++j)
{
cout << " ---- ";
}
cout << endl;
}
}
}
void success()
{
long time;
for (;;)
{
system("color a");
for (time = 0; time < 99999999; time++);
system("color b");
for (time = 0; time < 99999999; time++);
system("color c");
for (time = 0; time < 99999999; time++);
system("color d");
for (time = 0; time < 99999999; time++);
system("color e");
for (time = 0; time < 99999999; time++);
system("color f");
for (time = 0; time < 99999999; time++);
system("color 0");
for (time = 0; time < 99999999; time++);
system("color 1");
for (time = 0; time < 99999999; time++);
system("color 2");
for (time = 0; time < 99999999; time++);
system("color 3");
for (time = 0; time < 99999999; time++);
system("color 4");
for (time = 0; time < 99999999; time++);
system("color 5");
for (time = 0; time < 99999999; time++);
system("color 6");
for (time = 0; time < 99999999; time++);
system("color 7");
for (time = 0; time < 99999999; time++);
system("color 8");
for (time = 0; time < 99999999; time++);
system("color 9");
}
}
int main()
{
cout << "请选择你们想要的棋盘大小(棋盘的面积请不要大于100)" << endl;
cout << "棋盘的长:(推荐长度为10)" << endl;
cin >> m;
cout << "棋盘的宽:(推荐长度为10)" << endl;
cin >> n;
for (int i = 0;i < n;++i)
{
for (int j = 0;j < m;++j)
{
b[i][j] = i * m + j;
}
}
qipan();
while (1)
{
int x;
cout << "请玩家一输入:" << endl;
cin >> x;
if (x == 648)
{
cout << "玩家一使用系统外挂" << endl;
cout << "游戏结束" << endl;
cout << "玩家一获胜" << endl;
success();
return 0;
}
for (int i = 0;i < n;++i)
{
for (int j = 0;j < m;++j)
{
if (b[i][j] == x) b[i][j] = -1;
}
}
for (int i = 0;i < n;++i)
{
for (int j = 0;j < m - 4;++j)
{
if ((b[i][j] == -1 && b[i][j + 1] == -1 && b[i][j + 2] == -1 && b[i][j + 3] == -1 && b[i][j + 4] == -1)
|| (b[i][j] == -1 && b[i + 1][j] == -1 && b[i + 2][j] == -1 && b[i + 3][j] == -1 && b[i + 4][j] == -1)
|| (b[i][j] == -1 && b[i + 1][j + 1] == -1 && b[i + 2][j + 2] == -1 && b[i + 3][j + 3] == -1 && b[i + 4][j + 4] == -1))
{
cout << "游戏结束" << endl;
cout << "玩家一获胜" << endl;
success();
return 0;
}
else if (i >= 4 && (b[i][j] == -1 && b[i - 1][j + 1] == -1 && b[i - 2][j + 2] == -1 && b[i - 3][j + 3] == -1 && b[i - 4][j + 4] == -1))
{
cout << "游戏结束" << endl;
cout << "玩家一获胜" << endl;
success();
return 0;
}
}
}
qipan();
cout << "请玩家二输入:" << endl;
cin >> x;
if (x == 648)
{
cout << "玩家二使用系统外挂" << endl;
cout << "游戏结束" << endl;
cout << "玩家二获胜" << endl;
success();
return 0;
}
for (int i = 0;i < n;++i)
{
for (int j = 0;j < m;++j)
{
if (b[i][j] == x) b[i][j] = -2;
}
}
qipan();
for (int i = 0;i < n;++i)
{
for (int j = 0;j < m - 4;++j)
{
if ((b[i][j] == -2 && b[i][j + 1] == -2 && b[i][j + 2] == -2 && b[i][j + 3] == -2 && b[i][j + 4] == -2)
|| (b[i][j] == -2 && b[i + 1][j] == -2 && b[i + 2][j] == -2 && b[i + 3][j] == -2 && b[i + 4][j] == -2)
|| (b[i][j] == -2 && b[i + 1][j + 1] == -2 && b[i + 2][j + 2] == -2 && b[i + 3][j + 3] == -2 && b[i + 4][j + 4] == -2))
{
cout << "游戏结束" << endl;
cout << "玩家二获胜" << endl;
success();
return 0;
}
else if (i >= 4 && (b[i][j] == -2 && b[i - 1][j + 1] == -2 && b[i - 2][j + 2] == -2 && b[i - 3][j + 3] == -2 && b[i - 4][j + 4] == -2))
{
cout << "游戏结束" << endl;
cout << "玩家二获胜" << endl;
success();
return 0;
}
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。