java項(xiàng)目實(shí)現(xiàn)猜拳小游戲
本文實(shí)例為大家分享了java實(shí)現(xiàn)猜拳小游戲的具體代碼,供大家參考,具體內(nèi)容如下
項(xiàng)目名稱
猜拳小游戲
項(xiàng)目描述
玩家與電腦進(jìn)行猜拳游戲,玩家行為采用輸入方式,電腦行為采用隨機(jī)形式。
代碼實(shí)現(xiàn)
測試類
public class Test { public static void main(String[] args) { Game game = new Game(); game.start(); }}
主類:實(shí)現(xiàn)主方法
public class Game { private People people; private Computer computer; public Game(){ people = new People('zs'); computer = new Computer('computer'); } public void start(){ boolean flag = true; while (flag) { System.out.println('開始游戲:'); int count = 0; while (count < 3) {String peopleFist = people.doFist();String comFist = computer.doFist();//people贏if (peopleFist.equals('石頭') && comFist.equals('剪刀') || peopleFist.equals('剪刀') && comFist.equals('布') || peopleFist.equals('布') && comFist.equals('石頭')) { System.out.println(people.getName() + '贏了'); people.addScore(1);} else if (peopleFist.equals('石頭') && comFist.equals('石頭') || peopleFist.equals('剪刀') && comFist.equals('剪刀') || peopleFist.equals('布') && comFist.equals('布')) { System.out.println('平局');} else if (peopleFist.equals('石頭') && comFist.equals('布') || peopleFist.equals('剪刀') && comFist.equals('石頭') || peopleFist.equals('布') && comFist.equals('剪刀')) { System.out.println(computer.getName() + '贏了'); computer.addScore(1);}count++; } if (people.getScore() > computer.getScore()) {System.out.println(people.getName() + '贏了 ' + people.getScore() + ':' + computer.getScore()); } else if (people.getScore() == computer.getScore()) {System.out.println('平局'); } else if (people.getScore() < computer.getScore()) {System.out.println(computer.getName() + '贏了 ' + computer.getScore() + ':' + people.getScore()); } System.out.println('是否開始新游戲:'); Scanner scanner = new Scanner(System.in); String str = scanner.next(); if (str.equals('否')) {flag = false; }else {people.setScore();computer.setScore(); } } }}
玩家
public class People { private String name; private int score; public People(String name){ this.name = name; score = 0; } public String getName(){ return name; } public void addScore(int score){ this.score += score; } public int getScore(){ return score; } public int setScore(){ this.score = 0; return score; } public String doFist(){ System.out.println('請出拳:'); Scanner scanner = new Scanner(System.in); String fist = scanner.next(); return fist; }}
電腦
public class Computer { private String name; private int score; public Computer(String name){ this.name = name; score = 0; } public String getName(){ return name; } public void addScore(int score){ this.score += score; } public int getScore(){ return score; } public int setScore(){ this.score = 0; return score; } public String doFist(){ Random random = new Random(); int n = random.nextInt(3); String fist; if(n == 0){ fist = '石頭'; }else if(n == 1){ fist = '剪刀'; }else { fist = '布'; } System.out.println('對方出的是:'+fist); return fist; }}
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
C++經(jīng)典小游戲匯總
python經(jīng)典小游戲匯總
python俄羅斯方塊游戲集合
JavaScript經(jīng)典游戲 玩不停
javascript經(jīng)典小游戲匯總
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python 實(shí)現(xiàn)rolling和apply函數(shù)的向下取值操作2. CSS代碼檢查工具stylelint的使用方法詳解3. 淺談python多線程和多線程變量共享問題介紹4. Python如何批量獲取文件夾的大小并保存5. vue3?Error:Unknown?variable?dynamic?import:?../views/的解決方案6. python利用platform模塊獲取系統(tǒng)信息7. react axios 跨域訪問一個或多個域名問題8. Python的Tqdm模塊實(shí)現(xiàn)進(jìn)度條配置9. Python 多線程之threading 模塊的使用10. WML語言的基本情況
