schlayer

BoardJava

Feb 7th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.62 KB | None | 0 0
  1. package TicTacToe;
  2.  
  3. public class Board {
  4.     public final int BLANK = 0;
  5.     public final int X = 1;
  6.     public final int O = -1;
  7.     public int movesMade = 0;
  8.    
  9.     private int[] played = new int[9];
  10.     private Square[] squares = new Square[9];
  11.    
  12.     public Board() {
  13.         for (int p: played) { p = 0; }
  14.         for (int i = 1; i <= 9; i++) {
  15.             squares[i-1] = new Square(i);
  16.         }
  17.         this.movesMade = 0;
  18.         System.out.println("This board has been created.");
  19.        
  20.     }
  21.    
  22.     public void clearSquare(int position) {
  23.         squares[position-1].clear();
  24.     }
  25.    
  26.     public void clearBoard() {
  27.         for (int p: played) { p = 0; }
  28.         for (int i = 1; i <= 9; i++) {
  29.             squares[i-1] = new Square(i);
  30.         }
  31.         this.movesMade = 0;
  32.         System.out.println("This board has been cleared.");
  33.     }
  34.    
  35.     public boolean move(int player, int position) { // plays an X or O in the designated square
  36.         assert (player == X || player == O);
  37.        
  38.         int index = position-1;
  39.         Square sq = squares[index];
  40.         if (sq.getState() != BLANK) {
  41.             System.out.println("That square is occupied!");
  42.             return false;
  43.         }
  44.        
  45.         if (player == X) {
  46.             sq.playX();
  47.             System.out.println("X in square " + position);
  48.         }
  49.         if (player == O) {
  50.             sq.playO();
  51.             System.out.println("O in square " + position);
  52.         }
  53.        
  54.         movesMade ++;
  55.         return true;
  56.     }
  57.    
  58.     public Board result(int player, int position) { // plays an X or O in the designated square
  59.         assert (player == X || player == O);
  60.        
  61.         int index = position-1;
  62.         Square sq = squares[index];
  63.         if (sq.getState() != BLANK) {
  64.             System.out.println("That square is occupied!");
  65.             return null;
  66.         }
  67.        
  68.         if (player == X) {
  69.             sq.playX();
  70.             //System.out.println("X in square " + position);
  71.         }
  72.         if (player == O) {
  73.             sq.playO();
  74.             //System.out.println("O in square " + position);
  75.         }
  76.        
  77.         movesMade ++;
  78.         return this;
  79.     }
  80.    
  81.     public int[] getAllStates() { // Returns an array of all spaces' (0-8) contents
  82.         int[] states = new int[9];
  83.         for (int i = 0; i < 9; i++) {
  84.             int st = squares[i].getState();
  85.             assert st == X || st == O || st == BLANK; // Ensure win conditions will work
  86.             states[i] = st;
  87.         }
  88.        
  89.         return states;
  90.     }
  91.    
  92.     public int[] emptySquares() { // Returns an array of all empty positions (possible actions)
  93.         int empty = 9 - movesMade;
  94.         if (empty == 0) return null;
  95.        
  96.         int[] blanks = new int[empty];
  97.         int index = 0;
  98.         for (Square sq: squares) {
  99.             if (sq.getState() == BLANK) {
  100.                 blanks[index] = sq.getPosition();
  101.                 index ++;
  102.             }
  103.         }
  104.        
  105.         return blanks;
  106.     }
  107.    
  108.     public String toString() { // Create a string to represent the whole board
  109.         String str = "";
  110.         //str += "TicTacToe";
  111.         for (int i = 1; i <= 9; i++) {
  112.             int sq = i-1;
  113.             if (i == 1 || i == 4 || i == 7) str += "\n|" + squares[sq].print() + "|";
  114.             else str += squares[sq].print() + "|";  
  115.         }
  116.        
  117.         return str + "\n";
  118.     }
  119.    
  120.     public void showBoard() { // Print the whole board
  121.         System.out.println(this.toString());
  122.     }
  123.    
  124.     public int whoWon() { // Returns O if O won, X if X won, or BLANK is nobody has won
  125.         // All sets of three: ROWS 0,1,2; 3,4,5; 6,7,8; COLUMNS 0,3,6; 1,4,7; 2,5,8; DIAGONALS 0,4,8; 2,4,6
  126.         // Adding the lines of three will give 3 if X wins, -3 if O wins
  127.        
  128.         int[] T = getAllStates();
  129.         int center = T[4];
  130.        
  131.         if (center != BLANK) { // Check diagonals
  132.             if (center + T[0] + T[8] == 3)      { return X; }   // NW to SE
  133.             if (center + T[0] + T[8] == -3)     { return O; }
  134.             if (center + T[2] + T[6] == 3)      { return X; }   // NE to SW
  135.             if (center + T[2] + T[6] == -3)     { return O; }
  136.         }
  137.        
  138.         for (int s = 0; s <= 6; s += 3) {
  139.             if (T[s] + T[s+1] + T[s+2] == 3)    { return X; }   // Horizontal
  140.             if (T[s] + T[s+1] + T[s+2] == -3)   { return O; }
  141.         }
  142.        
  143.         for (int s = 0; s <= 2; s ++) {
  144.             if (T[s] + T[s+3] + T[s+6] == 3)    { return X; }   // Vertical
  145.             if (T[s] + T[s+3] + T[s+6] == -3)   { return O; }
  146.         }
  147.        
  148.         System.out.println("Nobody has won yet...");
  149.         return BLANK;
  150.     }
  151.    
  152.     public int numTwos(int player) { // Returns the number of twos on the board for the given player
  153.         // All sets of three: ROWS 0,1,2; 3,4,5; 6,7,8; COLUMNS 0,3,6; 1,4,7; 2,5,8; DIAGONALS 0,4,8; 2,4,6
  154.         // Adding the lines of three will give 2 if X has two, -2 if O has two
  155.         assert (player == X || player == O);
  156.         if (movesMade < 3) { return 0; } // No twos can exist unless 3 moves were made.
  157.        
  158.         int twos = 0;
  159.         int want = 2*player; // -2 for O, 2 for X
  160.        
  161.         int[] T = getAllStates();
  162.        
  163.         // Check diagonals
  164.         if (T[4] + T[0] + T[8] == want)     { twos ++; }    // NW to SE
  165.         if (T[4] + T[2] + T[6] == want)     { twos ++; }    // NE to SW
  166.  
  167.         for (int s = 0; s <= 6; s += 3) {
  168.             if (T[s] + T[s+1] + T[s+2] == want)     { twos ++; }    // Horizontal
  169.         }
  170.        
  171.         for (int s = 0; s <= 2; s ++) {
  172.             if (T[s] + T[s+3] + T[s+6] == want)     { twos ++; }    // Vertical
  173.         }
  174.        
  175.         String pstr = (player == X) ? "X" : "O";
  176.         System.out.println(pstr + " has " + twos + " twos.");
  177.         return twos;
  178.     }
  179.    
  180.     public int whereToBlock(int opponent) { // Returns 0 if enemy has no twos, or a position to block them.
  181.         // All sets of three: ROWS 0,1,2; 3,4,5; 6,7,8; COLUMNS 0,3,6; 1,4,7; 2,5,8; DIAGONALS 0,4,8; 2,4,6
  182.         // Adding the lines of three will give 2 if X has two, -2 if O has two
  183.         assert (opponent == X || opponent == O);
  184.         if (movesMade < 3) { return 0; } // No twos can exist unless 3 moves were made.
  185.        
  186.         int enemyTwos = 0;
  187.         int want = 2*opponent; // 2 for O, -2 for X
  188.         int blockPos = 0;
  189.  
  190.         int[] T = getAllStates();
  191.  
  192.         // Check diagonals
  193.         if (T[0] + T[4] + T[8] == want) { // NW to SE
  194.             enemyTwos ++;
  195.             for (int k = 0; k <= 8; k += 4) { // find the blank, set blockPos to that position
  196.                 if (T[k] == BLANK) { blockPos = k+1; break; }
  197.             }
  198.         }  
  199.         if (T[2] + T[4] + T[6] == want) { // NE to SW
  200.             enemyTwos ++;
  201.             for (int k = 0; k <= 6; k += 2) { // find the blank, set blockPos to that position
  202.                 if (T[k] == BLANK) { blockPos = k+1; break; }
  203.             }
  204.         }  
  205.  
  206.         for (int s = 0; s <= 6; s += 3) { // Horizontal
  207.             if (T[s] + T[s+1] + T[s+2] == want) {
  208.                 enemyTwos ++;
  209.                 for (int k = s; k <= s+2; k += 1) { // find the blank, set blockPos to that position
  210.                     if (T[k] == BLANK) { blockPos = k+1; break; }
  211.                 }
  212.             }  
  213.         }
  214.  
  215.         for (int s = 0; s <= 2; s ++) { // Vertical
  216.             if (T[s] + T[s+3] + T[s+6] == want) {
  217.                 enemyTwos ++;
  218.                 for (int k = s; k <= s+6; k += 3) { // find the blank, set blockPos to that position
  219.                     if (T[k] == BLANK) { blockPos = k+1; break; }
  220.                 }
  221.             }  
  222.         }
  223.        
  224.         String pstr = (opponent == X) ? "X" : "O";
  225.         System.out.println(pstr + " has " + enemyTwos + " twos.");
  226.         if (enemyTwos < 1) { return 0; }
  227.         else {
  228.             System.out.println("Must block at position " + blockPos + "!");
  229.         }
  230.  
  231.         return blockPos;
  232.     }
  233.    
  234.    
  235. }
Advertisement
Add Comment
Please, Sign In to add comment