本帖最后由 何亚辉 于 2016-5-19 00:44 编辑
由于代码实在太长,暂时只能实现一次只能出一张牌...哈哈哈哈
import java.util.Scanner;
class PlayCard {
public static void main(String[] args) {
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,49,50,51,52,53,54,55,56,57,74,81,75,48,49,50,51,52,53,54,55,56,57,74,81,75,48,49,50,51,52,53,54,55,56,57,74,81,75,48,49,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()*52)];
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= result[i+3];
}
for (int i =0;i<=16 ;i++ ) {
p2= result[i+20];
}
for (int i =0;i<=16 ;i++ ) {
p3= 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("黎玉梅",p2);
//py[1].showMyCard();
py[2] = new Player("3号玩家",p3);
//py[2].showMyCard();
for (int i =0;i<=16 ;i++ ) {
if (p1==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==target) {
int q = (int)(2* Math.random());
if (q==1) {
System.out.println("黎玉梅有地主牌"+target+"并成为地主");
py[1].getAll(result);
py[1].finalCard(result);
//py[1].showMyCard();
break;
}else {
System.out.println("黎玉梅放弃当地主");
py[2].callDZ(py, 2, result);
break;
}
}
if(p3==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+ "赢了");
break;
}
py[1].outCard(py, 1);
if (py[1].mycard.length==0) {
System.out.println(py[1].name+ "赢了");
break;
}
py[2].outCard(py, 2);
if(py[2].mycard.length==0) {
System.out.println(py[2].name+ "赢了");
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==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[] Card =new int[54];
static int t =0;
public Player(String name, char[] ch){
this.name = name;
mycard = ch;
}
public void showMyCard(){
System.out.println("\n"+ name +"您好,这是您的牌");
char[] standard = {'大','小','2','1','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==standard[k]) {
break;
}
}
for (k1=0;k1<=14 ;k1++ ) {
if (mycard[j]==standard[k1]) {
break;
}
}
if (k>k1) {
char temp = mycard;
mycard=mycard[j];
mycard[j] = temp;
}
}
}
for (int i =0;i<=mycard.length-1 ;i++ ) {
System.out.print(mycard+" ");
try {
Thread.sleep(100);
}
catch (Exception e) {
}
}
System.out.println();
}
public void callDZ(Player[] py, int i, char[] arr){
if (!(name.equals("黎玉梅"))&&!(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.finalCard(arr);
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.name +"成为地主");
py.getAll(arr);
py.finalCard(arr);
//py.showMyCard();
return;
}
else {
System.out.println(py.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+ " ");
}
System.out.println();
}
public void outCard(Player[] p, int i ){
if (!(name.equals("黎玉梅"))&&!(name.equals("3号玩家"))) {
while (true) {
char[] standard = {'大','小','2','1','K','Q','J','0','9','8','7','6','5','4','3'};
Card[0]= 16;
Scanner s0 = new Scanner(System.in);
System.out.println(name + "你要出第几张牌");
int q =s0.nextInt();
int w =0;
for (w=0;w<standard.length ; w++) {
if (mycard[q-1]==standard[w]) {
break;
}
}
int r =0;
for (r=0;r<standard.length ; r++) {
if (p[(i+1)%3].mycard[0]==standard[r]) {
break;
}
}
int m =0;
for (m=0;m<standard.length ; m++) {
if (p[(i+2)%3].mycard[0]==standard[m]) {
break;
}
}
if (w<=r&&w<=m) {
System.out.println(name+"-----打出一张"+ mycard[q-1]);
System.out.println(p[(i+1)%3].name + "没有更大的牌");
System.out.println(p[(i+2)%3].name + "没有更大的牌");
Card[t]=16;
char[] temp1 = new char[mycard.length-1];
for (int e =0;e<mycard.length-1 ;e++ ) {
if (e<q-1) {
temp1[e]=mycard[e];
}else {
temp1[e]=mycard[e+1];
}
}
mycard = temp1;
showMyCard();
continue;
}
if (w>= Card[t]) {
System.out.println("你的牌没有上家大");
continue;
}
Card[++t]=w;
System.out.println(name+"-----打出一张"+ mycard[q-1]);
char[] temp = new char[mycard.length-1];
for (int e =0;e<mycard.length-1 ;e++ ) {
if (e<q-1) {
temp[e]=mycard[e];
}else {
temp[e]=mycard[e+1];
}
}
mycard = temp;
showMyCard();
return;
}
}else {
while (true) {
char[] standard1 = {'大','小','2','1','K','Q','J','0','9','8','7','6','5','4','3'};
Card[0]= 16;
int x =(int)(mycard.length* Math.random())+1;
int f =0;
for (f=0;f<standard1.length ; f++) {
if (mycard[x-1]==standard1[f]) {
break;
}
}
int g =0;
for (g=0;g<standard1.length ; g++) {
if (p[(i+1)%3].mycard[0]==standard1[g]) {
break;
}
}
int h =0;
for (h=0;h<standard1.length ; h++) {
if (p[(i+2)%3].mycard[0]==standard1[h]) {
break;
}
}
if (f<=g&&f<=h) {
System.out.println(name+"-----打出一张"+ mycard[x-1]);
System.out.println(p[(i+1)%3].name + "没有更大的牌");
System.out.println(p[(i+2)%3].name + "没有更大的牌");
Card[t]=16;
char[] temp2 = new char[mycard.length-1];
for (int e =0;e<mycard.length-1 ;e++ ) {
if (e<f-1) {
temp2[e]=mycard[e];
}else {
temp2[e]=mycard[e+1];
}
}
mycard = temp2;
continue;
}
if (f >= Card[t]) {
continue;
}
Card[++t]=f;
System.out.println(name+"-----打出一张"+ mycard[x-1]);
char[] temp3 = new char[mycard.length-1];
for (int e =0;e<mycard.length-1 ;e++ ) {
if (e<f-1) {
temp3[e]=mycard[e];
}else {
temp3[e]=mycard[e+1];
}
}
mycard = temp3;
return;
}
}
}
public void finalCard(char[] arr){
char[] ch1 = new char[20];
for (int i =0;i<=19 ;i++ ) {
if (i<=16) {
ch1=mycard;
}else {
ch1= arr[i-17];
}
}
mycard = ch1;
if (!(name.equals("黎玉梅"))&&!(name.equals("3号玩家"))) {
System.out.print("\n"+ "尊敬的地主");
showMyCard();
}
}
}
|
|