主要精力放扑克牌的比较规则上面了,是我功力不够吗~~~~(>_<)~~~~

来源:7-1 简易扑克牌游戏

慕神6168092

2020-09-01 00:31

public class Card implements Comparable<Card> {
    String pattern; //花色
    
    String figure; //点数
    
    public Card(String pattern, String figure) {
        this.pattern = pattern;
        this.figure = figure;
    }
    
    /**	
    * 比较对象:当前扑克牌与传入的扑克牌	
    * 比较内容:点数、花色	
    * 比较步骤: 	
    * 对比二者的点数,	
    * 1.若点数相同,	
    * 	 (1)若当前扑克牌的花色较大,则返回正整数。	
    * 	 (2)若当前扑克牌的花色较小,则返回负整数。	
    * 2.若点数不同,	
    * 	 (1)若当前扑克牌的点数较大,则返回正整数。	
    * 	 (2)若当前扑克牌的点数较大,则返回负整数。	
    */	
    @Override	
    public int compareTo(Card c) {		
        if(this.figure.equals("10")) {			
            if(c.figure.equals(this.figure)) {				
                if(this.pattern.equals("方片")) {					
                    return -1;				
                }else if((this.pattern.equals("梅花")) && (!(c.pattern.equals("方片")))) {					
                    return -1;				
                }else if((this.pattern.equals("红桃")) && (c.pattern.equals("黑桃"))) {		
                    return -1;				
                }else {			
                    return 1;				
                }			
            }else if((c.figure.equals("J")) || (c.figure.equals("Q")) || (c.figure.equals("K")) || (c.figure.equals("A"))) {	
                return -1;			
            }else {				
                return 1;			
            }		
        }else if(this.figure.equals("Q")) {			
            if(c.figure.equals(this.figure)) {				
                if(this.pattern.equals("方片")) {					
                    return -1;				
                }else if((this.pattern.equals("梅花")) && (!(c.pattern.equals("方片")))) {	
                    return -1;				
                }else if((this.pattern.equals("红桃")) && (c.pattern.equals("黑桃"))) {		
                    return -1;				
                }else {					
                    return 1;		
                }			
            }else if((c.figure.equals("K")) || (c.figure.equals("A"))) {	
                return -1;			
            }else {				
                return 1;			
            }		
        }else if(this.figure.equals("A")) {			
            if(c.figure.equals(this.figure)) {				
                if(this.pattern.equals("方片")) {					
                    return -1;				
                }else if((this.pattern.equals("梅花")) && (!(c.pattern.equals("方片")))) {	
                    return -1;				
                }else if((this.pattern.equals("红桃")) && (c.pattern.equals("黑桃"))) {		
                    return -1;				
                }else {				
                    return 1;	
                }			
            }else {			
                return 1;	
            }
        }else if((!(this.figure.equals("J"))) && (!(this.figure.equals("K")))) {		
            if(c.figure.equals(this.figure)) {				
                if(this.pattern.equals("方片")) {					
                    return -1;				
                }else if((this.pattern.equals("梅花")) && (!(c.pattern.equals("方片")))) {	
                    return -1;				
                }else if((this.pattern.equals("红桃")) && (c.pattern.equals("黑桃"))) {		
                    return -1;				
                }else {			
                    return 1;				
                }			
            }else if(c.figure.equals("10")) {				
                return -1;			
            }else {				
                return this.figure.compareTo(c.figure);			
            }		
        }else if(this.figure.equals("J")) {			
            if(c.figure.equals(this.figure)) {				
                if(this.pattern.equals("方片")) {					
                    return -1;				
                }else if((this.pattern.equals("梅花")) && (!(c.pattern.equals("方片")))) {	
                    return -1;				
                }else if((this.pattern.equals("红桃")) && (c.pattern.equals("黑桃"))) {		
                    return -1;				
                }else {					
                    return 1;			
                }			
            }else if(c.figure.equals("A")) {				
                return -1;			
            }else {				
                return this.figure.compareTo(c.figure);			
            }		
        }else if(this.figure.equals("K")) {			
            if(c.figure.equals(this.figure)) {				
                if(this.pattern.equals("方片")) {					
                    return -1;				
                }else if((this.pattern.equals("梅花")) && (!(c.pattern.equals("方片")))) {	
                    return -1;				
                }else if((this.pattern.equals("红桃")) && (c.pattern.equals("黑桃"))) {		
                    return -1;				
                }else {					
                    return 1;				
                }			
            }else if(c.figure.equals("Q")) {				
                return 1;			
            }else if(c.figure.equals("A")) {				
                return -1;			
            }else {				
                return this.figure.compareTo(c.figure);			
            }		
        }		
        return 0;	
    }	
    
}
写回答 关注

1回答

  • 慕圣0830664
    2020-10-26 15:32:18

    你现在练的是外功,也就是招式,谈功力(内功)为时尚早,先将基本功打好。多看看别人写的代码,取长补短,事半功倍。实现功能只是第一步,优化永无止境。奔跑吧,少年!

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409792 学习 · 4340 问题

查看课程

相似问题