snippetjavaMinor
Create player according to chosen difficulty
Viewed 0 times
createplayerchosendifficultyaccording
Problem
I am coding a 2D board game. I let the user choose the difficulty level in the beginning, which influences the skills points of the player.
Here is the code I wrote for the Player class:
```
package model;
public class Player {
// VARIABLES ---------------------------
private Position position;
private int score = 0;
private int stepsLeft;
private int fightingSkill;
private int jokingSkill;
private int visionScope = 2;
private String skillChoice;
// CONSTRUCTOR --------------------------
public Player(Position position, int difficultyLevel) {
this.position = position;
switch(difficultyLevel) {
case 1:
this.stepsLeft = 150;
this.fightingSkill = 5;
this.jokingSkill = 5;
case 2:
this.stepsLeft = 150;
this.fightingSkill = 2;
this.jokingSkill = 2;
case 3:
this.stepsLeft = 100;
this.fightingSkill = 2;
this.jokingSkill = 2;
case 4:
this.stepsLeft = 10;
this.fightingSkill = 1;
this.jokingSkill = 1;
}
}
public Player(int stepsLeft, int fightingSkill, int jokingSkill) {
this.stepsLeft = stepsLeft;
this.fightingSkill = fightingSkill;
this.jokingSkill = jokingSkill;
this.position = new Position(1,1);
this.score = 0;
}
// METHODS ------------------------------
public void move(Position destination) {
setPosition(destination);
stepsLeft -= 1;
}
public void increaseScore(int bonus) {
score += bonus;
}
public void increaseStepsLeft(int bonus) {
stepsLeft+= bonus;
}
public void increaseFightingSkill(int bonus) {
fightingSkill += bonus;
}
public void increaseJokingSkill(int bonus) {
jokingSkill += bonus;
}
// GETTERS
public Position getPosition() {
return
Here is the code I wrote for the Player class:
```
package model;
public class Player {
// VARIABLES ---------------------------
private Position position;
private int score = 0;
private int stepsLeft;
private int fightingSkill;
private int jokingSkill;
private int visionScope = 2;
private String skillChoice;
// CONSTRUCTOR --------------------------
public Player(Position position, int difficultyLevel) {
this.position = position;
switch(difficultyLevel) {
case 1:
this.stepsLeft = 150;
this.fightingSkill = 5;
this.jokingSkill = 5;
case 2:
this.stepsLeft = 150;
this.fightingSkill = 2;
this.jokingSkill = 2;
case 3:
this.stepsLeft = 100;
this.fightingSkill = 2;
this.jokingSkill = 2;
case 4:
this.stepsLeft = 10;
this.fightingSkill = 1;
this.jokingSkill = 1;
}
}
public Player(int stepsLeft, int fightingSkill, int jokingSkill) {
this.stepsLeft = stepsLeft;
this.fightingSkill = fightingSkill;
this.jokingSkill = jokingSkill;
this.position = new Position(1,1);
this.score = 0;
}
// METHODS ------------------------------
public void move(Position destination) {
setPosition(destination);
stepsLeft -= 1;
}
public void increaseScore(int bonus) {
score += bonus;
}
public void increaseStepsLeft(int bonus) {
stepsLeft+= bonus;
}
public void increaseFightingSkill(int bonus) {
fightingSkill += bonus;
}
public void increaseJokingSkill(int bonus) {
jokingSkill += bonus;
}
// GETTERS
public Position getPosition() {
return
Solution
There are several things that can be improved, in my opinion:
-
Whitespaces around operators make the code more readable (you use them almost everywhere except this method):
-
Comments like
-
An alternative for using
It makes the code more readable in my opinion because all "magic" numbers are located in one place (and the code becomes more concise).
-
There is no need to use
-
Whitespaces around operators make the code more readable (you use them almost everywhere except this method):
public String toString() {
return "play \t" + position + "\t" + stepsLeft + "\t"
+ jokingSkill + " \t" + score + "\n";
}-
Comments like
// VARIABLES ---------- or // METHODS ---------- are useless (they do not contain any additional information). Adding comments for each individual method explaining what it does (especially for public ones) or what a field is used for is a good practice. -
An alternative for using
switch statement is creating an array of constants. It can look like this:private static int[] stepsLeftForLevel = new int[]{150, 150, 100, 10};
...
public Player(Position position, int difficultyLevel) {
...
stepsLeft = stepsLeftForLevel[difficultyLevel];
...
}It makes the code more readable in my opinion because all "magic" numbers are located in one place (and the code becomes more concise).
-
There is no need to use
this in a constructor unless the name of the field is shadowed by a parameter (that is, I would recommend using this only when it is necessary).Code Snippets
public String toString() {
return "play \t" + position + "\t" + stepsLeft + "\t"
+ jokingSkill + " \t" + score + "\n";
}private static int[] stepsLeftForLevel = new int[]{150, 150, 100, 10};
...
public Player(Position position, int difficultyLevel) {
...
stepsLeft = stepsLeftForLevel[difficultyLevel];
...
}Context
StackExchange Code Review Q#75424, answer score: 4
Revisions (0)
No revisions yet.