Skip to content

Commit 15e20e2

Browse files
committed
Online Assignment
1 parent c42e546 commit 15e20e2

File tree

8 files changed

+369
-0
lines changed

8 files changed

+369
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.dot_hunter;
2+
3+
public class BlueSoldier extends Soldier{
4+
5+
6+
7+
public BlueSoldier() {
8+
super();
9+
// TODO Auto-generated constructor stub
10+
}
11+
12+
@Override
13+
void hunt() {
14+
// TODO Auto-generated method stub
15+
System.out.println("Killed using a knife");
16+
17+
//call game over
18+
Board.gameOver();
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.dot_hunter;
2+
3+
public class Board {
4+
5+
//attributes
6+
private int dots;
7+
private int superDots;
8+
private String dotType;
9+
private Hunter myHunter;
10+
private Soldier[] threeSoldiers;
11+
12+
//return Dots
13+
public int getDots() {
14+
return dots;
15+
}
16+
17+
//assign Dots
18+
public void setDots(int dots) {
19+
this.dots = dots;
20+
}
21+
22+
//return Super Dots
23+
public int getSuperDots() {
24+
return superDots;
25+
}
26+
27+
//assign Super Dots
28+
public void setSuperDots(int superDots) {
29+
this.superDots = superDots;
30+
}
31+
32+
//return Dot Type
33+
public String getDotType() {
34+
return dotType;
35+
}
36+
37+
//assign Dot Type
38+
public void setDotType(String dotType) {
39+
this.dotType = dotType;
40+
}
41+
42+
//return MyHunter
43+
public Hunter getMyHunter() {
44+
return myHunter;
45+
}
46+
47+
//assign MyHunter
48+
public void setMyHunter(Hunter myHunter) {
49+
this.myHunter = myHunter;
50+
}
51+
52+
//return Three Solders
53+
public Soldier[] getThreeSoldiers() {
54+
return threeSoldiers;
55+
}
56+
57+
//
58+
public void setThreeSoilders(Soldier[] threeSoilders) {
59+
this.threeSoldiers = threeSoilders;
60+
}
61+
62+
public Board(String dotType, Hunter myHunter, Soldier[] threeSoldiers) {
63+
64+
super();
65+
this.dotType = dotType;
66+
this.myHunter = myHunter;
67+
this.threeSoldiers = threeSoldiers;
68+
69+
this.init();
70+
}
71+
72+
public void init() {
73+
74+
//set initial values for Dots and Super Dots
75+
this.setDots(97);
76+
this.setSuperDots(3);
77+
78+
//set soldiers' positions
79+
//generate a random number using Math.random function
80+
for (int i = 0 ; i < threeSoldiers.length ; i++)
81+
{
82+
threeSoldiers[i].setxPOS((int) Math.random() * 250);
83+
threeSoldiers[i].setyPOS((int) Math.random() * 360);
84+
85+
}
86+
87+
//set hunter positions
88+
myHunter.setxPOS(0);
89+
myHunter.setyPOS(0);
90+
91+
//prompt as ready
92+
System.out.println("Board is ready and three soldiers and the hunter is positioned in the board");
93+
}
94+
95+
//game over method
96+
public static void gameOver(){
97+
98+
//Prompt
99+
System.out.println("Game Over");
100+
101+
//Terminate the program
102+
System.exit(0);
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.dot_hunter;
2+
3+
public class GreenSoldier extends Soldier{
4+
5+
6+
public GreenSoldier() {
7+
super();
8+
// TODO Auto-generated constructor stub
9+
}
10+
11+
@Override
12+
void hunt() {
13+
// TODO Auto-generated method stub
14+
System.out.println("Killed using a gun");
15+
16+
//call game over
17+
Board.gameOver();
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.dot_hunter;
2+
3+
import java.util.Scanner;
4+
5+
public class MainApp {
6+
public static void main(String[] args)
7+
{
8+
System.out.println("Hello world");
9+
Scanner scan = new Scanner(System.in);
10+
Hunter myhunter = new Hunter("Maha Deva" , "Brown");
11+
Soldier threeSoilders[] = {new RedSoldier() , new RedSoldier() , new GreenSoldier()};
12+
Board myboard = new Board("superDot" , myhunter , threeSoilders);
13+
14+
System.out.println("Use the keyboard up , down , left , right arrow keys to move the hunter");
15+
myhunter.setxPOS(scan.nextInt());
16+
myhunter.setyPOS(scan.nextInt());
17+
myhunter.move(myhunter);
18+
myhunter.hunt(myboard);
19+
threeSoilders[2].hunt();
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.dot_hunter;
2+
3+
public class RedSoldier extends Soldier{
4+
5+
6+
public RedSoldier() {
7+
super();
8+
// TODO Auto-generated constructor stub
9+
}
10+
11+
@Override
12+
void hunt() {
13+
// TODO Auto-generated method stub
14+
System.out.println("Killed using a knife");
15+
16+
//call game over
17+
Board.gameOver();
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.dot_hunter;
2+
3+
abstract class Soldier {
4+
5+
//attributes
6+
private int x;
7+
private int y;
8+
9+
//define abstract method
10+
abstract void hunt();
11+
12+
public Soldier()
13+
{
14+
x = 0;
15+
y = 0;
16+
}
17+
18+
//return x value
19+
public int getxPOS() {
20+
return x;
21+
}
22+
23+
//set x value
24+
public void setxPOS(int x) {
25+
this.x = x;
26+
}
27+
28+
//return y value
29+
public int getyPOS() {
30+
return y;
31+
}
32+
33+
//set y value
34+
public void setyPOS(int y) {
35+
this.y = y;
36+
//y = 6;
37+
}
38+
39+
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.dot_hunter;
2+
3+
public class SoundException extends Exception {
4+
5+
SoundException (String message){
6+
super(message);
7+
}
8+
}

0 commit comments

Comments
 (0)