Advertisement
Khadija_Assem

Untitled

Dec 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.14 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.game.sample;
  2.  
  3. import eg.edu.alexu.csd.oop.game.GameObject;
  4. import eg.edu.alexu.csd.oop.game.World;
  5. import eg.edu.alexu.csd.oop.game.sample.GameObjects.Clown;
  6. import eg.edu.alexu.csd.oop.game.sample.GameObjects.Composite.Gate;
  7. import eg.edu.alexu.csd.oop.game.sample.GameObjects.Composite.GateDirector;
  8. import eg.edu.alexu.csd.oop.game.sample.GameObjects.Composite.ShelfDirector;
  9. import eg.edu.alexu.csd.oop.game.sample.GameObjects.ImageObject;
  10. import eg.edu.alexu.csd.oop.game.sample.GameObjects.Plates;
  11. import eg.edu.alexu.csd.oop.game.sample.GameObjects.Composite.Shelf;
  12. import eg.edu.alexu.csd.oop.game.sample.GameObjects.UndoButton;
  13. import eg.edu.alexu.csd.oop.game.sample.Logger.LoggerObject;
  14. import eg.edu.alexu.csd.oop.game.sample.Memento.Caretaker;
  15. import eg.edu.alexu.csd.oop.game.sample.Memento.Originator;
  16. import eg.edu.alexu.csd.oop.game.sample.Observer.Change;
  17. import eg.edu.alexu.csd.oop.game.sample.Observer.Subject;
  18. import eg.edu.alexu.csd.oop.game.sample.ShapeFactory.ShapeFactory;
  19. import eg.edu.alexu.csd.oop.game.sample.State.*;
  20. import org.apache.log4j.Logger;
  21.  
  22. import java.awt.*;
  23. import java.lang.reflect.Array;
  24. import java.util.ArrayList;
  25. import java.util.LinkedList;
  26. import java.util.List;
  27. import java.util.Timer;
  28. import java.util.Stack;
  29. import java.util.concurrent.TimeUnit;
  30.  
  31.  
  32. public class Game implements World {
  33. Logger logger = LoggerObject.getLogger();
  34.  
  35. private static int MAX_TIME = 1 * 60 * 1000; // 11 minute
  36. private int score = 0;
  37. private long startTime = System.currentTimeMillis();
  38. private final int width;
  39. private final int height;
  40.  
  41. private final List<GameObject> constant = new LinkedList<GameObject>();
  42. private final List<GameObject> moving = new LinkedList<GameObject>();
  43. private final List<GameObject> control = new LinkedList<GameObject>();
  44. private ImageObject LeftHand;
  45. private ImageObject RightHand;
  46. private final Stack<Plates> RightHandStack= new Stack<>();
  47. private final Stack<Plates> LeftHandStack= new Stack<>();
  48. ShapeFactory sF ;
  49. Caretaker careTaker = new Caretaker();
  50. boolean flag = true;
  51.  
  52. private int Speed =0;
  53. private Subject subject = new Subject();
  54. private Object CaughtState = new CaughtState();
  55.  
  56. private boolean intersect(ImageObject o1, ImageObject o2,char Ch){
  57. if(o2 instanceof Clown) {
  58. if ((Math.abs(o1.getX()-o2.getX())<o1.getWidth()/2) && (o1.getY() - o2.getY()+o1.getHeight() == 0 && Ch=='L')) {
  59. logger.info("Left hand Clown Intersection/Object Type : "+((Plates)o1).getPath());
  60. //System.exit(0);
  61. return true;
  62. }
  63. else if ((Math.abs(o1.getX()-o2.getX()-(o2.getWidth()/2))<o1.getWidth()) && (o1.getY() - o2.getY()+o1.getHeight() == 0&&Ch=='R')){
  64. logger.info("Right hand Clown Intersection/Object Type : "+((Plates)o1).getPath());
  65. return true;
  66. }
  67. return false;
  68. }
  69. else {
  70. if ((Math.abs(o1.getX()-o2.getX())<o1.getWidth()/2) && (o1.getY() - o2.getY() +o1.getHeight()==0)) {
  71. logger.info("Object intersection of type "+((Plates)o1).getPath());
  72. return true;
  73. }
  74. return false;
  75. }
  76. }
  77. private GameObjectsSave Clone() throws CloneNotSupportedException {
  78. ArrayList<GameObject> Mgame = new ArrayList<>();
  79. for (int i =0;i<moving.size();i++){
  80. // if (moving.get(i) instanceof Plates)
  81. Mgame.add(((Plates)moving.get(i)).clone());
  82. }
  83. ArrayList<GameObject> Congame = new ArrayList<>();
  84. for (int i =0;i<constant.size();i++){
  85. if (constant.get(i) instanceof Plates)
  86. Congame.add(((Plates)constant.get(i)).clone());
  87. else Congame.add(constant.get(i));
  88. }
  89. ArrayList<GameObject> Cgame = new ArrayList<>();
  90. Cgame.add(Clown.getInstance().clone());
  91. for (int i =0;i<control.size();i++){
  92. if (control.get(i) instanceof Plates)
  93. Congame.add(((Plates)control.get(i)).clone());
  94. else if (control.get(i) instanceof Clown)
  95. Congame.add (Clown.getInstance().clone());
  96. }
  97. ArrayList<GameObject> LHand = new ArrayList<>(LeftHandStack);
  98. ArrayList<GameObject> Lgame = new ArrayList<>();
  99. // for (int i =0;i<LHand.size()&&LeftHandStack.size()!=0;i++){
  100. // if (LeftHandStack.get(i) instanceof Plates)
  101. // Lgame.add(((Plates)LHand.get(i)).clone());
  102. // }
  103. ArrayList<GameObject> RHand = new ArrayList<>(RightHandStack);
  104. ArrayList<GameObject> Rgame = new ArrayList<>();
  105. // for (int i =0;i<RHand.size()&&RightHandStack.size()!=0;i++){
  106. // if (RightHandStack.get(i) instanceof Plates)
  107. // Rgame.add(((Plates)RHand.get(i)).clone());
  108. // }
  109. Stack R = new Stack<>();R.addAll(Rgame);
  110. Stack L = new Stack<>();L.addAll(Lgame);
  111. GameObjectsSave gS = new GameObjectsSave(Congame,Mgame,Clown.getInstance().clone(),LeftHand,RightHand,score,Cgame,R,L);
  112. return gS;
  113. }
  114.  
  115. public Game(GameObjectsSave GO){
  116. // this.sF = ShapeFactory.getFactory();
  117. // width = ShapeFactory.getFactory().getWorld().getWidth();
  118. // height = ShapeFactory.getFactory().getWorld().getHeight();
  119. // System.out.println(GO.getControl());
  120. // LeftHand = GO.getLeftHand();
  121. // RightHand = GO.getRightHand();
  122. // this.control.add(GO.getCl());
  123. //// this.moving.addAll(GO.getControl());
  124. // this.constant.addAll(ShapeFactory.getFactory().getWorld().getConstantObjects());
  125. // this.moving.addAll(ShapeFactory.getFactory().getWorld().getMovableObjects());
  126. //
  127. ////// try {
  128. // if(GO.getRightHandStack().size()!=0) {
  129. // for (int i=0;i<GO.getRightHandStack().size();i++){
  130. // moving.remove(GO.getRightHandStack().get(i));
  131. // GO.getRightHandStack().get(i).setX(RightHand.getX());
  132. // }
  133. //// this.RightHandStack.addAll(GO.getRightHandStack());
  134. //// this.control.addAll(GO.getRightHandStack());
  135. // System.out.println("Right hand stack "+this.RightHandStack.size());
  136. // System.out.println("Control Size "+this.control.size());
  137. // }
  138. // if (GO.getLeftHandStack().size()!=0) {
  139. // for (int i=0;i<GO.getLeftHandStack().size();i++){
  140. // moving.remove(GO.getLeftHandStack().get(i));
  141. // GO.getLeftHandStack().get(i).setX(LeftHand.getX());
  142. // }
  143. // System.out.println("Left Hand stack "+(GO.getLeftHandStack()));
  144. //// this.LeftHandStack.addAll(GO.getLeftHandStack());
  145. // System.out.println("Left hand stack " + this.LeftHandStack.size());
  146. //// this.control.addAll(LeftHandStack);
  147. // System.out.println("Control Size "+this.control.size());
  148. // }
  149. // sF.setWorld(this);
  150. this.sF = ShapeFactory.getFactory();
  151. width = ShapeFactory.getFactory().getWorld().getWidth();
  152. height = ShapeFactory.getFactory().getWorld().getHeight();
  153. LeftHand = GO.getLeftHand();
  154. RightHand = GO.getRightHand();
  155. this.control.addAll(GO.getControl());
  156. this.score = GO.getScore();
  157. this.moving.addAll(GO.getMoving());
  158. this.constant.addAll(ShapeFactory.getFactory().getWorld().getConstantObjects());
  159. try {
  160. TimeUnit.MILLISECONDS.sleep(500);
  161. } catch (InterruptedException e) {
  162. e.printStackTrace();
  163. }
  164. this.score = GO.getScore();
  165. DGameController.getInstance().pause();
  166. // System.out.println("AHO SA&AA");
  167. // DGameController.getInstance().resume();
  168. //// try {
  169. // if(GO.getRightHandStack().size()!=0) {
  170. // for (int i=0;i<GO.getRightHandStack().size();i++){
  171. // moving.remove(GO.getRightHandStack().get(i));
  172. // GO.getRightHandStack().get(i).setX(RightHand.getX());
  173. // }
  174. //// this.RightHandStack.addAll(GO.getRightHandStack());
  175. //// this.control.addAll(GO.getRightHandStack());
  176. // System.out.println("Right hand stack "+this.RightHandStack.size());
  177. // System.out.println("Control Size "+this.control.size());
  178. // }
  179. // if (GO.getLeftHandStack().size()!=0) {
  180. // for (int i=0;i<GO.getLeftHandStack().size();i++){
  181. // moving.remove(GO.getLeftHandStack().get(i));
  182. // GO.getLeftHandStack().get(i).setX(LeftHand.getX());
  183. // }
  184. // System.out.println("Left Hand stack "+(GO.getLeftHandStack()));
  185. //// this.LeftHandStack.addAll(GO.getLeftHandStack());
  186. // System.out.println("Left hand stack " + this.LeftHandStack.size());
  187. //// this.control.addAll(LeftHandStack);
  188. // System.out.println("Control Size "+this.control.size());
  189. // }
  190. }
  191.  
  192. public Game(int screenWidth, int screenHeight) {
  193. //moving.add(new ImageObject((int)(Math.random() * screenWidth), -1 * (int)(Math.random() * screenHeight), "/star.png"));
  194. sF = ShapeFactory.getFactory();
  195. sF.setWorld(this);
  196. width = screenWidth;
  197. height = screenHeight;
  198. control.add(Clown.getInstance());
  199. LeftHand=(ImageObject)control.get(0);
  200. RightHand=(ImageObject)control.get(0);
  201. constant.add(new Shelf(10,70,450,true, Color.RED));
  202. constant.add(new Shelf(800,70,500,true, Color.RED));
  203. constant.add(new Shelf(10,70+100,250,true, Color.BLUE));
  204. constant.add(new Shelf(1000,70+100,300,true, Color.BLUE));
  205. for(int i = 0;i<constant.size();i++)
  206. ShelfDirector.getInstance().addShelf((Shelf)constant.get(i));
  207. GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(0).getX(), 0));
  208. sF.setGate(0);
  209. moving.add(sF.getShape());
  210. GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(1).getX()+ShelfDirector.getInstance().showShelfeDetails(1).getWidth(), 0));
  211. sF.setGate(1);
  212. moving.add(sF.getShape());
  213. GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(2).getX(), 0+100));
  214. sF.setGate(2);
  215. moving.add(sF.getShape());
  216. GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(3).getX()+ShelfDirector.getInstance().showShelfeDetails(3).getWidth(), 0+100));
  217. sF.setGate(3);
  218. moving.add(sF.getShape());
  219. logger.info("Starting game of Dimensions \n Height : "+this.getHeight()+" And width : "+this.getWidth()+"\n Current Speed : "+this.getSpeed()+" Max Time : "+MAX_TIME/(1000*60) +" Minute And " + MAX_TIME%(1000*60)+" Seconds." );
  220. }
  221.  
  222. // boolean flag = true;
  223. private static int i =0;
  224. @Override
  225. public boolean refresh() {
  226. i++;
  227. if(i%50 == 0){
  228. Originator originator = new Originator();
  229. try {
  230. originator.setState(Clone());
  231. } catch (CloneNotSupportedException e) {
  232. e.printStackTrace();
  233. }
  234. careTaker.addMemento(originator.save());
  235. }
  236. boolean timeout = System.currentTimeMillis() - startTime > MAX_TIME; // time end and game over
  237. if(flag) {
  238. Frame.getFrames()[Frame.getFrames().length-1].add(UndoButton.getInstance());
  239. constant.add(UndoButton.getInstance());
  240. flag = false;
  241. }
  242.  
  243. for(int i=0;i<moving.size();i++) {
  244. if (((Plates) moving.get(i)).getState() instanceof ShelfState || ((Plates) moving.get(i)).getState() instanceof FallingState) {
  245. ((Plates) moving.get(i)).getState().doAction(((Plates) moving.get(i)));
  246. if (moving.get(i).getX() < getWidth()-200 && !((Plates) moving.get(i)).isUsed() && ((((Plates) moving.get(i)).getGate() == 1 ) || (((Plates) moving.get(i)).getGate() == 3 ))) {
  247. sF.setGate(((Plates) moving.get(i)).getGate());
  248. moving.add(sF.getShape());
  249. ((Plates) moving.get(i)).setUsed(true);
  250. }
  251. else if (moving.get(i).getX() > 200 && !((Plates) moving.get(i)).isUsed() && (((Plates) moving.get(i)).getGate() == 0 || (((Plates) moving.get(i)).getGate() == 2))) {
  252. sF.setGate(((Plates) moving.get(i)).getGate());
  253. moving.add(sF.getShape());
  254. ((Plates) moving.get(i)).setUsed(true);
  255. }
  256. }
  257. // else if (((Plates)moving.get(i)).getState() instanceof FloorState)((Plates)moving.get(i)).getState().doAction((Plates) moving.get(i));
  258.  
  259. if (((Plates) moving.get(i)).getState() instanceof FallingState) {
  260. Plates plateTest = (Plates) moving.get(i);
  261. if (intersect((ImageObject) moving.get(i), LeftHand,'L')) {
  262. if(! (LeftHand instanceof Clown))
  263. logger.info("Left hand intersection");
  264. control.add(moving.get(i));
  265. LeftHand = (ImageObject) moving.get(i);
  266. LeftHandStack.push((Plates)LeftHand);
  267. System.out.println("Moving size "+moving.size());
  268. ((Plates) moving.get(i)).setState((State) CaughtState);
  269. moving.remove(i);
  270. System.out.println("Moving size "+moving.size());
  271. plateTest.setState(new CaughtState());
  272. getScoreFrom(LeftHandStack);
  273. if(!LeftHandStack.empty())
  274. LeftHand=(Plates)LeftHandStack.peek();
  275. else
  276. LeftHand=Clown.getInstance();
  277. logger.info("Current Peek of Left Hand : "+LeftHand.getPath());
  278. } else if (intersect((ImageObject) moving.get(i), RightHand,'R')) {
  279. if(! (RightHand instanceof Clown))
  280. logger.info("Right hand intersection");
  281. control.add(moving.get(i));
  282. RightHandStack.push((Plates) moving.get(i));
  283. RightHand = (ImageObject) moving.get(i);
  284. moving.remove(i);
  285. plateTest.setState(new CaughtState());
  286. getScoreFrom(RightHandStack);
  287.  
  288. if(!RightHandStack.empty())
  289. RightHand=(Plates)RightHandStack.peek();
  290. else
  291. RightHand=Clown.getInstance();
  292. logger.info("Current Peek of Right Hand : "+RightHand.getPath());
  293. }
  294. }
  295. }
  296. return !timeout;
  297. }
  298.  
  299. @Override
  300. public int getSpeed() {
  301. return Speed;
  302. }
  303. @Override
  304. public int getControlSpeed() {
  305. return 20;
  306. }
  307. @Override
  308. public List<GameObject> getConstantObjects() {
  309. return constant;
  310. }
  311. @Override
  312. public List<GameObject> getMovableObjects() {
  313. return moving;
  314. }
  315. @Override
  316. public List<GameObject> getControlableObjects() {
  317. return control;
  318. }
  319. @Override
  320. public int getWidth() {
  321. return width;
  322. }
  323. @Override
  324. public int getHeight() {
  325. return height;
  326. }
  327. @Override
  328. public String getStatus() {
  329. // return "";
  330. return "Please Use Arrows To Move | Location = " + control.get(0).getX() + "," + control.get(0).getY() + " | Score=" + score + " | Time=" + Math.max(0, (MAX_TIME - (System.currentTimeMillis()-startTime))/1000); // update status
  331. }
  332.  
  333.  
  334. public void setSpeed(int speed) {
  335. Speed = speed;
  336. }
  337.  
  338. public int getScore() {
  339. return score;
  340. }
  341.  
  342. public void setScore( int NewScore) {
  343. this.score = NewScore ;
  344. }
  345.  
  346.  
  347. public void getScoreFrom ( Stack <Plates> HandStack ){
  348. subject.setWorld(this);
  349. new Change(subject , subject.getWorld() , HandStack );
  350. subject.setState(this.getScore());
  351. setScore(subject.getState()) ;
  352. }
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement