-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_bot.c
159 lines (136 loc) · 6.17 KB
/
basic_bot.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* basic_bot.c :+: :+: */
/* +:+ */
/* By: tbruinem <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/03/07 19:20:13 by tbruinem #+# #+# */
/* Updated: 2022/03/11 11:27:56 by tbruinem ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
// ### Initialization Input
// First line: `numberOfCells`
// Next `numberOfCells` lines: 7 space-separated integers:
// - `index` for the cell's index.
// - 6 `neigh` variables, one for each direction, containing the index of a neighboring cell or -1 if is there is no neighbor.
// Next line: An integer `yourColors`
// Next `yourColors` lines: 2 space-separated integers:
// - `index` for the color's index.
// - `maxAmount` for the max amount of pellets of that color.
// Next line: An integer `opponentColors`
// Next `opponentColors` lines: 2 space-separated integers:
// - `index` for the color's index.
// - `maxAmount` for the max amount of pellets of that color.
// ### Input for One Game Turn
// First line: An integer `direction` (between 0 and 5): the direction of gravity.
// Next line: `numberOfValidInsertSlots`
// next `numberOfValidInsertSlots` lines: 2 space-separated integers
// - `column`: the column to use for a PLACE command
// - `cellIndex`: the cell that correspond to the top of the column, according to the current gravity
// Next line: `numberOfNewPellets`
// next `numberOfNewPellets` lines: 4 space-separated integers:
// - `index`: index of the new pellet.
// - `cellIndex`: index of the cell the pellet is currently on.
// - `colorIndex`: index of the pellet's color.
// - `isMine`: 1 if you are the owner of this pellet, 0 otherwise.
// Next line: `numberOfChangedPellets`
// next `numberOfChangedPellets` lines: 2 space-separated integers:
// - `index`: index of the pellet.
// - `cellIndex`: index of the cell the pellet is currently on.
// Next line: `numberOfPelletsInHand`
// next `numberOfPelletsInHand` lines: 1 integer:
// - `colorIndex`: index of the pellet's color.
int main() {
// char buf[50 + 1];
int fd = open("bot.log", O_CREAT | O_WRONLY | O_TRUNC, 0644);
int numberOfCells;
scanf("%d\n", &numberOfCells);dprintf(fd, "numberOfCells %d\n", numberOfCells);
for (int i = 0; i < numberOfCells; i++) {
int index;
int n0,n1,n2,n3,n4,n5;
scanf("%d %d %d %d %d %d %d\n", &index, &n0, &n1, &n2, &n3, &n4, &n5);dprintf(fd, "index %d n0 %d n1 %d n2 %d n3 %d n4 %d n5 %d\n", index, n0, n1, n2, n3, n4, n5);
}
int yourColors;
int pellet_types[yourColors * 2];
scanf("%d\n", &yourColors);dprintf(fd, "yourColors %d\n", yourColors);
for (int i = 0; i < yourColors; i++) {
int colorIndex;
int colorAmount;
pellet_types[colorIndex] = true;
scanf("%d %d\n", &colorIndex, &colorAmount); dprintf(fd, "colorIndex %d colorAmount %d\n", colorIndex, colorAmount);
}
int opponentColors;
scanf("%d\n", &opponentColors);dprintf(fd, "opponentColors %d\n", opponentColors);
for (int i = 0; i < opponentColors; i++) {
int colorIndex;
int colorAmount;
pellet_types[colorIndex] = false;
scanf("%d %d\n", &colorIndex, &colorAmount);dprintf(fd, "colorIndex %d colorAmount %d\n", colorIndex, colorAmount);
}
while (true) {
// char* line = NULL;
// size_t cap = 0;
// fflush(stdin);
// fflush(stdout);
// int ret = getline(&line, &cap, stdin);
// dprintf(2, "RECEIVED LINE: %s | RET: %d", line, ret);
// free(line);
// dprintf(2, "NEXT ITERATION\n");
int column_to_play;
int direction;
scanf("%d\n", &direction);dprintf(fd, "direction %d\n", direction);
int numberOfValidInsertSlots;
scanf("%d\n", &numberOfValidInsertSlots);dprintf(fd, "numberOfValidInsertSlots %d\n", numberOfValidInsertSlots);
for (int i = 0; i < numberOfValidInsertSlots; i++) {
int column, cellIndex;
scanf("%d %d\n", &column, &cellIndex);dprintf(fd, "column %d cellIndex %d\n", column, cellIndex);
column_to_play = column;
}
int numberOfNewPellets;
scanf("%d\n", &numberOfNewPellets);dprintf(fd, "numberOfNewPellets %d\n", numberOfNewPellets);
for (int i = 0; i < numberOfNewPellets; i++) {
int index, cellIndex, colorIndex, isMine;
scanf("%d %d %d %d\n", &index, &cellIndex, &colorIndex, &isMine);dprintf(fd, "index %d cellIndex %d colorIndex %d isMine %d\n", index, cellIndex, colorIndex, isMine);
}
int numberOfChangedPellets;
scanf("%d\n", &numberOfChangedPellets);dprintf(fd, "numberOfChangedPellets %d\n", numberOfChangedPellets);
for (int i = 0; i < numberOfChangedPellets; i++) {
int index, cellIndex;
scanf("%d %d\n", &index, &cellIndex);dprintf(fd, "index %d cellIndex %d\n", index, cellIndex);
}
int numberOfPelletsInHand;
scanf("%d\n", &numberOfPelletsInHand);dprintf(fd, "numberOfPelletsInHand %d\n", numberOfPelletsInHand);
// TODO: figure out why this is causing the loop to halt
//WHY IS THE FIX TO CHANGE '\n' to %c !!!??????
int color_to_play;
for (int k = 0; k < numberOfPelletsInHand; k++) {
int colorIndex;
char newline;
// dprintf(fd, "INDEX OF PELLETS IN HAND ITERATION: %d\n", k);
scanf("%d%c", &colorIndex, &newline);
if (newline != '\n') {
dprintf(2, "HOUSTON WE HAVE A PROBLEM!!!!\n");
exit(1);
}
color_to_play = colorIndex;
dprintf(fd, "colorIndex %d\n", colorIndex);
dprintf(2, "colorIndex: %d\n", colorIndex);
// dprintf(fd, "\n");
}
// // close(fd);
// // read(STDIN_FILENO, buf, 50);
fflush(stdout);
fflush(stdin);
printf("PLACE %d %d\n", column_to_play, color_to_play);
dprintf(2, "BOT LOOP\n");
fflush(stdout);
}
}