Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.99 KB | None | 0 0
  1. package conectfinal;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. import conectfinal.HBrain;
  8. import sac.game.AlphaBetaPruning;
  9. import sac.game.GameSearchAlgorithm;
  10. import sac.game.GameState;
  11. import sac.game.MinMax;
  12.  
  13. public class Connect extends sac.game.GameStateImpl {
  14.  
  15.     public static final int n = 8; // wiersze
  16.     public static final int m = 8; // kolumny
  17.     public static final int pustyZnak = 0; // " "
  18.     public static final byte playerZnak = 1; // O
  19.     public static final byte komputerZnak = 2; // X
  20.     public int wybor = 0;
  21.  
  22.     public byte[][] board = null;
  23.  
  24.     public Connect() {
  25.         setMaximizingTurnNow(maximizingTurnNow);
  26.         board = new byte[n][m];
  27.         for (int i = 0; i < n; i++) {
  28.             for (int j = 0; j < m; j++) {
  29.                 board[i][j] = pustyZnak;
  30.             }
  31.         }
  32.     }
  33.  
  34.     public Connect(Connect x) {
  35.         setMaximizingTurnNow(x.isMaximizingTurnNow());
  36.         board = new byte[n][m];
  37.         for (int i = 0; i < n; i++) {
  38.             for (int j = 0; j < m; j++) {
  39.                 this.board[i][j] = x.board[i][j];
  40.             }
  41.         }
  42.     }
  43.  
  44.     public boolean insert(int x) {
  45.         if (x > m || x <= 0)
  46.             return false;
  47.         x = x - 1;
  48.         for (int i = 0; i < n; i++) {
  49.             if (board[(n - 1) - i][x] == pustyZnak) {
  50.                 if (isMaximizingTurnNow()) {
  51.                     board[(n - 1) - i][x] = playerZnak;
  52.                 } else {
  53.                     board[(n - 1) - i][x] = komputerZnak;
  54.                 }
  55.                 setMaximizingTurnNow(!isMaximizingTurnNow());
  56.                 return true;
  57.             }
  58.         }
  59.         return false;
  60.     }
  61.  
  62.     public boolean check(int x) {
  63.         int win = 0;
  64.         // poziom
  65.         for (int i = 0; i < n; i++) {
  66.             for (int j = 0; j < m; j++) {
  67.                 if (x == 1) {
  68.                     if (board[i][j] == 1)
  69.                         win++;
  70.                     else
  71.                         win = 0;
  72.                     if (win == 4)
  73.                         return true;
  74.                 }
  75.  
  76.                 if (x == 2) {
  77.                     if (board[i][j] == 2)
  78.                         win++;
  79.                     else
  80.                         win = 0;
  81.                     if (win == 4)
  82.                         return true;
  83.                 }
  84.             }
  85.             win = 0;
  86.         }
  87.  
  88.         win = 0;
  89.         // pion
  90.         for (int i = 0; i < m; i++) {
  91.             for (int j = 0; j < n; j++) {
  92.                 if (x == 1) {
  93.                     if (board[j][i] == 1)
  94.                         win++;
  95.                     else
  96.                         win = 0;
  97.                     if (win == 4)
  98.                         return true;
  99.                 }
  100.  
  101.                 if (x == 2) {
  102.                     if (board[j][i] == 2)
  103.                         win++;
  104.                     else
  105.                         win = 0;
  106.                     if (win == 4)
  107.                         return true;
  108.                 }
  109.             }
  110.             win = 0;
  111.         }
  112.  
  113.         // skos z gory na prawo
  114.         for (int i = 0; i < n; i++) {
  115.             for (int j = 0; j < m; j++) {
  116.                 if (x == 1) {
  117.                     if (board[i][j] == 1)
  118.                         if (i + 3 < n && j + 3 < m)
  119.                             if (board[i + 1][j + 1] == 1)
  120.                                 if (board[i + 2][j + 2] == 1)
  121.                                     if (board[i + 3][j + 3] == 1)
  122.                                         return true;
  123.                 }
  124.  
  125.                 if (x == 2) {
  126.                     if (board[i][j] == 2)
  127.                         if (i + 3 < n && j + 3 < m)
  128.                             if (board[i + 1][j + 1] == 2)
  129.                                 if (board[i + 2][j + 2] == 2)
  130.                                     if (board[i + 3][j + 3] == 2)
  131.                                         return true;
  132.                 }
  133.             }
  134.         }
  135.  
  136.         // skos z gory na lewo
  137.         for (int i = 0; i < n; i++) {
  138.             for (int j = 0; j < m; j++) {
  139.                 if (x == 1) {
  140.                     if (board[i][j] == 1)
  141.                         if (i + 3 < n && j - 3 >= 0)
  142.                             if (board[i + 1][j - 1] == 1)
  143.                                 if (board[i + 2][j - 2] == 1)
  144.                                     if (board[i + 3][j - 3] == 1)
  145.                                         return true;
  146.  
  147.                 }
  148.                 if (board[i][j] == 2)
  149.                     if (i + 3 < n && j - 3 >= 0)
  150.                         if (board[i + 1][j - 1] == 2)
  151.                             if (board[i + 2][j - 2] == 2)
  152.                                 if (board[i + 3][j - 3] == 2)
  153.                                     return true;
  154.             }
  155.         }
  156.         // sufit
  157.         for (int i = 0; i < m; i++) {
  158.             if (x == 1) {
  159.                 if (board[0][i] == 1)
  160.                     return true;
  161.             }
  162.             if (x == 2)
  163.                 if (board[0][i] == 2)
  164.                     return true;
  165.         }
  166.         return false;
  167.     }
  168.  
  169.     @Override
  170.     public String toString() {
  171.         String show = "";
  172.  
  173.         for (int i = 0; i < n; i++) {
  174.             for (int j = 0; j < m; j++) {
  175.                 if (board[i][j] == pustyZnak)
  176.                     show += " " + "|";
  177.                 else if (board[i][j] == playerZnak)
  178.                     show += "O" + "|";
  179.                 else if (board[i][j] == komputerZnak)
  180.                     show += "X" + "|";
  181.             }
  182.             show += "\n";
  183.         }
  184.         for (int i = 0; i < m; i++) {
  185.             show += (i + 1) + " ";
  186.         }
  187.         return show;
  188.     }
  189.  
  190.     @Override
  191.     public List<GameState> generateChildren() {
  192.         List<GameState> children = new ArrayList<GameState>();
  193.         for (int i = 0; i < n; i++) {
  194.             Connect child = new Connect(this);
  195.             if (child.insert(i)) {
  196.                 child.setMoveName("" + i);
  197.                 children.add(child);
  198.             }
  199.         }
  200.         return children;
  201.     }
  202.  
  203.     @Override
  204.     public int hashCode() {
  205.         return toString().hashCode();
  206.     }
  207.  
  208.     public static void main(String[] args) {
  209.         Connect a = new Connect();
  210.         Connect.setHFunction(new HBrain());
  211.         GameSearchAlgorithm alg = new AlphaBetaPruning(a);
  212.  
  213.         int x;
  214.         String y;
  215.         Scanner scanIn = new Scanner(System.in);
  216.  
  217.         System.out.println("Kto zaczyna ?");
  218.         System.out.println("1: Gracz");
  219.         System.out.println("2: Komputer");
  220.         a.wybor = Integer.parseInt(scanIn.nextLine());
  221.  
  222.         System.out.println(a);
  223.  
  224.         if (a.wybor == 1) {
  225.             for (;;) {
  226.                 System.out.println();
  227.                 System.out.print("Podaj numer kolumny ");
  228.  
  229.                 x = Integer.parseInt(scanIn.nextLine());
  230.                 a.insert(x);
  231.  
  232.                 if (a.check(1)) {
  233.                    
  234.                     System.out.println("Gratulacje, wygrales !");
  235.                     break;
  236.                 }
  237.  
  238.                 alg.execute();
  239.                 System.out.println(alg.getMovesScores());
  240.                 x = Integer.parseInt(alg.getFirstBestMove());
  241.                 a.insert(x);
  242.                 System.out.println(new HBrain().calculate(a));
  243.  
  244.                 System.out.println(a);
  245.  
  246.                 if (a.check(2)) {
  247.                     System.out.println("Przegrales, sprobuj jeszcze raz :) !");
  248.                     break;
  249.                 }
  250.  
  251.             }
  252.  
  253.         }
  254.  
  255.         if (a.wybor == 2) {
  256.             for (;;) {
  257.                 alg.execute();
  258.                 System.out.println(alg.getMovesScores());
  259.                 x = Integer.parseInt(alg.getFirstBestMove());
  260.            
  261.                 a.insert(x);
  262.                 System.out.println(new HBrain().calculate(a));
  263.  
  264.                 System.out.println(a);
  265.  
  266.                 if (a.check(1)) {
  267.                     System.out.println("Przegrales, sprobuj jeszcze raz :) !");
  268.                     break;
  269.                 }
  270.  
  271.                 System.out.println();
  272.                 System.out.print("Podaj numer kolumny ");
  273.  
  274.                 x = Integer.parseInt(scanIn.nextLine());
  275.                 a.insert(x);
  276.  
  277.                 if (a.check(2)) {
  278.                     System.out.println(a);
  279.                     System.out.println("Gratulacje, wygrales !");
  280.                     break;
  281.                 }
  282.  
  283.             }
  284.         }
  285.  
  286.     }
  287.  
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement