Game Rules: Players participating in the game, everyone throws twice. Who is big two times, who is the winner. It is mainly to try to use UML methods to do games, but I always feel that Actor has only one player, and the top is more Timer. This tour is relatively simple, but this process is familiar to do games is also a harvest.
Pre {Font-Family: "Courier New", Courier, Arial; font-size: 12px;}
. galan {color: # 000000;
. readyword {color: # 993300;
.IDENTIFIER {color: # 000087;
.properties {color: # 000087;
{Color: # 000087;
.LINECMMMENT, .BLOCKCOMMENT {Color: # 808080;
. String {color: # 0000ff;}
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;
}
}
// Program entry
Public static function main (): void
{
Var game: DiceGame = New DiceGame ("Xiaoqiang", "Zhang Li"); Game.Play ();
Var Winner: Player = Game.Getwinner ();
IF (Winner! = NULL)
TRACE (Winner.getName () "Sheng!");
}
}
//main.fla
Dicegame.main ();