196 lines
6.4 KiB
Java
196 lines
6.4 KiB
Java
|
import java.util.ArrayList;
|
||
|
import java.util.Map;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Scanner;
|
||
|
import java.util.Random;
|
||
|
|
||
|
public class TicTacToe {
|
||
|
private static ArrayList<String> freeSpaces;
|
||
|
private static Map<String, String> fullBoard;
|
||
|
private static Scanner input;
|
||
|
|
||
|
private static String player() {
|
||
|
while(true){
|
||
|
System.out.println(String.format("Make your move. Valid moves: {0}", freeSpaces));
|
||
|
String move = input.nextLine();
|
||
|
if (freeSpaces.contains(move)) {
|
||
|
return move;
|
||
|
} else {
|
||
|
System.out.println("The move you entered is not valid. Please enter a valid move.");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static String ai() {
|
||
|
int rnd = new Random().nextInt(freeSpaces.size());
|
||
|
String[] tmpFreeSpaces = freeSpaces.toArray(new String[0]);
|
||
|
String move = tmpFreeSpaces[rnd];
|
||
|
System.out.println(move);
|
||
|
return move;
|
||
|
}
|
||
|
|
||
|
private static void handle(String playing, String move) {
|
||
|
switch (playing) {
|
||
|
case "player":
|
||
|
fullBoard.put(move, "X");
|
||
|
freeSpaces.remove(move);
|
||
|
break;
|
||
|
case "ai":
|
||
|
fullBoard.put(move, "O");
|
||
|
freeSpaces.remove(move);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
private static boolean checkForDraw() {
|
||
|
if (freeSpaces.size() == 0) {
|
||
|
return true;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static String checkForWin() {
|
||
|
String a1Value = fullBoard.get("a1");
|
||
|
String a2Value = fullBoard.get("a2");
|
||
|
String a3Value = fullBoard.get("a3");
|
||
|
String b1Value = fullBoard.get("b1");
|
||
|
String b2Value = fullBoard.get("b2");
|
||
|
String b3Value = fullBoard.get("b3");
|
||
|
String c1Value = fullBoard.get("c1");
|
||
|
String c2Value = fullBoard.get("c2");
|
||
|
String c3Value = fullBoard.get("c3");
|
||
|
|
||
|
if (a1Value.equals("X") && a2Value.equals("X") && a3Value.equals("X")) {
|
||
|
return "player";
|
||
|
} else if (b1Value.equals("X") && b2Value.equals("X") && b3Value.equals("X")) {
|
||
|
return "player";
|
||
|
} else if (c1Value.equals("X") && c2Value.equals("X") && c3Value.equals("X")) {
|
||
|
return "player";
|
||
|
} else if (a1Value.equals("X") && b1Value.equals("X") && c1Value.equals("X")) {
|
||
|
return "player";
|
||
|
} else if (a2Value.equals("X") && b2Value.equals("X") && c2Value.equals("X")) {
|
||
|
return "player";
|
||
|
} else if (a3Value.equals("X") && b3Value.equals("X") && c3Value.equals("X")) {
|
||
|
return "player";
|
||
|
} else if (a1Value.equals("X") && b2Value.equals("X") && c3Value.equals("X")) {
|
||
|
return "player";
|
||
|
} else if (a3Value.equals("X") && b2Value.equals("X") && c1Value.equals("X")) {
|
||
|
return "player";
|
||
|
}
|
||
|
|
||
|
if (a1Value.equals("O") && a2Value.equals("O") && a3Value.equals("O")) {
|
||
|
return "ai";
|
||
|
} else if (b1Value.equals("O") && b2Value.equals("O") && b3Value.equals("O")) {
|
||
|
return "ai";
|
||
|
} else if (c1Value.equals("O") && c2Value.equals("O") && c3Value.equals("O")) {
|
||
|
return "ai";
|
||
|
} else if (a1Value.equals("O") && b1Value.equals("O") && c1Value.equals("O")) {
|
||
|
return "ai";
|
||
|
} else if (a2Value.equals("O") && b2Value.equals("O") && c2Value.equals("O")) {
|
||
|
return "ai";
|
||
|
} else if (a3Value.equals("O") && b3Value.equals("O") && c3Value.equals("O")) {
|
||
|
return "ai";
|
||
|
} else if (a1Value.equals("O") && b2Value.equals("O") && c3Value.equals("O")) {
|
||
|
return "ai";
|
||
|
} else if (a3Value.equals("O") && b2Value.equals("O") && c1Value.equals("O")) {
|
||
|
return "ai";
|
||
|
}
|
||
|
|
||
|
if (checkForDraw()){
|
||
|
return "draw";
|
||
|
}
|
||
|
|
||
|
return "none";
|
||
|
}
|
||
|
|
||
|
private static void showBoard() {
|
||
|
String a1Value = fullBoard.get("a1");
|
||
|
String a2Value = fullBoard.get("a2");
|
||
|
String a3Value = fullBoard.get("a3");
|
||
|
String b1Value = fullBoard.get("b1");
|
||
|
String b2Value = fullBoard.get("b2");
|
||
|
String b3Value = fullBoard.get("b3");
|
||
|
String c1Value = fullBoard.get("c1");
|
||
|
String c2Value = fullBoard.get("c2");
|
||
|
String c3Value = fullBoard.get("c3");
|
||
|
System.out.println("___1___2___3__");
|
||
|
System.out.printf("a| %s | %s | %s |\n", a1Value, a2Value, a3Value);
|
||
|
System.out.printf("b| %s | %s | %s |\n", b1Value, b2Value, b3Value);
|
||
|
System.out.printf("c| %s | %s | %s |\n", c1Value, c2Value, c3Value);
|
||
|
System.out.println("______________");
|
||
|
}
|
||
|
|
||
|
private static boolean processWin(String status) {
|
||
|
switch (status) {
|
||
|
case "player":
|
||
|
System.out.println("You have won! GG!");
|
||
|
showBoard();
|
||
|
return true;
|
||
|
case "ai":
|
||
|
System.out.println("You have lost against an AI that makes random moves. Shameful.");
|
||
|
showBoard();
|
||
|
return true;
|
||
|
case "draw":
|
||
|
System.out.println("It's a draw!");
|
||
|
showBoard();
|
||
|
return true;
|
||
|
default:
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// __a___b___c__
|
||
|
//1| X | O | O |
|
||
|
// _____________
|
||
|
//2| O | X | X |
|
||
|
// _____________
|
||
|
//3| O | X | X |
|
||
|
// _____________
|
||
|
|
||
|
public static void trueMain() {
|
||
|
while(true) {
|
||
|
showBoard();
|
||
|
handle("player", player());
|
||
|
if (processWin(checkForWin())) {
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
showBoard();
|
||
|
handle("ai", ai());
|
||
|
if (processWin(checkForWin())) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void main(String args[]) {
|
||
|
fullBoard = new HashMap<>();
|
||
|
freeSpaces = new ArrayList<>();
|
||
|
input = new Scanner(System.in);
|
||
|
fullBoard.put("a1", " ");
|
||
|
fullBoard.put("a2", " ");
|
||
|
fullBoard.put("a3", " ");
|
||
|
fullBoard.put("b1", " ");
|
||
|
fullBoard.put("b2", " ");
|
||
|
fullBoard.put("b3", " ");
|
||
|
fullBoard.put("c1", " ");
|
||
|
fullBoard.put("c2", " ");
|
||
|
fullBoard.put("c3", " ");
|
||
|
fullBoard.put("a1", " ");
|
||
|
freeSpaces.add("a1");
|
||
|
freeSpaces.add("a2");
|
||
|
freeSpaces.add("a3");
|
||
|
freeSpaces.add("b1");
|
||
|
freeSpaces.add("b2");
|
||
|
freeSpaces.add("b3");
|
||
|
freeSpaces.add("c1");
|
||
|
freeSpaces.add("c2");
|
||
|
freeSpaces.add("c3");
|
||
|
trueMain();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|