Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 15th, 2012  |  syntax: Java  |  size: 5.75 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.*;
  2.  
  3. class Estado {
  4.         char matrix[];
  5.         char player;
  6.         int val;
  7.  
  8.         Estado() {
  9.                 matrix = new char[9];
  10.                 for (int i = 0; i < 9; i++)
  11.                         matrix[i] = Integer.toString(i + 1).charAt(0);
  12.                 this.player = 'X';
  13.         }
  14.  
  15.         Estado(char player, char matrix[]) {
  16.                 this.matrix = matrix.clone();
  17.                 this.player = player;
  18.         }
  19.  
  20.         void printMatrix() {
  21.                 for (int i = 0; i < 9; i = i + 3) {
  22.                         for (int j = i; j < i + 3; j++)
  23.                                 System.out.print(matrix[j] + " ");
  24.                         System.out.println();
  25.                 }
  26.         }
  27.        
  28.         int getVal(){
  29.             char op;
  30.             if(player=='X')
  31.                 op = 'O';
  32.             else
  33.                 op = 'X';
  34.             if(checkWinner(player)){
  35.                 return Integer.MAX_VALUE-1;
  36.         }else if(checkWinner(op)){
  37.             return Integer.MIN_VALUE+1;
  38.         }else if(checkEnd()){
  39.             return 0;
  40.         }else{
  41.                 return checkChances(op)-checkChances(player);
  42.             }
  43.            
  44.         }
  45.  
  46.         int checkChances(char player) {
  47.                 int chances = 0;
  48.                 if (matrix[0] != player && matrix[1] != player && matrix[2] != player)
  49.                         chances++;// linha1
  50.                 if (matrix[3] != player && matrix[4] != player && matrix[5] != player)
  51.                         chances++;// linha2
  52.                 if (matrix[6] != player && matrix[7] != player && matrix[8] != player)
  53.                         chances++;// linha3
  54.                 if (matrix[0] != player && matrix[3] != player && matrix[6] != player)
  55.                         chances++;// coluna1
  56.                 if (matrix[1] != player && matrix[4] != player && matrix[7] != player)
  57.                         chances++;// coluna2
  58.                 if (matrix[2] != player && matrix[5] != player && matrix[8] != player)
  59.                         chances++;// coluna3
  60.                 if (matrix[0] != player && matrix[4] != player && matrix[8] != player)
  61.                         chances++;// diagonal1
  62.                 if (matrix[2] != player && matrix[4] != player && matrix[6] != player)
  63.                         chances++;// diagonal2
  64.                 return chances;
  65.         }
  66.  
  67.         boolean checkWinner(char player) {
  68.                 if ((matrix[0] == player && matrix[1] == player && matrix[2] == player)
  69.                                 || (matrix[3] == player && matrix[4] == player && matrix[5] == player)
  70.                                 || (matrix[6] == player && matrix[7] == player && matrix[8] == player)
  71.                                 || (matrix[0] == player && matrix[3] == player && matrix[6] == player)
  72.                                 || (matrix[1] == player && matrix[4] == player && matrix[7] == player)
  73.                                 || (matrix[2] == player && matrix[5] == player && matrix[8] == player)
  74.                                 || (matrix[0] == player && matrix[4] == player && matrix[8] == player)
  75.                                 || (matrix[2] == player && matrix[4] == player && matrix[6] == player))
  76.                         return true;
  77.  
  78.                 return false;
  79.         }
  80.  
  81.         boolean checkEnd() {
  82.                 if (checkWinner('X') || checkWinner('O'))
  83.                     return true;
  84.             for (int i = 0; i < 9; i++)
  85.                                 if (matrix[i] != 'X' && matrix[i] != 'O')
  86.                                         return false;
  87.                 return true;
  88.         }
  89.  
  90.         LinkedList<Estado> getFilhos() {
  91.                 char p;
  92.                 if (player == 'X')
  93.                         p = 'O';
  94.                 else
  95.                         p = 'X';
  96.                 LinkedList<Estado> res = new LinkedList<Estado>();
  97.                 Estado e;
  98.                 for (int i = 0; i < 9; i++) {
  99.                         if (matrix[i] == 'X' || matrix[i] == 'O')
  100.                                 continue;
  101.                         e = new Estado(p, this.matrix);
  102.                         e.matrix[i] = p;
  103.                         res.add(e);
  104.                 }
  105.                 return res;
  106.         }
  107. }
  108.  
  109. public class JogoDoGalo {
  110.         static char player;
  111.         static char computer;
  112.         static Scanner in = new Scanner(System.in);
  113.         static Estado state;
  114.  
  115.         static void printEscolha() {
  116.                 System.out.println("Escolha um numero");
  117.         }
  118.  
  119.         static boolean isPlayValid(int play) {
  120.                 if (state.matrix[play - 1] == 'X' || state.matrix[play - 1] == 'O') {
  121.                         state.printMatrix();
  122.                         System.out.println("Jogada Invalida");
  123.                         return false;
  124.                 } else
  125.                         return true;
  126.         }
  127.  
  128.         static void playerPlay() {
  129.                 state.printMatrix();
  130.                 printEscolha();
  131.                 int play = in.nextInt();
  132.                 while (!isPlayValid(play))
  133.                         play = in.nextInt();
  134.                 state.matrix[play-1] = player;
  135.         }
  136.  
  137.     static Estado minmax(Estado no, int prof){
  138.         Estado res = null;
  139.         int val;
  140.        
  141.         if(no.checkEnd() || prof==0){
  142.             no.val = no.getVal();
  143.             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
  144.                 no.val*=-1;
  145.             return no;
  146.         }
  147.        
  148.         LinkedList<Estado> filhos = no.getFilhos();
  149.        
  150.         if(no.player==player){//é o pc a jogar - MAX
  151.             val = Integer.MIN_VALUE;
  152.             for(Estado f : filhos){
  153.                 Estado f2 = minmax(f, prof-1);
  154.                 if(f2.val>val){
  155.                     res = f;
  156.                     val = f2.val;
  157.                     res.val = val;
  158.                 }
  159.             }
  160.         }else{//player a jogar - MIN
  161.             val = Integer.MAX_VALUE;
  162.             for(Estado f : filhos){
  163.                 Estado f2 = minmax(f, prof-1);
  164.                 if(f2.val<val){
  165.                     res = f;
  166.                     val = f2.val;
  167.                     res.val = val;
  168.                 }
  169.             }
  170.         }
  171.        
  172.         return res;
  173.     }
  174.  
  175.         static void computerPlay() {
  176.             state.matrix = minmax(state, 100).matrix;
  177.         }
  178.  
  179.         public static void main(String[] args) {
  180.                 state = new Estado();
  181.                 int p = 2;
  182.                 while (p != 0 && p != 1) {
  183.                         System.out.println("Escolha seu elemento 0-X, 1-O");
  184.                         p = in.nextInt();
  185.                 }
  186.                 if(p==0){
  187.                         player = 'X';
  188.                         computer = 'O';
  189.                 }else{
  190.                         player = 'O';
  191.                         computer = 'X';
  192.                 }
  193.                                
  194.                 char turn = 'X';
  195.  
  196.                 while (true) {
  197.                         if(turn==player){
  198.                                 state.player = computer;
  199.                                 playerPlay();
  200.                             turn = computer;
  201.                         }else{
  202.                             state.player = player;
  203.                                 computerPlay();
  204.                                 turn = player;
  205.                         }
  206.                         if (state.checkEnd()) {
  207.                                 if (state.checkWinner('X')) {
  208.                                         state.printMatrix();
  209.                                         System.out.println("Ganhou o jogador X");
  210.                                 }else if (state.checkWinner('O')) {
  211.                                         state.printMatrix();
  212.                                         System.out.println("Ganhou o jogador O");
  213.                                 }else{
  214.                                         state.printMatrix();
  215.                                         System.out.println("Empate");
  216.                                 }
  217.                                 break;
  218.                         }
  219.                 }
  220.  
  221.         }
  222. }