| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 600 人关注过本帖
标题:[求助]排序问题
只看楼主 加入收藏
much13
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-11-11
收藏
 问题点数:0 回复次数:5 
[求助]排序问题

1 编写一个图形界面的java application 程序,实现一个纸牌游戏。
2 一副牌有52张,四中花色, 以颜色来区分red,green,yellow,blue, 每种颜色都有13个不同的数字。

3 按钮控制“洗整幅牌”。

4 按钮控制发5张牌,并自动为5张牌排序:先颜色,后数字(升序)。并显示出来。
part2
5 胜利条件:
pair:2张以上的牌数字相同
color:3张牌同色

6 如果发一次牌没有满足胜利条件,从剩下的牌中,继续发5张牌。直到牌不足为止。

7 游戏中所有状态均有显示。
刚学习JAVA就做这个题目,感觉很麻烦...努力好久了,才做一部分,希望那位大侠帮帮我..
实在是没想到解决办法. 主要的困难是,不知道怎么排序 .排序:先颜色,后数字(升序)

搜索更多相关主题的帖子: 游戏 java yellow color 胜利 
2006-11-13 12:04
much13
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-11-11
收藏
得分:0 

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

2006-11-13 12:08
much13
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-11-11
收藏
得分:0 

帮帮忙啊 告诉我怎么排颜色,我觉得没什么可比较的,又不象数字可以比较,然后再排列
关键是用过什么方法,让颜色有 任意大小一样,然后再比较.而且这个大,啊要我自己定义,我要red最大red就应该最大
我要green 最大green就应该最大..有了大小后就好比较了...

2006-11-13 12:59
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
排颜色还不是一样的
你可以定义一个字母来代表颜色啊
大小还不一样都是你定的

可惜不是你,陪我到最后
2006-11-13 13:01
much13
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-11-11
收藏
得分:0 
可是 我能在把red=a这样定义么?
String colorArray[]={"red","green","yellow","blue"};
red 本来就是数组里面的元素
2006-11-13 13:12
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
你可以从数组里面随机取下标啊

可惜不是你,陪我到最后
2006-11-13 13:26
快速回复:[求助]排序问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021609 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved