package edu.xcdq.Demo413;
import java.util.Random;
import java.util.Scanner;
public class CaiQuanGameDemo {
Scanner input;
Random random;
GamePlayer c_cp;
GamePlayer g_cp;
int sum = 0;
{
//創(chuàng)建電腦的角色
c_cp= new GamePlayer();
input = new Scanner(System.in);
//創(chuàng)建玩家的角色
g_cp = new GamePlayer();
random = new Random();
}
public static void main(String[] args) {
CaiQuanGameDemo gd = new CaiQuanGameDemo();
gd.gameStart();
}
//定義一個視圖
public void gameStart() {
System.out.println("******************");
System.out.println("*****猜拳游戲******");
System.out.println("******************");
System.out.println("出拳規(guī)則:1嘲驾、剪刀 2、石頭 3迹卢、布");
System.out.println("請選擇對方角色:1辽故、劉備 2、孫權(quán) 3腐碱、曹操");
int n = input.nextInt();
//調(diào)用電腦角色選擇的方法
crole(n);
System.out.println(c_cp.getName());
System.out.println("請輸入你的姓名:");
g_cp.setName(input.next());
System.out.println(g_cp.getName()+" VS "+c_cp.getName());
while(true) {
System.out.println("請問是否開始游戲 Y/N");
String s = input.next();
if(s.equals("Y") || s.equals("y")) {
//進(jìn)入游戲
System.out.println("進(jìn)入游戲");
gameInto();
break;
}else if(s.equals("N") || s.equals("n")){
//游戲結(jié)束誊垢,或退出游戲
System.out.println("退出游戲");
gameOver();
break;
}else {
//繼續(xù)選擇
}
}
}
//進(jìn)入游戲
public void gameInto() {
System.out.println("請出拳:1、剪刀 2症见、石頭 3彤枢、布(請輸入對應(yīng)的數(shù)字)");
int go = input.nextInt();
//把手勢存入到玩家角色的對象中
g_cp.setGesture(go);
//電腦的手勢需要隨機(jī)
c_cp.setGesture(random.nextInt(3)+1);
//根據(jù)數(shù)字顯示對應(yīng)的手勢
System.out.println("你出的是:"+getStringCestore(g_cp.getGesture()));
System.out.println(c_cp.getName()+"出拳的是:"+getStringCestore(c_cp.getGesture()));
jieGuo();
while(true) {
System.out.println("是否開始下一局:Y/N");
String s = input.next();
if(s.equals("Y") || s.equals("y")) {
//遞歸,自己調(diào)用自己
gameInto();
break;
}else if(s.equals("N") || s.equals("n")) {
gameOver();
break;
}
}
}
/*電腦贏
* 1- 2 ==-1
* 2- 3 ==-1
* 3- 1 ==2
*/
//進(jìn)行每次出拳的結(jié)果判斷
public void jieGuo() {
sum++;
//用玩家的出拳-電腦的出拳
int i =g_cp.getGesture() - c_cp.getGesture();
if(i == -1 || i==2) {
System.out.println("這一句局"+c_cp.getName()+"贏了");
c_cp.setScore(c_cp.getScore()+1);//在對象中把電腦分?jǐn)?shù)+1
}else if(i ==0) {
System.out.println("這一句局你們不相上下");
}else {
System.out.println("這一句局"+g_cp.getName()+"贏了,^_^");
g_cp.setScore(g_cp.getScore()+1);//在對象中把玩家分?jǐn)?shù)+1
}
}
//根據(jù)int類型的數(shù)字返回字符串類型的手勢
public String getStringCestore(int gestore) {
if(gestore == 1) {
return "剪刀";
}else if(gestore ==2) {
return "石頭";
}else {
return "布";
}
}
//游戲結(jié)束筒饰,計算最終結(jié)果
public void gameOver() {
System.out.println(g_cp.getName()+" VS "+c_cp.getName());
System.out.println("對戰(zhàn)次數(shù):"+sum+"\n");
System.out.println("姓名\t得分");
System.out.println(c_cp.getName()+"\t"+c_cp.getScore());
System.out.println(g_cp.getName()+"\t"+g_cp.getScore()+"\t");
//結(jié)果
if(c_cp.getScore() == g_cp.getScore()) {
System.out.println("呵呵,和電腦那個廢物一樣");
}else if(c_cp.getScore() < g_cp.getScore()){
System.out.println("哼壁晒,你有什么可豪橫的");
}else {
System.out.println("果然瓷们,你就是個廢物");
}
}
//定義一個方法用來判定輸入的角色是那一個
public void crole(int n) {
if(n ==1) {
c_cp.setName("劉備");
}else if(n == 2) {
c_cp.setName("孫權(quán)");
}else if(n == 3) {
c_cp.setName("曹操");
}
}
}
/**
* 人機(jī)的實(shí)體類
* @author Hardy
*
*/
class GamePlayer {
private String name;//名字
private int gesture;//出拳
private int score;//分?jǐn)?shù)
public GamePlayer(String name, int gesture, int score) {
super();
this.name = name;
this.gesture = gesture;
this.score = score;
}
public GamePlayer() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGesture() {
return gesture;
}
public void setGesture(int gesture) {
this.gesture = gesture;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "GamePlayer [name=" + name + ", gesture=" + gesture + ", score=" + score + "]";
}
}