Paper rock Scissors Game inheritance problem

• 08-20-2010, 08:20 AM
kaanax
Paper rock Scissors Game inheritance problem
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.
• 08-20-2010, 08:30 AM
kaanax
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>();

for(Item item1: itemList){
for(Item item2:itemList){
System.out.print(item1 + "<--->" + item2 + " ");
item1.match(item2);
}

}

}
}
• 08-20-2010, 01:23 PM
Norm
How are you representing the players? And their choices?
• 08-20-2010, 01:41 PM
JosAH
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.

Code:

```String[][] win= {   { "tie", "computer wins", "human wins" },   { "human wins", "tie", "computer wins" },   { "computer wins", "human wins", "tie } };```
The row index is the computer's guess, the column index is the human's guess.

kind regards,

Jos
• 08-20-2010, 02:03 PM
imorio
I'd say use a switch statement.
• 08-20-2010, 02:06 PM
JosAH
Quote:

Originally Posted by imorio
I'd say use a switch statement.

A switch statement is an if statement in disguise ...

kind regards,

Jos
• 08-20-2010, 02:33 PM
imorio
Quote:

Originally Posted by JosAH
A switch statement is an if statement in disguise ...

kind regards,

Jos

A problem is a solution in disguise :D
• 08-20-2010, 02:44 PM
JosAH
Quote:

Originally Posted by imorio
A problem is a solution in disguise :D

True, and sometimes their disguise is so develish that they look like an entirely other problem ;-)

kind regards,

Jos