Hello everyone,
I wrote a game which name is in title. But i must write code without using if(comprasion). Can you give me some advice?
there are 4 class. Item, paper, rock, scissors. P, R, S are Item. Item is abstract class.
Thank you.
Printable View
Hello everyone,
I wrote a game which name is in title. But i must write code without using if(comprasion). Can you give me some advice?
there are 4 class. Item, paper, rock, scissors. P, R, S are Item. Item is abstract class.
Thank you.
this program main method:
Quote:
import java.util.ArrayList;
import java.util.List;
public class GameMain extends Item {
public static void main(String[] args) {
List <Item> itemList = new ArrayList<Item>();
itemList.add(new Paper("Paper"));
itemList.add(new Scissors("Scissors"));
itemList.add(new Stone("Stone"));
for(Item item1: itemList){
for(Item item2:itemList){
System.out.print(item1 + "<--->" + item2 + " ");
item1.match(item2);
}
}
}
}
How are you representing the players? And their choices?
If you have to do it completely without if statements you have to assign a rank to rock, scissors, paper, e.g. 0, 1, 2. You also have to define a 'decistion matrix' that tells the user who has won, e.g.
The row index is the computer's guess, the column index is the human's guess.Code:String[][] win= {
{ "tie", "computer wins", "human wins" },
{ "human wins", "tie", "computer wins" },
{ "computer wins", "human wins", "tie }
};
kind regards,
Jos
I'd say use a switch statement.