I'm not so sure if this is an important thing to worry about, but I want to develop good practices while I am still learning programming and, if possible, attenuate doubt. And learn a little bit of theory, too, while I am at it, I suppose.
**Currently learning Java, specifically, Classes, though this portion of the code focuses on the client.
So, currently working on a game of chance. Basically, two people begin with a certain budget of points. They each roll a die and the higher roll wins. The winner gains the difference to his or her budget and the loser gets the difference deducted from their budget. Game goes on until the budget of one player no longer has any points.
Which brings me to this point, the code. These are two variations of a solution I came up with and struggle to determine which one is 'better' so to speak.
public static int allocatePoints(int balance, int points){
balance = balance + points;
return balance;
}
-----------------------------------------------------------------------------------------
int points = Math.abs(die1.getFaceValue() - die2.getFaceValue());
if(points == 0){
System.out.println("Draw!\n");
continue;
}
if(die1.getFaceValue()>die2.getFaceValue()){
balance1 = allocatePoints(balance1, points);
balance2 = allocatePoints(balance2, -points);
} else {
balance1 = allocatePoints(balance1, -points);
balance2 = allocatePoints(balance2, points);
}
if(balance1 < 0){
balance1 = 0;
}
if(balance2 < 0){
balance2 = 0;
}
=========================================================================================
=========================================================================================
int difference = Math.abs(die1.getFaceValue() - die2.getFaceValue());
if(die1.getFaceValue()>die2.getFaceValue()){
balance1 = balance1 + difference;
balance2 = balance2 - difference;
} else if(die2.getFaceValue()>die1.getFaceValue()){
balance1 = balance1 - difference;
balance2 = balance2 + difference;
} else {
System.out.println("Draw!");
}
if(balance1 < 0){
balance1 = 0;
}
if(balance2 < 0) {
balance2 = 0;
}
It seems more practical to me that the second piece of code would be a more reasonable choice, since it is straightfoward and seems pointless to do it the other way-- considering all I'm doing is negating a value. Though, that is where I begin to get confused.