import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent; import TBNG.tbngAbstractGrid; import TBNG.tbngGameInterface; public class othello2Game extends tbngAbstractGrid { public static final int numOfRows = 8, numOfColumns = 8; private int cellBorderOffset = 5; //How far from a cell's border should the naught/cross be drawn from, the margin/padding private int coinHolderWidth = 2; //How big the coin holder is private final Color clrRedPiece = new Color(255, 0, 0); private final Color clrBluePiece = new Color(0, 0, 255); private final Color clrCellBorder = new Color(62, 62, 61); private int numMovesMade = 4; //The game continues until 64 has been reached (starts at 4 because 4 counters are always present at the start) public othello2Game (tbngGameInterface parent) { super(parent, numOfRows, numOfColumns); playerMatrix = new othello2PlayerMatrix(numOfRows, numOfColumns); clrGridBackground = new Color(82, 155, 33); } private boolean canMove () { if (isConnected && (currentPlayer != meNetworkPlayer)) { return false; } else if (playerMatrix.isBoardLocked()) { return false; } return true; } public void decodeNetworkMove (String strMove) { String[] strFields = strMove.split("&"); String[] strKeyValue = null; int row = 0; int column = 0; for (int i = 0; i < strFields.length; i++) { strKeyValue = strFields[i].split("="); if (strKeyValue[0].equals("row")) { row = Integer.parseInt(strKeyValue[1]); } else if (strKeyValue[0].equals("column")) { column = Integer.parseInt(strKeyValue[1]); } } doMove(row, column); } private void doMove (int row, int column) { numMovesMade++; playerMatrix.setCellContents(row, column, currentPlayer); //If this move was made by you, then send it to the other player if (isConnected && currentPlayer == meNetworkPlayer) { parent.gameEncodedMove("row=" + row + "&column=" + column); } if (numMovesMade >= 64) { int numPlayerOne = 0; int numPlayerTwo = 0; int piece = 0; for (int r = 0; r < numOfRows; r++) { for (int c = 0; c < numOfColumns; c++) { piece = playerMatrix.getCellContents(r, c); if (piece == PLAYER_ONE) { numPlayerOne++; } else if (piece == PLAYER_TWO) { numPlayerTwo++; } } } if (numPlayerOne > numPlayerTwo) { currentPlayer = PLAYER_ONE; } else if (numPlayerTwo > numPlayerOne) { currentPlayer = PLAYER_TWO; } if (numPlayerOne == numPlayerTwo) { gameDraw(); } else { gameWon(); } } else { moveMadeSwitchPlayers(); } repaint(); } private void drawCell (int fromX, int fromY, Graphics g) { g.setColor(clrCellBorder); g.fillRect(fromX, fromY, CELLSIZE, coinHolderWidth); //NW - NE g.fillRect(fromX, (fromY + (CELLSIZE - coinHolderWidth)), CELLSIZE, coinHolderWidth); //SW - SE g.fillRect(fromX, fromY, coinHolderWidth, CELLSIZE); //NW - SW g.fillRect((fromX + (CELLSIZE - coinHolderWidth)), fromY, coinHolderWidth, CELLSIZE); //NE - SE } private void drawPiece (int fromX, int fromY, Graphics g, int piece) { if (piece == PLAYER_ONE) { g.setColor(clrRedPiece); } else { g.setColor(clrBluePiece); } int size = CELLSIZE - (cellBorderOffset * 2); g.fillRoundRect((fromX + cellBorderOffset), (fromY + cellBorderOffset), size, size, size, size); } protected int getMouseCell (int coor) { int fromBorder = gridStartX + borderSize; for (int column = 0; column < numOfColumns; column++) { if (coor > fromBorder && coor < (fromBorder + CELLSIZE)) { return column; } fromBorder += CELLSIZE; } return -1; } public void mouseClicked (MouseEvent e) { if (isConnected && (currentPlayer != meNetworkPlayer)) { return; } else if (playerMatrix.isBoardLocked()) { return; } int row = getMouseCell(e.getY()); int column = getMouseCell(e.getX()); if (row == -1 || column == -1) { return; } else if (playerMatrix.getCellContents(row, column) != 0) { return; } else if (playerMatrix.isLegalMove(row, column, currentPlayer)) { doMove(row, column); } } public void newGame () { numMovesMade = 4; resetGame(); newGameLayout(); repaint(); } private void newGameLayout () { playerMatrix.setCellContents(3, 3, PLAYER_ONE); playerMatrix.setCellContents(4, 4, PLAYER_ONE); playerMatrix.setCellContents(3, 4, PLAYER_TWO); playerMatrix.setCellContents(4, 3, PLAYER_TWO); } public void paint (Graphics g) { int fromX = gridStartX + borderSize; int fromY = gridStartY + borderSize; int cellContents = 0; drawGrid(g); for (int row = 0; row < numRowCells; row++) { for (int column = 0; column < numColumnCells; column++) { cellContents = playerMatrix.getCellContents(row, column); if (cellContents != 0) { drawPiece(fromX, fromY, g, cellContents); } drawCell(fromX, fromY, g); fromX += CELLSIZE; } fromX = gridStartX + borderSize; fromY += CELLSIZE; } } }