import java.util.Scanner;
class PlayCard {
public static void main(String[] args) {
Scanner scr = new Scanner(System.in);
System.out.println("欢迎来到年度最好玩的游戏:玉梅斗地主");
while (true) {
System.out.println("1, 开始游戏");
System.out.println("2, 退出游戏");
Scanner sc = new Scanner (System.in);
int a = sc.nextInt();
if (a==1) {
char[] arr = {48,65,50,51,52,53,54,55,56,57,74,81,75,48,65,50,51,52,53,54,55,56,57,74,81,75,48,65,50,51,52,53,54,55,56,57,74,81,75,48,65,50,51,52,53,54,55,56,57,74,81,75,'大','小'};
char[] result = new char[54];
for (int j=0;j<=2 ;j++ ) {
result[j] = arr[(int)(arr.length* Math.random())];
arr= delete(arr, result[j]);
}
char target = arr[(int)(Math.random()*51)];
for (int j=3;j<=53 ;j++ ) {
result[j]=arr[(int)(arr.length* Math.random())];
arr= delete(arr, result[j]);
}
char[] p1 = new char[17];
char[] p2 = new char[17];
char[] p3 = new char[17];
for (int i =0;i<=16 ;i++ ) {
p1[i]= result[i+3];
}
for (int i =0;i<=16 ;i++ ) {
p2[i]= result[i+20];
}
for (int i =0;i<=16 ;i++ ) {
p3[i]= result[i+37];
}
Player[] py = new Player[3];
Scanner sc3 = new Scanner(System.in);
System.out.println("请设置您的名字");
String str = sc3.nextLine();
py[0] = new Player(str,p1);
py[0].showMyCard();
py[1] = new Player("2号玩家",p2);
py[1].showMyCard();
py[2] = new Player("3号玩家",p3);
py[2].showMyCard();
for (int i =0;i<=16 ;i++ ) {
if (p1[i]==target) {
System.out.println("\n" + py[0].name + "你好,你有地主牌"+target +"请问你要叫地主吗? 请输入Yes or No");
Scanner s0 = new Scanner(System.in);
String st1 =s0.nextLine();
if (st1.equals("Yes")) {
py[0].getAll(result);
py[0].finalCard(result);
py[0].showMyCard();
break;
}else {
py[1].callDZ(py, 1, result);
break;
}
}
if (p2[i]==target) {
int q = (int)(2* Math.random());
if (q==1) {
System.out.println("2号玩家有地主牌"+target+"并成为地主");
py[1].getAll(result);
py[1].finalCard(result);
py[1].showMyCard();
break;
}else {
System.out.println("2号玩家放弃当地主");
py[2].callDZ(py, 2, result);
break;
}
}
if(p3[i]==target) {
int w = (int)(2*Math.random());
if (w==1) {
System.out.println("3号玩家有地主牌"+target+ "并成为地主");
py[2].getAll(result);
py[2].finalCard(result);
py[2].showMyCard();
break;
}
else {
System.out.println("3号玩家放弃当地主");
py[0].callDZ(py, 0, result);
break;
}
}
}
while (true) {
py[0].outCard(py, 0);
if (py[0].mycard.length==0) {
System.out.println(py[0].name+ "赢了");
System.out.print(py[1].name +"最后的牌为: ");
for (int i =0;i<=py[1].mycard.length-1 ;i++ ) {
System.out.print(py[1].mycard[i]+" ");
}
System.out.println();
System.out.print(py[2].name +"最后的牌为: ");
for (int i =0;i<=py[2].mycard.length-1 ;i++ ) {
System.out.print(py[2].mycard[i]+" ");
}
System.out.println();
break;
}
py[1].outCard(py, 1);
if (py[1].mycard.length==0) {
System.out.println(py[1].name+ "赢了");
System.out.print(py[2].name +"最后的牌为: ");
for (int i =0;i<=py[2].mycard.length-1 ;i++ ) {
System.out.print(py[2].mycard[i]+" ");
}
System.out.println();
System.out.print(py[0].name +"最后的牌为: ");
for (int i =0;i<=py[0].mycard.length-1 ;i++ ) {
System.out.print(py[0].mycard[i]+" ");
}
System.out.println();
break;
}
py[2].outCard(py, 2);
if(py[2].mycard.length==0) {
System.out.println(py[2].name+ "赢了");
System.out.print(py[0].name +"最后的牌为: ");
for (int i =0;i<=py[0].mycard.length-1 ;i++ ) {
System.out.print(py[0].mycard[i]+" ");
}
System.out.println();
System.out.print(py[1].name +"最后的牌为: ");
for (int i =0;i<=py[1].mycard.length-1 ;i++ ) {
System.out.print(py[1].mycard[i]+" ");
}
System.out.println();
break;
}
}
}
else {
System.out.println("谢谢使用,下次再见");
break;
}
}
}
public static char[] delete(char[]arr, char x){
char[] a = new char[arr.length-1];
int i =0;
for (i =0;i<arr.length ;i++ ) {
if (arr[i]==x) {
break;
}
}
for (int j =0;j<arr.length-1 ; j++) {
if (j<i) {
a[j]=arr[j];
}else {
a[j]=arr[j+1];
}
}
return a;
}
}
class Player {
String name;
char[] mycard ;
static int rank=16;
public Player(String name, char[] ch){
this.name = name;
mycard = ch;
}
public void showMyCard(){
char[] standard = {'大','小','2','A','K','Q','J','0','9','8','7','6','5','4','3'};
for (int i =0;i<mycard.length-1 ;i++ ) {
for (int j =mycard.length-1;j>i ;j-- ) {
int k =0; int k1=0;
for ( k=0;k<=14 ;k++ ) {
if (mycard[i]==standard[k]) {
break;
}
}
for (k1=0;k1<=14 ;k1++ ) {
if (mycard[j]==standard[k1]) {
break;
}
}
if (k>k1) {
char temp = mycard[i];
mycard[i]=mycard[j];
mycard[j] = temp;
}
}
}
if (!(name.equals("2号玩家"))&&!(name.equals("3号玩家"))) {
System.out.print("\n" +"这是您的牌--");
for (int i =0;i<=mycard.length-1 ;i++ ) {
System.out.print(mycard[i]+" ");
try {
Thread.sleep(100);
}
catch (Exception e) {
}
}
System.out.println("\n");
}
}
public void callDZ(Player[] py, int i, char[] arr){
if (!(name.equals("2号玩家"))&&!(name.equals("3号玩家"))) {
Scanner sc = new Scanner(System.in);
System.out.println(name+ "您好,请问你要叫地主吗? 请输入Yes or No");
String s = sc.nextLine();
if (s.equals("Yes")) {
getAll(arr);
py[i].finalCard(arr);
py[i].showMyCard();
return;
}else {
System.out.println(py[(i+1)%3].name +"自动成为地主");
py[(i+1)%3].getAll(arr);
py[(i+1)%3].finalCard(arr);
py[(i+1)%3].showMyCard();
}
}
else {
int q = (int)(2* Math.random());
if (q==1) {
System.out.print(py[i].name +"成为地主");
py[i].getAll(arr);
py[i].finalCard(arr);
py[i].showMyCard();
return;
}
else {
System.out.println(py[i].name + "放弃成为地主");
System.out.println(py[(i+1)%3].name +"自动成为地主");
py[(i+1)%3].getAll(arr);
py[(i+1)%3].finalCard(arr);
py[(i+1)%3].showMyCard();
}
}
}
public void getAll( char[] arr){
System.out.print("3张地主底牌是");
for (int i=0;i<=2 ;i++ ) {
System.out.print(arr[i]+ " ");
}
System.out.println();
}
|
|