您当前的位置: 百科371电脑/网络JAVA相关 → 电脑/网络内容 退出登录 用户管理 用户注册
本类热门文章
相关文章
中原建材网
求剪刀石头布程序源程序 好的话我再加分

减小字体 增大字体

问题:求一个剪刀石头布的源程序 可以三个人玩,如果一个人赢了另外两个,这个人得2分,如果其中的两个人赢了另外一个,这两个人都得一分,要有interface, 可以显示每个人的得分,interface不用很复杂,简单显示每局得分就可以了 程序好的话,我再加分,先谢谢大家了  

import java.util.ArrayList;


public class Game {
ArrayList<GeneralPlayer> players = new ArrayList<GeneralPlayer>();
void addPlayer(GeneralPlayer p){
players.add(p);
}
void play(){
for(int i=0; i<players.size(); i++){
for(int j=0; j<players.size(); j++){
if(i!=j){
players.get(i).playWith(players.get(j));
}
}
}
}
void printResult(){
for(int i=0; i<players.size(); i++)
System.out.println(players.get(i));
}
public static void main(String[] args) {
Game g = new Game();
g.addPlayer(new Player("小明"));
g.addPlayer(new Player("小阳"));
g.addPlayer(new Player("小莉"));
g.addPlayer(new Player("小桃"));
g.addPlayer(new Player("那人"));

int count = 10;//玩10局,每个人将玩 局数*(人数-1)次
for(;count>0;count--)
g.play();
g.printResult();
}
}
/**
可以三个人玩,如果一个人赢了另外两个,这个人得2分,如果其中的两个人赢了另外一个,这两个人都得一分,
要有interface, 可以显示每个人的得分,interface不用很复杂,简单显示每局得分就可以了
* */
interface GeneralPlayer{
int getCount();
void plusCount();
int getScore();
void plusScore();
void playWith(GeneralPlayer p);
Action nextAction();
}
enum Action{
石头,剪刀,布,
}

class Player implements GeneralPlayer{
private String name;
private int score,count;
Player(String name){
this.name=name;
}

String getName(){return name;}

public int getScore() {
return score;
}
public Action nextAction() {
Action[] aa = Action.values();
return aa[(int)(Math.random()*aa.length)];
}
public void plusScore() {
++score;
}
public void playWith(GeneralPlayer p) {
Action a = this.nextAction();
Action b = p.nextAction();
int s=0;
switch(a){
case 石头:
s=b==Action.石头?-1:b==Action.剪刀?1:0;
break;
case 剪刀:
s=b==Action.石头?0:b==Action.剪刀?-1:1;
break;
case 布:
s=b==Action.石头?1:b==Action.剪刀?0:-1;
break;
}
if(s==-1){
replay(p);
return;
}
else if(s==1)this.plusScore();
else p.plusScore();
this.plusCount();
}

private void replay(GeneralPlayer p) {
this.playWith(p);
}

public String toString(){
return name+" 玩了 "+count+" 次, 赢 "+score+" 分.";
}
public int getCount() {
return count;
}
public void plusCount() {++count;}
}

如果你对求一个剪刀石头布的源程序 可以三个人玩,如果一个人赢了另外两个,这个人得2分,如果其中的两个人赢了另外一个,这两个人都得一分,要有interface, 可以显示每个人的得分,interface不用很复杂,简单显示每局得分就可以了 程序好的话,我再加分,先谢谢大家了这个问题有好的意见或 建议,请留言
[] [返回上一页] [打 印]
电脑/网络评论 (评论内容只代表网友观点,与本站立场无关!)

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码

中原建材网 简单版 站长QQ:382546553