JAVA编程 请帮忙

编写一个程序,叫PairOfDice, 包含两个Die对象。Die 对象是一个筛骰子并显示结果的程序。然后创建一个驱动类名叫BoxCars,包含一个主方法运行PairOfDice1000次,计算出现两个六的次数。然后用PairOfDice 类设计一个名叫Pig的游戏:用户对抗计算机,每轮玩家筛一对骰子并将点数相加,谁先达到一百谁赢。当筛出一个一此轮点数不计并换玩家,如果一对骰子都是一,则之前所有点数都清零,并失去骰子的控制权。每轮玩家都有权选择是否筛骰子。筛骰子有点数清零的风险,不筛骰子这有可能让对方赢。设置计算机在点数达到20或以上时总弃权。

这游戏设计的,电脑哪有胜算啊,点数到了20就全都放弃了,永远也到不了100啊。以下是代码:

package com.sino.baiduzhidao.throwDie;

import java.util.Random;
/**
 * 骰子
 * @author Yanghl
 *
 */
public class Die implements Runnable {
private Random r;
private int point;
public Die() {
r = new Random();
}

/**
 * 获得骨子点数
 * @return 骰子点数
 */
public int getPoint() {
return point;
}

@Override
public void run() {
//设置骰子点数为1-6的随机数
point = r.nextInt(6) + 1;
System.out.print(point + " ");
}
}

package com.sino.baiduzhidao.throwDie;

public class PairOfDice {
/**
 * 骰子1
 */
private Die die1;
/**
 * 骰子2
 */
private Die die2;

public PairOfDice() {
die1 = new Die();
die2 = new Die();
}

/**
 * 掷出两个骰子,并打印结果
 */
public void throwDie() {
System.out.print("骰子点数:");
//启用两个线程,分别转动两个骰子,模拟两个骰子同时被掷出
Thread t1 = new Thread(die1);
Thread t2 = new Thread(die2);
t1.start();
t2.start();

try {
//等待骰子结束
t1.join();
t2.join();
System.out.println();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

/**
 * 两个骰子点数都为6
 * @return
 */
public boolean isDouble6() {
if(die1.getPoint() == 6 && die2.getPoint() == 6) {
return true;
}
return false;
}

/**
 * 只有一个骰子点数为1
 * @return
 */
public boolean isSingle1() {
if(die1.getPoint() == 1 || die2.getPoint() == 1) {
if(isDouble1()) {
return false;
}
return true;
}
return false;
}

/**
 * 两个骰子点数都为1
 * @return
 */
public boolean isDouble1() {
if(die1.getPoint() == 1 && die2.getPoint() == 1) {
return true;
}
return false;
}

/**
 * 获取两个骰子点数总和
 * @return
 */
public int getPointNum() {
return die1.getPoint() + die2.getPoint();
}

public Die getDie1() {
return die1;
}

public Die getDie2() {
return die2;
}
}

package com.sino.baiduzhidao.throwDie;

public class BoxCars {
public static void main(String[] args) {
PairOfDice pod = new PairOfDice();
int times = 0;
for(int i=0;i<100;i++) {
pod.throwDie();
if(pod.isDouble6()) {
times ++;
}
}
System.out.println("掷骰子100次,都是6的次数:" + times);
}
}

以下是人机大战的代码:

package com.sino.baiduzhidao.throwDie;

/**
 * 玩家类
 */
public class Player {
/**
 * 玩家名称
 */
private String name;
/**
 * 掷骰子总数
 */
private int throwTimes;
/**
 * 总点数
 */
private int pointNum;

public Player(String name) {
this.name = name;
this.pointNum = 0;
this.throwTimes = 0;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getThrowTimes() {
return throwTimes;
}

public void setThrowTimes(int throwTimes) {
this.throwTimes = throwTimes;
}

public int getPointNum() {
return pointNum;
}

public void setPointNum(int pointNum) {
this.pointNum = pointNum;
}

}

package com.sino.baiduzhidao.throwDie;

import java.util.Scanner;

public class Pig {
/**
 * 电脑默认最大点数,超过后均放弃掷骰子
 */
private static int COMPUTER_DEFAULT_MAX_POINT = 20;
/**
 * 目标分数
 */
private static int TARGET_POINT = 100;
/**
 * 一对骰子
 */
private PairOfDice pod;
/**
 * 玩家
 */
private Player player;
/**
 * 电脑
 */
private Player computer;

/**
 * 构造函数,初始化骰子
 */
public Pig() {
pod = new PairOfDice();
player = new Player("player");
computer = new Player("computer");
}

/**
 * 开始游戏
 */
public void startPlay() {
while(true) {
//先玩家,后电脑
System.out.println("-----------------玩家---------------");
if(isThrowDie()) {
if(playerThrow(player)) {
break;
}
} else {
System.out.println("放弃掷骰子");
}
System.out.println();

System.out.println("-----------------电脑---------------");
if(computer.getPointNum() < COMPUTER_DEFAULT_MAX_POINT) {
if(playerThrow(computer)) {
break;
}
} else {
System.out.println("放弃掷骰子");
}
System.out.println();
}
}

/**
 * 玩家掷骰子,并返回玩家总分是否达到TARGET_POINT
 * @return 
 */
private boolean playerThrow(Player p) {
pod.throwDie();
//掷骰子次数 +1
p.setThrowTimes(p.getThrowTimes() + 1);

if(pod.isDouble1()) {
//掷出两个1,分数清0
p.setPointNum(0);
System.out.println("两个点数均为1,总分清0");
} else if(pod.isSingle1()) {
//掷出单个1,不加分
System.out.println("有一个点数为1,不能计入总分");
} else {
p.setPointNum(p.getPointNum() + pod.getPointNum());
}
System.out.println("当前总分:" + p.getPointNum());

//判断玩家总分是否达到TARGET_POINT
if(p.getPointNum() >= TARGET_POINT) {
System.out.println("Game Over! " + p.getName() + " win!");
return true;
} else {
return false;
}
}

/**
 * 选择是否掷骰子
 * @return 
 */
private boolean isThrowDie() {
Scanner sc = new Scanner(System.in);
System.out.print("选择是否掷骰子(Y:掷骰子; N:放弃):");
while(true) {
String in = sc.nextLine();
if("y".equalsIgnoreCase(in)) {
return true;
} else if("n".equalsIgnoreCase(in)) {
return false;
} else {
System.out.print("请选择Y/N:");
}
}
}

/**
 * @param args
 */
public static void main(String[] args) {
new Pig().startPlay();
}

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-07
拿个比较能看清看明白的需求来吧 这是小游戏来的啊 不简单哦
相似回答