C語言編一個數字益智小遊戲
阿新 • • 發佈:2020-01-07
程式功能及執行情況
設計的程式是一個數字益智遊戲,旨在培養小朋友玩家的數學思維,提高玩家的數學能力。遊戲共設有四個不同的小遊戲,分別是一位數四則運算、兩位數四則運算、找最值遊戲、排序遊戲。程式能實現產生隨機題目,並能檢驗玩家的作答是否正確。為了小朋友能感受到遊戲的樂趣,特意設定了得分系統,答對不同題目有不同的加分,並會根據總得分劃分不同「段位」,呈現給玩家。另外,程式還運用了更加充滿活力的設計,讓玩家愛上該遊戲。
程式執行截圖主選單
運算遊戲
找最值遊戲
排序遊戲
得分統計
程式碼部分標頭檔案區域
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #include<math.h> int score0 = 0; int u,z;
啟動介面及主選單
int main() //主函式遊戲說明 { int menu(); char o; printf("\n\n"); printf("\t= = = = = = = = = = = = = = = =\n"); printf("\t= 數 字 益 智 小 遊 戲 =\n"); printf("\t= =\n"); printf("\t= 歡迎遊玩該數字益智小遊戲,遊戲共設有 =\n"); printf("\t= 四個小遊戲,其中『算』小遊戲各設有四 =\n"); printf("\t= 個關卡。當你挑戰難度越高的小遊戲,並 =\n"); printf("\t= 挑戰成功,你將獲得更高的得分獎勵,最 =\n"); printf("\t= 終將根據你的總得分劃分你的遊戲段位。 =\n"); printf("\t= =\n"); printf("\t= =\n"); printf("\t= * * * * * * * * * =\n"); printf("\t= * 遊 戲 段 位 * =\n"); printf("\t= * 『青銅』 --- 5 分 以 下 * =\n"); printf("\t= * 『白銀』 --- 5 分 - 10 分 * =\n"); printf("\t= * 『黃金』 --- 10 分 - 15 分 * =\n"); printf("\t= * 『鉑金』 --- 15 分 - 20 分 * =\n"); printf("\t= * 『鑽石』 --- 20 分 - 25 分 * =\n"); printf("\t= * 『星耀』 --- 25 分 - 30 分 * =\n"); printf("\t= * 『王者』 --- 30 分 以 上 * =\n"); printf("\t= * * * * * * * * * =\n"); printf("\t= =\n"); printf("\t= 作 者 : Cowjiang =\n"); printf("\t= =\n"); printf("\t= = = = = = = = = = = = = = = =\n\n"); printf("\t 按回車鍵即可開始遊戲"); scanf_s("%c",&o,1); if (o == '\n') //輸入回車開始遊戲 { system("cls"); menu(); } else exit(0); } int menu() //遊戲主選單函式 { void one_arithmetic(); void two_arithmetic(); void comparation(); void maxmin(); int score(); int scores(); int scorep(); int o; system("color 30"); printf("\n\n"); printf("\t ** \n"); printf("\t * * \n"); printf("\t * 主 菜 單 * \n"); printf("\t * * \n"); printf("\t* * * * * * * * * * * * * * * \n"); printf("\t * * \n"); printf("\t * 1.『 算 』一位數運算 * \n"); printf("\t * 2.『 算 』兩位數運算 * \n"); printf("\t * 3.『 找 』夾縫找最值 * \n"); printf("\t * 4.『 排 』排序比大小 * \n"); printf("\t * 5. 我 的 目 前 得 分 * \n"); printf("\t * 6. 退 出 益 智 遊 戲 * \n"); printf("\t * * \n"); printf("\t * * * * * * * * * \n\n"); printf("\t 請輸入序號選擇:"); scanf_s("%d",&o); if (o == 1) { one_arithmetic(); } else if (o == 2) { two_arithmetic(); } else if (o == 3) { maxmin(); } else if (o == 4) { comparation(); } else if (o == 5) { scorep(); } else if (o == 6) { printf("\n\t\t= = = = = = = = = = = = = = = = =\n"); printf("\t\t= =\n"); printf("\t\t= 感謝你的遊玩! =\n"); printf("\t\t= =\n"); printf("\t\t= = = = = = = = = = = = = = = = =\n"); exit(0); } else { printf("\n\t 你輸入的數字序號有誤噢,請重新選擇!\n"); system("pause"); system("cls"); menu(); } }
得分統計
int scorep() //檢視當前得分 { system("color F4"); z = score0; system("cls"); printf("\n\n"); printf("\t\t * \n"); printf("\t\t * * \n"); printf("\t\t * * \n"); printf("\t\t* * * * * * * * \n"); printf("\t\t * 當前得分: * \n"); printf("\t\t * %2d分 * \n",z); printf("\t\t * * * * \n"); printf("\t\t * * * * \n"); if (z <= 5) { printf("\t\t ** [青 銅] ** \n"); printf("\t\t * * \n"); } else if (z > 5 && z <= 10) { printf("\t\t ** [白 銀] ** \n"); printf("\t\t * * \n"); } else if (z > 10 && z <= 15) { printf("\t\t ** [黃 金] ** \n"); printf("\t\t * * \n"); } else if (z > 15 && z <= 20) { printf("\t\t ** [鉑 金] ** \n"); printf("\t\t * * \n"); } else if (z > 20 && z <= 25) { printf("\t\t ** [鑽 石] ** \n"); printf("\t\t * * \n"); } else if (z > 25 && z <= 30) { printf("\t\t ** [星 耀] ** \n"); printf("\t\t * * \n"); } else if (z > 30) { printf("\t\t ** [王 者] ** \n"); printf("\t\t * * \n"); } printf("\n\n\t 繼續遊戲請輸入1,退出遊戲請輸入2 :"); scanf_s("%d",&u); if (u == 1) //返回主選單 { system("cls"); menu(); } else if (u == 2) //退出遊戲 { printf("\n\t\t= = = = = = = = = = = = = = = = =\n"); printf("\t\t= =\n"); printf("\t\t= 感謝你的遊玩! =\n"); printf("\t\t= =\n"); printf("\t\t= = = = = = = = = = = = = = = = =\n"); exit(0); } else { printf("\n\t 你輸入的數字序號有誤噢,返回主選單!\n"); menu(); } } int score() //加分函式 { score0++; return score0; } int scores() //減分函式 { score0--; return score0; }
一位數運算
void one_arithmetic() //一位數運算 { int p,a,b,c,d,f,g,h,l,m; float e; srand(time(NULL)); a = rand() % 10; b = rand() % 10; g = rand() % 10; h = rand() % 10; system("color E0"); system("cls"); printf("\n\n\t\t * * * * * * * * \n"); //一位數運算主選單 printf("\t\t * * \n"); printf("\t\t * 1. [第一關] 加 法 * \n"); printf("\t\t * 2. [第二關] 減 法 * \n"); printf("\t\t * 3. [第三關] 乘 法 * \n"); printf("\t\t * 4. [第四關] 除 法 * \n"); printf("\t\t * 5. [進階]混合運算 * \n"); printf("\t\t * 6. [返 回] 主選單 * \n"); printf("\t\t * * \n"); printf("\t\t * * * * * * * * \n"); printf("\t\t * * * \n"); printf("\t\t * * * \n"); printf("\t\t * * * * * \n"); printf("\t\t * * * \n"); printf("\t\t * \n"); printf("\t\t請輸入數字序號選擇挑戰關卡:"); scanf_s("%d",&p); while (p == 1) //一位數加法 { system("cls"); printf("\n\t%d + %d = ",b); scanf_s("%d",&c); if (c == a + b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != a + b) { system("color 4E"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } one_arithmetic(); } while (p == 2) //一位數減法 { system("cls"); if (a > b) //控制大數減小數 { printf("\n\t%d - %d = ",b); scanf_s("%d",&c); if (c == a - b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != a - b) { system("color 4E"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } one_arithmetic(); } else if (a < b) { system("cls"); printf("\n\t%d - %d = ",a); scanf_s("%d",&c); if (c == b - a) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != b - a) { system("color 4E"); printf("\n\t太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } one_arithmetic(); } } while (p == 3) //一位數乘法 { system("cls"); printf("\n\t%d * %d = ",&c); if (c == a * b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != a * b) { system("color 4E"); printf("\n\t\t太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } one_arithmetic(); } while (p == 4) //一位數除法 { system("cls"); b = rand() % (9) + 1; //防止被除數為0 printf("\n\t(四捨五入保留整數)%d除以%d等於",b); scanf_s("%f",&e); f = b / 2; //四捨五入 if (a % b <= f) { d = (int)(e); if (d == a / b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (d != a / b) { system("color 4E"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } } if (a % b > f) { d = (int)(e - 0.5); if (d == a / b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (d != a / b) { system("color 4E"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } } one_arithmetic(); } while (p == 5) //一位數混合運算 { system("cls"); printf("\n\t%d + %d * ( %d - %d ) = ",h); scanf_s("%d",&l); m = a + b * (g - h); if (l == m) { system("color AE"); printf("\n太棒了!挑戰成功,加 2 分!\n\n"); score(); score(); system("pause"); } else if (l != m) { system("color 4E"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } one_arithmetic(); } while (p == 6) { system("cls"); menu(); } while (p < 1 || p > 6) { system("color 4E"); printf("\n\t\t輸入數字序號有誤噢,請重新輸入!\n\n"); system("pause"); one_arithmetic(); } }
兩位數運算
void two_arithmetic() //兩位數運算 { int p,m; float e; srand(time(NULL)); a = rand() % (90) + 10; b = rand() % (90) + 10; g = rand() % (90) + 10; h = rand() % (90) + 10; system("color B0"); system("cls"); printf("\n\n\t\t * * * * * * * * \n"); //兩位數運算主選單 printf("\t\t * * \n"); printf("\t\t * 1. [第一關] 加 法 * \n"); printf("\t\t * 2. [第二關] 減 法 * \n"); printf("\t\t * 3. [第三關] 乘 法 * \n"); printf("\t\t * 4. [第四關] 除 法 * \n"); printf("\t\t * 5. [進階]混合運算 * \n"); printf("\t\t * 6. [返 回] 主選單 * \n"); printf("\t\t * * \n"); printf("\t\t * * * * * * * * \n"); printf("\t\t * * * \n"); printf("\t\t * * * \n"); printf("\t\t * * * * * \n"); printf("\t\t * * * \n"); printf("\t\t * \n"); printf("\t\t請輸入數字序號選擇挑戰關卡:"); scanf_s("%d",&p); while (p == 1) //兩位數加法 { system("cls"); printf("\n\t%d + %d = ",&c); if (c == a + b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != a + b) { system("color 4B"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } two_arithmetic(); } while (p == 2) //兩位數減法 { if (a > b) //控制大數減小數 { system("cls"); printf("\n\t%d - %d = ",&c); if (c == a - b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != a - b) { system("color 4B"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } two_arithmetic(); } else if (a < b) { system("cls"); printf("\n\t%d - %d = ",&c); if (c == b - a) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != b - a) { system("color 4B"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } two_arithmetic(); } } while (p == 3) //兩位數乘法 { system("cls"); printf("\n\t%d * %d = ",&c); if (c == a * b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != a * b) { system("color 4B"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } two_arithmetic(); } while (p == 4) //兩位數除法 { b = rand() % (9) + 1; //防止被除數為0 system("cls"); printf("\n\t(四捨五入保留整數)%d除以%d等於",&e); f = b / 2; //四捨五入 if (a % b <= f) { d = (int)(e); if (d == a / b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (d != a / b) { system("color 4B"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } } if (a % b > f) { d = (int)(e - 0.5); if (d == a / b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (d != a / b) { system("color 4B"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } } two_arithmetic(); } while (p == 5) //兩位數混合運算 { system("cls"); printf("\n\t%d + %d * ( %d - %d ) = ",&l); m = a + b * (g - h); if (l == m) { system("color AE"); printf("\n太棒了!挑戰成功,加 2 分!\n\n"); score(); score(); system("pause"); } else if (l != m) { system("color 4B"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } one_arithmetic(); } while (p == 6) { system("cls"); menu(); } while (p < 1 || p > 6) { system("color 4B"); printf("\n\t\t輸入數字序號有誤噢,請重新輸入!\n\n"); system("pause"); two_arithmetic(); } }
排序遊戲
void comparation() //排序遊戲 { int a[5],b[5]; int i,j,n,p,q; srand(time(NULL)); for (i = 0; i < 5; i++) a[i] = rand() % 10; system("cls"); system("color 9F"); printf("\n\n\t* * * * * * * * * * * * * *\n"); //排序遊戲規則 printf("\t* 遊 戲 規 則 *\n"); printf("\t* 開始遊戲後將會顯示隨機五個整數, *\n"); printf("\t* 請根據提示從小到大依次輸入排序後 *\n"); printf("\t* 的五個整數,若排序正確即可獲得 3 *\n"); printf("\t* 分獎勵。加油噢,你能行! *\n"); printf("\t* * * * * * * * * * * * * *\n"); printf("\t * * * \n"); printf("\t * * * \n"); printf("\t * * * * * \n"); printf("\t * * * \n"); printf("\t * \n"); printf("\t 輸入數字 0 即可開始遊戲:"); scanf_s("%d",&p); if (p == 0) printf("\n\n\t* * * * * * * * * * * * * *\n"); printf("\t* *\n"); printf("\t* "); for (i = 0; i < 5; i++) printf(" %d ",a[i]); for (j = 0; j < 4; j++) //將陣列從小到大排列 { for (i = 0; i < 4 - j; i++) { if (a[i] > a[i + 1]) { n = a[i]; a[i] = a[i + 1]; a[i + 1] = n; } } } printf(" *\n"); printf("\t* *\n"); printf("\t* * * * * * * * * * * * * *\n\n"); printf("接下來,請從小到大輸入排序後的五個整數(用空格隔開):"); for (i = 0; i < 5; i++) scanf_s("%d",&b[i]); for (i = 0; i < 5; i++) //驗證回答正確與否 { if (a[i] == b[i]) continue; else { system("color 49"); printf("\n\t太可惜了,就差一點就成功了!再挑戰一次吧!\n\n"); system("pause"); comparation(); } } system("color AF"); printf("\n哇你太棒啦,挑戰成功!獲得 3 分獎勵!\n\n"); score(); score(); score(); system("pause"); system("color 0F"); printf("\n\n想挑戰別的遊戲嗎?\n\n"); printf("1. 挑戰其它遊戲 "); printf(" 2. 繼續挑戰排序遊戲 "); printf(" 3. 檢視當前得分\n\n"); printf("回覆數字序號選擇一項吧:"); scanf_s("%d",&q); if (q == 2) { system("cls"); comparation(); } else if (q == 3) scorep(); else { system("cls"); menu(); } } else //退出排序遊戲? { system("color F0"); printf("\n\n想玩別的遊戲嗎?\n\n"); printf("1. 挑戰其它遊戲 "); printf(" 2. 繼續當前遊戲\n\n"); printf("回覆數字序號選擇一項吧:"); scanf_s("%d",&q); if (q == 2) comparation(); else { system("cls"); menu(); } } }
找最值遊戲
void maxmin() { int i,q,max,min,maxx,minn; int a[5]; srand(time(NULL)); system("color DF"); system("cls"); printf("\n\n\t* * * * * * * * * * * * * *\n"); //找最值遊戲規則 printf("\t* 遊 戲 規 則 *\n"); printf("\t* 開始遊戲後將會顯示隨機五個整數, *\n"); printf("\t* 請分別找出五個整數的最大值和最小 *\n"); printf("\t* 值,若回答正確即可獲得 2 分獎勵。 *\n"); printf("\t* 加油噢,你能行! *\n"); printf("\t* * * * * * * * * * * * * *\n"); printf("\t * * * \n"); printf("\t * * * \n"); printf("\t * * * * * \n"); printf("\t * * * \n"); printf("\t * \n"); printf("\t 輸入數字 0 即可開始遊戲:"); scanf_s("%d",&p); if (p == 0) { for (i = 0; i < 5; i++) a[i] = rand() % 10; printf("\n\n\t* * * * * * * * * * * * * *\n"); printf("\t* *\n"); printf("\t* "); for (i = 0; i < 5; i++) printf(" %d ",a[i]); printf(" *\n"); printf("\t* *\n"); printf("\t* * * * * * * * * * * * * *\n\n"); printf("接下來,請找出最大值:"); scanf_s("%d",&maxx); printf("\n然後請找出最小值:"); scanf_s("%d",&minn); max = a[4]; min = a[1]; for (i = 0; i < 5; i++) { if (a[i] > max) max = a[i]; if (a[i] < min) min = a[i]; } if (maxx == max && minn == min) //驗證回答正確與否 { system("color AF"); printf("\n哇你太棒啦!獲得獎勵 2 分!\n\n"); score(); score(); system("pause"); system("color 0F"); printf("\n\n想挑戰別的遊戲嗎?\n\n"); printf("1. 挑戰其它遊戲 "); printf(" 2. 繼續挑戰找最值遊戲 "); printf(" 3. 檢視當前得分\n\n"); printf("回覆數字序號選擇一項吧:"); scanf_s("%d",&q); if (q == 2) { system("cls"); maxmin(); } else if (q == 3) scorep(); else { system("cls"); menu(); } } else { system("color 4F"); printf("\n有點小可惜,再挑戰一次吧!\n\n"); system("pause"); maxmin(); } } else //退出找最值遊戲? { system("color F0"); printf("\n\n想玩別的遊戲嗎?\n\n"); printf("1. 挑戰其它遊戲 "); printf(" 2. 回到當前遊戲\n\n"); printf("回覆數字序號選擇一項吧:"); scanf_s("%d",&q); if (q == 2) maxmin(); else { system("cls"); menu(); } } }
小結
這個數字益智遊戲是我第一次編寫如此規模的 C 語言程式,知識水平有限,也花了不少時間去編寫這個程式,程式碼可能有不盡完美的地方,也沒有儘可能地簡化程式碼。最後,非常歡迎各路大神給我提出一些建議,大家一起學習,共同進步,奧力給!
以上所述是小編給大家介紹的C語言編一個數字益智小遊戲,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!