在做 mvc 时,我通常从命令行视图开始,然后添加东西直到事情稳定下来。然后添加gui的东西。始终保留命令行视图和控制器,因为它们对测试非常有用。
尝试从这样的事情开始:
编辑:重构代码,以便观察者在玩家资金发生变化或破产时得到通知。
import java.util.*;
class DiceRoller {
DiceRoller(final int sides) {
this(1,sides);
}
DiceRoller(final int dice,final int sides) {
this.sides=sides;
this.dice=dice;
}
int roll() {
int sum=0;
for(int i=0;i<dice;i++)
sum+=random.nextInt(sides)+1;
return sum;
}
final Random random=new Random();
final int dice;
final int sides;
}
class Stock {
Stock(int price) {
this.price=price;
}
int value;
int dividend;
final int id=ids++;
final int price;
static int ids;
public String toString() {
return ""+id+" "+value+" "+dividend;
}
}
class Player {
public void turn() {
System.out.print("turn for player "+id);
int die1=diceRoller.roll();
System.out.print(", he rolls "+die1);
money+=die1-5; // real simple for now
System.out.println(", he has "+money);
}
public String toString() {
String s=""+id+" $"+money;
if(stocks.size()>0)
s+=" stocks";
for(Stock stock:stocks.keySet())
s+=" "+stock+" "+stocks.get(stock);
return s;
}
Map<Stock,Integer> stocks=new LinkedHashMap<Stock,Integer>();
int money=10;
final int id=ids++;
DiceRoller diceRoller=new DiceRoller(6);
static int ids;
}
class Model extends Observable {
void init(int stocks,int players) {
for(int i=0;i<stocks;i++)
this.stocks.add(new Stock(random.nextInt(10)+1));
for(int i=0;i<players;i++)
this.players.add(new Player());
setChanged();
}
void turn(Player player) {
player.turn();
setChanged();
notifyObservers();
}
void busted(List<Player> busted) {
if(busted.size()>0) {
players.removeAll(busted);
setChanged();
notifyObservers();
}
}
public String toString() {
String s="";
for(Stock stock:stocks)
s+="stock "+stock+"\n";
for(Player player:players)
s+="player "+player+"\n";
s=s.substring(0,s.length()-1); // removed last line feed
return s;
}
Random random=new Random();
final List<Stock> stocks=new ArrayList<Stock>();
final List<Player> players=new ArrayList<Player>();
}
class CommandLineView implements Observer {
@Override public void update(Observable observable,Object hint) {
if(observable instanceof Model)
display((Model)observable);
else throw new RuntimeException("oops");
}
void display(Model model) {
System.out.println("[update: "+model);
System.out.println("end of update]");
}
}
class Game {
Game() {
model=new Model();
view=new CommandLineView();
model.addObserver(view);
model.init(4,3);
model.notifyObservers();
}
void run() {
for(round=0;model.players.size()>1;round++) {
System.out.println("round "+round);
for(Player player:model.players)
model.turn(player);
List<Player> busted=new ArrayList<Player>();
for(Player player:model.players)
if(player.money<=0) {
System.out.println("player "+player.id+" busted out.");
busted.add(player);
}
model.busted(busted);
if(round>10) {
System.out.println("breaking out");
break;
}
}
if(model.players.size()==1)
System.out.println("the winner is "+model.players.get(0));
}
final Model model;
Observer view;
int player;
int round;
}
public class So15578233 {
void run() {
game=new Game();
game.run();
}
public static void main(String[] args) {
new So15578233().run();
}
Game game;
}