一共两位玩家,发两轮牌,最后比较每个人手中最大的牌,较大的玩家获胜。
同花色中2最小,A最大。不同花色的排序为:方片<梅花<红桃<黑桃
(游戏无图形页面)
主类:Game类
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Game {
public Map cardMap;
public Game(){
this.cardMap = new HashMap();
}
//创建扑克牌
public static Card[] cards = new Card[]{
new Card(0,"方片2"),new Card(1,"梅花2"),new Card(2,"红桃2"),new Card(3,"黑桃2"),
new Card(4,"方片3"),new Card(5,"梅花3"),new Card(6,"红桃3"),new Card(7,"黑桃3"),
new Card(8,"方片4"),new Card(9,"梅花4"),new Card(10,"红桃4"),new Card(11,"黑桃4"),
new Card(12,"方片5"),new Card(13,"梅花5"),new Card(14,"红桃5"),new Card(15,"黑桃5"),
new Card(16,"方片6"),new Card(17,"梅花6"),new Card(18,"红桃6"),new Card(19,"黑桃6"),
new Card(20,"方片7"),new Card(21,"梅花7"),new Card(22,"红桃7"),new Card(23,"黑桃7"),
new Card(24,"方片8"),new Card(25,"梅花8"),new Card(26,"红桃8"),new Card(27,"红桃8"),
new Card(28,"方片9"),new Card(29,"梅花9"),new Card(30,"红桃9"),new Card(31,"黑桃9"),
new Card(32,"方片10"),new Card(33,"梅花10"),new Card(34,"红桃10"),new Card(35,"黑桃10"),
new Card(36,"方片J"),new Card(37,"梅花J"),new Card(38,"红桃J"),new Card(39,"黑桃J"),
new Card(40,"方片Q"),new Card(41,"梅花Q"),new Card(42,"红桃Q"),new Card(43,"黑桃Q"),
new Card(44,"方片K"),new Card(45,"梅花K"),new Card(46,"红桃K"),new Card(47,"黑桃K"),
new Card(48,"方片A"),new Card(49,"梅花A"),new Card(50,"红桃A"),new Card(51,"黑桃A")
};
public void inputCards(){
for(int i=0;i<=51;i++){
cardMap.put(i,cards[i]);
}
}
public Card[] getCards(){
Card[] card = new Card[2];
int flag = 0;
while(flag<2){
int num = (int)(Math.random()*52);
Card c = cardMap.get(num);
if(c!=null){
card[flag] = cards[num];
cardMap.remove(num);
flag++;
}else{
continue;
}
}
return card;
}
public int compare(Card[] card){
if(card[0].num>card[1].num){
return card[0].num;
}else{
return card[1].num;
}
}
public void getCardBack(int num){
cardMap.put(num, cards[num]);
}
public String getMaxName(int max){
return cardMap.get(max).cardName;
}
//主方法
public static void main(String[] args){
Game game = new Game();
System.out.println("------欢迎进入扑克牌游戏------");
System.out.println("---------创建扑克牌---------");
System.out.println("-------已成功创建扑克牌-------");
game.inputCards();
for(int i=0;i<=51;i++){
System.out.print(cards[i].cardName);
if(i!=51)
System.out.print(",");
if((i+1)%13==0)
System.out.println();
}
System.out.println("---------开始洗牌---------");
System.out.println("---------创建玩家---------");
Scanner s = new Scanner(System.in);
System.out.println("请输入第一位玩家的ID和姓名");
System.out.print("输入ID:");
String id1 = s.next();
System.out.print("输入姓名:");
String name1 = s.next();
System.out.println("请输入第二位玩家的ID和姓名");
System.out.print("输入ID:");
String id2 = s.next();
System.out.print("输入姓名:");
String name2 = s.next();
Player p1 = new Player(id1,name1);
Player p2 = new Player(id2,name2);
System.out.println("---------开始发牌---------");
System.out.println("---"+name1+"拿牌");
System.out.println("---"+name2+"拿牌");
System.out.println("---"+name1+"拿牌");
System.out.println("---"+name2+"拿牌");
System.out.println("---------发牌结束---------");
p1.cards = game.getCards();
p2.cards = game.getCards();
//添加回列表
game.getCardBack(p1.cards[0].num);
game.getCardBack(p1.cards[1].num);
game.getCardBack(p2.cards[0].num);
game.getCardBack(p2.cards[1].num);
int max1 = game.compare(p1.cards);
String maxName1 = game.getMaxName(max1);
int max2 = game.compare(p2.cards);
String maxName2 = game.getMaxName(max2);
System.out.println("---玩家"+name1+"最大的手牌为:"+maxName1);
System.out.println("---玩家"+name2+"最大的手牌为:"+maxName2);
String winner;
if(max1>max2){
winner = name1;
}else{
winner = name2;
}
System.out.println("------玩家:"+winner+"获胜------");
System.out.println("各自玩家的手牌为:");
System.out.println(name1+":["+p1.cards[0].cardName+","+p1.cards[1].cardName+"]");
System.out.println(name2+":["+p2.cards[0].cardName+","+p2.cards[1].cardName+"]");
}
}
Card类
public class Card {
public int num;
public String cardName;
public Card(){}
public Card(int num,String cardName){
this.num = num;
this.cardName = cardName;
}
}
Player类
public class Player {
public String ID;
public String name;
public Card[] cards;
public Player(String ID,String name){
this.ID = ID;
this.name = name;
System.out.println("---欢迎玩家:"+this.name);
}
}