repaint(); waitingInput = false; } } } public void startNewGame() { board = new OthelloBoard(); finished = false; waitingInput = false; lastMoveIndex = -666; //troca cores int temp = cpuColor; cpuColor = humanColor; humanColor = temp; new Thread(this).start(); } public void run() { while (! finished) { this.doLogic(); this.repaint(); try { Thread.sleep(20L); } catch (InterruptedException exception) {} } //fim de jogo int human = board.countDiscs(humanColor); int cpu = board.countDiscs(cpuColor); int empty = OthelloBoard.BOARD_SIZE * OthelloBoard.BOARD_SIZE - (human + cpu); String msg = null; if (human > cpu) { human += empty; msg = "Voce venceu por " + human + " - " + cpu + "!"; } else if (human < cpu) { cpu += empty; msg = "Voce perdeu por " + cpu + " - " + human + "!"; } else msg = "Empate!"; msg += "\nDeseja jogar uma nova partida?"; int x = JOptionPane.showConfirmDialog(null, msg, "Game Over", JOptionPane.YES_NO_OPTION); if (x == 0) startNewGame(); } private void drawGUI(Graphics g) { Graphics2D g2D = (Graphics2D) g; g2D.setStroke(new BasicStroke(1.5f)); //cor de fundo Color bg = new Color(0, 130, 0); setBackground(Color.gray); int w = OthelloBoard.BOARD_SIZE * cellSize; int origin = cellSize; //linhas horizontais g.setColor(Color.black); for (int i = 0; i < OthelloBoard.BOARD_SIZE + 1; i++) { g.drawLine(origin, i * cellSize + origin, origin + w, i * cellSize + origin); } //linhas verticais for (int i = 0; i < OthelloBoard.BOARD_SIZE + 1; i++) { g.drawLine(i * cellSize + origin, origin, i * cellSize + origin, w + origin); } g.setColor(Color.white); //headers String[] headers = new String[]{"A","B","C","D","E","F","G","H"}; for (int i = 0; i < OthelloBoard.BOARD_SIZE; i++) g.drawString(headers[i], cellSize * i + (int)(origin * 1.5), (int)(cellSize * 0.75)); //linhas for (int i = 0; i < OthelloBoard.BOARD_SIZE; i++) g.drawString("" + (i + 1), (int)(cellSize * 0.6), cellSize * i + (int)(origin * 1.5)); } private void drawScore(Graphics g) { //score g.setColor(Color.yellow); g.setFont(new Font(null, Font.BOLD, 15)); int wd = board.countDiscs(OthelloBoard.WHITE); int bd = board.countDiscs(OthelloBoard.BLACK); g.drawString("White: " + wd, cellSize * 1, (int)(cellSize * 9.5)); g.drawString("Black: " + bd, cellSize * 8, (int)(cellSize * 9.5)); } private void drawBoard(Graphics g) { int[] cells = board.getCells(); int gameCols = OthelloBoard.BOARD_SIZE; int hiddenCols = gameCols + 2; int mid = (cellSize - pieceSize)/2; for (int i = hiddenCols; i < cells.length - hiddenCols; i++) { int col = i % hiddenCols; int row = i / hiddenCols; if ((col != 0) && (col != hiddenCols - 1)) { int piece = cells[i]; if (piece == OthelloBoard.EMPTY) continue; OthelloPiece p = new OthelloPiece(cells[i], pieceSize); p.draw(g, col * cellSize + mid, row * cellSize + mid); } } //lance fantasma idx = -666 if (lastMoveIndex > 0) { g.setColor(Color.yellow); int col = lastMoveIndex % hiddenCols; int row = lastMoveIndex / hiddenCols; g.drawRect(col * cellSize, row * cellSize, cellSize, cellSize); } } private void drawMoves(Graphics g) { ArrayList<OthelloMove> moves = board.getAllMoves(humanColor); if (isHumanTurn() && moves.get(0).getFlipSquares() != null) { g.setColor(new Color(0, 0, 0, 80)); for (OthelloMove mv : moves) { int idx = mv.getIdx(); int col = idx % 10; int row = idx /10; g.fillOval(col * cellSize + cellSize/2,