-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgent_old.java
119 lines (103 loc) · 2.96 KB
/
Agent_old.java
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
/*********************************************
* Agent.java
* Sample Agent for Text-Based Adventure Game
* COMP3411 Artificial Intelligence
* UNSW Session 1, 2012
*/
//import java.util.*;
import java.io.*;
import java.net.*;
public class Agent_old {
public char get_action( char view[][] ) {
// REPLACE THIS CODE WITH AI TO CHOOSE ACTION
int ch=0;
System.out.print("Enter Action(s): ");
try {
while ( ch != -1 ) {
// read character from keyboard
ch = System.in.read();
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 );
}
}
}
catch (IOException e) {
System.out.println ("IO error:" + e );
}
return 0;
}
void print_view( char view[][] )
{
int i,j;
System.out.println("\n+-----+");
for( i=0; i < 5; i++ ) {
System.out.print("|");
for( j=0; j < 5; j++ ) {
if(( i == 2 )&&( j == 2 )) {
System.out.print('^');
}
else {
System.out.print( view[i][j] );
}
}
System.out.println("|");
}
System.out.println("+-----+");
}
public static void main( String[] args )
{
InputStream in = null;
OutputStream out= null;
Socket socket = null;
Agent_old agent = new Agent_old();
char view[][] = new char[5][5];
char action = 'F';
int port;
int ch;
int i,j;
if( args.length < 2 ) {
System.out.println("Usage: java Agent -p <port>\n");
System.exit(-1);
}
port = Integer.parseInt( args[1] );
try { // open socket to Game Engine
socket = new Socket( "localhost", port );
in = socket.getInputStream();
out = socket.getOutputStream();
}
catch( IOException e ) {
System.out.println("Could not bind to port: "+port);
System.exit(-1);
}
try { // scan 5-by-5 wintow around current location
while( true ) {
for( i=0; i < 5; i++ ) {
for( j=0; j < 5; j++ ) {
if( !(( i == 2 )&&( j == 2 ))) {
ch = in.read();
if( ch == -1 ) {
System.exit(-1);
}
view[i][j] = (char) ch;
}
}
}
agent.print_view( view ); // COMMENT THIS OUT BEFORE SUBMISSION
action = agent.get_action( view );
out.write( action );
}
}
catch( IOException e ) {
System.out.println("Lost connection to port: "+ port );
System.exit(-1);
}
finally {
try {
socket.close();
}
catch( IOException e ) {}
}
}
}