人機猜拳小游戲,只要用到的java面向對象的思維模式。本游戲中有游戲玩家和計算機兩個參與者。玩家和計算機都有的屬性是姓名和得分。共分為4個類。下圖是游俠小程序控制臺截圖:
以下是源代碼: Computer.java類
Game.java類。包含游戲的比賽核心代碼
package com.whf.guess.game;import java.util.Scanner;/** * @author :辰 * @version * 創建時間:2017年2月24日 上午8:37:06 * */public class Game<computer> { Person person ; //甲方 Computer computer;// 乙方 int count;// 對戰次數 public void inital(){ person = new Person(); computer = new Computer(); count = 0; } //開始游戲 public void startGame(){ System.err.println("-------------歡迎進入猜拳游戲---------------"); System.out.println(); System.out.println("******************************************"); System.out.println("——————出拳規則:1.剪刀 2.石頭 3.布——————————"); System.out.println("-------------》》》猜拳開始《《《------------"); System.err.println("-------------------------------------------"); Scanner input = new Scanner(System.in); String exit="n";//退出系統 do { inital();//初始化 //選擇對手 System.out.println("選擇你的對手1.馬云 2.馬化騰 3.馬大姐 "); int role = input.nextInt(); if (role ==1) { computer.name="馬云"; }else if (role == 2) { computer.name="馬化騰"; }else if (role == 3) { computer.name="馬大姐"; } //游戲玩家輸入姓名 System.out.println("請輸入你的姓名:"); person.name=input.next(); System.out.println(person.name+" VS "+computer.name+"對戰/n"); //開始游戲 System.out.println("要開始嗎?(y/n)"); //開始一局游戲 String start = input.next(); int perFist;//游戲用戶出拳 int comFist;//計算機出拳 while (start.equals("y")) { //出拳 perFist = person.showFist(); comFist = computer.showFist(); //判斷勝負 if ((perFist==1&&comFist==1)||(perFist==2&&comFist==2)||(perFist==3&&comFist==3)) { System.err.println("結果:和局,真衰!/n");//平局 }else if ((perFist==2&&comFist==1)||(perFist==3&&comFist==2)||(perFist==1&&comFist==3)) { System.err.println("結果:你贏了,真帥!/n");//勝利 person.score++; }else { System.err.println("結果是:^_^,你輸了,下載再戰!/n");//計算機贏 computer.score++; } count++; System.out.println("是否進行下一輪(y/n)"); start = input.next(); } //顯示結果 showResult(); System.out.println("/n要開始下一局嗎?(y/n):"); exit = input.next(); System.out.println(); } while (!exit.equals("n")); System.out.println("系統退出"); } private void showResult() { // TODO Auto-generated method stub //顯示對戰次數 System.out.println("------------------------------"); System.out.println(computer.name+" VS "+person.name); System.out.println("對戰次數:"+count); //顯示最終得分 System.out.println("/n姓名/t得分"); System.out.println(person.name+"/t"+person.score); System.out.println(computer.name+"/t"+computer.score+"/n"); //顯示對戰結果 int result = calcResult(); if (result==1) { System.err.println("結果:打成平手,下次再和你一分高下"); }else if (result==2) { System.err.println("結果:恭喜恭喜你贏得比賽"); }else { System.err.println("結果:不氣餒,下次再來");//計算機贏 } System.out.println("--------------------------"); } //計算比賽結果 private int calcResult() { // TODO Auto-generated method stub if (person.score == computer.score) { return 1;//戰平 }else if (person.score > computer.score) { return 2;//游戲玩家贏 }else { return 3;//惜敗 } }}StartGuess.java類
package com.whf.guess.game;/** * @author :辰 * @version * 創建時間:2017年2月24日 上午9:35:36 * */public class StartGuess { public static void main(String[] args) { Game game = new Game(); game.inital(); game.startGame(); }}以上就是完整的人機猜拳java面向對象的小游戲源碼.
新聞熱點
疑難解答