/* * Othellus.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 Othellus_1 extends JFrame implements ActionListener { /* * Creates new instance of class Othellus */ public Othellus_1() { wClock = new Stopwatch(); bClock = new Stopwatch(); initComponents(); whiteIcon = createIcon("images/white.gif"); blackIcon = createIcon("images/black.gif"); picture.setIcon(createIcon("images/8x8.gif")); Matrix(boardSize); possMovs = possibleMoves(getPlayer()); wClock.start(); //Register a listener for the game controls. jButton2.addActionListener(this); repaint(); } /* * Returns an ImageIcon, or null if the path was invalid. * @param path a String containing the path to the image to load * @return an ImageIcon, either null or a new instance */ private ImageIcon createIcon(String path) { java.net.URL imgURL = Othellus.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } } /* * Returns a Dimension, holding the window size * @return a Dimension */ private Dimension setWindowSize() { int pad_x = 275; int pad_y = 100; Dimension d = new Dimension(DIM + pad_x, DIM + pad_y); return d; } /* * Sets the white player's score * @param wS the current score */ private void setWScore(int wS) { wScore = wS; } /* * Sets the black player's score * @param bS the current score */ private void setBScore(int bS) { bScore = bS; } /* * Paints the current view * @param g a Graphics object */ public void paint(Graphics g) { int addX=0, addY=0; addX = 70; addY = 114;//118; // update the clocks 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; // paint out all the markers 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); } } } // update the scoring fields jTextField6.setText(wScore+""); jTextField8.setText(bScore+""); } /* * Initializes the board matrix * @param bs the current boardsize */ 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 a matrix, * returning the total number of possible moves * @param pl the current player * @return the total number of possible moves */ private int possibleMoves(boolean pl) { int m=0, n=0, player = 0, opponent=0, q=0; int numMoves = 0; // which player's turn is it??? if (pl) { player = WHITE; opponent = BLACK; } else { player = BLACK; opponent = WHITE; } // clearing the matrix for (int i = 0; i != boardSize; i++) { for (int j = 0; j != boardSize; j++) { playerMatrix[i][j] = 0; } } // searching for possible moves... 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; numMoves++; } 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; numMoves++; } 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; numMoves++; } 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; numMoves++; } 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; numMoves++; } 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; numMoves++; } 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; numMoves++; } 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; numMoves++; } 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; numMoves++; } 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; numMoves++; } 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; numMoves++; } 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; numMoves++; } else if(boardMatrix[m][q] == EMPTY && n == 0) { n = 1; } m = m + 1; } } } } } } return numMoves; } /* * Calculating which markers have to be flipped due to a made move * @param pl the current player * @param X the x coordinate * @param Y the y coordinate */ public void flip(boolean pl, int X, int Y) { int m=0, n=0, p=0, player = 0, opponent=0, q=0; // which player's turn is it??? if (pl) { player = WHITE; opponent = BLACK; } else { player = BLACK; opponent = WHITE; } 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; } 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; } 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; } 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; } 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; 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; 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; 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; q++; } boardMatrix[X][Y] = player; } } } } } /* * Updates the players' scores * @param oldMatrix the board as it was before the current move * @param newMatrix the current board */ 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++; } } // get difference diffB = newB - oldB; diffW = newW - oldW; // set score setBScore(newB); setWScore(newW); } /* * Prints a generic int matrix, used for DEBUG * @param m the matrix to be printed */ private void printMatrix(int[][] m) { for (int i = 0; i < boardSize; ++i) { for (int j = 0; j < boardSize; ++j) { if (j == (boardSize - 1)) System.out.println(m[i][j]+""); else System.out.print(m[i][j]+""); } } } /* * Catches mouse clicks * @param evt an MouseEvent */ private void pictureMouseClicked(java.awt.event.MouseEvent evt) { // getting the X value mouseX = evt.getX(); // getting the Y value mouseY = evt.getY(); // calling our "basic" function getPosition(mouseX, mouseY, boardSize); } /* * Calculating the current position of the click in the board and performs the move * @param m_x x coordinate of mouse click * @param m_y y coordinate of mouse click * @param bs the board size */ private void getPosition(int m_x, int m_y, int bs) { // is click within board? 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; // these variables hold the current position curX = (double)(m_x - cutoffX)/x; curX = Math.ceil(curX); curY = (double)(m_y - cutoffY)/y; curY = Math.ceil(curY); // the human player makes the first move if (getPlayer() && boardMatrix[(int)curX-1][(int)curY-1] == EMPTY && playerMatrix[(int)curX-1][(int)curY-1] == POSSIBLE) { int old[][]; if (possMovs != 0) { old = boardMatrix; flip(getPlayer(), (int)(curX-1), (int)(curY-1)); countScore(old, boardMatrix); } wClock.stop(); paint(this.getGraphics()); bClock.start(); changePlayer(); old = boardMatrix; Position p = miniMaxAB(player, 1, -1000, 1000, true); flip(player, p.x, p.y); countScore(old, boardMatrix); bClock.stop(); paint(this.getGraphics()); wClock.start(); changePlayer(); possMovs = possibleMoves(player); if (possMovs == 0) { wClock.stop(); bClock.start(); bClock.stop(); wClock.start(); } repaint(); } } } /* * Change which player is active */ private void changePlayer() { player = !player; if(player) printMessage("White to play"); else printMessage("Black to play"); } /* * Return active player * @return the active player */ private boolean getPlayer() { return player; } /* * Calculates the utility for the given player, choosing among various heuristics * @param pl the current player requesting its utility * @return the current utility score for the player */ private int getUtility(boolean pl) { int playerUtil = 0; int opponentUtil = 0; int player; int opponent; // which player's turn is it??? if (pl) { player = WHITE; opponent = BLACK; } else { player = BLACK; opponent = WHITE; } // a static matrix containing weights, representing the estimated // utility of having a square 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 } }; // calculate utility 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]; } } return playerUtil; } /* * Compares the available moves, and by using minimax search with alpha-beta pruning together * with some heuristics, choose the (estimated) best one * @param player the active player * @param lookahead the depth of the search * @param alpha the alpha value (the highest utility found so far by Max) * @param beta the beta value (the lowest utility found so far by Min) * @param isMax boolean telling if Max is active (true), or if Min is (false) * @return the Position for the (estimated) best move */ 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]; } } //final ply reached, end search by calculating the utility if (lookahead == 0) { if (isMax) { pos.value = getUtility(player); } else { pos.value = getUtility(!player); } return pos; } int numMoves = possibleMoves(player); // if there are no moves left, calculate the utility of the current leaf if (numMoves == 0) { if (isMax) { pos.value = getUtility(player); } else { pos.value = getUtility(!player); } return pos; } // the x and y coordinates for the so far best found move int bestX = 0; int bestY = 0; if (isMax) { //max play 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 play 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; } } /* * Prints a message in the game status window * @param message the message to be printed */ public void printMessage(String message) { screen.insert(message + "\n",0); } /* * Initializes all the javax.swing components */ 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(); screen = new JTextArea("Welcome to O-Thell-Us...",3,12); screen.setEditable(false); screen.setBorder(new javax.swing.border.SoftBevelBorder(1, Color.black, Color.black)); screen.setPreferredSize(new Dimension(84, 51)); screen.setMinimumSize(new Dimension(84, 51)); java.awt.GridBagConstraints gridBagConstraints2; jPanel11 = new JPanel(); jPanel11.setBackground(Color.white); jPanel11.setBorder(new javax.swing.border.TitledBorder("Game Info")); 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); 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);*/ gridBagConstraints2 = new java.awt.GridBagConstraints(); gridBagConstraints2.gridx = 0; gridBagConstraints2.gridy = 0; gridBagConstraints2.ipadx = 25; gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints2.insets = new Insets(10, 4, 10, 0); jPanel11.add(screen, gridBagConstraints2); jButton2.setText("Quit"); gridBagConstraints2 = new java.awt.GridBagConstraints(); gridBagConstraints2.gridx = 0; gridBagConstraints2.gridy = 1; gridBagConstraints2.ipadx = 25; gridBagConstraints2.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints2.insets = new Insets(10, 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 /* * Catch an exit request * @param evt a WindowEvent */ private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm System.exit(0); }//GEN-LAST:event_exitForm /* * The main function * @param args the command line arguments */ public static void main(String args[]) { new Othellus().show(); } /* * Catches the actions form the interface * @param e an ActionEvent */ public void actionPerformed(ActionEvent e) { 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 JTextArea screen; 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 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 int possMovs = 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; }