package cardgame;
/**
*/
public class Card {
String pattern;//扑克牌的花色
String figure;//扑克牌的点数
public Card(){
}
public Card(String figure, String pattern) {
this.pattern = pattern;
this.figure = figure;
}
}
package cardgame;
/**
*/
public class Player {
public int id;
public String name;
public Card card1;
public Card card2;
public Player() {
}
public Player(int id, String name, Card card1, Card card2) {
this.id = id;
this.name = name;
this.card1 = card1;
this.card2 = card2;
}
}
package cardgame;
import java.util.ArrayList;
import java.util.List;
/**
*/
public class Pattern {
public List<String> patternList;
public Pattern(){
patternList = new ArrayList<String>();
patternList.add("黑桃");
patternList.add("红桃");
patternList.add("梅花");
patternList.add("方片");
}
}
package cardgame;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*/
public class Figure {
public List<String> figureList;
public Figure() {
figureList = new ArrayList<String>();
String[] figure = {"2","3","4","5","6","7","8","9","10","J","Q","k","A"};
figureList.addAll(Arrays.asList(figure));
}
// public static void main(String[] args) {
// Figure fg = new Figure();
// for (String string : fg.figureList) {
// System.out.println(string);
//}
// }
}
package cardgame;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
*/
public class CardList {
public List<Card> cardList;
public CardList() {
cardList = new ArrayList<Card>();
}
/**
*/
public void creatCard() {
Pattern pattern = new Pattern();
Figure figure = new Figure();
for(int j = 0;j < 4;j++) {
for(int i =0;i < 13;i++) {
Card c = new Card(figure.figureList.get(i),pattern.patternList.get(j));
cardList.add(c);
}
}
System.out.print("为:[");
for (Card card : cardList) {
System.out.print(card.pattern + card.figure + ",");
}
System.out.print("]");
System.out.println();
}
/**
*/
public List<Card> shuffle() {
List<Card> lc = new ArrayList<Card>();
Random random = new Random();
while(true) {
Card c = cardList.get(random.nextInt(52));
if(lc.contains(c)) continue;
lc.add(c);
if(lc.size() == 52) break;
}
return lc;
}
/**
}
package cardgame;
import java.util.Scanner;
import sun.net.www.content.text.plain;
/**
*/
public class CardGame {
public static void main(String[] args) {
System.out.println("------------------创建扑克牌---------------------------");
System.out.println("------------------扑克牌创建成功!-----------------------");
CardList cl = new CardList();
cl.creatCard();
System.out.println("-----------------------开始洗牌------------------------");
//洗牌
cl.shuffle();
System.out.println("-----------------------洗牌结束------------------------");
System.out.println("-----------------------创建玩家-------------------------");
//提示创建玩家
Player[] p = {new Player(),new Player()};
for(int i = 0;i < 2;i++) {
System.out.println("请输入第" +(i+1) +"位玩家的ID和姓名:");
while(true) {
System.out.println("请输入玩家ID:");
try {
Scanner console = new Scanner(System.in);
p[i].id = console.nextInt();
break;
} catch (Exception e) {
System.out.println("输入不合法,请重新输入整数类型的ID");
continue;
}
}
System.out.println("请输入玩家姓名:");
Scanner console = new Scanner(System.in);
p[i].name = console.next();
}
System.out.println("----------欢迎玩家:" + p[0].name);
System.out.println("----------欢迎玩家:" + p[1].name);
System.out.println("-----------------------开始发牌-----------------------");
int k = 0;
while(k < 2) {
System.out.println("-------玩家:" + p[0].name+"-拿牌");
System.out.println("-------玩家:" + p[1].name+"-拿牌");
k++;
}
//发牌
Card c11 = cl.deal(52);
Card c12 = cl.deal(51);
Card c21 = cl.deal(50);
Card c22 = cl.deal(49);
p[0].card1 = c11;
p[0].card2 = c12;
p[1].card1 = c21;
p[1].card2 = c22;
System.out.println("-----------------------发牌结束-----------------------");
System.out.println("-----------------------开始游戏-----------------------");
Card c1 = cl.compare(c11, c12);//玩家一的最大手牌
Card c2 = cl.compare(c21, c22);//玩家二的最大手牌
Card c = cl.compare(c1, c2);//比较两位玩家的最大手牌
System.out.println("玩家:" + p[0].name + "最大的手牌为:" + c1.pattern + c1.figure);
System.out.println("玩家:" + p[1].name + "最大的手牌为:" + c2.pattern + c2.figure);
if(c.equals(c1) && !c.equals(c2))
System.out.println("----------------玩家" + p[0].name + "获胜!----------------");
if(c.equals(c2) && !c.equals(c1))
System.out.println("----------------玩家" + p[1].name + "获胜!----------------");
if(c.equals(c1) && c.equals(c2))
System.out.println("---------------------平局,继续加油-----------------------");
System.out.println("玩家各自的手牌为:");
System.out.println(p[0].name + ":" +"[" + c11.pattern + c11.figure + "," + c12.pattern + c12.figure+ "]");
System.out.println(p[1].name + ":" +"[" + c21.pattern + c21.figure + "," + c22.pattern + c22.figure + "]");
}
}
2025 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123