replace public fields with encapsulation

This commit is contained in:
Ingmar Goudt 2019-03-03 09:51:14 +01:00
parent d1e319787c
commit 8319fbf9ad
26 changed files with 544 additions and 327 deletions

View file

@ -615,7 +615,6 @@ public abstract class CardImpl extends MageObjectImpl implements Card {
}
List<ExpansionSet.SetCardInfo> cardInfo = Sets.findSet(expansionSetCode).findCardInfoByClass(secondSideCardClazz);
assert cardInfo.size() == 1; // should find 1 second side card
if (cardInfo.isEmpty()) {
return null;
}

View file

@ -174,9 +174,7 @@ public class StackAbility extends StackObjImpl implements Ability {
@Override
public Abilities<Ability> getAbilities() {
Abilities<Ability> abilities = new AbilitiesImpl<>();
abilities.add(ability);
return abilities;
return new AbilitiesImpl<>(ability);
}
@Override

View file

@ -9,7 +9,6 @@ import mage.game.tournament.TournamentPlayer;
import java.util.*;
/**
*
* @author Quercitron
*/
@ -23,7 +22,7 @@ public class SwissPairingMinimalWeightMatching {
private final int playersCount;
List<PlayerInfo> swissPlayers;
private List<PlayerInfo> swissPlayers;
// number of vertexes in graph
private final int n;
@ -37,8 +36,8 @@ public class SwissPairingMinimalWeightMatching {
swissPlayers = new ArrayList<>();
for (TournamentPlayer tournamentPlayer : players) {
PlayerInfo swissPlayer = new PlayerInfo();
swissPlayer.tournamentPlayer = tournamentPlayer;
swissPlayer.points = tournamentPlayer.getPoints();
swissPlayer.setTournamentPlayer(tournamentPlayer);
swissPlayer.setPoints(tournamentPlayer.getPoints());
swissPlayers.add(swissPlayer);
}
@ -46,8 +45,8 @@ public class SwissPairingMinimalWeightMatching {
Collections.shuffle(swissPlayers);
Map<TournamentPlayer, Integer> map = new HashMap<>();
for (int i = 0; i < playersCount; i++) {
swissPlayers.get(i).id = i;
map.put(swissPlayers.get(i).tournamentPlayer, i);
swissPlayers.get(i).setId(i);
map.put(swissPlayers.get(i).getTournamentPlayer(), i);
}
// calculate Tie Breaker points -- Sum of Opponents' Scores (SOS)
@ -62,10 +61,10 @@ public class SwissPairingMinimalWeightMatching {
// a player could have left the tournament, so we should check if id is not null
if (id1 != null) {
swissPlayers.get(id1).sosPoints += player2.getPoints();
swissPlayers.get(id1).setSosPoints(swissPlayers.get(id1).getSosPoints() + player2.getPoints());
}
if (id2 != null) {
swissPlayers.get(id2).sosPoints += player1.getPoints();
swissPlayers.get(id2).setSosPoints(swissPlayers.get(id2).getSosPoints() + player1.getPoints());
}
// todo: sos points for byes? maybe add player points?
}
@ -73,18 +72,18 @@ public class SwissPairingMinimalWeightMatching {
// sort by points and then by sos points
swissPlayers.sort((p1, p2) -> {
int result = p2.points - p1.points;
int result = p2.getPoints() - p1.getPoints();
if (result != 0) {
return result;
}
return p2.sosPoints - p1.sosPoints;
return p2.getSosPoints() - p1.getSosPoints();
});
// order could be changed, update ids and mapping
map.clear();
for (int i = 0; i < playersCount; i++) {
swissPlayers.get(i).id = i;
map.put(swissPlayers.get(i).tournamentPlayer, i);
swissPlayers.get(i).setId(i);
map.put(swissPlayers.get(i).getTournamentPlayer(), i);
}
// count ties and matches between players
@ -125,18 +124,18 @@ public class SwissPairingMinimalWeightMatching {
for (int i = 0; i < playersCount; i++) {
for (int j = 0; j < i; j++) {
w[i][j] = Math.abs(i - j) +
pointsDiffMultiplier * Math.abs(swissPlayers.get(i).points - swissPlayers.get(j).points);
pointsDiffMultiplier * Math.abs(swissPlayers.get(i).getPoints() - swissPlayers.get(j).getPoints());
w[j][i] = w[i][j];
}
}
} else {
for (int i = 0; i < playersCount; i++) {
PlayerInfo player = swissPlayers.get(i);
for (int p = player.points; p >= 0; p--) {
for (int p = player.getPoints(); p >= 0; p--) {
int first = -1;
int last = -1;
for (int j = 0; j < playersCount; j++) {
if (swissPlayers.get(j).points == p) {
if (swissPlayers.get(j).getPoints() == p) {
if (first < 0) {
first = j;
}
@ -146,8 +145,8 @@ public class SwissPairingMinimalWeightMatching {
if (first < 0) {
continue;
}
int self = (p == player.points ? i : first - 1);
int diff = pointsDiffMultiplier * (player.points - p);
int self = (p == player.getPoints() ? i : first - 1);
int diff = pointsDiffMultiplier * (player.getPoints() - p);
for (int j = Math.max(first, i); j <= last; j++) {
w[i][j] = Math.abs(j - (last + first - self)) + diff;
w[j][i] = w[i][j];
@ -168,7 +167,7 @@ public class SwissPairingMinimalWeightMatching {
// try to avoid giving the same person multiple byes
if (n > playersCount) {
for (int i = 0; i < playersCount; i++) {
w[i][n - 1] = 10 * (swissPlayers.get(i).points - swissPlayers.get(playersCount - 1).points) + (playersCount - i - 1);
w[i][n - 1] = 10 * (swissPlayers.get(i).getPoints() - swissPlayers.get(playersCount - 1).getPoints()) + (playersCount - i - 1);
w[i][n - 1] += byes[i] * 2000;
w[n - 1][i] = w[i][n - 1];
}
@ -195,9 +194,9 @@ public class SwissPairingMinimalWeightMatching {
List<TournamentPairing> pairings = new ArrayList<>();
List<TournamentPlayer> playerByes = new ArrayList<>();
Map<Integer, TournamentPlayer> map = new HashMap<>();
Map<Integer, TournamentPlayer> map = new HashMap<>();
for (PlayerInfo player : swissPlayers) {
map.put(player.id, player.tournamentPlayer);
map.put(player.getId(), player.getTournamentPlayer());
}
if (n > playersCount) {
@ -218,15 +217,15 @@ public class SwissPairingMinimalWeightMatching {
return new RoundPairings(pairings, playerByes);
}
boolean[] used;
private boolean[] used;
// current pairs
int[] pairs;
private int[] pairs;
// current weight
int weight;
private int weight;
int[] result;
int minCost;
private int[] result;
private int minCost;
// backtrack all possible pairings and choose one with minimal weight
private void makePairings(int t) {
@ -262,13 +261,45 @@ public class SwissPairingMinimalWeightMatching {
}
static class PlayerInfo {
public int id;
private int id;
public TournamentPlayer tournamentPlayer;
private TournamentPlayer tournamentPlayer;
public int points;
private int points;
public int sosPoints;
private int sosPoints;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public TournamentPlayer getTournamentPlayer() {
return tournamentPlayer;
}
public void setTournamentPlayer(TournamentPlayer tournamentPlayer) {
this.tournamentPlayer = tournamentPlayer;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
public int getSosPoints() {
return sosPoints;
}
public void setSosPoints(int sosPoints) {
this.sosPoints = sosPoints;
}
}
}