Basketball game
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
I have many lines of code that - to me - look very odd, and I would like to condense, but I don't exactly know how.
This is the part I dislike the most:
if(random.nextInt(50) == 26)
int rnteam = random.nextInt(twoteams.size());
String rndteam = "";
if(rnteam == 0) rndteam = team1.getName();
else if(rnteam == 1) rndteam = team2.getName();
String rndplayfrndteam = "";
int rndplayer = random.nextInt(5);
if(rndteam == team1.getName()) rndplayfrndteam = team1.getPlayers().get(rndplayer);
else if(rndteam == team2.getName()) rndplayfrndteam = team2.getPlayers().get(rndplayer);
// Determining this player's score
// Team 1
if(rndplayfrndteam == team1.getPlayers().get(0)) t1p1score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(1)) t1p2score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(2)) t1p3score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(3)) t1p4score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(4)) t1p5score += scoreAmount;
// Team 2
if(rndplayfrndteam == team2.getPlayers().get(0)) t2p1score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(1)) t2p2score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(2)) t2p3score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(3)) t2p4score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(4)) t2p5score += scoreAmount;
if(rnteam == 0)
t1sofar += scoreAmount;
else
t2sofar += scoreAmount;
int t1scores = t1p1score, t1p2score, t1p3score, t1p4score, t1p5score;
int t1maxScore = findGreatestInt(t1scores);
int t1bestPlayer = 0;
if(t1maxScore == t1p1score) t1bestPlayer = 1;
if(t1maxScore == t1p2score) t1bestPlayer = 2;
if(t1maxScore == t1p3score) t1bestPlayer = 3;
if(t1maxScore == t1p4score) t1bestPlayer = 4;
if(t1maxScore == t1p5score) t1bestPlayer = 5;
int t2scores = t2p1score, t2p2score, t2p3score, t2p4score, t2p5score;
int t2maxScore = findGreatestInt(t2scores);
int t2bestPlayer = 0;
if(t2maxScore == t2p1score) t2bestPlayer = 1;
if(t2maxScore == t2p2score) t2bestPlayer = 2;
if(t2maxScore == t2p3score) t2bestPlayer = 3;
if(t2maxScore == t2p4score) t2bestPlayer = 4;
if(t2maxScore == t2p5score) t2bestPlayer = 5;
hangOver(rndplayfrndteam + " scored " + scoreAmount, team1, team2, t1bestPlayer, t1maxScore, t2bestPlayer, t2maxScore, t1sofar, t2sofar);
else
int t1scores = t1p1score, t1p2score, t1p3score, t1p4score, t1p5score;
int t1maxScore = findGreatestInt(t1scores);
int t1bestPlayer = 0;
if(t1maxScore == t1p1score) t1bestPlayer = 1;
if(t1maxScore == t1p2score) t1bestPlayer = 2;
if(t1maxScore == t1p3score) t1bestPlayer = 3;
if(t1maxScore == t1p4score) t1bestPlayer = 4;
if(t1maxScore == t1p5score) t1bestPlayer = 5;
int t2scores = t2p1score, t2p2score, t2p3score, t2p4score, t2p5score;
int t2maxScore = findGreatestInt(t2scores);
int t2bestPlayer = 0;
if(t2maxScore == t2p1score) t2bestPlayer = 1;
if(t2maxScore == t2p2score) t2bestPlayer = 2;
if(t2maxScore == t2p3score) t2bestPlayer = 3;
if(t2maxScore == t2p4score) t2bestPlayer = 4;
if(t2maxScore == t2p5score) t2bestPlayer = 5;
hangOver("No one", team1, team2, t1bestPlayer, t1maxScore, t2bestPlayer, t2maxScore, t1sofar, t2sofar);
All of the if-statements combined just look bad, and aren't very efficient. Is there a way I could change these into a loop and it still work? If so, how?
java game
add a comment |Â
up vote
3
down vote
favorite
I have many lines of code that - to me - look very odd, and I would like to condense, but I don't exactly know how.
This is the part I dislike the most:
if(random.nextInt(50) == 26)
int rnteam = random.nextInt(twoteams.size());
String rndteam = "";
if(rnteam == 0) rndteam = team1.getName();
else if(rnteam == 1) rndteam = team2.getName();
String rndplayfrndteam = "";
int rndplayer = random.nextInt(5);
if(rndteam == team1.getName()) rndplayfrndteam = team1.getPlayers().get(rndplayer);
else if(rndteam == team2.getName()) rndplayfrndteam = team2.getPlayers().get(rndplayer);
// Determining this player's score
// Team 1
if(rndplayfrndteam == team1.getPlayers().get(0)) t1p1score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(1)) t1p2score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(2)) t1p3score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(3)) t1p4score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(4)) t1p5score += scoreAmount;
// Team 2
if(rndplayfrndteam == team2.getPlayers().get(0)) t2p1score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(1)) t2p2score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(2)) t2p3score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(3)) t2p4score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(4)) t2p5score += scoreAmount;
if(rnteam == 0)
t1sofar += scoreAmount;
else
t2sofar += scoreAmount;
int t1scores = t1p1score, t1p2score, t1p3score, t1p4score, t1p5score;
int t1maxScore = findGreatestInt(t1scores);
int t1bestPlayer = 0;
if(t1maxScore == t1p1score) t1bestPlayer = 1;
if(t1maxScore == t1p2score) t1bestPlayer = 2;
if(t1maxScore == t1p3score) t1bestPlayer = 3;
if(t1maxScore == t1p4score) t1bestPlayer = 4;
if(t1maxScore == t1p5score) t1bestPlayer = 5;
int t2scores = t2p1score, t2p2score, t2p3score, t2p4score, t2p5score;
int t2maxScore = findGreatestInt(t2scores);
int t2bestPlayer = 0;
if(t2maxScore == t2p1score) t2bestPlayer = 1;
if(t2maxScore == t2p2score) t2bestPlayer = 2;
if(t2maxScore == t2p3score) t2bestPlayer = 3;
if(t2maxScore == t2p4score) t2bestPlayer = 4;
if(t2maxScore == t2p5score) t2bestPlayer = 5;
hangOver(rndplayfrndteam + " scored " + scoreAmount, team1, team2, t1bestPlayer, t1maxScore, t2bestPlayer, t2maxScore, t1sofar, t2sofar);
else
int t1scores = t1p1score, t1p2score, t1p3score, t1p4score, t1p5score;
int t1maxScore = findGreatestInt(t1scores);
int t1bestPlayer = 0;
if(t1maxScore == t1p1score) t1bestPlayer = 1;
if(t1maxScore == t1p2score) t1bestPlayer = 2;
if(t1maxScore == t1p3score) t1bestPlayer = 3;
if(t1maxScore == t1p4score) t1bestPlayer = 4;
if(t1maxScore == t1p5score) t1bestPlayer = 5;
int t2scores = t2p1score, t2p2score, t2p3score, t2p4score, t2p5score;
int t2maxScore = findGreatestInt(t2scores);
int t2bestPlayer = 0;
if(t2maxScore == t2p1score) t2bestPlayer = 1;
if(t2maxScore == t2p2score) t2bestPlayer = 2;
if(t2maxScore == t2p3score) t2bestPlayer = 3;
if(t2maxScore == t2p4score) t2bestPlayer = 4;
if(t2maxScore == t2p5score) t2bestPlayer = 5;
hangOver("No one", team1, team2, t1bestPlayer, t1maxScore, t2bestPlayer, t2maxScore, t1sofar, t2sofar);
All of the if-statements combined just look bad, and aren't very efficient. Is there a way I could change these into a loop and it still work? If so, how?
java game
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have many lines of code that - to me - look very odd, and I would like to condense, but I don't exactly know how.
This is the part I dislike the most:
if(random.nextInt(50) == 26)
int rnteam = random.nextInt(twoteams.size());
String rndteam = "";
if(rnteam == 0) rndteam = team1.getName();
else if(rnteam == 1) rndteam = team2.getName();
String rndplayfrndteam = "";
int rndplayer = random.nextInt(5);
if(rndteam == team1.getName()) rndplayfrndteam = team1.getPlayers().get(rndplayer);
else if(rndteam == team2.getName()) rndplayfrndteam = team2.getPlayers().get(rndplayer);
// Determining this player's score
// Team 1
if(rndplayfrndteam == team1.getPlayers().get(0)) t1p1score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(1)) t1p2score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(2)) t1p3score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(3)) t1p4score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(4)) t1p5score += scoreAmount;
// Team 2
if(rndplayfrndteam == team2.getPlayers().get(0)) t2p1score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(1)) t2p2score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(2)) t2p3score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(3)) t2p4score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(4)) t2p5score += scoreAmount;
if(rnteam == 0)
t1sofar += scoreAmount;
else
t2sofar += scoreAmount;
int t1scores = t1p1score, t1p2score, t1p3score, t1p4score, t1p5score;
int t1maxScore = findGreatestInt(t1scores);
int t1bestPlayer = 0;
if(t1maxScore == t1p1score) t1bestPlayer = 1;
if(t1maxScore == t1p2score) t1bestPlayer = 2;
if(t1maxScore == t1p3score) t1bestPlayer = 3;
if(t1maxScore == t1p4score) t1bestPlayer = 4;
if(t1maxScore == t1p5score) t1bestPlayer = 5;
int t2scores = t2p1score, t2p2score, t2p3score, t2p4score, t2p5score;
int t2maxScore = findGreatestInt(t2scores);
int t2bestPlayer = 0;
if(t2maxScore == t2p1score) t2bestPlayer = 1;
if(t2maxScore == t2p2score) t2bestPlayer = 2;
if(t2maxScore == t2p3score) t2bestPlayer = 3;
if(t2maxScore == t2p4score) t2bestPlayer = 4;
if(t2maxScore == t2p5score) t2bestPlayer = 5;
hangOver(rndplayfrndteam + " scored " + scoreAmount, team1, team2, t1bestPlayer, t1maxScore, t2bestPlayer, t2maxScore, t1sofar, t2sofar);
else
int t1scores = t1p1score, t1p2score, t1p3score, t1p4score, t1p5score;
int t1maxScore = findGreatestInt(t1scores);
int t1bestPlayer = 0;
if(t1maxScore == t1p1score) t1bestPlayer = 1;
if(t1maxScore == t1p2score) t1bestPlayer = 2;
if(t1maxScore == t1p3score) t1bestPlayer = 3;
if(t1maxScore == t1p4score) t1bestPlayer = 4;
if(t1maxScore == t1p5score) t1bestPlayer = 5;
int t2scores = t2p1score, t2p2score, t2p3score, t2p4score, t2p5score;
int t2maxScore = findGreatestInt(t2scores);
int t2bestPlayer = 0;
if(t2maxScore == t2p1score) t2bestPlayer = 1;
if(t2maxScore == t2p2score) t2bestPlayer = 2;
if(t2maxScore == t2p3score) t2bestPlayer = 3;
if(t2maxScore == t2p4score) t2bestPlayer = 4;
if(t2maxScore == t2p5score) t2bestPlayer = 5;
hangOver("No one", team1, team2, t1bestPlayer, t1maxScore, t2bestPlayer, t2maxScore, t1sofar, t2sofar);
All of the if-statements combined just look bad, and aren't very efficient. Is there a way I could change these into a loop and it still work? If so, how?
java game
I have many lines of code that - to me - look very odd, and I would like to condense, but I don't exactly know how.
This is the part I dislike the most:
if(random.nextInt(50) == 26)
int rnteam = random.nextInt(twoteams.size());
String rndteam = "";
if(rnteam == 0) rndteam = team1.getName();
else if(rnteam == 1) rndteam = team2.getName();
String rndplayfrndteam = "";
int rndplayer = random.nextInt(5);
if(rndteam == team1.getName()) rndplayfrndteam = team1.getPlayers().get(rndplayer);
else if(rndteam == team2.getName()) rndplayfrndteam = team2.getPlayers().get(rndplayer);
// Determining this player's score
// Team 1
if(rndplayfrndteam == team1.getPlayers().get(0)) t1p1score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(1)) t1p2score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(2)) t1p3score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(3)) t1p4score += scoreAmount;
if(rndplayfrndteam == team1.getPlayers().get(4)) t1p5score += scoreAmount;
// Team 2
if(rndplayfrndteam == team2.getPlayers().get(0)) t2p1score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(1)) t2p2score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(2)) t2p3score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(3)) t2p4score += scoreAmount;
if(rndplayfrndteam == team2.getPlayers().get(4)) t2p5score += scoreAmount;
if(rnteam == 0)
t1sofar += scoreAmount;
else
t2sofar += scoreAmount;
int t1scores = t1p1score, t1p2score, t1p3score, t1p4score, t1p5score;
int t1maxScore = findGreatestInt(t1scores);
int t1bestPlayer = 0;
if(t1maxScore == t1p1score) t1bestPlayer = 1;
if(t1maxScore == t1p2score) t1bestPlayer = 2;
if(t1maxScore == t1p3score) t1bestPlayer = 3;
if(t1maxScore == t1p4score) t1bestPlayer = 4;
if(t1maxScore == t1p5score) t1bestPlayer = 5;
int t2scores = t2p1score, t2p2score, t2p3score, t2p4score, t2p5score;
int t2maxScore = findGreatestInt(t2scores);
int t2bestPlayer = 0;
if(t2maxScore == t2p1score) t2bestPlayer = 1;
if(t2maxScore == t2p2score) t2bestPlayer = 2;
if(t2maxScore == t2p3score) t2bestPlayer = 3;
if(t2maxScore == t2p4score) t2bestPlayer = 4;
if(t2maxScore == t2p5score) t2bestPlayer = 5;
hangOver(rndplayfrndteam + " scored " + scoreAmount, team1, team2, t1bestPlayer, t1maxScore, t2bestPlayer, t2maxScore, t1sofar, t2sofar);
else
int t1scores = t1p1score, t1p2score, t1p3score, t1p4score, t1p5score;
int t1maxScore = findGreatestInt(t1scores);
int t1bestPlayer = 0;
if(t1maxScore == t1p1score) t1bestPlayer = 1;
if(t1maxScore == t1p2score) t1bestPlayer = 2;
if(t1maxScore == t1p3score) t1bestPlayer = 3;
if(t1maxScore == t1p4score) t1bestPlayer = 4;
if(t1maxScore == t1p5score) t1bestPlayer = 5;
int t2scores = t2p1score, t2p2score, t2p3score, t2p4score, t2p5score;
int t2maxScore = findGreatestInt(t2scores);
int t2bestPlayer = 0;
if(t2maxScore == t2p1score) t2bestPlayer = 1;
if(t2maxScore == t2p2score) t2bestPlayer = 2;
if(t2maxScore == t2p3score) t2bestPlayer = 3;
if(t2maxScore == t2p4score) t2bestPlayer = 4;
if(t2maxScore == t2p5score) t2bestPlayer = 5;
hangOver("No one", team1, team2, t1bestPlayer, t1maxScore, t2bestPlayer, t2maxScore, t1sofar, t2sofar);
All of the if-statements combined just look bad, and aren't very efficient. Is there a way I could change these into a loop and it still work? If so, how?
java game
asked Jan 28 at 13:56
The Magician
477
477
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Here are my comments in order of severity:
1) BUGS
Test equality of Strings using
equals()
not==
use of
else if
let's examine this piece of codeString rndteam = "";
if(rnteam == 0) rndteam = team1.getName();
else if(rnteam == 1) rndteam = team2.getName();
it assigns a value to the String rndteam
. now, I assume that the logic was meant that rndteam
should be assigned the name of either team1
or team2
but the code actually allows for both if
statements to be false and the value will then be an empty String. You can fix that by changing the flow to an if else
one
String rndteam = "";
if(rnteam == 0) rndteam = team1.getName();
else rndteam = team2.getName();
in my eyes, in the case of deciding between two options, the short form of the if statement can be used to make it clear:
String rndteam = rnteam == 0 ? team1.getName() : team2.getName();
2) Design problems
Now we come to the question of all those
if
statements. You already have arrays that you assign the variables into so why not use them when you ask about the team players?int t1scores = new int[team1.size()];
for (int i = 0 ; i < team1.size(); i++)
if(rndplayfrndteam.equals(team1.getPlayers().get(i))) t1scores[i] += scoreAmount;
But really, as it was already suggested, you should design a class that holds all the information of a Player and another for team.
Why are you sayingteam1.size()
? The team class has no size() method. JustgetPlayers(),
getName(),
andgetWin/LosAm
as well assetWin/LosAm
â The Magician
Jan 29 at 13:01
probably meantteam1.getPlayers().size()
â Sharon Ben Asher
Jan 29 at 13:41
add a comment |Â
up vote
2
down vote
Once you have a number of variables with similar names, such as t?p?score
, you should consider changing these to arrays (a 2D one would make sense in your case), which you can then iterate over in a loop to reduce the repetition.
What is so special about the if(random.nextInt(50) == 26)
condition? Some constants would be useful here.
Try to avoid removing letters from variable names just to shorten them, lest they lose their meaning. I can't work out what rndplayfrndteam
means.
A method called hangOver
tells me absolutely nothing about what it does, and you should consider a different name.
You may wish to consider some data classes to keep the players and their scores together, possibly including their teams as well, to keep things a bit cleaner.
BTW:rndplayfrndteam
is random player from random team.if(random.nextInt(50) == 26)
is how I check: "score? or not?" I had it set toif(random.nextInt(50) == random.nextInt(50))
â The Magician
Jan 29 at 12:47
This would have been clearer:boolean didScore = random.nextInt(50) == 26; if (didScore) ... else ...
â Joe C
Jan 29 at 20:42
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Here are my comments in order of severity:
1) BUGS
Test equality of Strings using
equals()
not==
use of
else if
let's examine this piece of codeString rndteam = "";
if(rnteam == 0) rndteam = team1.getName();
else if(rnteam == 1) rndteam = team2.getName();
it assigns a value to the String rndteam
. now, I assume that the logic was meant that rndteam
should be assigned the name of either team1
or team2
but the code actually allows for both if
statements to be false and the value will then be an empty String. You can fix that by changing the flow to an if else
one
String rndteam = "";
if(rnteam == 0) rndteam = team1.getName();
else rndteam = team2.getName();
in my eyes, in the case of deciding between two options, the short form of the if statement can be used to make it clear:
String rndteam = rnteam == 0 ? team1.getName() : team2.getName();
2) Design problems
Now we come to the question of all those
if
statements. You already have arrays that you assign the variables into so why not use them when you ask about the team players?int t1scores = new int[team1.size()];
for (int i = 0 ; i < team1.size(); i++)
if(rndplayfrndteam.equals(team1.getPlayers().get(i))) t1scores[i] += scoreAmount;
But really, as it was already suggested, you should design a class that holds all the information of a Player and another for team.
Why are you sayingteam1.size()
? The team class has no size() method. JustgetPlayers(),
getName(),
andgetWin/LosAm
as well assetWin/LosAm
â The Magician
Jan 29 at 13:01
probably meantteam1.getPlayers().size()
â Sharon Ben Asher
Jan 29 at 13:41
add a comment |Â
up vote
2
down vote
accepted
Here are my comments in order of severity:
1) BUGS
Test equality of Strings using
equals()
not==
use of
else if
let's examine this piece of codeString rndteam = "";
if(rnteam == 0) rndteam = team1.getName();
else if(rnteam == 1) rndteam = team2.getName();
it assigns a value to the String rndteam
. now, I assume that the logic was meant that rndteam
should be assigned the name of either team1
or team2
but the code actually allows for both if
statements to be false and the value will then be an empty String. You can fix that by changing the flow to an if else
one
String rndteam = "";
if(rnteam == 0) rndteam = team1.getName();
else rndteam = team2.getName();
in my eyes, in the case of deciding between two options, the short form of the if statement can be used to make it clear:
String rndteam = rnteam == 0 ? team1.getName() : team2.getName();
2) Design problems
Now we come to the question of all those
if
statements. You already have arrays that you assign the variables into so why not use them when you ask about the team players?int t1scores = new int[team1.size()];
for (int i = 0 ; i < team1.size(); i++)
if(rndplayfrndteam.equals(team1.getPlayers().get(i))) t1scores[i] += scoreAmount;
But really, as it was already suggested, you should design a class that holds all the information of a Player and another for team.
Why are you sayingteam1.size()
? The team class has no size() method. JustgetPlayers(),
getName(),
andgetWin/LosAm
as well assetWin/LosAm
â The Magician
Jan 29 at 13:01
probably meantteam1.getPlayers().size()
â Sharon Ben Asher
Jan 29 at 13:41
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Here are my comments in order of severity:
1) BUGS
Test equality of Strings using
equals()
not==
use of
else if
let's examine this piece of codeString rndteam = "";
if(rnteam == 0) rndteam = team1.getName();
else if(rnteam == 1) rndteam = team2.getName();
it assigns a value to the String rndteam
. now, I assume that the logic was meant that rndteam
should be assigned the name of either team1
or team2
but the code actually allows for both if
statements to be false and the value will then be an empty String. You can fix that by changing the flow to an if else
one
String rndteam = "";
if(rnteam == 0) rndteam = team1.getName();
else rndteam = team2.getName();
in my eyes, in the case of deciding between two options, the short form of the if statement can be used to make it clear:
String rndteam = rnteam == 0 ? team1.getName() : team2.getName();
2) Design problems
Now we come to the question of all those
if
statements. You already have arrays that you assign the variables into so why not use them when you ask about the team players?int t1scores = new int[team1.size()];
for (int i = 0 ; i < team1.size(); i++)
if(rndplayfrndteam.equals(team1.getPlayers().get(i))) t1scores[i] += scoreAmount;
But really, as it was already suggested, you should design a class that holds all the information of a Player and another for team.
Here are my comments in order of severity:
1) BUGS
Test equality of Strings using
equals()
not==
use of
else if
let's examine this piece of codeString rndteam = "";
if(rnteam == 0) rndteam = team1.getName();
else if(rnteam == 1) rndteam = team2.getName();
it assigns a value to the String rndteam
. now, I assume that the logic was meant that rndteam
should be assigned the name of either team1
or team2
but the code actually allows for both if
statements to be false and the value will then be an empty String. You can fix that by changing the flow to an if else
one
String rndteam = "";
if(rnteam == 0) rndteam = team1.getName();
else rndteam = team2.getName();
in my eyes, in the case of deciding between two options, the short form of the if statement can be used to make it clear:
String rndteam = rnteam == 0 ? team1.getName() : team2.getName();
2) Design problems
Now we come to the question of all those
if
statements. You already have arrays that you assign the variables into so why not use them when you ask about the team players?int t1scores = new int[team1.size()];
for (int i = 0 ; i < team1.size(); i++)
if(rndplayfrndteam.equals(team1.getPlayers().get(i))) t1scores[i] += scoreAmount;
But really, as it was already suggested, you should design a class that holds all the information of a Player and another for team.
answered Jan 28 at 15:35
Sharon Ben Asher
2,073512
2,073512
Why are you sayingteam1.size()
? The team class has no size() method. JustgetPlayers(),
getName(),
andgetWin/LosAm
as well assetWin/LosAm
â The Magician
Jan 29 at 13:01
probably meantteam1.getPlayers().size()
â Sharon Ben Asher
Jan 29 at 13:41
add a comment |Â
Why are you sayingteam1.size()
? The team class has no size() method. JustgetPlayers(),
getName(),
andgetWin/LosAm
as well assetWin/LosAm
â The Magician
Jan 29 at 13:01
probably meantteam1.getPlayers().size()
â Sharon Ben Asher
Jan 29 at 13:41
Why are you saying
team1.size()
? The team class has no size() method. Just getPlayers(),
getName(),
and getWin/LosAm
as well as setWin/LosAm
â The Magician
Jan 29 at 13:01
Why are you saying
team1.size()
? The team class has no size() method. Just getPlayers(),
getName(),
and getWin/LosAm
as well as setWin/LosAm
â The Magician
Jan 29 at 13:01
probably meant
team1.getPlayers().size()
â Sharon Ben Asher
Jan 29 at 13:41
probably meant
team1.getPlayers().size()
â Sharon Ben Asher
Jan 29 at 13:41
add a comment |Â
up vote
2
down vote
Once you have a number of variables with similar names, such as t?p?score
, you should consider changing these to arrays (a 2D one would make sense in your case), which you can then iterate over in a loop to reduce the repetition.
What is so special about the if(random.nextInt(50) == 26)
condition? Some constants would be useful here.
Try to avoid removing letters from variable names just to shorten them, lest they lose their meaning. I can't work out what rndplayfrndteam
means.
A method called hangOver
tells me absolutely nothing about what it does, and you should consider a different name.
You may wish to consider some data classes to keep the players and their scores together, possibly including their teams as well, to keep things a bit cleaner.
BTW:rndplayfrndteam
is random player from random team.if(random.nextInt(50) == 26)
is how I check: "score? or not?" I had it set toif(random.nextInt(50) == random.nextInt(50))
â The Magician
Jan 29 at 12:47
This would have been clearer:boolean didScore = random.nextInt(50) == 26; if (didScore) ... else ...
â Joe C
Jan 29 at 20:42
add a comment |Â
up vote
2
down vote
Once you have a number of variables with similar names, such as t?p?score
, you should consider changing these to arrays (a 2D one would make sense in your case), which you can then iterate over in a loop to reduce the repetition.
What is so special about the if(random.nextInt(50) == 26)
condition? Some constants would be useful here.
Try to avoid removing letters from variable names just to shorten them, lest they lose their meaning. I can't work out what rndplayfrndteam
means.
A method called hangOver
tells me absolutely nothing about what it does, and you should consider a different name.
You may wish to consider some data classes to keep the players and their scores together, possibly including their teams as well, to keep things a bit cleaner.
BTW:rndplayfrndteam
is random player from random team.if(random.nextInt(50) == 26)
is how I check: "score? or not?" I had it set toif(random.nextInt(50) == random.nextInt(50))
â The Magician
Jan 29 at 12:47
This would have been clearer:boolean didScore = random.nextInt(50) == 26; if (didScore) ... else ...
â Joe C
Jan 29 at 20:42
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Once you have a number of variables with similar names, such as t?p?score
, you should consider changing these to arrays (a 2D one would make sense in your case), which you can then iterate over in a loop to reduce the repetition.
What is so special about the if(random.nextInt(50) == 26)
condition? Some constants would be useful here.
Try to avoid removing letters from variable names just to shorten them, lest they lose their meaning. I can't work out what rndplayfrndteam
means.
A method called hangOver
tells me absolutely nothing about what it does, and you should consider a different name.
You may wish to consider some data classes to keep the players and their scores together, possibly including their teams as well, to keep things a bit cleaner.
Once you have a number of variables with similar names, such as t?p?score
, you should consider changing these to arrays (a 2D one would make sense in your case), which you can then iterate over in a loop to reduce the repetition.
What is so special about the if(random.nextInt(50) == 26)
condition? Some constants would be useful here.
Try to avoid removing letters from variable names just to shorten them, lest they lose their meaning. I can't work out what rndplayfrndteam
means.
A method called hangOver
tells me absolutely nothing about what it does, and you should consider a different name.
You may wish to consider some data classes to keep the players and their scores together, possibly including their teams as well, to keep things a bit cleaner.
answered Jan 28 at 15:09
Joe C
58919
58919
BTW:rndplayfrndteam
is random player from random team.if(random.nextInt(50) == 26)
is how I check: "score? or not?" I had it set toif(random.nextInt(50) == random.nextInt(50))
â The Magician
Jan 29 at 12:47
This would have been clearer:boolean didScore = random.nextInt(50) == 26; if (didScore) ... else ...
â Joe C
Jan 29 at 20:42
add a comment |Â
BTW:rndplayfrndteam
is random player from random team.if(random.nextInt(50) == 26)
is how I check: "score? or not?" I had it set toif(random.nextInt(50) == random.nextInt(50))
â The Magician
Jan 29 at 12:47
This would have been clearer:boolean didScore = random.nextInt(50) == 26; if (didScore) ... else ...
â Joe C
Jan 29 at 20:42
BTW:
rndplayfrndteam
is random player from random team. if(random.nextInt(50) == 26)
is how I check: "score? or not?" I had it set to if(random.nextInt(50) == random.nextInt(50))
â The Magician
Jan 29 at 12:47
BTW:
rndplayfrndteam
is random player from random team. if(random.nextInt(50) == 26)
is how I check: "score? or not?" I had it set to if(random.nextInt(50) == random.nextInt(50))
â The Magician
Jan 29 at 12:47
This would have been clearer:
boolean didScore = random.nextInt(50) == 26; if (didScore) ... else ...
â Joe C
Jan 29 at 20:42
This would have been clearer:
boolean didScore = random.nextInt(50) == 26; if (didScore) ... else ...
â Joe C
Jan 29 at 20:42
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186195%2fbasketball-game%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password