So I have a class that holds a bunch of stat info and such about a "gamer". I'm trying to implement a mock trophy system for certain stat goals that were achieved through an RNG.
I have a 2d int array that holds values for the grade/level of the trophy, that once achieved upgrades the trophy from bronze, to silver, etc and a corresponding 2d String array that holds the titles of such trophy levels. In testing what seemed to work with a now unused method actually provided trophies to only certain types. What I have now is a path I think I need to follow in order for this to work. I have a method called getBadges(int requestedStat) that takes an index value for another array to view that stats trophies. In that method is a for loop that compares the method's argument to both 2d arrays to determine if the stat's value (stored in another array) qualifies it for a bronze, silver, or gold trophy. My main problem is I'm getting lost in how to access these different data points in my 2d arrays without going out of the index's range. Not to mention when I set up a bunch of if-else statements my test output always produced the trophy's name, but no trophy level. Like such:
Healer: No Badge
Explorer: No Badge
Socialite: No Badge
Contributor: No Badge
As the skill points go up so should the badge levels (i.e. go from "No Badge" to "Bronze" etc). Is this a logic or syntax error? I'm super confused on what is happening in my code, despite my pseudo-code efforts. Here is the Gamer class:
package GamerProject;
import java.io.Serializable;
import java.util.Comparator;
public class Gamer implements Serializable, Comparable<Gamer> {
private String playerName;
private static final int HEALTH_POINTS_RESD = 23;
private static final int AREAS_VISITED = 200;
private static final int PLAYERS_ENCOUNTERED = 175;
private static final int MAPS_CREATED = 1500;
private static final int ITEMS_GATHERED = 20;
private static final int ITEMS_REPAIRED = 100;
private static final int ITEMS_MERGED = 125;
private static final int TOP_SCORES = 250;
private static final int DMG_POINTS_DEALT = 17;
private static final int MAPS_COMPLETED = 750;
private static final int LEVEL2 = 10000;
private static final int LEVEL3 = 25000;
private static final int LEVEL4 = 80000;
private static final int LEVEL5 = 150000;
private static final int LEVEL6 = 300000;
private static final int LEVEL7 = 1000000;
private static final int LEVEL8 = 2200000;
private static final int LEVEL9 = 4500000;
private static final int LEVEL10 = 10000000;
private static final int LEVEL11 = 20000000;
private static final int LEVEL12 = 35000000;
private final int[] gamerStatValues = new int[10];
private final int[] gamerActions = {HEALTH_POINTS_RESD, AREAS_VISITED, PLAYERS_ENCOUNTERED, MAPS_CREATED,
ITEMS_GATHERED, ITEMS_REPAIRED, ITEMS_MERGED, TOP_SCORES, DMG_POINTS_DEALT, MAPS_COMPLETED};
private final int[] expValues = {LEVEL2, LEVEL3, LEVEL4, LEVEL5, LEVEL6, LEVEL7,
LEVEL8, LEVEL9, LEVEL10, LEVEL11, LEVEL12};
private final int[][] badgePoints = {
{0, 2000, 10000, 30000, 100000, 200000},
{0, 50, 1000, 5000, 17000, 40000},
{0, 100, 1000, 2000, 10000, 30000},
{0, 3, 10, 20, 90, 150},
{0, 2000, 10000, 30000, 100000, 200000},
{0, 100, 1000, 5000, 15000, 40000},
{0, 100, 500, 2000, 10000, 40000},
{0, 20, 200, 1000, 5000, 20000},
{0, 2000, 10000, 30000, 100000, 300000},
{0, 10, 50, 200, 500, 5000}};
private final String[] badgeTitles = {"Healer: ", "Explorer: ", "Socialite: ", "Contributor: ",
"Hoarder: ", "Fixer: ", "Joiner: ", "Leader: ", "Punisher: ", "Obsessed: ",};
private final String[] badgeRanks = {"No Badge ", "Tin ", "Bronze ", "Silver ", "Gold ", "Platinum "};
Gamer() {
playerName = "";
}
public int getTotalExp() {
int totalExp = 0;
for (int i = 0; i < gamerStatValues.length; i++) {
totalExp += (gamerStatValues[i] * gamerActions[i]);
}
return totalExp;
}
public int getLevel() {
int playerLevel = 1;
int totalExp = getTotalExp();
for (int i = 0; i < expValues.length; i++) {
if (totalExp >= expValues[i]) {
playerLevel += 1;
//System.out.println(getTotalExp());
}
}
return playerLevel;
}
public String getBadge(int requestedStat) {
String badgeOutput = "";
//index = 0;
if (requestedStat >= 0 && requestedStat <=9) {
for (int i = 0; i < badgeRanks.length; i++) {//not sure how to get around going out of the array bounds
if (gamerActions[requestedStat] >= badgePoints[requestedStat][i]
&& gamerActions[requestedStat] < badgePoints[requestedStat][i + 1]) {
badgeOutput = badgeTitles[requestedStat] + badgeRanks[i];
} else if (gamerActions[requestedStat] >= badgePoints[requestedStat][i+1]
&& gamerActions[requestedStat] < badgePoints[requestedStat][i + 2]) {
badgeOutput = badgeTitles[requestedStat] + badgeRanks[i+1];
}
}
//did this as an extraneous solution. Still doesn't work
// if (gamerActions[requestedStat] >= badgePoints[requestedStat][index]
// && gamerActions[requestedStat] < badgePoints[requestedStat][index + 1]) {
// badgeOutput = badgeTitles[requestedStat] + badgeRanks[index];
// } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+1]
// && gamerActions[requestedStat] < badgePoints[requestedStat][index + 2]) {
// badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+1];
// } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+2]
// && gamerActions[requestedStat] < badgePoints[requestedStat][index + 3]) {
// badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+2];
// } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+3]
// && gamerActions[requestedStat] < badgePoints[requestedStat][index + 4]) {
// badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+3];
// } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+4]
// && gamerActions[requestedStat] < badgePoints[requestedStat][index + 5]) {
// badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+4];
// } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+5]
// && gamerActions[requestedStat] < badgePoints[requestedStat][index + 6]) {
// badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+5];
// } else {
// badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+6];
// }
//
} else {
badgeOutput = "No Badges Available";
}
return badgeOutput;
}
//Incomplete Method
public String getBadges() {
String badgeOutput = "Badges: ";
for (int i = 0; i < badgeTitles.length; i++) {
// if (gamerActions[i]) {
//
// }
}
return badgeOutput;
}
public String getPlayerName() {
return playerName;
}
public int getHealthPointsResd() {
return gamerStatValues[0];
}
public int getAreasVisited() {
return gamerStatValues[1];
}
public int getPlayersEncountered() {
return gamerStatValues[2];
}
public int getMapsCreated() {
return gamerStatValues[3];
}
public int getItemsGathered() {
return gamerStatValues[4];
}
public int getItemsRepaired() {
return gamerStatValues[5];
}
public int getItemsMerged() {
return gamerStatValues[6];
}
public int getTopScores() {
return gamerStatValues[7];
}
public int getDmgPointsDealt() {
return gamerStatValues[8];
}
public int getMapsCompleted() {
return gamerStatValues[9];
}
//Unused Method
public void updateRandomGamerAction(int intValue) {
if (intValue == 0) {
gamerActions[0]+=1;
} else if (intValue == 1) {
gamerActions[1]+=1;
} else if (intValue == 2) {
gamerActions[2]+=1;
} else if (intValue == 3) {
gamerActions[3]+=1;
} else if (intValue == 4) {
gamerActions[4]+=1;
} else if (intValue == 5) {
gamerActions[5]+=1;
} else if (intValue == 6) {
gamerActions[6]+=1;
} else if (intValue == 7) {
gamerActions[7]+=1;
} else if (intValue == 8) {
gamerActions[8]+=1;
} else {
gamerActions[9]+=1;
}
}
public String setPlayerName(String playerName) {
this.playerName = playerName;
return this.playerName;
}
public int setHealthPointsResd(int healthPointsResd) {
if (healthPointsResd >= 0) {
gamerStatValues[0] = healthPointsResd;
return gamerStatValues[0];
} else {
return gamerStatValues[0];
}
}
public int setAreasVisited(int areasVisited) {
if (areasVisited >= 0) {
gamerStatValues[1] = areasVisited;
return gamerStatValues[1];
} else {
return gamerStatValues[1];
}
}
public int setPlayersEncountered(int playersEncountered) {
if (playersEncountered >= 0) {
gamerStatValues[2] = playersEncountered;
return gamerStatValues[2];
} else {
return gamerStatValues[2];
}
}
public int setMapsCreated(int mapsCreated) {
if (mapsCreated >= 0) {
gamerStatValues[3] = mapsCreated;
return gamerStatValues[3];
} else {
return gamerStatValues[3];
}
}
public int setItemsGathered(int itemsGathered) {
if (itemsGathered >= 0) {
gamerStatValues[4] = itemsGathered;
return gamerStatValues[4];
} else {
return gamerStatValues[4];
}
}
public int setItemsRepaired(int itemsRepaired) {
if (itemsRepaired >= 0) {
gamerStatValues[5] = itemsRepaired;
return gamerStatValues[5];
} else {
return gamerStatValues[5];
}
}
public int setItemsMerged(int itemsMerged) {
if (itemsMerged >= 0) {
gamerStatValues[6] = itemsMerged;
return gamerStatValues[6];
} else {
return gamerStatValues[6];
}
}
public int setTopScores(int topScores) {
if (topScores >= 0) {
gamerStatValues[7] = topScores;
return gamerStatValues[7];
} else {
return gamerStatValues[7];
}
}
public int setDmgPointsDealt(int dmgPointsDealt) {
if (dmgPointsDealt >= 0) {
gamerStatValues[8] = dmgPointsDealt;
return gamerStatValues[8];
} else {
return gamerStatValues[8];
}
}
public int setMapsCompleted(int mapsCompleted) {
if (mapsCompleted >= 0) {
gamerStatValues[9] = mapsCompleted;
return gamerStatValues[9];
} else {
return gamerStatValues[9];
}
}
public void setStatsToZero(){
for (int i = 0; i < gamerActions.length; i++) {
gamerActions[i] = 0;
}
}
public String statsString() {
return "Stats: " + "Health Points Restored = " + gamerStatValues[0]
+ ",\n Areas Visited = " + gamerStatValues[1] + ", PlayersEncountered = " + gamerStatValues[2]
+ ", Maps Created = " + gamerStatValues[3] + ",\n Items Gathered = " + gamerStatValues[4]
+ ", Items Repaired = " + gamerStatValues[5] + ", Items Merged = " + gamerStatValues[6]
+ ",\n Top Scores = " + gamerStatValues[7] + ", Damage Points Dealt = " + gamerStatValues[8]
+ ", Maps Completed = " + gamerStatValues[9] + '}';
}
public String shortDecription() {
return String.format("%16s: Level %2d, Experience Points: %,10d",
playerName, this.getLevel(), this.getTotalExp());
}
#Override
public String toString() {
return "Gamer{" + "Player Name = " + playerName + " Player Stats: "
+ "Health Points Restored = " + gamerStatValues[0]
+ ",\n Areas Visited = " + gamerStatValues[1] + ", PlayersEncountered = " + gamerStatValues[2]
+ ", Maps Created = " + gamerStatValues[3] + ",\n Items Gathered = " + gamerStatValues[4]
+ ", Items Repaired = " + gamerStatValues[5] + ", Items Merged = " + gamerStatValues[6]
+ ",\n Top Scores = " + gamerStatValues[7] + ", Damage Points Dealt = " + gamerStatValues[8]
+ ", Maps Completed = " + gamerStatValues[9] + '}';
}
#Override
public int compareTo(Gamer player) {
if (this.getTotalExp() > player.getTotalExp()) {
return 1;
} else if (this.getTotalExp() == player.getTotalExp()) {
return 0;
} else {
return -1;
}
}
}
and here is the driver I'm testing it with:
package GamerProject;
import java.util.Random;
public class Program7Driver {
private static final int rngRange = 10;
private static final Gamer[] gamers = new Gamer[10];
private static final String[] gamerNames = {"BestGamer99", "CdrShepardN7",
"Gandalf_The_Cool", "SharpShooter01", "TheDragonborn", "SithLord01",
"MrWolfenstien", "Goldeneye007", "DungeonMaster91", "MasterThief","TheDarkKnight"};
public static void main(String[] args) {
Random rand = new Random();
for (int i = 0; i < gamers.length; i++) {
gamers[i] = new Gamer();
gamers[i].setPlayerName(gamerNames[i]);
gamers[i].setStatsToZero();
}
// for (int i = 0; i < 200000; i++) {
// int rng = rand.nextInt(rngRange);
// gamers[rng].setRandomGamerAction(rng);
// }
int count = 0;
for (int i = 0; i < 20000; i++) {
int rng = rand.nextInt(rngRange);
System.out.println(gamers[0].getBadge(count));
//System.out.println(gamers[0].toString());
//gamers[0].updateRandomGamerAction(rng);
if (rng == 0) {
gamers[0].setHealthPointsResd(gamers[0].getHealthPointsResd()+1);
} else if (rng == 1) {
gamers[0].setAreasVisited(gamers[0].getAreasVisited()+1);
} else if (rng == 2) {
gamers[0].setPlayersEncountered(gamers[0].getPlayersEncountered()+1);
} else if (rng == 3) {
gamers[0].setMapsCreated(gamers[0].getMapsCreated()+1);
} else if (rng == 4) {
gamers[0].setItemsGathered(gamers[0].getItemsGathered()+1);
} else if (rng == 5) {
gamers[0].setItemsRepaired(gamers[0].getItemsRepaired()+1);
} else if (rng == 6) {
gamers[0].setItemsMerged(gamers[0].getItemsMerged()+1);
} else if (rng == 7) {
gamers[0].setTopScores(gamers[0].getTopScores()+1);
} else if (rng == 8) {
gamers[0].setDmgPointsDealt(gamers[0].getDmgPointsDealt()+1);
} else {
gamers[0].setMapsCompleted(gamers[0].getMapsCompleted()+1);
}
count += 1;
if (count == 10) {
count -= 10;
}
// System.out.println(gamers[i].statsString());
}
}
}
Okay, I made some changes. See if this does what you were wanting:
package GamerProject;
import java.io.Serializable;
import java.util.Comparator;
public class Gamer implements Serializable, Comparable<Gamer> {
/**
*
*/
private static final long serialVersionUID = 1L;
private String playerName;
private static final int HEALTH_POINTS_RESD = 23;
private static final int AREAS_VISITED = 200;
private static final int PLAYERS_ENCOUNTERED = 175;
private static final int MAPS_CREATED = 1500;
private static final int ITEMS_GATHERED = 20;
private static final int ITEMS_REPAIRED = 100;
private static final int ITEMS_MERGED = 125;
private static final int TOP_SCORES = 250;
private static final int DMG_POINTS_DEALT = 17;
private static final int MAPS_COMPLETED = 750;
private static final int LEVEL2 = 10000;
private static final int LEVEL3 = 25000;
private static final int LEVEL4 = 80000;
private static final int LEVEL5 = 150000;
private static final int LEVEL6 = 300000;
private static final int LEVEL7 = 1000000;
private static final int LEVEL8 = 2200000;
private static final int LEVEL9 = 4500000;
private static final int LEVEL10 = 10000000;
private static final int LEVEL11 = 20000000;
private static final int LEVEL12 = 35000000;
private final int[] gamerStatValues = new int[10];
private final int[] gamerActions = {HEALTH_POINTS_RESD, AREAS_VISITED, PLAYERS_ENCOUNTERED, MAPS_CREATED,
ITEMS_GATHERED, ITEMS_REPAIRED, ITEMS_MERGED, TOP_SCORES, DMG_POINTS_DEALT, MAPS_COMPLETED};
private final int[] expValues = {LEVEL2, LEVEL3, LEVEL4, LEVEL5, LEVEL6, LEVEL7,
LEVEL8, LEVEL9, LEVEL10, LEVEL11, LEVEL12};
private final int[][] badgePoints = {
{0, 2000, 10000, 30000, 100000, 200000},
{0, 50, 1000, 5000, 17000, 40000},
{0, 100, 1000, 2000, 10000, 30000},
{0, 3, 10, 20, 90, 150},
{0, 2000, 10000, 30000, 100000, 200000},
{0, 100, 1000, 5000, 15000, 40000},
{0, 100, 500, 2000, 10000, 40000},
{0, 20, 200, 1000, 5000, 20000},
{0, 2000, 10000, 30000, 100000, 300000},
{0, 10, 50, 200, 500, 5000}};
private final String[] badgeTitles = {"Healer: ", "Explorer: ", "Socialite: ", "Contributor: ",
"Hoarder: ", "Fixer: ", "Joiner: ", "Leader: ", "Punisher: ", "Obsessed: ",};
private final String[] badgeRanks = {"No Badge ", "Tin ", "Bronze ", "Silver ", "Gold ", "Platinum "};
Gamer() {
playerName = "";
}
public int getTotalExp() {
int totalExp = 0;
for (int i = 0; i < gamerStatValues.length; i++) {
totalExp += (gamerStatValues[i] * gamerActions[i]);
}
return totalExp;
}
public int getLevel() {
int playerLevel = 1;
int totalExp = getTotalExp();
for (int i = 0; i < expValues.length; i++) {
if (totalExp >= expValues[i]) {
playerLevel += 1;
//System.out.println(getTotalExp());
}
}
return playerLevel;
}
public String getBadge(int requestedStat) {
String badgeOutput = "";
//index = 0;
if (requestedStat >= 0 && requestedStat <=9) {
for (int i = 0; i < badgeRanks.length; i++) {//not sure how to get around going out of the array bounds
if (gamerActions[requestedStat] >= badgePoints[requestedStat][i]
&& gamerActions[requestedStat] < badgePoints[requestedStat][i + 1]) {
badgeOutput = badgeTitles[requestedStat] + badgeRanks[i];
} else if (gamerActions[requestedStat] >= badgePoints[requestedStat][i+1]
&& gamerActions[requestedStat] < badgePoints[requestedStat][i + 2]) {
badgeOutput = badgeTitles[requestedStat] + badgeRanks[i+1];
}
}
//did this as an extraneous solution. Still doesn't work
// if (gamerActions[requestedStat] >= badgePoints[requestedStat][index]
// && gamerActions[requestedStat] < badgePoints[requestedStat][index + 1]) {
// badgeOutput = badgeTitles[requestedStat] + badgeRanks[index];
// } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+1]
// && gamerActions[requestedStat] < badgePoints[requestedStat][index + 2]) {
// badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+1];
// } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+2]
// && gamerActions[requestedStat] < badgePoints[requestedStat][index + 3]) {
// badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+2];
// } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+3]
// && gamerActions[requestedStat] < badgePoints[requestedStat][index + 4]) {
// badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+3];
// } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+4]
// && gamerActions[requestedStat] < badgePoints[requestedStat][index + 5]) {
// badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+4];
// } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+5]
// && gamerActions[requestedStat] < badgePoints[requestedStat][index + 6]) {
// badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+5];
// } else {
// badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+6];
// }
//
} else {
badgeOutput = "No Badges Available";
}
return badgeOutput;
}
//Incomplete Method
public String getBadges() {
String badgeOutput = "Badges: ";
for (int i = 0; i < badgeTitles.length; i++) {
// if (gamerActions[i]) {
//
// }
}
return badgeOutput;
}
public String getPlayerName() {
return playerName;
}
public int getHealthPointsResd() {
return gamerStatValues[0];
}
public int getAreasVisited() {
return gamerStatValues[1];
}
public int getPlayersEncountered() {
return gamerStatValues[2];
}
public int getMapsCreated() {
return gamerStatValues[3];
}
public int getItemsGathered() {
return gamerStatValues[4];
}
public int getItemsRepaired() {
return gamerStatValues[5];
}
public int getItemsMerged() {
return gamerStatValues[6];
}
public int getTopScores() {
return gamerStatValues[7];
}
public int getDmgPointsDealt() {
return gamerStatValues[8];
}
public int getMapsCompleted() {
return gamerStatValues[9];
}
//Unused Method
public void updateRandomGamerAction(int intValue) {
if (intValue == 0) {
gamerActions[0]+=1;
} else if (intValue == 1) {
gamerActions[1]+=1;
} else if (intValue == 2) {
gamerActions[2]+=1;
} else if (intValue == 3) {
gamerActions[3]+=1;
} else if (intValue == 4) {
gamerActions[4]+=1;
} else if (intValue == 5) {
gamerActions[5]+=1;
} else if (intValue == 6) {
gamerActions[6]+=1;
} else if (intValue == 7) {
gamerActions[7]+=1;
} else if (intValue == 8) {
gamerActions[8]+=1;
} else {
gamerActions[9]+=1;
}
}
public String setPlayerName(String playerName) {
this.playerName = playerName;
return this.playerName;
}
public int setHealthPointsResd(int healthPointsResd) {
if (healthPointsResd >= 0) {
gamerStatValues[0] = healthPointsResd;
return gamerStatValues[0];
} else {
return gamerStatValues[0];
}
}
public int setAreasVisited(int areasVisited) {
if (areasVisited >= 0) {
gamerStatValues[1] = areasVisited;
return gamerStatValues[1];
} else {
return gamerStatValues[1];
}
}
public int setPlayersEncountered(int playersEncountered) {
if (playersEncountered >= 0) {
gamerStatValues[2] = playersEncountered;
return gamerStatValues[2];
} else {
return gamerStatValues[2];
}
}
public int setMapsCreated(int mapsCreated) {
if (mapsCreated >= 0) {
gamerStatValues[3] = mapsCreated;
return gamerStatValues[3];
} else {
return gamerStatValues[3];
}
}
public int setItemsGathered(int itemsGathered) {
if (itemsGathered >= 0) {
gamerStatValues[4] = itemsGathered;
return gamerStatValues[4];
} else {
return gamerStatValues[4];
}
}
public int setItemsRepaired(int itemsRepaired) {
if (itemsRepaired >= 0) {
gamerStatValues[5] = itemsRepaired;
return gamerStatValues[5];
} else {
return gamerStatValues[5];
}
}
public int setItemsMerged(int itemsMerged) {
if (itemsMerged >= 0) {
gamerStatValues[6] = itemsMerged;
return gamerStatValues[6];
} else {
return gamerStatValues[6];
}
}
public int setTopScores(int topScores) {
if (topScores >= 0) {
gamerStatValues[7] = topScores;
return gamerStatValues[7];
} else {
return gamerStatValues[7];
}
}
public int setDmgPointsDealt(int dmgPointsDealt) {
if (dmgPointsDealt >= 0) {
gamerStatValues[8] = dmgPointsDealt;
return gamerStatValues[8];
} else {
return gamerStatValues[8];
}
}
public int setMapsCompleted(int mapsCompleted) {
if (mapsCompleted >= 0) {
gamerStatValues[9] = mapsCompleted;
return gamerStatValues[9];
} else {
return gamerStatValues[9];
}
}
public void setStatsToZero(){
for (int i = 0; i < gamerActions.length; i++) {
gamerActions[i] = 0;
}
}
public String statsString() {
return "Stats: " + "Health Points Restored = " + gamerStatValues[0]
+ ",\n Areas Visited = " + gamerStatValues[1] + ", PlayersEncountered = " + gamerStatValues[2]
+ ", Maps Created = " + gamerStatValues[3] + ",\n Items Gathered = " + gamerStatValues[4]
+ ", Items Repaired = " + gamerStatValues[5] + ", Items Merged = " + gamerStatValues[6]
+ ",\n Top Scores = " + gamerStatValues[7] + ", Damage Points Dealt = " + gamerStatValues[8]
+ ", Maps Completed = " + gamerStatValues[9] + '}';
}
public String shortDecription() {
return String.format("%16s: Level %2d, Experience Points: %,10d",
playerName, this.getLevel(), this.getTotalExp());
}
#Override
public String toString() {
return "Gamer{" + "Player Name = " + playerName + " Player Stats: "
+ "Health Points Restored = " + gamerStatValues[0]
+ ",\n Areas Visited = " + gamerStatValues[1] + ", PlayersEncountered = " + gamerStatValues[2]
+ ", Maps Created = " + gamerStatValues[3] + ",\n Items Gathered = " + gamerStatValues[4]
+ ", Items Repaired = " + gamerStatValues[5] + ", Items Merged = " + gamerStatValues[6]
+ ",\n Top Scores = " + gamerStatValues[7] + ", Damage Points Dealt = " + gamerStatValues[8]
+ ", Maps Completed = " + gamerStatValues[9] + '}';
}
#Override
public int compareTo(Gamer player) {
if (this.getTotalExp() > player.getTotalExp()) {
return 1;
} else if (this.getTotalExp() == player.getTotalExp()) {
return 0;
} else {
return -1;
}
}
}
And the other:
package GamerProject;
import java.util.Random;
public class Program7Driver {
private static final int rngRange = 10;
private static final Gamer[] gamers = new Gamer[10];
private static final String[] gamerNames = {"BestGamer99", "CdrShepardN7",
"Gandalf_The_Cool", "SharpShooter01", "TheDragonborn", "SithLord01",
"MrWolfenstien", "Goldeneye007", "DungeonMaster91", "MasterThief","TheDarkKnight"};
public static void main(String[] args) {
Random rand = new Random();
for (int i = 0; i < gamers.length; i++) {
gamers[i] = new Gamer();
gamers[i].setPlayerName(gamerNames[i]);
gamers[i].setStatsToZero();
}
// for (int i = 0; i < 200000; i++) {
// int rng = rand.nextInt(rngRange);
// gamers[rng].setRandomGamerAction(rng);
// }
int count = 0;
for (int i = 0; i < gamers.length; i++) {
int rng = rand.nextInt(rngRange);
System.out.println(gamers[i]);
//System.out.println(gamers[0].toString());
//gamers[0].updateRandomGamerAction(rng);
if (rng == 0) {
gamers[0].setHealthPointsResd(gamers[0].getHealthPointsResd()+1);
} else if (rng == 1) {
gamers[0].setAreasVisited(gamers[0].getAreasVisited()+1);
} else if (rng == 2) {
gamers[0].setPlayersEncountered(gamers[0].getPlayersEncountered()+1);
} else if (rng == 3) {
gamers[0].setMapsCreated(gamers[0].getMapsCreated()+1);
} else if (rng == 4) {
gamers[0].setItemsGathered(gamers[0].getItemsGathered()+1);
} else if (rng == 5) {
gamers[0].setItemsRepaired(gamers[0].getItemsRepaired()+1);
} else if (rng == 6) {
gamers[0].setItemsMerged(gamers[0].getItemsMerged()+1);
} else if (rng == 7) {
gamers[0].setTopScores(gamers[0].getTopScores()+1);
} else if (rng == 8) {
gamers[0].setDmgPointsDealt(gamers[0].getDmgPointsDealt()+1);
} else {
gamers[0].setMapsCompleted(gamers[0].getMapsCompleted()+1);
}
count += 1;
if (count == 10) {
count -= 10;
}
// System.out.println(gamers[i].statsString());
}
}
}
I'm creating a TCP game of Battleship in Java, and as I've made some functions work, methods that used to work no longer work.
Here's what's happening when it's a users turn. A bomb being dropped on 2 diffrent char[][], where clientGrid is the actual grid where the client has his ships. It's on this grid where the dropBomb method is being printed, and tells us whether a bomb has hit or not. clientDamage is just an overview for the server to see where he has previously dropped bombs.
if (inFromServer.ready()) {
System.out.println("\nInput coordinate");
int x = scan.nextInt();
int y = scan.nextInt();
System.out.println(dropBomb(x, y, clientGrid));
dropBomb(x, y, clientDamage);
outToClient.writeChars("Enemy player has targeted " + x + ", " + y +'\n');
System.out.println(printBoard(clientDamage));
}
Here's the drop bomb method. It's proved to work before, however I have made some small changes to it. However I can't see why it wouldn't work. + indicates that there is a ship, x indicates that the target already has been bombed, and the else is ~, which is water. The method always returns "Nothing was hit on this coordinate...", and I can't seem to figure out why?
public static String dropBomb(int row, int col, char[][] board) {
if (row < gridSize && col < gridSize) {
if (board[row][col] == '+') {
board[row][col] = 'x';
return "Ship has been hit!";
} else if (board[row][col] == 'x') {
return "Already bombed.";
} else {
board[row][col] = 'x';
return "Nothing was hit on this coordinate...";
}
} else {
return "No such coordinate.";
}
}
Here is the full code... can someone please point out what I'm missing?
//server = player 1
//client = player 2
public class BattleshipGame {
public static ArrayList<Ship> fleet;
public static InetAddress clientAddress;
public static BattleshipGame battleship;
private final static int gridSize = 11;
public final static int numberOfShips = 3;
public static char[][] serverGrid;
public static char[][] clientGrid;
public static char[][] serverDamage;
public static char[][] clientDamage;
private int whosTurn;
private int whoStarts;
public static int count;
public static int bombCount;
public BattleshipGame() {
whoStarts = 0;
start();
showShipList();
}
public static String printBoard(char[][] board) {
String out = "";
for (int i = 1; i < board.length; i++) {
for (int j = 1; j < board.length; j++) {
out += board[j][i];
}
out += '\n';
}
return out;
}
public void start() {
Random rand = new Random();
if (rand.nextBoolean()) {
whoStarts = 1;
whosTurn = 1;
} else {
whoStarts = 2;
whosTurn = 2;
}
whoStarts = 1;
whosTurn = 1;
System.out.println("Player " + whoStarts + " starts\n");
}
public void initializeGrid(int playerNo) {
serverGrid = new char[gridSize][gridSize];
for (int i = 0; i < serverGrid.length; i++) {
for (int j = 0; j < serverGrid.length; j++) {
serverGrid[i][j] = '~';
}
}
serverDamage = new char[gridSize][gridSize];
for (int i = 0; i < serverDamage.length; i++) {
for (int j = 0; j < serverDamage.length; j++) {
serverDamage[i][j] = '~';
}
}
clientGrid = new char[gridSize][gridSize];
for (int i = 0; i < clientGrid.length; i++) {
for (int j = 0; j < clientGrid.length; j++) {
clientGrid[i][j] = '~';
}
}
clientDamage = new char[gridSize][gridSize];
for (int i = 0; i < clientDamage.length; i++) {
for (int j = 0; j < clientDamage.length; j++) {
clientDamage[i][j] = '~';
}
}
if (playerNo == 1) {
System.out.println("\nYour grid (player 1):\n"
+ printBoard(serverGrid));
} else if (playerNo == 2) {
System.out.println("\nYour grid (player 2):\n"
+ printBoard(clientGrid));
} else {
System.out.println("No such player.");
}
}
public static void deployShip(char[][] board, Ship ship, char direction,
int x, int y) {
if (direction == 'H') {
if (x < gridSize && y < gridSize) {
for (int i = 0; i < ship.getSize(); i++) {
board[x + i][y] = '+';
}
System.out
.println("Ship has been placed horizontally on coordinates "
+ x + ", " + y + ".");
} else {
System.out.println("Unable to place ship in this coordinate.");
}
} else if (direction == 'V') {
if (x < gridSize && y < gridSize) {
for (int i = 0; i < ship.getSize(); i++) {
board[x][y + i] = '+';
}
System.out
.println("Ship has been placed vertically on coordinates "
+ x + ", " + y + ".");
} else {
System.out.println("Unable to place ship in this coordinate.");
}
}
}
public static String dropBomb(int row, int col, char[][] board) {
if (row < gridSize && col < gridSize) {
if (board[row][col] == '+') {
System.out.println(board[row][col]);
board[row][col] = 'x';
bombCount++;
return "Ship has been hit!";
}
if (board[row][col] == 'x') {
System.out.println(board[row][col]);
bombCount++;
return "Already bombed.";
}
if (board[row][col] == '~') {
System.out.println(board[row][col]);
board[row][col] = 'x';
bombCount++;
return "Nothing was hit on this coordinate...";
}
} else {
return "No such coordinate.";
}
return "";
}
public void showShipList() {
System.out
.println("Choose ships to add to your fleet from the following list ("
+ numberOfShips + " ships allowed):");
ArrayList<Ship> overview = new ArrayList<Ship>();
Ship ac = new Ship("Aircraft carrier", 5, false);
Ship bs = new Ship("Battleship", 4, false);
Ship sm = new Ship("Submarine", 3, false);
Ship ds = new Ship("Destroyer", 3, false);
Ship sp = new Ship("Patrol Boat", 2, false);
overview.add(ac);
overview.add(bs);
overview.add(sm);
overview.add(ds);
overview.add(sp);
for (int i = 0; i < overview.size(); i++) {
System.out.println(i + 1 + ". " + overview.get(i));
}
}
public static ArrayList<Ship> createFleet(ArrayList<Ship> fleet, int choice) {
if (count < numberOfShips && choice > 0 && choice < 6) {
if (choice == 1) {
Ship ac = new Ship("Aircraft carrier", 5, false);
fleet.add(ac);
count++;
System.out.println("Aircraft carrier has been added to fleet.");
} else if (choice == 2) {
Ship bs = new Ship("Battleship", 4, false);
fleet.add(bs);
count++;
System.out.println("Battleship has been added to fleet.");
} else if (choice == 3) {
Ship sm = new Ship("Submarine", 3, false);
fleet.add(sm);
count++;
System.out.println("Submarine has been added to fleet.");
} else if (choice == 4) {
Ship ds = new Ship("Destroyer", 3, false);
fleet.add(ds);
count++;
System.out.println("Destroyer has been added to fleet.");
} else if (choice == 5) {
Ship sp = new Ship("Patrol Boat", 2, false);
fleet.add(sp);
count++;
System.out.println("Patrol boat has been added to fleet.");
}
} else {
System.out.println("Not an option.");
}
System.out.println("\nYour fleet now contains:");
for (Ship s : fleet) {
System.out.println(s);
}
return fleet;
}
}
public class TCPServer extends BattleshipGame {
private static final int playerNo = 1;
public static void main(String[] args) {
System.out.println("You are player: " + playerNo);
battleship = new BattleshipGame();
try {
InetAddress.getLocalHost().getHostAddress();
ServerSocket serverSocket = new ServerSocket(6780);
Socket socket = serverSocket.accept();
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(System.in));
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
Scanner scan = new Scanner(System.in);
while (true) {
if (inFromServer.ready()) {
setup(scan, playerNo);
System.out.println("\nInput coordinate");
int x = scan.nextInt();
int y = scan.nextInt();
System.out.println(dropBomb(x, y, clientGrid));
System.out.println(printBoard(clientGrid));
dropBomb(x, y, clientDamage);
outToClient.writeChars("Enemy player has targeted " + x + ", " + y +'\n');
printBoard(clientDamage);
}
if (inFromClient.ready()) {
System.out.println(inFromClient.readLine());
System.out.println(printBoard(serverGrid));
}
}
// socket.close();
// serverSocet.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void setup(Scanner inFromUser, int playerNo) {
fleet = new ArrayList<Ship>();
while (count < numberOfShips) {
createFleet(fleet, inFromUser.nextInt());
}
battleship.initializeGrid(playerNo);
for (int i = 0; i < fleet.size(); i++) {
System.out.println(fleet.get(i));
System.out.println("Define direction (V/H) plus coordinates");
deployShip(serverGrid, fleet.get(i), inFromUser.next().charAt(0), inFromUser.nextInt(), inFromUser.nextInt());
}
System.out.println("Placements:\n" + printBoard(serverGrid));
System.out.println("Fleet has been deployed. Press enter to continue.\n");
}
}
public class TCPClient extends BattleshipGame {
private static final int playerNo = 2;
public static void main(String[] args) {
System.out.println("You are player: " + playerNo);
battleship = new BattleshipGame();
try {
InetAddress.getLocalHost().getHostAddress();
Socket socket = new Socket(InetAddress.getLocalHost()
.getHostAddress(), 6780);
DataOutputStream dataOutputStream = new DataOutputStream(
socket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(System.in));
Scanner scan = new Scanner(System.in);
setup(scan, playerNo);
while (true) {
if (inFromClient.ready()) {
System.out.println("\nInput coordinate");
int x = scan.nextInt();
int y = scan.nextInt();
System.out.println(dropBomb(x, y, serverGrid));
dropBomb(x, y, serverDamage);
dataOutputStream.writeChars("Enemy player has targeted " + x + ", " + y +'\n');
System.out.println(printBoard(serverDamage));
}
if (inFromServer.ready()) { // modtag data fra server
System.out.println(inFromServer.readLine());
System.out.println(printBoard(clientGrid));
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void setup(Scanner inFromUser, int playerNo) {
fleet = new ArrayList<Ship>();
while (count < numberOfShips) {
createFleet(fleet, inFromUser.nextInt());
}
battleship.initializeGrid(playerNo);
for (int i = 0; i < fleet.size(); i++) {
System.out.println(fleet.get(i));
System.out.println("Define direction (V/H) plus coordinates");
deployShip(clientGrid, fleet.get(i), inFromUser.next().charAt(0), inFromUser.nextInt(), inFromUser.nextInt());
}
System.out.println("Placements:\n" + printBoard(clientGrid));
System.out.println("Fleet has been deployed. Press enter to continue.\n");
}
}
package Battleships;
public class Ship {
private String name;
private int size;
private boolean isDestroyed;
public Ship(String n, int s, boolean d) {
this.name = n;
this.size = s;
this.isDestroyed = d;
}
public String getName() {
String output = "";
output = "[" + name + "]";
return output;
}
public int getSize() {
return size;
}
public String toString() {
String output = "";
output = "[" + name + ", " + size + ", " + isDestroyed + "]";
return output;
}
}