package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Card {
public String color;
public String number;
public static List<String>newCards;
public Card(String color, String number){
this.color = color;
this.number = number;
}
public String getcolor(){
return color;
}
public void setcolor(String color){
this.color = color;
}
public String getNumber(){
return number;
}
public void setNumber(String number){
this.number = number;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((color == null) ? 0 : color.hashCode());
result = prime * result + ((number == null) ? 0 : number.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Card))
return false;
Card other = (Card) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
if (number == null) {
if (other.number != null)
return false;
} else if (!number.equals(other.number))
return false;
return true;
}
}
package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Cards {
private static List<Card> list= new ArrayList();
public String[] color={"方块","梅花","红桃","黑桃"};
public String[] number={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
public void isCard(){
System.out.println("---------------------------------开始创建扑克牌:--------------------------");
System.out.println("---------------------------------扑克牌创建成功!--------------------------");
for(int i = 0;i < color.length;i++){
for(int j = 0; j < number.length;j++){
list.add(new Card(color[i],number[j]));
}
}
}
public void show(){
System.out.println("扑克牌:");
for(int i = 0;i < list.size();i++){
System.out.print(list.get(i).color + list.get(i).number + "\t");
if((i+1)%13 ==0){
System.out.println(" ");
}
}
}
public static List<Card> getlist(){
return list;
}
//洗牌
public void shuffleCards(){
System.out.println("---------------------------------开始洗牌:--------------------------");
Collections.shuffle(list);
System.out.println("---------------------------------洗牌结束!--------------------------");
}
}
package com.imooc;
import java.util.ArrayList;
import java.util.List;
public class Player {
public String name;
public int ID;
ArrayList<Card> cardList = new ArrayList<Card>();
List<Player> p = new ArrayList<Player>();
public Player(String name,int ID){
this.name = name;
this.ID = ID;
this.cardList = new ArrayList<Card>();
}
public int getid(){
return ID;
}
public void setid(int ID){
this.ID = ID;
}
public String getname(){
return name;
}
public void setname(String name){
this.name = name;
}
public List<Card> getHandCards() {
return cardList;
}
public void setHandCards(Card string) {
cardList.add(string);
}
}
package com.imooc;
import java.util.Comparator;
public class CardCompare implements Comparator<Card> {
@Override
public int compare(Card o1, Card o2) {
// TODO Auto-generated method stub
String[] color={"方块","梅花","红桃","黑桃"};
String[] number={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
int valueo1 = 0;
int valueo2 = 0;
for(int i = 0; i < number.length;i++){
if(o1.getNumber().equals(number[i])){
valueo1 += i*10;
}
if(o2.getNumber().equals(number[i])){
valueo2 += i*10;
}
}
for(int j = 0;j < color.length;j++){
if(o1.getcolor().equals(color[j])){
valueo1 += 1;
}
if(o2.getNumber().equals(color[j])){
valueo2 += 1;
}
}
return valueo1>valueo2 ? -1 : 1;
/* int valueOfC1 = 0;
int valueOfC2 = 0;
for(int i=0;i<number.length;i++){
if(c1.getNumber().equals(number[i])) valueOfC1 += i*10;
if(c2.getNumber().equals(number[i])) valueOfC2 += i*10;
}
for(int i=0;i<color.length;i++){
if(c1.getColor().equals(color[i])) valueOfC1 += i;
if(c2.getColor().equals(color[i])) valueOfC2 += i;
}
if( valueOfC1 > valueOfC2 ) return -1;
if( valueOfC1 < valueOfC2 ) return 1;*/
}
}
package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Compare {
//随机发牌
public void sendCard(){
}
//创建玩家
public static void createPlayer(){
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Compare com = new Compare();
Cards cp = new Cards();
cp.isCard();
cp.show();
cp.shuffleCards();
// cp.show();
List<Player> p = new ArrayList<Player>();
Scanner console = new Scanner(System.in);
int num = 0;
boolean flag = true;
for(int i = 0;i < 2;i++){
System.out.println(" ");
System.out.println("请输入第 " + (i+1) + " 位玩家的姓名和ID:");
// Compare.createPlayer();
do{
try{
System.out.println("");
System.out.println("请输入ID:");
num = console.nextInt();
flag = true;
}catch(Exception e){
System.out.println("");
System.out.println("请输入整数型ID!");
flag = false;
console.nextLine();
}
}while(flag == false);
System.out.println("");
System.out.println("请输入姓名:");
String name = console.next();
Player player1 = new Player(name,num);
p.add(player1);
Player temp = p.get(0);
System.out.println("成功创建玩家 :\t" + temp.getname());
}
System.out.println("---------------------------------开始发牌:--------------------------");
int count = 0;
for(int i = 0;i < 2;i++){
for(int j = 0; j < p.size();j++){
System.out.println("玩家" + p.get(j).getname());
p.get(j).setHandCards(Cards.getlist().get(count));
count++;
}
}
System.out.println("---------------------------------发牌结束--------------------------");
System.out.println("---------------------------------开始游戏!--------------------------");
for(int m = 0;m < p.size();m++){
Collections.sort(p.get(m).getHandCards(),new CardCompare());
}
List<Card> maxCard = new ArrayList<Card>();
for(int i = 0;i < p.size();i++){
Card maxC = p.get(i).getHandCards().get(0);
System.out.println(p.get(i).getname() + "玩家最大手牌:" + maxC.color + maxC.number);
maxCard.add(maxC);
}
List<Card> max = new ArrayList<Card>();
max.addAll(maxCard);
Collections.sort(max,new CardCompare());
for(int i = 0;i < p.size();i++){
if(maxCard.get(i).equals(max.get(0))){
System.out.println( "--------------------------------玩家" + p.get(i).getname() +"获胜!--------------------------");
}
}
System.out.println("-------------------------------玩家各自的手牌为:--------------------------");
for(int x = 0;x < p.size();x++){
System.out.print("玩家:" + p.get(x).getname() + "的手牌为:[");
for(int y = 0;y < p.get(x).getHandCards().size();y++){
Card handCard = p.get(x).getHandCards().get(y);
System.out.print( handCard.color + handCard.number + " ");
}
System.out.println("]");
}
}
}
输入代码