此聊天機器人是通過用戶輸入的陳述句識別陳述句中的關鍵詞進行隨機回答回答語庫中的陳述句,小天聊天機器人能夠回答用戶的"小天","在嗎","你好","撒嬌","挖掘機","音樂","歌","拜拜","再見","不聊了","退出"陳述句,同時還可以回答用戶"時間"并回傳實時時間,還可以和用戶玩猜數小游戲,同時添加了斗地主功能(注:斗地主的發牌,洗牌,搶地主都已經完成但是人機玩家的未有完成),
package xiaotian;
import java.util.*;
//import java.util.logging.*;
import java.text.*;
import java.security.*;
import java.util.ArrayList;
import java.util.Collections;
//import org.apache.http.*;
public class Test {
public static void main(String[] args){
//創建控制臺輸入類
Scanner input = new Scanner(System.in);
//創建資料處理類
DataProcessing data = new DataProcessing();
//創建回答處理演算法類
Handle handle = new Handle();
//創建人物物件類
CharacterObject charact = new CharacterObject();
/*****************************************************************/
//回圈運行系統
while (true)
{
charact.empty();
charact.me();
//控制臺輸入
String str = input.next();
//傳出資料
data.keyWord(str);
data.combination();
//決議處理演算法
handle.anlysis();
/*****************************************************************/
}
}
}
//資料處理類
class DataProcessing
{
//詢問關鍵詞
boolean start1;
boolean start2;
//你好關鍵詞
boolean hello1;
//撒嬌關鍵詞
boolean spoiled1;
//挖掘機廣告語
boolean excavatingMachinery1;
//斗地主關鍵詞類
boolean fightAgainstLandlords1;
boolean fightAgainstLandlords2;
//音樂關鍵詞
boolean music1;
boolean music2;
//退出關鍵詞
boolean quit1;
boolean quit2;
boolean quit3;
//猜數游戲關鍵詞
boolean guessingGame1;
//時間關鍵詞
boolean time1;
boolean time2;
boolean time3;
//資料區塊
public static boolean start;//傳輸詢問關鍵詞識別結果
public static boolean hello;//傳輸你好關鍵詞識別結果
public static boolean music;//傳輸音樂關鍵詞識別結果
public static boolean quit ;//傳輸退出關鍵詞識別結果
public static boolean guessingGame;//傳輸猜數游戲關鍵詞識別結果
public static boolean time;//傳輸時間關鍵詞識別結果
public static boolean spoiled;//傳輸撒嬌關鍵詞識別結果
public static boolean fightAgainstLandlords;//傳輸斗地主關鍵詞識別結果
public static boolean excavatingMachinery;//傳輸挖掘機關鍵詞識別結果
//關鍵字識別方法
public void keyWord(String str)
{
//詢問關鍵詞識別語塊
//**************************************//
boolean start1 = str.contains("小天");
this.start1 = start1;
boolean start2 = str.contains("在嗎");
this.start2 = start2;
//**************************************//
//你好關鍵詞識別語塊
//**************************************//
boolean hello1 = str.contains("你好");
this.hello1 = hello1;
//**************************************//
//撒嬌關鍵詞識別語塊
//**************************************//
boolean spoiled1 = str.contains("撒嬌");
this.spoiled1 = spoiled1;
//***************************************//
//挖掘機關鍵詞識別語塊
//***************************************//
boolean excavatingMachinery1 = str.contains("挖掘機");
this.excavatingMachinery1 = excavatingMachinery1;
//***************************************//
//斗地主關鍵詞識別語塊
//***************************************//
boolean fightAgainstLandlords1 = str.contains("斗地主");
boolean fightAgainstLandlords2 = str.contains("打牌");
this.fightAgainstLandlords1 = fightAgainstLandlords1;
this.fightAgainstLandlords2 = fightAgainstLandlords2;
//***************************************//
//音樂關鍵詞識別語塊
//**************************************//
boolean music1 = str.contains("音樂");
this.music1 = music1;
boolean music2 = str.contains("歌");
this.music2 = music2;
//**************************************//
//退出關鍵詞識別語塊
//**************************************//
boolean quit1 = str.contains("拜");
this.quit1 = quit1;
boolean quit2 = str.contains("退出");
this.quit2 = quit2;
boolean quit3 = str.contains("不聊了");
this.quit3 = quit3;
//**************************************//
//猜數游戲
//**************************************//
boolean guessingGame1 = str.contains("猜數");
this.guessingGame1 = guessingGame1;
//**************************************//
//時間
//*************************************//
boolean time1 = str.contains("時間");
this.time1 = time1;
boolean time2 = str.contains("幾點");
this.time2 = time2;
boolean time3 = str.contains("報時");
this.time3 = time3;
//*************************************//
}
//關鍵詞組合判斷方法
public void combination()
{
//詢問關鍵詞語塊組合判斷
boolean start = start1 || start2;
this.start = start;
//你好關鍵詞語塊組合判斷
boolean hello = hello1 || false;
this.hello = hello;
//撒嬌關鍵詞語塊組合判斷
boolean spoiled = spoiled1 || false;
this.spoiled = spoiled;
//挖掘機關鍵詞語塊組合判斷
boolean excavatingMachinery = excavatingMachinery1 || false;
this.excavatingMachinery = excavatingMachinery;
//斗地主關鍵詞語塊組合判斷
boolean fightAgainstLandlords = fightAgainstLandlords1 || fightAgainstLandlords2;
this.fightAgainstLandlords = fightAgainstLandlords;
//音樂關鍵詞語塊組合判斷
boolean music = music1 || music2;
this.music = music;
//退出關鍵詞語塊組合判斷
boolean quit = quit1 || quit2 || quit3;
this.quit = quit;
//猜數游戲關鍵詞語塊組合判斷
boolean guessingGame = guessingGame1 || false;
this.guessingGame = guessingGame;
//時間關鍵詞語塊組合判斷
boolean time = time1 || time2 || time3;
this.time = time;
}
}
//回答處理演算法類
class Handle
{
/**********創建物件區**********/
Random random = new Random(); /*創建亂數類物件*/
Answer an = new Answer(); /*創建回答陳述句語塊類物件*/
CharacterObject character = new CharacterObject();/*創建人物物件*/
DataProcessing data = new DataProcessing();//創建資料處理類
/****************************/
//創建決議演算法方法
public void anlysis()
{
/***接受資料區塊***/
boolean music = data.music;//音樂
boolean quit = data.quit;//退出
boolean start = data.start;//詢問
boolean guessingGame= data.guessingGame;//猜數游戲
boolean time = data.time;//時間
boolean hello = data.hello;//你好
boolean spoiled = data.spoiled;//撒嬌
boolean excavatingMachinery = data.excavatingMachinery;
boolean fightAgainstLandlords = data.fightAgainstLandlords;//斗地主
/****************/
switch (1)
{
case 1:
//基礎陳述句區******************************/
/*音樂關鍵詞識別*/
if (music == true)
{
int digit = random.nextInt(an.music.length);
character.smallDay();
System.out.println(an.music[digit]);
break;
}
/*退出關鍵詞識別*/
if (quit == true)
{
int digit = random.nextInt(an.music.length);
character.smallDay();
System.out.println(an.quit[digit]);
break;
}
/*你好關鍵詞識別*/
if (hello == true)
{
int digit = random.nextInt(an.hello.length);
character.smallDay();
System.out.println(an.hello[digit]);
break;
}
/*撒嬌關鍵詞識別*/
if (spoiled == true)
{
int digi = random.nextInt(an.spoiled.length);
character.smallDay();
System.out.println(an.spoiled[digi]);
break;
}
/*挖掘機關鍵詞識別*/
if (excavatingMachinery == true)
{
int digi = random.nextInt(an.excavatingMachinery.length);
character.smallDay();
System.out.println(an.excavatingMachinery[digi]);
break;
}
//擴展功能區*****************************/
//猜數游戲關鍵詞識別
if (guessingGame == true)
{
character.smallDay();
int digi = random.nextInt(an.guessingGame1.length);
System.out.println(an.guessingGame1[digi]);
//創建猜數游戲演算法類
GuessingGame guessing = new GuessingGame();
guessing.rule();
guessing.algorithm();
break;
}
/*時間關鍵詞識別*/
if (time == true)
{
character.smallDay();
int digi = random.nextInt(an.time.length);
System.out.println(an.time[digi]);
//創建時間類
Time timeDate = new Time();
timeDate.date();
break;
}
/*斗地主關鍵詞識別*/
if (fightAgainstLandlords == true)
{
START_GAME START_GAME = new START_GAME();
START_GAME.START_GAME();
break;
}
//低級處理關鍵詞區**************************/
/*詢問關鍵詞識別*/
if (start == true)
{
int digit = random.nextInt(an.start.length);
character.smallDay();
System.out.println(an.start[digit]);
break;
}
default:
/*沒有識別到關鍵詞*/
int digit = random.nextInt(an.no.length);
character.smallDay();
System.out.println(an.no[digit]);
}
}
}
//回答陳述句語塊
class Answer
{
//基礎回答陳述句
String music[] = {"想聽什么音樂🎶","哦,好啊👌","播放什么呢🎧","我喜歡聽鄧紫棋的歌,你呢😃"}; /*音樂回答語塊*/
String hello[] = {"你好啊,我是小天","你好*?(?′?`?)?"};/*你好回答語塊*/
String spoiled[] = {"寶寶生氣了😡","氣死寶寶了😠","寶寶好開心啊😋","寶寶要抱抱(ˊ?ˋ*)?","親親^3^"};/*撒嬌回答語塊*/
String excavatingMachinery[] = {"中國山東找找藍翔😂😂","山東藍翔😎"};
String quit[] = {"嗯,再見😃","拜拜ノBye~!😋","別忘了我啊,拜👌"}; /*退出回答語塊*/
String start[] = {"你好啊!😃","嗯,在的!😋","想我了嗎😍"}; /*詢問回答語塊*/
String no[] = {"你說什么,我沒明白🙄","能再說一遍嗎😳","沒聽清楚😭"}; /*沒有找到關鍵詞默認回答語塊*/
//擴展功能回答陳述句
/******猜數游戲******/
String guessingGame1[] = {"準備好了嗎,要開始了","開始了哦"};//開始陳述句
String guessingGame2[] = {"有點大了哦😃","您猜的數太大了,再猜猜😉"};//數大了回答陳述句
String guessingGame3[] = {"有點小了哦😃","您猜的數太小了,再猜猜😉"};//數小了回答陳述句
String guessingGame4[] = {"太棒了,猜對了😘","你真厲害👍"};
//斗地主回答陳述句
String pokerGame1[] = {"準備好了嗎,開始了","開始了哦","游戲開始"};
/******時間**********/
String time[] = {"現在的時間是","親^3^,以下是中國區時間","好的👌,現在給您報時"};
}
//人物物件
class CharacterObject
{
public void empty()
{
System.out.println();
}
public void me()
{
System.out.print("我:");
}
public void smallDay()
{
System.out.print("小天:");
}
public void tips()
{
System.out.print("小天提示:");
}
public void erNi()
{
System.out.print("二妮:");
}
}
/*********************************************************************/
//猜數游戲演算法類
class GuessingGame
{
//生成人物物件類
CharacterObject chara = new CharacterObject();
//游戲規則
public void rule()
{
chara.tips();
System.out.println("系統已經隨機生成一個0~99的整數,猜猜看是多少,你只有10次機會哦,加油^0^~");
}
//生成游戲演算法
public void algorithm()
{
/************************************/
//創建亂數類
Random random = new Random();
//創建亂數
int randomNumber = random.nextInt(100);
/************************************/
//創建控制臺輸入類
Scanner input = new Scanner(System.in);
/****************************?*******/
//創建回答陳述句語塊類
Answer an = new Answer();
for (int i = 1;i <= 10;i++)
{
//控制臺輸入
chara.tips();
System.out.println("您的數值是:");
chara.me();
int str = input.nextInt();
//判斷數值是否在可選范圍內
while (true)
{
if (str > 0)
{
if (str < 100)
{
//數值在可選擇范圍內
break;
}
else
{
//數值不在可選擇范圍內
chara.tips();
System.out.println("親您的數值不在可選性擇范圍內,請重新輸入");
System.out.println("您的數值是");
chara.me();
str = input.nextInt();
}
}
else
{
//數值不在可選擇范圍內
chara.tips();
System.out.println("親您的數值不在可選性擇范圍內,請重新輸入");
System.out.println("您的數值是");
chara.me();
str = input.nextInt();
}
}
if (str < randomNumber)
{
if (i != 10)
{
chara.tips();
int digit = random.nextInt(an.guessingGame3.length);
System.out.println(an.guessingGame3[digit]);
}
else
{
chara.tips();
System.out.println("抱歉游戲失敗!");
}
}
if (str > randomNumber)
{
if (i != 10)
{
chara.tips();
int digit = random.nextInt(an.guessingGame2.length);
System.out.println(an.guessingGame2[digit]);
}
else
{
chara.tips();
System.out.println("抱歉游戲失敗!");
}
}
if (str == randomNumber)
{
if (i != 10)
{
chara.tips();
int digit = random.nextInt(an.guessingGame4.length);
System.out.println(an.guessingGame4[digit]);
break;
}
else
{
chara.tips();
System.out.println("抱歉游戲失敗!");
}
}
}
}
}
/*********************************************************************/
//時間類
class Time
{
//創建時間類
Date date = new Date();
public void date()
{
DateFormat df1 = new SimpleDateFormat("yyyy年MM月dd日 hh時mm分ss秒 EE", Locale.CHINA);
System.out.println(df1.format(date));
}
}
/*********************************************************************/
//***********斗地主*********//
//初始化資料
class InitializationData
{
//初始化資料模塊
TreeSet<Integer> initializationData_1 = new TreeSet<Integer>();
ArrayList<String> initializationData_2= new ArrayList<String>();
GamePlayer gamePlayer = new GamePlayer();//玩家類
TransmissionPoker transmissionPoker = new TransmissionPoker();//創建傳輸資料類
public void initializationData()
{
//初始化提示語
System.out.println("初始化資料中.......................");
System.out.println("初始化資料中.......................");
System.out.println("初始化資料中.......................");
System.out.println("初始化進度: 0%");
//儲存撲克牌索引初始化
try
{
for (Integer i: gamePlayer.gamePlayer_1)
{
initializationData_1.add(i);
}
for (Integer i : initializationData_1)
{
gamePlayer.gamePlayer_1.remove(i);
}
System.out.println("初始化進度: 11.11%");
}
catch (Exception e )
{
System.out.println("初始化資料失敗!!!");
}
try
{
for (Integer i: gamePlayer.gamePlayer_2)
{
initializationData_1.add(i);
}
for (Integer i : initializationData_1)
{
gamePlayer.gamePlayer_2.remove(i);
}
System.out.println("初始化進度: 23%");
}
catch (Exception e )
{
System.out.println("初始化資料失敗!!!");
}
try
{
for (Integer i: gamePlayer.gamePlayer_3)
{
initializationData_1.add(i);
}
for (Integer i : initializationData_1)
{
gamePlayer.gamePlayer_3.remove(i);
}
System.out.println("初始化進度: 24.11%");
}
catch (Exception e )
{
System.out.println("初始化資料失敗!!!");
}
try
{
for (Integer i: gamePlayer.dipai)
{
initializationData_1.add(i);
}
for (Integer i : initializationData_1)
{
gamePlayer.dipai.remove(i);
}
System.out.println("初始化進度: 50%");
}
catch (Exception e )
{
System.out.println("初始化資料失敗!!!");
}
//儲存撲克牌初始化
try
{
for (String i : gamePlayer.gamePlayer_4)
{
initializationData_2.add(i);
}
for (String i : initializationData_2)
{
gamePlayer.gamePlayer_4.remove(i);
}
System.out.println("初始化進度: 61.11%");
}
catch (Exception e )
{
System.out.println("初始化資料失敗!!!");
}
try
{
for (String i : gamePlayer.gamePlayer_5)
{
initializationData_2.add(i);
}
for (String i : initializationData_2)
{
gamePlayer.gamePlayer_5.remove(i);
}
System.out.println("初始化進度: 70%");
}
catch (Exception e )
{
System.out.println("初始化資料失敗!!!");
}
try
{
for (String i : gamePlayer.gamePlayer_6)
{
initializationData_2.add(i);
}
for (String i : initializationData_2)
{
gamePlayer.gamePlayer_6.remove(i);
}
System.out.println("初始化進度: 87.5%");
}
catch (Exception e )
{
System.out.println("初始化資料失敗!!!");
}
try
{
for (String i : gamePlayer.dipai_p)
{
initializationData_2.add(i);
}
for (String i : initializationData_2)
{
gamePlayer.dipai_p.remove(i);
}
System.out.println("初始化進度: 93%");
}
catch (Exception e )
{
System.out.println("初始化資料失敗!!!");
}
//帶索引撲克牌初始化
//索引物件初始化
try
{
for (Integer i: transmissionPoker.lndexCard)
{
initializationData_1.add(i);
}
for (Integer i : initializationData_1)
{
transmissionPoker.lndexCard.remove(i);
}
System.out.println("初始化進度: 100%");
}
catch (Exception e )
{
System.out.println("初始化資料失敗!!!");
}
System.out.println("初始化資料成功!!!");
}
}
/*身份牌類*/
class Identity
{
RobLandlord robLandlord = new RobLandlord();//創建搶地主類
//平民身份
public void identity(int gamePlayer)
{
switch (gamePlayer)
{
case 1:
System.out.print("(地主)");
break;
case 2:
System.out.print("(平民)");
}
}
public void aHand()
{
System.out.print("(底牌)");
}
}
//玩家類
class GamePlayer
{
//存盤撲克牌索引
public static TreeSet<Integer> gamePlayer_1 = new TreeSet<Integer>();//玩家1
public static TreeSet<Integer> gamePlayer_2 = new TreeSet<Integer>();//玩家2
public static TreeSet<Integer> gamePlayer_3 = new TreeSet<Integer>();//玩家3
public static TreeSet<Integer> dipai = new TreeSet<Integer>();//底牌
//存盤撲克牌
public static ArrayList<String> gamePlayer_4 = new ArrayList<String>();//玩家1
public static ArrayList<String> gamePlayer_5 = new ArrayList<String>();//玩家2
public static ArrayList<String> gamePlayer_6 = new ArrayList<String>();//玩家3
public static ArrayList<String> dipai_p = new ArrayList<String>();//底牌
}
//生成撲克牌
class Poker
{
//創建撲克
String poker_p[] = {"3","3","3","3","4","4","4","4","5","5","5","5","6","6","6","6","7","7","7","7","8","8","8","8","9","9","9","9","S","S","S","S","J","J","J","J","Q","Q","Q","Q","K","K","K","K","A","A","A","A","2","2","2","2","小","大"};
//索引撲克
String poker[] = {"3","4","5","6","7","8","9","S","J","Q","K","A","2","小王","大王"};
}
//創建傳輸資料
class TransmissionPoker
{
//*******************************************//
//創建撲克
Poker poker = new Poker();
//創建帶索引的撲克
public static HashMap<Integer,String> allCards = new HashMap<Integer,String>();
//創建索引集合物件
public static ArrayList<Integer> lndexCard =new ArrayList<Integer>();
//******************************************//
//裝牌
public void packUp()
{
//創建索引值
int index = 0;
for (String tpoker_p :poker.poker_p)
{
//傳輸索引
allCards.put(index, tpoker_p);
//記錄索引
lndexCard.add(index);
//索引遞增
index++;
}
}
}
//洗牌
class ShuffleTheCards
{
TransmissionPoker transmissionPoker =new TransmissionPoker();
public void ShuffleTheCards()
{
//洗牌(打亂索集合中的元素)
Collections.shuffle(transmissionPoker.lndexCard);//洗牌一次
Collections.shuffle(transmissionPoker.lndexCard);//洗牌兩次
Collections.shuffle(transmissionPoker.lndexCard);//洗牌三次
Collections.shuffle(transmissionPoker.lndexCard);//洗牌四次
Collections.shuffle(TransmissionPoker.lndexCard);//洗牌五次
}
}
//發牌器
class LicensingDevice
{
//創建玩家
GamePlayer gamePlayer = new GamePlayer();
//傳輸資料
TransmissionPoker transmissionPoker = new TransmissionPoker();
//洗牌
ShuffleTheCards shuffleTheCards = new ShuffleTheCards();
//****************************************************//
public void licensingDevice()
{
for (int i = 0;i < transmissionPoker.lndexCard.size();i++)
{
if (i >= transmissionPoker.lndexCard.size() - 3)
{
//預留底牌
gamePlayer.dipai.add(transmissionPoker.lndexCard.get(i));
}
else if (i % 3 == 0)
{
//發牌給玩家1
gamePlayer.gamePlayer_1.add(transmissionPoker.lndexCard.get(i));
}
else if (i % 3 == 1)
{
//發牌給玩家2
gamePlayer.gamePlayer_2.add(transmissionPoker.lndexCard.get(i));
}
else
{
//發牌給玩家3
gamePlayer.gamePlayer_3.add(transmissionPoker.lndexCard.get(i));
}
}
/**************************************************/
//玩家1的牌
for (Integer i : gamePlayer.gamePlayer_1)
{
gamePlayer.gamePlayer_4.add(transmissionPoker.allCards.get(i));
}
//玩家2的牌
for (Integer i : gamePlayer.gamePlayer_2)
{
gamePlayer.gamePlayer_5.add(transmissionPoker.allCards.get(i));
}
//玩家3的牌
for (Integer i : gamePlayer.gamePlayer_3)
{
gamePlayer.gamePlayer_6.add(transmissionPoker.allCards.get(i));
}
//底牌
for (Integer i : gamePlayer.dipai)
{
gamePlayer.dipai_p.add(transmissionPoker.allCards.get(i));
}
}
}
//搶地主提示
class Tips
{
public void tips1()
{
System.out.println("提示語音: 是否搶地主 是1 否2");
}
}
//搶地主
class RobLandlord
{
Tips tips = new Tips();//創建提示
GamePlayer gamePlayer = new GamePlayer();
LicensingDevice LicensingDevice = new LicensingDevice();//創建發牌器
Random random = new Random(); /*創建亂數類物件*/
Scanner input = new Scanner(System.in);//創建控制臺輸入
//初始化身份牌
public static int gamePlayer_1 = 2;//定義玩家1的身份
public static int gamePlayer_2 = 2;//定義玩家2的身份
public static int gamePlayer_3 = 2;//定義玩家3的身份
public void robLandlord()
{
tips.tips1();//提示
int str = input.nextInt();//輸入
switch (str)
{
case 1:
//把底牌給玩家1
for (String i : gamePlayer.dipai_p)
{
gamePlayer.gamePlayer_4.add(i);
}
this.gamePlayer_1 = 1;//切換身份牌
System.out.println("我是地主牌");
break;
case 2:
//選擇地主
int choice = random.nextInt(2);
switch (choice)
{
case 0:
//把底牌給玩家2
for (String i : gamePlayer.dipai_p)
{
gamePlayer.gamePlayer_5.add(i);
}
this.gamePlayer_2 = 1;
System.out.println("小天是地主牌");
break;
case 1:
for (String i : gamePlayer.dipai_p)
{
gamePlayer.gamePlayer_6.add(i);
}
this.gamePlayer_3 = 1;
System.out.println("二妮是地主牌");
break;
}
break;
}
}
}
//開始游戲類
class START_GAME
{
CharacterObject CharacterObject = new CharacterObject();//創建人物物件
Random random = new Random();//創建亂數
Answer Answer = new Answer();//基礎回答陳述句
public void START_GAME()
{
/***************************************************/
/*初始化*/
InitializationData initializationData = new InitializationData();//創建初始化
initializationData.initializationData();
/*開始提示語*/
CharacterObject.smallDay();
int digi = random.nextInt(Answer.pokerGame1.length);//生成亂數
System.out.println(Answer.pokerGame1[digi]);//列印隨機提示語
/***************************************************/
/*傳輸資料*/
TransmissionPoker transmissionPoker = new TransmissionPoker();//創建傳輸資料
transmissionPoker.packUp();//呼叫
/***************************************************/
/*洗牌*/
ShuffleTheCards shuffleTheCards = new ShuffleTheCards();//洗牌
shuffleTheCards.ShuffleTheCards();//呼叫洗牌
/***************************************************/
/*發牌*/
LicensingDevice licensingDevice = new LicensingDevice();//創建發牌器
licensingDevice.licensingDevice();//呼叫
/***************************************************/
/*打牌*/
MyPlayCards mYplayCards = new MyPlayCards();//創建發牌類
mYplayCards.playCards();//呼叫
}
}
//我出牌類
class MyPlayCards
{
GamePlayer gamePlayer = new GamePlayer();//創建玩家類
CharacterObject characterObject = new CharacterObject();//人物物件類
RobLandlord robLandlord = new RobLandlord();//創建搶地主
Identity identity = new Identity();//身份牌
/********//*資料塊*/
boolean number1;
boolean number2;
boolean number3;
/********//*******/
public void playCards()
{
/****************************************************/
/*列印我的牌*/
identity.identity(robLandlord.gamePlayer_1);//身份牌
System.out.println(gamePlayer.gamePlayer_4);
/****************************************************/
/*搶地主*/
robLandlord.robLandlord();//呼叫
/****************************************************/
//確認出牌順序
Order order = new Order();
order.order();
/****************************************************/
Use use = new Use();
while (true)
{
//查找一手牌
/*玩家1*/
switch (order.order1)
{
case 1:
use.use();
number1 = gamePlayer.gamePlayer_4.isEmpty();
break;
}
if (number1 == true)
{
System.out.println("我的牌已經出完,我勝出");
break;
}
/*玩家2*/
switch (order.order2)
{
case 1:
use.manMachine_1();
number2 = gamePlayer.gamePlayer_5.isEmpty();
break;
}
if (number2 == true)
{
System.out.println("小天的牌已經出完,小天勝出");
break;
}
/*玩家3*/
switch (order.order3)
{
case 1:
use.manMachine_2();
number3 = gamePlayer.gamePlayer_6.isEmpty();
break;
}
if (number3 == true)
{
System.out.println("二妮的牌已經出完,二妮勝出");
break;
}
//查找二手牌
/*玩家1*/
switch (order.order1)
{
case 2:
use.use();
number1 = gamePlayer.gamePlayer_4.isEmpty();
break;
}
if (number1 == true)
{
System.out.println("我的牌已經出完,我勝出");
break;
}
/*玩家2*/
switch (order.order2)
{
case 2:
use.manMachine_1();
number2 = gamePlayer.gamePlayer_5.isEmpty();
break;
}
if (number2 == true)
{
System.out.println("小天的牌已經出完,小天勝出");
break;
}
/*玩家3*/
switch (order.order3)
{
case 2:
use.manMachine_2();
number3 = gamePlayer.gamePlayer_6.isEmpty();
break;
}
if (number3 == true)
{
System.out.println("二妮的牌已經出完,二妮勝出");
break;
}
//查找三手牌
/*玩家1*/
switch (order.order1)
{
case 3:
use.use();
number1 = gamePlayer.gamePlayer_4.isEmpty();
break;
}
if (number1 == true)
{
System.out.println("我的牌已經出完,我勝出");
break;
}
/*玩家2*/
switch (order.order2)
{
case 3:
use.manMachine_1();
number2 = gamePlayer.gamePlayer_5.isEmpty();
break;
}
if (number2 == true)
{
System.out.println("小天的牌已經出完,小天勝出");
break;
}
/*玩家3*/
switch (order.order3)
{
case 3:
use.manMachine_2();
number3 = gamePlayer.gamePlayer_6.isEmpty();
break;
}
if (number3 == true)
{
System.out.println("二妮的牌已經出完,二妮勝出");
break;
}
}
}
}
//用戶出牌
class Use
{
int poker_1;
int poker_2;
int poker_3;
/***********/
Random random = new Random();//亂數
Eliminate eliminate = new Eliminate();//清除資料
CharacterObject CharacterObject = new CharacterObject();//人物物件
Scanner input = new Scanner(System.in);//輸入
GamePlayer gamePlayer = new GamePlayer();//玩家類
RobLandlord robLandlord = new RobLandlord();//搶地主類
Transformation transformation = new Transformation();//轉換資料
Identity identity = new Identity();//身份牌
public static ArrayList<String> out_1= new ArrayList<String>();//存盤
public static ArrayList<String> out_2 = new ArrayList<String>();//存盤橋
//人
public void use()
{
System.out.println();
identity.identity(robLandlord.gamePlayer_1);//列印身份牌
System.out.println(gamePlayer.gamePlayer_4);
/******************************************/
/***********************************/
System.out.println("請選擇要出的牌");
CharacterObject.me();
String str_1 = input.next();//選擇要出的牌
System.out.println("出牌1 不出2");
int str_2 = input.nextInt();//是否出牌
switch (str_2)//判斷是否出牌
{
case 1:
int s = 0;
int size = str_1.length();
for (int i = 0 ; i < size;i++)
{
char brand = str_1.charAt(i);//分離字串
String brand_s = String.valueOf(brand);//轉換資料型別
if (gamePlayer.gamePlayer_4.contains(brand_s))//是否屬于手牌
{
}
else
{
s = 1;
System.out.println("出牌錯誤,下家出牌");
break;
}
out_2.add(brand_s);//轉換出的牌
gamePlayer.gamePlayer_4.remove(brand_s);//移除出的牌
}
eliminate.eliminate_1(out_1);//初始化資料1
transformation.transformation(out_1, out_2);//轉換資料
eliminate.eliminate_2(out_2);//初始化資料2
if (s == 1)
{
break;
}
}
}
//人機
public void manMachine_1()
{
//人機出牌
System.out.println();
identity.identity(robLandlord.gamePlayer_2);
CharacterObject.smallDay();
System.out.println("小天出牌 過!");
}
public void manMachine_2()
{
//人機出牌
System.out.println();
identity.identity(robLandlord.gamePlayer_3);
CharacterObject.erNi();
System.out.println("二妮出牌 過!");
}
}
//清除資料
class Eliminate
{
ArrayList<String> initializationData= new ArrayList<String>();
public void eliminate_1(ArrayList<String> out_1)
{
for (String i: out_1)
{
initializationData.add(i);
}
for (String i : initializationData)
{
out_1.remove(i);
}
}
public void eliminate_2(ArrayList<String> out_2)
{
for (String i: out_2)
{
initializationData.add(i);
}
for (String i : initializationData)
{
out_2.remove(i);
}
}
}
//轉換資料
class Transformation
{
public void transformation(ArrayList<String> out_1, ArrayList<String> out_2)
{
for (String i: out_2)
{
out_1.add(i);
}
}
}
//出牌順序
class Order
{
public static int order1;//玩家1
public static int order2;//玩家2
public static int order3;//玩家3
RobLandlord robLandlord = new RobLandlord();
public void order()
{
if (robLandlord.gamePlayer_1 == 1)
{
int order1 = 1;//玩家1[一手]
this.order1 = order1;
int order2 = 2;//玩家2[二手]
this.order2 = order2;
int order3 = 3;//玩家3[三手]
this.order3 = order3;
}
else
{
if (robLandlord.gamePlayer_2 == 1)
{
int order2 = 1;//玩家2[一手]
this.order2 = order2;
int order3 = 2;//玩家3[二手]
this.order3 = order3;
int order1 = 3;//玩家1[三手]
this.order1 = order1;
}
else
{
if (robLandlord.gamePlayer_3 == 1)
{
int order3 = 1;//玩家3[一手]
this.order3 = order3;
int order1 = 2;//玩家1[二手]
this.order1 = order1;
int order2 = 3;//玩家2[三手]
this.order2 = order2;
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/386732.html
標籤:其他
下一篇:狀態模式——紙牌游戲
