Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package omok;
- public class OmokBase {
- protected final short[] cacherow = new short[15];
- protected final short[] rows = new short[15];
- protected final short[] cachecolumn = new short[15];
- protected final short[] columns = new short[15];
- protected final short[] cacherdiag = new short[21];
- protected final short[] rdiags = new short[21];
- protected final short[] cacheldiag = new short[21];
- protected final short[] ldiags = new short[21];
- protected OmokBase() {
- }
- /**
- * Returns the value stored at the coordinate (x, y) on the rows.
- * @param x The x-coordinate value.
- * @param y The y-coordinate value.
- * @return The value stored at (x, y)
- */
- public final byte get(int x, int y) {
- return (byte)((this.rows[y]&(1<<x)) >> x);
- }
- public final byte getcache(int x, int y) {
- return (byte)((this.cacherow[y]&(1<<x)) >> x);
- }
- /**
- * Set the value at the coordinate (x, y) on the rows.
- * @param x The x-coordinate value.
- * @param y The y-coordinate value.
- * @param value The value to set. (Generally it is the piece ID usually 1 or 2 depending on the player)
- */
- public final void set(int x, int y, byte value) {
- if (x < 15 && y < 15) {
- this.cacherow[y] |= 1 << x;
- this.cachecolumn[x] |= 1 << y;
- int rdindex = y-x+10; //r diag index
- int ldindex = x+y-4; //l diag index
- if (-1 < rdindex && rdindex < 21) {
- int rdindey = (y >= x) ? x : y;
- this.cacherdiag[rdindex] |= 1 << rdindey;
- if (value == (byte)1) {
- this.rdiags[rdindex] |= 1 << rdindey;
- } else {
- this.rdiags[rdindex] &= ~(1 << rdindey);
- }
- }
- if (-1 < ldindex && ldindex < 21) {
- int ldindey = (x+y >= 14) ? (14-x) : y;
- this.cacheldiag[ldindex] |= 1 << ldindey;
- if (value == (byte)1) {
- this.ldiags[ldindex] |= 1 << ldindey;
- } else {
- this.ldiags[ldindex] &= ~(1 << ldindey);
- }
- }
- if (value == (byte)1) {
- this.rows[y] |= 1 << x;
- this.columns[x] |= 1 << y;
- } else {
- this.rows[y] &= ~(1 << x);
- this.columns[x] &= ~(1 << y);
- }
- } else {
- throw new IllegalArgumentException("(" + x + ", " + y + ") is not a valid coordinate.");
- }
- }
- /**
- * Resets the entire rows to value 0.
- */
- public final void resetBoard() {
- for (int i = 0; i < rows.length; i++) {
- cacherow[i] = 0;
- rows[i] = 0;
- cachecolumn[i] = 0;
- columns[i] = 0;
- }
- for (int i = 0; i < cacherdiag.length; i++) {
- cacherdiag[i] = 0;
- rdiags[i] = 0;
- cacheldiag[i] = 0;
- ldiags[i] = 0;
- }
- }
- /**
- * Prints the view of the rows to standard out.
- */
- public final void printBoard() {
- StringBuilder sb = new StringBuilder("rows: ");
- sb.append(rows.length);
- if (rows.length > 0) {
- sb.append(" x ");
- sb.append(15); //rows[0].length
- } else {
- sb.append(" x 0");
- }
- sb.append("\n");
- for (int i = 0; i < rows.length; i++) {
- for(int j=0; j< 15; j++) {
- if (getcache(i, j) == (byte)1 ) {
- sb.append(Byte.toString(get(i, j)));
- } else {
- sb.append("x");
- }
- }
- sb.append("\n");
- }
- System.out.println(sb);
- }
- /**
- * Returns <code>true</code> if a player has connect 5 pieces horizontally, vertically, or diagonally.
- * @param type The piece ID (Generally 1 or 2 depending on the player)
- * @return <code>true</code> if the specified type is a winner.
- */
- public boolean checkWinner(byte type) {
- short row;
- for(int i=0; i<15; i++) {
- if (type == (byte)1) { row = (short)(rows[i] & cacherow[i]); }
- else { row = (short)(~rows[i] & cacherow[i]); }
- if (row == 0) { continue; }
- row &= row >> 1;
- row &= row >> 1;
- row &= row >> 1;
- row &= row >> 1;
- if (row != 0) { return true; }
- }
- for(int i=0; i<15; i++) {
- if (type == (byte)1) { row = (short)(columns[i] & cachecolumn[i]); }
- else { row = (short)(~columns[i] & cachecolumn[i]); }
- if (row == 0) { continue; }
- row &= row >> 1;
- row &= row >> 1;
- row &= row >> 1;
- row &= row >> 1;
- if (row != 0) { return true; }
- }
- for(int i=0; i<21; i++) {
- if (type == (byte)1) { row = (short)(rdiags[i] & cacherdiag[i]); }
- else { row = (short)(~rdiags[i] & cacherdiag[i]); }
- if (row == 0) { continue; }
- row &= row >> 1;
- row &= row >> 1;
- row &= row >> 1;
- row &= row >> 1;
- if (row != 0) { return true; }
- }
- for(int i=0; i<21; i++) {
- if (type == (byte)1) { row = (short)(ldiags[i] & cacheldiag[i]); }
- else { row = (short)(~ldiags[i] & cacheldiag[i]); }
- if (row == 0) { continue; }
- row &= row >> 1;
- row &= row >> 1;
- row &= row >> 1;
- row &= row >> 1;
- if (row != 0) { return true; }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement