-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
183 lines (167 loc) · 3.29 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#include <stdbool.h>
#define ROCK "moves/rock.txt"
#define PAPER "moves/paper.txt"
#define SCISSOR "moves/scissor.txt"
#define TARGET_SCORE 10
#define PLAY_AGAIN '0'
#define BUFFER_SIZE 300
int player = 0, cpu = 0; // scores
char moveset[6][BUFFER_SIZE]; // ASCII arts
typedef enum color
{
RED = 12, GREEN = 10, YELLOW = 14
}color;
static inline void reset()
{
player = 0;
cpu = 0;
}
static inline void gotoxy( short x, short y)
{
COORD pt = { x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pt);
}
static inline void colorf( const char* msg, color hue)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), hue);
printf("%s", msg);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
void readmoveset(char** files)
{
FILE* f;
for( int i = 0; i < 3; i++)
{
fopen_s( &f, files[i], "r");
fscanf_s( f, "%[^X]", moveset[2*i], sizeof(moveset[i]));
fscanf_s( f, "%[^NULL]", moveset[(2*i)+1], sizeof(moveset[i]));
moveset[(2*i)+1][0] = ' ';
}
fclose(f);
}
static inline void update_score()
{
gotoxy( 4, 0);
printf("SCORES");
gotoxy( 2, 2);
printf("PLAYER | CPU");
gotoxy( 2, 3);
printf("%6d | %3d", player, cpu);
}
bool xwins( int x, int y) // win condition for p1 (x)
{
short eval = y+1;
if(eval == 3) eval = 0;
if(eval == x)
return true;
return false;
}
void display_result( int pmove, int cmove)
{
if(xwins( pmove, cmove))
{
colorf("\n\n\n\t\t YOU WIN!", GREEN);
player++;
}
else if(xwins( cmove, pmove))
{
colorf( "\n\n\n\t\t YOU LOSE!", RED);
cpu++;
}
else
colorf( "\n\n\n\t\t DRAW..", YELLOW);
}
void draw(int move) // utility fn for identifying moves
{
static int turn = 0;
switch(move)
{
case 0 :
printf("%s\n", moveset[(turn%2 != 0)?0:1]);
break;
case 1 :
printf("%s\n", moveset[(turn%2 != 0)?2:3]);
break;
case 2 :
printf("%s\n", moveset[(turn%2 != 0)?4:5]);
break;
}
turn++;
}
short rng() // random number generation [CPU's input]
{
int move = rand();
move = move%2?move*2:move/3;
return move%3;
}
int input() // input and validation [Player's input]
{
int move;
printf("MAKE YOUR MOVE :");
printf("\n\n%64s 1 : ROCK\n%64s 2 : PAPER\n%64s 3 : SCISSOR", "PRESS", "PRESS", "PRESS");
while(1)
{
move = _getch() - 49;
if(move <= 2 && move >= 0)
break;
else printf("\a");
}
return move;
}
void cycle() // user input + rng() = one cycle
{
gotoxy( 25, 3);
printf("ROCK.. PAPER.. SCISSOR!!");
gotoxy( 60, 5);
int cmove = rng();
int pmove = input();
gotoxy( 40, 5);
draw(pmove);
printf("\n%22s\n", "VS");
draw(cmove);
display_result( pmove, cmove);
}
char game_over()
{
if(player == TARGET_SCORE)
printf("WOO HOO! YOU WON!!");
else
printf("YOU LOST.. :[");
gotoxy( 55, 17);
_getch();
printf("Press 0 to play again! ^_^");
return _getch();
}
int main() // driver code
{
char* files[] = { ROCK, PAPER, SCISSOR};
srand(time(NULL));
readmoveset(files);
while(1)
{
update_score();
cycle();
if((cpu == TARGET_SCORE || player == TARGET_SCORE))
{
update_score();
gotoxy( 60, 15);
Sleep(100);
if(game_over() == PLAY_AGAIN) reset();
else break;
}
else
{
gotoxy( 60, 14);
system("pause");
}
system("cls");
}
system("cls");
printf("\n\n\tSEE YA!!\n");
return 0;
}