本文實例為大家分享了C#實現簡易猜數字游戲的具體代碼,供大家參考,具體內容如下
游戲規則說明:
由系統生成一個隨機數,玩家有三次猜數字的機會,如果在三次內猜出數字反饋玩家猜對了,否則Game Over!
代碼設計說明:
1.首先設計一個簡易的歡迎界面,并提示玩家是否開始游戲;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
public void Rule() { string symbol; bool flag = false ; Console.WriteLine( "***********************************************" ); Console.WriteLine( "------------歡迎進入猜數字游戲!-------------" ); Console.WriteLine( "我們將從1到10間隨機生成一個數字由玩家進行猜測!" ); Console.WriteLine( " 來看看你們的運氣,提供你們三次猜測機會!" ); Console.WriteLine( "\tY--開始游戲\t\tN--退出游戲" ); Console.WriteLine( "***********************************************" ); Console.Write( "是否進入游戲:" ); symbol = Convert.ToString(Console.ReadLine()); while (flag == false ) { switch (symbol) { case "Y" : Console.Clear(); Console.WriteLine( "游戲開始!" ); flag = true ; break ; case "N" : Console.WriteLine( "退出游戲!" ); Console.ReadKey(); Environment.Exit(0); break ; default : Console.WriteLine( "輸入無效符號!" ); Console.Write( "是否進入游戲:" ); symbol = Convert.ToString(Console.ReadLine()); break ; } } } |
2.由系統自動生成一個隨機數;
1
2
3
4
5
6
7
8
|
public int SetRandom() { int number; var random = new Random(); number = random.Next(1,10); // 使用該方法獲得的隨機數大于等于1,小于10; return number; } |
3.由玩家輸入一個數值與系統生成隨機數進行比較,判斷是否相同,如果相同則表示玩家猜對,否則繼續猜,直到三次機會使用完反饋Game Over!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
public void Guess( int num) { int number; int count = 0; Console.WriteLine( "請輸入猜測數值:" ); number = Convert.ToInt32(Console.ReadLine()); while (num <= 10) { if (number == num) { Console.WriteLine( "恭喜你,猜對了!" ); break ; } else { count++; // 獲取玩家輸入次數 Console.WriteLine( "親。猜錯了哦! 您以使用{0}次機會\n" , count); if (count >= 3) //判斷玩家是否已輸入三次 { Console.WriteLine( "您已用完猜測次數,Game Over!" ); break ; } Console.WriteLine( "請再次輸入猜測數值:" ); number = Convert.ToInt32(Console.ReadLine()); } } } |
完整代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
using System; namespace GuessNumberApplication { class Program { static void Main( string [] args) { var guessNumber = new GuessNumber(); var ruleExplain = new RuleExplain(); ruleExplain.Rule(); int number = guessNumber.SetRandom(); guessNumber.Guess(number); Console.ReadKey(); } } class RuleExplain { public void Rule() { string symbol; bool flag = false ; Console.WriteLine( "***********************************************" ); Console.WriteLine( "------------歡迎進入猜數字游戲!-------------" ); Console.WriteLine( "我們將從1到10間隨機生成一個數字由玩家進行猜測!" ); Console.WriteLine( " 來看看你們的運氣,提供你們三次猜測機會!" ); Console.WriteLine( "\tY--開始游戲\t\tN--退出游戲" ); Console.WriteLine( "***********************************************" ); Console.Write( "是否進入游戲:" ); symbol = Convert.ToString(Console.ReadLine()); while (flag == false ) { switch (symbol) { case "Y" : Console.Clear(); Console.WriteLine( "游戲開始!" ); flag = true ; break ; case "N" : Console.WriteLine( "退出游戲!" ); Console.ReadKey(); Environment.Exit(0); break ; default : Console.WriteLine( "輸入無效符號!" ); Console.Write( "是否進入游戲:" ); symbol = Convert.ToString(Console.ReadLine()); break ; } } } } class GuessNumber { public int SetRandom() { int number; Random random = new Random(); number = random.Next(1, 10); // 使用該方法獲得的隨機數大于等于1,小于10; return number; } public void Guess( int num) { int number; int count = 0; Console.WriteLine( "請輸入猜測數值:" ); number = Convert.ToInt32(Console.ReadLine()); while (num <= 10) { if (number == num) { Console.WriteLine( "恭喜你,猜對了!" ); break ; } else { count++; // 獲取玩家輸入次數 Console.WriteLine( "親。猜錯了哦! 您以使用{0}次機會\n" , count); if (count >= 3) //判斷玩家是否已輸入三次 { Console.WriteLine( "您已用完猜測次數,Game Over!" ); break ; } Console.WriteLine( "請再次輸入猜測數值:" ); number = Convert.ToInt32(Console.ReadLine()); } } } } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/drift-code/archive/2018/04/13/8821582.html