-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberBaseball.cpp
More file actions
595 lines (519 loc) · 16.4 KB
/
NumberBaseball.cpp
File metadata and controls
595 lines (519 loc) · 16.4 KB
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#include <iostream>
#include <string> // using String for input
#include <random> // for getting random digit
#include <fstream> // for saving records
#include <WinSock2.h> // for socket networking
// Configuration Properties => Linker => Input => Additional dependencies -> add 'ws2_32.lib'
// 프로젝트 속성 -> 링커-> 입력 -> 추가종속성에 ws2_32.lib
#pragma warning(disable:4996) // for ignore LinkWanring 'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS
using namespace std;
#define SAVEPATH "record.txt"
struct checkC {
checkC(int b, int s) : ball(b), strike(s) {}
int ball;
int strike;
int getBall() {
return ball;
}
int getStrike() {
return strike;
}
};
checkC compare(int answ[], int guess[], int GameDigits) {
checkC check(0, 0);
for (int i = 0; i < GameDigits; i++) {
for (int j = 0; j < GameDigits; j++) {
if (answ[i] == guess[j]) { // Find Same Digit
if (i == j) // If same Position strike++ else, ball++
check.strike++;
else
check.ball++;
}
}
}
return check;
}
string getNumber(int GameDigits){ // Get only N-digits integer(by string) and return
bool ok = false;
string result;
while (!ok){
cout << "중복없는 " << GameDigits << "자리 숫자를 입력해주세요 : "; // "please input (GameDigits)-digit integer without repetition"
cin >> result;
if (result.length() == GameDigits)
{
bool allDigits = true;
for (unsigned index = 0; index < GameDigits; ++index)
{
allDigits = allDigits && (
(result[index] >= '0') &&
(result[index] <= '9')
);
}
ok = allDigits;
}
}
return result;
}
bool IsRepetition(int arr[], int GameDigits) { // check repetition of integer array
for (int i = 0; i < GameDigits; i++) {
for (int j = i + 1; j < GameDigits; j++) {
if (arr[i] == arr[j]) {
return true;
}
}
}
return false;
}
void makeProfile() {
ofstream os;
string ID;
cout << " Input your new ID" << endl;
cout << " '0' for cancel " << endl;
cin >> ID;
if (ID == "0") {
return;
}
else if (ID.length() > 20) {
cout << " System : Id too long(plz under 20)" << endl;
}
os.open(SAVEPATH, 'w');
os << ID << endl;
for (int i = 0; i < 8; i++) {
os << 0 << endl;
}
os.close();
}
void showRecord() {
ifstream is;
is.open(SAVEPATH);
if (!is.is_open()) {
cout << "record file Not found" << endl;
return;
}
ofstream os;
int data[8];
string ID;
int length = ID.length();
is >> ID; // ID
for (int i = 0; i < 8; i++) {
is >> data[i];
}
cout << " ----------------Record------------------" << endl;
cout << " ID : " << ID << endl;
cout << " number of Triple : " << data[0] << " times" << endl;
cout << " best score : " << data[1] << endl;
cout << " number of Qudaruple : " << data[2] << " times" << endl;
cout << " best score : " << data[3] << endl;
cout << " Multi Play(Total/W/L/D) : " << data[4] << "/" << data[5] << "/" << data[6] << "/" << data[7] << endl;
is.close();
}
void saveRecord(int GameDigits, int checkCount) { // gamedigit => 0 net win, 1 net lose, 2 net draw, 3 single trip, 4 single quad
ifstream is;
is.open(SAVEPATH);
if (!is.is_open()) {
cout << "profile not found" << endl;
return;
}
ofstream os;
int data[8];
string ID;
is >> ID;
for (int i = 0; i < 8; i++) {
is >> data[i];
}
if (GameDigits == 0) {
data[4]++;
data[5]++;
}
else if (GameDigits == 1) {
data[4]++;
data[6]++;
}
else if (GameDigits == 2) {
data[4]++;
data[7]++;
}
else if (GameDigits == 3) {
data[0]++;
if (data[1] < 101 - checkCount)
data[1] = 101 - checkCount;
}
else if (GameDigits == 4) {
data[2]++;
if (data[3] < 101 - checkCount)
data[3] = 101 - checkCount;
}
is.close();
os.open(SAVEPATH);
os << ID << endl;
for (int i = 0; i < 8; i++) {
os << data[i] << endl;
}
os.close();
cout << " record saved " << endl;
}
void makeRandNumber(int * target, int GameDigits) {
random_device rd; // only used once to initialise (seed) engine
mt19937 rng(rd()); // random-number engine used (Mersenne-Twister in this case)
uniform_int_distribution<int> uni(0, 9); // guaranteed unbiased
auto random_integer = uni(rng);
while (IsRepetition(target, GameDigits)) { // number must be non-Repeating digits for NumberBaseballGame
for (int i = 0; i < GameDigits; i++) {
target[i] = uni(rng);
}
}
}
void runGame(int GameDigits) {
int answ[10] = { 0, };
makeRandNumber(answ, GameDigits);
int checkCount = 0;
cout << "**************************************" << endl;
cout << " Play Ball " << endl;
cout << "**************************************" << endl;
for (int i = 0; i < GameDigits; i++) {
cout << answ[i];
}cout << endl;
while (1) { // loop until strike == (GameDigits)
string input;
int output[10];
do {
input = getNumber(GameDigits); // Get string, If not (GameDigits)-digit integer, wait input again
for (int i = 0; i < GameDigits; i++) {
output[i] = input[i] - '0';
}
} while (IsRepetition(output, GameDigits)); // there should be no repetition
checkC compareResult = compare(answ, output, GameDigits);
cout << ++checkCount << "▼: " << input << " (Strike :" << compareResult.getStrike() << " Ball :" << compareResult.getBall() << ")" << endl;
if (compareResult.getStrike() == GameDigits) // game end
break;
}
cout << "**************************************" << endl;
cout << "* Correct!! *" << endl;
cout << "* Score : " << 101 - checkCount << " *" << endl;
cout << "**************************************" << endl << endl;
saveRecord(GameDigits, checkCount);
}
int netWorkServer() {
SOCKET sock, clientsock;
WSADATA wsa;
struct sockaddr_in sockinfo, clientinfo;
int clientsize;
int strlen;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
cout << "network init fail " << endl;
return 1;
}
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
cout << "socket init fail " << endl;
return 1;
}
memset(&sockinfo, 0, sizeof(sockinfo));
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(1234);
sockinfo.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (SOCKADDR*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR) {
cout << "bind fail " << endl;
return 1;
}
if (listen(sock, 5) == SOCKET_ERROR) {
cout << "listen fail" << endl;
return 1;
}
clientsize = sizeof(clientinfo);
cout << "waiting client... " << endl;
clientsock = accept(sock, (SOCKADDR*)&clientinfo, &clientsize);
if (clientsock == INVALID_SOCKET) {
cout << "Invalid client socket " << endl;
return 1;
}
// get ID
ifstream is;
char myID[20];
char enemyID[20];
is.open(SAVEPATH);
if (!is.is_open()) {
cout << "profile not found" << endl;
strcpy(myID, "Guest");
}
else {
is >> myID;
is.close();
}
send(clientsock, myID, sizeof(myID), 0);
strlen = recv(clientsock, enemyID, sizeof(enemyID), 0);
// Start Game //
int GameDigits = 4; // network game is Quadruple
string input; // input from std input
int output[10]; // output parsed from input
char gameData[8]; // data for socket send message
// index : [0~3] player's guess, [4]th try, [5] strike, [6] ball, [7]gameStatus
int checkCount = 0; // try
int answ[10] = { 0, };
makeRandNumber(answ, GameDigits);
cout << "******************************************l***********************************" << endl;
cout << " " << myID << " " << enemyID << " " << endl;
cout << "******************************************l***********************************" << endl;
while (1) {
if (clientsock == INVALID_SOCKET) { // check network status
cout << "client network failed " << endl;
closesocket(sock);
closesocket(clientsock);
WSACleanup();
break;
}
do {
input = getNumber(GameDigits);
for (int i = 0; i < GameDigits; i++) {
output[i] = input[i] - '0';
}
} while (IsRepetition(output, GameDigits));
checkC compareResult = compare(answ, output, GameDigits);
cout << ++checkCount << "▲: " << input << " (Strike :" << compareResult.getStrike() << " Ball :" << compareResult.getBall() << ")" << endl;
if (compareResult.getStrike() == GameDigits)
gameData[7] = '1'; // gameStatus '1' for "Client win"
for (int i = 0; i < 4; i++) {
gameData[i] = output[i] + '0';
}
gameData[4] = checkCount + '0';
gameData[5] = compareResult.getStrike() + '0';
gameData[6] = compareResult.getBall() + '0';
send(clientsock, gameData, sizeof(gameData), 0);
strlen = recv(clientsock, gameData, sizeof(gameData), 0);
if (strlen == -1) {
cout << " recieve failed" << endl;
closesocket(sock);
closesocket(clientsock);
WSACleanup();
return 1;
}
cout << " "
<< int(gameData[4] - '0') << "▼: " << gameData[0] << gameData[1] << gameData[2] << gameData[3] << " (Strike :" << gameData[5] << " Ball :" << gameData[6] << ")" << endl;
if (gameData[7] == '1') {
cout << "**************************************" << endl;
cout << "* Win *" << endl;
cout << "**************************************" << endl << endl;
saveRecord(0, 0);
break;
}
else if (gameData[7] == '2') {
cout << "**************************************" << endl;
cout << "* Lose *" << endl;
cout << "**************************************" << endl << endl;
saveRecord(1, 0);
break;
}
else if (gameData[7] == '3') {
cout << "**************************************" << endl;
cout << "* Draw *" << endl;
cout << "**************************************" << endl << endl;
saveRecord(2, 0);
break;
}
}
closesocket(sock);
closesocket(clientsock);
WSACleanup();
return 0;
}
int netWorkClient() {
SOCKET clientsock;
WSADATA wsa;
struct sockaddr_in sockinfo;
int strlen;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
cout << "init failed" << endl;
return 1;
}
clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientsock == INVALID_SOCKET) {
cout << "Invalid client socket" << endl;
return 1;
}
memset(&sockinfo, 0, sizeof(sockinfo));
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(1234); // using port "1234"
sockinfo.sin_addr.s_addr = inet_addr("127.0.0.1"); // set Server IP (127.0.0.1 for test)
if (connect(clientsock, (SOCKADDR*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR) {
cout << "connect failed" << endl;
return 1;
}
// get ID
ifstream is;
char myID[20];
char enemyID[20];
is.open(SAVEPATH);
if (!is.is_open()) {
cout << "profile not found" << endl;
strcpy(myID, "Guest");
}
else {
is >> myID;
is.close();
}
strlen = recv(clientsock, enemyID, sizeof(enemyID), 0);
send(clientsock, myID, sizeof(myID), 0);
// Start Game //
int GameDigits = 4;
int checkCount = 0;
string input;
int output[10];
char gameData[8];
int answ[4] = { 0,0,0,0 };
makeRandNumber(answ, GameDigits);
cout << "******************************************l***********************************" << endl;
cout << " " << myID << " " << enemyID << " " << endl;
cout << "******************************************l***********************************" << endl;
strlen = recv(clientsock, gameData, sizeof(gameData), 0);
if (strlen == -1) {
cout << " recieve failed" << endl;
return 1;
}
cout << " "
<< int(gameData[4] - '0') << "▲: " << gameData[0] << gameData[1] << gameData[2] << gameData[3] << " (Strike :" << gameData[5] << " Ball :" << gameData[6] << ")" << endl;
while (1) {
if (clientsock == INVALID_SOCKET) {
cout << "client network failed " << endl;
closesocket(clientsock);
WSACleanup();
return 1;
}
do {
input = getNumber(GameDigits);
for (int i = 0; i < GameDigits; i++) {
output[i] = input[i] - '0';
}
} while (IsRepetition(output, GameDigits));
checkC compareResult = compare(answ, output, GameDigits);
cout << ++checkCount << "▼: " << input << " (Strike :" << compareResult.getStrike() << " Ball :" << compareResult.getBall() << ")" << endl;
if (compareResult.getStrike() == GameDigits) { // Client end the Game
if (gameData[5] == '4') { // Server also ended the game -> Draw
for (int i = 0; i < 4; i++) {
gameData[i] = output[i] + '0';
}
gameData[4] = checkCount + '0';
gameData[5] = compareResult.getStrike() + '0';
gameData[6] = compareResult.getBall() + '0';
gameData[7] = '3'; // GameData[7] for transfer "draw" to Server
send(clientsock, gameData, sizeof(gameData), 0);
cout << "**************************************" << endl;
cout << "* Draw *" << endl;
cout << "**************************************" << endl << endl;
saveRecord(2, 0);
break;
}
else { // Server did not finished -> Client Win
for (int i = 0; i < 4; i++) {
gameData[i] = output[i] + '0';
}
gameData[4] = checkCount + '0';
gameData[5] = compareResult.getStrike() + '0';
gameData[6] = compareResult.getBall() + '0';
gameData[7] = '2'; // GameData[7] for transfer "Client Win" to Server
send(clientsock, gameData, sizeof(gameData), 0);
cout << "**************************************" << endl;
cout << "* Win *" << endl;
cout << "**************************************" << endl << endl;
saveRecord(0, 0);
break;
}
}
else if (gameData[5] == '4') { // Server ended Game but client yet -> "Client Lose"
for (int i = 0; i < 4; i++) {
gameData[i] = output[i] + '0';
}
gameData[4] = checkCount + '0';
gameData[5] = compareResult.getStrike() + '0';
gameData[6] = compareResult.getBall() + '0';
gameData[7] = '1'; // GameData[7] for transfer "Client Lose" to Server
send(clientsock, gameData, sizeof(gameData), 0);
cout << "**************************************" << endl;
cout << "* Lose *" << endl;
cout << "**************************************" << endl << endl;
saveRecord(1, 0);
break;
}
else { // no Situation
for (int i = 0; i < 4; i++) {
gameData[i] = output[i] + '0';
}
gameData[4] = checkCount + '0';
gameData[5] = compareResult.getStrike() + '0';
gameData[6] = compareResult.getBall() + '0';
gameData[7] = '0'; // GameData[7] for transfer "keep going on" to Server
send(clientsock, gameData, sizeof(gameData), 0);
strlen = recv(clientsock, gameData, sizeof(gameData), 0);
if (strlen == -1) {
cout << " recieve failed" << endl;
closesocket(clientsock);
WSACleanup();
return 1;
}
cout << " "
<< int(gameData[4]-'0') << "▲: " << gameData[0] << gameData[1] << gameData[2] << gameData[3] << " (Strike :" << gameData[5] << " Ball :" << gameData[6] << ")" << endl;
}
}
closesocket(clientsock);
WSACleanup();
return 0;
}
int main()
{
cout << " Number Baseball Game " << endl;
cout << " 0.2v" << endl << endl;
while (1) {
bool ok = false;
string gamemodeS;
cout << " ---------------------------------------" << endl;
cout << " 1. Single Play (Triple) " << endl;
cout << " 2. Single Play (Quadruple) " << endl;
cout << " 3. Make Multi Play Room (Server)" << endl;
cout << " 4. Find Multi Play Room (Client)" << endl;
cout << " 5. Make New Profile " << endl;
cout << " 6. View Record " << endl;
cout << " 0. exit game " << endl;
cout << " ---------------------------------------" << endl;
while (!ok)
{
cout << "select menu : ";
cin >> gamemodeS;
if (gamemodeS.length() == 1)
{
bool allDigits = true;
allDigits = allDigits && (
(gamemodeS[0] >= '0') &&
(gamemodeS[0] <= '6')
);
ok = allDigits;
}
}
int gamemode = stoi(gamemodeS);
if (gamemode == 1) {
cout << endl;
runGame(3);
}
if (gamemode == 2) {
cout << endl;
runGame(4);
}
else if (gamemode == 3) {
netWorkServer();
}
else if (gamemode == 4) {
netWorkClient();
}
else if (gamemode == 5) {
makeProfile();
}
else if (gamemode == 6) {
showRecord();
}
else if (gamemode == 0) {
cout << "**************************************" << endl;
cout << " Good Bye " << endl;
cout << "**************************************" << endl;
break;
}
}
return 0;
}