/* * Othello2.java * * Created on 21. Oktober 2004, 22:20 */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.lang.*; import java.util.*; public class Othello2 extends JFrame implements ActionListener { /** Creates new form Othello2 */ public Othello2() { wClock = new Stopwatch(); bClock = new Stopwatch(); initComponents(); whiteIcon = createIcon("images/white.gif"); blackIcon = createIcon("images/black.gif"); picture.setIcon(createBoard()); Matrix(boardSize); possibleMoves(getPlayer()); wClock.start(); repaint(); //Register a listener for the game controls. jButton1.addActionListener(this); jButton2.addActionListener(this); } /** Returns an ImageIcon, or null if the path was invalid. */ private ImageIcon createBoard() { String path = "images/8x8.gif"; java.net.URL imgURL = Othello2.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } } private ImageIcon createIcon(String path) { java.net.URL imgURL = Othello2.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } } private Dimension setWindowSize() { int pad_x = 275; int pad_y = 100; Dimension d = new Dimension(DIM + pad_x, DIM + pad_y); return d; } private void setWScore(int wS) {wScore = wS;} private void setBScore(int bS) {bScore = bS;} public void paint(Graphics g) { int addX=0, addY=0; addX = 70; addY = 118;//118; jLabel10.setText(wClock.getTimeString(wClock.getSeconds())); jLabel12.setText(bClock.getTimeString(bClock.getSeconds())); if (whiteIcon != null && blackIcon != null) { double x = (picture.getWidth() - (2 * cutoffX)) / boardSize; double y = (picture.getHeight() - (2 * cutoffY)) / boardSize; for (int i = 0; i < boardSize; ++i) { for (int j = 0; j < boardSize; ++j) { if (boardMatrix[i][j] == WHITE) whiteIcon.paintIcon(picture, g, (int)(x * i) + addX, (int)(y * j) + addY); else if (boardMatrix[i][j] == BLACK) blackIcon.paintIcon(picture, g, (int)(x * i) + addX, (int)(y * j) + addY); } } } } private void Matrix(int bs) { boardMatrix = new int[bs][bs]; for (int i = 0; i != bs; i++) { for (int j = 0; j != bs; j++) { // first part of the number (x) encodes the occupation of the field (0,1,2), second part (= y)the scoring (xyy) // black if (i == bs/2 - 1 && j == bs/2 - 1 || i == (bs/2) && j == (bs/2) ) { boardMatrix [i][j] = BLACK ; // white } else if (i == bs/2 - 1 && j == (bs/2) || i == (bs/2) && j == (bs/2 - 1) ) { boardMatrix [i][j] = WHITE; } else { // not occupied boardMatrix [i][j] = EMPTY; } } } } // checking matrix for possible moves and save them in an array private int possibleMoves(boolean pl) { int m=0, n=0, player = 0, opponent=0, q=0; Vector flipV = new Vector(); Direction dir = new Direction(); int numMoves = 0; if (pl) { player = WHITE; opponent = BLACK; } else { player = BLACK; opponent = WHITE; } // setting the player matrix and the flip matrix to 0 / null for (int i = 0; i != boardSize; i++) { for (int j = 0; j != boardSize; j++) { playerMatrix[i][j] = 0; flipM[i][j] = null; } } for (int i = 0; i != boardSize; i++) { for (int j = 0; j != boardSize; j++) { if (boardMatrix[i][j] == EMPTY){ // column going down if(i < boardSize - 1 && boardMatrix[i+1][j] == opponent) { n = 0; for (m = i+2; m != boardSize; m++) { if(boardMatrix[m][j] == player && n == 0) { n = 1; playerMatrix[i][j] = POSSIBLE; // saving direactions dir.dx = 0; dir.dy = 1; flipV.add(dir); } else if( boardMatrix[m][j]== EMPTY && n == 0) { n = 1; } } } // column going up if(i > 1 && boardMatrix[i-1][j] == opponent) { n = 0; for (m = i-2; m >= 0; m--) { if(boardMatrix[m][j] == player && n == 0) { n = 1;; playerMatrix[i][j] = POSSIBLE; // saving direactions dir.dx = 0; dir.dy = -1; flipV.add(dir); } else if( boardMatrix[m][j]== EMPTY && n == 0) { n = 1; } } } // row going right if(j < boardSize - 1 && boardMatrix[i][j+1] == opponent) { n = 0; for (m = j+2; m != boardSize; m++) { if(boardMatrix[i][m] == player && n == 0) { n = 1; playerMatrix[i][j] = POSSIBLE; // saving direactions dir.dx = 1; dir.dy = 0; flipV.add(dir); } else if( boardMatrix[i][m]== EMPTY && n == 0) { n = 1; } } } // row going left if(j > 1 && boardMatrix[i][j-1] == opponent) { n = 0; for (m = j-2; m >= 0; m--) { if(boardMatrix[i][m] == player && n == 0) { n = 1; playerMatrix[i][j] = POSSIBLE; // saving direactions dir.dx = -1; dir.dy = 0; flipV.add(dir); } else if( boardMatrix[i][m]== EMPTY && n == 0) { n = 1; } } } // diagonal from upper left to lower right if(i < boardSize - 1 && j < boardSize - 1 && boardMatrix[i+1][j+1] == opponent) { n = 0; if(i>j) { q = j+2; for (m = i+2; m != boardSize; m++){ if(boardMatrix[m][q] == player && n == 0 ){ n = 1; playerMatrix[i][j] = POSSIBLE; // saving direactions dir.dx = 1; dir.dy = 1; flipV.add(dir); } else if(boardMatrix[m][q]== EMPTY && n == 0){ n = 1; } q = q + 1; } } else { m = i + 2; for (q = j+2; q != boardSize; q++) { if(boardMatrix[m][q] == player && n ==0) { n = 1; playerMatrix[i][j] = POSSIBLE; // saving direactions dir.dx = 1; dir.dy = 1; flipV.add(dir); } else if(boardMatrix[m][q] == EMPTY && n == 0) { n = 1; } m = m + 1; } } } // diagonal from lower left to upper right if(i > 1 && j < boardSize - 1 && boardMatrix[i-1][j+1] == opponent) { n = 0; if(i+j >= 7) { m = i-2; for (q = j+2; q != boardSize; q++){ if(boardMatrix[m][q] == player && n == 0 ){ n = 1; playerMatrix[i][j] = POSSIBLE; // saving direactions dir.dx = 1; dir.dy = -1; flipV.add(dir); } else if(boardMatrix[m][q]== EMPTY && n == 0){ n = 1; } m = m - 1; } } else { q = j + 2; for (m = i-2; m >= 0; m--) { if(boardMatrix[m][q] == player && n == 0) { n = 1; playerMatrix[i][j] = POSSIBLE; // saving direactions dir.dx = 1; dir.dy = -1; flipV.add(dir); } else if(boardMatrix[m][q] == EMPTY && n == 0) { n = 1; } q = q + 1; } } } // diagonal from lower right to upper left if(i > 1 && j > 1 && boardMatrix[i-1][j-1] == opponent) { n = 0; if(i>=j) { m = i-2; for (q = j-2; q >= 0; q--){ if(boardMatrix[m][q] == player && n == 0 ){ n = 1; playerMatrix[i][j] = POSSIBLE; // saving direactions dir.dx = -1; dir.dy = -1; flipV.add(dir); } else if(boardMatrix[m][q]== EMPTY && n == 0){ n = 1; } m = m - 1; } } else { q = j - 2; for (m = i-2; m >= 0; m--) { if(boardMatrix[m][q] == player && n ==0) { n = 1; playerMatrix[i][j] = POSSIBLE; // saving direactions dir.dx = -1; dir.dy = -1; flipV.add(dir); } else if(boardMatrix[m][q] == EMPTY && n == 0) { n = 1; } q = q - 1; } } } // diagonal from upper right to lower left if(i < boardSize - 1 && j > 1 && boardMatrix[i+1][j-1] == opponent) { n = 0; if(i+j>=7) { q = j-2; for (m = i+2; m != boardSize; m++){ if(boardMatrix[m][q] == player && n == 0 ){ n = 1; playerMatrix[i][j] = POSSIBLE; // saving direactions dir.dx = -1; dir.dy = 1; flipV.add(dir); } else if(boardMatrix[m][q]== EMPTY && n == 0){ n = 1; } q = q - 1; } } else { m = i + 2; for (q = j-2; q >= 0; q--) { if(boardMatrix[m][q] == player && n ==0) { n = 1; playerMatrix[i][j] = POSSIBLE; // saving direactions dir.dx = -1; dir.dy = 1; flipV.add(dir); } else if(boardMatrix[m][q] == EMPTY && n == 0) { n = 1; } m = m + 1; } } } //System.out.println("flipVsave:"+ flipV.size()); flipM[i][j] = flipV; if(flipV.size() != 0) numMoves++; flipV = new Vector(); } } } return numMoves; } // x any y hold the position that was clicked public void flip(boolean pl, int X, int Y) { int m=0, n=0, p=0, player = 0, opponent=0, q=0; if (pl) { player = WHITE; opponent = BLACK; System.out.println("Player: " + player); } else { player = BLACK; opponent = WHITE; System.out.println("Player: " + player); } if ( X <= boardSize && X >= 0 && Y <= boardSize && Y >= 0) { // checking and maybe changing horizontally from left to right if (X < boardSize - 1) { if (boardMatrix[X+1][Y] == opponent) { n = 0; p = 0; for (m = X+1; m != boardSize; m++) { if (boardMatrix[m][Y] == opponent && p == 0) { n++; } else if (boardMatrix[m][Y] == player && p == 0) { p = 1; } else if (boardMatrix[m][Y] == EMPTY && p == 0) { p = 2; } } if (p == 1) { for (m = X; m <= X + n; m++) { boardMatrix[m][Y] = player; //System.out.println("horizontal: " + m + " " + Y); } boardMatrix[X][Y] = player; } } } // checking and maybe changing horizontally from right to left if (X > 0) { if (boardMatrix[X-1][Y] == opponent && X > 0) { n = 0; p = 0; for (m = X-1; m >= 0; m--) { if (boardMatrix[m][Y] == opponent && p == 0) { n++; } else if (boardMatrix[m][Y] == player && p == 0) { p = 1; } else if (boardMatrix[m][Y] == EMPTY && p == 0) { p = 2; } } if (p == 1) { for (m = X; m >= X - n; m--) { boardMatrix[m][Y] = player; //System.out.println("horizontal: " + m + " " + Y); } boardMatrix[X][Y] = player; } } } // checking and maybe changing vertically from top to bottom if (Y < boardSize - 1) { if (boardMatrix[X][Y+1] == opponent) { n = 0; p = 0; for (m = Y+1; m != boardSize; m++) { if (boardMatrix[X][m] == opponent && p == 0) { n++; } else if (boardMatrix[X][m] == player && p == 0) { p = 1; } else if (boardMatrix[X][m] == EMPTY && p == 0) { p = 2; } } if (p == 1) { for (m = Y; m <= Y + n; m++) { boardMatrix[X][m] = player; //System.out.println("vertical " + X + " " + m); } boardMatrix[X][Y] = player; } } } // checking and maybe changing vertically from bottom to top if (Y > 0) { if (boardMatrix[X][Y-1] == opponent) { n = 0; p = 0; for (m = Y-1; m >= 0; m--) { if (boardMatrix[X][m] == opponent && p == 0) { n++; } else if (boardMatrix[X][m] == player && p == 0) { p = 1; } else if (boardMatrix[X][m] == EMPTY && p == 0) { p = 2; } } if (p == 1) { for (m = Y; m >= Y - n; m--) { boardMatrix[X][m] = player; //System.out.println("vertical " + X + " " + m); } boardMatrix[X][Y] = player; } } } // checking and maybe changing diagonally from upper left to lower right if (X < boardSize - 1 && Y < boardSize - 1) { if (boardMatrix[X+1][Y+1] == opponent) { n = 0; p = 0; if (X >= Y) { q = Y + 1; for (m = X+1; m != boardSize; m++) { if (boardMatrix[m][q] == opponent && p == 0) { n++; } else if (boardMatrix[m][q] == player && p == 0) { p = 1; } else if (boardMatrix[m][q] == EMPTY && p == 0) { p = 2; } q++; } } else { m = Y; for (q = Y; q != boardSize; q++) { if (boardMatrix[m][q] == opponent && p == 0) { n++; } else if (boardMatrix[m][q] == player && p == 0) { p = 1; } else if (boardMatrix[m][q] == EMPTY && p == 0) { p = 2; } m++; } } if (p == 1) { q = X; for (m = Y; m <= Y + n; m++) { boardMatrix[q][m] = player; //System.out.println("diagonal_1: " + q + " " + m); q++; } boardMatrix[X][Y] = player; } } } // checking and maybe changing diagonally from lower right to upper left if (X > 0 && Y > 0) { if (boardMatrix[X-1][Y-1] == opponent) { n = 0; p = 0; if (X >= Y) { m = X - 1; for (q = Y - 1; q >= 0; q--) { if (boardMatrix[m][q] == opponent && p == 0) { n++; } else if (boardMatrix[m][q] == player && p == 0) { p = 1; } else if (boardMatrix[m][q] == EMPTY && p == 0) { p = 2; } m--; } } else { q = Y - 1; for (m = X - 1; m >= 0; m--) { if (boardMatrix[m][q] == opponent && p == 0) { n++; } else if (boardMatrix[m][q] == player && p == 0) { p = 1; } else if (boardMatrix[m][q] == EMPTY && p == 0) { p = 2; } q--; } } if (p == 1) { q = X; for (m = Y; m >= Y - n; m--) { boardMatrix[q][m] = player; //System.out.println("diagonal_1: " + q + " " + m); q--; } boardMatrix[X][Y] = player; } } } // checking and maybe changing diagonally from lower left to upper right if (X > 0 && Y < boardSize - 1) { if (boardMatrix[X-1][Y+1] == opponent) { n = 0; p = 0; if (X + Y >= 7) { m = X - 1; for (q = Y + 1; q != boardSize; q++) { if (boardMatrix[m][q] == opponent && p == 0) { n++; } else if (boardMatrix[m][q] == player && p == 0) { p = 1; } else if (boardMatrix[m][q] == EMPTY && p == 0) { p = 2; } m--; } } else { q = Y + 1; for (m = X - 1; m >= 0; m--) { if (boardMatrix[m][q] == opponent && p == 0) { n++; } else if (boardMatrix[m][q] == player && p == 0) { p = 1; } else if (boardMatrix[m][q] == EMPTY && p == 0) { p = 2; } q++; } } if (p == 1) { q = X; for (m = Y; m <= Y + n; m++) { boardMatrix[q][m] = player; //System.out.println("diagonal_2: " + q + " " + m); q--; } boardMatrix[X][Y] = player; } } } // checking and maybe changing diagonally from upper right to lower left if (X < boardSize -1 && Y > 0) { if (boardMatrix[X+1][Y-1] == opponent) { n = 0; p = 0; if (X + Y >= 7) { q = Y - 1; for (m = X + 1; m != boardSize; m++) { if (boardMatrix[m][q] == opponent && p == 0) { n++; } else if (boardMatrix[m][q] == player && p == 0) { p = 1; } else if (boardMatrix[m][q] == EMPTY && p == 0) { p = 2; } q--; } } else { m = X + 1; for (q = Y - 1; q >= 0; q--) { if (boardMatrix[m][q] == opponent && p == 0) { n++; } else if (boardMatrix[m][q] == player && p == 0) { p = 1; } else if (boardMatrix[m][q] == EMPTY && p == 0) { p = 2; } m++; } } if (p == 1) { q = X; for (m = Y; m >= Y - n; m--) { boardMatrix[q][m] = player; //System.out.println("diagonal_2: " + q + " " + m); q++; } boardMatrix[X][Y] = player; } } } } } // getting the square of the Click private void pictureMouseClicked(java.awt.event.MouseEvent evt) { // getting the X value mouseX = evt.getX(); // getting the Y value mouseY = evt.getY(); getPosition(mouseX, mouseY, boardSize); } private void countScore(int[][] oldMatrix, int[][] newMatrix) { int oldW = 0, oldB = 0, newW = 0, newB = 0, score = 0; for (int i = 0; i != boardSize; i++) { for (int j = 0; j != boardSize; j++) { if (oldMatrix[i][j] == WHITE) oldW++; if (oldMatrix[i][j] == BLACK) oldB++; if (newMatrix[i][j] == WHITE) newW++; if (newMatrix[i][j] == BLACK) newB++; } } diffB = newB - oldB; diffW = newW - oldW; setBScore(newB); setWScore(newW); } // mouseX, mouseY and bs to get the position of the click private void getPosition(int m_x, int m_y, int bs) { if (m_x > cutoffX && m_x < picture.getWidth() - cutoffX && m_y > cutoffY && m_y < picture.getHeight() - cutoffY) { double x; double y; x = picture.getWidth(); y = picture.getHeight(); x = (x - 2 * (double)cutoffX)/boardSize; y = (y - 2 * (double)cutoffY)/boardSize; curX = (double)(m_x - cutoffX)/x; curX = Math.ceil(curX); curY = (double)(m_y - cutoffY)/y; curY = Math.ceil(curY); if (getPlayer() && boardMatrix[(int)curX-1][(int)curY-1] == EMPTY && playerMatrix[(int)curX-1][(int)curY-1] == POSSIBLE) { //boardMatrix[(int)curX-1][(int)curY-1] = WHITE; int old[][] = boardMatrix; flip(getPlayer(), (int)(curX-1), (int)(curY-1)); //countScore(old, boardMatrix); wClock.stop(); bClock.start(); changePlayer(); //possibleMoves(player); repaint(); Position p = miniMaxAB(player, 1, -1000, 1000, true); flip(player, p.x, p.y); bClock.stop(); wClock.start(); changePlayer(); possibleMoves(player); //repaint(); } /*else if (!getPlayer() && boardMatrix[(int)curX-1][(int)curY-1] == EMPTY && playerMatrix[(int)curX-1][(int)curY-1] == POSSIBLE) { boardMatrix[(int)curX-1][(int)curY-1] = BLACK; flip(getPlayer(), (int)(curX-1), (int)(curY-1)); bClock.stop(); wClock.start(); changePlayer(); possibleMoves(player); repaint(); }*/ } } private void changePlayer() { player = !player; } private boolean getPlayer() { return player; } private int getUtility(boolean pl) { int playerUtil = 0; int opponentUtil = 0; int player; int opponent; if (pl) { player = WHITE; opponent = BLACK; } else { player = BLACK; opponent = WHITE; } int boardWeight[][] = { { 100, -10, 11, 6, 6, 11, -10, 100 }, { -10, -20, 1, 2, 2, 1, -20, -10 }, { 10, 1, 5, 4, 4, 5, 1, 10 }, { 6, 2, 4, 2, 2, 4, 2, 6 }, { 6, 2, 4, 2, 2, 4, 2, 6 }, { 10, 1, 5, 4, 4, 5, 1, 10 }, { -10, -20, 1, 2, 2, 1, -20, -10 }, { 100, -10, 11, 6, 6, 11, -10, 100 } }; for (int i = 0; i < boardSize; ++i) { for (int j = 0; j < boardSize; ++j) { if(boardMatrix[i][j] == player) playerUtil += boardWeight[i][j]; else if (boardMatrix[i][j] == opponent) opponentUtil += boardWeight[i][j]; } } checkNeighbours(pl, boardWeight, 15); printScore(playerUtil, opponentUtil); return playerUtil; } // increasing the score of the boardMatrix in some special cases // 1. if there are only two different neighbouring pieces on first/last row/column private void checkNeighbours(boolean pl, int matrix[][], int val) { int player; int opponent; if (pl) { player = WHITE; opponent = BLACK; } else { player = BLACK; opponent = WHITE; } int n = 0; int p = 0; int m = 0; for (int i = 0; i != boardSize; i++) { for (int j = 0; j != boardSize; j++) { if (i == 0 || i == boardSize - 1) { if (boardMatrix[i][j] == player) { for (m = j + 1; m < boardSize; m++) { if (boardMatrix[i][m] == opponent && p == 0) { n++; } else if (boardMatrix[i][m] == player && p == 0) { p = 1; } else if (boardMatrix[i][m] == EMPTY && p == 0) { p = 2; } } if (p == 1) { for (m = j; m <= j + n; m++) { matrix[i][m] = val; } matrix[i][j] = val; } for (m = j - 1; m >= 0; m--) { if (boardMatrix[i][m] == opponent && p == 0) { n++; } else if (boardMatrix[i][m] == player && p == 0) { p = 1; } else if (boardMatrix[i][m] == EMPTY && p == 0) { p = 2; } } if (p == 1) { for (m = j; m >= j - n; m--) { matrix[i][m] = val; } matrix[i][j] = val; } } } if (j == 0 || j == boardSize - 1) { if (boardMatrix[i][j] == player) { for (m = i + 1; m < boardSize; m++) { if (boardMatrix[m][j] == opponent && p == 0) { n++; } else if (boardMatrix[m][j] == player && p == 0) { p = 1; } else if (boardMatrix[m][j] == EMPTY && p == 0) { p = 2; } } if (p == 1) { for (m = i; m <= i + n; m++) { matrix[m][j] = val; } matrix[i][j] = val; } for (m = i - 1; m >= 0; m--) { if (boardMatrix[m][j] == opponent && p == 0) { n++; } else if (boardMatrix[m][j] == player && p == 0) { p = 1; } else if (boardMatrix[m][j] == EMPTY && p == 0) { p = 2; } } if (p == 1) { for (m = i; m >= i - n; m--) { matrix[m][j] = val; } matrix[i][j] = val; } } } } } printMatrix(matrix); } /* potential mobility can be estimated by simply * counting all the opponent's pieces that are adjacent to an empty field * http://home.tiscalinet.ch/t_wolf/tw/misc/reversi/html/ */ private void potentialMobility(boolean pl) { } /* * count the number of moves a player can make */ private void Mobility(boolean pl) { } /* * If the player occupies a corner it is desirable to enlarge this whole area */ private void enlargeCornerArea(boolean pl) { } /* * edge moves interact with the pieces on the adjacent edges, * turning more pieces and thus creating opportunities for the opponent. */ private void avoidEdge(boolean pl) { } /* * only playing the perpendicular or the diagonal opening */ private void noParallelOpening(boolean pl) { } /* * discs that cannot be flipped */ private void createStableDiscs(boolean pl) { } /* * avoid long and thick wall that are no stable discs */ private void noThickWall(boolean pl) { } /* * no unbalanced edges * http://www.othello.org.hk/tutorials/eng-tu3.html */ private void noUnbalancedEdges(boolean pl) { } public void printMatrix(int[][] matrix) { for (int i = 0; i != boardSize; i++) { for (int j = 0; j != boardSize; j++) { if (j < (boardSize - 1)) System.out.print(matrix[i][j]+""); else System.out.println(matrix[i][j]+""); } } System.out.println(""); } // giving ourselves as many options as possible for our next move, // whilst restricting our opponent to very few private void mobility() { } private void printScore(int player, int opponent){ System.out.println("player: " + player); System.out.println("opponent: " + opponent); } public Position miniMaxAB(boolean player, int lookahead, int alpha, int beta, boolean isMax) { Position pos = new Position(); int tempBoard[][] = new int[boardSize][boardSize]; for (int i = 0; i < boardSize; ++i) { for (int j = 0; j < boardSize; ++j) { tempBoard[i][j] = boardMatrix[i][j]; } } if (lookahead == 0) {//final ply reached if (isMax) { pos.value = getUtility(player); } else { pos.value = getUtility(!player); } return pos; } int numMoves = possibleMoves(player); if (numMoves == 0) { if (isMax) { pos.value = getUtility(player); } else { pos.value = getUtility(!player); } return pos; } int bestX = 0; int bestY = 0; if (isMax) { //max for (int i = 0; i < boardSize; ++i) { for (int j = 0; j < boardSize; ++j) { if (playerMatrix[i][j] == POSSIBLE) { flip(player, i, j); Position newPos = miniMaxAB(!player, lookahead-1, alpha, beta, !isMax); if (newPos.value > alpha) { alpha = newPos.value; bestX = i; bestY = j; if (alpha >= beta) { pos.value = beta; pos.x = bestX; pos.y = bestY; for (i = 0; i < boardSize; ++i) { for (j = 0; j < boardSize; ++j) { boardMatrix[i][j] = tempBoard[i][j]; } } return pos; } } } } } pos.value = alpha; pos.x = bestX; pos.y = bestY; for (int i = 0; i < boardSize; ++i) { for (int j = 0; j < boardSize; ++j) { boardMatrix[i][j] = tempBoard[i][j]; } } return pos; } else { //min for (int i = 0; i < boardSize; ++i) { for (int j = 0; j < boardSize; ++j) { if (playerMatrix[i][j] == POSSIBLE) { flip(player, i, j); Position newPos = miniMaxAB(!player, lookahead-1, alpha, beta, !isMax); if (newPos.value < beta) { beta = newPos.value; bestX = i; bestY = j; if (alpha >= beta) { pos.value = alpha; pos.x = bestX; pos.y = bestY; for (i = 0; i < boardSize; ++i) { for (j = 0; j < boardSize; ++j) { boardMatrix[i][j] = tempBoard[i][j]; } } return pos; } } } } } pos.value = beta; pos.x = bestX; pos.y = bestY; for (int i = 0; i < boardSize; ++i) { for (int j = 0; j < boardSize; ++j) { boardMatrix[i][j] = tempBoard[i][j]; } } return pos; } } public void initComponents() {//GEN-BEGIN:initComponents setTitle("O-Thell-Us"); setBackground(Color.white); setDefaultLookAndFeelDecorated(true); setResizable(false); java.awt.GridBagConstraints gridBagConstraints; jPanel1 = new JPanel(); jPanel1.setPreferredSize(setWindowSize()); jPanel1.setBackground(Color.white); jPanel2 = new JPanel(); jPanel2.setBackground(Color.white); picture = new JLabel(); jLabel6 = new JLabel(); jLabel11 = new JLabel(); jTextField6 = new JTextField(); jTextField8 = new JTextField(); jPanel3 = new JPanel(); jPanel3.setBackground(Color.white); jLabel7 = new JLabel(); jLabel8 = new JLabel(); jLabel10 = new JLabel(wClock.getTimeString(wClock.getSeconds())); jLabel12 = new JLabel(bClock.getTimeString(bClock.getSeconds())); jLabel9 = new JLabel(); java.awt.GridBagConstraints gridBagConstraints2; jPanel11 = new JPanel(); jPanel11.setBackground(Color.white); jPanel11.setBorder(new javax.swing.border.TitledBorder("Game Controls")); jLabel1 = new JLabel(); jLabel1.setBackground(Color.white); jComboBox1 = new JComboBox(); jComboBox1.setBackground(Color.white); jButton1 = new JButton(); jButton2 = new JButton(); picture.addMouseListener( new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { pictureMouseClicked(evt); } }); addWindowListener( new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); jPanel1.setLayout(new java.awt.GridBagLayout()); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.gridheight = 4; gridBagConstraints.insets = new Insets(10,10,10,10); gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; jPanel1.add(picture, gridBagConstraints); // adding the game's logo jLabel9.setIcon(createIcon("images/logo2.gif")); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 0; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; jPanel1.add(jLabel9, gridBagConstraints); jPanel2.setLayout(new java.awt.GridBagLayout()); jPanel2.setBorder(new javax.swing.border.TitledBorder("White")); jLabel6.setText("Elapsed Time"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.insets = new Insets(0,10,0,0); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; jPanel2.add(jLabel6, gridBagConstraints); jLabel11.setText("Score"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.insets = new Insets(0,10,0,0); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; jPanel2.add(jLabel11, gridBagConstraints); jLabel10.setBackground(Color.white); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.insets = new Insets(10,10,10,10); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.ipadx = 1; jPanel2.add(jLabel10, gridBagConstraints); jTextField6.setText(wScore+""); jTextField6.setEditable(false); jTextField6.setBackground(Color.white); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 2; gridBagConstraints.insets = new Insets(10,10,10,10); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.ipadx = 2; jPanel2.add(jTextField6, gridBagConstraints); // adding white's statistics gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; jPanel1.add(jPanel2, gridBagConstraints); jPanel3.setLayout(new java.awt.GridBagLayout()); jPanel3.setBorder(new javax.swing.border.TitledBorder("Black")); jLabel7.setText("Elapsed Time"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.insets = new Insets(0,10,0,0); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; jPanel3.add(jLabel7, gridBagConstraints); jLabel8.setText("Score"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.insets = new Insets(0,10,0,0); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; jPanel3.add(jLabel8, gridBagConstraints); jLabel12.setBackground(Color.white); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 0; gridBagConstraints.insets = new Insets(10,10,10,10); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; jPanel3.add(jLabel12, gridBagConstraints); jTextField8.setText(bScore+""); jTextField8.setEditable(false); jTextField8.setBackground(Color.white); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.insets = new Insets(10,10,10,10); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; jPanel3.add(jTextField8, gridBagConstraints); // adding black's statistics gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 2; jPanel1.add(jPanel3, gridBagConstraints); // start oth1 jPanel11.setLayout(new java.awt.GridBagLayout()); jComboBox1.setModel(new DefaultComboBoxModel(new String[] { "Human vs. Computer", "Human vs. Human", "Computer vs. Human" })); gridBagConstraints2 = new java.awt.GridBagConstraints(); gridBagConstraints2.gridx = 2; gridBagConstraints2.gridy = 0; gridBagConstraints2.insets = new Insets(0,0,10,0); jPanel11.add(jComboBox1, gridBagConstraints2); jButton1.setText("Start New Game"); gridBagConstraints2 = new java.awt.GridBagConstraints(); gridBagConstraints2.gridx = 2; gridBagConstraints2.gridy = 8; gridBagConstraints2.ipadx = 25; gridBagConstraints2.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints2.insets = new Insets(20, 4, 0, 0); jPanel11.add(jButton1, gridBagConstraints2); jButton2.setText("Quit"); gridBagConstraints2 = new java.awt.GridBagConstraints(); gridBagConstraints2.gridx = 2; gridBagConstraints2.gridy = 9; gridBagConstraints2.ipadx = 25; gridBagConstraints2.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints2.insets = new Insets(20, 4, 10, 0); jPanel11.add(jButton2, gridBagConstraints2); // add game controls gridBagConstraints2 = new java.awt.GridBagConstraints(); gridBagConstraints2.gridx = 1; gridBagConstraints2.gridy = 3; jPanel1.add(jPanel11, gridBagConstraints2); getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER); pack(); setVisible(true); }//GEN-END:initComponents /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm System.exit(0); }//GEN-LAST:event_exitForm /** * @param args the command line arguments */ public static void main(String args[]) { new Othello2().show(); } public void actionPerformed(ActionEvent e) { if(e.getSource() == jButton1) { playerMode = jComboBox1.getSelectedIndex(); if (playerMode == -1) System.err.println("Error in combobox!"); } if(e.getSource() == jButton2) { System.exit(0); } } // Variables declaration private JLabel jLabel6; private JLabel jLabel7; private JLabel jLabel8; private JLabel jLabel9; private JLabel jLabel10; // white clock private JLabel jLabel11; private JLabel jLabel12; // black clock private JPanel jPanel1; private JPanel jPanel2; private JPanel jPanel3; private JTextField jTextField6; //score private JTextField jTextField8; private JLabel picture; private JButton jButton1; private JButton jButton2; private JComboBox jComboBox1; private JLabel jLabel1; private JPanel jPanel11; private int mouseX; private int mouseY; private double scaleX; private double scaleY; private double cutoffX = 32; // 8: 33, 32; 12: 32, 34; 16: 34, 34 private double cutoffY = 32; // 8: 32, 32; 12: 32, 32; 16: 32, 33 private double curX = 0; private double curY = 0; private int boardSize = 8; private int playerMode; private int boardMatrix[][]; private boolean player = true; //white = true private int playerMatrix[][] = new int[boardSize][boardSize]; private Vector flipM[][] = new Vector[boardSize][boardSize]; private static final int BLACK = 0; private static final int WHITE = 1; private static final int EMPTY = 2; private static final int POSSIBLE = 3; private static final int DIM = 465; private int wScore = 2; private int bScore = 2; private int diffB = 0; private int diffW = 0; private ImageIcon whiteIcon; private ImageIcon blackIcon; private Stopwatch wClock; private Stopwatch bClock; // End of variables declaration } class Direction { short dx; short dy; } class Position { int value = 0; int x; int y; }