-
Notifications
You must be signed in to change notification settings - Fork 0
/
RRNetOut.java
140 lines (116 loc) · 2.46 KB
/
RRNetOut.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright © 2003, 2011 Bart Massey
* [This program is licensed under the "MIT License"]
* Please see the file COPYING in the source
* distribution of this software for license terms.
*/
/*
* Ricochet Robots Output Server Connection
*/
/**
* @author Bart Massey <[email protected]>
*
*/
import java.io.*;
import java.net.*;
public class RRNetOut {
PrintStream out;
RRController ctl;
/* XXX Doesn't belong here */
public String[] dirname = {
"north", "east", "south", "west"
};
void uniprint(String s) {
boolean isodd = false;
for (int i = 0; i < s.length(); i++) {
int c = s.charAt(i);
if (c <= ' ' || c >= 0x7f || c == '"') {
isodd = true;
break;
}
}
if (isodd) {
out.print('"');
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '"')
out.print('\\');
out.print(s.charAt(i));
}
out.print('"');
return;
}
out.print(s);
}
public RRNetOut(Socket s, RRController ctl)
throws IOException, UnsupportedEncodingException {
this.ctl = ctl;
OutputStream so = s.getOutputStream();
BufferedOutputStream sb = new BufferedOutputStream(so);
out = new PrintStream(sb, true, "UTF-8");
}
public void hello(String name) {
out.print("helo ");
uniprint(name);
out.println();
}
public void newGame(String game) {
out.print("new ");
uniprint(game);
out.println();
}
public void join(String game) {
out.print("join ");
uniprint(game);
out.println();
}
public void show() {
out.println("show");
}
public void watch(String game) {
out.print("watch ");
uniprint(game);
out.println();
}
public void bid(int b) {
out.print("bid ");
out.print(b);
out.println();
}
public void move(int color, int dir) {
out.println("move " +
RRSquare.colorname[color] + " " +
dirname[dir]);
}
public void undo() {
out.println("undo");
}
public void reset() {
out.println("reset");
}
public void revoke () {
out.println("revoke");
}
public void abandon () {
out.println ("abandon");
}
public void nobid () {
out.println ("nobid");
}
public void turn () {
out.println ("turn");
}
public void pass () {
out.println ("pass");
}
public void part () {
out.println ("part");
}
public void quit () {
out.println ("quit");
}
public void message (String text) {
out.print ("message ");
uniprint (text);
out.println();
}
}