From 8dcb29d701e076c63bbb43eb266621c91f79e03f Mon Sep 17 00:00:00 2001 From: beratkocak Date: Fri, 20 Dec 2024 14:04:03 +0100 Subject: [PATCH] added backonestep method to retract steps from user and repaint panel --- .../informatik/pr2projekt/gui/GameGUI.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/GameGUI.java b/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/GameGUI.java index 59cc330..03d5170 100644 --- a/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/GameGUI.java +++ b/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/GameGUI.java @@ -108,6 +108,63 @@ public class GameGUI { madeMoves.push(logEntrance); } + public static void backOneStep(Stack movesMade, JButton[][] buttons, String[][] colors, JPanel grid){ + try { + String move = movesMade.pop(); + String[] line = move.split("\\."); + String y = line[0]; + String x = line[1]; + String color = line[2]; + if(color.equals("W")){ + int i = Integer.parseInt(y); + int j = Integer.parseInt(x); + JButton b0 = buttons[i][j]; + b0.setOpaque(true); + b0.setForeground(Color.WHITE); + b0.setContentAreaFilled(true); + b0.setBorderPainted(false); + b0.setFocusPainted(false); + b0.setBackground(Color.BLACK); + buttons[i][j] = b0; + String str = colors[i][j]; + String str0 = str.substring(0, str.length() - 1); + colors[i][j] = str0; + grid.repaint(); + gridUpdate(grid, buttons); + }else if(color.equals("G")){ + int i = Integer.parseInt(y); + int j = Integer.parseInt(x); + JButton b0 = buttons[i][j]; + b0.setOpaque(true); + b0.setForeground(Color.BLACK); + b0.setContentAreaFilled(true); + b0.setBorderPainted(false); + b0.setFocusPainted(false); + b0.setBackground(Color.WHITE); + buttons[i][j] = b0; + String str = colors[i][j]; + String str0 = str.substring(0, str.length() - 1); + colors[i][j] = str0; + gridUpdate(grid, buttons); + } else if(color.equals("B")){ + int i = Integer.parseInt(y); + int j = Integer.parseInt(x); + JButton b0 = buttons[i][j]; + b0.setOpaque(true); + b0.setForeground(Color.BLACK); + b0.setContentAreaFilled(true); + b0.setBorderPainted(false); + b0.setFocusPainted(false); + b0.setBackground(Color.lightGray); + buttons[i][j] = b0; + String str = colors[i][j]; + String str0 = str.substring(0, str.length() - 1); + colors[i][j] = str0; + gridUpdate(grid, buttons); + } + } catch(EmptyStackException e) {} + } + public static void gridUpdate(JPanel grid, JButton[][] buttons){ grid.removeAll(); grid.repaint();