21点牌标准代码
21点源代码
package trys;public class Card {/*** <p>目标:获取和设置牌得类型和面值* <p>@param type,value* <p>@author Lee*/private int value=0; //代表纸牌的面值private int type=0; //代表纸牌的花色public Card(int type,int value){ //构造方法,给面值和花色赋值this.value=value;this.type=type;}//定义方法获取类型public int getType(){return type;}//定义方法获取面值public int getValue(){return value;}//定义方法设置纸牌的类型public void setType(int type){ this.type=type;}//定义方法设置纸牌的面值public void setValue(int value){ this.value=value;}}package trys;import java.awt.Container;import javax.swing.ImageIcon;import javax.swing.JLabel;public class CardManager {/*** <p>目的:初始化牌,生成,和发牌* <p>@version Lee*/public Card[] cards=new Card[52]; //定义数组存放纸牌/*初始化纸牌*/public void initCards(){for(int i=1;i<=4;i++){ //纸牌类型for(int j=1;j<=13;j++){ //纸牌面值cards[(i-1)*13+j-1]=new Card(i,j);}}}/*随机生成牌号*/public void randomCards(){Card temp=null; //从新定义纸牌的类型for(int i=0;i<52;i++){int a=(int)(Math.random()*52);int b=(int)(Math.random()*52);temp=cards[a];cards[a]=cards[b];cards[b]=temp;}}/*定义方法,发牌*/public void gameStart(JLabel game[],Container c){ //在容器中删除标签组件if(game[0]!=null){for(int i=0;i<52;i++){c.remove(game[i]);}c.repaint();}/*在容器中放置52个组件,用于盛放图片*/for(int i=0;i<52;i++){game[i]=new JLabel();game[i].setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));}//设置标签组件的图片为rear.gif,即牌得背面for(int i=0;i<52;i++){game[i].setIcon(newImageIcon("images/aabb.jpg"));}}}package trys;import java.awt.Dimension;import java.awt.Rectangle;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Vector;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;public class GameFrame extends JFrame implements ActionListener{JButton clear_btn=new JButton(); //洗牌按钮JButton compute_btn=new JButton(); //游戏开始按钮JButton game_btn=new JButton();//玩家按钮JButton gameover_btn=new JButton(); //游戏结束按钮JLabel game[]=new JLabel[52]; //放置52张牌得标签框CardManager cm=new CardManager(); //定义指派管理类的对象int i=0; //记录抓牌数int computer_dot=0; //记录电脑点数int game_dot=0; //记录玩家点数Vector v=new Vector(); //存储电脑抓的纸牌JLabel jLabel1=new JLabel();JLabel jLabel2=new JLabel();public GameFrame(){getContentPane().setLayout(null);this.setTitle("二十一点游戏");this.setSize(800,500);//获得当前屏幕的宽和高DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSiz e();Dimension frameSize=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.wid th)/2,(screenSize.height-frameSize.height)/2);clear_btn.setBounds(newRectangle(78,388,86,31)); //设置位置clear_btn.setText("洗牌"); //设置标签内容clear_btn.addActionListener(this); //注册监听器compute_btn.setBounds(newRectangle(233,388,86,31));compute_btn.setEnabled(false);//不能更改compute_btn.setText("开始游戏");compute_btn.addActionListener(this);game_btn.setBounds(newRectangle(413,389,91,32));game_btn.setEnabled(false);game_btn.setText("玩家抓牌");game_btn.addActionListener(this);gameover_btn.setBounds(newRectangle(625,390,91,32));gameover_btn.setEnabled(false);gameover_btn.setText("本轮结果");gameover_btn.addActionListener(this);JMenuBar mb=new JMenuBar(); //定义菜单条JMenu mnuFile=new JMenu("文件"); //定义菜单JMenu mnuHelp=new JMenu("帮助");JMenuItem mnuFileExit=new JMenuItem("退出"); //定义菜单项JMenuItem mnuHelpAbout=new JMenuItem("关于...");this.setJMenuBar(mb); //把菜单条添加到窗体上jLabel1.setText("电脑显示牌区");jLabel1.setBounds(newRectangle(104,330,95,38));jLabel2.setText("用户显示牌区");jLabel2.setBounds(newRectangle(499,343,92,33));mb.add(mnuFile); //将菜单加到菜单条中mb.add(mnuHelp);mnuFile.add(mnuFileExit); //将菜单项添加到菜单中mnuHelp.add(mnuHelpAbout);//对菜单产生的事件进行注册mnuFileExit.addActionListener(newActionListener(){public void actionPerformed(ActionEvent e){ System.exit(0);}});mnuHelpAbout.addActionListener(newActionListener(){public void actionPerformed(ActionEvent e){ new AboutFrame();}});this.getContentPane().add(jLabel2);this.getContentPane().add(jLabel1);this.getContentPane().add(game_btn);this.getContentPane().add(clear_btn);this.getContentPane().add(gameover_btn);this.getContentPane().add(compute_btn);this.setVisible(true);}public static void main(String [] args){GameFrame gameframe=new GameFrame();}public void actionPerformed(ActionEvent e){ //洗牌按钮if(e.getSource()==clear_btn){//关闭和开启相应的按钮compute_btn.setEnabled(true);clear_btn.setEnabled(false);//对记牌器,电脑点数和玩家点数进行初始化i=0; computer_dot=0; game_dot=0;//把标签控件数组放入窗体的窗格中cm.gameStart(game, this.getContentPane());cm.initCards(); //初始化一副纸牌cm.randomCards(); //随机打乱}//开始游戏按钮if(e.getSource()==compute_btn){//关闭和开启相应的按钮compute_btn.setEnabled(false);game_btn.setEnabled(true);//电脑抓牌for(int k=0;k<20;k++){game[i].setIcon(newImageIcon("images/aabb.jpg"));game[i].setBounds(newRectangle(50+i*20,200,71,96));getContentPane().setComponentZOrder(game[i],1);if(cm.cards[i].getValue()>10){computer_dot=computer_dot+1;}else{computer_dot=computer_dot+cm.cards[i].getValue() ;}v.add(cm.cards[i]);getContentPane().repaint();i=i+1;//如果面值大于15则停止抓牌if(computer_dot>15) return;}}//玩家抓牌按钮if(e.getSource()==game_btn){//提示玩家if(game_dot>=10){int a=JOptionPane.showConfirmDialog(null,"现在点数为:"+game_dot+"是否在抓牌","提示",JOptionPane.NO_OPTION);if(a==JOptionPane.NO_OPTION){game_btn.setEnabled(false);gameover_btn.setEnabled(true);return;}}//设置标签,显示抓到的纸牌game[i].setIcon(newImageIcon("images/"+cm.cards[i].getType()+"-"+cm.ca rds[i].getValue()+".jpg"));game[i].setBounds(newRectangle(350+i*20,200,71,96));this.getContentPane().setComponentZOrder(game[i],1);//计算扎到纸牌的面值if(cm.cards[i].getValue()<=10)game_dot=game_dot+cm.cards[i].getValue();else game_dot=game_dot+1;i=i+1; //记录抓到的牌数//面值大于21时停止抓牌,关闭和开启相应的按钮if(game_dot>21){game_btn.setEnabled(false);gameover_btn.setEnabled(true);return;}}//本轮游戏结束按钮if(e.getSource()==gameover_btn){//吧电脑的纸牌反过来for(int i=0;i<v.size();i++){Card card=(Card)v.get(i);game[i].setIcon(newImageIcon("images/"+card.getType()+"-"+card.getValu e()+".jpg"));game[i].setBounds(newRectangle(50+i*20,200,71,96));this.getContentPane().setComponentZOrder(game[i], 1);}//计算胜负String game_over="";if(game_dot>21 && computer_dot<=21)game_over="电脑获胜";else if(game_dot<=21 && computer_dot>=21) game_over="玩家获胜";else if(game_dot>=21 && computer_dot>=21) game_over="平局";else if(game_dot>computer_dot) game_over="玩家获胜";else if(game_dot<computer_dot) game_over="电脑获胜";else if(game_dot==computer_dot)game_over="平局";//以对话框的方式显示胜负String message="游戏结果\n";message=message+"电脑点数:"+String.valueOf(computer_dot)+"\n";message=message+"玩家点数:"+String.valueOf(game_dot)+"\n";message=message+"游戏结果:"+game_over;JOptionPane.showMessageDialog(null,message,"本轮游戏的结果",RMATION_MESSAGE);//设置按钮可操作clear_btn.setEnabled(true);compute_btn.setEnabled(true);game_btn.setEnabled(true);gameover_btn.setEnabled(true);}}}package trys;import java.awt.Dimension;import java.awt.Rectangle;import java.awt.Toolkit;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextArea;public class AboutFrame extends JFrame{JLabel jLabel1=new JLabel();JTextArea ta1=new JTextArea();JLabel jLabel2=new JLabel();JTextArea ta2=new JTextArea();public AboutFrame(){getContentPane().setLayout(null);jLabel1.setText("游戏规则");jLabel1.setBounds(new Rectangle(27,0,97,36));jLabel2.setText("声明");jLabel2.setBounds(new Rectangle(26,176,80,27));ta2.setEditable(false);ta2.setLineWrap(true);ta2.setBounds(new Rectangle(26,207,398,63));ta1.setColumns(40);ta1.setLineWrap(true);this.setTitle("关于");this.getContentPane().add(jLabel1);this.getContentPane().add(ta1);this.getContentPane().add(jLabel2);this.getContentPane().add(ta2);this.setEnabled(false);ta1.setText("电脑先抓牌,玩家后抓牌.计算自己的面值总额,比较面值数,如果面值总数都大于21点,则平局.如果玩家和电脑的面值总数有一个大于21点,另一个" +"不大于21点,则不大于21点的为赢家.如果都不大于21点,则点数多达额赢");ta1.setBounds(new Rectangle(25,36,392,130));ta2.setText("游戏中的,纸牌的图片来自windowXp的纸牌游戏,图片版权属于Java_Lee 所有");this.setSize(450,300);Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();//获得当前窗体的宽和高Dimension frameSize=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);}}。
21点玩家点数 玩家要牌不会爆的几率
玩家点数玩家要牌不会爆的几率(%) 玩家要牌会爆的几率(%)11点或以下100 012 69.4 30.613 61.5 38.514 53.6 46.415 46.2 53.816 38.5 61.517 30.7 69.318 23 7719 15.5 81.520 7.7 92.3庄家点数庄家在17至21点会停止的几率(%) 庄家会爆的几率(%)2 70 303 62 384 60 405 57 436 57 437 75 258 76 249 77 2310 77 23A 82 18例如: 你有15点, 参照上面二个表,这时你要牌不会爆的几率为46.2%, 要牌会爆的几率为53.8%, 这样看,还是不要牌好, 因为要牌会爆的几率大于不会爆的几率, 事实上还要看庄家的情况, 如果庄家牌面为9点, 少于17点, 庄家必须要牌, 庄家在17至21点会停止的几率为77 %, 庄家会爆的几率为23%, 即你如果无所为, 不要牌了, 等庄家爆死, 你自动赢, 几率是23%, 而你要牌不会爆, 即你要牌会赢的几率为46.2%, 结论你还是应该要牌, 所以就产生如下了基本策略。
分一副牌和多副牌两种基本策略.一副牌基本策略上面一行代表庄家持牌, 左边一列代表玩家持牌硬牌(A计为1时是"硬"牌)硬牌2 3 4 5 6 7 8 9 10 A8 要要要双双要要要要要9 双双双双双要要要要要10 双双双双双双双双要要11 双双双双双双双双双双12 要要停停停要要要要要13 停停停停停要要要要要14 停停停停停要要要要要15 停停停停停要要要要要16 停停停停停要要要要要17-20 停停停停停停停停停停软牌(A计为11时是"软"牌) 软牌2 3 4 5 6 7 8 9 10 A A 2 要要双双双要要要要要A 3 要要双双双要要要要要A 4 要要双双双要要要要要A 5 要要双双双要要要要要A 6 双双双双双要要要要要A 7 停双双双双停停要要要A 8 停停停停双停停停停停A 9 停停停停停停停停停停对子分牌对子2 3 4 5 6 7 8 9 10 A2 2 要分分分分分要要要要3 3 要分分分分分要要要要4 4 要要要双双要要要要要5 5 双双双双双双双双要要6 6 分分分分分要要要要要7 7 分分分分分分要要要要8 8 分分分分分分分分停分9 9 分分分分分停分分停停10's 停停停停停停停停停停A's 分分分分分分分分分分要=要牌停=停叫双=双倍分=分牌。
C++PlayCards21点游戏(源码)
private:
string name;
Card cd[52];
int num;
int total;
};
class Game
{
public:
Game(int num,string s)
{
playernum=num;
my_house.Setname(s);
cout<<"("<<my_player[i].GetTotal()<<")"<<endl;
}
}
void Being()
{
char temp;
int i;
for(i=0;i<playernum;i++){
int t=0;
cout<<"玩家"<<i+1<<":"<<endl;
}
void Prepare()
{
int i;
for(i=0;i<playernum;i++){
cout<<"请输入玩家"<<i+1<<"的名字:";
my_player[i].Setname();
cout<<endl;
}
}
void Begin()
}
void Setname()
{
cin>>name;
}
void Add(Card c)
C语言21点游戏
C语言21点游戏#include#include#include#include#includeint Bookies_score=1000,/*庄家金币*/Rlayer_score=1000;//玩家金币int Bookiesscore=0,/*庄家当前点数*/Rlayerscore=0;//玩家当前点数int Random,/*玩家随机数*/Randoms;//庄家随机数int Bets_P;//玩家下注int Card;//是否发牌int i=1;int s=1;int main(void){Star_interface();//开始界面getch();system("CLS");//---------------------------------以下为产生玩家随机数发牌代码Judge_WorL();//输或者赢函数}//------------以下开始界面int Star_interface(){int i;system("color 9B");printf("\t\t\t C语言游戏:二十一点\n");printf("\n\n\n\n\n\n\n");printf(" 〓〓〓〓〓〓〓〓");printf("\n 〓\n 〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n 〓\n 〓\n");printf(" 〓〓〓〓〓〓〓〓\n");printf("\n\n");printf("\t\t\t ①:查看规则②:开始游戏\n\n");printf("\t\t\t 制作人:小二\n\n");printf("\t\t\t 版本号:1.0\n\t\t\t");scanf("%d",&i);switch(i){case 1:system("CLS");printf("\t\t\t 二十一点游戏规则\n\n\n\t 介绍:手中所有的牌点数之和不超过21点,谁更接近21点,就赢得游戏。
21点源代码
public class run extends javax.swing.JFrame {public run() {initComponents(); // 从A - K的扑克牌,用来计算扑克牌点数String faces[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11","12", "13"};String color[] = {"Spades", "Heart", "Clubs", "Diamond"}; // 扑克牌的"?黑桃"、"?红桃"、"?梅花"、"?方块"deck=new Card[52]; // 一共52张扑克牌for(int i=0;i<deck.length;i++)deck[i]= new Card(faces[i%13],color[i/13]); // 扑克牌点数及花色shuffle(); // 洗牌代码// Gamedh.setVisible(false);Gamepn.setVisible(false); // 设置在没有下注的前,其他按钮是不可点击的Naskcard.setEnabled(false);Nnoaskcard.setEnabled(false);Ndoublemoney.setEnabled(false);Ndecreasemoney.setEnabled(false);Giveup.setEnabled(false);}/****相关按钮等对象的创建*****/@SuppressWarnings("unchecked")private void initComponents() {Dialog1 = new javax.swing.JDialog();jLabel1 = new javax.swing.JLabel();jLabel2 = new javax.swing.JLabel();jLabel3 = new javax.swing.JLabel();jLabel4 = new javax.swing.JLabel();jLabel5 = new javax.swing.JLabel();jLabel6 = new javax.swing.JLabel();jLabel7 = new javax.swing.JLabel();jLabel8 = new javax.swing.JLabel();jLabel9 = new javax.swing.JLabel();jLabel10 = new javax.swing.JLabel();jLabel11 = new javax.swing.JLabel();jLabel12 = new javax.swing.JLabel();jLabel13 = new javax.swing.JLabel();jLabel14 = new javax.swing.JLabel();jLabel15 = new javax.swing.JLabel();jLabel16 = new javax.swing.JLabel();jLabel17 = new javax.swing.JLabel();North = new javax.swing.JLabel();West = new javax.swing.JLabel();East = new javax.swing.JLabel();Gamepn = new javax.swing.JPanel();Computercard = new javax.swing.JTextField();//Nputedmoney.addActionListener(new MyActionMoniter());Nputedmoney = new javax.swing.JButton();//Naskcard.setEnabled(false);//Naskcard.addActionListener(new MyActionMoniter()); Naskcard = new javax.swing.JButton();//Nnoaskcard.addActionListener(new MyActionMoniter()); Nnoaskcard = new javax.swing.JButton();//Ndoublemoney.addActionListener(new MyActionMoniter()); Ndoublemoney = new javax.swing.JButton();//Ndecreasemoney.addActionListener(new MyActionMoniter()); Ndecreasemoney = new javax.swing.JButton();Nputedcash = new javax.swing.JTextField();Nhavecash = new javax.swing.JTextField();Nplayercard = new javax.swing.JTextField();Nscore = new javax.swing.JLabel();Nmoney = new javax.swing.JLabel();Ncash = new javax.swing.JLabel();Cscore = new javax.swing.JLabel();Ncbx = new javax.swing.JComboBox();Giveup = new javax.swing.JButton();jPanel1 = new javax.swing.JPanel();pic1 = new javax.swing.JLabel();pic2 = new javax.swing.JLabel();pic3 = new javax.swing.JLabel();pic4 = new javax.swing.JLabel();pic5 = new javax.swing.JLabel();pic6 = new javax.swing.JLabel();pic7 = new javax.swing.JLabel();pic8 = new javax.swing.JLabel();pic9 = new javax.swing.JLabel();pic10 = new javax.swing.JLabel();pic11 = new javax.swing.JLabel();pic12 = new javax.swing.JLabel();South = new javax.swing.JLabel();jMenuBar1 = new javax.swing.JMenuBar();Game = new javax.swing.JMenu();Start = new javax.swing.JMenuItem();Nunber = new javax.swing.JMenu();Nun2 = new javax.swing.JMenuItem();Restart = new javax.swing.JMenuItem();Exit = new javax.swing.JMenuItem();Option = new javax.swing.JMenu();Filetop = new javax.swing.JMenuItem();Help = new javax.swing.JMenu();help = new javax.swing.JMenuItem();Aboat = new javax.swing.JMenuItem();/*********"排行榜"对话框**********/Dialog1.setTitle("Java-21点游戏英雄排行榜");Dialog1.setBounds(new java.awt.Rectangle(0, 0, 400, 250)); // "排行榜"对话框Dialog1.setIconImage(null); // "排行榜"对话框图标为无Dialog1.setResizable(false); // 不可改变对话框的大小jLabel1.setForeground(new java.awt.Color(255, 0, 0));jLabel1.setText("1、");jLabel2.setForeground(new java.awt.Color(255, 0, 0));jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);jLabel2.setText("姓名");jLabel3.setForeground(new java.awt.Color(255, 0, 0));jLabel3.setText("分数");jLabel4.setForeground(new java.awt.Color(255, 0, 0));jLabel4.setText("2、");jLabel5.setForeground(new java.awt.Color(255, 0, 0));jLabel5.setText("3、");jLabel6.setForeground(new java.awt.Color(255, 0, 0));jLabel6.setText("4、");jLabel7.setForeground(new java.awt.Color(255, 0, 0));jLabel7.setText("5、");jLabel8.setText("jLabel8");jLabel9.setText("jLabel9");jLabel10.setText("jLabel10");jLabel11.setText("jLabel11");jLabel12.setText("jLabel12");jLabel13.setText("jLabel13");jLabel14.setText("jLabel14");jLabel15.setText("jLabel15");jLabel16.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);jLabel16.setText("jLabel16");jLabel17.setText("jLabel17");javax.swing.GroupLayout Dialog1Layout = new javax.swing.GroupLayout(Dialog1.getContentPane());Dialog1.getContentPane().setLayout(Dialog1Layout);Dialog1Layout.setHorizontalGroup(Dialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(Dialog1Layout.createSequentialGroup().addGroup(Dialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false).addGroup(Dialog1Layout.createSequentialGroup().addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jLabel2)).addGroup(javax.swing.GroupLayout.Alignment.LEADING,Dialog1Layout.createSequentialGrou p().addGap(36, 36, 36).addGroup(Dialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jLabel7).addComponent(jLabel6).addComponent(jLabel5).addComponent(jLabel4).addComponent(jLabel1)).addGap(81, 81, 81).addGroup(Dialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jLabel8).addComponent(jLabel10).addComponent(jLabel12).addComponent(jLabel14).addComponent(jLabel16)))).addPreferredGap(ponentPlacement.RELATED,101,Short.MAX_VAL UE).addGroup(Dialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jLabel9).addComponent(jLabel11).addComponent(jLabel13).addComponent(jLabel15).addComponent(jLabel17).addComponent(jLabel3)).addGap(74, 74, 74)) );Dialog1Layout.setVerticalGroup(Dialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alig nment.LEADING).addGroup(Dialog1Layout.createSequentialGroup().addGap(7, 7, 7).addGroup(Dialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(jLabel3)).addPreferredGap(ponentPlacement.REL ATED,25,Short.MAX_VALUE).addGroup(Dialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1).addComponent(jLabel8).addComponent(jLabel9)).addGap(18, 18, 18).addGroup(Dialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel4).addComponent(jLabel10).addComponent(jLabel11)).addGap(18, 18, 18).addGroup(Dialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel5).addComponent(jLabel12).addComponent(jLabel13)).addGap(18, 18, 18).addGroup(Dialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel6).addComponent(jLabel14).addComponent(jLabel15)).addGap(18, 18, 18).addGroup(Dialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel7).addComponent(jLabel16).addComponent(jLabel17)).addGap(74, 74, 74)) );setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);setTitle("21点游戏(人机对战Java版)"); // 程序标题setBounds(new java.awt.Rectangle(0, 0, 0, 0));setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));addWindowListener(new java.awt.event.WindowAdapter() {public void windowClosing(java.awt.event.WindowEvent evt) {formWindowClosing(evt) }});North.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);North.setText("北(庄)");getContentPane().add(North, java.awt.BorderLayout.NORTH);West.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);West.setText("西");getContentPane().add(West, java.awt.BorderLayout.WEST);East.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);East.setT ext("东");getContentPane().add(East, java.awt.BorderLayout.EAST);Gamepn.setMaximumSize(new java.awt.Dimension(32767, 34000));Gamepn.setPreferredSize(new java.awt.Dimension(972, 630));Computercard.setColumns(5);Computercard.setEditable(false);Computercard.setForeground(new java.awt.Color(255, 0, 255));Computercard.setHorizontalAlignment(javax.swing.JTextField.CENTER);Computercard.setT ext("0");Nputedmoney.setText("下注");Nputedmoney.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {NputedmoneyActionPerformed(evt); }});Naskcard.setText("要牌");Naskcard.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {NaskcardActionPerformed(evt);}});Nnoaskcard.setText("不要");Nnoaskcard.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {NnoaskcardActionPerformed(evt); } });Ndoublemoney.setText("赌注加倍");Ndoublemoney.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {NdoublemoneyActionPerformed(evt); }});Ndecreasemoney.setText("减少赌注");Ndecreasemoney.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {NdecreasemoneyActionPerformed(evt); } });Nputedcash.setColumns(5);Nputedcash.setEditable(false);Nputedcash.setForeground(new java.awt.Color(255, 51, 51));Nputedcash.setHorizontalAlignment(javax.swing.JTextField.CENTER);Nputedcash.setText("0");Nhavecash.setColumns(5);Nhavecash.setEditable(false);Nhavecash.setForeground(new java.awt.Color(255, 0, 0));Nhavecash.setHorizontalAlignment(javax.swing.JTextField.CENTER);Nhavecash.setText("95");Nplayercard.setColumns(5);Nplayercard.setEditable(false);Nplayercard.setForeground(new java.awt.Color(255, 0, 255));Nplayercard.setHorizontalAlignment(javax.swing.JTextField.CENTER);Nplayercard.setT ext("0");Nscore.setText("点数");Nmoney.setText("赌注");Ncash.setText("现金");Cscore.setText("点数");Ncbx.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "5", "10", "15", "20" }));Giveup.setText("放弃");Giveup.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {GiveupActionPerformed(evt); } });pic1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));pic2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));pic3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));pic4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));pic5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));pic6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));pic7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));pic8.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));pic9.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));pic10.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));pic11.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));pic12.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);jPanel1.setLayout(jPanel1Layout);jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(pic12,javax.swing.GroupLayout.PREFERRED_SIZE,97,javax.swing.GroupLayout.PR EFERRED_SIZE).addGap(38,38,38) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Align ment.TRAILING).addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addComponent(pic6,javax.swing.GroupLayout.PREFERRED_SIZE,72,javax.swing.GroupLayout.PRE FERRED_SIZE).addPreferredGap(ponentPlacement.RELATED).addComponent(pic7,javax.swing.GroupLayout.PREFERRED_SIZE,72,javax.swing.GroupLayout.PRE FERRED_SIZE).addPreferredGap(ponentPlacement.RELATED).addComponent(pic8,javax.swing.GroupLayout.PREFERRED_SIZE,72,javax.swing.GroupLayout.PRE FERRED_SIZE).addPreferredGap(ponentPlacement.UNRELATED).addComponent(pic9,javax.swing.GroupLayout.PREFERRED_SIZE,72,javax.swing.GroupLayout.PRE FERRED_SIZE).addPreferredGap(ponentPlacement.RELATED).addComponent(pic10,javax.swing.GroupLayout.PREFERRED_SIZE,72,javax.swing.GroupLayout.PR EFERRED_SIZE)).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,jPanel1Layout.createSequentialGroup() .addComponent(pic1,javax.swing.GroupLayout.PREFERRED_SIZE,72,javax.swing.GroupLayout.PRE FERRED_SIZE).addPreferredGap(ponentPlacement.UNRELATED).addComponent(pic2,javax.swing.GroupLayout.PREFERRED_SIZE,72,javax.swing.GroupLayout.PRE FERRED_SIZE).addPreferredGap(ponentPlacement.RELATED).addComponent(pic3,javax.swing.GroupLayout.PREFERRED_SIZE,72,javax.swing.GroupLayout.PRE FERRED_SIZE).addPreferredGap(ponentPlacement.RELATED).addComponent(pic4,javax.swing.GroupLayout.PREFERRED_SIZE,72,javax.swing.GroupLayout.PRE FERRED_SIZE).addPreferredGap(ponentPlacement.RELATED).addComponent(pic5,javax.swing.GroupLayout.PREFERRED_SIZE,72,javax.swing.GroupLayout.PRE FERRED_SIZE))).addGap(107, 107, 107)).addGroup(jPanel1Layout.createSequentialGroup().addComponent(pic11,javax.swing.GroupLayout.PREFERRED_SIZE,97,javax.swing.GroupLayout.PR EFERRED_SIZE).addContainerGap()))) );jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGro up(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(pic6,javax.swing.GroupLayout.PREFERRED_SIZE,97,javax.swing.GroupLayout.PRE FERRED_SIZE).addComponent(pic7,javax.swing.GroupLayout.PREFERRED_SIZE,97,javax.swing.GroupLayout.PRE FERRED_SIZE).addComponent(pic8,javax.swing.GroupLayout.PREFERRED_SIZE,97,javax.swing.GroupLayout.PRE FERRED_SIZE).addComponent(pic9,javax.swing.GroupLayout.PREFERRED_SIZE,97,javax.swing.GroupLayout.PRE FERRED_SIZE).addComponent(pic10,javax.swing.GroupLayout.PREFERRED_SIZE,97,javax.swing.GroupLayout.PR EFERRED_SIZE)).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(156, 156, 156).addComponent(pic12,javax.swing.GroupLayout.PREFERRED_SIZE,71,javax.swing.GroupLayout. PREFERRED_SIZE).addContainerGap(86, Short.MAX_VALUE)).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,jPanel1Layout.createSequentialGrou p().addPreferredGap(ponentPlacement.RELATED).addComponent(pic11,javax.swing.GroupLayout.PREFERRED_SIZE,71,javax.swing.GroupLayout. PREFERRED_SIZE).addGap(18, 18, 18).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(pic2,javax.swing.GroupLayout.PREFERRED_SIZE,97,javax.swing.GroupLayout.P REFERRED_SIZE).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,jPanel1Layout.createParallelGroup(ja vax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(pic5,javax.swing.GroupLayout.PREFERRED_SIZE,97,javax.swing.GroupLayout.P REFERRED_SIZE).addComponent(pic4,javax.swing.GroupLayout.PREFERRED_SIZE,97,javax.swing.GroupLayout.P REFERRED_SIZE).addComponent(pic3,javax.swing.GroupLayout.PREFERRED_SIZE,97,javax.swing.GroupLayout.PREFERRED_SIZE)).addComponent(pic1,javax.swing.GroupLayout.PREFERRED_SIZE,97,javax.swing.GroupLayout.P REFERRED_SIZE))).addContainerGap()))) );South.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);South.setText("南"); javax.swing.GroupLayout GamepnLayout = new javax.swing.GroupLayout(Gamepn);Gamepn.setLayout(GamepnLayout);GamepnLayout.setHorizontalGroup(GamepnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(GamepnLayout.createSequentialGroup().addGap(476, 476, 476).addComponent(Cscore).addContainerGap(676, Short.MAX_VALUE)).addGroup(GamepnLayout.createSequentialGroup().addGap(459, 459, 459).addComponent(Computercard,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLa yout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(681, Short.MAX_VALUE)).addGroup(GamepnLayout.createSequentialGroup().addGap(147, 147, 147).addComponent(jPanel1,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DE FAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(389, Short.MAX_VALUE)).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,GamepnLayout.createSequentialG roup().addContainerGap(261, Short.MAX_VALUE).addGroup(GamepnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN G).addGroup(GamepnLayout.createSequentialGroup().addComponent(Ncbx,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout. DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(ponentPlacement.RELATED).addComponent(Nputedmoney).addPreferredGap(ponentPlacement.RELATED).addComponent(Naskcard)).addComponent(Nmoney)).addPreferredGap(ponentPlacement.RELATED).addGroup(GamepnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN G, false).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,GamepnLayout.createSequentialGroup().addComponent(Nnoaskcard).addPreferredGap(ponentPlacement.RELATED).addComponent(Ndoublemoney).addPreferredGap(ponentPlacement.RELATED)).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,GamepnLayout.createSequentialGroup().addComponent(Nputedcash,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupL ayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(ponentPlacement.RELATED,javax.swing.Grou pLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(Nhavecash,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLa yout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18, 18, 18))).addGroup(GamepnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN G).addGroup(GamepnLayout.createSequentialGroup().addComponent(Ndecreasemoney).addPreferredGap(ponentPlacement.RELATED).addComponent(Giveup)).addComponent(Ncash,javax.swing.GroupLayout.PREFERRED_SIZE,33,javax.swing.GroupLayo ut.PREFERRED_SIZE)).addGap(451, 451, 451)).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,GamepnLayout.createSequentialGroup().addContainerGap(467, Short.MAX_VALUE).addGroup(GamepnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Nplayercard,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLay out.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(Nscore)).addGap(673, 673, 673)).addGroup(GamepnLayout.createSequentialGroup().addComponent(South,javax.swing.GroupLayout.PREFERRED_SIZE,1156,javax.swing.GroupLay out.PREFERRED_SIZE).addContainerGap(20,Short.MAX_VALUE)) );GamepnLayout.setVerticalGroup(GamepnLayout.createParallelGroup(javax .swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,GamepnLayout.createSequentialGroup().addContainerGap().addComponent(Computercard,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.Group Layout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(ponentPlacement.RELATED).addComponent(Cscore).addGap(9, 9, 9).addComponent(jPanel1,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout. DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(ponentPlacement.RELATED).addComponent(Nscore).addPreferredGap(ponentPlacement.RELATED).addComponent(Nplayercard,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLa yout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(ponentPlacement.RELATED).addGroup(GamepnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN E).addComponent(Ncash).addComponent(Nhavecash,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLay out.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(Nputedcash,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLa yout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(Nmoney)).addPreferredGap(ponentPlacement.UNRELATED).addGroup(GamepnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(Ndecreasemoney).addComponent(Giveup).addComponent(Ndoublemoney).addComponent(Nnoaskcard).addComponent(Naskcard).addComponent(Nputedmoney).addComponent(Ncbx,javax.swing.GroupLayout.PREFERRED_SIZE,javax.s wing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(56, 56, 56).addComponent(South).addGap(63, 63, 63)) );getContentPane().add(Gamepn, java.awt.BorderLayout.CENTER);Game.setMnemonic(java.awt.event.KeyEvent.VK_G); // 为"游戏"菜单设置快捷键"G"Game.setText("游戏(G)");Start.setMnemonic(java.awt.event.KeyEvent.VK_S);// 为"开始"子菜单设置快捷键"S"Start.setText("开始(S)");Start.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {StartActionPerformed(evt); } });Game.add(Start);Nunber.setMnemonic(java.awt.event.KeyEvent.VK_P); // 为"人数"子菜单设置快捷键"P"Nunber.setText("人数(P)");Nun2.setMnemonic(java.awt.event.KeyEvent.VK_T);Nun2.setText("2人游戏(T)"); // 为"人数"子菜单的"2人游戏"设置快捷键"T"Nun2.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {Nun2ActionPerformed(evt);} });Nunber.add(Nun2);Game.add(Nunber);Restart.setMnemonic(java.awt.event.KeyEvent.VK_R); // 为"重开"子菜单设置快捷键"R"Restart.setT ext("重开(R)");Restart.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {RestartActionPerformed(evt); } });Game.add(Restart);Exit.setMnemonic(java.awt.event.KeyEvent.VK_X);// 为"退出"子菜单设置快捷键"X"Exit.setText("退出(X)");Exit.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {ExitActionPerformed(evt); } });Game.add(Exit);jMenuBar1.add(Game);Option.setMnemonic(java.awt.event.KeyEvent.VK_O); // 为"选项"菜单设置快捷键"O"Option.setText("选项(O)");Filetop.setMnemonic(java.awt.event.KeyEvent.VK_P);// 为"排行榜"子菜单快捷键"P"Filetop.setText("排行榜(P)");Filetop.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {FiletopActionPerformed(evt); } });Option.add(Filetop);jMenuBar1.add(Option);Help.setMnemonic(java.awt.event.KeyEvent.VK_H); // 为"帮助"菜单设置快捷键"H"Help.setText(" 帮助(H)");help.setMnemonic(java.awt.event.KeyEvent.VK_H);// 为"帮助"子菜单设置快捷键"H"help.setText("帮助(H)");help.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {helpActionPerformed(evt); } });Help.add(help);Aboat.setMnemonic(java.awt.event.KeyEvent.VK_A); // 为"关于"子菜单设快捷键"A"Aboat.setText("关于(A)");Aboat.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {AboatActionPerformed(evt); } });Help.add(Aboat);jMenuBar1.add(Help);setJMenuBar(jMenuBar1);pack();}/****************处理"赢牌"代码*******/public void Judge_Win(){/**********赢牌,筹码增加*****/if(pic == 5 || Nplayscore == 21) // 玩家爆点或5张牌不超过21点,赢得赌注加倍{ cash = Integer.parseInt(Nhavecash.getT ext()) + Integer.parseInt(Nputedcash.getText()) * 3;Nhavecash.setText(cash +"");}else //玩家不爆点、牌数不超过5张{ cash = Integer.parseInt(Nhavecash.getT ext()) + Integer.parseInt(Nputedcash.getText()) * 2;Nhavecash.setText(cash +"");}/********初始化庄家、玩家的信息******/pic=1; // 设置玩家重新发牌pic1.setIcon(new javax.swing.ImageIcon()); // 初始化扑克牌pic2.setIcon(new javax.swing.ImageIcon());pic3.setIcon(new javax.swing.ImageIcon());pic4.setIcon(new javax.swing.ImageIcon());pic5.setIcon(new javax.swing.ImageIcon()); //清空南玩家的所有扑克牌pic1.setIcon(new javax.swing.ImageIcon(null));Nplayscore = 0; // 重新发牌,玩家点数设为0Nputedcash.setText("0"); // 初始化玩家点数、下注、Nplayercard.setT ext("0");/***** 初始化电脑相关信息**********/cpic=1; // 设置电脑重新发牌pic6.setIcon(new javax.swing.ImageIcon()); // 初始化扑克牌pic7.setIcon(new javax.swing.ImageIcon());pic8.setIcon(new javax.swing.ImageIcon());pic9.setIcon(new javax.swing.ImageIcon());pic10.setIcon(new javax.swing.ImageIcon()); //清空电脑的所有扑克牌pic1.setIcon(new javax.swing.ImageIcon(null));Computercard.setT ext("0"); //重置电脑点数Computerscore = 0; // 重新发牌,电脑点数设为0/********** 设置要牌等按钮不可点击*******/Naskcard.setEnabled(false); // 设置要牌、不要、赌注加倍、赌注减少等按钮不能点击Nnoaskcard.setEnabled(false);Ndoublemoney.setEnabled(false);Ndecreasemoney.setEnabled(false);Giveup.setEnabled(false);Nputedmoney.setEnabled(true); // 重新发牌后,下注按钮被激活}/********************处理"输牌"代码**********/public void Judge_Lose(){/***********初始化庄家、玩家的信息********/pic=1; // 设置玩家重新发牌pic1.setIcon(new javax.swing.ImageIcon()); // 初始化扑克牌pic2.setIcon(new javax.swing.ImageIcon());pic3.setIcon(new javax.swing.ImageIcon());pic4.setIcon(new javax.swing.ImageIcon());pic5.setIcon(new javax.swing.ImageIcon()); //清空南玩家的所有扑克牌pic1.setIcon(new javax.swing.ImageIcon(null));Nplayscore = 0; // 重新发牌,玩家点数设为0Nputedcash.setText("0"); // 初始化玩家点数、下注、Nplayercard.setT ext("0");/**** 初始化电脑相关信息************/cpic=1; // 设置电脑重新发牌pic6.setIcon(new javax.swing.ImageIcon()); // 初始化扑克牌pic7.setIcon(new javax.swing.ImageIcon());pic8.setIcon(new javax.swing.ImageIcon());pic9.setIcon(new javax.swing.ImageIcon());pic10.setIcon(new javax.swing.ImageIcon()); //清空电脑的所有扑克牌pic1.setIcon(new javax.swing.ImageIcon(null));Computercard.setT ext("0"); //重置电脑点数Computerscore = 0; // 重新发牌,电脑点数设为0/******** 设置要牌等按钮不可点击*************/Naskcard.setEnabled(false); // 设置要牌、不要、赌注加倍、赌注减少等按钮不能点击Nnoaskcard.setEnabled(false);Ndoublemoney.setEnabled(false);Ndecreasemoney.setEnabled(false);Giveup.setEnabled(false);Nputedmoney.setEnabled(true); // 重新发牌后,下注按钮被激活}/***************处理"要牌"按钮的代码*********/public void Judgmentac(){if(Nplayscore == 21){javax.swing.JOptionPane.showMessageDialog(this, "因为杰克(=21),你赢了双倍下注额,恭喜恭喜!", "恭喜", RMATION_MESSAGE);Judge_Win(); // 调用赢牌的代码}if(Nplayscore > 21){ javax.swing.JOptionPane.showMessageDialog(this,"你输了,请再接再厉吧!","遗憾",RMATION_MESSAGE);Judge_Lose(); // 调用输了代码}if(Nplayscore < 21 && pic > 5){javax.swing.JOptionPane.showMessageDialog(this, "你赢得了双倍金钱,恭喜恭喜!", "恭喜", RMATION_MESSAGE);Judge_Win(); // 调用赢牌的代码,且是赢得了双倍金钱}}。
Python扑克牌21点游戏实例代码
Python扑克牌21点游戏实例代码废话还是说太多了直接上代码import randomimport sys# 牌⾯列表card_code = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']# 花⾊列表card_symbol = ['♦', '♣', '♥', '♠']# 游戏初始化def init(player_count):# 根据玩家数来⽣成玩家记牌器player_group = [[] for _ in range(player_count)]# 根据玩家数来⽣成玩家是否要牌player_isWant = [True for _ in range(player_count)]# ⽣成元素1~52的列表 (去掉⼤⼩⿁的扑克牌[52张])poker = list(range(1, 53))# ⽤random的shuffle函数对列表打乱顺序 (洗牌)random.shuffle(poker)# 返回玩家组玩家是否要牌乱序52张扑克return player_group, player_isWant, poker# 打印玩家点数def print_player_point(player_group):# 存放玩家点数player_point = []# 遍历每⼀位玩家for index in range(len(player_group)):# 打印每位玩家的牌和点数print("-------玩家"+str(index+1)+"------")# 初始化玩家点数如果含有牌A 因为A可视为1点或11点则有两种点数current_player = [0, 0]# 遍历每位玩家的⼿牌for card in player_group[index]:"""核⼼代码由于牌⾯的数字是从1到52 所以牌⾯要先减1再求余才是牌⾯列表真正的下标若玩家抽到牌为15 即牌⾯为15 - 13 = 2 且按花⾊顺序为♣即2♣牌⾯ 15 - 1 = 14 再 14 % 13 = 1 这个就是对应牌⾯列表的第⼆位元素即2花⾊ 15 - 1 = 14 再 14 / 13 = 1 对应花⾊列表第⼆位元素即♣"""# 获取牌⾯和花⾊下标code_index = int((card - 1) % 13)symbol_index = int((card - 1) / 13)# 打印玩家牌信息print(card_code[code_index] + card_symbol[symbol_index], end="\t")# 如果牌⾯含有A 则添加不同点数1和11if (code_index + 1) == 1:current_player[0] += 1current_player[1] += 11# 如果牌⾯不含A 则添加相同点数else:current_player[0] += code_index + 1current_player[1] += code_index + 1# 如果两个点数⼀致则打印⼀个点数if current_player[0] == current_player[1]:print("点数为"+str(current_player[0])+"点")# 否则打印两个点数else:print("点数为"+str(current_player[0])+"点或"+str(current_player[1]))# 添加当前玩家点数player_point.append(current_player)# 返回所有玩家点数return player_point# 玩游戏def play_game():# 打印游戏规则print("-------21点游戏------")print("---A可看做1点或11点---")# 死循环⼀直进⾏游戏while True:# 初始化玩家数为0player_count = 0# 当玩家数⼩于等于1或⼤于5时继续询问while player_count <= 1 or player_count > 5:# 询问玩家数print("有多少位玩家?(2~5位)", end="")# 获取控制台输⼊的数字⽆验证输⼊若输⼊⾮数字程序直接报错player_count = int(input())# 初始化游戏返回玩家组玩家是否要牌乱序52张扑克player_group, player_isWant, poker = init(player_count)# 开始发牌先为每位玩家发两张牌循环玩家数for index in range(player_count):for i in range(2):# pop() 函数⽤于移除列表中的⼀个元素(默认最后⼀个元素)并且返回该元素的值。
c++21点游戏的代码和报告
数学与计算机学院课程设计说明书课程名称: 面向对象程序设计-课程设计课程代码: 8404101题目: 21点的扑克牌游戏年级/专业/班: 09/软件工程/1班学生姓名:学号: 312009*********开始时间:2011 年06 月13日完成时间:2011 年06月21 日课程设计成绩:指导教师签名:年月日目录1 引言 (1)1.1问题的提出 (1)1.2任务与分析 (1)2 程序的主要功能 (2)2.1新建用户功能 (2)2.2判断密码功能 (2)2.3判断进程功能 (2)2.4显示功能 (2)2.5获得牌的功能 (2)2.6初始化功能 (2)3 程序运行平台 (3)4 总体设计 (4)5 程序类的说明 (5)6 模块分析 (6)6.1新建用户模块 (6)6.2显示模块 (6)6.3判断密码模块 (8)6.4判断游戏进程模块 (10)6.5获得一张牌模块 (11)6.6初始化模块 (12)7 系统测试 (13)8 结论 (16)参考文献 (17)摘要随着计算机的普及,计算机游戏逐渐进入了这个时代,本次利用学到的c++编写了一个21点的扑克牌游戏,该游戏具有新建用户,判断牌等功能关键词:21点的扑克牌游戏;计算机;1 引言1.1 问题的提出设计一个21点的扑克牌游戏,玩家一共可以要5张牌,但如果牌的点数超过21,则暴了,自动出局;在不超过21点的情况下,玩家与庄家比牌的点数大小,大者为赢。
1.2任务与分析(1) 将所有的函数与相应的数据封装到类中。
(2) 程序中一共有13张扑克牌可以随机抽取,大于10的点数全部为半点:只需设计随机取下一张牌的成员函数时使用if语句判定牌的点数是否大于10,大于10的牌对其重新赋值为0.5即可。
(3) 要求庄家永远不会暴庄,即庄家可以查看下一张牌,若大于21点,则拒绝,当然,在游戏规则上不能泄露这点秘密:在函数中首先随机产生下一张牌,接着用条件语句判定原有点数与新牌的点数之和是否大于21点,然后决定是否取牌。
二十一点规则
二十一点规则引言:二十一点(Blackjack),也被称为“21点”,是一种非常流行的纸牌游戏,通常由一至六副扑克牌进行。
在这个游戏中,玩家的目标是尽可能接近但不超过21点,同时要打败庄家。
二十一点规则虽然简单易学,但是需要玩家掌握一定的策略和技巧,才能提高胜算。
一、基本规则:1. 二十一点的牌面点数规则如下:Ace(A)的点数可以是1点或11点;2-10的点数按牌面上的数值计算;J、Q、K的点数都为10点。
2. 游戏开始时,玩家和庄家将分别发两张牌。
玩家所有的牌都是面向上的,而庄家的一张牌则是面向上的,另一张牌是面向下的。
3. 玩家可以选择“要牌”(Hit),即向庄家要求发一张额外的牌,以增加自己的牌面点数。
玩家可以任意次数的要牌,直到决定停止。
4. 玩家也可以选择“停牌”(Stand),即停止要牌,并进入庄家的回合。
5. 当玩家的牌面点数超过21点时,即为“爆牌”(Bust),玩家将直接判负。
6. 庄家的回合:庄家会根据规定的策略继续要牌或停牌。
通常情况下,庄家在手中的牌面点数低于17点时会要牌,超过17点时则停牌。
7. 当玩家停牌后,庄家会将自己未亮出的牌翻开,进行点数比较。
点数更接近21点的一方获胜,如果点数相同,则判定为平局。
二、特殊情况:1. “黑杰克”(Blackjack):当玩家拿到一张A和一张10点牌(包括J、Q、K)时,即为“黑杰克”或“天才”,此时玩家直接获胜。
2. “分牌”(Split):当玩家手中的两张牌点数相同时,可以选择将其分为两个独立的手牌,并继续游戏。
这需要玩家再下注一次,并将每张牌的点数作为新手牌的起始点数。
3. “加倍下注”(Double Down):玩家在初始两张牌面向上时,可以选择加倍下注。
玩家将下注金额加倍,并且必须要求庄家发给他一张额外牌。
这之后,玩家将不能再要牌,直接进入庄家的回合。
4. “保险”(Insurance):当庄家的明牌是A时,玩家可以选择购买保险。
21点纸牌游戏_C++课程设计报告
郑州轻工业学院课程设计报告课程设计名称: 21点纸牌游戏课程名称:面向对象程序设计(C++)姓名:谢先斌院(系):计算机与通信工程学院专业班级:计算机10-1学号: 541007010144指导教师:王秉政、郭锋成绩:时间:2011年12月26日至 2011年12月30日郑州轻工业学院学生课程设计任务书一、题目(任选一题)21点纸牌游戏公司员工管理系统二、学生信息专业计算机科学与技术学号 541007010144 姓名谢先斌三、基本要求及约束条件(一)“21点纸牌游戏”设计1、需求及内容21点是一个古老的扑克牌游戏,游戏规则是:各个参与者设法使自己的牌达到总分21而不超过这个数值。
扑克牌的分值取它们的面值,A充当1分或者11分(由玩家自己选择一种分值),J、Q和K人头牌都是10分。
庄家对付1~7个玩家。
在一局开始时,包括庄家在内的所有参与者都有两张牌。
玩家可以看到它们的所有牌以及总分,而专家有一张牌暂时是隐藏的。
接下来,只要愿意,各个玩家都有机会依次再拿一张牌。
如果某个玩家的总分超过了21(称为“引爆”),那么这个玩家就输了。
在所有玩家都拿了额外的牌后,庄家将显示隐藏的牌。
只有庄家的总分等于或小于16,那么他就必须再拿牌。
如果庄家引爆了,那么还没有引爆的所有玩家都将获胜,引爆的玩家打成平局。
否则,将余下的各玩家的总分与庄家的总分做比较,如果玩家的总分大于庄家的总分,则玩家获胜。
如果二者的总分相同,则玩家与庄家打成平局。
编写程序实现游戏,计算机作为庄家,1~7个人作为普通玩家参与游戏。
游戏程序运行输出如下所示:2、要点及分析首先定义表示扑克牌花色与面值的枚举类型,这样程序可读性更强,具体定义如下:typedef enum{ // 扑克牌面值:ACE(A),TWO(2)~TEN(10),JACK(J), QUEEN(Q), KING(K)ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING} RankType;typedef enum{ // 扑克牌花色:CLUBS(梅花), DIAMONDS(方块), HEARTS(红桃)和SPADES(黑桃) CLUBS, DIAMONDS, HEARTS, SPADES} SuitType;struct Card{ // 扑克牌结构体RankType rank; // 扑克牌面值SuitType suit; // 扑克牌花色};(二)“公司员工管理系统”设计1、需求及内容为了方便对公司员工基本信息进行电子化管理,利用面向对象的程序设计技术实现对员工信息的自动化管理。
编程算21点的思路
编程算21点的思路
21点(又称为黑杰克)是一种流行的扑克牌游戏,玩家的目标
是使手中的牌的点数尽量接近21点,但不能超过21点。
这个游戏
可以通过编程来实现,下面是实现21点游戏的一种可能思路:
1. 定义扑克牌的数据结构,首先,需要定义扑克牌的数据结构,可以使用类或者结构体来表示每张牌的花色和点数。
可以使用数组
或者列表来表示一副扑克牌。
2. 初始化扑克牌,创建一副完整的扑克牌,包括52张牌,四
种花色(红桃、方块、梅花、黑桃)和13种点数(2-10、J、Q、K、A)。
3. 发牌,实现发牌的功能,可以随机从扑克牌中抽取若干张牌
给玩家和庄家。
4. 计算点数,根据牌的点数计算玩家和庄家手中牌的总点数,
A可以算作1点或者11点,J、Q、K算作10点,其余牌按照面值计
算点数。
5. 玩家决策,根据玩家手中的牌和庄家的一张牌,实现玩家的
决策逻辑,包括要牌(继续抽牌)或者停牌(不再抽牌)。
6. 庄家决策,根据庄家手中的牌和规则,实现庄家的决策逻辑,通常是在点数小于17时继续抽牌。
7. 判定胜负,根据玩家和庄家的点数进行比较,判定胜负并结
算赌注。
8. 游戏循环,实现游戏的循环逻辑,包括询问玩家是否继续游戏,重新发牌等。
以上是一个简单的21点游戏的编程思路,实现过程中还需要考
虑各种边界情况、用户输入的合法性检查、界面交互等方面。
这个
思路可以作为实现21点游戏的初始框架,具体的实现细节还需要根
据具体的编程语言和平台来进行进一步的设计和开发。
21点源代码
#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <time.h>#include <windows.h>//二维数组:第一行存储牌的点数,第二行存储牌的符号int cpu_card[2][5]={0},cpu_count=0,cpu_trun; //cpu_count表示电脑牌数,cpu_turn表示电脑是否要牌的状态int play_card[2][5]={0},play_count=0,play_trun; //play_count表示玩家牌数,play_turn表示玩家是否要牌的状态int win=0,lose=0,draw=0; //win表示赢的局数,lose表示输的局数,draw表示平的局数int cpuds(); //计算电脑总点数int playerds(); //计算玩家总点数void rule() //游戏规则{printf("*********欢迎进入21点游戏世界!*********\n");printf("* 基本游戏规则:*\n");printf("* 游戏纸牌不包含大小鬼*\n");printf("* 开始游戏每人发一张牌*\n");printf("* 玩家最多可以要五张牌*\n");printf("* 谁先到21点或最接近21点算谁赢*\n");printf("* 点数相同或都超过21点时算平局*\n");printf("* 祝您好运!*\n");printf("**************************CJY作*********\n");printf("(1)新游戏\n");printf("(0)离开游戏\n");printf("(2)退出\n");}void result() //最终结局{printf("您胜了%d次\n",win);printf("您输了%d次\n",lose);printf("您平了%d次\n",draw);printf("\n");}int random(int n){int nn;srand((unsigned)time(NULL)+rand()); //设置随机数种子nn=rand()%n;return nn;}void cpucard(int less) //电脑的纸牌{for(int i=0;i<cpu_count;i++)printf("┏┓ ");printf("\n");if(less==1){printf(" * * ");for(int i=1;i<cpu_count;i++){printf(" %c",cpu_card[1][i]);if(cpu_card[0][i]<10&&cpu_card[0][i]!=1)printf(" %d ",cpu_card[0][i]);if(cpu_card[0][i]==10)printf("10 ");if(cpu_card[0][i]==11)printf(" J ");if(cpu_card[0][i]==12)printf(" Q ");if(cpu_card[0][i]==13)printf(" K ");if(cpu_card[0][i]==1)printf(" A");}}else{for(int i=0;i<cpu_count;i++){printf(" %c",cpu_card[1][i]);if(cpu_card[0][i]<10&&cpu_card[0][i]!=1)printf(" %d ",cpu_card[0][i]);if(cpu_card[0][i]==10)printf("10 ");if(cpu_card[0][i]==11)printf(" J ");if(cpu_card[0][i]==12)printf(" Q ");if(cpu_card[0][i]==13)printf(" K ");if(cpu_card[0][i]==1)printf(" A");}}printf("\n");for(int i=0;i<cpu_count;i++)printf("┗┛ ");printf("\n");}void playercard() //玩家的纸牌{for(int i=0;i<play_count;i++)printf("┏┓ ");printf("\n");for(int i=0;i<play_count;i++){printf(" %c",play_card[1][i]);if(play_card[0][i]<10&&play_card[0][i]!=1)printf(" %d ",play_card[0][i]); if(play_card[0][i]==10)printf("10 ");if(play_card[0][i]==11)printf(" J ");if(play_card[0][i]==12)printf(" Q ");if(play_card[0][i]==13)printf(" K ");if(play_card[0][i]==1)printf(" A");}printf("\n");for(int i=0;i<play_count;i++)printf("┗┛ ");printf("\n");}int cpuds(){int s=0;for(int i=0;i<cpu_count;i++){if(cpu_card[0][i]<10)s+=cpu_card[0][i];else s+=10;}return s;}int playerds(){int s=0;for(int i=0;i<play_count;i++){if(play_card[0][i]<10)s+=play_card[0][i];else s+=10;}return s;}int minus(int n){if(n>10)return 10;return n;}void print() //显示全部纸牌{printf("***********************************\n");printf("您的牌点分别是:\n");playercard();printf("电脑的牌点分别是:\n");cpucard(1);printf("您的牌得点数和是:%d\n",playerds());printf("电脑已知的牌得点数和是:%d\n",cpuds()-minus(cpu_card[0][0])); //减去第一张牌的点数(针对电脑而言)printf("***********************************\n");}void print_end() //显示每局的最终结果{printf("***********************************\n");printf("您的牌点分别是:\n");playercard();printf("电脑的牌点分别是:\n");cpucard(0);printf("您的牌的点数和是:%d\n",playerds());printf("电脑的牌的点数和是:%d\n",cpuds());printf("***********************************\n");}void judge_end() //判断输赢{system ("cls");print_end();if(playerds()<=21&&cpuds()<=21&&playerds()==cpuds()){draw++;printf("双方一样,平局!\n");}if(playerds()>21&&cpuds()>21){draw++;printf("双方都爆了,平局!\n");}if(playerds()<=21&&cpuds()<=21&&playerds()>cpuds()){win++;printf("您获胜了!\n");}if(playerds()<=21&&cpuds()>21){win++;printf("电脑爆了,您获胜了!\n");}if(playerds()<=21&&cpuds()<=21&&playerds()<cpuds()){lose++;printf("您输了,电脑获胜了!\n");}if(playerds()>21&&cpuds()<=21){lose++;printf("您爆了,电脑获胜了!\n");}}void game() //开始游戏{int ss;cpu_count=1;play_count=1;cpu_card[0][0]=random(13)+1; //随即获取纸牌数值cpu_card[1][0]=random(4)+3; //随即获取纸牌符号play_card[0][0]=random(13)+1;play_card[1][0]=random(4)+3;while(1){system ("cls");print();if(playerds()>21){printf("点数超过21,自动弃牌!\n");Sleep(1200);ss=0;} else {printf("您是否继续要牌?(1/0)");Sleep(1000);scanf("%d",&ss);}if(ss==1&&play_count==5){printf("\n对不起,您已经要了五张牌了!");ss=0;}if(ss==1) play_trun=1;else play_trun=0;if(cpuds()<16&&cpu_count<=5) cpu_trun=1;else cpu_trun=0;if(cpu_trun==0&&play_trun==0){judge_end();return;}if(cpu_trun==1){cpu_card[0][cpu_count]=random(13)+1;cpu_card[1][cpu_count]=random(4)+3;cpu_count++;}if(play_trun==1){play_card[0][play_count]=random(13)+1;play_card[1][play_count]=random(4)+3;play_count++;}}}main(){int ss;while(1){system ("cls");rule();printf("请输入:");scanf("%d",&ss);if(ss==0){system ("cls");printf("您结束了游戏,最后您的状态是:\n");result();printf(" \n适度游戏益脑沉迷游戏伤身\n");printf("按任意键继续...");getch();}if(ss==1){system ("cls");game();printf("\n按任意键继续.........");getch();system ("cls");}if(ss==2) {exit(0);}}}。
21点游戏源代码
printf("你愿意玩一局吗?(Y or N)\n");
scanf("%c",&chanswer);
if((chanswer=='y')||(chanswer=='Y'))
{
pmoney=100;
}
else
printf("\n\t谢谢使用!再见!\n");
*pturns=2;
*cturns=2;
playercard1=random(13,1);
delay(150);
cpucard1=random(13,1);
delay(150);
playercard2=random(13,1);
delay(150);
printf(" ***********如果你赢了可以获得两倍的你下注的资金*************\n");
printf(" ****************牌数总和在16点以后就不再发牌****************\n");
}
void pause()// 暂停函数
{
printf("\n 恭喜你!你赢了啊!\n");
++*pwin;
*pmoney=*pmoney+*pbet+*pbet1;
}
else if((player>21)&&(cpu>21))
{
printf("\n 平局!很可惜啊,你什么也没捞!\n");
++*pdraw;
21点游戏程序代码有特殊情况
Public Class GameInterfaceDim p(10), c(10) As IntegerDim t, i, CScore, PScore, m As IntegerPrivate Sub GameInterface_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadMe.Top = 45Me.Left = 80Randomize()Timer1.Interval = 1000End SubPrivate Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.ClickTryDisplayReset()m = 10lblTime.Text = mIf m = 10 ThenTimer1.Enabled = TrueEnd Ifp(1) = Int(Rnd() * 52 + 1)Dot = Int(Rnd() * 52 + 1)Loop While t = p(1)c(1) = tDot = Int(Rnd() * 52 + 1)Loop While t = p(1) Or t = c(1)p(2) = tDot = Int(Rnd() * 52 + 1)Loop While t = p(1) Or t = c(1) Or t = p(2)c(2) = tptbP1.Image = Image.FromFile("Images\" & p(1) & ".jpg")ptbC1.Image = Image.FromFile("Images\" & c(1) & ".jpg")PtbP2.Image = Image.FromFile("Images\" & p(2) & ".jpg")ptbC2.Image = Image.FromFile("Images\back.jpg")Catch ex As ExceptionMsgBox("Error")End TryEnd SubPrivate Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.TicklblTime.Text = lblTime.Text - 1If lblTime.Text = 0 ThenDisplayData()End IfEnd SubPrivate Sub btnCall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCall.ClickTryIf ptbP3.Image Is Nothing ThenDot = Int(Rnd() * 52 + 1)Loop While t = p(1) Or t = c(1) Or t = p(2) Or t = c(2)p(3) = tptbP3.Image = Image.FromFile("Images\" & p(3) & ".jpg")Add()If PScore >= 22 ThenMsgBox("Yours points beyond the blackjack, you lose!")DisplayData()End IflblTime.Text = 10ElseIf ptbP4.Image Is Nothing ThenDot = Int(Rnd() * 52 + 1)Loop While t = p(1) Or t = c(1) Or t = p(2) Or t = c(2) Or t = p(3)p(4) = tptbP4.Image = Image.FromFile("Images\" & p(4) & ".jpg")Add()If PScore >= 22 ThenMsgBox("Yours points beyond the blackjack, you lose!")DisplayData()End IflblTime.Text = 10ElseIf ptbP5.Image Is Nothing ThenDot = Int(Rnd() * 52 + 1)Loop While t = p(1) Or t = c(1) Or t = p(2) Or t = c(2) Or t = p(3) Or t = p(4) p(5) = tptbP5.Image = Image.FromFile("Images\" & p(5) & ".jpg")lblTime.Text = 10Add()If PScore >= 22 ThenMsgBox("Yours points beyond the blackjack, you lose!")DisplayData()End IfElseIf ptbP6.Image Is Nothing ThenDot = Int(Rnd() * 52 + 1)Loop While t = p(1) Or t = c(1) Or t = p(2) Or t = c(2) Or t = p(3) Or t = p(4) Or t = p(5)p(6) = tptbP6.Image = Image.FromFile("Images\" & p(6) & ".jpg")lblTime.Text = 10Add()If PScore >= 22 ThenMsgBox("Yours points beyond the blackjack, you lose!")DisplayData()End IfEnd IfCatch ex As ExceptionMsgBox("Error")End TryEnd SubPrivate Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.ClickDisplayData()End SubPrivate Sub Calculate()Add()If PScore >= 22 ThenElseIf CScore < 17 ThenDot = Int(Rnd() * 52 + 1)Loop While t = p(1) Or t = c(1) Or t = p(2) Or t = c(2)c(3) = tptbC3.Image = Image.FromFile("Images\back.jpg")Add()If CScore < 17 ThenDot = Int(Rnd() * 52 + 1)Loop While t = p(1) Or t = c(1) Or t = p(2) Or t = c(2) Or t = c(3)c(4) = tptbC4.Image = Image.FromFile("Images\back.jpg")Add()If CScore < 17 ThenDot = Int(Rnd() * 52 + 1)Loop While t = p(1) Or t = c(1) Or t = p(2) Or t = c(2) Or t = c(3) Or t = c(4)c(5) = tptbC5.Image = Image.FromFile("Images\back.jpg")Add()End IfEnd IfEnd IfEnd IfEnd SuPrivate Sub ShowC()PScore = 0CScore = 0For i = 1 To 10If p(i) <= 10 ThenMe.p(i) = p(i)ElseIf p(i) >= 14 And p(i) <= 23 ThenMe.p(i) = p(i) - 13ElseIf p(i) >= 27 And p(i) <= 36 ThenMe.p(i) = p(i) - 26ElseIf p(i) >= 40 And p(i) <= 49 ThenMe.p(i) = p(i) - 39ElseMe.p(i) = 10End IfNextFor i = 1 To 10If c(i) <= 10 ThenMe.c(i) = c(i)ElseIf c(i) >= 14 And c(i) <= 23 ThenMe.c(i) = c(i) - 13ElseIf c(i) >= 27 And c(i) <= 36 ThenMe.c(i) = c(i) - 26ElseIf c(i) >= 40 And c(i) <= 49 ThenMe.c(i) = c(i) - 39ElseMe.c(i) = 10End IfNextEnd SubPrivate Sub DisplayResult()TryIf PScore <= 21 And CScore <= 21 ThenIf PScore > CScore ThenlblResult.Text = "Congratulations, you win!"ElselblResult.Text = "Sorry, you lost!"End IfElseIf PScore = CScore ThenlblResult.Text = "Sorry, you lost!"ElseIf CScore >= 22 And PScore <= 21 ThenlblResult.Text = "Congratulations, you win!"ElselblResult.Text = "Sorry, you lost!"End IfCatch ex As ExceptionMsgBox("Error")End TryEnd SubPrivate Sub DisplayData()TryCalculate()lblPScore.Text = PScorelblCScore.Text = CScoreptbC2.Image = Image.FromFile("Images\" & c(2) & ".jpg")lblResult.Visible = TrueTimer1.Enabled = FalseDisplayResult()btnPlay.Enabled = TruelblPScore.Visible = TruelblCScore.Visible = TruebtnCall.Enabled = FalsebtnStop.Enabled = FalseIf ptbC3.Image Is Nothing ThenElseptbC3.Image = Image.FromFile("Images\" & c(3) & ".jpg") End IfIf ptbC4.Image Is Nothing ThenElseptbC4.Image = Image.FromFile("Images\" & c(4) & ".jpg") End IfIf ptbC5.Image Is Nothing ThenElseptbC5.Image = Image.FromFile("Images\" & c(5) & ".jpg") End IfCatch ex As ExceptionMsgBox("Error")End TryEnd SubPrivate Sub DisplayReset()TrybtnCall.Enabled = TruebtnStop.Enabled = TrueTimer1.Enabled = TruebtnPlay.Enabled = FalselblCScore.Text = ""lblPScore.Text = ""lblResult.Text = ""ptbP3.Image = Nothingp(3) = 0ptbP4.Image = Nothingp(4) = 0ptbP5.Image = Nothingp(5) = 0ptbP6.Image = Nothingp(6) = 0ptbC3.Image = Nothingc(3) = 0ptbC4.Image = Nothingc(4) = 0ptbC5.Image = Nothingc(5) = 0Catch ex As ExceptionMsgBox("Error")End TryEnd SubPrivate Sub btnHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHelp.ClickMsgBox("If your points is 10 + 1 or 1 + 10, points you are 21. So you met in both cases, if you don't have to going to the next card.")End SubPrivate Sub Add()ShowC()For i = 0 To 10If PScore = 11 ThenIf Me.p(1) = 10 And Me.p(2) = 1 ThenIf ptbP3.Image Is Nothing ThenPScore = 21End IfEnd IfIf Me.p(1) = 1 And Me.p(2) = 10 ThenIf ptbP3.Image Is Nothing ThenPScore = 21End IfEnd IfEnd IfIf CScore = 11 ThenIf Me.c(1) = 10 And Me.c(2) = 1 ThenIf ptbP3.Image Is Nothing ThenCScore = 21End IfEnd IfIf Me.c(1) = 1 And Me.c(2) = 10 ThenIf ptbP3.Image Is Nothing ThenCScore = 21End IfEnd IfEnd IfPScore += p(i)CScore += c(i)NextEnd SubPrivate Sub btnRule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRule.ClickMsgBox("Rules:" & vbCrLf & " The game role is computer and the player, respectively. Players play with computer, the computer is a banker. Start , each 2 CARDS. Players according to their own situtaion decided to brand or not.Loses more than 21 points. No more than 21 points,than the size od porins is the winner. When points are at the same time,the banker wins.") End SubEnd Class。
21点游戏代码
RET
RAND ENDP
DISP0 PROC
CALL SPACE ;h312,输出空格
CALL SPACE ;h312,输出空格
CALL SPACE ;h312,输出空格
MOV AH,0 ;读当前时钟值
INT 1AH ;中断
MOV AX,DX
AND AH,3 ;求和
MOV DL,13
DB ' '
DB '****************************************','$'
N DB ?
A9: MOV AL,QH2 ;
CMP QH1,AL ;判断游戏者与电脑点数大小
JA A14 ;h109,判断若电脑点数大,跳转
A10: CALL RAND
MOV QH2,BL ;产生底牌
CALL XX4 ;h252输出message0
A4: MOV AH,0 ;从键盘读字符,ah/al=扫描码
INT 16H
CALL JIAN ;h226,记录积分
CMP JIFEN,10 ;若积分不大于10分,跳转,游戏结束
JB A18
JMP A16 ;若积分大于10,跳转,message4
MOV N,5
JMP A3 ;无条件跳转
JI: MOV N,10
CALL XX20 ;h344,输出字母y
A3: CALL RAND ;h124,产生一个随机数
START: PUSH DS ;将ds寄存器压入栈
MOV AX,0 ;将0传入ax寄存器
PUSH AX ;将ax压入栈
微机程序设计参考——21点扑克游戏设计
21点扑克游戏设计21点游戏中,我们想要实现的功能大致有以下几点,具体的扑克21点规则程序(随机的取牌、自动判断是否大于21点、电脑玩家的牌底隐藏)、玩家的赌注程序、游戏的可循环性、将游戏形象在dos 界面简洁的输出、游戏的难度选择设计。
程序框图:成员: 冯麟堡 2012180016 随机数的生成,dos 界面的输出,辅助功能的实现 林康尘 2012180012 取牌条件判断子程序,输赢判断子程序实现,赌注计算 由子程序的调用和宏指令调用完善整个程序。
循环次数 随机数发牌子程序产生ASCII 码 ASCII 码赋予变量并输出循环次数 随机数发牌子程序产生ASCII 码 ASCII 码赋予变量并输出 电脑取牌条件判断 由输入进行条件判断 START输赢判断子程序 赌注计算子程序 屏幕清理 寄存器清理 变量清理不取 大于2 取 大于2程序代码:DA TA SEGMENTN DW 0COMPUTERN DB 0,0,0,0,0,0,0 ;电脑点数数字PLAYERN DB 0,0,0,0,0,0,0 ;玩家点数数字COMPUTERA DB 0,0,0,0,0,0,0 ;电脑点数ASCIII码CHOOSEP DB 0 ;玩家选择变量S_PLAYER DB 'player: $' ;游戏提示字符串S_COMPUTER DB 'computer:$' ;游戏提示字符串S_GAMETIME DB 'your game times:$';游戏次数提示字符串GAMETIME DB '00$' ;游戏次数字符串GAMETIMEN DB 0 ;游戏次数S_CASH DB 'your cash:$' ;游戏现金提示字符串CASH DB '00500$' ;游戏现金字符串CASHN DW 500 ;游戏现金S_BET DB 'your bet:$' ;游戏赌注提示字符串GAMBLE DB 4,0,0,0,0,0 ;游戏赌注变量GAMBLEN DW 0 ;游戏赌注SPA DB ' ','$' ;空格字符串S_CHOOSE DB 'your choose ',40,' ',41,'$' ;游戏提示字符串WIN DB 'Congratulations! YOU WIN!$' ;游戏提示字符串LOSE DB 'YOU LOSE!$' ;游戏提示字符串DRAW DB 'DRAW!$' ;游戏提示字符串S_INPUT DB '!Please input your bet!$' ;游戏提示字符串NOCASH DB 'NO CASH!$' ;游戏提示字符串REPEAT DB 'play again',129,'(Y or N)$' ;游戏提示字符串CARD DB 'take another card or not',129,'(Y or N)$' ;游戏提示字符串SPA1 DB ' $' ;长空格字符串DA TA ENDSSTACK SEGMENT ;堆栈设置STA DB 128 DUP (0)STACK_TOP DB 0STACK ENDSCODE SEGMENTASSUME CS:CODE,DS:DATA,SS:STACKPRINT MACRO ;设置光标位置,显示字符串MOV AH,02HINT 10H ;根据dx中的内容设置光标位置MOV AH,09H ;屏幕输出SI指针的字符串MOV DX,SIINT 21HENDMCASHSUM MACRO ;将现金cash数字换算为ascii码LEA SI,CASHMOV AX,CASHN ;AX中保存CASHNMOV DX,0 ;DX清零MOV BX,10000DIV BX ;計算CASHN中的萬分位ADD AL,30HMOV [SI],ALINC SIMOV AX,DXMOV DX,0MOV BX,1000DIV BX ;;計算CASHN中的千分位ADD AL,30HMOV [SI],ALINC SIMOV AX,DXMOV DX,0MOV BX,100DIV BX ;計算CASHN中的百分位ADD AL,30HMOV [SI],ALINC SIMOV AX,DXMOV DX,0MOV BX,10DIV BX ;計算CASHN中的十分位ADD AL,30HMOV [SI],ALADD DL,30HMOV [SI+1],DL ;余数即为CASHN的个位ENDMBETPRINT MACRO ;赌注的显示程序宏MOV AH,02HMOV DX,020CHINT 10HLEA SI,GAMBLEMOV AH,02HMOV DL,[SI+2]INT 21HMOV DL,[SI+3]INT 21HMOV DL,[SI+4]INT 21HENDMSTART:MOV AX,DATA ;数据段初始化MOV DS,AX ;设置数据段MOV AX,STACKMOV SS,AX ;设置堆栈段LEA SP,STACK_TOP ;设置堆栈顶端指针LEA SI,S_GAMETIMEMOV DX,0 ;DH为显示器行位置;DL为显示器列位置PRINT ;显示游戏次数文字LEA SI,GAMETIMEMOV DX,0010HPRINT ;显示游戏次LEA SI,S_BETMOV DX,0203HPRINT ;显示赌注文字LEA SI,S_CASHMOV DX,0017HPRINT ;显示现金文字LEA SI,CASHMOV DX,0021HPRINT ;显示现金LEA SI,S_COMPUTER ;显示computer字符MOV DX,0503HPRINTLEA SI,S_PLAYER ;显示player字符MOV DX,0903HPRINTLEA SI,S_CHOOSEMOV DX,0E03HPRINT ;显示CHOOSE字符串NEW:MOV AH,0 ;初始化玩家选择开关MOV CHOOSEP,AHLEA SI,SPA1 ;利用显示空格字符串进行屏幕清理MOV DX,050EHPRINT ;利用显示空格字符串进行屏幕清理MOV DX,060EHPRINT ;利用显示空格字符串进行屏幕清理MOV DX,090EHPRINT ;利用显示空格字符串进行屏幕清理MOV DX,1005HPRINT ;利用显示空格字符串进行屏幕清理MOV DX,1105HPRINTLEA SI,SPA ;利用显示空格字符串进行屏幕清理MOV DX,020CHPRINTLEA SI,COMPUTERN ;电脑扑克点数清零MOV CX,7COMN0:MOV [SI],0INC SILOOP COMN0LEA SI,PLAYERN ;玩家扑克点数清零MOV CX,7PLAYN0:MOV [SI],0INC SILOOP PLAYN0LEA SI,COMPUTERA ;电脑ASCII码清零MOV CX,7COMA0:MOV [SI],0INC SILOOP COMA0MOV AL,GAMETIMEN ;游戏次数重新计算ADD AL,1MOV GAMETIMEN,ALMOV AH,0SUB AL,0AAA ;游戏次数由十六进制变为十进制ADD AH,30HADD AL,30HLEA SI,GAMETIME ;更改游戏次数字符串MOV [SI],AHMOV [SI+1],ALLEA SI,GAMETIME ;游戏次数屏幕显示MOV DX,0010HPRINTLEA SI,S_INPUT ;显示提示输入赌注字符串MOV DX,1106HPRINTAG1:LEA SI,SPA ;输出空格字符串进行屏幕清理MOV DX,0E10HPRINTLEA SI,GAMBLE ;赌注ASCII清零MOV CX,5RG: MOV [SI+1],0INC SILOOP RGMOV AX,0 ;赌注数清零MOV GAMBLEN,AXMOV DX,0E10H ;设置光标位置到your bet:MOV AH,02HINT 10HMOV AH,0AHLEA SI,GAMBLE ;赌注字符串输入存入GAMBLEMOV DX,SIINT 21HMOV BX,1 ;判断输入个数,非法字符不纳入计算BETNUM:CMP [SI+BX+1],30HJS BETSUMCMP [SI+BX+1],40HJNS BETSUMINC BXJMP BETNUMBETSUM:CMP BX,1 ;对赌注字符串变换为数字JZ CMPCGMOV AL,[SI+BX]SUB AL,30HMOV AH,0ADD GAMBLEN,AX ;个位加入GAMBLENDEC BXCMP BX,1JZ CMPCGMOV DX,10MOV AL,[SI+BX]SUB AL,30HMUL DLADD GAMBLEN,AX ;十位加入GAMBLENDEC BXCMP BX,1JZ CMPCGMOV DX,100MOV AL,[SI+BX]SUB AL,30HMUL DLADD GAMBLEN,AX ;百位加入GAMBLEN CMPCG:MOV AX,CASHNMOV DX,GAMBLENCMP AX,DX ;比较输入的赌注和现金大小JNS F1LEA SI,NOCASH ;显示现金不够的字符串MOV DX,1006HPRINTJMP AG1 ;跳转至赌注输入处F1: SUB AX,DX ;现金减去赌注MOV CASHN,AX ;赋予CASHN现金值CASHSUM ;;将现金cash数字换算为ascii码LEA SI,CASH ;屏幕输出CASHMOV DX,0021HPRINTBETPRINT ;赌注屏幕显示BEGIN:LEA SI,SPA1 ;利用空格字符串进行屏幕清理MOV DX,1105HPRINTMOV DX,1005HPRINTLEA SI,SPAMOV DX,0E10HPRINTLEA SI,COMPUTERALEA DI,COMPUTERNMOV DH,05H ;设置DH为05HCMP CX,2 ;电脑取牌条件判断JS TACALL SUM ;调用点数大小计算子程序CMP AH,17JS TA ;大点数小于17取牌CMP AH,22JS NTA ;大点数小于22不取牌CMP AL,17JS TA ;小点数小于17取牌JMP NTATA: CALL 21P ;调用随机产生21点数子程序ADD DI,CX ;DL为点数大小MOV [DI],DL ;存至COMPUTERNINC DIADD SI,CX ;BL为ascii码MOV [SI],BL ;存至COMPUTERAINC SIMOV AX,1CMP CX,0JZ CNP ;当cx等于0,AX=0使子程序输出*MOV AX,0CNP:CALL 21PRINT ;调用点数屏幕显示子程序JMP GOAHEAD ;跳过取牌判断NTA:MOV AH,CHOOSEP ;若电脑不取牌,判断玩家选择CMP AH,1JZ ADDSTAGE ;玩家也不取牌,进行结果统计GOAHEAD:MOV AH,CHOOSEP ;判断玩家选择CMP AH,0JZ PTAKEC ;若玩家CHOOSEP为0,直接执行电脑取牌判断INC CXJMP BEGINPTAKEC:LEA DI,PLAYERN ;指针指向PLAYERNMOV DH,09HCALL 21P ;调用随机数取21点ADD DI,CX ;DL为点数大小MOV [DI],DL ;存入PLAYERN中INC DIMOV AX,1 ;当AX为1时,21PRINT不输出*CALL 21PRINT ;调用21点数屏幕显示子程序CMP CX,6 ;取牌次数判断JZ ADDSTAGE ;双方最多取7次扑克,跳至结果统计INC CXCMP CX,2 ;至少循环两次,再询问玩家的取牌JS BEGINLEA SI,CARD ;屏幕输出取牌的提示MOV DX,1105HPRINTMOV DX,0E10H ;光标位置确认MOV AH,02HINT 10HMOV AH,01H ;单字符输入INT 21HCMP AL,'N' ;玩家输入'N'则跳转JZ DONOTTAKECMP AL,'n' ;玩家输入'n'则跳转JZ DONOTTAKEJMP BEGINDONOTTAKE: ;玩家不取牌,CHOOSEP赋予1 MOV AH,1MOV CHOOSEP,AHJMP BEGINADDSTAGE:MOV DH,06H ;DH为显示的行数LEA SI,COMPUTERA;结果统计时在下一行输出电脑隐藏扑克MOV N,CX ;重置CX次数MOV CX,0SHOWCARD:MOV AX,1 ;重复重置AXMOV BL,[SI]INC SICALL 21PRINT ;调用21点屏幕输出子程序INC CXCMP CX,NJZ SUMMA TIONJMP SHOWCARDSUMMA TION:LEA DI,PLAYERNCALL SUM ;玩家扑克点数和计算子程序CMP AL,22 ;玩家AL点数大于21点即输掉比赛JNS LOSEPCMP AH,22 ;AH点数小于22点,则使用AH点数JNS EQ1MOV DL,AH ;玩家点数赋予DLJMP PO1EQ1:MOV DL,AL ;玩家点数赋予DLPO1:LEA DI,COMPUTERNCALL SUM ;电脑扑克点数和计算子程序CMP AL,22 ;电脑AL点数大于21点即输掉比赛JNS WINPCMP AH,22 ;AH点数小于22点,则使用AH点数JNS EQ2MOV DH,AH ;电脑点数赋予DHJMP PO2EQ2:MOV DH,AL ;电脑点数赋予DHPO2:CMP DH,DL ;DH与DL比较;电脑点数和与玩家点数和比较JS WINP ;胜负跳转JZ DRAWPJMP LOSEPWINP: ;玩家胜利的情况LEA SI,WIN ;提示胜利的字符串屏幕显示MOV DX,100AHPRINTMOV AX,GAMBLEN ;玩家获得双倍的赌注ADD CASHN,AX ;CASHN加上GAMBLENADD CASHN,AX ;CASHN加上GAMBLENJMP GAMEOVERDRAWP: ;玩家平局LEA SI,DRAW ;提示平局的字符串屏幕显示MOV DX,100AHPRINTMOV AX,GAMBLEN ;赌注还给玩家ADD CASHN,AX ;CASHN加上GAMBLENJMP GAMEOVERLOSEP: ;电脑胜利LEA SI,LOSE ;提示失败的字符串进行屏幕显示MOV DX,100AHPRINTJMP GAMEOVERGAMEOVER:MOV AX,CASHNCASHSUM ;CASH十六进制转换为ASCII码LEA SI,CASH ;对CASH进行屏幕显示MOV DX,0021HPRINTLEA SI,REPEAT ;提示重新游戏进行屏幕显示MOV DX,1106HPRINTLEA SI,SPA ;输出空格进行屏幕清理MOV DX,0E10HPRINTMOV DX,0E10H ;设置光标位置到CHOOSE位置MOV AH,02HINT 10HMOV AH,01H ;单字符输入调用INT 21HCMP AL,'Y' ;输入为'Y'时,重新21点游戏JZ NEWCMP AL,'y' ;输入为'y'时,重新21点游戏JZ NEWMOV AX,4C00HINT 21HSUM PROC ;计算点数总和;输出AL,AH为点数和PUSH CXPUSH DXPUSH DIPUSH BXMOV CX,7MOV AX,0MOV BX,0SUMREPEAT:MOV BH,[DI] ;DI指针指向需要计算点数和的变量ADD AL,BHADD AH,BHCMP BH,01H ;BH为01H时进行A扑克特殊规则计算JNZ SUMOVERCMP BL,1 ;A扑克规则只进行一次JZ SUMOVERMOV BL,1ADD AH,10 ;若牌堆中含A,AH为A为11点时点数和,AL为A作1点时点数和SUMOVER:INC DILOOP SUMREPEATPOP BXPOP DIPOP DXPOP CXRETENDP21PRINT PROC ;ASCII码在屏幕输出,需给出DH行位置,BL输出ASCII PUSH CX ;子程序外调用,ax=0输出'*'PUSH AXMOV DL,0BH ;ASCII码显示初始列位置ADD CX,1MOV AX,4MUL CLADD DL,AL ;根据cx确定正确输出位置MOV AH,02H ;光标位置调用INT 10HPOP AXCMP AX,0 ;判断ax,若为0输出*JNZ PRIMOV AH,02HMOV DL,2AH ;'*'的ASCII码为2AHINT 21HJMP PRINTOVERPRI:MOV AH,02HCMP BL,31H ;判断是否为10的ASCIIJNZ NORMALPRMOV DL,31H ;输出1INT 21HMOV DL,30H ;输出0INT 21HJMP PRINTOVERNORMALPR:MOV DL,BL ;输出其余ASCIIINT 21HPRINTOVER:POP CXRETENDP21P PROC ;随机取数计算成扑克点数PUSH CX ;输出dl为点数大小CALL RAND ;输出bl为ascii大小CMP BL,8JS L1JNZ L2MOV DL,1MOV BL,41H ;AJMP OVERL2: MOV DL,10CMP BL,0BHJNS L3ADD BL,41H ;J/KJMP OVERL3: JNZ L4MOV BL,51H ;QJMP OVERL4: MOV BL,31H ;10JMP OVERL1: MOV DL,BLADD DL,2ADD BL,32H ;2~9OVER:POP CXRET21P ENDPRAND PROC ;随机数产生器PUSH CXPUSH DXPUSH AXSTIMOV AH,0 ;读取时钟计数器数值INT 1AHMOV AX,DXAND AH,0 ;清高位MOV DL,13 ;除以13,产生0~12余数DIV DLMOV BL,AH ;余数存bl,作随机数CLIPOP AXPOP DXPOP CXRETRAND ENDPCODE ENDS程序中的关键功能调用:MOV AH,02HINT 10H功能为设置当前显示光标位置,输入参数有BH=页数,DH=行数,DL=列数MOV AH,00HINT 1AH功能为读取时钟“滴答”计数,出口参数有AL表示是否经过午夜;CX:DX=时钟“滴答”计数通过调用后,读取的计数单位是微秒量级的,即使同样程序多次运行过程,其中输出的DX 都不同,能够很好的实现小范围的随机数设计。
编程算21点的思路
编程算21点的思路21点(也称为黑杰克)是一种流行的纸牌游戏,通常由一名玩家与庄家对战。
游戏的目标是使手中的牌的点数尽量接近21点,但不能超过。
如果玩家的手牌点数超过21点,则玩家输掉游戏;如果玩家的手牌点数等于或小于21点,而庄家的手牌点数超过21点,则玩家获胜。
如果玩家和庄家的手牌点数都小于或等于21点,那么点数更接近21点的一方获胜。
在编程21点游戏时,可以遵循以下思路:1. 创建一副扑克牌,首先,你需要创建一副扑克牌,包括52张牌,每张牌有自己的点数和花色。
你可以使用数组或者集合来表示扑克牌,然后初始化这副牌。
2. 发牌,在游戏开始时,玩家和庄家会分别获得两张牌。
你可以编写一个发牌的函数,从扑克牌中随机抽取两张牌分别给玩家和庄家。
3. 计算手牌点数,根据扑克牌的点数规则,计算玩家和庄家手中牌的点数。
A可以算作1点或者11点,J、Q、K都算作10点,其余牌按照牌面点数计算。
4. 玩家决策,玩家可以选择继续叫牌(要求再发一张牌)或者停止叫牌。
你可以编写一个函数,根据玩家手中的牌和当前点数来判断玩家的决策。
5. 庄家决策,庄家的决策通常是固定的,当庄家手中的牌点数小于17点时,必须继续叫牌,直到点数大于等于17点为止。
6. 判断胜负,最后,根据玩家和庄家手中牌的点数来判断胜负。
如果有一方的点数超过21点,则对方获胜;否则比较双方的点数,点数更接近21点的一方获胜。
在编程实现21点游戏时,需要考虑游戏规则、玩家决策、庄家决策以及胜负判断等多个方面,同时需要考虑如何表示扑克牌、如何进行发牌和计算点数等细节。
希望这个思路对你有所帮助。
python实现21点小游戏
python实现21点⼩游戏⽤python实现21点⼩游戏,供⼤家参考,具体内容如下from random import shuffleimport randomimport numpy as npfrom sys import exit# 初始化扑克牌playing_cards = {"⿊桃A": 1, "⿊桃2": 2, "⿊桃3": 3, "⿊桃4": 4, "⿊桃5": 5, "⿊桃6": 6, "⿊桃7": 7, "⿊桃8": 8, "⿊桃9": 9, "⿊桃10": 10, "⿊桃J": 10, "⿊桃Q": 10, "⿊桃K": 10,"红桃A": 1, "红桃2": 2, "红桃3": 3, "红桃4": 4, "红桃5": 5, "红桃6": 6, "红桃7": 7, "红桃8": 8, "红桃9": 9, "红桃10": 10, "红桃J": 10, "红桃Q": 10, "红桃K": 10,"梅花A": 1, "梅花2": 2, "梅花3": 3, "梅花4": 4, "梅花5": 5, "梅花6": 6, "梅花7": 7, "梅花8": 8, "梅花9": 9, "梅花10": 10, "梅花J": 10, "梅花Q": 10, "梅花K": 10,"⽅块A": 1, "⽅块2": 2, "⽅块3": 3, "⽅块4": 4, "⽅块5": 5, "⽅块6": 6, "⽅块7": 7, "⽅块8": 8, "⽅块9": 9, "⽅块10": 10, "⽅块J": 10, "⽅块Q": 10, "⽅块K": 10}# 扑克牌⾯poker_name = list(playing_cards.keys())# 扑克牌的数量poker_count = 1poker_list = poker_count*poker_name# ⽤于判断⼿中的牌是否有A,再根据牌⾯判断A是否取值1还是11four_a = {'⿊桃A', '红桃A', '梅花A', '⽅块A'}# 计分器total_score = np.array([0, 0])# 记录回合数game_round = 1def random_cards(poker_name_list):"""定义洗牌函数:重新对牌进⾏随机排列"""shuffle(poker_name_list)def score_count(hand_poker):"""计算⼿中牌的分数:param hand_poker:⼀个含有牌名的列表:return: ⼿中牌的分数poker_score"""# 声明⼀个变量,记录牌的总分数poker_score = 0# 标记:判断是否有A的标记,默认没有have_a = False# 计算⼿中牌的分数for k in hand_poker:poker_score += playing_cards[k]# 判断⼿中的牌是否含有A,再根据A的规则进⾏分数的计算for i in hand_poker:if i in four_a:have_a = Truebreakelse:continueif have_a:if poker_score + 10 <= 21:poker_score = poker_score + 10return poker_scoredef who_win(your_score, pc_score):"""判断游戏的胜负:param your_score: 玩家分数:param pc_score: 电脑分数:return: 胜负的数组"""if your_score > 21 and pc_score > 21:print('平局')return np.array([0, 0])elif your_score > 21 and pc_score <= 21:print('对不起,玩家输了')return np.array([0, 1])elif your_score <= 21 and pc_score > 21:print('恭喜!!玩家胜利了')return np.array([1, 0])elif your_score <= 21 and pc_score <= 21:if your_score > pc_score:print('恭喜!!玩家胜利了')return np.array([1, 0])elif your_score < pc_score:print('对不起,玩家输了')return np.array([0, 1])else:print('平局!!')return np.array([0, 0])def if_get_next_poker():"""是否继续要牌"""if_continue = input("是否继续要下⼀张牌?(Y/N)>>>>:")if if_continue.upper() == "Y":return get_one_poker()elif if_continue.upper() == "N":print('玩家停⽌叫牌')return Falseelse:print("输⼊有误,请重新输⼊")return if_get_next_poker()def get_one_poker():"""发牌函数:随机将poker_list⾥的牌取出⼀张:return:"""return poker_list.pop(random.randint(0, len(poker_list)-1))def continue_or_quit():"""⼀轮游戏结束后,询问玩家是否进⾏下⼀轮"""if_next_round = input("是否进⾏下⼀轮游戏(Y/N)>>>>:")if if_next_round.upper() == 'Y':# 判断扑克牌是否玩的了下⼀轮if len(poker_list) <= 15:print('对不起,剩余牌数不⾜,⽆法进⾏下⼀轮,游戏结束。
C语言程序设计21点扑克牌游戏
实用标准文案实验报告课程名称:C语言程序设计实验项目:21点扑克牌游戏设计专业班级:电子信息工程实验室号:信息205 实验组号:xx 实验时间:2010-5-4 批阅时间:指导教师:成绩:沈阳工业大学实验报告(适用计算机程序设计类)专业班级:电子信息工程0902 学号:姓名:实验名称:21点扑克牌游戏附件A 沈阳工业大学实验报告(适用计算机程序设计类)专业班级:电子信息工程0902 学号:姓名:实验步骤或程序:(我所做的程序是作弊程序改动已经加粗)#include<time.h>#include<stdio.h>#include<conio.h>#include<stdlib.h>void Wait_f(int);void Pause_f();/*暂停,按任意键继续*/int Random_f(long ,long );/*返回两个参数之间的随机数*/void Replay_f(char *);/*询问是否重开一局*/void Hit_f(int *);/*发一张牌*/void Deal_f(int *,int *,int *,int *);/*双方各发两张牌*/void Bet_f(int *,int *);/*接受下注的数目*/void Print_f(int ,int ,int ,int );/*打印结果*/void Rules_f();/*显示游戏规则*/void Results_f(int ,int ,int ,int *,int *,int *,int *);/*统计结果(输赢平)*/void Hit_f2(int * npTotal,int a);int nnn;int main(){int nPlayer=0,nCpu=0,nWin=0,nLose=0,nDraw=0,nPlayerTurns=0, nCpuTurns=0,nMoney=0,nBet;/* nPlayer——玩家手中全部牌的点数和,nPlayerTurns——玩家手中牌的数量*/char chAnswer;char mmm;int a;system("cls");/*执行系统命令“清屏”*/Rules_f();printf("\t\t\tWOULD YOU LIKE TO PLAY? ");scanf("%c",&chAnswer);if((chAnswer=='y')||(chAnswer=='Y')){printf("\t\t\tI WILL LET YOU START WITH $100\n");nMoney=100;printf("\t\t\t");Pause_f();}else{return(0);}do{system("cls");if(nMoney<0){printf("Your broke\n");return(0);}Bet_f(&nBet,&nMoney);/*接受下注的数目*/Deal_f(&nPlayer,&nCpu,&nPlayerTurns,&nCpuTurns);/*双方各发两张牌*/ printf("\t\t\tWould you want to cheat? yes or no?");scanf("%c",&mmm);if((mmm=='y')||(mmm=='Y')){printf("%d",nnn);printf("\t\t\twhich card do you want?");scanf("%d",&a);}system("cls");do{printf("\t\t\tWould You Like To Hit or Stay :");/*询问玩家是否接着要牌*/scanf(" %c",&chAnswer);if((chAnswer=='h')||(chAnswer=='H')){nPlayerTurns++;/*计算总牌数*/if(nPlayerTurns>5)/*看是否允许再要牌*/{printf("\t\t\tYou Can't Have more than 5 cards");}}if((nPlayerTurns<6)&&(chAnswer=='h')){printf("\n");if((mmm=='y')||(mmm=='Y'))Hit_f2(&nPlayer,a);else Hit_f(&nPlayer);/*为玩家发一张牌*/}}while((chAnswer=='h')||(chAnswer=='H'));for(;(nCpu<16)&&(nCpuTurns<6);nCpuTurns++)/*计算机要牌的条件*/{printf("\n");printf("\t\t\tThe Dealer Takes A Card");Hit_f(&nCpu);/*为计算机发一张牌*/}printf("\n\n");printf("\t\t\tThe Dealer Has A Total: %d\n", nCpu);/*显示总点数*/printf("\t\t\tYou Have A Total Of: %d\n", nPlayer);printf("\n");Results_f(nPlayer,nCpu,nBet,&nMoney,&nDraw,&nWin,&nLose);/*计算最后的结果(统计输赢平的局数)*/Replay_f(&chAnswer);/*询问是否重开一局*/}while((chAnswer=='y')||(chAnswer=='Y'));Print_f(nWin,nLose,nDraw,nMoney);/*打印最后结果*/printf("\n");printf("\t\t\t\t");return(0);}void Rules_f() /*这个函数显示游戏规则*/{printf("\t\t WELCOME TO ISAAC SHAFFERS BLACK JACK GAME\n");printf("\t\t\t HERE ARE SOME SIMPLE RULES\n");printf("\t\t\t1:You Can only have a max of 5 cards.\n");printf("\t\t\t2:If you bust you automatically lose.\n");printf("\t\t\t3:If you win you win double what you bet");printf("\t\t\t4:The Dealer stops at or after 16.\n");printf("\t\t\t\t");getchar();}void Bet_f(int* npBet,int* npMoney) /*该函数接受玩家下注,并计算赌资的总数*/{system("cls");printf("\t\t\tYOU HA VE:$%d\n",*npMoney);printf("\t\t\tHow Much Do You Wish To Bet:");scanf("%d",npBet);if(*npBet<0){*npBet=*npBet*(-1);}*npMoney=*npMoney-*npBet;}void Deal_f(int* npPlayer,int*npCpu,int*npPlayeReturns,int *npCpuReturns)/*该函数是给计算机与玩家各发两张牌*/{int nPlayerCard1,nPlayerCard2,nCpuCard1,nCpuCard2;*npPlayeReturns=2;*npCpuReturns=2;nPlayerCard1=Random_f(13,1);printf("\n\t\t\tDEALING HAND\n");getchar();Pause_f();nPlayerCard2=Random_f(13,1);Pause_f();nCpuCard1=Random_f(13,1);Pause_f();nCpuCard2=Random_f(13,1);if (nPlayerCard1>=10){nPlayerCard1=10;}if (nPlayerCard2>=10){nPlayerCard2=10;}if (nCpuCard1>=10){nCpuCard1=10;}if (nCpuCard2>=10){nCpuCard2=10;}*npPlayer=nPlayerCard1+nPlayerCard2;*npCpu=nCpuCard1+nCpuCard2;printf("\t\t\tYOU HA VE a total of:%d\n",*npPlayer);printf("[%d]", nPlayerCard1);printf("[%d]",nPlayerCard2);printf("\n");printf("\t\t\tThe DEALER HAS A %d SHOWING",nCpuCard1);printf("\n");printf("[*][%d]",nCpuCard1);nnn=nCpuCard2;}void Hit_f(int * npTotal) /* 发一张牌,返回点数之和*/{int nCard;nCard=Random_f(13,1);if(nCard>=10){nCard=10;}* npTotal=* npTotal+nCard;printf("\t\t\tThe card is a :%d\n", nCard);printf("\t\t\tTotal is :%d\n", * npTotal);}void Results_f(int nPlayer,int nCpu,int nBet,int * npMoney,int * npDraw,int* npWin,int * npLose)/*统计结果(输赢平及输赢平的局数)*/ {if(nCpu==nPlayer){printf("\t\t\tIT WAS A DRAW HOUSE WINS\n");++* npDraw;}if(nPlayer>21){printf("\t\t\tYou Bust\n");++* npLose;}else{if(nCpu<nPlayer){printf("\n\t\t\tYOU WIN");*npMoney=*npMoney+(nBet*2);++* npWin;}}if(nCpu>21){printf("\t\t\tDealer Bust\n");if(nPlayer<=21){printf("\n\t\t\tYOU WIN");++* npWin;*npMoney=*npMoney+(nBet*2);}}else{if(nCpu>nPlayer){printf("\t\t\tYOU LOSE\n");++* npLose;}}}void Replay_f(char *chpAns) /*询问是否重开一局*/ {printf("\n\t\tWould You Like To Play This Game Again:");scanf("%c",chpAns);}void Print_f(int nWins,int nLose,int nDraw,int nMoney){printf("\t\t\t\tWINS:%d\n", nWins);printf("\t\t\t\tLOSE:%d\n", nLose);printf("\t\t\t\tDRAWS:%d\n",nDraw);printf("\t\t\t\tMONEY:%d\n",nMoney);}void Wait_f(int nMilli){int nStart1,nStart2;for(nStart1=0;nStart1<nMilli*100;nStart1++)for(nStart2=0;nStart2<nMilli*100;nStart2++){;}}void Pause_f(){printf("Press Any Key To Continue");getchar();}int Random_f(long lBigNumber, long lSmallNumber) /*选择两个参数之间的随机数*/{int nRan;srand((unsigned)time(NULL));nRan=rand()%(lBigNumber-(lSmallNumber-1))+lSmallNumber;return(nRan);}void Hit_f2(int * npTotal,int a) /* 发一张牌,返回点数之和*/{int nCard;nCard=a;if(nCard>=10){nCard=10;}* npTotal=* npTotal+nCard;printf("\t\t\tThe card is a :%d\n", nCard);printf("\t\t\tTotal is :%d\n", * npTotal);}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
j++;
}
return sum;
}
void Player::showpoker()
{
int j=0;
while(card[j]!=NULL)
{
card[j]->GetText();
j++;
}
cout<<endl;
{
int m=get(54,1)%54;
Card r;
r=card[k];
card[k]=card[m];
card[m]=r;
}
}
int Poker::Fapai()
{
if(count<=54&&count>=1){
card[54-count].GetText();
#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <time.h>
#include"string.h"
#include<iostream>
using namespace std;
///////////////////////
///////////////////////////////////////////
class Player
{
public:
Card *card[20];
int count;
char name[10];
int score;
Player(){
count=score=0;
count--;
}
else
cout<<"牌已经发完!"<<endl;
return count;
}
Card *Poker::getcard(void)
{
if(count<=54&&count>0)
{
h=&card[54-count];
count--;
return h;
int num,n;
char boss[10],a[10];
public:
Newgame();
void login();
void begin();
char *victory();
bool callpoker(Player &);
void show();
};
////////////////////////
}
else
return 0;
}
}
}
int Player::showpoint()
{
int j=0,sum=0;
while(card[j]!=NULL)
{
if(card[j]->outnum()>10||card[j]->outnum()==0) sum+=1;
}
void Player::reset()
{
for(int i=0;i<20;i++)
card[i]=NULL;
count=0;
}
char *Newgame::victory()
{
int k=1;Player *r=player[0];
while(k<num)
}
else
cout<<"牌已经发完!"<<endl;
return NULL;
}
int Poker::sum(int a,int b,int c)
{
return card[a].outnum()+card[b].outnum()+card[c].outnum();
}
////////////////////////////////////
class Card
{
private:
char m_type;
int m_num;
public:
void card(char a,int b){m_type=a,m_num=b;}
int outnum(){return m_num;}
if(m_type=='S'){ cout<<"黑桃Q "; return 1;}
}
case 0:{if(m_type='H'){ cout<<"红桃K "; return 1;}
if(m_type=='C'){ cout<<"梅花K "; return 1;}
if(m_type=='D'){ cout<<"方块K "; return 1;}
cin>>a;
}
if(a=='Y'||a=='y')
{
t.card[t.count]=poker->getcard();
cout<<"你抓到了一张 ";
t.card[t.count]->GetText();
cout<<endl;
t.count++;
return 1;
if(m_type=='S'){ cout<<"黑桃J "; return 1;}
}
case 12:
{
if(m_type=='H'){ cout<<"红桃Q "; return 1;}
if(m_type=='C'){ cout<<"梅花Q "; return 1;}
if(m_type=='D'){ cout<<"方块Q "; return 1;}
{
if(r->showpoint()==player[k]->showpoint())
{
if(!strcmp(player[k]->name,boss))
for(int i=0;i<20;i++)
card[i]=NULL;
}
int showpoint();
void showpoker();
void reset();
};
class Newgoker *poker;
Player *player[5];
};
///////////////////////////////////////////////////
Card *h;
int get(int max,int min);
// TODO: reference additional headers your program requires here
return n;
}
int Card::GetText()
{
if((1==m_num)&&(m_type=='K')){ printf("大王 ");return 1;}
if((0==m_num)&&(m_type=='K')){ printf("小王 ");return 1;}
else{
cout<<"你想继续抓牌吗?你现在的点数为:"<<t.showpoint()<<" Y or N"<<endl;
char a;
cin>>a;
while(a!='y'&&a!='Y'&&a!='N'&&a!='n')
{
cout<<"请你再次确认输入是否出错?(N or Y)"<<endl;
if(m_type=='S'){ cout<<"黑桃K "; return 1;}
}
return 1;
}
if(m_num<=10&&m_num>=2)
{
if(m_type=='H'){ cout<<"红桃"<<m_num<<" "; return 1;}
if(m_type=='C'){ cout<<"梅花"<<m_num<<" "; return 1;}
void Newgame::show()
{
for(int f=0;f<54;f++)
poker->Fapai();
}
/////////////////////////////////