Skip to content

Instantly share code, notes, and snippets.

@mslenc
Created November 14, 2017 17:54
Show Gist options
  • Save mslenc/e1eb04a2173a9c2a60016b414a6fe1fd to your computer and use it in GitHub Desktop.
Save mslenc/e1eb04a2173a9c2a60016b414a6fe1fd to your computer and use it in GitHub Desktop.
import java.util.*;
import java.util.Scanner;
class Logic {
static class Move {
final int position;
final int score;
Move(int position, int score) {
this.position = position;
this.score = score;
}
}
public static class TicTacToe {
private final int[] board = new int[9];
private final int emptySpace = 0;
private final int playerAI = 1;
private final int playerHuman = 2;
private List<Integer> possibleMoves() {
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < 9; i++) {
if (board[i] == emptySpace)
res.add(i);
}
return res;
}
private boolean win(int player) {
return (board[0] == player && board[1] == player && board[2] == player) ||
(board[3] == player && board[4] == player && board[5] == player) ||
(board[6] == player && board[7] == player && board[8] == player) ||
(board[0] == player && board[3] == player && board[6] == player) ||
(board[1] == player && board[4] == player && board[7] == player) ||
(board[2] == player && board[5] == player && board[8] == player) ||
(board[0] == player && board[4] == player && board[8] == player) ||
(board[2] == player && board[4] == player && board[6] == player);
}
private Move mechanics(int player) {
int otherPlayer = 3 - player;
if (win(player))
return new Move(-1, 10);
if (win(otherPlayer))
return new Move(-1, -10);
List<Integer> possibleMoves = possibleMoves();
if (possibleMoves.isEmpty())
return new Move(-1, 0);
Move bestMove = null;
for (int move : possibleMoves) {
board[move] = player;
Move result = mechanics(otherPlayer);
board[move] = emptySpace;
if (bestMove == null || bestMove.score < -result.score) {
bestMove = new Move(move, -result.score);
}
}
return bestMove;
}
public void printTable() {
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println("- - -");
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println("- - -");
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
System.out.println();
}
public void startGame() {
int currentPlayer = playerHuman;
Scanner ulaz = new Scanner(System.in);
while (true) {
printTable();
if (win(playerHuman)) {
System.out.println("YOU WIN!");
break;
}
if (win(playerAI)) {
System.out.println("YOU LOSE!");
break;
}
if (possibleMoves().isEmpty()) {
System.out.println("DRAW");
break;
}
if (currentPlayer == playerHuman) {
board[ulaz.nextInt()] = playerHuman;
} else {
Move move = mechanics(playerAI);
if (move.position >= 0) {
board[move.position] = playerAI;
} else {
break;
}
}
currentPlayer = 3 - currentPlayer;
}
}
}
public static void main(String[] args) {
TicTacToe game = new TicTacToe();
game.startGame();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment