Java入門案列之猜拳小游戲
最近正在學習Java基礎知識,終于完成了第一個小demo,記錄下來,算是一個小總結與整理,也希望可以幫助到別人。
先看看我寫了哪些類:
Player:玩家類;
ComputerPlayer:機器人玩家類,主要用來實現(xiàn)機器人隨機出拳;
Game:游戲類,主要實現(xiàn)游戲規(guī)則的邏輯,以及正式游戲的邏輯;
TestGuessBox:代碼測試類;
Player類:
//玩家類 public class Player { private String name; //玩家昵稱 private int score; //玩家積分 private String box; //玩家出的 //玩家構造函數(shù),傳入玩家昵稱與玩家初始積分 Player(String name,int score){ this.name=name; this.score=score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public String getBox() { return box; } public void setBox(String box) { this.box = box; } }
ComputerPlayer類
public class ComputerPlayer extends Player { //機器人玩家構造函數(shù),傳入機器人昵稱與初始積分 ComputerPlayer(String name, int score) { super("機器人"+name, score); // TODO Auto-generated constructor stub } /** * 實現(xiàn)機器人玩家隨機出拳的邏輯 */ void punch() { String[] box = {"剪刀","石頭","布"}; int index =(int) (Math.random()*3); this.setBox(box[index]); } }
Game類
import java.util.Scanner; public class Game { Player p; //玩家 ComputerPlayer cp; //機器人玩家 //構造函數(shù),得到一個玩家和一個機器人玩家 Game(Player p, ComputerPlayer cp) { this.p = p; this.cp = cp; } //開始游戲 void start() { System.out.println("玩家"+p.getName()+ "和"+cp.getName()+"開始游戲咯"); System.out.println("您的初始積分為:"+p.getScore()+ "\n"+cp.getName()+"的積分是:"+cp.getScore()); Scanner sc=new Scanner(System.in); while(true) { System.out.println("請出拳(剪刀石頭布,exit退出游戲):"); String pbox=sc.next(); if(filter(pbox)) { //過濾器 if(pbox.equals("exit")) { //退出游戲 break; }else { p.setBox(pbox); cp.punch(); System.out.println("您出了:"+p.getBox()); System.out.println(cp.getName()+"出了:"+cp.getBox()); int result = ruler(p,cp); if(result>0) { System.out.println("您贏了,贏得10積分"); p.setScore(p.getScore()+10); cp.setScore(cp.getScore()-10); } else if(result<0) { System.out.println("您輸了,扣除10積分"); p.setScore(p.getScore()-10); cp.setScore(cp.getScore()+10); } else { System.out.println("您和機器人打平了!"); } } } else { System.out.println("輸入了無法識別的關鍵字,請重新輸入:"); continue; //退出本次循環(huán),進入下一次循環(huán) } } System.out.println("本輪結束,積分情況如下:"); System.out.println("您的當前積分:"+p.getScore()); System.out.println(cp.getName()+"的當前積分:" +cp.getScore()); } /** * 游戲規(guī)則 * * @param p1 玩家1 * @param cp2 機器玩家2 * @return 0為打平,1為玩家贏,-1為機器玩家贏 */ int ruler(Player p1, Player cp2) { if (p1.getBox().equals("剪刀")) { if (cp2.getBox().equals("石頭")) return -1; else if (cp2.getBox().equals("布")) return 1; } else if (p1.getBox().equals("石頭")) { if (cp2.getBox().equals("剪刀")) return 1; else if (cp2.getBox().equals("布")) return -1; } else if (p1.getBox().equals("布")) { if (cp2.getBox().equals("剪刀")) return -1; else if (cp2.getBox().equals("石頭")) return 1; } return 0; } /** * 過濾器 * @param s 需要過濾的文字 * @return */ boolean filter(String s) { if (s.equals("剪刀") || s.equals("石頭") || s.equals("布") || s.equals("exit")) { return true; } else return false; } }
TestGuessBox類
public class TestGuessBox { public static void main(String[] args) { // TODO Auto-generated method stub Player p =new Player("小七月",100); ComputerPlayer cp=new ComputerPlayer("小丑八怪",100); Game game=new Game(p,cp); game.start(); } }
很簡單,理清了思路就很容易寫了。如果有錯誤請指出。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java利用Dijkstra和Floyd分別求取圖的最短路徑
本文主要介紹了圖的最短路徑的概念,并分別利用Dijkstra算法和Floyd算法求取最短路徑,最后提供了基于鄰接矩陣和鄰接表的圖對兩種算法的Java實現(xiàn)。需要的可以參考一下2022-01-01SpringBoot整合WebSocket實現(xiàn)實時通信功能
在當今互聯(lián)網(wǎng)時代,實時通信已經(jīng)成為了許多應用程序的基本需求,而WebSocket作為一種全雙工通信協(xié)議,為開發(fā)者提供了一種簡單、高效的實時通信解決方案,本文將介紹如何使用SpringBoot框架來實現(xiàn)WebSocket的集成,快速搭建實時通信功能,感興趣的朋友可以參考下2023-11-11SpringBoot詳細講解靜態(tài)資源導入的實現(xiàn)
在Web開發(fā)過程中,我們需要接觸許多靜態(tài)資源,如CSS、JS、圖片等;在之前的開發(fā)中,這些資源都放在Web目錄下,用到的時候按照對應路徑訪問即可。不過在SpringBoot項目中,沒有了Web目錄,那這些靜態(tài)資源該放到哪里去,又要如何訪問呢?這就是我們要講的靜態(tài)資源導入2022-05-05Java前后端分離的在線點餐系統(tǒng)實現(xiàn)詳解
這是一個基于SpringBoot+Vue框架開發(fā)的在線點餐系統(tǒng)。首先,這是一個前后端分離的項目。具有一個在線點餐系統(tǒng)該有的所有功能,感興趣的朋友快來看看吧2022-01-01