From 2aba45ba01b1f9566e6214287a8d473eeebd7765 Mon Sep 17 00:00:00 2001 From: Victor Emmanuel Date: Mon, 8 May 2023 18:38:12 -0300 Subject: [PATCH 1/2] Support to be capable of execute in eclipse IDE --- jogo-oito/.classpath | 5 +- .../.settings/org.eclipse.jdt.core.prefs | 13 +- jogo-oito/src/main/java/victor/GameRun.java | 11 ++ .../java/victor/entities/BoardPosition.java | 27 +++ .../java/victor/managers/BoardManager.java | 114 ++++++++++++ .../java/victor/managers/GameManager.java | 83 +++++++++ .../java/victor/managers/ScreenManager.java | 54 ++++++ .../src/main/java/victor/screens/Game.java | 168 ++++++++++++++++++ .../src/main/java/victor/screens/Welcome.java | 86 +++++++++ .../src/main/java/victor/screens/Winning.java | 73 ++++++++ 10 files changed, 627 insertions(+), 7 deletions(-) create mode 100644 jogo-oito/src/main/java/victor/GameRun.java create mode 100644 jogo-oito/src/main/java/victor/entities/BoardPosition.java create mode 100644 jogo-oito/src/main/java/victor/managers/BoardManager.java create mode 100644 jogo-oito/src/main/java/victor/managers/GameManager.java create mode 100644 jogo-oito/src/main/java/victor/managers/ScreenManager.java create mode 100644 jogo-oito/src/main/java/victor/screens/Game.java create mode 100644 jogo-oito/src/main/java/victor/screens/Welcome.java create mode 100644 jogo-oito/src/main/java/victor/screens/Winning.java diff --git a/jogo-oito/.classpath b/jogo-oito/.classpath index 1d97b2f..14ca02c 100644 --- a/jogo-oito/.classpath +++ b/jogo-oito/.classpath @@ -8,13 +8,14 @@ + - - + + diff --git a/jogo-oito/.settings/org.eclipse.jdt.core.prefs b/jogo-oito/.settings/org.eclipse.jdt.core.prefs index bc0c2ff..d089a9b 100644 --- a/jogo-oito/.settings/org.eclipse.jdt.core.prefs +++ b/jogo-oito/.settings/org.eclipse.jdt.core.prefs @@ -1,8 +1,11 @@ eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=18 -org.eclipse.jdt.core.compiler.compliance=18 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore -org.eclipse.jdt.core.compiler.release=disabled -org.eclipse.jdt.core.compiler.source=18 +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/jogo-oito/src/main/java/victor/GameRun.java b/jogo-oito/src/main/java/victor/GameRun.java new file mode 100644 index 0000000..b90deaa --- /dev/null +++ b/jogo-oito/src/main/java/victor/GameRun.java @@ -0,0 +1,11 @@ +package victor; + +import victor.managers.ScreenManager; + +public class GameRun { + + public static void main(String[] args) { + new ScreenManager(); + } + +} diff --git a/jogo-oito/src/main/java/victor/entities/BoardPosition.java b/jogo-oito/src/main/java/victor/entities/BoardPosition.java new file mode 100644 index 0000000..b4bf7c5 --- /dev/null +++ b/jogo-oito/src/main/java/victor/entities/BoardPosition.java @@ -0,0 +1,27 @@ +package victor.entities; + +public class BoardPosition { + private final int xPosition; + private final int yPosition; + + public BoardPosition(int xPosition, int yPosition) { + this.xPosition = xPosition; + this.yPosition = yPosition; + } + + public int getXPosition() { + return this.xPosition; + } + + public int getYPosition() { + return this.yPosition; + } + + @Override + public String toString() { + return "BoardPosition { " + + "xPosition = " + xPosition + + ", yPosition = " + yPosition + + " }"; + } +} diff --git a/jogo-oito/src/main/java/victor/managers/BoardManager.java b/jogo-oito/src/main/java/victor/managers/BoardManager.java new file mode 100644 index 0000000..51ce69b --- /dev/null +++ b/jogo-oito/src/main/java/victor/managers/BoardManager.java @@ -0,0 +1,114 @@ +package victor.managers; + +import java.util.*; +import java.util.stream.IntStream; + +import victor.entities.BoardPosition; + +public class BoardManager { + private final int boardSize; + private final int[][] board; + private final HashMap boardPositions = new HashMap<>(); + + public BoardManager(int boardSize) { + this.boardSize = boardSize; + this.board = new int[boardSize][boardSize]; + + resetBoard(); + } + + public BoardPosition getBoardPosition(String boardValue) { + return this.boardPositions.get(boardValue); + } + + public int getBoardSize() { + return this.boardSize; + } + + public int getBoardValue(int xPosition, int yPosition) { + return this.board[xPosition][yPosition]; + } + + public BoardPosition getEmptyPositions() { + return this.boardPositions.get("0"); + } + + public void setBoardPositions(Integer boardValue, BoardPosition boardPosition) { + board[boardPosition.getXPosition()][boardPosition.getYPosition()] = boardValue; + boardPositions.put(boardValue.toString(), boardPosition); + } + + public void resetBoard() { + ArrayList defaultValuesList = new ArrayList<>(); + int totalBoardSize = this.boardSize * this.boardSize; + + IntStream.range(0, totalBoardSize).forEach(defaultValuesList::add); + + initializeBoardPositions(defaultValuesList); + } + + private void initializeBoardPositions(ArrayList defaultValues) { + Random rand = new Random(); + for (int i = 0; i < boardSize; i++) { + for (int j = 0; j < boardSize; j++) { + int randomChoice = rand.nextInt(defaultValues.size()); + int boardValue = defaultValues.remove(randomChoice); + + board[i][j] = boardValue; + boardPositions.put(Integer.toString(boardValue), new BoardPosition(i, j)); + } + } + } + + private int safeSearchInBoard(int x, int y) { + try { + return this.getBoardValue(x, y); + } catch (Exception e) { + return -1; + } + } + + public boolean hasEmptySpaceInSurround(int xPosition, int yPosition) { + boolean hasEmptyAbove = safeSearchInBoard(xPosition, yPosition + 1) == 0; + boolean hasEmptyBelow = safeSearchInBoard(xPosition, yPosition - 1) == 0; + boolean hasEmptyLeft = safeSearchInBoard(xPosition - 1, yPosition) == 0; + boolean hasEmptyRight = safeSearchInBoard(xPosition + 1, yPosition) == 0; + + return hasEmptyBelow || hasEmptyLeft || hasEmptyAbove || hasEmptyRight; + } + + public boolean isOrdered() { + int highestValue = 0; + for (int i = 0; i < boardSize; i++) { + for (int j = 0; j < boardSize; j++) { + int currentValue = board[i][j]; + + if (currentValue == 0 && (i != boardSize - 1 || j != boardSize - 1)) { + return false; + } + + if (currentValue != 0 && currentValue > highestValue) { + highestValue = currentValue; + } else { + return false; + } + } + } + + return true; + } + + public boolean changeButtonsPositions(int buttonXPosition, int buttonYPosition) { + boolean hasEmptySpaceInSurround = this.hasEmptySpaceInSurround(buttonXPosition, buttonYPosition); + + if (hasEmptySpaceInSurround) { + int value = this.getBoardValue(buttonXPosition, buttonYPosition); + BoardPosition emptyPosition = this.getEmptyPositions(); + + this.setBoardPositions(value, emptyPosition); + this.setBoardPositions(0, new BoardPosition(buttonXPosition, buttonYPosition)); + } + + return hasEmptySpaceInSurround; + } +} diff --git a/jogo-oito/src/main/java/victor/managers/GameManager.java b/jogo-oito/src/main/java/victor/managers/GameManager.java new file mode 100644 index 0000000..d9a8bb9 --- /dev/null +++ b/jogo-oito/src/main/java/victor/managers/GameManager.java @@ -0,0 +1,83 @@ +package victor.managers; + +import javax.swing.*; +import java.awt.*; + +public class GameManager { + private final int screenHeight; + private final int screenWidth; + + private int clickQuantity = 0; + + private int seconds = 0; + private int secondsToResolve; + private Thread coutingThread; + + public BoardManager boardManager; + + public GameManager(int boardSize) { + this.boardManager = new BoardManager(boardSize); + + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + double width = screenSize.getWidth(); + double height = screenSize.getHeight(); + + screenHeight = Math.min(boardManager.getBoardSize() * 100, (int) width); + screenWidth = Math.min(boardManager.getBoardSize() * 100, (int) height); + } + + public int getClickQuantity() { + return this.clickQuantity; + } + + public int getScreenHeight() { + return this.screenHeight; + } + + public int getScreenWidth() { + return this.screenWidth; + } + + public void addClick() { + clickQuantity++; + } + + public int getSeconds() { + return this.seconds; + } + + public int getSecondsToResolve() { + return this.secondsToResolve; + } + + public void startCounting(JLabel timeLabel) { + coutingThread = new Thread(() -> { + while (true) { + try { + Thread.sleep(1000); + this.seconds++; + timeLabel.setText("Tempo: " + this.seconds + " segs"); + } catch (InterruptedException e) { + break; + } + } + }); + + coutingThread.start(); + } + + public void stopCounting() { + this.coutingThread.interrupt(); + + this.secondsToResolve = this.seconds; + this.seconds = 0; + } + + public void restartGame() { + this.clickQuantity = 0; + this.seconds = 0; + this.secondsToResolve = 0; + this.boardManager.resetBoard(); + } +} + diff --git a/jogo-oito/src/main/java/victor/managers/ScreenManager.java b/jogo-oito/src/main/java/victor/managers/ScreenManager.java new file mode 100644 index 0000000..2758de1 --- /dev/null +++ b/jogo-oito/src/main/java/victor/managers/ScreenManager.java @@ -0,0 +1,54 @@ +package victor.managers; + +import javax.swing.*; + +import victor.screens.Game; +import victor.screens.Welcome; +import victor.screens.Winning; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.HashMap; + +public class ScreenManager implements ActionListener { + private GameManager gameManager; + private final HashMap screens = new HashMap<>(); + + public ScreenManager() { + screens.put("Welcome", new Welcome(this)); + } + + public GameManager getGameManager() { + return this.gameManager; + } + + @Override + public void actionPerformed(ActionEvent e) { + String buttonText = e.getActionCommand(); + JFrame welcome = screens.get("Welcome"); + + switch (buttonText) { + case "Começar" -> { + welcome.setVisible(false); + int boardSize = ((Welcome) welcome).getOptionSelected(); + + this.gameManager = new GameManager(boardSize); + screens.put("PlayGame", new Game(this)); + } + case "Voltar" -> { + screens.get("PlayGame").dispose(); + welcome.setVisible(true); + } + case "Win" -> { + screens.get("PlayGame").setEnabled(false); + screens.put("Win", new Winning(this)); + } + case "Tela inicial" -> { + screens.get("PlayGame").dispose(); + screens.get("Win").dispose(); + welcome.setVisible(true); + } + default -> System.out.println("Default"); + } + } +} diff --git a/jogo-oito/src/main/java/victor/screens/Game.java b/jogo-oito/src/main/java/victor/screens/Game.java new file mode 100644 index 0000000..3c4f621 --- /dev/null +++ b/jogo-oito/src/main/java/victor/screens/Game.java @@ -0,0 +1,168 @@ +package victor.screens; + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; + +import victor.managers.BoardManager; +import victor.managers.GameManager; +import victor.managers.ScreenManager; + +public class Game extends JFrame implements KeyListener { + private final ScreenManager screenManager; + private final GameManager gameManager; + private final BoardManager boardManager; + + private final JButton[][] buttons; + private final JLabel clickQuantityLabel = new JLabel("Tentativas: 0"); + private final JLabel timeLabel = new JLabel("Tempo: 0 segs"); + + public Game(ScreenManager screenManager) { + super("Jogo dos Oito"); + this.screenManager = screenManager; + this.gameManager = screenManager.getGameManager(); + this.boardManager = gameManager.boardManager; + + this.buttons = new JButton[boardManager.getBoardSize()][boardManager.getBoardSize()]; + + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + setSize(gameManager.getScreenWidth(), gameManager.getScreenHeight()); + setLocationRelativeTo(null); + + setLayout(new GridLayout(boardManager.getBoardSize() + 2, boardManager.getBoardSize())); + + gameManager.startCounting(this.timeLabel); + + addLabels(); + addButtons(); + + addKeyListener(this); + setFocusable(true); + + updateBoard(); + + setVisible(true); + } + + private void addLabels() { + if (boardManager.getBoardSize() % 2 != 0) { + add(timeLabel); + add(clickQuantityLabel); + + addWhiteSpaces(boardManager.getBoardSize() - 2); + } else { + addWhiteSpaces(boardManager.getBoardSize() / 2 - 1); + + add(timeLabel); + add(clickQuantityLabel); + + addWhiteSpaces(boardManager.getBoardSize() / 2 - 1); + } + } + + private void addButtons() { + for (int i = 0; i < boardManager.getBoardSize(); i++) { + for (int j = 0; j < boardManager.getBoardSize(); j++) { + JButton button = new JButton(); + button.setFont(new Font("Arial", Font.BOLD, 36)); + button.addActionListener(e -> onClick(button)); + + buttons[i][j] = button; + add(button); + } + } + + JButton backButton = new JButton("Voltar"); + backButton.addActionListener(screenManager); + + JButton restartButton = new JButton("Reiniciar"); + restartButton.addActionListener(e -> restartGame()); + + addBottomButtons(restartButton, backButton); + } + + private void addWhiteSpaces(int quantity) { + for (int i = 0; i < quantity; i++) { + add(new JLabel("")); + } + } + + private void addBottomButtons(JButton restartButton, JButton backButton) { + addWhiteSpaces(boardManager.getBoardSize() / 2 - 1); + + add(backButton); + add(restartButton); + + if (boardManager.getBoardSize() % 2 != 0) { + addWhiteSpaces(boardManager.getBoardSize() / 2); + } else { + addWhiteSpaces(boardManager.getBoardSize() / 2 - 1); + } + } + + @Override + public void dispose() { + super.dispose(); + gameManager.stopCounting(); + } + + private void onClick(JButton buttonClicked) { + var firstButtonPosition = boardManager.getBoardPosition(buttonClicked.getText()); + + if (firstButtonPosition == null) return; + + boolean isValidMove = boardManager.changeButtonsPositions(firstButtonPosition.getXPosition(), firstButtonPosition.getYPosition()); + + if (isValidMove) { + updateBoard(); + gameManager.addClick(); + clickQuantityLabel.setText("Tentativas: " + gameManager.getClickQuantity()); + } + + boolean isOrdered = boardManager.isOrdered(); + if (isOrdered) { + gameFinished(); + } + } + + private void gameFinished() { + gameManager.stopCounting(); + screenManager.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Win")); + } + + private void updateBoard() { + for (int i = 0; i < boardManager.getBoardSize(); i++) { + for (int j = 0; j < boardManager.getBoardSize(); j++) { + JButton button = buttons[i][j]; + int value = boardManager.getBoardValue(i, j); + button.setText(value == 0 ? "" : String.valueOf(value)); + } + } + } + + private void updateUI() { + updateBoard(); + clickQuantityLabel.setText("Tentativas: " + gameManager.getClickQuantity()); + timeLabel.setText("Tempo: " + gameManager.getSeconds() + " segs"); + } + + private void restartGame() { + gameManager.restartGame(); + updateUI(); + } + + public void keyPressed(KeyEvent e) { + } + + public void keyTyped(KeyEvent e) { + } + + public void keyReleased(KeyEvent e) { + } +} diff --git a/jogo-oito/src/main/java/victor/screens/Welcome.java b/jogo-oito/src/main/java/victor/screens/Welcome.java new file mode 100644 index 0000000..42c62be --- /dev/null +++ b/jogo-oito/src/main/java/victor/screens/Welcome.java @@ -0,0 +1,86 @@ +package victor.screens; + +import javax.swing.*; + +import victor.managers.ScreenManager; + +import java.awt.*; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +public class Welcome extends JFrame implements KeyListener { + private final JComboBox sizeOptions; + + public Welcome(ScreenManager screenManager) { + super("Welcome"); + + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + setSize(300, 300); + setLocationRelativeTo(null); + + setLayout(new GridBagLayout()); + GridBagConstraints c = new GridBagConstraints(); + + JLabel welcomeLabel = new JLabel("Bem vindo ao jogo dos oito"); + welcomeLabel.setHorizontalAlignment(SwingConstants.CENTER); + c.fill = GridBagConstraints.BOTH; + c.anchor = GridBagConstraints.CENTER; + c.weightx = 1; + c.gridwidth = 2; + c.gridx = 0; + c.gridy = 0; + add(welcomeLabel, c); + + JLabel boardSizeLabel = new JLabel("Tamanho do jogo"); + c.weightx = 0.75; + c.gridx = 0; + c.gridwidth = 1; + c.gridy = 1; + add(boardSizeLabel, c); + + Integer[] sizeOptions = {3, 4, 5, 6, 7, 8, 9}; + this.sizeOptions = new JComboBox<>(sizeOptions); + + c.weightx = 0.25; + c.gridx = 1; + c.gridy = 1; + add(this.sizeOptions, c); + + JButton startButton = new JButton(); + startButton.setText("Começar"); + startButton.setHorizontalAlignment(SwingConstants.CENTER); + startButton.addActionListener(screenManager); + + c.fill = GridBagConstraints.HORIZONTAL; + c.gridwidth = 2; + c.weightx = 0.5; + c.gridx = 0; + c.gridy = 2; + add(startButton, c); + + addKeyListener(this); + setFocusable(true); + + setVisible(true); + } + + public Integer getOptionSelected() { + return Integer.valueOf(String.valueOf(sizeOptions.getSelectedItem())); + } + + @Override + public void keyTyped(KeyEvent e) { + + } + + @Override + public void keyPressed(KeyEvent e) { + + } + + @Override + public void keyReleased(KeyEvent e) { + + } +} diff --git a/jogo-oito/src/main/java/victor/screens/Winning.java b/jogo-oito/src/main/java/victor/screens/Winning.java new file mode 100644 index 0000000..3933d68 --- /dev/null +++ b/jogo-oito/src/main/java/victor/screens/Winning.java @@ -0,0 +1,73 @@ +package victor.screens; + +import javax.swing.*; + +import victor.managers.ScreenManager; + +import java.awt.*; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +public class Winning extends JFrame implements KeyListener { + public Winning(ScreenManager screenManager) { + super("Welcome"); + + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + setSize(300, 300); + setLocationRelativeTo(null); + + setLayout(new GridBagLayout()); + GridBagConstraints c = new GridBagConstraints(); + + JLabel winningLabel = new JLabel("Parabéns, você conseguiu concluir o jogo!"); + winningLabel.setHorizontalAlignment(SwingConstants.CENTER); + c.fill = GridBagConstraints.BOTH; + c.anchor = GridBagConstraints.CENTER; + c.weightx = 1; + c.gridx = 0; + c.gridy = 0; + add(winningLabel, c); + + JLabel tries = new JLabel("Você precisou de " + screenManager.getGameManager().getClickQuantity() + " tentativas " + "e " + screenManager.getGameManager().getSecondsToResolve() + " segundos"); + tries.setHorizontalAlignment(SwingConstants.CENTER); + c.fill = GridBagConstraints.BOTH; + c.anchor = GridBagConstraints.CENTER; + c.weightx = 1; + c.gridx = 0; + c.gridy = 1; + add(tries, c); + + JButton inicialButton = new JButton(); + inicialButton.setText("Tela inicial"); + inicialButton.setHorizontalAlignment(SwingConstants.CENTER); + inicialButton.addActionListener(screenManager); + + c.fill = GridBagConstraints.HORIZONTAL; + c.gridwidth = 2; + c.weightx = 0.5; + c.gridx = 0; + c.gridy = 2; + add(inicialButton, c); + + addKeyListener(this); + setFocusable(true); + + setVisible(true); + } + + @Override + public void keyTyped(KeyEvent e) { + + } + + @Override + public void keyPressed(KeyEvent e) { + + } + + @Override + public void keyReleased(KeyEvent e) { + + } +} From ef97e109815302046f12d61ea5aa44e8429fc417 Mon Sep 17 00:00:00 2001 From: Victor Emmanuel Date: Mon, 8 May 2023 18:48:47 -0300 Subject: [PATCH 2/2] Removing the unused chat gpt code --- .../src/main/java/chat/gpt/JogoDosOito.java | 158 ------------------ 1 file changed, 158 deletions(-) delete mode 100644 jogo-oito/src/main/java/chat/gpt/JogoDosOito.java diff --git a/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java b/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java deleted file mode 100644 index e51dd08..0000000 --- a/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java +++ /dev/null @@ -1,158 +0,0 @@ -package chat.gpt; - -import java.awt.Font; -import java.awt.GridLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; - -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JOptionPane; - -public class JogoDosOito extends JFrame implements KeyListener { - - private int[][] tabuleiro = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } }; - private JButton[][] botoes = new JButton[3][3]; - private JButton botaoReiniciar; - - public JogoDosOito() { - super("Jogo dos Oito"); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setSize(300, 300); - setLayout(new GridLayout(4, 3)); - - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - JButton botao = new JButton(); - botao.setFont(new Font("Arial", Font.BOLD, 36)); - botoes[i][j] = botao; - add(botao); - } - } - - botaoReiniciar = new JButton("Reiniciar"); - botaoReiniciar.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - reiniciarJogo(); - } - }); - add(new JLabel("")); - add(botaoReiniciar); - add(new JLabel("")); - - addKeyListener(this); - setFocusable(true); - atualizarTabuleiro(); - setVisible(true); - } - - public void keyPressed(KeyEvent e) { - int keyCode = e.getKeyCode(); - switch (keyCode) { - case KeyEvent.VK_UP: - mover(1, 0); - break; - case KeyEvent.VK_DOWN: - mover(-1, 0); - break; - case KeyEvent.VK_LEFT: - mover(0, 1); - break; - case KeyEvent.VK_RIGHT: - mover(0, -1); - break; - } - } - - public void keyTyped(KeyEvent e) { - } - - public void keyReleased(KeyEvent e) { - } - - private void mover(int linha, int coluna) { - int linhaVazia = -1; - int colunaVazia = -1; - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - if (tabuleiro[i][j] == 0) { - linhaVazia = i; - colunaVazia = j; - } - } - } - int novaLinha = linhaVazia + linha; - int novaColuna = colunaVazia + coluna; - if (novaLinha < 0 || novaLinha > 2 || novaColuna < 0 || novaColuna > 2) { - // movimento inválido - return; - } - tabuleiro[linhaVazia][colunaVazia] = tabuleiro[novaLinha][novaColuna]; - tabuleiro[novaLinha][novaColuna] = 0; - atualizarTabuleiro(); - if (jogoConcluido()) { - JOptionPane.showMessageDialog(this, "Parabéns, você venceu!"); - reiniciarJogo(); - } - } - - public static void main(String[] args) { - new JogoDosOito(); - } - - private boolean jogoConcluido() { - int count = 1; - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - if (tabuleiro[i][j] != count % 9) { - return false; - } - count++; - } - } - return true; - } - - private boolean movimentarPeca(int linha, int coluna) { - if (linha > 0 && tabuleiro[linha - 1][coluna] == 0) { - tabuleiro[linha - 1][coluna] = tabuleiro[linha][coluna]; - tabuleiro[linha][coluna] = 0; - return true; - } else if (linha < 2 && tabuleiro[linha + 1][coluna] == 0) { - tabuleiro[linha + 1][coluna] = tabuleiro[linha][coluna]; - tabuleiro[linha][coluna] = 0; - return true; - } else if (coluna > 0 && tabuleiro[linha][coluna - 1] == 0) { - tabuleiro[linha][coluna - 1] = tabuleiro[linha][coluna]; - tabuleiro[linha][coluna] = 0; - return true; - } else if (coluna < 2 && tabuleiro[linha][coluna + 1] == 0) { - tabuleiro[linha][coluna + 1] = tabuleiro[linha][coluna]; - tabuleiro[linha][coluna] = 0; - return true; - } - return false; - } - - private void atualizarTabuleiro() { - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - JButton botao = botoes[i][j]; - int valor = tabuleiro[i][j]; - if (valor == 0) { - botao.setText(""); - } else { - botao.setText(String.valueOf(valor)); - } - } - } - } - - private void reiniciarJogo() { - tabuleiro = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } }; - atualizarTabuleiro(); - } -}