基于C++实现五子棋小游戏
时间:2023-03-02 09:34:44|栏目:C代码|点击: 次
本文实例为大家分享了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; } } } } }
上一篇:C语言的数组指针与函数指针详解
栏 目:C代码
下一篇:C语言实现学生成绩管理系统项目
本文标题:基于C++实现五子棋小游戏
本文地址:http://www.codeinn.net/misctech/226733.html