斗地主游戏的开发 业务需求分析 业务:共有54张牌 点数:“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“J”,“Q”,“K”,“A”,“2” 花色:“♠”,“❤”,“♣”,“♦” 大小王:“👲”,“🃏” *点数分别要组合成4种花色,大小王各一张 斗地主:发出51张牌,剩下3张作为底牌
主程序
public class Gamedome {
public static void main ( String [ ] args) {
Room m = new Room ( ) ;
m. start ( ) ;
}
}
先创建一个牌类
public class Card {
private String number;
private String color;
private int size;
public Card ( ) {
}
public Card ( String number, String color, int size) {
this . number = number;
this . color = color;
this . size = size;
}
public String getNumber ( ) {
return number;
}
public void setNumber ( String number) {
this . number = number;
}
public String getColor ( ) {
return color;
}
public void setColor ( String color) {
this . color = color;
}
public int getSize ( ) {
return size;
}
public void setSize ( int size) {
this . size = size;
}
@Override
public String toString ( ) {
return color + number ;
}
}
创建房间类
import java. util. * ;
public class Room {
private List < Card > allCards = new ArrayList < > ( ) ;
public Room ( ) {
String [ ] numbers = { "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" , "2" } ;
String [ ] colors = { "♠" , "❤" , "♣" , "♦" } ;
int size = 0 ;
for ( String number : numbers) {
size++ ;
for ( String color : colors) {
Card c = new Card ( number, color, size) ;
allCards. add ( c) ;
}
}
Card c1 = new Card ( "" , "🃏" , ++ size) ;
Card c2 = new Card ( "" , "👲" , ++ size) ;
Collections . addAll ( allCards, c1, c2) ;
System . out. println ( "新牌" + allCards) ;
}
public void start ( ) {
Collections . shuffle ( allCards) ;
System . out. println ( "洗牌后" + allCards) ;
List < Card > L = new ArrayList < > ( ) ;
List < Card > C = new ArrayList < > ( ) ;
List < Card > T = new ArrayList < > ( ) ;
for ( int i = 0 ; i < allCards. size ( ) - 3 ; i++ ) {
Card c = allCards. get ( i) ;
if ( i % 3 == 0 ) {
L . add ( c) ;
} else if ( i % 3 == 1 ) {
C . add ( c) ;
} else if ( i % 3 == 2 ) {
T . add ( c) ;
}
}
sortCards ( L ) ;
sortCards ( C ) ;
sortCards ( T ) ;
System . out. println ( "L的牌" + L ) ;
System . out. println ( "C的牌" + C ) ;
System . out. println ( "T的牌" + T ) ;
List < Card > lastThreeCards = allCards. subList ( allCards. size ( ) - 3 , allCards. size ( ) ) ;
System . out. println ( "底牌" + lastThreeCards) ;
C . addAll ( lastThreeCards) ;
sortCards ( C ) ;
System . out. println ( "C抢到地主后" + C ) ;
}
private void sortCards ( List < Card > cards) {
Collections . sort ( cards, new Comparator < Card > ( ) {
@Override
public int compare ( Card o1, Card o2) {
return o2. getSize ( ) - o1. getSize ( ) ;
}
} ) ;
}
}