forked from emilytouchingcomputers/CTFium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.c
153 lines (127 loc) · 2.97 KB
/
source.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ncurses.h>
#include <wchar.h>
#include <locale.h>
#include <math.h>
#include <string.h>
#include "source.h"
char cursorX = 0, cursorY = 0;
char board[9] = {0};
logic_func DIFFICULTY = IMPOSSIBLE;
void moveAI()
{
int loc = -1;
int score = -2;
int i;
for (i = 0; i < 9; ++i)
{
if (board[i] == NONE)
{
board[i] = COMPUTER;
int tempScore = -(*DIFFICULTY)(PLAYER);
board[i] = NONE;
if (tempScore > score)
{
score = tempScore;
loc = i;
}
}
}
board[loc] = COMPUTER;
drawO(loc / 3, loc % 3);
}
void initialize()
{
setlocale(LC_ALL, "");
initscr();
keypad(stdscr, TRUE);
cbreak();
noecho();
curs_set(0);
start_color();
init_pair(1, COLOR_WHITE, COLOR_BLACK); // BORDER_PAIR
init_pair(2, COLOR_RED, COLOR_GREEN); // CURSOR_PAIR
init_pair(3, COLOR_CYAN, COLOR_BLACK); // O_PAIR
init_pair(4, COLOR_RED, COLOR_BLACK); // O_PAIR
cursorX = 0;
cursorY = 0;
memset(board, 0, 9);
clear();
drawBoard();
attron(CURSOR_PAIR);
mvaddch((cursorX * 10) + 5, (cursorX * 20) + 10, ' ');
attroff(CURSOR_PAIR);
}
int startGame()
{
bool paused = FALSE;
char ch, win;
while ((ch = getch()) != 'q' && ch != 'Q')
{
if (paused)
continue;
if (ch == 5 || ch == 'd' || ch == 'D') // right
drawCursor(1, 0);
if (ch == 4 || ch == 'a' || ch == 'd') // left
drawCursor(-1, 0);
if (ch == 3 || ch == 'w' || ch == 'W') // up
drawCursor(0, -1);
if (ch == 2 || ch == 's' || ch == 'S') // down
drawCursor(0, 1);
if (ch == ' ')
{
if (board[cursorX * 3 + cursorY])
continue;
board[cursorX * 3 + cursorY] = PLAYER;
drawX(cursorX, cursorY);
moveAI();
char i = 0;
// check for draw
for (i = 0; i < 9; i++)
if (!board[i])
break;
if ((win = checkWin()) || i == 9)
paused = TRUE;
}
}
endwin();
return win;
}
void main(void)
{
char option = 0, message[100], name[25];
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
puts("Please enter your name: ");
scanf("%24s", name);
getchar();
snprintf(message, 100, "Welcome %s!\n", name);
printf(message);
printf("Press ENTER to begin.\n");
getchar();
do
{
initialize();
switch (startGame())
{
case PLAYER:
{ // player won
FILE *flag = fopen("flag", "r");
fgets(message, 40, flag);
puts(message);
break;
}
case COMPUTER: // player lost
printf("YOU LOST\n");
break;
default:
printf("DRAW\n");
}
puts("Do you want to play again? [y/n] ");
scanf("%c", &option);
getchar();
} while (option == 'y' || option == 'Y');
puts("Goodbye!");
}