-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandt.c
171 lines (157 loc) · 3.74 KB
/
randt.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
/*********************************************************
* randt9.c
* Nine-Board Tic-Tac-Toe Random Player
* COMP3411/9414/9814 Artificial Intelligence
* Alan Blair, CSE, UNSW
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "common.h"
#include "agent.h"
#include "game.h"
#define MAX_MOVE 81
int board[10][10];
int move[MAX_MOVE+1];
int player;
int m;
/*********************************************************//*
Print usage information and exit
*/
void usage( char argv0[] )
{
printf("Usage: %s\n",argv0);
printf(" [-p port]\n"); // tcp port
printf(" [-h host]\n"); // tcp host
exit(1);
}
/*********************************************************//*
Parse command-line arguments
*/
void agent_parse_args( int argc, char *argv[] )
{
int i=1;
while( i < argc ) {
if( strcmp( argv[i], "-p" ) == 0 ) {
if( i+1 >= argc ) {
usage( argv[0] );
}
port = atoi(argv[i+1]);
i += 2;
}
else if( strcmp( argv[i], "-h" ) == 0 ) {
if( i+1 >= argc ) {
usage( argv[0] );
}
host = argv[i+1];
i += 2;
}
else {
usage( argv[0] );
}
}
}
/*********************************************************//*
Called at the beginning of a series of games
*/
void agent_init()
{
struct timeval tp;
// generate a new random seed each time
gettimeofday( &tp, NULL );
srandom(( unsigned int )( tp.tv_usec ));
}
/*********************************************************//*
Called at the beginning of each game
*/
void agent_start( int this_player )
{
reset_board( board );
m = 0;
move[m] = 0;
player = this_player;
}
/*********************************************************//*
Choose second move and return it
*/
int agent_second_move( int board_num, int prev_move )
{
int this_move;
move[0] = board_num;
move[1] = prev_move;
board[board_num][prev_move] = !player;
m = 2;
do {
this_move = 1 + random()% 9;
} while( board[prev_move][this_move] != EMPTY );
move[m] = this_move;
board[prev_move][this_move] = player;
return( this_move );
}
/*********************************************************//*
Choose third move and return it
*/
int agent_third_move(
int board_num,
int first_move,
int prev_move
)
{
int this_move;
move[0] = board_num;
move[1] = first_move;
move[2] = prev_move;
board[board_num][first_move] = player;
board[first_move][prev_move] = !player;
m=3;
do {
this_move = 1 + random()% 9;
} while( board[prev_move][this_move] != EMPTY );
move[m] = this_move;
board[move[m-1]][this_move] = player;
return( this_move );
}
/*********************************************************//*
Choose next move and return it
*/
int agent_next_move( int prev_move )
{
int this_move;
m++;
move[m] = prev_move;
board[move[m-1]][move[m]] = !player;
m++;
do {
this_move = 1 + random()% 9;
} while( board[prev_move][this_move] != EMPTY );
move[m] = this_move;
board[move[m-1]][this_move] = player;
return( this_move );
}
/*********************************************************//*
Receive last move and mark it on the board
*/
void agent_last_move( int prev_move )
{
m++;
move[m] = prev_move;
board[move[m-1]][move[m]] = !player;
}
/*********************************************************//*
Called after each game
*/
void agent_gameover(
int result,// WIN, LOSS or DRAW
int cause // TRIPLE, ILLEGAL_MOVE, TIMEOUT or FULL_BOARD
)
{
// nothing to do here
}
/*********************************************************//*
Called after the series of games
*/
void agent_cleanup()
{
// nothing to do here
}