21点游戏.docx

上传人:b****8 文档编号:12948738 上传时间:2023-06-09 格式:DOCX 页数:17 大小:18.75KB
下载 相关 举报
21点游戏.docx_第1页
第1页 / 共17页
21点游戏.docx_第2页
第2页 / 共17页
21点游戏.docx_第3页
第3页 / 共17页
21点游戏.docx_第4页
第4页 / 共17页
21点游戏.docx_第5页
第5页 / 共17页
21点游戏.docx_第6页
第6页 / 共17页
21点游戏.docx_第7页
第7页 / 共17页
21点游戏.docx_第8页
第8页 / 共17页
21点游戏.docx_第9页
第9页 / 共17页
21点游戏.docx_第10页
第10页 / 共17页
21点游戏.docx_第11页
第11页 / 共17页
21点游戏.docx_第12页
第12页 / 共17页
21点游戏.docx_第13页
第13页 / 共17页
21点游戏.docx_第14页
第14页 / 共17页
21点游戏.docx_第15页
第15页 / 共17页
21点游戏.docx_第16页
第16页 / 共17页
21点游戏.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

21点游戏.docx

《21点游戏.docx》由会员分享,可在线阅读,更多相关《21点游戏.docx(17页珍藏版)》请在冰点文库上搜索。

21点游戏.docx

21点游戏

21点游戏

package二十一点游戏;

importjava.awt.Dimension;

importjava.awt.Rectangle;

importjava.awt.Toolkit;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.util.Vector;

importjavax.swing.ImageIcon;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JMenu;

importjavax.swing.JMenuBar;

importjavax.swing.JMenuItem;

importjavax.swing.JOptionPane;

publicclassCardFrameextendsJFrameimplementsActionListener{

/**

*@author21点游戏

*/

privatestaticfinallongserialVersionUID=1L;

JMenuBarmenuBar;//定义菜单栏

JMenufileMenu;//定义文件菜单

JMenuhelpMenu;

JMenuItemexitItem;//定义退出菜单项

JMenuItemaboutItem;

//显示牌区

JLabellabel1=newJLabel();

JLabellabel2=newJLabel();

JLabellabel3=newJLabel();

JLabellabel4=newJLabel();

JLabellabel5=newJLabel();

JLabellabel6=newJLabel();

JLabellabel7=newJLabel();

JLabelplayer=newJLabel();

JLabelcomputer=newJLabel();

//用于放置52张纸牌图片的标签框

JLabelgame[]=newJLabel[52];

//定于纸牌管理对象

CardManager18cm18=newCardManager18();

//记录抓牌数

intcount=0;

//定义电脑点数

intcomputer_dot=0;

//定义玩家点数

intgame_dot=0;

//存储电脑抓的纸牌

Vectorv18=newVector();

//洗牌按钮

JButtonbutton1;

//开始游戏按钮

JButtonbutton2;

//玩家抓牌按钮

JButtonbutton3;

//游戏结束按钮

JButtonbutton4;

intplayer_int;//玩家积分

intcomputer_int;//电脑积分

publicCardFrame()

{

getContentPane().setLayout(null);

menuBar=newJMenuBar();

fileMenu=newJMenu("文件");

helpMenu=newJMenu("帮助");

exitItem=newJMenuItem("退出");

aboutItem=newJMenuItem("关于");

button1=newJButton("洗牌");

//设置按钮在窗口中的位置

button1.setBounds(newRectangle(80,388,90,31));

//设置按钮的可编辑性

button1.setEnabled(true);

//为按钮添加事件监听

button1.addActionListener(this);

button2=newJButton("开始游戏");

button2.setBounds(newRectangle(263,388,90,31));

button2.setEnabled(false);

button2.addActionListener(this);

button3=newJButton("玩家抓牌");

button3.setBounds(newRectangle(433,388,90,31));

button3.setEnabled(false);

button3.addActionListener(this);

button4=newJButton("本轮结果");

button4.setBounds(newRectangle(630,388,90,31));

button4.setEnabled(false);

button4.addActionListener(this);

label1.setText("电脑显示牌区");

label1.setBounds(newRectangle(104,330,95,38));

label2.setText("玩家显示牌区");

label2.setBounds(newRectangle(604,330,95,38));

//积分统计区

label3.setText("积分统计区");

label3.setBounds(newRectangle(330,170,120,15));

player.setText("玩家积分:

"+"0分");

player.setBounds(newRectangle(330,215,150,15));

computer.setText("电脑积分:

"+"0分");

computer.setBounds(newRectangle(330,260,150,15));

//游戏规则区

label4.setText("游戏规则");

label4.setBounds(newRectangle(685,0,100,15));

label5.setText("赢:

3分");

label5.setBounds(newRectangle(685,15,100,15));

label6.setText("平局:

1分");

label6.setBounds(newRectangle(685,30,100,15));

label7.setText("输:

0分");

label7.setBounds(newRectangle(685,45,100,15));

menuBar.add(fileMenu);//添加菜单到菜单栏中

menuBar.add(helpMenu);

fileMenu.add(exitItem);//添加菜单项到菜单中

helpMenu.add(aboutItem);

exitItem.addActionListener(newMenuListerner());//添加文件菜单的监听器

aboutItem.addActionListener(newAboutListerner());

this.setTitle("二十一点游戏");

this.setResizable(false);

this.setJMenuBar(menuBar);//添加菜单栏到窗口中

this.setSize(800,500);//设置窗口大小

//把按钮添加进窗口中

this.getContentPane().add(label1);

this.getContentPane().add(label2);

this.getContentPane().add(label3);

this.getContentPane().add(label4);

this.getContentPane().add(label5);

this.getContentPane().add(label6);

this.getContentPane().add(label7);

this.getContentPane().add(player);

this.getContentPane().add(computer);

this.getContentPane().add(button1);

this.getContentPane().add(button2);

this.getContentPane().add(button3);

this.getContentPane().add(button4);

//获得屏幕的宽和高

DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();

//获得当前窗体的宽和高

DimensionframeSize=this.getSize();

//设置窗体居中

if(frameSize.height>screenSize.height){

frameSize.height=screenSize.height;

}

if(frameSize.width>screenSize.width){

frameSize.width=screenSize.width;

}

this.setLocation((screenSize.width-frameSize.width)/2,

(screenSize.height-frameSize.height)/2);

//设置窗口可见

this.setVisible(true);

}

//菜单监听器

privateclassMenuListernerimplementsActionListener

{

@Override

publicvoidactionPerformed(ActionEvente){

System.exit(0);

}

}

//关于监听器

privateclassAboutListernerimplementsActionListener

{

@Override

publicvoidactionPerformed(ActionEvente){

newAboutFrame();

}

}

publicstaticvoidmain(Stringargs[])

{

newCardFrame();

}

@Override

publicvoidactionPerformed(ActionEvente){

//洗牌

if(e.getSource()==button1){

//关闭洗牌按钮,开启开始游戏按钮

button2.setEnabled(true);

button1.setEnabled(false);

//对计数器、电脑点数、玩家点数等的初始化

count=0;

computer_dot=0;

game_dot=0;

v18.clear();

//把标签框控件数组放入窗体的窗格中

cm18.gameStart(game,this.getContentPane());

//初始化一副牌

cm18.initCard();

//随机打乱这副牌

cm18.randomCard();

}

//开始游戏

if(e.getSource()==button2){

//关闭开始游戏按钮,开启玩家抓牌按钮

button3.setEnabled(true);

button2.setEnabled(false);

//电脑抓牌

for(;;){

game[count].setIcon(newImageIcon("images/rear.gif"));

game[count].setBounds(newRectangle(50+count*20,200,71,96));

getContentPane().setComponentZOrder(game[count],1);

//设置电脑的第一张牌为正面显示

if(count==0){

game[count].setIcon(newImageIcon("images/"+cm18.card18[count].getSuit()+

"-"+cm18.card18[count].getValue()+".gif"));

game[count].setBounds(newRectangle(50+count*20,200,71,96));

this.getContentPane().setComponentZOrder(game[count],1);

}

//将牌JQK的面值设为10点

if(cm18.card18[count].getValue()>10){

computer_dot=computer_dot+10;

}else{

computer_dot=computer_dot+cm18.card18[count].getValue();

}

v18.add(cm18.card18[count]);

getContentPane().repaint();

count+=1;

//如果面值总数大于18则停止抓牌

if(computer_dot>=18){

return;

}

}

}

//玩家抓牌按钮

if(e.getSource()==button3){

//提示玩家

if(game_dot>=15){

inta=JOptionPane.showConfirmDialog(null,"现在点数为"+game_dot+"是否再抓牌","提示"

JOptionPane.YES_NO_OPTION);

if(a==JOptionPane.NO_OPTION){

button3.setEnabled(false);

button4.setEnabled(true);

return;

}

}

//设置标签框显示抓到的纸牌

game[count].setIcon(newImageIcon("images/"+cm18.card18[count].getSuit()+

"-"+cm18.card18[count].getValue()+".gif"));

game[count].setBounds(newRectangle(450+count*20,200,71,96));

this.getContentPane().setComponentZOrder(game[count],1);

//计算抓到纸牌的面值

if(cm18.card18[count].getValue()>10){

game_dot=game_dot+10;

}else{

game_dot=game_dot+cm18.card18[count].getValue();

}

//记录抓到的牌数

count+=1;

//面值大于21停止抓牌,关闭玩家抓牌按钮,开启本轮结果按钮

if(game_dot>21){

button3.setEnabled(false);

button4.setEnabled(true);

return;

}

}

//本轮结果按钮

if(e.getSource()==button4){

//把电脑的纸牌翻过来

for(inti=0;i

Card18card18=(Card18)v18.get(i);

game[i].setIcon(newImageIcon("images/"+card18.getSuit()+

"-"+card18.getValue()+".gif"));

game[i].setBounds(newRectangle(50+i*20,200,71,96));

this.getContentPane().setComponentZOrder(game[i],1);

}

//计算胜负

Stringgame_over="";

if(game_dot>21&&computer_dot<=21){

game_over="电脑获胜";

computer_int+=3;

}elseif(game_dot<=21&&computer_dot>21){

game_over="玩家获胜";

player_int+=3;

}elseif(game_dot>=21&&computer_dot>=21){

game_over="平局";

player_int+=1;

computer_int+=1;

}elseif(game_dot>computer_dot){

game_over="玩家获胜";

player_int+=3;

}elseif(game_dot

game_over="电脑获胜";

computer_int+=3;

}elseif(game_dot==computer_dot){

game_over="平局";

player_int+=1;

computer_int+=1;

}

//以对话框的方式显示胜负

Stringmessage="游戏结果\n";

message+="电脑点数:

"+String.valueOf(computer_dot)+"\n";

message+="玩家点数:

"+String.valueOf(game_dot)+"\n";

message+="游戏结果:

"+game_over;

JOptionPane.showMessageDialog(null,message,"本轮游戏结果",JOptionPane.INFORMATION_MESSAGE);

//显示积分

player.setText("玩家积分:

"+player_int+"分");

computer.setText("电脑积分:

"+computer_int+"分");

//设置命令按钮可操作

button1.setEnabled(true);

button2.setEnabled(false);

button3.setEnabled(false);

button4.setEnabled(false);

}

}

}

package二十一点游戏;

publicclassCard18{

privateintsuit=0;

privateintvalue=0;

Card18(intsuit,intvalue){

this.suit=suit;

this.value=value;

}

publicintgetSuit(){

returnsuit;

}

publicvoidsetSuit(intsuit){

this.suit=suit;

}

publicintgetValue(){

returnvalue;

}

publicvoidsetValue(intvalue){

this.value=value;

}

}

package二十一点游戏;

importjava.awt.Container;

importjava.awt.Rectangle;

importjavax.swing.ImageIcon;

importjavax.swing.JLabel;

publicclassCardManager18{

publicCard18[]card18=newCard18[52];

//初始化牌

publicvoidinitCard(){

for(inti=1;i<=4;i++){

for(intj=1;j<=13;j++){

card18[(i-1)*13+j-1]=newCard18(i,j);

}

}

}

//随机洗牌

publicvoidrandomCard(){

Card18temp=null;

for(inti=0;i<=52;i++){

inta=(int)(Math.random()*52);

intb=(int)(Math.random()*52);

temp=card18[a];

card18[a]=card18[b];

card18[b]=temp;

}

}

//发牌

publicvoidgameStart(JLabelgame[],Containerc18){

//在容器中删除标签组件

if(game[0]!

=null){

for(inti=0;i<52;i++){

c18.remove(game[i]);

}

c18.repaint();

}

//在容器中放置52个标签组件用于盛放图片

for(inti=0;i<52;i++){

game[i]=newJLabel();

game[i].setBorder(javax.swing.BorderFactory

.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

game[i].setBounds(newRecta

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 动态背景

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2