-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
75 lines (68 loc) · 2.3 KB
/
player.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
/**
* Runs a player's game turn ...
*
* Can produce either a Hunter player or a Dracula player
* depending on the setting of the I_AM_DRACULA #define
*
* This is a dummy version of the real player.c used when
* you submit your AIs. It is provided so that you can
* test whether your code is likely to compile ...
*
* Note that it used to drive both Hunter and Dracula AIs.
* It first creates an appropriate view, and then invokes the
* relevant decide-my-move function, which should use the
* registerBestPlay() function to send the move back.
*
* The real player.c applies a timeout, so that it will halt
* decide-my-move after a fixed amount of time, if it doesn't
* finish first. The last move registered will be the one
* used by the game engine. This version of player.c won't stop
* your decide-my-move function if it goes into an infinite
* loop. Sort that out before you submit.
*
* Based on the program by David Collien, written in 2012
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Game.h"
#ifdef I_AM_DRACULA
#include "DracView.h"
#include "dracula.h"
#else
#include "HunterView.h"
#include "hunter.h"
#endif
// moves given by registerBestPlay are this long (including terminator)
#define MOVE_SIZE 3
// The minimum static globals I can get away with
static char latestPlay[MOVE_SIZE] = "";
static char latestMessage[MESSAGE_SIZE] = "";
int main(int argc, char *argv[])
{
#ifdef I_AM_DRACULA
DracView gameState;
char *plays = "GZA.... SED.... HZU.... MZU....";
PlayerMessage msgs[3] = { "", "", "" };
gameState = newDracView(plays,msgs);
decideDraculaMove(gameState);
disposeDracView(gameState);
#else
HunterView gameState;
char *plays = "GAM.... SCN....";
PlayerMessage msgs[3] = { "", "", "" };
gameState = newHunterView(plays,msgs);
decideHunterMove(gameState);
disposeHunterView(gameState);
#endif
printf("Move: %s, Message: %s\n", latestPlay, latestMessage);
return EXIT_SUCCESS;
}
// Saves characters from play (and appends a terminator)
// and saves characters from message (and appends a terminator)
void registerBestPlay (char *play, PlayerMessage message) {
strncpy(latestPlay, play, MOVE_SIZE-1);
latestPlay[MOVE_SIZE-1] = '\0';
strncpy(latestMessage, message, MESSAGE_SIZE);
latestMessage[MESSAGE_SIZE-1] = '\0';
}