问题
I'm making a terrible 2D remake of Minecraft as a class project in java, and I have a crafting bench thing (or whatever it's called) and I have an if statement that checks if you have one piece of wood in the top left and nothing in the other 3, or if you have one piece of wood in the top right and nothing in the other 3, and so on...
The following if statement which I'm using seems to let you have a "wood" block in multiple slots at the same time and still lets you get the "plank" block. (id1
- id4
represent the crafting bench slots) 2x2 crafting bench Tile.wood
is a wood block, Tile.blank
is basically a null block or nothing.
//this if statement is what I need to change maybe?
if ((id1.id == Tile.wood && id2.id == Tile.blank
&& id3.id == Tile.blank && id4.id == Tile.blank) ||
(id1.id == Tile.blank && id2.id == Tile.wood
&& id3.id == Tile.blank && id4.id == Tile.blank) ||
(id1.id == Tile.blank && id2.id == Tile.blank
&& id3.id == Tile.wood && id4.id == Tile.blank) ||
(id1.id == Tile.blank && id2.id == Tile.blank
&& id3.id == Tile.blank && id4.id == Tile.wood)) {
//this code I don't need to change, it works fine
Inventory.inv_result.id = Tile.plank;
Inventory.inv_result.blockNum += 4;
System.out.println("You can have 4 planks");
}
So once again you should only be allowed to have one block in one place at one time, otherwise it will do nothing.
How can I fix it so I only get the "plank" block if there is only a single "wood" block in one of the four slots?
回答1:
The if-condition looks untidy but will work perfectly fine. However, you can modularize it for better understanding & debugging.
Create few utility methods to do jobs for you, as below:
boolean isWood(<id object>) {
if(Tile.wood.equals(<id object>))
return true;
else
return false;
}
boolean isBlank(<id object>) {
if(Tile.blank.equals(<id object>))
return true;
else
return false;
}
void doProcess(){
Inventory.inv_result.id = Tile.plank;
Inventory.inv_result.blockNum += 4;
System.out.println("You can have 4 planks");
}
Then re-write your if-condition like below
if(isWood(id1.id) && isBlank(id2.id) && isBlank(id3.id) && isBlank(id4.id))
doProcess();
else if(isWood(id2.id) && isBlank(id1.id) && isBlank(id3.id) && isBlank(id4.id))
doProcess();
else if(isWood(id3.id) && isBlank(id2.id) && isBlank(id1.id) && isBlank(id4.id))
doProcess();
else if(isWood(id4.id) && isBlank(id2.id) && isBlank(id3.id) && isBlank(id1.id))
doProcess();
Shishir
回答2:
i think if statement is ok even though it is so long. Try to use .equal() instead of == in the if statement.
回答3:
The problem you have with your if-condition has nothing to do with the if-condition but some other part of your crafting bench code. You should post your crafting bench code for us to find the actual problem.
Besides about the untidiness and stuff... This may not be the answer to your question but this will definitely make it easier for you to do many recipes:
// These are your function calls. One line per recipe. Way better than multiple if functions
// example for shaped crafting using minecraft's crafting bench recipe.
shapedCrafting(new int[]{Tile.plank, Tile.plank, Tile.plank, Tile.plank}, 4, Tile.plank);
// example for shapeless crafting using your recipe
shapelessCrafting(new int[]{Tile.plank}, 4, Tile.plank);
This would be an example of your crafting bench with an array instead of variables:
// This array resembles your crafting bench grid (2x2) in this case.
public static int[] arrayCraftingSlots = {Tile.plank, 0, 0, Tile.wood};
The function for shaped crafting:
/* int elements[] - takes in the crafting layout (this should equal your crafting bench grid)
* int amount - the amount of the item or tile you get as a result
* int result - the id of the result you get
*/
public static void shapedCrafting(int elements[], int amount, int result)
{
for(int i = 0; i < elements.length; i++)
{
if(arrayCraftingSlots[i] != elements[i])
{
return;
}
}
Inventory.inv_result.id = result;
Inventory.inv_result.blockNum += amount;
System.out.println("You can have " + amount + " " + result);
}
And the function for shapeless crafting: I've restricted this one to 1 instance of an item per recipe just like in Minecraft. So using 2 planks in one shapeless recipe would not be possible. Of course you can change that if you want to. Keep in mind you have to change a bit of the algorithm as well.
/* int elements[] - list of elements that need to be anywhere in the crafting bench
* int amount - the amount of the item or tile you get as a result
* int result - the id of the result you get
*/
public static void shapelessCrafting(int elements[], int amount, int result)
{
for(int element : arrayCraftingSlots)
{
boolean in = false;
for(int i = 0; i < elements.length; i++)
{
if(element == elements[i] || element == 0)
{
in = true;
}
}
if(!in)
{
return;
}
}
for(int element : elements)
{
int occassions = 0;
for(int i = 0; i < arrayCraftingSlots.length; i++)
{
if(element == arrayCraftingSlots[i])
{
occassions++;
}
}
if(occassions > 1 || occassions == 0)
{
return;
}
}
Inventory.inv_result.id = result;
Inventory.inv_result.blockNum += amount;
System.out.println("You can have " + amount + " " + result);
}
来源:https://stackoverflow.com/questions/21744679/java-if-statement-checking