I'd like a critique of my first java application.
It's a rock paper scissors game but it's not a game yet. It's just a structure and a test set.
CLASS: Program
public class Program {
public static void main(String[] args) {
IAction player = null;
IAction computer = null;
// Test tie on ROCK
player = ActionFactory.get(1);
computer = ActionFactory.get(1);
Comparer.compare(player, computer);
// Test tie on PAPER
player = ActionFactory.get(2);
computer = ActionFactory.get(2);
Comparer.compare(player, computer);
// Test tie on SCISORS
player = ActionFactory.get(3);
computer = ActionFactory.get(3);
Comparer.compare(player, computer);
// Player beats computer ROCK beats SCISORS
player = ActionFactory.get(1);
computer = ActionFactory.get(3);
Comparer.compare(player, computer);
// Player beats computer PAPER beats ROCK
player = ActionFactory.get(2);
computer = ActionFactory.get(1);
Comparer.compare(player, computer);
// Player beats computer SCISORS beats PAPER
player = ActionFactory.get(3);
computer = ActionFactory.get(2);
Comparer.compare(player, computer);
// Computer beats player ROCK beats SCISORS
player = ActionFactory.get(3);
computer = ActionFactory.get(1);
Comparer.compare(player, computer);
// Computer beats player PAPER beats ROCK
player = ActionFactory.get(1);
computer = ActionFactory.get(2);
Comparer.compare(player, computer);
// Computer beats player SCISORS beats PAPER
player = ActionFactory.get(2);
computer = ActionFactory.get(3);
Comparer.compare(player, computer);
}
}
INTERFACE: IAction
public interface IAction {
public String getName();
public IAction getBeats();
}
CLASS: Rock, Paper, Scissors
public class Rock implements IAction {
public String getName()
{
return "Rock";
}
public IAction getBeats()
{
return new Scisors();
}
}
public class Paper implements IAction {
public String getName()
{
return "Paper";
}
public IAction getBeats()
{
return new Rock();
}
}
public class Scisors implements IAction {
public String getName()
{
return "Scisors";
}
public IAction getBeats()
{
return new Paper();
}
}
CLASS: ActionFactory
public class ActionFactory {
public static IAction get(int action)
{
switch (action) {
case 1:
return new Rock();
case 2:
return new Paper();
case 3:
return new Scisors();
default:
return null;
}
}
}
CLASS: Comparer
public class Comparer {
public static void compare(IAction player, IAction computer)
{
if (player.getName() == computer.getName())
System.out.println(player.getName() + " ties " + computer.getName());
else
{
IAction toWin = player.getBeats();
if (computer.getName() == toWin.getName())
System.out.println(player.getName() + " beats " + computer.getName());
else
System.out.println(player.getName() + " loses to " + computer.getName());
}
}
}
Finally, output:
Quote
Paper ties Paper
Scisors ties Scisors
Rock beats Scisors
Paper beats Rock
Scisors beats Paper
Scisors loses to Rock
Rock loses to Paper
Paper loses to Scisors
Anything you can comment on would be great.
I noticed that you cannot switch statement a string. Is there a best practice on this to achieve close to the same thing as a string compare? I don't like these numbers. I suppose I can use an enum but I'm not sure how to structure it.
Thanks.
Oh and attached is the source code if you want to look at the whole project.
EDIT:
Oh and I know I was too dumb to recognize how to spell scissors... save it.
Attached File(s)
-
RockPaperScisors.zip (6.28K)
Number of downloads: 85
This post has been edited by MentalFloss: 30 August 2010 - 10:17 AM

New Topic/Question
Reply




MultiQuote









|