这是一个编辑,我完全修改了我的答案。
**从游戏角度来看问题**
玩家有 2 个绿色筹码(5 分)和 1 个蓝色筹码(10 分)。这总分是 20 分。现在玩家想要购买一个花费 16 点的游戏图标。玩家有足够的钱购买该物品。所以玩家支付了 16 点,但是他会给商店多少点才能正确支付。
现在我已经编写了一个工作示例,所有工作都已完成。
代码
程序.java
import java.util.Arrays;
public class Program {
public static void main(String[] args) {
// Setting up test environment
Player player = new Player("Borrie", new int[]{0,0,0,0, 230});
int itemCost = 16626;
// Pay for item
System.out.printf("First we check if the player can pay with it's current ChipSet");
if (!player.canPayWithChipSet(player.getChips(), 5)) {
if (player.exchangeChips(5)) {
System.out.printf("\n\nThe players ChipSet:" + Arrays.toString(player.getChips().chips));
System.out.printf("\nThe players ChipSet has been succesfully exchanged.");
} else {
System.out.printf("\n\nThe players ChipSet:" + Arrays.toString(player.getChips().chips));
System.out.printf("\nThe players ChipSet was not able to be exchanged.\n");
}
} else {
System.out.printf("\n\nThe player can pay exact with it's original ChipSet. No need to exchange.");
}
}
}
Player.java
import java.util.ArrayList;
import java.util.Arrays;
public class Player {
private String name;
private ChipSet chips;
private int points = 0;
public Player(String name, int[] chips) {
this.name = name;
this.chips = new ChipSet(chips);
this.points = this.chips.getSum();
}
public boolean exchangeChips(int cost) {
ChipSet newChipSet = exchangePlayerChipSet(this.chips.getChips(), cost);
if (newChipSet == null) {
return false;
} else {
this.chips = newChipSet;
return true;
}
}
public ChipSet exchangePlayerChipSet(int[] originalChipValues, int cost) {
ChipSet newChipSet = null;
// Create possible combinations to compare
ArrayList<ChipSet> chipSetCombos = createCombinations(this.chips.getChips());
// Filter the chipset based on if it's able to pay without changing chips
System.out.printf("\n\n---- Filter which of these combinations are able to be payed with without changing chips ----");
ArrayList<ChipSet> filteredCombos = filterCombinations(chipSetCombos, cost);
// Compare the filtered chipsets to determine which one has changed the least
if (!filteredCombos.isEmpty()) {
newChipSet = compareChipSets(originalChipValues, filteredCombos);
}
return newChipSet;
}
private ArrayList<ChipSet> createCombinations(int[] array) {
ArrayList<ChipSet> combos = new ArrayList<>();
int[] startCombo = array;
System.out.printf("Player has " + getTotalPoints(startCombo) + " points in chips.");
System.out.printf("\nPlayer has these chips (WHITE,RED,GREEN,BLUE,BLACK): " + Arrays.toString(startCombo));
while (startCombo[4] != 0) {
startCombo = lowerBlack(startCombo);
if (getTotalPoints(startCombo) == points) {
combos.add(new ChipSet(startCombo));
}
}
while (startCombo[3] != 0) {
startCombo = lowerBlue(startCombo);
if (getTotalPoints(startCombo) == points) {
combos.add(new ChipSet(startCombo));
}
}
while (startCombo[2] != 0) {
startCombo = lowerGreen(startCombo);
if (getTotalPoints(startCombo) == points) {
combos.add(new ChipSet(startCombo));
}
}
while (startCombo[1] != 0) {
startCombo = lowerRed(startCombo);
if (getTotalPoints(startCombo) == points) {
combos.add(new ChipSet(startCombo));
}
}
System.out.printf("\n\n---- Creating variations on the players chips ----");
System.out.printf("\nVariation (all worth " + getTotalPoints(startCombo) + " points):\n");
int teller = 1;
for (ChipSet a : combos) {
System.out.printf("\nCombo " + teller + ": " + Arrays.toString(a.getChips()));
teller++;
}
return combos;
}
private ArrayList<ChipSet> filterCombinations(ArrayList<ChipSet> combinations, int cost) {
ArrayList<ChipSet> filteredChipSet = new ArrayList<>();
combinations.stream().filter((cs) -> (canPayWithChipSet(cs, cost))).forEach((cs) -> {
filteredChipSet.add(cs);
});
return filteredChipSet;
}
// This method has be worked out
public boolean canPayWithChipSet(ChipSet cs, int cost) {
ChipSet csOrig = new ChipSet(cs.chips);
ChipSet csCopy = new ChipSet(cs.chips);
int counterWhite = 0;
int counterRed = 0;
int counterGreen = 0;
int counterBlue = 0;
int counterBlack = 0;
while (20 <= cost && cost > 0 && csOrig.getChips()[4] != 0) {
csOrig.getChips()[4] -= 1;
cost -= 20;
counterBlack++;
}
while (10 <= cost && cost > 0 && csOrig.getChips()[3] != 0) {
csOrig.getChips()[3] -= 1;
cost -= 10;
counterBlue++;
}
while (5 <= cost && cost > 0 && csOrig.getChips()[2] != 0) {
csOrig.getChips()[2] -= 1;
cost -= 5;
counterGreen++;
}
while (2 <= cost && cost > 0 && csOrig.getChips()[1] != 0) {
csOrig.getChips()[1] -= 1;
cost -= 2;
counterRed++;
}
while (1 <= cost && cost > 0 && csOrig.getChips()[0] != 0) {
csOrig.getChips()[0] -= 1;
cost -= 1;
counterWhite++;
}
if (cost == 0){
System.out.printf("\nCombo: %s can pay exact. With %d white, %d red, %d green, %d blue an %d black chips", Arrays.toString(csCopy.chips),counterWhite,counterRed,counterGreen,counterBlue,counterBlack);
return true;
} else {
System.out.printf("\nCombo: %s cannot pay exact.\n\n\n", Arrays.toString(csCopy.chips));
return false;
}
}
private ChipSet compareChipSets(int[] originalChipValues, ArrayList<ChipSet> chipSetCombos) {
ChipSet newChipSet;
int[] chipSetWaardes = originalChipValues; // originele chipset aantal van kleur
int[] chipSetCombosDifferenceValues = new int[chipSetCombos.size()];
int teller = 1;
System.out.printf("\n\n---- Calculate differences between players stack and it's variations ----");
for (ChipSet cs : chipSetCombos) {
int aantalWhite = cs.getChips()[0];
int aantalRed = cs.getChips()[1];
int aantalGreen = cs.getChips()[2];
int aantalBlue = cs.getChips()[3];
int aantalBlack = cs.getChips()[4];
int differenceWhite = Math.abs(chipSetWaardes[0] - aantalWhite);
int differenceRed = Math.abs(chipSetWaardes[1] - aantalRed);
int differenceGreen = Math.abs(chipSetWaardes[2] - aantalGreen);
int differenceBlue = Math.abs(chipSetWaardes[3] - aantalBlue);
int differenceBlack = Math.abs(chipSetWaardes[4] - aantalBlack);
int totalDifference = differenceWhite + differenceRed + differenceGreen + differenceBlue + differenceBlack;
chipSetCombosDifferenceValues[teller - 1] = totalDifference;
System.out.printf("\nCombo " + teller + ": " + Arrays.toString(cs.getChips()) + " = " + totalDifference);
teller++;
}
newChipSet = chipSetCombos.get(smallestValueOfArrayIndex(chipSetCombosDifferenceValues));
System.out.printf("\n\nThe least different ChipSet is: " + Arrays.toString(newChipSet.getChips()) + " ");
return newChipSet;
}
private int smallestValueOfArrayIndex(int[] array) {
int huidigeWaarde = array[0];
int smallestIndex = 0;
for (int j = 1; j < array.length; j++) {
if (array[j] < huidigeWaarde) {
huidigeWaarde = array[j];
smallestIndex = j;
}
}
return smallestIndex;
}
private int[] lowerBlack(int[] array) {
return new int[]{array[0], array[1], array[2], array[3] + 2, array[4] - 1};
}
private int[] lowerBlue(int[] array) {
return new int[]{array[0], array[1], array[2] + 2, array[3] - 1, array[4]};
}
private int[] lowerGreen(int[] array) {
return new int[]{array[0] + 1, array[1] + 2, array[2] - 1, array[3], array[4]};
}
private int[] lowerRed(int[] array) {
return new int[]{array[0] + 2, array[1] - 1, array[2], array[3], array[4]};
}
private int getTotalPoints(int[] array) {
return ((array[0] * 1) + (array[1] * 2) + (array[2] * 5) + (array[3] * 10) + (array[4] * 20));
}
public String getName() {
return this.name;
}
public int getPoints() {
return this.points;
}
public ChipSet getChips() {
return chips;
}
}
ChipSet.java
public class ChipSet {
public static final int WHITE_VALUE = 1;
public static final int RED_VALUE = 2;
public static final int GREEN_VALUE = 5;
public static final int BLUE_VALUE = 10;
public static final int BLACK_VALUE = 20;
public static final int[] VALUES = new int[]{WHITE_VALUE, RED_VALUE, GREEN_VALUE, BLUE_VALUE, BLACK_VALUE};
protected int[] chips;
public ChipSet(int[] chips) {
if (chips == null || chips.length != 5) {
throw new IllegalArgumentException("ChipSets should contain exactly 5 integers!");
}
// store a copy of passed array
this.chips = new int[5];
for (int i = 0; i < this.chips.length; i++) {
this.chips[i] = chips[i];
}
}
public int getSum() {
return chips[0] * WHITE_VALUE
+ chips[1] * RED_VALUE
+ chips[2] * GREEN_VALUE
+ chips[3] * BLUE_VALUE
+ chips[4] * BLACK_VALUE;
}
public int[] getChips() {
return this.chips;
}
}
一些解释:
我们创建了一些子方法,将筹码换成较低的筹码。所以
例如黑色 = 2 个蓝色。然后我们按顺序创建 5 个循环。这
第一个检查是否还有黑色筹码,如果有,减少 1 个黑色
添加2个蓝调。将此新组合保存在列表中,如果
新 ChipSet 中的筹码等于原始 ChipSets 值。环形
一直持续到没有黑人为止。然后它检查是否存在
是蓝色并重复相同的过程,直到没有红色
了。现在我们列出了 X 值的所有可能变化
筹码。
您根据以下条件过滤 ChipSet
如果您可以在不交换的情况下与他们支付 X 积分。我们遍历所有
上一部分中创建的芯片组的可能组合。如果你
可以使用 ChipSet 支付而无需交换将其添加到过滤列表
芯片组。结果是一个仅包含有效芯片组的归档列表。
对于每个 ChipSet,我们计算一个中所有颜色的芯片数量
ChipSet 并用它减去原始芯片组的芯片数。
你把它的绝对值加起来
原始颜色和组合颜色的差异。现在我们有一个
代表与原始差异的数字。现在我们都
所要做的就是比较所有芯片组的“差异数”。唯一的那个
我们用来分配给播放器的差异最小。
简单吧