Skip to content

Commit

Permalink
Add source files
Browse files Browse the repository at this point in the history
  • Loading branch information
UdeshAthukorala committed Nov 4, 2020
1 parent d3ca465 commit e17efcc
Show file tree
Hide file tree
Showing 32 changed files with 580 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions OOP_Project-Lake_Nozama.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="11" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
11 changes: 11 additions & 0 deletions src/game/Fish.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package game;

public abstract class Fish extends InhabitantCharacter implements Runnable{
public Fish(String name,int x,int y){
this.setCharacterName(name);
this.setXCoordinate(x);
this.setYCoordinate(y);
}
public void swim(){}
public abstract void eat();
}
61 changes: 61 additions & 0 deletions src/game/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package game;
import java.util.*;

public class Game {
static GameController gamecontroller;
static Treasure tChest;
public static void main(String[] args) {
ArrayList<InhabitantCharacter> Inhabitants=new ArrayList<>(); //store newly created warrior and fish objects
Warrior[] listOfWarriors=new Warrior[4]; //store newly created warrior objects
gamecontroller=new GameController(); //make a new playground(lake)
System.out.println("-------Let's start the game--------");
int[][] warriorCoordinates=gamecontroller.placeWarriors(); //get random initial coordinates for warriors
String[] warriorNames={"SuperWarrior1","NormalWarrior1","SuperWarrior2","NormalWarrior2"};
for(int i=0;i<4;i++){ //create two normalWarriors & two superWarriors
Warrior newWarrior;
if(i%2==0){
newWarrior=new SuperWarrior(warriorNames[i],warriorCoordinates[i][0],warriorCoordinates[i][1]);
}else{
newWarrior=new NormalWarrior(warriorNames[i],warriorCoordinates[i][0],warriorCoordinates[i][1]);
}
gamecontroller.addCharacters(newWarrior,newWarrior.getXCoordinate(),newWarrior.getYCoordinate()); //insert the newly created warrior into the grid
listOfWarriors[i]=newWarrior;
Inhabitants.add(newWarrior);
}
int[][][] fishAndlotusCoordinates=gamecontroller.placeFishAndLotus();
int[][] fishCoordinates=fishAndlotusCoordinates[0]; //get random initial coordinates for fish
String[] fishNames={"KillerFish1","KillerFish2","RubberFish1","RubberFish2","InnocentFish1","InnocentFish2"};
for(int i=0;i<6;i++){ //create fish objects
Fish newfish;
if(i<2){
newfish=new KillerFish(fishNames[i],fishCoordinates[i][0],fishCoordinates[i][1]);
}else if(i<4){
newfish=new RubberFish(fishNames[i],fishCoordinates[i][0],fishCoordinates[i][1]);
}else{
newfish=new InnocentFish(fishNames[i],fishCoordinates[i][0],fishCoordinates[i][1]);
}
gamecontroller.addCharacters(newfish,newfish.getXCoordinate(),newfish.getYCoordinate()); //insert the newly created fish into the grid
Inhabitants.add(newfish);
}
for(int i=0;i<Inhabitants.size();i++){ //show the names & initial coordinates of the inhabitants
InhabitantCharacter inhabitant=Inhabitants.get(i);
System.out.println(inhabitant.getClass().getSimpleName()+" "+inhabitant.getCharacterName()+" is at ("+inhabitant.getXCoordinate()+","+inhabitant.getYCoordinate()+")");
}
int[][] lotusCoordinates=fishAndlotusCoordinates[1]; //get random coordinates for lotus flowers
for(int i=0;i<5;i++){
Lotus newLotus=new Lotus(lotusCoordinates[i][0],lotusCoordinates[i][1]);
gamecontroller.addCharacters(newLotus,newLotus.getXCoordinate(),newLotus.getYCoordinate()); //insert the newly created lotus into the grid
System.out.println("Lotus is at ("+newLotus.getXCoordinate()+","+newLotus.getYCoordinate()+")");
}
tChest=new Treasure(5,5); //create the teasure object
gamecontroller.addCharacters(tChest,tChest.getXCoordinate(),tChest.getYCoordinate()); //insert the newly created treasure into the grid
System.out.println("Treasure is at ("+tChest.getXCoordinate()+","+tChest.getYCoordinate()+")");
Warrior warrior1=listOfWarriors[0];Warrior warrior2=listOfWarriors[1];Warrior warrior3=listOfWarriors[2];Warrior warrior4=listOfWarriors[3];

Thread t1=new Thread(warrior1); //create warrior threads
Thread t2=new Thread(warrior2);
Thread t3=new Thread(warrior3);
Thread t4=new Thread(warrior4);
t1.start();t2.start();t3.start();t4.start(); //start warrior threads
}
}
95 changes: 95 additions & 0 deletions src/game/GameController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package game;
import java.util.*;
import java.io.*;

public class GameController {
private GridLocation[][] grid;
Random ran=new Random();
public GameController(){
grid=new GridLocation[11][11]; //create the grid
for(int i=0;i<11;i++){
for(int j=0;j<11;j++){
grid[i][j]=new GridLocation(); //add GridLocation objects to each of the location of grid
}
}
}
public GridLocation getTheLocation(int xCoordinate,int yCoordinate){ //return the GridLocation object corresponding to a given list
return grid[xCoordinate][yCoordinate];
}
public void tranferTheWarrior(Warrior w,int previouseX,int previouseY,int newX, int newY){ //transfer the warrior to new location
GridLocation previouseLocation=this.getTheLocation(previouseX, previouseY);
GridLocation newLocation=this.getTheLocation(newX, newY);
newLocation.addTheWarrior(w);
previouseLocation.removeTheWarrior();
}
public void addCharacters(Object obj,int xCoordinate,int yCoordinate){ //insert character to its initial location
grid[xCoordinate][yCoordinate].initiallyFillTheLocation(obj);
}
public int[][] placeWarriors(){ //randomly create the initial coordinates of the warriors
int[][] initialCoordinates=new int[4][2];
ArrayList<int[]> possibleCoordinates=new ArrayList<>();
for(int i=1;i<11;i++){
int[] a={0,i};int[] b={i,0};
possibleCoordinates.add(a);possibleCoordinates.add(b);
}
for(int j=0;j<4;j++){
int temp=ran.nextInt(possibleCoordinates.size());
int[] coordinates=possibleCoordinates.get(temp);
possibleCoordinates.remove(temp);
initialCoordinates[j][0]=coordinates[0];
initialCoordinates[j][1]=coordinates[1];
}
return initialCoordinates;
}
public int[][][] placeFishAndLotus(){ //randomly create the initial coordinates of the fish and lotus
int[][] initialCoordinates1=new int[6][2];
int[][] initialCoordinates2=new int[5][2];
ArrayList<int[]> possibleCoordinates=new ArrayList<>();
for(int i=1;i<11;i++){
for(int j=1;j<11;j++){
if(i==5 && j==5){continue;}
int[] temp={i,j};
possibleCoordinates.add(temp);
}
}
for(int j=0;j<6;j++){
int temp=ran.nextInt(possibleCoordinates.size());
int[] coordinates=possibleCoordinates.get(temp);
possibleCoordinates.remove(temp);
initialCoordinates1[j][0]=coordinates[0];
initialCoordinates1[j][1]=coordinates[1];
}
for(int j=0;j<5;j++){
int temp=ran.nextInt(possibleCoordinates.size());
int[] coordinates=possibleCoordinates.get(temp);
possibleCoordinates.remove(temp);
initialCoordinates2[j][0]=coordinates[0];
initialCoordinates2[j][1]=coordinates[1];
}
int[][][] temp={initialCoordinates1,initialCoordinates2};
return temp;
}
public void checkTheNewLocation(Warrior w){
System.out.println(w.getCharacterName()+" moves to "+"("+w.getXCoordinate()+","+w.getYCoordinate()+")"); //print the new position on console

GridLocation newGridLocation=getTheLocation(w.getXCoordinate(),w.getYCoordinate()); //get the new grid location of the warrior
if(newGridLocation.checkForTreasure()){ //check whether he is win or not
Game.tChest.anounceTheWinner(w);
double finishingTime=System.currentTimeMillis()-w.getStartTime();
printWinner(w.getCharacterName(),finishingTime);
// System.out.println("Congratz, "+w.getCharacterName()+" won the game !!!!!!!!!!!! -"+" (Finishing Time - "+finishingtime+" ms)");
}
newGridLocation.checkForFish(); //check for any kind of fish
if(newGridLocation.checkForLotus()){
if(!w.checkImmortle()){
w.setImmortal(true); //if there is a lotus then make player immortle
System.out.println(w.getCharacterName()+" become immortal !!!!!!!!!!!");
}
}
}
public void printWinner(String name,Double time){ //store the winner&time in a file
System.out.println("Congratz, "+name+" won the game !!! - ");
System.out.println(" (Finishing Time - "+time+" ms)");
System.out.println(" ---Game Over---");
}
}
60 changes: 60 additions & 0 deletions src/game/GridLocation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package game;

public class GridLocation {
private Object[] charactersOccupiedTheLocation; //store the inhabitants that occupied this
public GridLocation(){ //of the grid at the moment
charactersOccupiedTheLocation=new Object[4];
}
public Warrior checkWarrior(){ //check whether there is a warrior in this location
Object obj=charactersOccupiedTheLocation[0];
if(obj!=null){
return (Warrior)obj;
}
return null;
}
public void checkForFish(){ //check whether there is ant kind of fish in this location
Object obj=charactersOccupiedTheLocation[1];
if(obj!=null){
Thread t=new Thread((Fish)obj);
t.start(); //start the thread of the fish
try {
t.join(); //join warrior's thread to the end of the fish thread
} catch (InterruptedException ex) {}
}
}
public boolean checkForLotus(){ //check whether there is a lotus in this location
Object obj=charactersOccupiedTheLocation[2];
if(obj!=null){return true;}
return false;
}
public boolean checkForTreasure(){ //check whether there is the treasure in this location
Object obj=charactersOccupiedTheLocation[3];
if(obj!=null){return true;}
return false;
}
public void initiallyFillTheLocation(Object obj){ //add objects to grid location at the start of game
if(obj instanceof Warrior){
charactersOccupiedTheLocation[0]=obj;
}else if(obj instanceof Fish){
charactersOccupiedTheLocation[1]=obj;
}else if(obj instanceof Lotus){
charactersOccupiedTheLocation[2]=obj;
}else{
charactersOccupiedTheLocation[3]=obj;
}
}
public synchronized void addTheWarrior(Warrior w){ //add the incoming warrior to the location
Warrior obj=(Warrior)charactersOccupiedTheLocation[0];
while(obj!=null && w.checkMobility() && obj.checkMobility() && obj.checkIsAlive()){
try {
wait(1000); //1000
} catch (InterruptedException ex) {}
obj=(Warrior)charactersOccupiedTheLocation[0];
}
charactersOccupiedTheLocation[0]=w;
}
public synchronized void removeTheWarrior(){ //remove outgoing warrior from the location
charactersOccupiedTheLocation[0]=null;
notifyAll();
}
}
25 changes: 25 additions & 0 deletions src/game/InhabitantCharacter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package game;

public abstract class InhabitantCharacter{
private String characterName;
private int currentXCoordinate;
private int currentYCoordinate;
public void setCharacterName(String name){
characterName=name;
}
public String getCharacterName(){
return characterName;
}
public int getXCoordinate(){
return currentXCoordinate;
}
public int getYCoordinate(){
return currentYCoordinate;
}
public void setXCoordinate(int x){
currentXCoordinate=x;
}
public void setYCoordinate(int y){
currentYCoordinate=y;
}
}
11 changes: 11 additions & 0 deletions src/game/InnocentFish.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package game;

public class InnocentFish extends Fish{
public InnocentFish(String name,int x,int y){
super(name,x,y);
}
@Override
public void run(){}
@Override
public void eat(){}
}
21 changes: 21 additions & 0 deletions src/game/KillerFish.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package game;

public class KillerFish extends Fish{
public KillerFish(String name,int x,int y){
super(name,x,y);
}
@Override
public void run(){
GridLocation gl=Game.gamecontroller.getTheLocation(this.getXCoordinate(),this.getYCoordinate()); //get its location
Warrior w=gl.checkWarrior(); //get the wrrior there
kill(w); //execute kill method
}
public void kill(Warrior w){
if(!w.checkImmortle()){
w.setIsAlive(false); //kill the warrior
System.out.println("KillerFish "+this.getCharacterName()+" kills "+w.getCharacterName());
}
}
@Override
public void eat(){}
}
22 changes: 22 additions & 0 deletions src/game/Lotus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package game;

public class Lotus{
private int xCoordinate;
private int yCoordinate;
public Lotus(int x,int y){
xCoordinate=x;
yCoordinate=y;
}
public int getXCoordinate(){
return xCoordinate;
}
public int getYCoordinate(){
return yCoordinate;
}
public void setXCoordinate(int x){
xCoordinate=x;
}
public void setYCoordinate(int y){
yCoordinate=y;
}
}
Loading

0 comments on commit e17efcc

Please sign in to comment.