I'm making a program that creates random trades, but there's a lot of conditional statements to prevent duplicate or pointless trades (ex: trading items A and B for B and A, or trading A and B for C and A, etc.)
Is a ton of if-else statements the best approach, or is there a more effective method to process conditions and give correct outputs?
Here's some older code I used to use prior to starting again on this project from scratch:
int[] prevTrade = new int[5];
// Note previous trade info
prevTrade[0] = this.tradeSlot1;
prevTrade[1] = this.tradeSlot2;
prevTrade[2] = this.tradeSlot3;
prevTrade[3] = this.tradeSlot4;
// Reset trade choices
this.tradeSlot1 = -1;
this.tradeSlot2 = -1;
this.tradeSlot3 = -1;
this.tradeSlot4 = -1;
// Generate output list for conversion to image
int[] outList = new int[5];
// Make trade small or large using random number
int randTradeSize = (int)(Math.random() * 100);
//java.awt.Toolkit.getDefaultToolkit().beep();
if (randTradeSize >= 75) { // is a 2 for 2 trade, happens 25% of the time
this.tradeSize = "Large";
// Create a new trade offer with 4 outputs (2 items for 2 other items)
while (randTradeSize != -1) {
this.tradeSlot1 = newTrade();
this.tradeSlot2 = newTrade();
this.tradeSlot3 = newTrade();
this.tradeSlot4 = newTrade();
if (((this.tradeSlot1 == this.tradeSlot3) && (this.tradeSlot1 == this.tradeSlot4) && (this.tradeSlot2 == this.tradeSlot3) && (this.tradeSlot2 == this.tradeSlot4)) || (this.tradeSlot1 == this.tradeSlot3) || (this.tradeSlot1 == this.tradeSlot4) || (this.tradeSlot2 == this.tradeSlot3) || (this.tradeSlot2 == this.tradeSlot4) || ((input.tradeSize == "Large") && (secondTrade.tradeSize == "Large")) && ((input.tradeSlot1 == secondTrade.tradeSlot1) && (input.tradeSlot2 == secondTrade.tradeSlot2) && (input.tradeSlot3 == secondTrade.tradeSlot3) && (input.tradeSlot4 == secondTrade.tradeSlot4)) || ((input.tradeSlot1 == secondTrade.tradeSlot1) && (input.tradeSlot2 == secondTrade.tradeSlot2) && (input.tradeSlot3 == secondTrade.tradeSlot4) && (input.tradeSlot4 == secondTrade.tradeSlot3)) || ((input.tradeSlot1 == secondTrade.tradeSlot2) && (input.tradeSlot2 == secondTrade.tradeSlot1) && (input.tradeSlot3 == secondTrade.tradeSlot3) && (input.tradeSlot4 == secondTrade.tradeSlot4)) || ((input.tradeSlot1 == secondTrade.tradeSlot1) && (input.tradeSlot2 == secondTrade.tradeSlot2) && (input.tradeSlot4 == secondTrade.tradeSlot3) && (input.tradeSlot3 == secondTrade.tradeSlot4)) || ((input.tradeSlot2 == secondTrade.tradeSlot1) && (input.tradeSlot1 == secondTrade.tradeSlot2) && (input.tradeSlot3 == secondTrade.tradeSlot3) && (input.tradeSlot4 == secondTrade.tradeSlot4))) {
this.tradeSlot1 = -1;
//System.out.println("Large\tInvalid Trade");
}
if ((this.repeatTrade(this, prevTrade) == false) && (this.tradeSlot1 != -1)) {
randTradeSize = -1;
outList[0] = this.tradeSlot1;
outList[1] = this.tradeSlot2;
outList[2] = 5; // arrow.png
outList[3] = this.tradeSlot3;
outList[4] = this.tradeSlot4;
//System.out.println("Large\tTrade Success");
}
}
} else { // is a 1 for 1 trade, happens 75% of the time
this.tradeSize = "Small";
// Create a new trade offer with 2 outputs (1 item for another item)
while (randTradeSize != -1) {
this.tradeSlot1 = newTrade();
this.tradeSlot2 = newTrade();
this.tradeSlot3 = -1;
this.tradeSlot4 = -1;
// Check if trade is copy of other trade
if ((this.tradeSlot1 == this.tradeSlot2) || (((input.tradeSize == "Small") && (secondTrade.tradeSize == "Small")) && (input.tradeSlot1 == secondTrade.tradeSlot1) && (input.tradeSlot2 == secondTrade.tradeSlot2))) {
this.tradeSlot1 = -1;
//System.out.println("Small\tInvalid Trade");
}
// Check if trade is repeat, then if all other parameters are met
if ((this.repeatTrade(this, prevTrade) == false) && (this.tradeSlot1 != -1)) {
randTradeSize = -1;
outList[0] = this.tradeSlot1;
outList[1] = 5; // arrow.png
outList[2] = this.tradeSlot2;
outList[3] = -1; // null value, no image
outList[4] = -1; // null value, no image
//System.out.println("Small\tTrade Success");
}
}
}
// Reset random trade lifetime
this.randTradeLifetime(this);
return outList;
}```
Aucun commentaire:
Enregistrer un commentaire