写的好烂,我自己没事做写的。我还没看集合,用数组写的。无聊写着玩。
还有输入玩家的编号的姓名没有去判断是否是错误或者两个玩家姓名编号一样。大概完成了就不想写了。你们大概看一下,提一下意见。
- card类
package pukercard;
import java.util.Random;
public class Card {
Random random = new Random();
Player player1 = null;
Player player2 = null;
int a = 0;
int b = 0;
int c;
int d;
String[][] card = new String[4][14];// 因为没看集合又不知道写什么,我就用数组完成
String[] color1 = new String[] { "", "", "", "" };
String num1[] = new String[] { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
public void Card() {// 把牌打印出来
System.out.println("-------创建扑克牌-------");
System.out.println("扑克牌为:");
for (int i = 0; i < card.length; i++) {// 打印输出
for (int j = 1; j < card[i].length; j++) {
card[i][0] = color1[i];
card[i][j] = num1[j - 1];
if ((j - 1) % 13 == 0)
System.out.println();
System.out.print(card[i][0] + card[i][j] + " ");
if (j == card[i].length - 1) {
}
}
}
System.out.println();
System.out.println("\n" + "-------创建扑克牌成功!-------");
System.out.println("-------开始洗牌-------");
System.out.println("-------洗牌结束-------");
}
public void choseCard(Player player1, Player player2) {// 传入两个玩家 玩的环节
this.player1 = player1;
this.player2 = player2;
int one;
int two;
System.out.println("\n" + "-------开始游戏...-------");
System.out.println("\n" + "-------玩家拿牌...-------");
while (a == b || a == c || a == d || b == c || b == d || c == d) {// 随机产生四张不一样的牌,就当做洗牌了。不然数组乱序用循环懒得写。
a = (random.nextInt(13) + 1) * 10 + random.nextInt(3);
b = (random.nextInt(13) + 1) * 10 + random.nextInt(3);
c = (random.nextInt(13) + 1) * 10 + random.nextInt(3);
d = (random.nextInt(13) + 1) * 10 + random.nextInt(3);
}
if (a > c)
one = a;
else
one = c;
if (b > d)
two = b;
else
two = d;
System.out.println("\n" + "玩家:" + player1.name + "最大手牌为:" + card[one % 10][0] + card[one % 10][one / 10]);
System.out.println("\n" + "玩家:" + player2.name + "最大手牌为:" + card[two % 10][0] + card[two % 10][two / 10]);
if (one > two)
System.out.println("\n" + "---玩家:" + player1.name + "胜利---");
else
System.out.println("\n" + "---玩家:" + player2.name + "胜利---");
System.out.println("\n" + "玩家1:" + player1.name + "的手牌为:" + card[a % 10][0] + card[a % 10][a / 10] + " "
+ card[c % 10][0] + card[c % 10][c / 10]);
System.out.println("\n" + "玩家2:" + player2.name + "的手牌为:" + card[b % 10][0] + card[b % 10][b / 10] + " "
+ card[d % 10][0] + card[d % 10][d / 10]);
// System.out.println(card.length);//很尴尬,测试长度
// System.out.println(card[1].length);//测试长度
}
}
**
- player类
**package pukercard;
public class Player {
String name;
int num;
public Player(int num,String name) {
// TODO Auto-generated constructor stub
this.num=num;
this.name=name;
System.out.println("-----欢迎玩家:"+name);
}
}
**
- Text类
**package pukercard;
import java.util.Scanner;
public class Text {
public static void main(String[] args) {
Card puker=new Card();
puker.Card();//初始化扑克牌
Scanner input=new Scanner(System.in);
System.out.println("输入第一个玩家编号和姓名:");
Player player1 =new Player(input.nextInt(),input.next());
System.out.println("输入第二个玩家编号和姓名:");
Player player2 =new Player(input.nextInt(),input.next());
puker.choseCard(player1, player2);
}
}