網站首頁 編程語言 正文
本文實例為大家分享了C語言實現簡單的三子棋游戲的具體代碼,供大家參考,具體內容如下
1、游戲的整體劃分
因為C語言是面向過程的,我將游戲抽象出來玩家下棋,電腦下棋,在判斷輸贏這一過程,又通過對過程的分析,進行了具體函數的實現,分為如下模塊:
游戲主菜單函數
void menu();
初始化棋盤函數
void InitBoard(char board[ROW][COL], int row, int col);
打印棋盤函數
void DisplayBoard(char board[ROW][COL], int row, int col);
玩家下棋函數
void PlayerMove(char board[ROW][COL], int row, int col);
電腦下棋函數
void ComputerMove(char board[ROW][COL], int row, int col);
判斷棋盤是否為空的函數
int IsFull(char board[ROW][COL], int row, int col);
判斷輸贏的函數
char IsWin(char board[ROW][COL], int row, int col);
//.h文件的源碼,僅供大家參考 #pragma once #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 3 #define COL 3 void menu(); void InitBoard(char board[ROW][COL], int row, int col); void DisplayBoard(char board[ROW][COL], int row, int col); void PlayerMove(char board[ROW][COL], int row, int col); void ComputerMove(char board[ROW][COL], int row, int col); int IsFull(char board[ROW][COL], int row, int col); char IsWin(char board[ROW][COL], int row, int col);
2、整體講解及其菜單函數實現
在對于棋盤方面,利用#define定義了一個大小為3行3列的char類型的數組,來表示整個的棋盤。整體游戲邏輯的實現使用do……while();循環和switch多分支語句的配合使用,使得完成整個游戲的邏輯過程。
菜單menu函數的實現是要配合switch語句來進行的,以便后期對switch語句進行操作,menu函數具體實現如下:
void menu() { ?? ?printf("************歡迎來到三子棋游戲中心**************\n"); ?? ?printf("************1.開始游戲 ? ? ? ? ? ?**************\n"); ?? ?printf("************2.再來一局 ? ? ? ? ? ?**************\n"); ?? ?printf("************0.退出游戲 ? ? ? ? ? ?**************\n"); }
3、初始化棋盤和打印棋盤
1.初始化棋盤
利用函數將數組傳遞過來,對二維數組進行遍歷賦值為空字符,從而完成對整個數組的初始化。具體代碼如下:
void InitBoard(char board[ROW][COL], int row, int col) { ?? ?for (int i = 0; i < row; i++) ?? ?{ ?? ??? ?for (int j = 0; j < col; j++) ?? ??? ?{ ?? ??? ??? ?board[i][j] = ' '; ?? ??? ?} ?? ?} }
2.打印棋盤
打印棋盤時數組的遍歷要配合條件控制下的字符的打印結合,從而使打印的棋盤不僅僅是單純不變的數組,是棋盤被|和-分割開,更加的形象。具體實現如下:
void DisplayBoard(char board[ROW][COL], int row, int col) { ?? ?for (int i = 0; i < row; i++) ?? ?{ ?? ??? ?for (int j = 0; j < col; j++) ?? ??? ?{ ?? ??? ??? ?printf("%c", board[i][j]); ?? ??? ??? ?if (0 == j || 1 == j) ?? ??? ??? ?{ ?? ??? ??? ??? ?printf("|"); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?printf("\n"); ?? ??? ?for (int k = 0; k < col; k++) ?? ??? ?{ ?? ??? ??? ?if (0 == i || 1 == i) ?? ??? ??? ?{ ?? ??? ??? ??? ?printf("--"); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?printf("\n"); ?? ?} }
4、玩家下棋和電腦下棋
1.玩家下棋
玩家下棋時,定義了兩個整型的變量,用來充當數組的下標。當玩家輸入數組的下標位置在合適的范圍的時候,在當前下標賦值了字符‘*’來代表玩家的下棋。并且下棋成功后,將棋盤的情況打印出來,便于玩家了解當前游戲的局勢和情況。具體實現如下:
void PlayerMove(char board[ROW][COL], int row, int col) { ?? ?int r = 0, c = 0; ?? ?printf("請輸入您要下棋的位置:\n"); ?? ?while (1) ?? ?{ ?? ??? ?scanf("%d %d", &r, &c); ?? ??? ?r = r - 1; ?? ??? ?c = c - 1; ?? ??? ?if (board[r][c]==' ') ?? ??? ?{ ?? ??? ??? ?board[r][c] = '*'; ?? ??? ??? ?printf("下棋成功\n"); ?? ??? ??? ?DisplayBoard(board, ROW, COL); ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?printf("輸入的位置有誤或已被占用,請重新輸入:\n"); ?? ??? ?} ?? ?} }
2.電腦下棋
電腦下棋時原來同玩家一樣,區別在于電腦用的不是直接從鍵盤輸入的值,而是利用rand函數隨機生成的值,具體實現如下
void ComputerMove(char board[ROW][COL], int row, int col) { ?? ?int r = 0, c = 0; ?? ?printf("電腦走>\n"); ?? ?while (1) ?? ?{ ?? ??? ?r = rand() % row; ?? ??? ?c = rand() % col; ?? ??? ?if (board[r][c] == ' ') ?? ??? ?{ ?? ??? ??? ?board[r][c] = '#'; ?? ??? ??? ?DisplayBoard(board, ROW, COL); ?? ??? ??? ?break; ?? ??? ?} ?? ?} }
5、判斷輸贏
當棋盤中橫、豎或者對角線的值相等的時候就可分出勝負,如果棋盤已經被下滿并且沒有分出勝負,那就是平局。采用比較暴力遍歷比較,返回4種不同的狀態:
1、玩家贏 - ‘*’
2、電腦贏 - ‘#’
3、平局 - ‘Q’
4、繼續 - ‘C’
具體實現如下:
char IsWin(char board[ROW][COL], int row, int col) { ?? ?int i = 0; ?? ?for (int i = 0; i < row; i++) ?? ?{ ?? ??? ?if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ') ?? ??? ?{ ?? ??? ??? ?return board[i][1]; ?? ??? ?} ?? ?} ?? ?for (int i = 0; i < i; i++) ?? ?{ ?? ??? ?if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ') ?? ??? ?{ ?? ??? ??? ?return board[1][i]; ?? ??? ?} ?? ?} ?? ?if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') ?? ?{ ?? ??? ?return board[1][1]; ?? ?} ?? ?if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') ?? ?{ ?? ??? ?return board[1][1]; ?? ?} ?? ?int ret = IsFull(board, row, col); ?? ?if (ret == 1) ?? ?{ ?? ??? ?return 'Q'; ?? ?} ?? ?return 'C'; }
原文鏈接:https://blog.csdn.net/weixin_51692487/article/details/122303997
相關推薦
- 2022-06-22 深入淺析C/C++?的條件編譯_C 語言
- 2022-05-16 C語言中有哪些字符處理函數你知道嗎_C 語言
- 2022-10-01 詳解Python變量與注釋高級用法_python
- 2022-08-13 Redis 性能影響 - 異步機制和響應延遲
- 2022-11-02 React中編寫CSS實例詳解_React
- 2023-01-10 CentOS7?minimal?最小化安裝網絡設置過程_Linux
- 2022-12-23 Android開發之線程通信詳解_Android
- 2022-06-08 VM配置Centos7虛擬機
- 最近更新
-
- 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同步修改后的遠程分支