Skip to content

Checkmate #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/it/unipv/ingsw/blackmarket/Dealer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ public abstract class Dealer implements Comparable<Dealer> {
/// Money made so far.
private long coins = 0;


/**
* Some more protections against extreme cheaters
*/
private final CoinValidator cv = new CoinValidator();

private final class CoinValidator {
private long verifiedCoins = 0;

private void setVerifiedCoins(long c) {
verifiedCoins = c;
}

private boolean legit(long c) {
if (verifiedCoins == c) return true;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return verifiedCoins == c

return false;
}
}

/**
* Checks if dealer's coins are legit
*/
final boolean isCheater() {
return !cv.legit(coins);
}


/**
* Get the name of the Dealer.
*/
Expand All @@ -25,13 +52,21 @@ public final long getCoins() {

/**
* Add some money to the profit of the dealer.
* Setting coins value by using reflection is NOT allowed.
*/
final void addCoins(long amount) {
if (this.isCheater()) {
coins = Long.MIN_VALUE;
cv.setVerifiedCoins(coins);
return;
}

try {
coins = Math.addExact(coins, amount);
} catch (ArithmeticException e) {
coins = Long.MIN_VALUE;
}
cv.setVerifiedCoins(coins);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/it/unipv/ingsw/blackmarket/Market.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,14 @@ public void simulateSeason(int days, int roundsPerDay) {

/**
* Sort the list of dealers by decreasing profit.
* Cheaters are set to minimum value
*/
public void sortDealers() {
for (Dealer d : dealers) {
if (d.isCheater())
d.addCoins(0);
}

dealers.sort(Collections.reverseOrder());
}

Expand Down