Skip to content

Commit

Permalink
Reliabilty issues fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandandai committed May 10, 2024
1 parent fb2dde6 commit 9f8b432
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
14 changes: 9 additions & 5 deletions src/Character.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
2 changes: 1 addition & 1 deletion src/Control.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void Adjust(int src, int dest){
*/
public void EndMove(){
updateFrame();
Game.getInstance().getCurrentCharacter().WakeUp();
Game.getInstance().getCurrentCharacter().wakeUp();
}

/**
Expand Down
27 changes: 15 additions & 12 deletions src/ElementButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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";
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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()){
Expand Down Expand Up @@ -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 ) ;
Expand All @@ -486,7 +489,7 @@ public void update(){
* @return
*/
public ArrayList<ElementButton> getNeighboursElementButton(ArrayList<ElementButton> eb){
if(element.getClass().getName().equals("Pipe")){
if(element instanceof Pipe){
List<Element> es = (List<Element>) element.getNeighbors();
ArrayList<ElementButton> toReturn = new ArrayList<ElementButton>();
for(Element eiter : es){
Expand All @@ -509,7 +512,7 @@ public ArrayList<ElementButton> getNeighboursElementButton(ArrayList<ElementButt
* @param eb - Az összes UI elementButton elem.
*/
public void drawWaterFlowDirection(Graphics g, ArrayList<ElementButton> eb){
if(element.getClass().getName().equals("Pump")){
if(element instanceof Pump){
Pump p = (Pump)element;
Element src = p.getSrc();
Element dest = p.getDest();
Expand All @@ -519,9 +522,9 @@ public void drawWaterFlowDirection(Graphics g, ArrayList<ElementButton> 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);
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/Pipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Pipe extends Element implements SaboteurPointSource {
private int sticky;
private int slimey;
private List<NonPipe> neighbors;
private Random random = new Random();

/**
* @author Szikszai Levente
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 12 additions & 3 deletions src/Prototype.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down Expand Up @@ -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();
Expand All @@ -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)
{
Expand All @@ -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");
}
Expand Down

0 comments on commit 9f8b432

Please sign in to comment.