-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleAI.java
More file actions
142 lines (128 loc) · 5.07 KB
/
SimpleAI.java
File metadata and controls
142 lines (128 loc) · 5.07 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
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.nio.Buffer;
import java.util.Scanner;
public class SimpleAI {
private String serverAddress;
private String[] cards = new String[5];
private DifficultyAlgorithm difficultyAlgorithm;
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_YELLOW = "\u001B[33m";
public SimpleAI(String serverAddress, DifficultyAlgorithm difficultyAlgorithm) {
this.serverAddress = serverAddress;
this.difficultyAlgorithm = difficultyAlgorithm;
}
public void playAI() throws Exception {
Socket socket = new Socket(serverAddress, 58901);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
while (true) {
try {
String line = in.readLine();
if (line.startsWith("WAIT")) {
System.out.println(line);
continue;
} else if (line.startsWith("START")) {
line = in.readLine();
readCards(in);
out.println("READY");
} else if (line.startsWith("SELECT")) {
System.out.println(line);
out.println("READY");
selectCard(out);
} else if (line.startsWith("COMBAT")) {
System.out.println(line);
displayCombat(in);
} else if (line.startsWith("RESULT")) {
printColor(line);
} else if (line.startsWith("CARDS")){
readCards(in);
} else if (line.startsWith("SCORE")) {
System.out.println(line);
break;
}
} catch (Exception e) {
System.out.println(e.getMessage());
break;
}
}
socket.close();
}
private void printColor(String message) {
if (message.contains("WIN")) {
System.out.println(ANSI_GREEN + message + ANSI_RESET);
} else if (message.contains("LOSE")) {
System.out.println(ANSI_RED + message + ANSI_RESET);
} else if (message.contains("TIE")) {
System.out.println(ANSI_YELLOW + message + ANSI_RESET);
} else {
System.out.println(message);
}
}
private void readCards(BufferedReader in) throws Exception {
int numCards = Integer.parseInt(in.readLine());
System.out.println("Your cards are:");
for (int i = 0; i < numCards; i++) {
String cardString = in.readLine();
System.out.println("AI " + i + " :" + cardString);
cards[i] = cardString;
}
}
private void selectCard(PrintWriter out) {
String cardChoice = difficultyAlgorithm.selectCard(cards);
System.out.println("The card is " + cardChoice);
out.println(cardChoice);
}
private void displayCombat(BufferedReader in) throws Exception {
String yourCard = in.readLine();
String theirCard = in.readLine();
System.out.println("Your card: " + yourCard);
System.out.println("Their card: " + theirCard);
}
public static void main(String[] args) throws Exception {
DifficultyAlgorithm difficultyAlgorithm;
Scanner in = new Scanner(System.in);
boolean goodInput = false;
int choiceNum = 5;
System.out.println("Please Select a Difficulty:");
while (!goodInput) {
System.out.println("0: Easy \n1: Medium \n2: Hard");
String choice = in.nextLine();
try {
choiceNum = Integer.parseInt(choice);
if (choiceNum == 0 || choiceNum == 1 || choiceNum == 2) {
goodInput = true;
} else {
System.out.println("Please Enter a Valid Choice From 0 to 2");
}
} catch (Exception e) {
System.out.println("Please Enter a Number from 0 to 2");
}
}
//Set Difficulty
switch (choiceNum) {
case 0:
difficultyAlgorithm = new EasyAlgorithm();
System.out.println("Easy Algorithm Selected");
break;
case 1:
difficultyAlgorithm = new MediumAlgorithm();
System.out.println("Medium Algorithm Selected");
break;
case 2:
difficultyAlgorithm = new HardAlgorithm();
System.out.println("Hard Algorithm Selected");
break;
// Will never happen
default:
difficultyAlgorithm = new EasyAlgorithm();
}
SimpleAI simpleAI = new SimpleAI("localhost", difficultyAlgorithm);
simpleAI.playAI();
in.close();
}
}