这个游戏对学习类的朋友是一个不错的例子。
来源AOL
游戏规则:参加游戏的玩家,每个人掷两次骰子.两次之和谁大的,谁就是赢家.
主要是试一下用UML的方法来做游戏,但总觉得Actor只有一个玩家,顶多也就是多个Timer.这个游比较简单也看不出来什么,不过熟悉一下做游戏的这道流程也是一种收获.
class Player
{
private var name:String;
private var score:Number;
function Player(name:String)
{
this.name = name;
}
public function play(die1:Die, die2:Die):Void
{
die1.roll();
die2.roll();
score = die1.getFaceValue()+die2.getFaceValue();
}
public function getName():String
{
return name;
}
public function getScore():Number
{
return score;
}
}
class Die
{
private var faceValue:Number;
public function getFaceValue():Number
{
return faceValue;
}
public function roll():Void
{
faceValue = int(Math.random() * 6 + 1);
}
}
class DiceGame
{
private var die1, die2:Die;
private var player1,player2:Player;
public function DiceGame(name1:String,name2:String)
{
player1 = new Player(name1);
player2 = new Player(name2);
die1 = new Die();
die2 = new Die();
}
public function play():Void
{
player1.play(die1, die2);
player2.play(die1, die2);
}
public function getWinner():Player
{
var score1:Number = player1.getScore();
var score2:Number = player2.getScore();
trace(score1+":"+score2);
if (score1== score2)
{
return null;
}
else
{
return (score1> score2)?player1:player2;
}
}
public static function main():Void
{
var game:DiceGame = new DiceGame("小强","张立");
game.play();
var winner:Player =game.getWinner();
if(winner!=null)
trace(winner.getName()+"胜!");
}
}
DiceGame.main();