参考了不少人写的,学java也有一段时间了,但是自己写东西还是很困难,在这个小程序也是,写写改改写了两天才搞好,虽然大多数是参考大神们的,但是运行出来那一刻还是挺开心的。
---------以此记录自己的学习
输入代码
package play.card;
public class Card implements Comparable<Card>{
public String num;
public String suit;
public Card(String num,String suit) {
this.num = num;
this.suit = suit;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public Card() {
super();
}
@Override
public String toString() {
String str = suit+" "+ num;
return str;
}
@Override
public int compareTo(Card o) {
if(DeskOfCard.numList.indexOf(this.getNum())==DeskOfCard.numList.indexOf(this.getNum())){
return Integer.valueOf(DeskOfCard.suitList.indexOf(this.getSuit())).
compareTo(Integer.valueOf(DeskOfCard.suitList.indexOf(this.getSuit())));
}
else
return Integer.valueOf(DeskOfCard.numList.indexOf(this.getNum())).
compareTo(Integer.valueOf(DeskOfCard.numList.indexOf(this.getNum())));
}
}
package play.card;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class DeskOfCard {
static List<Card> cards;
static String [] nums = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
static String [] suits = {"方块","梅花","红桃","黑桃"};
public static final List<String> suitList = new ArrayList<String>(Arrays.asList(suits));
public static final List<String> numList = new ArrayList<String>(Arrays.asList(nums));
//创建一副手牌
public void initCard(){
cards = new ArrayList<Card>();
//先创建四种花色的牌
for (int i = 0; i < 52; i++) {
cards.add(new Card(nums[i%13],suits[i%3]));
}
System.out.println("全部扑克牌:");
System.out.println(cards);
}
//发牌
public void deal(List<Player> players,List<Card> cards){
int count = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < players.size(); j++) {
Player player = players.get(j);
System.out.println("-------玩家:"+player.getName()+"拿牌");
player.setCards(cards.get(count));
count++;
}
}
}
//创建玩家
public List<Player> createPlayers(int playersNum){
List<Player> players = new ArrayList<Player>();
Scanner scan = new Scanner(System.in);
for (int i = 0; i < playersNum; i++) {
System.out.println("请输入第"+(i+1)+"位玩家的姓名:");
String name = scan.next();
Player p = new Player(name);
players.add(p);
}
if (scan!=null) {
scan.close();
}
return players;
}
//比较大小
public Card compare(Card c1,Card c2){
if(numList.indexOf(c1.num)==numList.indexOf(c2.num)){
return suitList.indexOf(c1.getSuit())>suitList.indexOf(c2.getSuit())?c1:c2;
}
else
return numList.indexOf(c1.getNum())>numList.indexOf(c2.getNum())?c1:c2;
}
//测试程序
public static void main(String[] args) {
DeskOfCard test = new DeskOfCard();
System.out.println("------------创建扑克牌-----------------");
test.initCard();//初始化
System.out.println("-------------扑克牌创建成功-------------");
System.out.println("-------------开始洗牌-------------");
//洗牌
Collections.shuffle(DeskOfCard.cards);
System.out.println("-------------洗牌成功-------------");
//创建玩家
System.out.println("-------------创建玩家-------------");
List<Player> players = test.createPlayers(2);//创建两名玩家
for(Player player :players){
System.out.println("------------欢迎玩家"+player.getName());
}
//发牌
System.out.println("-------------开始发牌-------------");
test.deal(players, cards);
//比牌
List<Card> maxCard = new ArrayList<Card>();
for (Player player :players) {
Card tempCard = new Card("方片","2");
for(Card card : player.getCards()){
tempCard = test.compare(tempCard, card);
}
System.out.println("玩家:"+player.getName()+
"最大的牌为:"+player.getCards().get(0));
maxCard.add(tempCard);
}
Card tempCard1 = new Card("方片", "2");
for(Card card : maxCard){
tempCard1 = test.compare(tempCard1, card) ;
}
String winner = players.get(maxCard.indexOf(tempCard1)).getName();
System.out.println("--------玩家:"+winner +"获胜!");
System.out.println("玩家各自手中的牌为:");
for(Player player :players){
System.out.println(player.toString());
}
}
}
package play.card;
import java.util.ArrayList;
import java.util.List;
public class Player {
public String name;
public List<Card> handCard;
public Player(String name) {
super();
this.name = name;
handCard = new ArrayList<Card>();
}
public List<Card> getCards() {
return handCard;
}
public void setCards(Card cards) {
this.handCard.add(cards);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Player() {
super();
}
@Override
public String toString() {
return name + ":" + handCard.toString();
}
}