Skip to content

Instantly share code, notes, and snippets.

@annibuliful
Created February 23, 2018 07:11
Show Gist options
  • Save annibuliful/e1f6f33ee7645bda7e3d0be7a477f2d0 to your computer and use it in GitHub Desktop.
Save annibuliful/e1f6f33ee7645bda7e3d0be7a477f2d0 to your computer and use it in GitHub Desktop.
package project;
//Jarupong pajakgo 6088107 section 2
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
public class Arena {
public enum Row {Front, Back}; //enum for specifying the front or back row
public enum Team {A, B}; //enum for specifying team A or B
private Player[][] teamA = null; //two dimensional array representing the players of Team A
private Player[][] teamB = null; //two dimensional array representing the players of Team B
private int numRowPlayers;
private int numRow = 2;
public static final int MAXROUNDS = 100; //Max number of turn
public static final int MAXEACHTYPE = 3;//Max number of players of each type, in each team.
private final Path logFile = Paths.get("battle_log.txt");
private int numRounds = 0; //keep track of the number of rounds so far
private int countplayer = 0;
/**
* Constructor.
* @param _numRowPlayers is the number of player in each row.
*/
public Arena(int _numRowPlayers)
{
//INSERT YOUR CODE HERE
this.teamA = new Player[numRow][_numRowPlayers];
this.teamB = new Player[numRow][_numRowPlayers];
this.numRowPlayers = _numRowPlayers;
////Keep this block of code. You need it for initialize the log file.
////(You will learn how to deal with files later)
try {
Files.deleteIfExists(logFile);
} catch (IOException e) {
e.printStackTrace();
}
/////////////////////////////////////////
}
/**
* Returns true if "player" is a member of "team", false otherwise.
* Assumption: team can be either Team.A or Team.B
* @param player
* @param team
* @return
*/
public boolean isMemberOf(Player player, Team team)
{
//INSERT YOUR CODE HERE
boolean checkmember = false;
if(team == Team.A) {
for (int i = 0; i < this.numRow; i++) {
for (int j = 0; j < teamA[i].length; j++) {
if(teamA[i][j]==player) {
checkmember = true;
break;
}
}
}
}else if(team == Team.B) {
for (int i = 0; i < this.numRow; i++) {
for (int j = 0; j < teamB[i].length; j++) {
if(teamB[i][j]==player) {
checkmember = true;
break;
}
}
}
}
return checkmember;
}
/**
* This methods receives a player configuration (i.e., team, type, row, and position),
* creates a new player instance, and places him at the specified position.
* @param team is either Team.A or Team.B
* @param pType is one of the Player.Type {Healer, Tank, Samurai, BlackMage, Phoenix}
* @param row either Row.Front or Row.Back
* @param position is the position of the player in the row. Note that position starts from 1, 2, 3....
*/
public void addPlayer(Team team, Player.PlayerType pType, Row row, int position)
{
//INSERT YOUR CODE HERE
if(team == Arena.Team.A) {
if(row == Arena.Row.Front) {
teamA[0][position-1] = new Player(pType);
teamA[0][position-1].setTeam("A");
teamA[0][position-1].setRow("Front");
teamA[0][position-1].setPosition(position);
}else {
teamA[1][position-1] = new Player(pType);
teamA[1][position-1].setTeam("A");
teamA[1][position-1].setRow("Back");
teamA[1][position-1].setPosition(position);
}
}else if(team == Arena.Team.B) {
if(row == Arena.Row.Front) {
teamB[0][position-1] = new Player(pType);
teamB[0][position-1].setTeam("B");
teamB[0][position-1].setRow("Front");
teamB[0][position-1].setPosition(position);
}else {
teamB[1][position-1] = new Player(pType);
teamB[1][position-1].setTeam("B");
teamB[1][position-1].setRow("Back");
teamB[1][position-1].setPosition(position);
}
}
countplayer++;
}
/**
* Validate the players in both Team A and B. Returns true if all of the following conditions hold:
*
* 1. All the positions are filled. That is, there each team must have exactly numRow*numRowPlayers players.
* 2. There can be at most MAXEACHTYPE players of each type in each team. For example, if MAXEACHTYPE = 3
* then each team can have at most 3 Healers, 3 Tanks, 3 Samurais, 3 BlackMages, and 3 Phoenixes.
*
* Returns true if all the conditions above are satisfied, false otherwise.
* @return
*/
public boolean validatePlayers()
{
int countAHealer = 0;
int countASamurais = 0;
int countABlackMages = 0;
int countAPhoenix = 0;
int countATank = 0;
int countACherry = 0;
int countBHealer = 0;
int countBSamurais = 0;
int countBBlackMages = 0;
int countBPhoenix = 0;
int countBTank = 0;
int countBCherry = 0;
boolean result = true;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < numRowPlayers; j++) {
if(Player.PlayerType.Healer == teamA[i][j].getType()) {
countAHealer++;
}else if(Player.PlayerType.Samurai == teamA[i][j].getType()) {
countASamurais++;
}else if(Player.PlayerType.BlackMage == teamA[i][j].getType()) {
countABlackMages++;
}else if(Player.PlayerType.Phoenix == teamA[i][j].getType()) {
countAPhoenix++;
}else if(Player.PlayerType.Tank == teamA[i][j].getType()) {
countATank++;
}else if(Player.PlayerType.Cherry == teamA[i][j].getType()) {
countACherry++;
}else if(Player.PlayerType.Healer == teamB[i][j].getType()) {
countBHealer++;
}else if(Player.PlayerType.Samurai == teamB[i][j].getType()) {
countBSamurais++;
}else if(Player.PlayerType.BlackMage == teamB[i][j].getType()) {
countABlackMages++;
}else if(Player.PlayerType.Phoenix == teamB[i][j].getType()) {
countBPhoenix++;
}else if(Player.PlayerType.Tank == teamB[i][j].getType()) {
countBTank++;
}else if(Player.PlayerType.Cherry == teamB[i][j].getType()) {
countBCherry++;
}
}
}
if(countAHealer > MAXEACHTYPE || countASamurais > MAXEACHTYPE || countABlackMages > MAXEACHTYPE || countAPhoenix > MAXEACHTYPE || countATank > MAXEACHTYPE || countACherry > MAXEACHTYPE
|| countBHealer > MAXEACHTYPE || countBSamurais > MAXEACHTYPE || countBBlackMages > MAXEACHTYPE || countBPhoenix >MAXEACHTYPE || countBTank > MAXEACHTYPE || countBCherry > MAXEACHTYPE) {
result = false;
}
if(countplayer == 2*numRow*numRowPlayers) {
result = true;
}else {
result = false;
}
return result;
}
/**
* Returns the sum of HP of all the players in the given "team"
* @param team
* @return
*/
public static double getSumHP(Player[][] team)
{
double getSumHP = 0;
for (int i = 0; i < 2; i++) {
for (int j = team[i].length-1; j >= 0; j--) {
if(team[i][j].getCurrentHP() > 0) {
getSumHP += team[i][j].getCurrentHP();
}
}
}
return getSumHP;
}
/**
* Return the team (either teamA or teamB) whose number of alive players is higher than the other.
*
* If the two teams have an equal number of alive players, then the team whose sum of HP of all the
* players is higher is returned.
*
* If the sums of HP of all the players of both teams are equal, return teamA.
* @return
*/
public Player[][] getWinningTeam()
{
if(getSumHP(teamA)>=getSumHP(teamB)) {
return teamA;
}else {
return teamB;
}
}
/**
* This method simulates the battle between teamA and teamB. The method should have a loop that signifies
* a round of the battle. In each round, each player in teamA invokes the method takeAction(). The players'
* turns are ordered by its position in the team. Once all the players in teamA have invoked takeAction(),
* not it is teamB's turn to do the same.
*
* The battle terminates if one of the following two conditions is met:
*
* 1. All the players in a team has been eliminated.
* 2. The number of rounds exceeds MAXROUNDS
*
* After the battle terminates, report the winning team, which is determined by getWinningTeam().
*/
public void startBattle()
{
while(getSumHP(teamA)>0 && getSumHP(teamB)>0 && numRounds<= MAXROUNDS) {
numRounds++;
System.out.println("@ Round"+numRounds);
//TeamA
for (int i = 0; i < numRow; i++) {
for (int j = 0; j < numRowPlayers; j++) {
Player playerA = teamA[i][j];
if(playerA.getCurrentHP() > 0) {
playerA.takeAction(this);
}
}
}
//TeamB
for (int i = 0; i < numRow; i++) {
for (int j = 0; j < numRowPlayers; j++) {
Player playerB = teamB[i][j];
if(playerB.getCurrentHP() > 0) {
playerB.takeAction(this);
}
}
}
displayArea(this, true);
logAfterEachRound();
}
if(getWinningTeam() == teamA) {
System.out.println("@@@ TeamA won");
}else if(getWinningTeam() == teamB) {
System.out.println("@@@ TeamB won");
}
}
/**
* This method displays the current area state, and is already implemented for you.
* In startBattle(), you should call this method once before the battle starts, and
* after each round ends.
*
* @param arena
* @param verbose
*/
public static void displayArea(Arena arena, boolean verbose)
{
StringBuilder str = new StringBuilder();
if(verbose)
{
str.append(String.format("%43s %40s","Team A","")+"\t\t"+String.format("%-38s%-40s","","Team B")+"\n");
str.append(String.format("%43s","BACK ROW")+String.format("%43s","FRONT ROW")+" | "+String.format("%-43s","FRONT ROW")+"\t"+String.format("%-43s","BACK ROW")+"\n");
for(int i = 0; i < arena.numRowPlayers; i++)
{
str.append(String.format("%43s",arena.teamA[1][i])+String.format("%43s",arena.teamA[0][i])+" | "+String.format("%-43s",arena.teamB[0][i])+String.format("%-43s",arena.teamB[1][i])+"\n");
}
}
str.append("@ Total HP of Team A = "+getSumHP(arena.teamA)+". @ Total HP of Team B = "+getSumHP(arena.teamB)+"\n\n");
System.out.print(str.toString());
}
/**
* This method writes a log (as round number, sum of HP of teamA, and sum of HP of teamB) into the log file.
* You are not to modify this method, however, this method must be call by startBattle() after each round.
*
* The output file will be tested against the auto-grader, so make sure the output look something like:
*
* 1 47415.0 49923.0
* 2 44977.0 46990.0
* 3 42092.0 43525.0
* 4 44408.0 43210.0
*
* Where the numbers of the first, second, and third columns specify round numbers, sum of HP of teamA, and sum of HP of teamB respectively.
*/
private void logAfterEachRound()
{
try {
Files.write(logFile, Arrays.asList(new String[]{numRounds+"\t"+getSumHP(teamA)+"\t"+getSumHP(teamB)}), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
} catch (IOException e) {
e.printStackTrace();
}
}
public Player[][] getTeamA() {
return teamA;
}
public Player[][] getTeamB()
{
return teamB;
}
//Add method
public Player findTarget(Player player) {
Player [][] opp = null;
Player target = null;
boolean findtarget = false;
double mininum = 9999;
if(isMemberOf(player, Team.A)) {
opp = teamB;
}else if(isMemberOf(player, Team.B)) {
opp = teamA;
}
for (int i = 0; i < 2; i++) {
if(i == 0) {
for (int j = 0; j < opp[i].length; j++) {
target = opp[i][j];
if(target.isTaunting() && target.getCurrentHP() > 0) {
findtarget = true;
break;
}
}
}else {
for (int j = 0; j < opp[i].length; j++) {
target = opp[i][j];
if(target.isTaunting() && target.getCurrentHP() > 0) {
findtarget = true;
break;
}
}
}
if(findtarget) {
break;
}
}
if(!findtarget) {
for (int i = 0; i < 2; i++) {
if(i==0) {
for (int j = 0; j < opp[i].length; j++) {
if(opp[i][j].getCurrentHP() > 0 && opp[i][j].getCurrentHP() < mininum) {
mininum = opp[i][j].getCurrentHP();
target = opp[i][j];
findtarget = true;
}
}
}else {
for (int j = 0; j < opp[i].length; j++) {
if(opp[i][j].getCurrentHP() > 0 && opp[i][j].getCurrentHP() < mininum) {
mininum = opp[i][j].getCurrentHP();
target = opp[i][j];
findtarget = true;
}
}
}
if(findtarget) {
break;
}else {
target = null;
}
}
}
return target;
}
public Player[][] getMyTeam(Player player){
if(isMemberOf(player, Team.A)) {
return getTeamA();
}else if(isMemberOf(player, Team.B)){
return getTeamB();
}
return null;
}
public Player[][] OppTeam(Player player){
if(isMemberOf(player, Team.A)) {
return getTeamA();
}else if(isMemberOf(player, Team.B)){
return getTeamB();
}
return null;
}
}
package project;
//Jarupong pajakgo 6088107 section 2
public class Player {
public enum PlayerType {Healer, Tank, Samurai, BlackMage, Phoenix, Cherry};
private PlayerType type; //Type of this player. Can be one of either Healer, Tank, Samurai, BlackMage, or Phoenix
private double maxHP; //Max HP of this player
private double currentHP; //Current HP of this player
private double atk; //Attack power of this player
//ADD
private int internalcount;
private int special_number;
private Player player_cursed = null;
private String teamName;
private String Row;
private int position;
private boolean isTaunt;
private boolean isCursed;
private boolean isSleeeping;
/**
* Constructor of class Player, which initializes this player's type, maxHP, atk, numSpecialTurns,
* as specified in the given table. It also reset the internal turn count of this player.
* @param _type
*/
public Player(PlayerType _type)
{
this.type = _type;
if(this.type == Player.PlayerType.Healer) {
this.special_number = 4;
this.maxHP = 4790;
this.currentHP = 4790;
this.atk = 238;
}else if(this.type == Player.PlayerType.Tank) {
this.special_number = 4;
this.maxHP = 5340;
this.currentHP = 5340;
this.atk = 255;
}else if(this.type == Player.PlayerType.Samurai) {
this.special_number = 3;
this.maxHP = 4005;
this.currentHP = 4005;
this.atk = 368;
}else if(this.type == Player.PlayerType.BlackMage) {
this.special_number = 4;
this.maxHP = 4175;
this.currentHP = 4175;
this.atk = 303;
}else if(this.type == Player.PlayerType.Phoenix) {
this.special_number = 8;
this.maxHP = 4175;
this.currentHP = 4175;
this.atk = 209;
}else if(this.type == Player.PlayerType.Cherry) {
this.special_number = 4;
this.maxHP = 3560;
this.currentHP = 3560;
this.atk = 198;
}
}
/**
* Returns the current HP of this player
* @return
*/
public double getCurrentHP()
{
return this.currentHP;
}
/**
* Returns type of this player
* @return
*/
public Player.PlayerType getType()
{
return this.type;
}
/**
* Returns max HP of this player.
* @return
*/
public double getMaxHP()
{
return this.maxHP;
}
/**
* Returns whether this player is sleeping.
* @return
*/
public boolean isSleeping()
{
return isSleeeping;
}
/**
* Returns whether this player is being cursed.
* @return
*/
public boolean isCursed()
{
return isCursed;
}
/**
* Returns whether this player is alive (i.e. current HP > 0).
* @return
*/
public boolean isAlive()
{
if(this.currentHP>0) {
return true;
}else {
return false;
}
}
/**
* Returns whether this player is taunting the other team.
* @return
*/
public boolean isTaunting()
{
return isTaunt;
}
public void attack(Player target)
{
target.currentHP -= this.atk;
if(target.currentHP < 0) {
target.currentHP = 0;
}
}
public Player IsTaunting(Player[][] team) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < team[i].length; j++) {
if(team[i][j].isTaunt) {
return team[i][j];
}
}
}
return null;
}
public void useSpecialAbility(Player[][] myTeam, Player[][] theirTeam)
{
internalcount = 0;
if(this.type == PlayerType.Healer) {
Player lowest = findHeal(myTeam);
if(lowest.isCursed) {
return;
}
if(lowest.currentHP > 0 && lowest.currentHP<lowest.maxHP && lowest.isCursed == false) {
System.out.println("#"+this.getTeam()+"["+this.getRow()+"]"+"["+this.getPosition()+"]"+" {"+this.type.toString()+"}"+" Heals "+lowest.getTeam()+"["+lowest.getRow()+"]"+"["+lowest.getPosition()+"]"+" {"+lowest.type.toString()+"}");
lowest.currentHP += (0.25*lowest.maxHP);
if(lowest.currentHP>lowest.maxHP) {
lowest.currentHP = lowest.maxHP;
}
}
}else if(this.type == PlayerType.Tank) {
System.out.println("#"+this.getTeam()+"["+this.getRow()+"]"+"["+this.getPosition()+"]"+" {"+this.type.toString()+"}"+" is Taunting ");
setTaunt(true);
}else if(this.type == PlayerType.BlackMage) {
Player curse = findCurses(theirTeam);
if(IsTaunting(theirTeam) != null) {
curse = IsTaunting(theirTeam);
}
if (curse != null) {
System.out.println("#"+this.getTeam()+"["+this.getRow()+"]"+"["+this.getPosition()+"]"+" {"+this.type.toString()+"}"+" Curses "+curse.getTeam()+"["+curse.getRow()+"]"+"["+curse.getPosition()+"]"+" {"+curse.type.toString()+"}");
curse.setCursed(true);
player_cursed = curse;
}
}else if (this.type == PlayerType.Phoenix) {
Player dead = myTeam[0][0];
boolean find_dead = false;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < myTeam[0].length; j++) {
if(!myTeam[i][j].isAlive() && find_dead == false) {
dead = myTeam[i][j];
find_dead = true;
}
}
}
System.out.println("#"+this.getTeam()+"["+this.getRow()+"]"+"["+this.getPosition()+"]"+" {"+this.type.toString()+"}"+" Revives "+dead.getTeam()+"["+dead.getRow()+"]"+"["+dead.getPosition()+"]"+" {"+dead.type.toString()+"}");
dead.currentHP = dead.currentHP + (dead.maxHP*0.3);
dead.setTaunt(false);
dead.setCursed(false);
dead.internalcount = 0;
}else if (this.type == PlayerType.Cherry) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < theirTeam[0].length; j++) {
if(theirTeam[i][j].isAlive()) {
System.out.println("#"+this.getTeam()+"["+this.getRow()+"]"+"["+this.getPosition()+"]"+" {"+this.type.toString()+"}"+" Feeds a Fortune Cookies to "+theirTeam[i][j].getTeam()+"["+theirTeam[i][j].getRow()+"]"+"["+theirTeam[i][j].getPosition()+"]"+" {"+theirTeam[i][j].type.toString()+"}");
theirTeam[i][j].setSleeping(true);
}
}
}
}
}
/**
* This method is called by Arena when it is this player's turn to take an action.
* By default, the player simply just "attack(target)". However, once this player has
* fought for "numSpecialTurns" rounds, this player must perform "useSpecialAbility(myTeam, theirTeam)"
* where each player type performs his own special move.
* @param arena
*/
public void takeAction(Arena arena)
{
if(this.type == PlayerType.BlackMage && player_cursed != null) {
player_cursed.setCursed(false);
player_cursed = null;
}
if(this.isTaunt) {
this.setTaunt(false);
}
if(this.isSleeeping) {
this.setSleeping(false);
return;
}
addInternalcount();
Player[][] myteam = null;
Player[][] opp = null;
if(arena.getMyTeam(this) == arena.getTeamA()) {
myteam = arena.getTeamA();
opp = arena.getTeamB();
}else if(arena.getMyTeam(this) == arena.getTeamB()) {
myteam = arena.getTeamB();
opp = arena.getTeamA();
}
if(this.type == PlayerType.Healer && internalcount == this.special_number) {
useSpecialAbility(myteam, opp);
}else if(this.type == PlayerType.Tank && internalcount == this.special_number) {
useSpecialAbility(myteam, opp);
}else if(this.type == PlayerType.Samurai && internalcount == this.special_number) {
internalcount = 0;
Player target = arena.findTarget(this);
if(target == null) {
return;
}
System.out.println("#"+this.getTeam()+"["+this.getRow()+"]"+"["+this.getPosition()+"]"+" {"+this.type.toString()+"}"+" Double-Slash "+target.getTeam()+"["+target.getRow()+"]"+"["+target.getPosition()+"]"+" {"+target.type.toString()+"}");
attack(target);
attack(target);
}else if (this.type == PlayerType.BlackMage && internalcount == this.special_number) {
useSpecialAbility(myteam, opp);
}else if (this.type == PlayerType.Phoenix && internalcount == this.special_number) {
useSpecialAbility(myteam, opp);
}else if (this.type == PlayerType.Cherry && internalcount == this.special_number) {
useSpecialAbility(myteam, opp);
}else {
Player target = arena.findTarget(this);
if(target != null) {
attack(target);
System.out.println("#"+this.getTeam()+"["+this.getRow()+"]"+"["+this.getPosition()+"]"+" {"+this.type.toString()+"}"+" Attacks "+target.getTeam()+"["+target.getRow()+"]"+"["+target.getPosition()+"]"+" {"+target.type.toString()+"}");
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < opp[i].length; j++) {
if(!opp[i][j].isAlive()) {
opp[i][j].isCursed = false;
opp[i][j].isTaunt = false;
opp[i][j].isSleeeping = false;
}
}
}
}
/**
* This method overrides the default Object's toString() and is already implemented for you.
*/
@Override
public String toString()
{
return "["+this.type.toString()+" HP:"+this.currentHP+"/"+this.maxHP+" ATK:"+this.atk+"]["
+((this.isCursed())?"C":"")
+((this.isTaunting())?"T":"")
+((this.isSleeping())?"S":"")
+"]";
}
//ADD
public void setTeam(String team) {
this.teamName = team;
}
public String getTeam() {
return this.teamName;
}
public void setRow(String row) {
this.Row = row;
}
public String getRow() {
return Row;
}
public void setPosition(int position) {
this.position = position;
}
public int getPosition() {
return this.position;
}
public void addInternalcount() {
this.internalcount++;
}
public void setTaunt(boolean isTaunt) {
this.isTaunt = isTaunt;
}
public void setCursed(boolean isCursed) {
this.isCursed = isCursed;
}
public void setSleeping(boolean isSleeping) {
this.isSleeeping = isSleeping;
}
public Player findHeal(Player[][] myteam) {
double mininum = 9999;
Player target = null;
for (int i = 0; i < 2; i++) {
if(i==0) {
for (int j = 0; j < myteam[i].length; j++) {
if(myteam[i][j].getCurrentHP() > 0 && myteam[i][j].getCurrentHP() /(myteam[i][j].getMaxHP())*100 < mininum) {
mininum = myteam[i][j].getCurrentHP() /(myteam[i][j].getMaxHP())*100;
target = myteam[i][j];
}
}
}else {
for (int j = 0; j < myteam[i].length; j++) {
if(myteam[i][j].getCurrentHP() > 0 && myteam[i][j].getCurrentHP() /(myteam[i][j].getMaxHP())*100 < mininum) {
mininum = myteam[i][j].getCurrentHP() /(myteam[i][j].getMaxHP())*100;
target = myteam[i][j];
}
}
}
}
return target;
}
public Player findCurses(Player[][] theirTeam) {
double mininum = 9999;
Player target = null;
boolean findcurse = false;
for (int i = 0; i < 2; i++) {
if(i==0) {
for (int j = 0; j < theirTeam[i].length; j++) {
if(theirTeam[i][j].isAlive() && theirTeam[i][j].currentHP < mininum) {
mininum = theirTeam[i][j].currentHP;
target = theirTeam[i][j];
findcurse = true;
}
}
}else {
for (int j = 0; j < theirTeam[i].length; j++) {
if(theirTeam[i][j].isAlive() && theirTeam[i][j].currentHP < mininum && findcurse == false) {
mininum = theirTeam[i][j].currentHP;
target = theirTeam[i][j];
findcurse = true;
}
}
}
}
return target;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment