问题:求一个剪刀石头布的源程序 可以三个人玩,如果一个人赢了另外两个,这个人得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不用很复杂,简单显示每局得分就可以了 程序好的话,我再加分,先谢谢大家了这个问题有好的意见或
建议,请留言
|