網站首頁 編程語言 正文
本文實例為大家分享了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;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
?
?
? ? }
}
原文鏈接:https://blog.csdn.net/WUHU648/article/details/108139362
相關推薦
- 2022-04-18 css設置彈性布局讓中間內容寬度自適應
- 2022-12-15 C++?Boost?Lockfree超詳細講解使用方法_C 語言
- 2022-12-28 kotlin開發cli工具小技巧詳解_Android
- 2022-06-30 Python+SymPy實現秒解微積分詳解_python
- 2022-03-27 Redis?使用?List?實現消息隊列的優缺點_Redis
- 2022-03-24 使用Redis如何設置永久有效_Redis
- 2022-04-10 C#實現泛型動態循環數組隊列的方法_C#教程
- 2022-04-27 Jquery實現移動端控制DIV拖拽_jquery
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支