Brand类 (定义卡牌的属性)
package cn.edu.nuc;
import java.util.Comparator;
/**
* Created by asus on 2016/11/7.
*/
/*
功能描述
1.创建一副扑克牌
包括4种花色:黑桃,红桃,梅花,方块
十三中点数:2-10,J,Q,K,A
2.创建两名玩家
玩家至少要有ID,姓名,手牌等属性,手牌为扑克牌的集合
3.洗牌
将之前创建的“一副扑克牌”打乱顺序
4.发牌
讲洗牌之后的扑克牌集合,从第一张开始,发给两名玩家,按照一人一张的方式,每人发两张
5.游戏
比较两个人手中的扑克牌,规则为:取两人各自手中点数最大牌进行比较,点数大的赢;
如果两个人各自的点数相等,则按花色比较
*/
public class Brand implements Comparable<Brand>{
private String color;
private String point ;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
public Brand(String color , String point ){
this.color=color;
this.point=point;
}
@Override
public int compareTo(Brand o) {
if (this.getPoint() == o.getPoint()) {
if (this.getColor().equals("黑桃")) {
return 1;
} else if (this.getColor().equals("红桃")) {
if (o.getColor().equals("黑桃")) {
return -1;
} else {
return 1;
}
} else if (this.getColor().equals("梅花")) {
if (o.getColor().equals("黑桃") || o.getColor().equals("红桃")) {
return -1;
} else {
return 1;
}
} else {
return -1;
}
}else {
if(this.getPoint().equals("A")){
return 1;
}else if(this.getPoint().equals("K")){
if(o.getPoint().equals("A")){
return -1 ;
}else{
return 1 ;
}
}else if(this.getPoint().equals("Q")){
if(o.getPoint().equals("A")||o.getPoint().equals("K")){
return -1;
}else{
return 1 ;
}
}else if(this.getPoint().equals("J")){
if(o.getPoint().equals("A")||o.getPoint().equals("K")
||o.getPoint().equals("Q")){
return -1 ;
}else{
return 1 ;
}
}else if(this.getPoint().equals("10")){
if(o.getPoint().equals("A")||o.getPoint().equals("K")
||o.getPoint().equals("Q")||o.getPoint().equals("J")){
return -1 ;
}else{
return 1 ;
}
}else{
return this.getPoint().compareTo(o.getPoint());
}
}
}
}
Plays类(定义玩家的属性)
* Created by asus on 2016/11/8.
*/
public class Plays {
private int ID ;
private String name ;
public List<Brand> cards;
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 Plays(){
}
public Plays(int ID , String name ){
this.ID= ID;
this.name =name ;
this.cards= new ArrayList<Brand>();
}
}
测试类
package cn.edu.nuc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
/**
* Created by asus on 2016/11/8.
*/
public class Test {
//保存扑克牌的集合
List<Brand > cards =new ArrayList<Brand>();
//保存玩家的集合
List<Plays> playsList = new ArrayList<Plays>();
public Test(){
}
//创建扑克牌
private void foundCard(){
System.out.println("-----------创建扑克牌-------------");
for (int i= 1 ; i <= 4;i++){
for (int j = 2;j<=14 ;j++){
switch (i){
case 1 :
Brand card =new Brand("黑桃",setCardpoint(j));
cards.add(card);
break;
case 2 :
Brand card2 =new Brand("红桃",setCardpoint(j));
cards.add(card2);
break;
case 3:
Brand card3 =new Brand("梅花",setCardpoint(j));
cards.add(card3);
break;
case 4 :
Brand card4 =new Brand("方块",setCardpoint(j));
cards.add(card4);
break;
}
}
}
}
public String setCardpoint(int j ){
String point = null;
if(j<=10){
point = j +"";
}else {
switch (j){
case 11:
point = "J";
break;
case 12 :
point ="Q";
break;
case 13 :
point ="K";
break;
case 14:
point= "A";
break;
}
}return point ;
}
//遍历扑克牌
public void testForEach(){
for(Brand cr :cards){
System.out.println(cr.getColor()+cr.getPoint()+"");
}
}
//洗牌
public void shuffle(){
System.out.println();
System.out.println("--------开始洗牌--------");
Collections.shuffle(cards);
System.out.println("--------洗牌结束---------");
}
//创建玩家
public void foundPlays(){
System.out.println("---------创建玩家----------");
Scanner console = new Scanner(System.in);
for(int i = 1 ;i <=2 ; i++){
System.out.println("请输入第"+i+"位玩家的ID和姓名:");
System.out.println("请输入ID");
int ID = 0;
while(true){
try {
//trim()方法返回调用字符串对象的一个副本,但是所有起始和结尾的空格都被删除了
ID = Integer.parseInt(console.next().trim());
break;
}catch (NumberFormatException e){
System.out.println("请输入整数类型的ID");
System.out.println("请重新输入ID");
continue;
}
}
System.out.println("请输入玩家姓名");
String name = console.next();
Plays plays =new Plays(ID,name );
playsList.add(plays);
}
System.out.println("欢迎玩家:"+playsList.get(0).getName());
System.out.println("欢迎玩家:"+playsList.get(1).getName());
}
//发牌
public void getCard(){
System.out.println("-----开始发牌-------");
System.out.println("玩家"+playsList.get(0).getName()+"拿牌");
playsList.get(0).cards.add(cards.get(1));
System.out.println("玩家"+playsList.get(1).getName()+"拿牌");
playsList.get(1).cards.add(cards.get(2));
System.out.println("玩家"+playsList.get(0).getName()+"拿牌");
playsList.get(0).cards.add(cards.get(3));
System.out.println("玩家"+playsList.get(1).getName()+"拿牌");
playsList.get(1).cards.add(cards.get(4));
System.out.println("发牌结束 ");
}
//开始游戏
public void gameStart(){
System.out.println("----------开始游戏---------");
Collections.sort(playsList.get(0).cards);
System.out.println("玩家:"+playsList.get(0).getName()+" 最大手牌为:"+
playsList.get(0).cards.get(1).getColor()+playsList.get(0).cards.get(1).getPoint());
System.out.println("玩家:"+playsList.get(1).getName()+" 最大手牌为:"+
playsList.get(1).cards.get(1).getColor()+playsList.get(1).cards.get(1).getPoint());
List<Brand> gameCards = new ArrayList<Brand>();
gameCards.addAll(playsList.get(0).cards);
gameCards.addAll(playsList.get(1).cards);
Collections.sort(gameCards);
if(playsList.get(0).cards.contains(gameCards.get(gameCards.size()-1))){
System.out.println("玩家:"+playsList.get(0).getName()+"获胜");
}else{
System.out.println("玩家:"+playsList.get(1).getName()+"获胜");
}
}
public void LookPlaysCards(){
System.out.println("玩家各自的手牌为:");
System.out.println(playsList.get(0).getName()+":"+"["+playsList.get(0).cards.get(0).getColor()+playsList.get(0).cards.get(0).getPoint()+"]"
+"["+playsList.get(0).cards.get(1).getColor()+playsList.get(0).cards.get(1).getPoint()+"]");
System.out.println(playsList.get(1).getName()+":"+"["+playsList.get(1).cards.get(0).getColor()+playsList.get(1).cards.get(0).getPoint()+"]"
+"["+playsList.get(1).cards.get(1).getColor()+playsList.get(1).cards.get(1).getPoint()+"]");
}
public static void main(String[] args){
Test cr =new Test();
cr.foundCard();
cr.testForEach();
cr.foundPlays();
cr.shuffle();
cr.getCard();
cr.gameStart();
cr.LookPlaysCards();
}
}
- 结果会不一样
- 本代码的难点是重写compareTo()方法,我们按照自己的想要的规则定义后就可以进行排序了
- 尽量以private的形式定义变量,再以Get()andSet()函数调用变量
打开App,阅读手记
热门评论
用户id相等的判断呢?