import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent; import TBNG.tbngAbstractGrid; import TBNG.tbngGameInterface; public class ncGame extends tbngAbstractGrid { public static final int numCellsInLine = 3; private int cellBorderOffset = 5; //How far from a cell's border should the item being drawn be, the margin/padding private final Color clrNaught = new Color(0, 0, 255); private final Color clrCross = new Color(255, 0, 0); private final Color clrCellBorder = new Color(64, 64, 64); private int numMovesMade = 0; //Keep count of how many moves have been made, 9 is the maximum possible in naughts and crosses public ncGame (tbngGameInterface parent) { //The super class will create a default sized grid based on the amount of cells super(parent, numCellsInLine, numCellsInLine); playerMatrix = new ncPlayerMatrix(numCellsInLine, numCellsInLine); } //Moves sent along the network need to be converted into a string. Once received on the other computer //it has to be converted back. NC moves come in the format row=#&column=# 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); boolean win = playerMatrix.isWinner(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 (win) { gameWon(); } else if (numMovesMade >= 9) { gameDraw(); } else { moveMadeSwitchPlayers(); } repaint(); } protected void drawCell (int fromX, int fromY, Graphics g) { g.setColor(clrCellBorder); g.drawRect(fromX, fromY, CELLSIZE, CELLSIZE); } private void drawCross (int fromX, int fromY, Graphics g) { int toX = fromX + CELLSIZE; //the position of the end border for this cell int toY = fromY + CELLSIZE; g.setColor(clrCross); //drawLine(fromX, fromY, toX, toY); g.drawLine((fromX + cellBorderOffset), (fromY + cellBorderOffset), (toX - cellBorderOffset), (toY - cellBorderOffset)); g.drawLine((toX - cellBorderOffset), (fromY + cellBorderOffset), (fromX + cellBorderOffset), (toY - cellBorderOffset)); } private void drawNaught (int fromX, int fromY, Graphics g) { int size = CELLSIZE - (cellBorderOffset * 2); g.setColor(clrNaught); g.drawRoundRect((fromX + cellBorderOffset), (fromY + cellBorderOffset), size, size, size, size); } protected int getMouseCell (int coor) { int fromBorder = gridStartX + borderSize; for (int column = 0; column < numCellsInLine; 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 || playerMatrix.getCellContents(row, column) != 0) { return; } doMove(row, column); } public void newGame () { numMovesMade = 0; resetGame(); repaint(); } public void paint (Graphics g) { drawGrid(g); int fromX = gridStartX + borderSize; int fromY = gridStartY + borderSize; int cellContents = 0; for (int row = 0; row < numRowCells; row++) { for (int column = 0; column < numColumnCells; column++) { cellContents = playerMatrix.getCellContents(row, column); drawCell(fromX, fromY, g); if (cellContents == PLAYER_ONE) { drawCross(fromX, fromY, g); } else if (cellContents == PLAYER_TWO) { drawNaught(fromX, fromY, g); } fromX += CELLSIZE; } fromX = gridStartX + borderSize; fromY += CELLSIZE; } } }