Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.06 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.lang.*;
  4.  
  5. //classe que representa um estado do jogo
  6. //inclui os métodos: jogada; clear; imprime; cheio
  7.  
  8. class Tabuleiro {
  9.     char [][] matriz = new char [6][7];
  10.  
  11.     public Tabuleiro (){
  12.     for(int i=0; i<6; i++){
  13.         for(int j=0; j<7; j++){
  14.         matriz[i][j] = '-';
  15.         }
  16.     }
  17.     }
  18.  
  19.     public void jogada (int y, char turn){
  20.     for (int i=5; i>=0; i--){
  21.         if (matriz[i][y]=='-'){
  22.         matriz[i][y]=turn;
  23.         break;
  24.         }
  25.     }
  26.     }
  27.  
  28.     public void clear (int j){
  29.     for (int i=0; i<6; i++){
  30.         if (matriz[i][j]!='-'){
  31.         matriz[i][j]='-';
  32.         break;
  33.         }
  34.     }
  35.     }
  36.  
  37.     public void imprime () {
  38.    
  39.     for(int i=0;i<6;i++){
  40.         System.out.print(i +" ");
  41.         for(int j=0;j<7;j++){
  42.         //System.out.print(j+" ");
  43.         System.out.print( matriz[i][j]+" ");
  44.         }
  45.         System.out.println();
  46.     }
  47.     System.out.println("  0 1 2 3 4 5 6");
  48.     System.out.println();
  49.     }
  50.     public int cheio (){
  51.     int blanck = 0;
  52.     for(int i=0; i<6; i++){
  53.         for(int j=0; j<7; j++){
  54.         if(matriz[i][j]=='-')
  55.             blanck++;
  56.         }
  57.     }
  58.     return blanck;
  59.     }
  60. }
  61.  
  62. public class Alpha_Beta {
  63.     //Estado do jogo que é atualizado em cada jogada
  64.     private Tabuleiro t;
  65.     //Scanner para ler input
  66.     private Scanner in;
  67.     //coluna escolhida pelo minimax para a proxima jogada do pc
  68.     private int next;
  69.     //profundidade até onde deixamos o minimax iterar
  70.     private int maxDepth;
  71.     //caracter do pc
  72.     private char c;
  73.     //caracter do adversario
  74.     private char pl;
  75.    
  76.     public Alpha_Beta (Tabuleiro t){
  77.     this.t = t;
  78.     in = new Scanner (System.in);
  79.     }
  80.  
  81.     public void check (){
  82.     int res = Final (t);
  83.     if (res == 512){
  84.         System.out.println ("O computador ganhou!");
  85.         System.exit(0);
  86.     }
  87.     else if (res == -512){
  88.         System.out.println ("Parabéns! Ganhou!");
  89.         System.exit(0);
  90.     }
  91.     else {
  92.         if (t.cheio()==0){
  93.         System.out.println ("Empate");
  94.         //j.play();
  95.         System.exit(0);
  96.         }
  97.     }
  98.     }
  99.    
  100.     public int Final (Tabuleiro t){
  101.     //verificação na horizontal
  102.     int pc = 0;
  103.     int player = 0;
  104.     for(int i=0; i<6; i++){
  105.         for(int j=0; j<7-3; j++){
  106.         if(t.matriz[i][j]==c && t.matriz[i][j+1]==c && t.matriz [i][j+2]==c && t.matriz [i][j+3]==c)
  107.             pc = 4;
  108.         else if (t.matriz[i][j]==pl && t.matriz[i][j+1]==pl && t.matriz [i][j+2]==pl && t.matriz [i][j+3]==pl)
  109.             player = 4;
  110.         }
  111.     }
  112.     if (pc==4) return 512;
  113.     if (player==4) return -512;
  114.     //verificação na vertical
  115.     for (int i=0; i< 6-3; i++){
  116.         for(int j=0; j<7; j++){
  117.         if (t.matriz[i][j]==c && t.matriz[i+1][j]==c && t.matriz[i+2][j]==c && t.matriz[i+3][j]==c)
  118.             pc = 4;
  119.         else if (t.matriz[i][j]==pl && t.matriz[i+1][j]==pl && t.matriz[i+2][j]==pl && t.matriz[i+3][j]==pl)
  120.             player = 4;
  121.         }
  122.     }
  123.     if (pc==4) return 512;
  124.     if (player==4) return -512;
  125.     //verificação ma diagonal
  126.     //diagonal ascendente
  127.     for(int i = 3; i<6; i++){
  128.         for(int j=0; j<7-3; j++){
  129.         if (t.matriz[i][j]==c && t.matriz[i-1][j+1]==c && t.matriz[i-2][j+2]==c && t.matriz[i-3][j+3]==c)
  130.             pc = 4;
  131.         else if (t.matriz[i][j]==pl && t.matriz[i-1][j+1]==pl && t.matriz[i-2][j+2]==pl && t.matriz[i-3][j+3]==pl)
  132.             player = 4;
  133.         }
  134.     }
  135.     if (pc==4) return 512;
  136.     if (player==4) return -512;
  137.     //diagonal descendente
  138.     for(int i = 0; i<3; i++){
  139.         for(int j=0; j<7-3; j++){
  140.         if (t.matriz[i][j]==c && t.matriz[i+1][j+1]==c && t.matriz[i+2][j+2]==c && t.matriz[i+3][j+3]==c)
  141.             pc = 4;
  142.         else if (t.matriz[i][j]==pl && t.matriz[i+1][j+1]==pl && t.matriz[i+2][j+2]==pl && t.matriz[i+3][j+3]==pl)
  143.             player = 4;
  144.         }
  145.     }
  146.     if (pc==4) return 512;
  147.     if (player==4) return -512;
  148.     return 0;
  149.     }
  150.    
  151.     public void Adversario (){
  152.     System.out.println ("Introduza a coluna, de 0 a 6, onde pretende jogar");
  153.     int col = in.nextInt();
  154.     while (col < 0 || col > 7 || t.matriz[0][col]!='-'){
  155.         System.out.println ("Introduza a coluna, de 0 a 6, onde pretende jogar");
  156.         col = in.nextInt();
  157.     }
  158.     t.jogada (col,pl);
  159.     }
  160.    
  161.     public void Computador (){
  162.         minimax(0, c, Integer.MIN_VALUE, Integer.MAX_VALUE);
  163.     t.jogada(next,c);
  164.     }
  165.     public int minimax(int depth, char turn, int alpha, int beta){
  166.     if (beta<=alpha){
  167.         if (turn == c)
  168.         return Integer.MAX_VALUE;
  169.         else
  170.         return Integer.MIN_VALUE;
  171.     }
  172.    
  173.         int res = Final(t);
  174.         if(res == 512) return Integer.MAX_VALUE;
  175.         else if(res == -512) return Integer.MIN_VALUE;
  176.     else if (t.cheio() == 0) return 0;
  177.        
  178.         if(depth == maxDepth) return score(t);
  179.        
  180.         int maxScore = Integer.MIN_VALUE;
  181.     int minScore = Integer.MAX_VALUE;
  182.     //System.out.println("score "+ score(t));
  183.         for(int j=0; j<=6; ++j){
  184.             if(t.matriz[0][j]!='-')
  185.         continue;
  186.        
  187.             if(turn == c){
  188.         t.jogada (j, c);
  189.         int currentScore = minimax(depth+1, pl, alpha, beta);
  190.        
  191.         maxScore = Math.max(currentScore, maxScore);
  192.        
  193.         if(depth==0){
  194.             System.out.println("Pontuação da coluna "+j+" = "+currentScore);
  195.             if(maxScore==currentScore) next = j;
  196.                    
  197.             if(maxScore==Integer.MAX_VALUE){
  198.             t.clear(j);
  199.             break;
  200.             }
  201.             alpha = Math.max (currentScore, alpha);
  202.         }
  203.             }
  204.         else if(turn==pl){
  205.         t.jogada(j, pl);
  206.         int currentScore = minimax(depth+1, c, alpha, beta);
  207.         minScore = Math.min(currentScore, minScore);
  208.         beta = Math.min(currentScore, beta);
  209.             }
  210.             t.clear(j);
  211.         }
  212.         return turn==c ? maxScore : minScore;
  213.     }
  214.     public int score (Tabuleiro t){
  215.     //Identica a função "Final", mas verifica o contador, se estiver a 3, 2, 1 ou 0, atribuindo um pontuação - positiva para o pc e negatva para o player.
  216.     int sum = 0;
  217.     int player = 0;
  218.     int pc = 0;
  219.     //horizontal
  220.     for(int i=0; i<6; i++){
  221.         for(int j=0; j<7-3; j++){
  222.         for(int h=0; h<4; h++){
  223.             if(t.matriz[i][j+h]==c) //contador para o pc
  224.             pc++;
  225.             else if (t.matriz[i][j+h]==pl) //contador para o player
  226.             player++;
  227.         }
  228.         sum += soma(pc,player);
  229.             }
  230.     }
  231.     //vertical
  232.     pc=0;
  233.     player=0;
  234.     for(int j=0; j<7;j++){
  235.         for(int i=0; i<6-3; i++){
  236.         for(int h=0; h<4; h++){
  237.             if (t.matriz[i+h][j]==c)
  238.             pc++;
  239.             else if (t.matriz[i+h][j]==pl){
  240.             player++;
  241.             }
  242.         }
  243.         sum += soma(pc,player);
  244.         }
  245.     }
  246.     //sum += soma (pc, player);
  247.     //diagonal ascendente
  248.     pc=0;
  249.     player=0;
  250.     for(int i = 3; i<6; i++){
  251.         for(int j=0; j<7-3; j++){
  252.         for(int h=0; h<4; h++){
  253.             if(t.matriz[i-h][j+h]==c)
  254.             pc++;
  255.             else if (t.matriz[i-h][j]==pl){
  256.             player++;
  257.             }
  258.         }
  259.         sum += soma(pc,player);
  260.         }
  261.     }
  262.     //sum += soma(pc,player);
  263.     //diagonal descendente
  264.     pc=0;
  265.     player=0;
  266.     for(int i = 3; i<6; i++){
  267.         for(int j=3; j<7-3; j++){
  268.         for(int h=0; h<4; h++){
  269.             if(t.matriz[i-h][j-h]==c)
  270.             pc++;
  271.             else if (t.matriz[i-h][j-h]==pl){
  272.             player++;
  273.             }
  274.         }
  275.         sum += soma(pc,player);
  276.         }
  277.     }
  278.     return sum;
  279.    
  280.     }
  281.     public int soma (int pc, int player){
  282.     int sum = 0;
  283.     if (pc==3 && player==0) sum = 50;
  284.     if (pc==2 && player==0) sum = 10;
  285.     if (pc==1 && player==0) sum = 1;
  286.     if (pc==0 && player==3) sum = -50;
  287.     if (pc==0 && player==2) sum = -10;
  288.     if (pc==0 && player==1) sum = -1;
  289.     return sum;
  290.     }
  291.    
  292.     public void play(){
  293.     in = new Scanner (System.in);
  294.     System.out.println ("Escolha o nível de difculdade com que pretende jogar:");
  295.     System.out.println ("Fácil: 0; Médio: 1; Difícil: 2");
  296.     int d = in.nextInt();
  297.     if (d==0) maxDepth = 3;
  298.     else if (d==1) maxDepth = 5;
  299.     else maxDepth = 8;
  300.    
  301.     System.out.println ("Escolha o caracter que o vai representar, X ou O.");
  302.     pl = in.next().charAt(0);
  303.     if (pl!='O' && pl !='X'){
  304.         System.out.println("Opção inválida.");
  305.         System.out.println ("Escolha o caracter que o vai representar, X ou O.");
  306.         pl = in.next().charAt(0);
  307.     }
  308.     if (pl=='O') c = 'X';
  309.     else c = 'O';
  310.     t.imprime();
  311.     while (true){
  312.         Adversario ();
  313.         t.imprime();
  314.  
  315.         check();
  316.  
  317.         Computador ();
  318.         t.imprime();
  319.  
  320.         check ();
  321.     }
  322.     }
  323.     public static void main (String [] args){
  324.     Tabuleiro t = new Tabuleiro ();
  325.     Alpha_Beta j = new Alpha_Beta (t);
  326.     j.play();
  327.     }
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement