學了一段時間,心血來潮寫了一個1A2B小遊戲,很多人應該玩過,是一個挺有意思的益智小遊戲,之前用易語言寫過,現在又用C++重寫了一下。
編譯運作無錯,整體程式設計思路為:進入循環,初始化遊戲,讀入一個數,判斷是否合法,判斷是否符合規則,判斷是否正确,再給出答案提示。各部分都用函數封轉友善管理和維護。不過有一點确實還需要改進,就是在輸入輸出語句的使用上,顯得有些許混亂,用一個單獨的函數來二次封裝也許會更好,這樣也能友善控制程式在任何時候都能退出遊戲和做出最外層的響應。
1A2B遊戲規則介紹:
你和對手分别標明一個四位數,各位數字不要重複。
遊戲開始後,由雙方分别猜對方所標明的四位數,猜測的結果将會列在自己的猜測曆史清單,并以A和B來表示結果。
A代表猜測的數字中,數字相同且位置也正确的個數。
B代表猜測的數字中,數字相同但位置不一樣的個數。
舉例來說,如果對方的數字為1234,且你猜的數字為5283,其中2被猜到且位置正确,3也被猜到但位置不對,是以結果會出現1A1B。
比賽由先完整猜出對方數字的人獲得勝利(也就是先得到4A的玩家)。
代碼如下:
1 // name:1A2B.cpp
2 // author:Frank
3 // descraption: 1A2B Game, in the beginning, the program will generate a random four-digits number
4 // in which the digits is not equal to each other, you need to guess it, and as you
5 // enter your answer, there would be a tips likes: xAyB. x means the count of right
6 // numbers those are in the right sites, y means the count of right numbers those
7 // are not in the right sites. 4A0B means you have got the right number.
8 #include <iostream>
9 #include <stdlib.h>
10 #include <time.h>
11 #include <string.h>
12 #include <stdbool.h>
13
14 void InitializeGame(void);
15 void GetInput(char * input);
16 bool CheckAnswer(char * input);
17 bool GiveTips(char * input);
18 void GetRandom(char * random);
19 using namespace std;
20
21 char answer[5] = "";
22 char input[10] = "";
23 int times = 0;
24
25 int main(int argc, char** argv) {
26 char c;
27 while (true){
28 cout << "Enter 'S' to start the game, 'Q' to quit." << endl;
29 c = toupper(getchar());
30 while(getchar() != '\n');
31 if (c == 'Q')
32 break;
33 else if(c == 'S'){
34 cout << "Game Start! Enter your answer:" << endl;
35 times = 0;
36 InitializeGame();//初始化遊戲
37 // cout << "The answer is: " << answer << endl;
38 GetInput(input); //輸入猜測值
39 //檢查猜測是否正确 不正确則給出提示
40 while(GiveTips(input) == false){
41 times++;
42 GetInput(input);
43 }
44 times++;
45 cout << "Congratulations! You have got it after " << times << " times." << endl;
46 }else
47 cout << "Only 'S' and 'Q' are received." << endl;
48 }
49
50 return 0;
51 }
52
53 /******************************************************************************
54 *函數名稱:void InitializeGame(void)
55 *函數功能:初始化遊戲,生成随機數
56 *入口參數:無
57 *返 回 值:無
58 *******************************************************************************/
59 void InitializeGame(void){
60 static bool init_rand = false;
61 if (init_rand == false){
62 srand ((unsigned) time(NULL)); //如果未初始化則初始化随機數種子
63 init_rand = true;
64 }
65 GetRandom(answer);//生成随機數
66 // cout << answer << endl;
67 }
68
69 /******************************************************************************
70 *函數名稱:void GetInput(char * input)
71 *函數功能:讀取一個字元串
72 *入口參數:傳回讀取的字元串
73 *返 回 值:無
74 *******************************************************************************/
75 void GetInput(char * input){
76 gets(input);
77 while(true){
78 if(strlen(input) != 4){
79 cout << "Please input a 4-digits number!" << endl;
80 gets(input);
81 continue;
82 }
83 if(CheckAnswer(input) == false){
84 cout << "There couldn't be two same character in your answer!" << endl;
85 gets(input);//不合法則重新輸入
86 continue;
87 }
88 break;
89 }
90 }
91
92 /******************************************************************************
93 *函數名稱:bool checkanswer(char * input)
94 *函數功能:判斷答案是否合法,即是否存在重複數字
95 *入口參數:input為待判斷的答案
96 *返 回 值:正确則傳回真,否則傳回假
97 *******************************************************************************/
98 bool CheckAnswer(char * input){
99 char temp[5];
100 strcpy (temp, input);
101 for(int i = 0; i < 4; i++){
102 for(int j = i + 1; j < 4; j++)
103 if(temp[i] == input[j])
104 return false;
105 }
106 return true;
107 }
108
109 /******************************************************************************
110 *函數名稱:void GiveTips(char * input)
111 *函數功能:根據輸入的答案來給出提示
112 *入口參數:待判斷的答案
113 *返 回 值:無
114 *******************************************************************************/
115 bool GiveTips(char * input){
116 // cout << "I'm checking." << endl;
117 int a = 0, b = 0;
118 for(int i = 0; i < 4; i++){
119 for(int j = 0; j < 4; j++){
120 // cout << "i:" << i << "j:" << j << endl;
121 if (input[i] == answer[j]){
122 if(i == j)
123 a++;
124 else
125 b++;
126 continue;
127 }
128 }
129 }
130 cout << "Tips:" << a << "A" << b << "B\n" << endl;
131 if (a == 4)
132 return true;
133 cout << "Enter another answer:";
134 return false;
135 }
136
137 /******************************************************************************
138 *函數名稱:void GetRandom(char * random)
139 *函數功能:産生一個各位數不相等的四位随機數
140 *入口參數:random為傳回的随機數
141 *返 回 值:無
142 *備 注:先生成一個0-9的整數數組,再随機從中取四個數,每取一個将該位置為-1
143 *******************************************************************************/
144 void GetRandom(char * random){
145 int i, j[10], k;
146 for (i = 0; i < 10; i++){
147 j[i] = i;
148 }
149 for(i = 0; i < 4; i++){
150 //生成第i個随機數
151 k = (int)rand() % 10;//k為下标
152 while (j[k] == -1){
153 k = (k + 1) % 10;
154 }
155 random[i] = '0' + j[k];
156 j[k] = -1;
157 }
158 }
代碼格式相對工整,每個函數都比較簡短,便于閱讀和了解。當然,如果有更好的建議,還望不啬賜教。
真正重要的東西,用眼睛是看不見的。