========================================
public abstract class Item { //return 1 if the receiver wins, -1 if it looses, //0 otherwise. public abstract int play(Item that);}public class Paper extends Item { @override public int play(Item that) { if(that instanceof Rock) return 1; if(that instanceof Scissors) return -1; return 0; }}public class Scissors extends Item { @override public int play(Item that) { if(that instanceof Rock) return -1; if(that instanceof Paper) return 1; return 0; }}public class Rock extends Item { @override public int play(Item that) { if(that instanceof Scissors) return 1; if(that instanceof Paper) return -1; return 0; }}public static void main(String[] args){ Item rock = new Rock(); Item scissors = new Scissors(); if(rock.play(scissors) > 0) System.out.println("Rock Wins"); else if(rock.play(scissors) System.out.println("Rock Looses"); else System.out.println("Tie");}
========================================
About the game: http://en.wikipedia.org/wiki/Rock-paper-scissors
No comments:
Post a Comment