-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.c
More file actions
98 lines (79 loc) · 1.79 KB
/
Copy pathagent.c
File metadata and controls
98 lines (79 loc) · 1.79 KB
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
/*********************************************
* agent.c
* Sample Agent for Text-Based Adventure Game
* COMP3411 Artificial Intelligence
* UNSW Session 1, 2012
*/
#include <stdio.h>
#include <stdlib.h>
#include "pipe.h"
int pipe_fd;
FILE* in_stream;
FILE* out_stream;
char view[5][5];
char get_action( char view[5][5] ) {
// REPLACE THIS CODE WITH AI TO CHOOSE ACTION
int ch=0;
printf("Enter Action(s): ");
while(( ch = getchar()) != -1 ) { // read character from keyboard
switch( ch ) { // if character is a valid action, return it
case 'F': case 'L': case 'R': case 'C': case 'B':
case 'f': case 'l': case 'r': case 'c': case 'b':
return((char) ch);
}
}
return 0;
}
void print_view()
{
int i,j;
printf("\n+-----+\n");
for( i=0; i < 5; i++ ) {
putchar('|');
for( j=0; j < 5; j++ ) {
if(( i == 2 )&&( j == 2 )) {
putchar( '^' );
}
else {
putchar( view[i][j] );
}
}
printf("|\n");
}
printf("+-----+\n");
}
int main( int argc, char *argv[] )
{
char action;
int sd;
int ch;
int i,j;
if ( argc < 3 ) {
printf("Usage: %s -p port\n", argv[0] );
exit(1);
}
// open socket to Game Engine
sd = tcpopen("localhost", atoi( argv[2] ));
pipe_fd = sd;
in_stream = fdopen(sd,"r");
out_stream = fdopen(sd,"w");
while(1) {
// scan 5-by-5 wintow around current location
for( i=0; i < 5; i++ ) {
for( j=0; j < 5; j++ ) {
if( !(( i == 2 )&&( j == 2 ))) {
ch = getc( in_stream );
if( ch == -1 ) {
exit(1);
}
view[i][j] = ch;
}
}
}
print_view(); // COMMENT THIS OUT BEFORE SUBMISSION
action = get_action( view );
putc( action, out_stream );
fflush( out_stream );
}
return 0;
}