/**
* This class creates window can be used for playing games.
* It shuffles 24 cards and randomly come out one of them
* when ueer click Shuffle button at the begining of the game
* or click the 0~8 button for location after the first card
* has been located. When nine cards have been located, the
* prigram compare each cards with the cards on the same row
* and colume and give out the score.
*
* This code was given out on webCT on 15 May 2011
* by Instructor Name. The constructor, dealcard method, shuffle
* method and main methods were provided.
* Changes:
*
* 1. Added inner class CardListener, which is the actionlistener
* for the ArrayList cards button.
* 2. Added calcuScore method to calculate the score when the
* game is over.
* 3. Added codes for cunstructor.
*
* @author Deitel and Deitel,
* fromFrom Java How to Program Deitel and Deitel
* @author Xiaoyuan Lu
* @version Last edit on 18 May 2011
*/
import javax.swing.*; //import for thr ues of GUI
import java.util.*; //import for the use ArrayLiast
import java.lang.String; //import for thr ues of String
import java.awt.*;
import java.awt.event.*;
public class DeckOfCards extends JFrame {
static public boolean result=true;
private Card deck[];
private int currentCard;
private JLabel status;
private ArrayList<JButton> cardButton; //create an ArrayList for the nine cardButton
private JButton shuffleButton;
private JButton skipButton; //create the skipButton
private Card tempc; //temp object of class crad
private int clicktimes=0,score=0,hands=0; //the there int instance count the number.
private String faceOfLocate[][],suitOfLocate[][]; //these two two-demention array
private boolean ace=true;
public DeckOfCards() {
super("Card Grid game");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six"};
String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
deck = new Card[24];
suitOfLocate=new String[3][3]; // Create an object of two-demention String
faceOfLocate=new String[3][3]; // Create an object of two-demention String
currentCard = -1;
for (int i= 0; i < deck.length;i++)
deck[i] = new Card(faces[i % 6], suits[i / 6]);
JPanel p1=new JPanel();
p1.setLayout(new GridLayout(3,3)); //Create the layout for p1,which is for 9 button.
Container c = getContentPane();
c.setLayout(new BorderLayout());
cardButton= new ArrayList<JButton>(); //create 9 button
for(int i=0;i<=8;i++){
cardButton.add(new JButton(""));
cardButton.get(i).setEnabled(false);
p1.add(cardButton.get(i));
cardButton.get(i).setSize(70, 70);
cardButton.get(i).addActionListener(new CardListener()); //create its listener
}
JPanel p2= new JPanel(new FlowLayout()); //create the layout for p2, which is the panel foe shufflebutton and lable
shuffleButton = new JButton("Shuffle cards"); //create shuffleButton and set text on it
shuffleButton.setSize(80, 20);
shuffleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //set ActionListener for shuffleButton
status.setText("SHUFFLING ...");
skipButton.setEnabled(true);
for(int i=0;i<=8;i++){
cardButton.get(i).setEnabled(true);
cardButton.get(i).setText(""+i);
}
shuffle();
tempc=dealCard();
status.setText(tempc.toString());
shuffleButton.setEnabled(false);
}
});
skipButton = new JButton("Skip(Once)"); //create object skipButton, and set its text:Skip(Once)
skipButton.setEnabled(false); //set it initially be disabled
skipButton.addActionListener(new ActionListener(){ //create its ActionListener
public void actionPerformed(ActionEvent e){
shuffle();
tempc=dealCard(); //get a new card
status.setText(tempc.toString());
skipButton.setEnabled(false);
}
}
);
p2.add( shuffleButton); //add shuffleButton to the p2 panel
p2.add(skipButton);
status = new JLabel("SHUFFLE CARDS TO BEGIN");
p2.add( status); //add status to p2 panel
c.add(BorderLayout.NORTH,p2); //add p2 panel to the up of the window
c.add(BorderLayout.CENTER,p1); //add p1 panel to the buttom of the window
setSize(500, 500); // set the window size
setLocation(500,100);
setVisible(true); // show the window
/**
* The constructor of class Deck of cards , create the object of a window
* 9 buttons for cards location, a shuffle button, and a label.
* The method set the location of each component and create their listener function.
*
* It is adapted from the original constructor of Deck of Card downloaded from WebCt.
*/
}
public class CardListener implements ActionListener{ //create an inner class for actionListener for cardButton
public void actionPerformed(ActionEvent e){
((AbstractButton) e.getSource()).setText(tempc.toString()); //set the text of action source to current card
clicktimes=clicktimes+1; //add 1 to clicktime
String[] strSplit = ((String) ((AbstractButton) e.getSource()).getText().toString()).split(" "); //divide the string into 3 pices, which are the face of current card,"of" and suit of current card
faceOfLocate[cardButton.indexOf(e.getSource())/3][cardButton.indexOf(e.getSource())%3]=strSplit[0]; //put the face of current card into the faceOfLocate array
suitOfLocate[cardButton.indexOf(e.getSource())/3][cardButton.indexOf(e.getSource())%3]=strSplit[2]; //put the face of current card into the suitOfLocate array
tempc=dealCard();
status.setText(tempc.toString()); //come out the next card
if(clicktimes==9){ //if all the button has benn clicked, start a new game
calcuScore();
ResultOfGame re = new ResultOfGame(score,hands); //pop a window
re.setVisible(result);
shuffleButton.setEnabled(true);
clicktimes=0; //set the instances to 0 to start a new game
score=0;
hands=0;
}
((AbstractButton)e.getSource()).setEnabled(false);
}
/**
* The CardListener class is an inner class od class DeckOfCard, this class is a listener
* of arrayList cardButton. Every time when user click one of the nine cardButton
* the previous slected card will be placed on the locate of the botton of 3*3 grid,
* and the face and suit will be placed in two two-dementional array.
*/
}
public void calcuScore(){
for(int i=0;i<=2;i++){ //check each row for hands
if((suitOfLocate[i][0].equals(suitOfLocate[i][1])&&(suitOfLocate[i][1]).equals(suitOfLocate[i][2]))){ //check if the three faces of the same row is the same
for(int j=0;j<=2;j++) { //if the three faces of the same row is the same, check if there is an Ace
if (faceOfLocate[i][j].equals("Ace")){
ace=true; //there is an "Ace",set ace to true
break; //and break
}
else
ace=false;
}
if(ace)
score=score+1000; //if ace is ture, means there is an Ace, plus 1000 to the total score
else
score=score+500; //if ace is false, means there is not, plus 500 to the total score
}
if((faceOfLocate[i][0].equals(faceOfLocate[i][1]))||(faceOfLocate[i][1].equals(faceOfLocate[i][2]))||(faceOfLocate[i][0].equals(faceOfLocate[i][2]))){ //check if there are two same suit in the same row
score=score+100; //add 100 to the total score
hands=hands+1; //add 1 to hands
}
}
for(int i=0;i<=2;i++){ //check each colum for hands
if((suitOfLocate[0][i].equals(suitOfLocate[1][i]))&&(suitOfLocate[1][i].equals(suitOfLocate[2][i]))){ //check if the three faces of the same colum is the same
for(int j=0;j<=2;j++){
if (faceOfLocate[j][1]=="Ace"){ //if the three faces of the same colum is the same, check if there
北京邮电大学 java 课设作业
4星 · 超过85%的资源 需积分: 19 4 浏览量
2011-09-18
10:20:27
上传
评论 2
收藏 14KB ZIP 举报
revoir
- 粉丝: 0
- 资源: 4
最新资源
- 前端开发-什么是前端开发-关于前端开发的一些相关介绍
- Sora AI-关于文生视频的使用场景说明
- suno AI文生视频的相关教程和介绍使用
- 什么是后端开发-关于后端开发的一些小介绍分享
- Jurassic Pack Vol. II Dinosaurs 侏罗纪包卷恐龙二号Unity游戏模型资源unitypackage
- Jurassic Pack Vol. III Dinosaurs 侏罗纪包卷恐龙三号Unity游戏模型资源unitypackag
- Ultimate Seating Controller 终极座椅控制器Unity游戏开发插件资源unitypackage
- 什么是人工智能-关于人工智能的相关介绍说明
- Figma Converter for Unity适用Unity的Figma转换器Unity游戏开发插件unitypackage
- Creepy Animatronic Anims 令人毛骨悚然的电子动画Unity游戏动画插件资源unitypackage
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈