Advertisement
Khadija_Assem

Untitled

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