Advertisement
beary605

OmokBase.java

Jul 12th, 2014
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.85 KB | None | 0 0
  1. package omok;
  2.  
  3. public class OmokBase {
  4.     protected final short[] cacherow = new short[15];
  5.     protected final short[] rows = new short[15];
  6.     protected final short[] cachecolumn = new short[15];
  7.     protected final short[] columns = new short[15];
  8.     protected final short[] cacherdiag = new short[21];
  9.     protected final short[] rdiags = new short[21];
  10.     protected final short[] cacheldiag = new short[21];
  11.     protected final short[] ldiags = new short[21];
  12.     protected OmokBase() {
  13.     }
  14.  
  15.     /**
  16.      * Returns the value stored at the coordinate (x, y) on the rows.
  17.      * @param x The x-coordinate value.
  18.      * @param y The y-coordinate value.
  19.      * @return The value stored at (x, y)
  20.      */
  21.     public final byte get(int x, int y) {
  22.         return (byte)((this.rows[y]&(1<<x)) >> x);
  23.     }
  24.    
  25.     public final byte getcache(int x, int y) {
  26.         return (byte)((this.cacherow[y]&(1<<x)) >> x);
  27.     }        
  28.  
  29.     /**
  30.      * Set the value at the coordinate (x, y) on the rows.
  31.      * @param x The x-coordinate value.
  32.      * @param y The y-coordinate value.
  33.      * @param value The value to set. (Generally it is the piece ID usually 1 or 2 depending on the player)
  34.      */
  35.     public final void set(int x, int y, byte value) {
  36.         if (x < 15 && y < 15) {
  37.             this.cacherow[y] |= 1 << x;
  38.             this.cachecolumn[x] |= 1 << y;
  39.            
  40.             int rdindex = y-x+10; //r diag index
  41.             int ldindex = x+y-4; //l diag index
  42.             if (-1 < rdindex && rdindex < 21) {
  43.                 int rdindey = (y >= x) ? x : y;
  44.                 this.cacherdiag[rdindex] |= 1 << rdindey;
  45.                 if (value == (byte)1) {
  46.                     this.rdiags[rdindex] |= 1 << rdindey;
  47.                 } else {
  48.                     this.rdiags[rdindex] &= ~(1 << rdindey);
  49.                 }
  50.             }
  51.            
  52.             if (-1 < ldindex && ldindex < 21) {
  53.                 int ldindey = (x+y >= 14) ? (14-x) : y;
  54.                 this.cacheldiag[ldindex] |= 1 << ldindey;
  55.                 if (value == (byte)1) {
  56.                     this.ldiags[ldindex] |= 1 << ldindey;
  57.                 } else {
  58.                     this.ldiags[ldindex] &= ~(1 << ldindey);
  59.                 }
  60.             }
  61.            
  62.             if (value == (byte)1) {
  63.                 this.rows[y] |= 1 << x;
  64.                 this.columns[x] |= 1 << y;
  65.             } else {
  66.                 this.rows[y] &= ~(1 << x);
  67.                 this.columns[x] &= ~(1 << y);
  68.             }
  69.         } else {
  70.             throw new IllegalArgumentException("(" + x + ", " + y + ") is not a valid coordinate.");
  71.         }
  72.     }
  73.  
  74.     /**
  75.      * Resets the entire rows to value 0.
  76.      */
  77.     public final void resetBoard() {
  78.         for (int i = 0; i < rows.length; i++) {
  79.             cacherow[i] = 0;
  80.             rows[i] = 0;
  81.             cachecolumn[i] = 0;
  82.             columns[i] = 0;
  83.         }
  84.        
  85.         for (int i = 0; i < cacherdiag.length; i++) {
  86.             cacherdiag[i] = 0;
  87.             rdiags[i] = 0;
  88.             cacheldiag[i] = 0;
  89.             ldiags[i] = 0;
  90.         }
  91.     }
  92.  
  93.     /**
  94.      * Prints the view of the rows to standard out.
  95.      */
  96.     public final void printBoard() {
  97.         StringBuilder sb = new StringBuilder("rows: ");
  98.         sb.append(rows.length);
  99.         if (rows.length > 0) {
  100.             sb.append(" x ");
  101.             sb.append(15); //rows[0].length
  102.         } else {
  103.             sb.append(" x 0");
  104.         }
  105.         sb.append("\n");
  106.         for (int i = 0; i < rows.length; i++) {
  107.             for(int j=0; j< 15; j++) {
  108.                 if (getcache(i, j) == (byte)1 ) {
  109.                     sb.append(Byte.toString(get(i, j)));
  110.                 } else {
  111.                     sb.append("x");
  112.                 }
  113.             }
  114.             sb.append("\n");
  115.         }
  116.         System.out.println(sb);
  117.     }
  118.  
  119.     /**
  120.      * Returns <code>true</code> if a player has connect 5 pieces horizontally, vertically, or diagonally.
  121.      * @param type The piece ID (Generally 1 or 2 depending on the player)
  122.      * @return <code>true</code> if the specified type is a winner.
  123.      */
  124.     public boolean checkWinner(byte type) {
  125.         short row;
  126.         for(int i=0; i<15; i++) {
  127.             if (type == (byte)1) { row = (short)(rows[i] & cacherow[i]); }
  128.             else { row = (short)(~rows[i] & cacherow[i]); }
  129.            
  130.             if (row == 0) { continue; }
  131.             row &= row >> 1;
  132.             row &= row >> 1;
  133.             row &= row >> 1;
  134.             row &= row >> 1;
  135.             if (row != 0) { return true; }
  136.         }
  137.        
  138.         for(int i=0; i<15; i++) {
  139.             if (type == (byte)1) { row = (short)(columns[i] & cachecolumn[i]); }
  140.             else { row = (short)(~columns[i] & cachecolumn[i]); }
  141.            
  142.             if (row == 0) { continue; }
  143.             row &= row >> 1;
  144.             row &= row >> 1;
  145.             row &= row >> 1;
  146.             row &= row >> 1;
  147.             if (row != 0) { return true; }
  148.         }
  149.        
  150.         for(int i=0; i<21; i++) {
  151.             if (type == (byte)1) { row = (short)(rdiags[i] & cacherdiag[i]); }
  152.             else { row = (short)(~rdiags[i] & cacherdiag[i]); }
  153.            
  154.             if (row == 0) { continue; }
  155.             row &= row >> 1;
  156.             row &= row >> 1;
  157.             row &= row >> 1;
  158.             row &= row >> 1;
  159.             if (row != 0) { return true; }
  160.         }
  161.        
  162.         for(int i=0; i<21; i++) {
  163.             if (type == (byte)1) { row = (short)(ldiags[i] & cacheldiag[i]); }
  164.             else { row = (short)(~ldiags[i] & cacheldiag[i]); }
  165.            
  166.             if (row == 0) { continue; }
  167.             row &= row >> 1;
  168.             row &= row >> 1;
  169.             row &= row >> 1;
  170.             row &= row >> 1;
  171.             if (row != 0) { return true; }
  172.         }
  173.         return false;
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement