import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Poker extends JFrame {
private Card deck[];
private Card hand[];
private int currentCard;
private JButton sendButton,shuffleButton; //two button
private JTextArea displayCard,status;//show
private int numbers,samecolor; //mark same value card and same color card
private String values[],colors[],output;
public Poker() {
super("Pork game program");
String valueArray[]={"1","2","3","4","5","6","7","8","9","10","11","12","13"};
String colorArray[]={"red","green","yellow","blue"};
values=valueArray;
colors=colorArray;
deck=new Card[52];
hand=new Card[5];
currentCard=-1;
//initialize
for(int count=0;count<deck.length;count++)
{deck[count]=new Card(values[count%13],colors[count/13]);}
Container container=getContentPane();
container.setLayout(new FlowLayout());
sendButton=new JButton("send cards");
sendButton.addActionListener(new ActionListener(){//anonymous inner class
public void actionPerformed(ActionEvent e){
numbers=0; //clear to zero
samecolor=0; //clear to zero
displayCard.setText(" "); //clear text area
output=" ";
//send a round of cards
for(int count=0;count<hand.length;count++){
Card sendt=sendCard();
if(sendt!=null){
hand[count]=sendt;
displayCard.setText(displayCard.getText()+hand[count].toString()+"\n");
}
else{
displayCard.setText("Not enough card to send");
status.setText("shuffle cards to continue");
return ;}
}
for(int count=0;count<hand.length-1;count++){
int i=count+1;
for(;i<hand.length;i++){
if(hand[count].getvalue().equals(hand[i].getvalue()))
++numbers;
if(hand[count].getcolor().equals(hand[i].getcolor()))
++samecolor;
}
}
pairs();
threecolor();
}
}//end anonymous inner class
);//end call to addActionListenner
container.add(sendButton);
shuffleButton=new JButton("shuffle cards");
shuffleButton.addActionListener(new ActionListener(){//anonymous inner class
//shuffle deck
public void actionPerformed(ActionEvent e){
status.setText(" "); //clear text area
numbers=0;
samecolor=0;
displayCard.setText("shuffling...");
shuffle();
displayCard.setText("card is shuffled");
}
}//end anonymous innner class
);//end call to addActionListener
container.add(shuffleButton);
displayCard=new JTextArea(6,20);
displayCard.setEditable(false);
container.add(displayCard);
status=new JTextArea(6,20);
displayCard.setEditable(false);
container.add(status);
setSize(300,250); //set the window size
show(); //show the window
}//end Poker
public void pairs(){
if(numbers>=1){
output+=("has same value!\n");
}
status.setText(output);
}
public void threecolor(){
if(samecolor>=3){
output+=("has three same color card !\n");
}
status.setText(output);
}
public void shuffle()
{currentCard=-1;
//swap two cards as many times as there are cards in the deck
for (int first= 0; first<deck.length;first++){
int second=(int)(Math.random()*deck.length);
Card temp=deck[first];
deck[first]=deck[second];
deck[second]=temp;
}
sendButton.setEnabled(true);
}
public Card sendCard()
{if(++currentCard<deck.length)
return deck[currentCard];
else{
sendButton.setEnabled(false);
return null;
}
}
// main()
public static void main(String args[]){
Poker application=new Poker();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class Card{
private String value;
private String color;
public Card(String f,String s)
{
value=f;
color=s;
}
protected String getcolor()
{
return color;
}
protected String getvalue()
{
return value;
}
public String toString()
{
return color+" "+value;
}
}
}
这个是我费力编的代码,实现了PART2 部分,但是关于按颜色和数字排序不会排列
特别是按颜色排列,要按指令的颜色排列顺序,然后再按数字排列
希望高手指教 !3 Q