面向对象分析与设计——一个简单模拟掷色子游戏的实现(二)

上篇对模拟掷色子的游戏进行了面向对象分析与设计,现在我把我的CODE发上来,大家参考下。基本上是完全实现了UML类图里的方法,代码量不大,还有一些界面设计我用的是SWT,所以增加了代码的长度。我把完整的程序打包成了JAR包,双击即可运行(前提是你的机器必须安装java虚拟机),由于附件限制暂时没有上传。
代码如下:
主类:
DiceGame.java
package cn.winux.dicegame;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class DiceGame
{
	private static int wincounter=0;
	private static int failecounter=0;
	private static int money=100;
	
	public static void initialize()
	{
		final Player player = new Player();
		Die die = new Die();
		Display display = new Display().getDefault();
		final Shell shell = new Shell();
		shell.setText("UML-excise-投掷色子游戏");
		shell.setBounds(400, 150, 500, 400);
		shell.open();
		//-----------------------------------------------------------------------
		//-----------------------定义组产生边框效果--------------------------------
		Group group1 = new Group(shell, SWT.NONE);
		group1.setBounds(10, 10, 300, 260);
		
		final Group group2 = new Group(shell, SWT.NONE);
		group2.setBounds(330, 10, 140, 260);
		
		final Group group3 = new Group(group2, SWT.COLOR_BLACK);
		group3.setBounds(20, 100, 100, 80);
		group3.setText("公告牌");
		
		Label label = new Label(group2, SWT.COLOR_INFO_BACKGROUND|SWT.WRAP|SWT.LEFT);
		//此处刚开始定义的是Text text = new Text(group2, SWT.COLOR_INFO_BACKGROUND|SWT.WRAP|SWT.LEFT);
		//当给文本框的属性加上SWT.COLOR_INFO_BACKGROUND时,SWT.WRAP失去作用
		//为了让颜色和面板一致就使用了标签组件
		label.setBounds(10, 20, 120, 200);
		label.setText("Author:winux from buptsse   email:[email protected]");
		//-----------------------定义显示色子值的文本框---------------------------------------
		final Text text1 = new Text(group1, SWT.CENTER);
		text1.setFont(new Font(display, "黑体", 120, SWT.BOLD));
		text1.setBounds(20, 20, 120, 150);
		final Text text2 = new Text(group1, SWT.CENTER);
		text2.setFont(new Font(display, "黑体", 120, SWT.BOLD));
		text2.setBounds(160, 20, 120, 150);
		//------------------------投掷按钮-----------------------------------------
		final Button button_play = new Button(shell, SWT.PUSH);
		//button_play.
		button_play.setBounds(100, 300, 100, 30);
		button_play.setText("掷色子");
		//-----------------------退出----------------------------------------------
		Button button_quit = new Button(shell, SWT.PUSH);
		button_quit.setBounds(250, 300, 100, 30);
		button_quit.setText("退出");
		//------------------------监听掷色子按钮-------------------------------------------			
		final Text text_win = new Text(group3, SWT.COLOR_INFO_BACKGROUND);
		text_win.setBounds(5, 15, 90, 20);
		text_win.setText("你赢了:"+wincounter+"局");
		
		final Text text_faile = new Text(group3, SWT.COLOR_INFO_BACKGROUND);	
		text_faile.setBounds(5, 35, 90, 20);
		text_faile.setText("你输了:"+failecounter+"局");
		
		final Text text_money = new Text(group3, SWT.COLOR_INFO_BACKGROUND);
		text_money.setText("赌金:$"+money);
		text_money.setBounds(5, 55, 90, 20);

		SelectionAdapter sl = new SelectionAdapter()
		{			
			public void widgetSelected(SelectionEvent e)
			{	
				int result=0;
				text1.setText(player.play()+"");
				result=result+player.play();
				text2.setText(player.play()+"");
				result=Integer.parseInt(text1.getText())+Integer.parseInt(text2.getText());
				//------------------------------------------------------------------------
					if(result==7&&money>0)
					{
						wincounter++;					
						money+=10;
						MessageDialog.openInformation(shell, "结果", "你赢得了整个世界!!!");
						text_win.setText("你赢了:"+wincounter+"局");
						text_money.setText("赌金:$"+money);
					}
					else
						if(money>0)
						{
							failecounter++;
							money-=5;							
							MessageDialog.openInformation(shell, "结果", "你输掉了你的裤衩!!!");
							text_faile.setText("你输了:"+failecounter+"局");
							text_money.setText("赌金:$"+money);
						}
						else
						{
							MessageDialog.openWarning(shell, "必杀令", "你可以跳楼了!!!");
						}
			}
	   };
		button_play.addSelectionListener(sl);
		//------------------------------------------------------------------------
		//------------------监听退出按钮动作----------------------------------------
		SelectionAdapter s2 = new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e)
			{	
				shell.close();
			}
		};
		button_quit.addSelectionListener(s2);
		//-------------------------------------------------------------------------
		
		//-----------------主窗口一直存在直到被销毁----------------------------------
		while(!shell.isDisposed())
		{
			if(!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
	public static void main(String []args)
	{
		initialize();
	}
}


色子类:
Dia.java

package cn.winux.dicegame;

import java.util.Random;

public class Die 
{	
	public int getint;
	public int roll()
	{	
		return this.getint=1+(int)(Math.random()*6);//产生1到6的随机数
	}
}


赌徒类:
Player.java

package cn.winux.dicegame;

public class Player 
{
	private String name;
	public void Player(String name)
	{
		this.name=name;
	}
	public int  play()
	{
		int die;//定义色子
		die=new Die().roll();
		return die;
	}
}

你可能感兴趣的:(eclipse,游戏,虚拟机,UML)