|
| 1 | +package com.dot_hunter; |
| 2 | + |
| 3 | +public class Hunter { |
| 4 | + |
| 5 | + //attributes |
| 6 | + private String name; |
| 7 | + private String color; |
| 8 | + private int x; |
| 9 | + private int y; |
| 10 | + |
| 11 | + //default constructor |
| 12 | + public Hunter() { |
| 13 | + super(); |
| 14 | + } |
| 15 | + |
| 16 | + //overload constructor |
| 17 | + public Hunter(String name, String color) { |
| 18 | + super(); |
| 19 | + this.name = name; |
| 20 | + this.color = color; |
| 21 | + } |
| 22 | + |
| 23 | + //return name |
| 24 | + public String getName() { |
| 25 | + return name; |
| 26 | + } |
| 27 | + |
| 28 | + //assign name |
| 29 | + public void setName(String name) { |
| 30 | + this.name = name; |
| 31 | + } |
| 32 | + |
| 33 | + //return color |
| 34 | + public String getColor() { |
| 35 | + return color; |
| 36 | + } |
| 37 | + |
| 38 | + //assign color |
| 39 | + public void setColor(String color) { |
| 40 | + this.color = color; |
| 41 | + } |
| 42 | + |
| 43 | + //return x value |
| 44 | + public int getxPOS() { |
| 45 | + return x; |
| 46 | + } |
| 47 | + |
| 48 | + //set x value |
| 49 | + public void setxPOS(int x) { |
| 50 | + this.x = x; |
| 51 | + } |
| 52 | + |
| 53 | + //return y value |
| 54 | + public int getyPOS() { |
| 55 | + return y; |
| 56 | + } |
| 57 | + |
| 58 | + //set y value |
| 59 | + public void setyPOS(int y) { |
| 60 | + this.y = y; |
| 61 | + } |
| 62 | + |
| 63 | + //move method |
| 64 | + public void move(Hunter myHunter) |
| 65 | + { |
| 66 | + //handle the exception using try-catch |
| 67 | + try { |
| 68 | + |
| 69 | + //condition checking if the hunter hits the wall |
| 70 | + if(myHunter.x >= 250 || myHunter.y >= 360) |
| 71 | + { |
| 72 | + throw new SoundException("Oh oo!!"); |
| 73 | + } |
| 74 | + else { |
| 75 | + System.out.println("Hunter is moving, X:" + myHunter.x + " Y:" + myHunter.y); |
| 76 | + } |
| 77 | + //handle sound exception |
| 78 | + } catch (SoundException e) { |
| 79 | + // TODO: handle exception |
| 80 | + System.out.println(e.getMessage()); |
| 81 | + |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + //hunt method |
| 86 | + public void hunt(Board myBoard) |
| 87 | + { |
| 88 | + |
| 89 | + //dot type of the board |
| 90 | + String dotType = myBoard.getDotType(); |
| 91 | + |
| 92 | + //position of soldiers |
| 93 | + Soldier[] threeSoldiers = myBoard.getThreeSoldiers(); |
| 94 | + int x = myBoard.getMyHunter().getxPOS(); |
| 95 | + int y = myBoard.getMyHunter().getyPOS(); |
| 96 | + |
| 97 | + //game over when all dots are hunted |
| 98 | + if (myBoard.getDots() == 0 && myBoard.getSuperDots() == 0) |
| 99 | + { |
| 100 | + Board.gameOver(); |
| 101 | + } |
| 102 | + |
| 103 | + //Hunter get special power when hunting superDot |
| 104 | + for(int i = 0; i < threeSoldiers.length; i++) { |
| 105 | + |
| 106 | + if(x == threeSoldiers[i].getxPOS() && y == threeSoldiers[i].getyPOS()) { |
| 107 | + |
| 108 | + //if hunted dot is a normalDot, no soldier gets killed |
| 109 | + if(dotType.equals("dot")) { |
| 110 | + Board.gameOver(); |
| 111 | + } |
| 112 | + |
| 113 | + //if hunted dot is a superDot, hunter gets special power |
| 114 | + //soldier gets killed |
| 115 | + else if(dotType.equals("superDot")) { |
| 116 | + System.out.println("Soldier KILLS"); |
| 117 | + |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + //reduce Dot by one |
| 123 | + if(dotType.equals("dot")) { |
| 124 | + myBoard.setDots(myBoard.getDots() - 1); |
| 125 | + System.out.println("Hunting Dots"); |
| 126 | + } |
| 127 | + |
| 128 | + //reduce superDot by one |
| 129 | + else if(dotType.equals("superDot")) { |
| 130 | + myBoard.setSuperDots(myBoard.getSuperDots() - 1); |
| 131 | + System.out.println("Hunting Super Dots"); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments