From 9f8b43288c22669d60d44c442c881f44a7621d6a Mon Sep 17 00:00:00 2001 From: rolandandai <87148863+rolandandai@users.noreply.github.com> Date: Fri, 10 May 2024 11:11:57 +0200 Subject: [PATCH] Reliabilty issues fixed --- src/Character.java | 14 +++++++++----- src/Control.java | 2 +- src/ElementButton.java | 27 +++++++++++++++------------ src/Pipe.java | 8 ++++++-- src/Prototype.java | 15 ++++++++++++--- 5 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/Character.java b/src/Character.java index c61cc0b..6c27f35 100644 --- a/src/Character.java +++ b/src/Character.java @@ -24,16 +24,20 @@ public int decreaseRemainingSteps() { } public synchronized void step() { resetRemainingSteps(); - try{ + try { wait(); - }catch(Exception e) - { + } + catch (ThreadDeath e) { + e.printStackTrace(); + throw e; + } + catch (Exception e) { e.printStackTrace(); } } public void makeSticky(){}/** Ragadossa teszi az adott csovet amin all*/ - public synchronized void WakeUp() { - this.notify(); + public synchronized void wakeUp() { + this.notifyAll(); } } diff --git a/src/Control.java b/src/Control.java index 34e6c91..22f5614 100644 --- a/src/Control.java +++ b/src/Control.java @@ -125,7 +125,7 @@ public void Adjust(int src, int dest){ */ public void EndMove(){ updateFrame(); - Game.getInstance().getCurrentCharacter().WakeUp(); + Game.getInstance().getCurrentCharacter().wakeUp(); } /** diff --git a/src/ElementButton.java b/src/ElementButton.java index d627a35..ba776f4 100644 --- a/src/ElementButton.java +++ b/src/ElementButton.java @@ -34,7 +34,7 @@ public class ElementButton extends JButton{ ElementButton(Element element) { this.element=element; - if(getImageName()!=""){ + if(!getImageName().isEmpty()){ try { /** * Beolvassa a megfelelő képfájlt, es atmeretezi 40x40-re. @@ -101,6 +101,9 @@ private Element getCurrentCharacterPlace() private boolean findElementInNeighbors(Element e) { boolean out = false; + if (e == null) { + return false; + } for(Element neighbor : e.getNeighbors()) { if(neighbor==element) @@ -157,7 +160,7 @@ private void showActionButtonWindow() { dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); /** Csak azokat a muveleteket jelenitjuk meg amit az adott karakterrel lehet vegezni.*/ - boolean isRepairman = Game.getInstance().getCurrentCharacter().getClass().getName().equals("Repairman"); + boolean isRepairman = Game.getInstance().getCurrentCharacter() instanceof Repairman; boolean hasNothing = isRepairman&&((Repairman)Game.getInstance().getCurrentCharacter()).getHoldingPipe()==null&& !((Repairman)Game.getInstance().getCurrentCharacter()).hasHoldingPump(); /** Letrehozunk egy JPanel objektumot az ActionButton-ok tarolasara*/ JPanel buttonPanel = new JPanel(new GridBagLayout()); @@ -252,7 +255,7 @@ public void actionPerformed(ActionEvent e) { } //Ha az elemen végre lehet hajtani javítást, akkor hozzáadunk egy ezt végrehajtó gombot - if(isRepairman && element.canPerformAction("Repair")&&place==element && (element.getClass().getName().equals("Pump") && ((Pump)element).getBroken() || element.getClass().getName().equals("Pipe") && ((Pipe)element).getHoleOnPipe())) { + if(isRepairman && element.canPerformAction("Repair")&&place==element && ((element instanceof Pump) && ((Pump)element).getBroken() || element instanceof Pipe && ((Pipe)element).getHoleOnPipe())) { ActionButton repairButton = new ActionButton(null); repairButton.setActionCommand("Repair"); repairButton.setText("Repair"); @@ -336,7 +339,7 @@ public void update(){ for(Character c : cs){ JLabel b = new JLabel(); String imgname; - if(c.getClass().getName().equals("Repairman")){ + if(c instanceof Repairman){ imgname = "man-mechanic.png"; if(((Repairman)c).hasHoldingPump()){ imgname = "man-with-pump.png"; @@ -359,7 +362,7 @@ public void update(){ Image img = ImageIO.read(getClass().getResource("img/" + imgname)); Image newimg = img.getScaledInstance( 20, 20, java.awt.Image.SCALE_SMOOTH ) ; b.setIcon(new ImageIcon(newimg)); - if(element.getClass().getName().equals("Pipe")){ + if(element instanceof Pipe){ b.setBounds(20, 20, 20, 20); }else{ b.setBounds(offset, 0, 20, 20); @@ -372,7 +375,7 @@ public void update(){ } // Állapotok ha pumpa - if(element.getClass().getName().equals("Pump")){ + if(element instanceof Pump){ Pump e = (Pump)element; if(e.getBroken()){ JLabel b = new JLabel(); @@ -401,7 +404,7 @@ public void update(){ } // Állapotok ha cső - if(element.getClass().getName().equals("Pipe")){ + if(element instanceof Pipe){ Pipe e = (Pipe)element; if(e.getHoleOnPipe()){ @@ -461,7 +464,7 @@ public void update(){ } } JLabel b = new JLabel(); - if(imgname!=""){ + if(!imgname.isEmpty()){ try { Image img = ImageIO.read(getClass().getResource("img/" + imgname)); Image newimg = img.getScaledInstance( 20, 20, java.awt.Image.SCALE_SMOOTH ) ; @@ -486,7 +489,7 @@ public void update(){ * @return */ public ArrayList getNeighboursElementButton(ArrayList eb){ - if(element.getClass().getName().equals("Pipe")){ + if(element instanceof Pipe){ List es = (List) element.getNeighbors(); ArrayList toReturn = new ArrayList(); for(Element eiter : es){ @@ -509,7 +512,7 @@ public ArrayList getNeighboursElementButton(ArrayList eb){ - if(element.getClass().getName().equals("Pump")){ + if(element instanceof Pump){ Pump p = (Pump)element; Element src = p.getSrc(); Element dest = p.getDest(); @@ -519,9 +522,9 @@ public void drawWaterFlowDirection(Graphics g, ArrayList eb){ Graphics2D g2 = (Graphics2D)g; g2.setStroke(new BasicStroke(2)); g2.setColor(Color.GREEN); - g2.fillOval((pe.getBounds().x + pe.getWidth()/2) + (int)(0.5*((srce.getBounds().x + srce.getWidth()/2)-(pe.getBounds().x + pe.getWidth()/2)))-10, (pe.getBounds().y + pe.getHeight()/2) + (int)(0.5*((srce.getBounds().y + srce.getHeight()/2)-(pe.getBounds().y + pe.getHeight()/2)))-10, 20, 20); + g2.fillOval((pe.getBounds().x + pe.getWidth()/2) + (int)(0.5*((srce.getBounds().x + srce.getWidth()/(double)2)-(pe.getBounds().x + pe.getWidth()/(double)2)))-10, (pe.getBounds().y + pe.getHeight()/2) + (int)(0.5*((srce.getBounds().y + srce.getHeight()/(double)2)-(pe.getBounds().y + pe.getHeight()/(double)2)))-10, 20, 20); g2.setColor(Color.RED); - g2.fillOval((pe.getBounds().x + pe.getWidth()/2) + (int)(0.5*((deste.getBounds().x + deste.getWidth()/2)-(pe.getBounds().x + pe.getWidth()/2)))-10, (pe.getBounds().y + pe.getHeight()/2) + (int)(0.5*((deste.getBounds().y + deste.getHeight()/2)-(pe.getBounds().y + pe.getHeight()/2)))-10, 20, 20); + g2.fillOval((pe.getBounds().x + pe.getWidth()/2) + (int)(0.5*((deste.getBounds().x + deste.getWidth()/(double)2)-(pe.getBounds().x + pe.getWidth()/(double)2)))-10, (pe.getBounds().y + pe.getHeight()/2) + (int)(0.5*((deste.getBounds().y + deste.getHeight()/(double)2)-(pe.getBounds().y + pe.getHeight()/(double)2)))-10, 20, 20); } } diff --git a/src/Pipe.java b/src/Pipe.java index b7ca566..0e25627 100644 --- a/src/Pipe.java +++ b/src/Pipe.java @@ -13,6 +13,7 @@ public class Pipe extends Element implements SaboteurPointSource { private int sticky; private int slimey; private List neighbors; + private Random random = new Random(); /** * @author Szikszai Levente @@ -60,7 +61,6 @@ public boolean accept(Character c) { if (slimey > 0) { success = false; if (neighbors.size() > 1) { - Random random = new Random(); if (random.nextBoolean()) { success = getNeighbors().get(0).accept(c); Control.getInstance().appendToLog("Slipped to " + getNeighbors().get(0).getName()); @@ -167,6 +167,10 @@ public Pipe placePump(Pump holdingPump) { // System.out.println("No placable pump"); } n = (NonPipe) getNeighbors().get(0); + if (holdingPump == null) { + Control.getInstance().appendToLog("HoldingPump is null, can't place it."); + return null; + } if (n != null) { removeNeighbor(n); n.removeNeighbor(this); @@ -199,7 +203,7 @@ public Pipe placePump(Pump holdingPump) { @Override public Pipe lift(int dir) { try { - if (neighbors.size() == 1 && neighbors.get(0).getClass().getName().equals("Cistern")) { + if (neighbors.size() == 1 && neighbors.get(0) instanceof Cistern) { return this; } } catch (IndexOutOfBoundsException e) { diff --git a/src/Prototype.java b/src/Prototype.java index 17cdb90..e5a0a2a 100644 --- a/src/Prototype.java +++ b/src/Prototype.java @@ -107,10 +107,11 @@ public void load(String file) pumps.clear(); File f = new File("mapdeclarations/"+file+".txt"); + Scanner sc = null; try { int readPhase = 0; - Scanner sc = new Scanner(f); + sc = new Scanner(f); sc.nextLine(); //kidobjuk az első sort while(sc.hasNext()) { @@ -276,6 +277,9 @@ else if(nonpipeN!=null) { System.out.println(file+".txt Load failed"); } + finally { + sc.close(); + } } System.out.println(file+".txt Load successful"); sc.close(); @@ -288,16 +292,16 @@ else if(nonpipeN!=null) int repPoints = 0; int _slimey = 3; int _sticky = 3; + Scanner scPoints = null; try{ File fPoints = new File(file+"Points.txt"); - Scanner scPoints = new Scanner(fPoints); + scPoints = new Scanner(fPoints); round = sc.nextInt(); repPoints = sc.nextInt(); sabPoints = sc.nextInt(); _slimey = sc.nextInt(); _sticky = sc.nextInt(); - scPoints.close(); } catch(Exception e) { @@ -307,6 +311,11 @@ else if(nonpipeN!=null) _slimey = 3; _sticky = 3; } + finally { + if (scPoints != null) { + scPoints.close(); + } + } Game.getInstance().load(gameElements, sabPointSource, cisterns, repairmanGroup, saboteurGroup, repPoints, sabPoints, round,_slimey,_sticky,pumps); //System.out.println(file+"Points.txt Load Successful"); }