본문 바로가기

C#/Project

2020-05-04 경마배팅게임.

하고있는 2048게임 코딩은 더이상 진척이 보이지 않아, 계속 고민할바엔 여태배운 내용을 응용해서 다른걸 만들어 보자 생각해서, 경마배팅게임을 만들어봤다.

혼자 게임을 처음부터 끝까지 생각해서 디테일한 예외처리를 다해주고, 완성을 시켜본적은 처음이라 감회가 새로웠다.

 

말이달리는 것을 구현하기 위해, 프로그램을 딜레이 시켜주는 기능이 필요해서 처음에는 Sleep 함수를 사용하려고 했으나, 검색해보니 [System.Threading.Thread.Sleep을 이용해 간단하게 지연이 가능하나, 프로그램 자체를 완전 멈춰버린다.] 라는 얘기가 많아서 안정성을 위해 http://www.bluene.net/blog/550 의 게시물을 참고하여, C# Delay 함수를 사용했다. 

 

-----------------------------------------------------------------------------------------------------------------------------------

App.Class

더보기
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;
 
namespace Repeat_019
{
    class App
    {
        const string LINE = "**************************************************************";
        const int LINELANGTH = 45;
        const int HORSECOUNT = 4;
        public int minbat = 500;
        public int maxbat = 1000;
 
        Random rnd1;
        Random rnd2;
        Random rnd3;
        Dictionary<intstring> dicHorse;
        List<int> horseTodayBatList;
        List<string> horseNameList;
 
        Dictionary<int, UserInfo> dicUserInfo;
        UserInfo userInfo;
 
        public App()
        {
            this.Init();
 
            while (true)
            {
                this.PlayGame();
 
                if (this.Exit() == true)
                {
                    break;
                }
                else if(this.Exit() == false)
                {
                    if (this.userInfo.gold <= 0)
                    {
                        Console.WriteLine("==============GAME OVER==============");
                        break;
                    }
                    else
                    {
                        Console.ReadLine();
                        Console.Clear();
                        this.SaveToLocal();
                        continue;
                    }
                }
            }
        }
        public void Init()
        {
            this.rnd1 = new Random();
            this.rnd2 = new Random();
            this.rnd3 = new Random();
            this.dicHorse = new Dictionary<intstring>();
            this.horseNameList = new List<string>();
            
            this.horseTodayBatList = new List<int>();
            this.dicUserInfo = new Dictionary<int, UserInfo>();
 
            for (int i = 0; i < HORSECOUNT; i++)
            {
                this.dicHorse.Add(i, string.Format("[{0}번말]", i + 1));
            }
            this.LoadData();
        }
 
        public void PlayGame()
        {
 
            List<int> horsePositionList = new List<int>();
 
            string selecthorsename = null;
            bool isStrExist = false;
            string selectHorse;
            int batamount;
 
            foreach (var pair in this.dicHorse)
            {
                horsePositionList.Add(pair.Key);
                this.horseNameList.Add(pair.Value);
            }
 
            Console.WriteLine("=============경마게임==============");
 
            this.PrintTodayBat();
 
            this.userInfo.PrintMyGold();
 
            while (true)
            {
                Console.Write("배팅하실 말의 번호를 입력해주세요. [ex) 1,2,3 ...] :");
                selectHorse = Console.ReadLine();
 
                int selectHorseNum = int.Parse(selectHorse);
                selectHorseNum -= 1;
                if (this.dicHorse.ContainsKey(selectHorseNum))
                {
                    break;
                }
                else
                {
                    Console.WriteLine("선택하신 번호는 없습니다.");
                    continue;
                }
 
            }
 
 
            foreach (var element in this.horseNameList)
            {
                isStrExist = element.Contains(selectHorse);
 
                if (isStrExist == true)
                {
                    selecthorsename = element;
                }
            }
 
            while (true)
            {
                Console.Write("얼마나 거시겠습니까? [{0}~{1}]"this.minbat, this.maxbat);
                batamount = int.Parse(Console.ReadLine());
 
                if (batamount >= this.minbat && batamount <= this.maxbat && batamount <= this.userInfo.gold)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("배팅액을 잘못 입력하셨습니다.");
                    continue;
                }
            }
 
 
            while (true)
            {
                int horseNum = rnd2.Next(05);
                int horsespeed = rnd3.Next(05);
 
                Delay(200);
                Console.Clear();
 
                switch (horseNum)
                {
                    case 1:
                        horsePositionList[0+= horsespeed;
                        break;
                    case 2:
                        horsePositionList[1+= horsespeed;
                        break;
                    case 3:
                        horsePositionList[2+= horsespeed;
                        break;
                    case 4:
                        horsePositionList[3+= horsespeed;
                        break;
                }
 
 
                Console.WriteLine(LINE);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("{0}", selecthorsename);
                Console.ResetColor();
                Console.WriteLine("을 선택하셨습니다.");
 
                Console.WriteLine(LINE);
 
                for (int i = 0; i < horsePositionList[0]; i++)
                {
                    Console.Write(" ");
                }
                if (selecthorsename == this.horseNameList[0])
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(this.horseNameList[0]);
                    Console.ResetColor();
                }
                else
                {
                    Console.WriteLine(this.horseNameList[0]);
                }
 
 
                for (int i = 0; i < horsePositionList[1]; i++)
                {
                    Console.Write(" ");
                }
                if (selecthorsename == this.horseNameList[1])
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(this.horseNameList[1]);
                    Console.ResetColor();
                }
                else
                {
                    Console.WriteLine(this.horseNameList[1]);
                }
 
 
                for (int i = 0; i < horsePositionList[2]; i++)
                {
                    Console.Write(" ");
                }
                if (selecthorsename == this.horseNameList[2])
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(this.horseNameList[2]);
                    Console.ResetColor();
                }
                else
                {
                    Console.WriteLine(this.horseNameList[2]);
                }
 
 
                for (int i = 0; i < horsePositionList[3]; i++)
                {
                    Console.Write(" ");
                }
                if (selecthorsename == this.horseNameList[3])
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(this.horseNameList[3]);
                    Console.ResetColor();
                }
                else
                {
                    Console.WriteLine(this.horseNameList[3]);
                }
 
                Console.WriteLine(LINE);
 
 
                if (horsePositionList[0>= LINELANGTH || horsePositionList[1>= LINELANGTH || horsePositionList[2>= LINELANGTH || horsePositionList[3>= LINELANGTH)
                {
                    if (horsePositionList[0>= LINELANGTH)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("**************{0}이 승리했습니다.****************"this.horseNameList[0]);
                        Console.ResetColor();
 
                        if (selecthorsename == this.horseNameList[0])
                        {
                            Console.WriteLine("★★★★선택하신 {0}이 승리하여 {1}원을 받았습니다.★★★★", selecthorsename, this.Reward(selecthorsename, batamount));
                            this.userInfo.gold += (int)this.Reward(selecthorsename, batamount);
                        }
                        else
                        {
                            Console.WriteLine("ㅠㅠㅠㅠ선택하신 {0}이 패배하여 배팅금 {1}원이 모조리 날아갔습니다.ㅠㅠㅠㅠ", selecthorsename, batamount);
                            this.userInfo.gold -= batamount;
                        }
 
                        Console.WriteLine("다시 하시려면 엔터를 입력하세요.");
                        break;
                    }
 
 
                    if (horsePositionList[1>= LINELANGTH)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("**************{0}이 승리했습니다.****************"this.horseNameList[1]);
                        Console.ResetColor();
 
                        if (selecthorsename == this.horseNameList[1])
                        {
                            Console.WriteLine("★★★★선택하신 {0}이 승리하여 {1}원을 받았습니다.★★★★", selecthorsename, this.Reward(selecthorsename, batamount));
                            this.userInfo.gold += (int)this.Reward(selecthorsename, batamount);
                        }
                        else
                        {
                            Console.WriteLine("ㅠㅠㅠㅠ선택하신 {0}이 패배하여 배팅금 {1}원이 모조리 날아갔습니다.ㅠㅠㅠㅠ", selecthorsename, batamount);
                            this.userInfo.gold -= batamount;
                        }
 
                        Console.WriteLine("다시 하시려면 엔터를 입력하세요.");
                        break;
                    }
 
 
                    if (horsePositionList[2>= LINELANGTH)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("**************{0}이 승리했습니다.****************"this.horseNameList[2]);
                        Console.ResetColor();
 
                        if (selecthorsename == this.horseNameList[2])
                        {
                            Console.WriteLine("★★★★선택하신 {0}이 승리하여 {1}원을 받았습니다.★★★★", selecthorsename, this.Reward(selecthorsename, batamount));
                            this.userInfo.gold += (int)this.Reward(selecthorsename, batamount);
                        }
                        else
                        {
                            Console.WriteLine("ㅠㅠㅠㅠ선택하신 {0}이 패배하여 배팅금 {1}원이 모조리 날아갔습니다.ㅠㅠㅠㅠ", selecthorsename, batamount);
                            this.userInfo.gold -= batamount;
                        }
 
                        Console.WriteLine("다시 하시려면 엔터를 입력하세요.");
                        break;
                    }
 
 
                    if (horsePositionList[3>= LINELANGTH)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("**************{0}이 승리했습니다.****************"this.horseNameList[3]);
                        Console.ResetColor();
 
                        if (selecthorsename == this.horseNameList[3])
                        {
                            Console.WriteLine("★★★★선택하신 {0}이 승리하여 {1}원을 받았습니다.★★★★", selecthorsename, this.Reward(selecthorsename, batamount));
                            this.userInfo.gold += (int)this.Reward(selecthorsename, batamount);
                        }
                        else
                        {
                            Console.WriteLine("ㅠㅠㅠㅠ선택하신 {0}이 패배하여 배팅금 {1}원이 모조리 날아갔습니다.ㅠㅠㅠㅠ", selecthorsename, batamount);
                            this.userInfo.gold -= batamount;
                        }
 
                        Console.WriteLine("다시 하시려면 엔터를 입력하세요.");
                        break;
                    }
                }
 
                
            }
 
        }
 
        public void PrintTodayBat()
        {
            int inx = 0;
 
            if (this.horseTodayBatList.Count != 4)
            {
                for (int i = 0; i < HORSECOUNT; i++)
                {
                    int todayHorseBat = rnd1.Next(100500);
                    this.horseTodayBatList.Add(todayHorseBat);
                }
            }
 
            foreach (var element in this.horseTodayBatList)
            {
                Console.Write("오늘 ");
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.Write("{0}"this.horseNameList[inx++]);
                Console.ResetColor();
 
                Console.Write("의 배당은 ");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write((double)element / 100);
                Console.ResetColor();
 
                Console.WriteLine(" 입니다.");
            }
        }
 
        private double Reward(string selecthorsename, int batamount)
        {
            bool isCorrect = false;
            int findinx = 0;
            double reward = 0;
 
            foreach (var element in this.horseNameList)
            {
                isCorrect = element.Contains(selecthorsename);
 
                if (isCorrect == true)
                    findinx = this.horseNameList.IndexOf(element);
            }
 
            reward = batamount * this.horseTodayBatList[findinx];
            return reward;
        }
 
        public void SaveToLocal()
        {
            File.WriteAllText("./save_data.json",JsonConvert.SerializeObject(this.dicUserInfo));
        }
 
        public void LoadData()
        {
            if (File.Exists("./save_data.json"== true)
            {
                this.dicUserInfo = JsonConvert.DeserializeObject<Dictionary<int, UserInfo>>(File.ReadAllText("./save_data.json"));
 
                foreach (var pair in this.dicUserInfo)
                {
                    this.userInfo = pair.Value;
                }
            }
            else
            {
 
                this.userInfo = new UserInfo(1000"홍길동"1000);
                this.dicUserInfo.Add(this.userInfo.id, this.userInfo);
 
                this.SaveToLocal();
            }
        }
 
        public bool Exit()
        {
            bool isAchieve = false;
            if (this.userInfo.gold >= 50000000)
            {
                Console.WriteLine("달성목표를 달성하여 게임을 종료 합니다.");
                isAchieve = true;
            }
            return isAchieve;
        }
 
        private static DateTime Delay(int MS)
        {
            DateTime ThisMoment = DateTime.Now;
            TimeSpan duration = new TimeSpan(0000, MS);
            DateTime AfterWards = ThisMoment.Add(duration);
 
            while (AfterWards >= ThisMoment)
            {
                ThisMoment = DateTime.Now;
            }
 
            return DateTime.Now;
        }
    }
}
 
 
 
 
 

UserInfo.Class

더보기
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
 
namespace Repeat_019
{
    class UserInfo
    {
        public int id;
        public string name;
        public int gold;
 
        public UserInfo(int id, string name, int gold)
        {
            this.id = id;
            this.name = name;
            this.gold = gold;
        }
 
        public void PrintMyGold()
        {
            Console.WriteLine("-------------------------------------------");
            Console.WriteLine("현재 {0}님이 소지하고 있는 금액은 {1}입니다."this.name, this.gold);
            Console.WriteLine("-------------------------------------------");
        }
    }
}
 

출력값