Spy Robot public abstract class Spy Robot private
Spy Robot public abstract class Spy. Robot { private String model; public Spy. Robot(String model) { this. model=model; } public String get. Model() { return this. model; } public abstract void move. Forward(); move. Backward(); turn. Left(); turn. Right(); public void take. Picture() {. . . } public void charge. Battery() {. . . } } 8
Roboquad – Spy Robot public class Legs. Spy. Robot extends Spy. Robot { public Legs. Spy. Robot() { super("Roboquad"); } public void move. Forward() { for(int i=0; i<4; i++) this. move. Leg(i, 1); } public void move. Backward() { for(int i=0; i<4; i++) this. move. Leg(i, -1); } 3 2 1 0 public void turn. Right() { public void turn. Left() { this. move. Leg(0, 1); this. move. Leg(0, -1); this. move. Leg(1, -1); this. move. Leg(2, 1); this. move. Leg(3, -1); this. move. Leg(3, 1); } } // direction {1=forward, -1=backward} private void move. Leg(int leg. Id, int dir) {. . . }; } 9
Spyke – Spy Robot public class Wheels. Spy. Robot extends Spy. Robot { public Wheels. Spy. Robot() { super("Spyke"); } public void move. Forward() { this. turn. Wheels(1, 1); } public void move. Backward() { this. turn. Wheels(-1, -1); } public void turn. Left() { this. turn. Wheels(0, -1); } public void turn. Right() { this. turn. Wheels(-1, 0); } // direction {1=forward, 0=stop, -1=backward} private void turn. Wheels(int right. Dir, int left. Dir) {. . . }; // move features public void wave. Hands() {. . . } } 10
מימוש המחלקה של פעולה כללית public class Action { private String name; public Action(String name) { this. name = name; } public String get. Name(){ return this. name; } public boolean equals(Object other) { boolean ans = false; if(other instanceof Action) ans = this. name. equals(((Action)other). name); return ans; } } 16
מימוש המחלקה של שחקן כללי public abstract class Player { private String name; private int score; public Player(String name){ this. name = name; this. score = 0; } public abstract Action select. Action(Action[] actions); public boolean is. Winner(Player p){ return (this. score > p. get. Score()); } public void update. Score(int score){ this. score = this. score + score; } public int get. Score(){ return this. score; } } 17
מימוש שחקן אקראי public class Random. Player extends Player{ public Random. Player(String name) { super(name); } public Action select. Action(Action[] actions){ int rand. Idx = (int)(Math. random()*actions. length); return actions[rand. Idx]; } } 18
מימוש שחקן עיקבי public class Consecutive. Player extends Player { private int last. Idx; public Consecutive. Player(String name) { super(name); this. last. Idx = 0; } public Action select. Action(Action[] actions) { this. last. Idx = (this. last. Idx + 1) % actions. length; return actions[this. last. Idx]; } } 19
מימוש משחק כללי public abstract class Game { private Player p 1, p 2; private String name; //game name protected Action[] actions; // the set of actions public Game(Player p 1, Player p 2, String name){ this. p 1 = p 1; this. p 2 = p 2; this. name = name; this. init. Actions(); } // There is no real list of actions in a general game protected abstract void init. Actions(); … 20
( כללי )המשך מימוש משחק public abstract class Game { … public void play(int turn. Count) { for (int i=0; i<turn. Count; i=i+1) this. play. Single. Turn(); } private void play. Single. Turn() { /* the selection order is not important * as each player does not * know the choice of the other player */ Action a 1 = this. p 1. select. Action(actions); Action a 2 = this. p 2. select. Action(actions); this. reward. Players(a 1, a 2); } // There is no real scoring strategy in a general game protected abstract void reward. Players(Action a 1, Action a 2); … 21
( כללי )המשך מימוש משחק public abstract class Game { … public Player get. Winner () { if (this. p 1. is. Winner(this. p 2)) return this. p 1; else return this. p 2; } protected Player get. First. Player() { return this. p 1; } protected Player get. Second. Player() { return this. p 2; } } 22
מימוש המשחק אבן נייר ומספריים public class Rock. Paper. Scissors extends Game{ public Rock. Paper. Scissors(Player p 1, Player p 2) { super(p 1, p 2, "Rock Paper Scissors"); } protected void init. Actions(){ this. actions = new Action[3]; this. actions[0] = new Action("rock"); this. actions[1] = new Action("paper"); this. actions[2] = new Action("scissors"); }. . . 23
protected void reward. Players(Action a 1, Action a 2) { int p 1 score = 0; if (!(a 1. equals(a 2))) {// Different actions if ((a 1. get. Name(). equals("rock") && a 2. get. Name(). equals("scissors")) || (a 1. get. Name(). equals("paper") && a 2. get. Name(). equals("rock")) || (a 1. get. Name(). equals("scissors") && a 2. get. Name(). equals("paper"))) { p 1 score = 1; } else { p 1 score = -1; } } this. get. First. Player(). update. Score(p 1 score); this. get. Second. Player(). update. Score(-p 1 score); } } 24
מימוש המשחק דילמת האסיר public class Prisoner. Dilemmas extends Game{ public Prisoner. Dilemmas(Player p 1, Player p 2) { super(p 1, p 2, "Prisoner's Dilemma"); } protected void init. Actions(){ this. actions = new Action[2]; this. actions[0] = new Action("silent"); this. actions[1] = new Action("blame"); }. . . 25
protected void reward. Players(Action a 1, Action a 2) { if (a 1. equals(a 2)) { // Same actions if (a 1. get. Name(). equals("blame") { // blame & blame this. get. First. Player(). update. Score(-5); this. get. Second. Player(). update. Score(-5); } else { // silent & silent this. get. First. Player(). update. Score(-1); this. get. Second. Player(). update. Score(-1); } } else { // Different actions if (a 1. get. Name(). equals("blame") { // blame & silent this. get. First. Player(). update. Score(0); this. get. Second. Player(). update. Score(-15); } else { // silent & blame this. get. First. Player(). update. Score(-15); this. get. Second. Player(). update. Score(0); } } 26
public interface Predator { public boolean chase. Prey(Prey p); public void eat. Prey(Prey p); } public class Tiger implements Predator { public boolean chase. Prey(Prey p) { // code to chase prey p (specifically for a tiger) return run. After. Prey(p); } public void eat. Prey (Prey p) { // code to eat prey p (for a tiger) chew(p); swallow(p); }. . . } 32
public interface Predator { public boolean chase. Prey(Prey p); public void eat. Prey(Prey p); } public class Shark implements Predator { public boolean chase. Prey(Prey p) { // code to chase prey p (specifically for a shark) return swim. After. Prey(p); } public void eat. Prey (Prey p) { // code to eat prey p //(specifically for a shark) bite(p); swallow(p); } } 33
המסוגל לבצע מספר פעולות , "טרף" הוא גם כן תאור כללי של יצור : גם לו יהיה ממשק. בסיסיות public interface Prey { public boolean is. Alive(); public void die(); public void run. Away(); } : " דוגמאות למחלקות שעשויות לממש "טרף public class Frog implements Prey { public void jump() {. . . } public void run. Away() {. . . } public class Deer implements Prey {. . . } 34
ללא , להוסיף פעולות )שיטות( ומצב )שדות( למחלקות השונות , כמובן , ניתן : לדוגמה. קשר לממשק אותן ממשות public class Shark implements Predator { private String name; private int num. Of. Teeth; public Shark(String name) { this. name = name; num. Of. Teeth = 3000 + (int)(Math. random()*1000); } private void swallow(Prey p) { p. die(); } public int get. Num. Of. Teeth() { return num. Of. Teeth; } public void swim. For. Fun() {. . . } public void eat. Prey (Prey p) { bite(p); swallow(p); }. . . } 38
instanceof אופרטור אם ורק אם האובייקט משמאלו הוא true זהו אופרטור המחזיר instanceof. ( של המחלקה מימינו של האופרטור instance) מופע : דוגמאות public class Cat {…} public interface Show. Animal {…} public class Siamese extends Cat implements Show. Animal {…} 42
instanceof אופרטור public class Cat {…} public interface Show. Animal {…} public class Siamese extends Cat implements Show. Animal {…} Cat myaoo = new Cat(); Cat mitzi = new Siamese(); System. out. println(myaoo instanceof Cat); System. out. println(mitzi instanceof Cat); System. out. println(myaoo instanceof "Cat"); //true //Compilation Error System. out. println(mitzi instanceof Siamese); System. out. println(myaoo instanceof Siamese); //true //false System. out. println(mitzi instanceof Show. Animal); //true System. out. println(mitzi instanceof Object); //true 43
instanceof אופרטור public class Cat {…} public interface Show. Animal {…} public class Siamese extends Cat implements Show. Animal {…} Cat myaoo = new Cat(); Cat mitzi = new Siamese(); System. out. println(mitzi instanceof myaoo); //Compilation Error System. out. println(!(mitzi instanceof Cat)); //Compilation Error: // operator ! cannot be applied to Cat //false Cat pitzi = null; System. out. println(pitzi instanceof Cat); System. out. println(null instanceof String); //false System. out. println(!mitzi instanceof Cat); 44
- Slides: 44