import java.util.*;
class Estado {
char matrix[];
char player;
int val;
Estado() {
matrix = new char[9];
for (int i = 0; i < 9; i++)
matrix[i] = Integer.toString(i + 1).charAt(0);
this.player = 'X';
}
Estado(char player, char matrix[]) {
this.matrix = matrix.clone();
this.player = player;
}
void printMatrix() {
for (int i = 0; i < 9; i = i + 3) {
for (int j = i; j < i + 3; j++)
System.out.print(matrix[j] + " ");
System.out.println();
}
}
int getVal(){
char op;
if(player=='X')
op = 'O';
else
op = 'X';
if(checkWinner(player)){
return Integer.MAX_VALUE-1;
}else if(checkWinner(op)){
return Integer.MIN_VALUE+1;
}else if(checkEnd()){
return 0;
}else{
return checkChances(op)-checkChances(player);
}
}
int checkChances(char player) {
int chances = 0;
if (matrix[0] != player && matrix[1] != player && matrix[2] != player)
chances++;// linha1
if (matrix[3] != player && matrix[4] != player && matrix[5] != player)
chances++;// linha2
if (matrix[6] != player && matrix[7] != player && matrix[8] != player)
chances++;// linha3
if (matrix[0] != player && matrix[3] != player && matrix[6] != player)
chances++;// coluna1
if (matrix[1] != player && matrix[4] != player && matrix[7] != player)
chances++;// coluna2
if (matrix[2] != player && matrix[5] != player && matrix[8] != player)
chances++;// coluna3
if (matrix[0] != player && matrix[4] != player && matrix[8] != player)
chances++;// diagonal1
if (matrix[2] != player && matrix[4] != player && matrix[6] != player)
chances++;// diagonal2
return chances;
}
boolean checkWinner(char player) {
if ((matrix[0] == player && matrix[1] == player && matrix[2] == player)
|| (matrix[3] == player && matrix[4] == player && matrix[5] == player)
|| (matrix[6] == player && matrix[7] == player && matrix[8] == player)
|| (matrix[0] == player && matrix[3] == player && matrix[6] == player)
|| (matrix[1] == player && matrix[4] == player && matrix[7] == player)
|| (matrix[2] == player && matrix[5] == player && matrix[8] == player)
|| (matrix[0] == player && matrix[4] == player && matrix[8] == player)
|| (matrix[2] == player && matrix[4] == player && matrix[6] == player))
return true;
return false;
}
boolean checkEnd() {
if (checkWinner('X') || checkWinner('O'))
return true;
for (int i = 0; i < 9; i++)
if (matrix[i] != 'X' && matrix[i] != 'O')
return false;
return true;
}
LinkedList<Estado> getFilhos() {
char p;
if (player == 'X')
p = 'O';
else
p = 'X';
LinkedList<Estado> res = new LinkedList<Estado>();
Estado e;
for (int i = 0; i < 9; i++) {
if (matrix[i] == 'X' || matrix[i] == 'O')
continue;
e = new Estado(p, this.matrix);
e.matrix[i] = p;
res.add(e);
}
return res;
}
}
public class JogoDoGalo {
static char player;
static char computer;
static Scanner in = new Scanner(System.in);
static Estado state;
static void printEscolha() {
System.out.println("Escolha um numero");
}
static boolean isPlayValid(int play) {
if (state.matrix[play - 1] == 'X' || state.matrix[play - 1] == 'O') {
state.printMatrix();
System.out.println("Jogada Invalida");
return false;
} else
return true;
}
static void playerPlay() {
state.printMatrix();
printEscolha();
int play = in.nextInt();
while (!isPlayValid(play))
play = in.nextInt();
state.matrix[play-1] = player;
}
static Estado minmax(Estado no, int prof){
Estado res = null;
int val;
if(no.checkEnd() || prof==0){
no.val = no.getVal();
if(no.player==player) // a euristica tem de ser em relacao ao computaor, se o utilizador tiver possibilidade de ganhar a euristica tem de ser negativa
no.val*=-1;
return no;
}
LinkedList<Estado> filhos = no.getFilhos();
if(no.player==player){//é o pc a jogar - MAX
val = Integer.MIN_VALUE;
for(Estado f : filhos){
Estado f2 = minmax(f, prof-1);
if(f2.val>val){
res = f;
val = f2.val;
res.val = val;
}
}
}else{//player a jogar - MIN
val = Integer.MAX_VALUE;
for(Estado f : filhos){
Estado f2 = minmax(f, prof-1);
if(f2.val<val){
res = f;
val = f2.val;
res.val = val;
}
}
}
return res;
}
static void computerPlay() {
state.matrix = minmax(state, 100).matrix;
}
public static void main(String[] args) {
state = new Estado();
int p = 2;
while (p != 0 && p != 1) {
System.out.println("Escolha seu elemento 0-X, 1-O");
p = in.nextInt();
}
if(p==0){
player = 'X';
computer = 'O';
}else{
player = 'O';
computer = 'X';
}
char turn = 'X';
while (true) {
if(turn==player){
state.player = computer;
playerPlay();
turn = computer;
}else{
state.player = player;
computerPlay();
turn = player;
}
if (state.checkEnd()) {
if (state.checkWinner('X')) {
state.printMatrix();
System.out.println("Ganhou o jogador X");
}else if (state.checkWinner('O')) {
state.printMatrix();
System.out.println("Ganhou o jogador O");
}else{
state.printMatrix();
System.out.println("Empate");
}
break;
}
}
}
}