转向博客
题目
狼人杀,身份分配。
12人身份,3个阵营。分配身份。
阵营:
神民:预言家、女巫、猎人、白痴
平民:4民
狼人:4 狼人
程序执行:
1. 分配身份
2. 退出
点击分配身份,将12个身份随机分给12个用户,打印出来。
例如:
1.预言家 7.女巫
2.平民 8.狼人
3.猎人 9狼人
4.狼人 10.平民
5.白痴 11狼人
6.平民 12.平民
(进阶功能,可以做一个猜身份的功能)
每次分配身份结果都随机,不是固定为上述功能。
package com.azwcl.Game;
import java.util.*;
public class Game {
private final String[] identity = new String[]{"预言家", "女巫", "猎人", "白痴", "平民", "平民", "平民", "平民", "狼人", "狼人", "狼人", "狼人"};
private final Random random = new Random();
private final String[] GamerIdentity = new String[12];
private final int identityNum = 12;
public void distributionIdentity(){
Vector<String> copyIdentity = new Vector<String>(Arrays.asList(identity));
for(int i = this.identityNum; i > 0; i--){
int randomNum = this.random.nextInt(i);
GamerIdentity[12 - i] = copyIdentity.get(randomNum);
copyIdentity.remove(randomNum);
}
}
public void printGamerIdentity(){
System.out.println("目前身份:");
for(int i = 0 ; i < this.identityNum; i++){
System.out.println((i + 1) + "号玩家是" + this.GamerIdentity[i] + "身份");
}
System.out.println("-----------------------------------------");
System.out.println();
}
public boolean guessIdentity(int index, String identity){
return GamerIdentity[index].equals(identity);
}
public void guessIdentityGame(){
int gamerGuessId = 0;
String gamerGuessName;
System.out.print("请输入你要猜测的玩家号:");
while(true){
try{
Scanner sc = new Scanner(System.in);
gamerGuessId = sc.nextInt();
if(gamerGuessId > 12 || gamerGuessId < 1) throw new InputMismatchException();
break;
}catch (InputMismatchException e){
System.out.println("输入有误,请重新选择");
}
}
System.out.print("请输入你猜测的身份:");
Scanner sc = new Scanner(System.in);
gamerGuessName = sc.nextLine();
boolean flag = guessIdentity(gamerGuessId - 1, gamerGuessName);
if(flag) System.out.println("恭喜您猜测正确!");
else System.out.println("对不起,您猜测错误!");
this.guessMenu();
}
public void guessMenu(){
int option = 0;
System.out.println("\n1.猜测身份");
System.out.println("2.重新分配身份");
System.out.println("3.查看所有人身份");
System.out.println("4.退出游戏");
System.out.print("请输入您的选项:");
while(true){
try{
Scanner sc = new Scanner(System.in);
option = sc.nextInt();
if(option > 4 || option < 1) throw new InputMismatchException();
break;
}catch (InputMismatchException e){
System.out.println("选择有误,请重新选择");
}
}
if(option == 1) this.guessIdentityGame();
else if(option == 2){
this.distributionIdentity();
this.guessMenu();
}else if(option == 3){
this.printGamerIdentity();
this.guessMenu();
}
else this.gameOver();
}
public void menu(){
System.out.println("欢迎您来到狼人杀游戏!");
System.out.println("1.分配身份");
System.out.println("2.退出");
System.out.print("请输入您的选项:");
int option = 0;
while(true){
try{
Scanner sc = new Scanner(System.in);
option = sc.nextInt();
if(option > 2 || option < 1) throw new InputMismatchException();
break;
}catch (InputMismatchException e){
System.out.println("选择有误,请重新选择");
}
}
if(option == 1){
System.out.print("分配身份中");
for(int i = 0 ; i < 3; i++){
try{
Thread.sleep(500);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.print(".");
}
System.out.println();
this.distributionIdentity();
System.out.println("身份分配完毕");
this.guessMenu();
}else this.gameOver();
}
public void gameOver(){
System.out.println("再见了~,欢迎下次继续游玩");
System.exit(0);
}
public static void main(String[] args) {
Game game = new Game();
game.menu();
}
}