diff --git a/Arctic_Nigthmare/src/arctic_nightmare/Board.java b/Arctic_Nigthmare/src/arctic_nightmare/Board.java index 85d6f0facc0542b9ea849a0771cc581f2333dbb7..231039e2782e90aae88378eba4e97582d8dcf97e 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/Board.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/Board.java @@ -1,7 +1,4 @@ package arctic_nightmare; -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; @@ -17,10 +14,10 @@ public class Board { public Board(int field_num , int column_num) { fields = new ArrayList<Field>(); for(int i = 0; i < field_num; ++i) { - if(i % 2 == 0) - fields.add(new StableField()); - else - fields.add(new UnstableField()); + if(i % 2 == 0) + fields.add(new StableField()); + else + fields.add(new UnstableField()); } columns = column_num; setNeighbors(); @@ -31,6 +28,7 @@ public class Board { fields = new ArrayList<Field>(); id = idcounter++; } + public void addField(Field f) { fields.add(f); } @@ -41,37 +39,6 @@ public class Board { } - /*Only for proto*/ - public int getid() {return id;} - - /*Only for proto*/ - public void save() - { - save(System.out); - save(Logger.getFileStream()); - } - - public void save(OutputStream os) { - PrintWriter pw = new PrintWriter(os); - String s = new String("Board("); - s += getid() + "): fields:"; - if(fields != null && fields.size() != 0) - for(int i = 0; i < fields.size(); ++i) { - s += fields.get(i).getid(); - if(i == fields.size() - 1) - s += ";"; - s += " "; - } - else - s += "null"; - s += "\n"; - pw.write(s); - pw.flush(); - pw = null; - for(Field f : fields) - f.save(os); - } - public Field getfield(int i) { if(i < 0 || i > fields.size()) throw new ArrayIndexOutOfBoundsException(); @@ -82,37 +49,15 @@ public class Board { public void setNeighbors() { int n = fields.size(); for(int i = 0; i < n; ++i) { - Field f = fields.get(i); - if(i >= columns) - f.addNeighbor(fields.get(i - columns)); - if(i % columns > 0) - f.addNeighbor(fields.get(i - 1)); - if(i % columns < columns - 1) - f.addNeighbor(fields.get(i + 1)); - if(i < n - columns) - f.addNeighbor(fields.get(i + columns)); - } - } - - /*Only for proto*/ - /*Use only when the fields lay in grid, and board1s size is rows*colomn*/ - public void draw_minimap(OutputStream os) { - PrintWriter pw = new PrintWriter(os); - for(int i = 0; i < fields.size() / columns; ++i) { - for(int j = 0; j < columns; ++j) { - pw.format("%03d", fields.get(i * columns + j).getid()); - if(j != columns - 1) - pw.write("|"); - } - pw.write("\n"); - if(i != fields.size() / columns -1) - for(int j = 0; j < columns; ++j) { - if(j != columns - 1) - pw.write("---|"); - else - pw.write("---\n"); - } + Field f = fields.get(i); + if(i >= columns) + f.addNeighbor(fields.get(i - columns)); + if(i % columns > 0) + f.addNeighbor(fields.get(i - 1)); + if(i % columns < columns - 1) + f.addNeighbor(fields.get(i + 1)); + if(i < n - columns) + f.addNeighbor(fields.get(i + columns)); } - pw.flush(); } } diff --git a/Arctic_Nigthmare/src/arctic_nightmare/Eskimo.java b/Arctic_Nigthmare/src/arctic_nightmare/Eskimo.java index 21381eb0fd923ce657ddc2c8943c71d6b504f039..3f94d1d8521cf6eec0feea61b28a4fa2b452ea87 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/Eskimo.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/Eskimo.java @@ -1,17 +1,23 @@ package arctic_nightmare; public class Eskimo extends Person { - public Eskimo(String name, Field f, Game g) { - super(name, f, g); - maxTemp = 5; - bodyTemp = 5; - } - - public Eskimo(String name, Game g) { - super(name, g); - maxTemp = 5; - bodyTemp = 5; - } + public Eskimo(String name, Field f, Game g) { + super(name, f, g); + maxTemp = 5; + bodyTemp = 5; + } + + public Eskimo(String name, Game g) { + super(name, g); + maxTemp = 5; + bodyTemp = 5; + } + + public Eskimo(String name, Field f) throws PlayerDiedException { + super(name, f); + maxTemp = 5; + bodyTemp = 5; + } //public Eskimo(String name) { @@ -24,9 +30,4 @@ public class Eskimo extends Person { if(this.field.setIgloo()) work--; } - - /*Only for proto*/ - public String get_type_name() { - return "Eskimo"; - } } diff --git a/Arctic_Nigthmare/src/arctic_nightmare/Event.java b/Arctic_Nigthmare/src/arctic_nightmare/Event.java index 387e1af6c4bb35cb453f1b4de8d7819304d395e8..00b42884456b466623f5dee5a0b736bd6140d9f3 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/Event.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/Event.java @@ -1,9 +1,7 @@ package arctic_nightmare; public class Event { - private int code; - private Object[] params; - public Event(int c, Object[] o) { - } - + private int code; + private Object[] params; + public Event(int c, Object[] o) {} } diff --git a/Arctic_Nigthmare/src/arctic_nightmare/Explorer.java b/Arctic_Nigthmare/src/arctic_nightmare/Explorer.java index fb1db9a31e061b2fe15be4d08318077522536a76..c64fc4835be16c2b2168fbaac067dba8b9928bab 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/Explorer.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/Explorer.java @@ -3,17 +3,23 @@ package arctic_nightmare; public class Explorer extends Person { - public Explorer(String name, Field f, Game g) { - super(name, f, g); - maxTemp = 4; - bodyTemp = 4; - } - - public Explorer(String name, Game g) { - super(name, g); - maxTemp = 4; - bodyTemp = 4; - } + public Explorer(String name, Field f, Game g) { + super(name, f, g); + maxTemp = 4; + bodyTemp = 4; + } + + public Explorer(String name, Game g) { + super(name, g); + maxTemp = 4; + bodyTemp = 4; + } + + public Explorer(String name, Field f) throws PlayerDiedException { + super(name, f); + maxTemp = 5; + bodyTemp = 5; + } //public Explorer(String name) { // super(name); @@ -26,10 +32,5 @@ public class Explorer extends Person { if(field.isNeighbor(f)) if(f.revealCapacity()) work--; - } - - /*Only for proto*/ - public String get_type_name() { - return "Explorer"; } } diff --git a/Arctic_Nigthmare/src/arctic_nightmare/Field.java b/Arctic_Nigthmare/src/arctic_nightmare/Field.java index fe9642af0463f3c1cdb6a69307cbc1dff12e87e2..9e51ab4a2240a1dc72cd1fb5721323136f967284 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/Field.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/Field.java @@ -1,7 +1,5 @@ package arctic_nightmare; -import java.io.OutputStream; -import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; @@ -57,14 +55,17 @@ public abstract class Field { item = it; } + public void addItem(String it) { + item = new Item(it); + } + public void addNeighbor(Field f) { neighbors.add(f); } - - //For initializing components - public abstract boolean canAddPlayer(); - //For initializing components - public void addPlayer(Person p) { + //For initializing components + public abstract boolean canAddPlayer(); + //For initializing components + public void addPlayer(Person p) { persons.add(p); } @@ -91,10 +92,10 @@ public abstract class Field { public boolean emitItem(){ if(snow ==0 && !itemPickable) - { - this.itemPickable= true; // ha nincs mar a mezon ho, az item lathatova valik - return true; - } + { + this.itemPickable= true; // ha nincs mar a mezon ho, az item lathatova valik + return true; + } return false; } @@ -140,87 +141,20 @@ public abstract class Field { public boolean revealCapacity() { - if(visibleCapacity) - return false; + if(visibleCapacity) return false; visibleCapacity = true; return true; } public boolean setIgloo(){ - if(hasIgloo) - return false; + if(hasIgloo) return false; hasIgloo = true; return true; } public boolean setTent(Tent t) { - if(tent != null) - return false; + if(tent != null) return false; tent = t; return true; } - - /*Only for proto*/ - public int getid() - { - return id; - } - - /*Only for proto*/ - public void save() - { - save(System.out); - save(Logger.getFileStream()); - } - - public void save(OutputStream os) - { - PrintWriter pw = new PrintWriter(os); - String s = new String(get_type_name() + "("); - s += getid() + "): "; - s += "snow:" + snow + "; "; - s += "capacity:" + capacity + "; "; - s += "hasIgloo:" + hasIgloo + "; "; - s += "itemPickable:" + itemPickable + "; "; - s += "neighbors:"; - if(neighbors != null && neighbors.size() != 0) - for(int i = 0; i < neighbors.size(); ++i) { - s += neighbors.get(i).getid(); - if(i == neighbors.size() -1) - s += ";"; - s += " "; - } - else - s += "null; "; - s += "persons:"; - if(persons != null && persons.size() != 0) - for(int i = 0; i < persons.size(); ++i) { - s += persons.get(i).getid(); - if(i == persons.size() - 1) - s += ";"; - s += " "; - } - else - s += "null; "; - s += "bear:"; - if(bear != null) - s += bear.getid() + "; "; - else - s += "null; "; - s += "item:"; - if(item != null) - s += item.getName() + "; "; - else - s += "null; "; - s += "tent: "; - if(tent != null) - s += tent.getid() + "\n"; - else - s += "null\n"; - pw.write(s); - pw.flush(); - } - - /*Only for proto*/ - public abstract String get_type_name(); } diff --git a/Arctic_Nigthmare/src/arctic_nightmare/FragileShovel.java b/Arctic_Nigthmare/src/arctic_nightmare/FragileShovel.java index b5e9f31c1629755684d03879d7387e5903547c63..60516721a1bdadaa195d67878bb9189fe2e2bbd6 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/FragileShovel.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/FragileShovel.java @@ -1,30 +1,16 @@ package arctic_nightmare; -import java.io.OutputStream; -import java.io.PrintWriter; - public class FragileShovel extends Item{ - private int remainingUse; - - public FragileShovel() { - super("FragileShovel"); - remainingUse = 3; - } - - public boolean use(Field f) { - remainingUse--; - if(remainingUse == 0) - return false; - return true; - } - - @Override - public void save(OutputStream os) { - PrintWriter pw = new PrintWriter(os); - String s = new String("Item(" + getid() + "): name:"); - s += getName() + "; "; - s += "remainingUse:" + remainingUse + "\n"; - pw.write(s);; - pw.flush(); - } + private int remainingUse; + + public FragileShovel() { + super("FragileShovel"); + remainingUse = 3; + } + + public boolean use(Field f) { + remainingUse--; + if(remainingUse == 0) return false; + return true; + } } diff --git a/Arctic_Nigthmare/src/arctic_nightmare/Game.java b/Arctic_Nigthmare/src/arctic_nightmare/Game.java index ac97e944a7dc4db27200fb1cc01f80876ad52f91..c73ee7ecb2b0740267315abf08cecbc9b746fe01 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/Game.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/Game.java @@ -1,22 +1,7 @@ package arctic_nightmare; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.PrintWriter; -import java.nio.charset.Charset; - -import static java.lang.System.in; import java.util.ArrayList; import java.util.List; -import java.util.Scanner; -import javax.swing.JFrame; -import javax.swing.JTextArea; public class Game{ private Person actualPlayer; @@ -57,20 +42,18 @@ public class Game{ public void play() { try { - while(true) { - actualPlayer.enablePlayer(); - nextPlayer(); - } + while(true) { + actualPlayer.enablePlayer(); + nextPlayer(); + } } catch(PlayerDiedException e) { - gameOver(); + gameOver(); } catch(PlayersWinException e) { - win = true; - } - finally { - + win = true; } + finally {} } @@ -79,15 +62,14 @@ public class Game{ bear = new IceBear(board.getfield(0)); board.getfield(0).bear = bear; for(int i = 0; i < playernum; ++i) { - Person newperson = null; - if(i % 2 == 0) - newperson = new Eskimo("Fakename", this); - else - newperson = new Explorer("Fakename", board.getfield((i + 1) * 2), this); - players.add(newperson); - if(board.getfield((i + 1) * 2).canAddPlayer()) - board.getfield((i + 1) * 2).addPlayer(newperson); - newperson.addField(board.getfield((i + 1) * 2)); + Person newperson = null; + if(i % 2 == 0) + newperson = new Eskimo("Fakename", this); + else + newperson = new Explorer("Fakename", board.getfield((i + 1) * 2), this); + players.add(newperson); + if(board.getfield((i + 1) * 2).canAddPlayer()) board.getfield((i + 1) * 2).addPlayer(newperson); + newperson.addField(board.getfield((i + 1) * 2)); } actualPlayer = players.get(0); } @@ -97,69 +79,6 @@ public class Game{ } void win() throws PlayersWinException{ - throw new PlayersWinException(); - } - - /*Only for proto*/ - public void save() throws IOException - { - save(System.out); - save(Logger.getFileStream()); - } - - public void save(OutputStream os) throws IOException { - String string = new String("Game(" + id + "): players:"); - if(players != null && players.size() != 0) - for(int i = 0; i < players.size(); ++i) { - string += players.get(i).getid(); - if( i == players.size() - 1) - string += ";"; - string += " "; - } - else - string += "null; "; - string += "actualPlayer:"; - if(actualPlayer != null) - string += actualPlayer.getid() + "; "; - else - string += "null; "; - string += "bear:"; - if(bear != null) - string += bear.getid() + "; "; - else - string += "null; "; - string += "board:"; - if(board != null) - string += board.getid() + "; "; - else - string += "null; "; - string += "win:" + win + "\n"; - PrintWriter pw = new PrintWriter(os); - pw.write(string); - pw.flush(); - pw = null; - if(bear != null) - bear.save(os); - if(board != null) - board.save(os); - if(players != null && players.size() != 0) - for(Person p : players) - p.save(os); - if(items != null && items.size() != 0) - for(Item i : items) - i.save(os); - } - - /*Only for proto*/ - public int getid() {return id;} - - /*Pnly for proto*/ - public void draw_minimap() { - board.draw_minimap(System.out); - board.draw_minimap(Logger.getFileStream()); - } - - public void draw_minimap(OutputStream os) { - board.draw_minimap(os); + //throw new PlayersWinException(); } } diff --git a/Arctic_Nigthmare/src/arctic_nightmare/IceBear.java b/Arctic_Nigthmare/src/arctic_nightmare/IceBear.java index a73a0ac074f65abe0dd5de2fafb451721937b563..3805c19ec08bec6a8d86c4365c666bf2c9d75c1a 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/IceBear.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/IceBear.java @@ -1,52 +1,21 @@ package arctic_nightmare; -import java.io.OutputStream; -import java.io.PrintWriter; import java.util.List; import java.util.Random; public class IceBear { - private Field field; + private Field field; - /*Only for proto*/ - private int id; - private static int idcounter = 1; - - - public IceBear(Field f) { - field = f; - id = idcounter++; - } - public void step() throws PlayerDiedException { - Random rand = new Random(); - List<Field> neighborList = field.getNeighbors(); - int rand_idx = rand.nextInt(neighborList.size()); - Field nextField = field.getNeighbors().get(rand_idx); - field.removeBear(); - field = nextField; - nextField.accept(this); - } - - /*Only for proto*/ - public int getid() {return id;} - - /*Only for proto*/ - public void save() - { - save(System.out); - save(Logger.getFileStream()); + public IceBear(Field f) { + field = f; } - - public void save(OutputStream os) { - PrintWriter pw = new PrintWriter(os); - String s = new String("IceBear("); - s += id + "): field:"; - if(field != null) - s += field.getid(); - else - s += "null"; - s += "\n"; - pw.write(s); - pw.flush(); - } + public void step() throws PlayerDiedException { + Random rand = new Random(); + List<Field> neighborList = field.getNeighbors(); + int rand_idx = rand.nextInt(neighborList.size()); + Field nextField = field.getNeighbors().get(rand_idx); + field.removeBear(); + field = nextField; + nextField.accept(this); + } } diff --git a/Arctic_Nigthmare/src/arctic_nightmare/Item.java b/Arctic_Nigthmare/src/arctic_nightmare/Item.java index 255e84d21b062c96080a048b7528b1f088cf1f84..9e3074c78f8bcb1ca81febda9a898ae2d885dbd7 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/Item.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/Item.java @@ -1,40 +1,13 @@ package arctic_nightmare; -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintWriter; - public class Item { private String name; - - /*Only for proto*/ - private int id; - private static int idcounter = 1; public Item(String s) { name = new String(s); - id = idcounter++; } - - /*Only for proto*/ - public int getid() {return id;} public String getName() { return name;} public boolean use(Field f) {return true;} - - /*Only for proto*/ - public void save() - { - save(System.out); - save(Logger.getFileStream()); - } - - public void save(OutputStream os) { - PrintWriter pw = new PrintWriter(os); - String s = new String("Item(" + id + "): name:"); - s += name + "\n"; - pw.write(s);; - pw.flush(); - } } diff --git a/Arctic_Nigthmare/src/arctic_nightmare/Logger.java b/Arctic_Nigthmare/src/arctic_nightmare/Logger.java index ff06372471d892d00f5f3ddac4a76f3b639db085..d2084b0af361fad155a5ef7e782ada9c9e140749 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/Logger.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/Logger.java @@ -7,7 +7,6 @@ import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Scanner; -import java.util.logging.Level; final public class Logger { @@ -23,7 +22,6 @@ final public class Logger { if (! directory.exists())directory.mkdir(); logfile = DateTimeFormatter.ofPattern("yyyy_MM_dd-HH_mm_ss").format(LocalDateTime.now()).toString() + extension; File file = new File(library + logfile); - System.out.println(file.toString()); try { fileoutput = new FileOutputStream(file); } catch (FileNotFoundException ex) { @@ -61,31 +59,50 @@ final public class Logger { if (1 > tabs) tabs =0; } - static void Log(String message) + public static void Log(String message) { try { if (tabs != 0) { String output = String.format("%0" + tabs + "d", 0).replace("0", "\t") + message; System.out.println(output); - fileoutput.write(output.getBytes()); + fileoutput.write((output + "\n").getBytes()); } else { System.out.println(message); - fileoutput.write(message.getBytes()); + fileoutput.write((message + "\n").getBytes()); } } catch (IOException ex) { Log(ex.toString(), false); } } + + public static void LogNoNewLine(String message) + { + try { + if (tabs != 0) + { + String output = String.format("%0" + tabs + "d", 0).replace("0", "\t") + message; + System.out.print(output); + fileoutput.write(output.getBytes()); + } + else + { + System.out.print(message); + fileoutput.write(message.getBytes()); + } + } catch (IOException ex) { + Log(ex.toString(), false); + } + } - static void Log(String message, boolean tabulated) { + public static void Log(String message, boolean tabulated) { if(!tabulated) { System.out.println(message); try { - fileoutput.write(message.getBytes()); + fileoutput.write((message + "\n").getBytes()); } catch (IOException ex) { Log(ex.toString(), false); } @@ -93,6 +110,19 @@ final public class Logger { else Log(message); } + public static void LogNoNewLine(String message, boolean tabulated) { + if(!tabulated) + { + System.out.print(message); + try { + fileoutput.write(message.getBytes()); + } catch (IOException ex) { + Log(ex.toString(), false); + } + } + else Log(message); + } + static void LogAndIncreaseTabs(String message) { Log(message); diff --git a/Arctic_Nigthmare/src/arctic_nightmare/Main.java b/Arctic_Nigthmare/src/arctic_nightmare/Main.java index e6d5d2a73928fde0e2247cbe861692c557ece30d..12368cf59302ddb3440e45d685ff94ab7b738770 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/Main.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/Main.java @@ -10,7 +10,7 @@ public class Main { public static void main(String[] args) throws IOException { Game game = new Game(); game.newGame(9, 3, 4); - game.save(); - game.draw_minimap(); + //game.save(); + //game.draw_minimap(); } } diff --git a/Arctic_Nigthmare/src/arctic_nightmare/Person.java b/Arctic_Nigthmare/src/arctic_nightmare/Person.java index 922c7bebf010a009f084d18df2623287d3bcb337..f9a8c6865495a02dbef2f337c528c68d1f405132 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/Person.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/Person.java @@ -1,290 +1,247 @@ -package arctic_nightmare; - -import java.util.List; -import java.io.OutputStream; -import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.Vector; - -import org.w3c.dom.events.Event; - -public abstract class Person { - protected int bodyTemp; - protected int maxTemp; - protected int work; - private String name; //Teszt kiiratashoz - protected Field field; - private Vector<Item> items; - private Game game; - private Tent builtTent; - - /*Only for proto*/ - private int id; - private static int idcounter = 1; - - - - ///Person konstruktorba kell Game mindenképpen - - public Person(String name, Field f, Game g) { - work = 0; - this.name = name; - field = f; - items = new Vector<Item>(); - game = g; - builtTent = null; - id = idcounter++; - } - - public Person(String name, Game g) { - work = 0; - this.name = name; - field = null; - items = new Vector<Item>(); - game = g; - builtTent = null; - id = idcounter++; - } - - /*Person(String name, Field field) - { - items = new Vector<>(); - this.field = field; - field.accept(this); - this.name = name; - work = 4; - id = idcounter++; - }*/ - - Person(String name) - { - items = new Vector<>(); - this.name = name; - work = 4; - id = idcounter++; - } - - /*Only for proto*/ - public int getid() {return id;} - - public void assembleFlare() throws PlayersWinException{ - List<Person> persons = field.getPersons(); - String parts[] = {"Pistol", "Cartridge", "Flare"}; - boolean b[] = {false, false, false}; - for(int i = 0; i <3; ++i) { - for(Person p : persons) { - if(p.hasItem(parts[i])){ - b[i] = true; - break; - } - } - } - if(b[0] && b[1] && b[2] && persons.size() == game.getPlayerNumber()) { - game.win(); - } - - } - - public void die() throws PlayerDiedException - { - throw new PlayerDiedException(); - } - - public void dig(){ - if(this.field.emitItem()) work--; - } - - public void fall() throws PlayerDiedException - { - if(hasItem("DivingSuit")) { - step(field.getNeighbors().get(0)); - } - else { - ArrayList<Person> potentialhelpers = findHelpers(); - boolean saved = false; - if(!potentialhelpers.isEmpty()) { - for (Person person : potentialhelpers) { - saved = person.pullOut(this); - if(saved) return; - } - } - die(); - } - } - - public String getName() - { - return name; - } - - public int getWork(){ - return work; - } - - public boolean hasItem(String itemname){ - for(Item it : items) - if(it.getName().equals(itemname)) - return true; - return false; - } - - public void hitByBlizzard() throws PlayerDiedException{ - this.bodyTemp--; - if(this.bodyTemp==0) - die(); - } - - public ArrayList<Person> findHelpers(){ - ArrayList<Person> potentialhelpers = new ArrayList<Person>(); - List<Field> neighbors = field.getNeighbors(); - for(Field neighbor: neighbors) { //iteralas az osszes iranyt tartalmazo tombon - potentialhelpers.addAll(neighbor.getPersons()); - } - return potentialhelpers; - } - - public void pickUpItem() - { - Item item = field.getItem(); - if (item != null) - { - if(item.getName().equals("Food")) - { - if(bodyTemp < maxTemp) - bodyTemp++; // ha elelmet talalt a person azt nem tarolja el, hanem megeszi es noveli vele a bodyTempjét - } - else - { - if(!items.contains(item)) { - items.add(item); - work --; - } - } - } - } - - public boolean pullOut(Person p) throws PlayerDiedException{ - if(hasItem("Rope")) { - p.step(field); - return true; - } - return false; - } - - public void setWork(int i) - { - work = i; - } - - public void shovel() - { - int snow_layers = 1; - if(hasItem("Shovel") || hasItem("FragileShovel")) - snow_layers = 2; - if(field.clearSnow(snow_layers)){ - work--; - for(int i = 0; i < items.size(); ++i) { - Item it = items.get(i); - if(it.getName() == "FragileShovel") - if(!it.use(null)) { - items.remove(it); - return; - } - } - } - } - - - public void step(Field f) throws PlayerDiedException - { - if(field.isNeighbor(f)) { - this.field.remove(this); - work--; - f.accept(this); - } - } - - public void setBodyTemp(int bodyTemp) { - this.bodyTemp=bodyTemp; - } - - //Segedfuggveny a teszthez. Nem a teszteset soran, hanem mar korabban megszerzett targy hozzaadása szereplohoz - public void addItem(Item item) { - if(!items.contains(item)) - items.add(item); - } - - //For initializing components - public void addField(Field f) { - field = f; - } - - public void enablePlayer() throws PlayerDiedException, PlayersWinException { - setWork(4); - if(builtTent != null) - builtTent.removeFromField(); - return; - //switch(Event.code) { - //case 1: step((Field) event.params[0]); - //case 2: func2((Field) event.params[0], (Item) event.params[1]); - //} - } - - public void buildTent() { - if(work > 0) { - for(Item it : items) { - if(it.getName().equals("Tent")) { - if(it.use(field)) { - items.remove(it); - work--; - } - break; - } - } - } - } - /*Only for proto*/ - public void save() - { - save(System.out); - save(Logger.getFileStream()); - } - - public void save(OutputStream os) { - PrintWriter pw = new PrintWriter(os); - String s = new String(get_type_name() + "(" + id + "): "); - s += "bodyTemp:" + bodyTemp + "; "; - s += "maxTemp:" + maxTemp + "; "; - s += "work:" + work + "; "; - s += "game:"; - if(game != null) - s += game.getid() + "; "; - else - s += "null; "; - s += "field:"; - if(field != null) - s += field.getid() + "; "; - else - s += "null; "; - s += "items:"; - if(items != null && items.size() != 0) - for(int i = 0; i < items.size(); ++i) { - s += items.get(i).getid(); - if(i == items.size() - 1) - s += ";"; - s += " "; - } - else - s += "null; "; - s += "builtTent:"; - if(builtTent != null) - s += builtTent.getid() + "\n"; - else - s += "null\n"; - pw.write(s); - pw.flush(); - } - - /*Only for proto*/ - public abstract String get_type_name(); -} +package arctic_nightmare; + +import java.util.List; +import java.util.ArrayList; +import java.util.Vector; + + +public abstract class Person { + protected int bodyTemp; + protected int maxTemp; + protected int work; + private String name; //Teszt kiiratashoz + protected Field field; + private Vector<Item> items; + private Game game; + private Tent builtTent; + + ///Person konstruktorba kell Game mindenképpen + + public Person(String name, Field f, Game g) { + work = 0; + this.name = name; + field = f; + items = new Vector<Item>(); + game = g; + builtTent = null; + } + + public Person(String name, Game g) { + work = 0; + this.name = name; + field = null; + items = new Vector<Item>(); + game = g; + builtTent = null; + } + + Person(String name, Field field) throws PlayerDiedException + { + items = new Vector<>(); + this.field = field; + field.accept(this); + this.name = name; + work = 4; + } + + Person(String name) + { + items = new Vector<>(); + this.name = name; + work = 4; + } + + public void assembleFlare() throws PlayersWinException{ + List<Person> persons = field.getPersons(); + String parts[] = {"Pistol", "Cartridge", "Flare"}; + boolean b[] = {false, false, false}; + for(int i = 0; i <3; ++i) { + for(Person p : persons) { + if(p.hasItem(parts[i])){ + b[i] = true; + break; + } + } + } + if(b[0] && b[1] && b[2] && persons.size() == game.getPlayerNumber()) { + throw new PlayersWinException(name + "shoot the flare, and the good guys win again!"); + } + + } + + public void die() throws PlayerDiedException + { + this.bodyTemp = 0; + throw new PlayerDiedException(name + " died on the icefield alone and miserably!"); + } + + public void dig(){ + if(this.field.emitItem()) work--; + } + + public void fall() throws PlayerDiedException + { + if(hasItem("DivingSuit")) { + step(field.getNeighbors().get(0)); + } + else { + ArrayList<Person> potentialhelpers = findHelpers(); + boolean saved = false; + if(!potentialhelpers.isEmpty()) { + for (Person person : potentialhelpers) { + saved = person.pullOut(this); + if(saved) return; + } + } + die(); + } + } + + public String getName() + { + return name; + } + + public int getWork(){ + return work; + } + + public boolean hasItem(String itemname){ + for(Item it : items) + if(it.getName().equals(itemname)) + return true; + return false; + } + + public void hitByBlizzard() throws PlayerDiedException{ + this.bodyTemp--; + if(this.bodyTemp==0) die(); + } + + public ArrayList<Person> findHelpers(){ + ArrayList<Person> potentialhelpers = new ArrayList<Person>(); + List<Field> neighbors = field.getNeighbors(); + for(Field neighbor: neighbors) { //iteralas az osszes iranyt tartalmazo tombon + potentialhelpers.addAll(neighbor.getPersons()); + } + return potentialhelpers; + } + + public void pickUpItem() + { + Item item = field.getItem(); + if (item != null) + { + if(item.getName().equals("Food")) + { + if(bodyTemp < maxTemp) bodyTemp++; // ha elelmet talalt a person azt nem tarolja el, hanem megeszi es noveli vele a bodyTempjét + } + else + { + if(!items.contains(item)) { + items.add(item); + work --; + } + } + } + } + + public boolean pullOut(Person p) throws PlayerDiedException{ + if(hasItem("Rope")) { + p.step(field); + return true; + } + return false; + } + + public void setWork(int i) + { + work = i; + } + + public void shovel() + { + int snow_layers = 1; + if(hasItem("shovel") || hasItem("FragileShovel")) snow_layers = 2; + if(field.clearSnow(snow_layers)) + { + work--; + for(int i = 0; i < items.size(); ++i) + { + Item it = items.get(i); + if(items.get(i).getName().equals("FragileShovel")) + { + if(!it.use(null)) + { + items.remove(it); + return; + } + } + } + } + } + + + public void step(Field f) throws PlayerDiedException + { + if(field.isNeighbor(f)) { + this.field.remove(this); + work--; + f.accept(this); + } + } + + public void setBodyTemp(int bodyTemp) { + this.bodyTemp=bodyTemp; + } + + //Segedfuggveny a teszthez. Nem a teszteset soran, hanem mar korabban megszerzett targy hozzaadása szereplohoz + public void addItem(Item item) { + if(!items.contains(item)) + items.add(item); + } + + public void addItem(String item) { + Item newitem; + if(item.equals("FragileShovel")) { + newitem = new FragileShovel(); + } + else { + if(item.equals("Tent")) + newitem = new Tent(); + else + newitem = new Item(item); + } + if(!items.contains(newitem)) + items.add(newitem); + } + + //For initializing components + public void addField(Field f) { + field = f; + } + + public void enablePlayer() throws PlayerDiedException, PlayersWinException { + setWork(4); + if(builtTent != null) builtTent.removeFromField(); + return; + //switch(Event.code) { + //case 1: step((Field) event.params[0]); + //case 2: func2((Field) event.params[0], (Item) event.params[1]); + //} + } + + public void buildTent() { + if(work > 0) { + for(Item it : items) { + if(it.getName().equals("Tent")) { + if(it.use(field)) { + items.remove(it); + work--; + } + break; + } + } + } + } +} diff --git a/Arctic_Nigthmare/src/arctic_nightmare/PlayerDiedException.java b/Arctic_Nigthmare/src/arctic_nightmare/PlayerDiedException.java index 314d58d2378a7ac835a3dc8217a7539f0f0b6f98..8d0f2977747df3261ed237d6b6a2770ad056e6dd 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/PlayerDiedException.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/PlayerDiedException.java @@ -1,10 +1,6 @@ package arctic_nightmare; public class PlayerDiedException extends Exception{ - - /** - * - */ - private static final long serialVersionUID = 1L; - + private static final long serialVersionUID = 1L; + PlayerDiedException(String error) {super(error);} } diff --git a/Arctic_Nigthmare/src/arctic_nightmare/PlayersWinException.java b/Arctic_Nigthmare/src/arctic_nightmare/PlayersWinException.java index 3769ef7ab3ee546940ab8400584ae2ec54333b98..b3b2f9dca0d6af32924358f815bbd41fb88ff98e 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/PlayersWinException.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/PlayersWinException.java @@ -1,8 +1,6 @@ package arctic_nightmare; public class PlayersWinException extends Exception{ - /** - * - */ - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; + PlayersWinException(String error) {super(error);} } diff --git a/Arctic_Nigthmare/src/arctic_nightmare/StableField.java b/Arctic_Nigthmare/src/arctic_nightmare/StableField.java index b729e90d7dc09ee0162153e33ecd9fccda119f33..25bfa5eadf14fbf4371eb9128bb1f747762c6ab7 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/StableField.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/StableField.java @@ -6,27 +6,24 @@ public class StableField extends Field { super(10, snow, item); } + public StableField(int snow, String item) { + super(10, snow, new Item(item)); + } public StableField() { super(); capacity = 10; } - - public void accept(Person person) throws PlayerDiedException + public void accept(Person person) throws PlayerDiedException { - if(bear != null && !hasIgloo) - person.die(); person.field = this; + if(bear != null && !hasIgloo) person.die(); persons.add(person); } - - /*Only for proto*/ - public String get_type_name() {return "StableField";} - - public boolean canAddPlayer() { - if(bear != null) - return false; - return true; - } + + public boolean canAddPlayer() { + if(bear != null) return false; + return true; + } } diff --git a/Arctic_Nigthmare/src/arctic_nightmare/UnstableField.java b/Arctic_Nigthmare/src/arctic_nightmare/UnstableField.java index c3c8a8487d4ad286ee4ce3f27845d538465ac4fb..0d12059940b69f85647d3371b1ac051b7d796110 100644 --- a/Arctic_Nigthmare/src/arctic_nightmare/UnstableField.java +++ b/Arctic_Nigthmare/src/arctic_nightmare/UnstableField.java @@ -7,19 +7,23 @@ public class UnstableField extends Field{ capacity = 0; } - public UnstableField(int capacity, int snow, Item item) { + public UnstableField(int snow, int capacity, Item item) { super(capacity, snow, item); } + public UnstableField(int snow, int capacity, String item) { + super(capacity, snow, new Item(item)); + } + public void accept(Person person) throws PlayerDiedException { persons.add(person); + person.field = this; if(bear != null && !hasIgloo) person.die(); if (persons.size() > capacity) overturn(); - person.field = this; } public void overturn() throws PlayerDiedException @@ -34,13 +38,9 @@ public class UnstableField extends Field{ person.fall(); } } - - /*Only for proto*/ - public String get_type_name() {return "UnstableField";} - - public boolean canAddPlayer() { - if(bear != null || (persons.size() + 1) > capacity) - return false; - return true; - } + public boolean canAddPlayer() { + if(bear != null || (persons.size() + 1) > capacity) + return false; + return true; + } }