21点 高质量C语言程序
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);}}。
编写高质量的单片机C程序word精品文档15页

§5 编写高质量的单片机C程序收集于网络高质量的单片机C程序代码运行效率高、可读性和可维护性强。
在编写小型的单片机程序时,代码质量的重要性可能不是很明显。
但如果要编写较大规模的程序,特别是多人合作编写程序时,这一点就变得十分重要了。
本章内容以林锐的《高质量C++/C编程指南》为蓝本,针对单片机编程的实际各部进行了大量删节和改写。
5.1 文件结构每个单片机C程序通常分为两种文件。
一个文件用于保存程序的声明(declaration),称为头文件。
另一个文件用于保存程序的实现(implementation),称为定义(definition)文件。
程序的头文件以“.h”为后缀,定义文件以“.c”为后缀。
5.1.1 版权和版本的声明版权和版本的声明位于头文件和定义文件的开头(参见示例5.1),主要内容有:(1)版权信息。
(2)文件名称,标识符,摘要。
(3)当前版本号,作者/修改者,完成日期。
(4)版本历史信息。
* Copyright (c) 2019,上海贝尔有限公司网络应用事业部* All rights reserved.* 文件名称:filename.h* 文件标识:见配置管理计划书* 摘要:简要描述本文件的内容* 当前版本:1.1* 作者:输入作者(或修改者)名字* 完成日期:2001年7月20日* 取代版本:1.0* 原作者:输入原作者(或修改者)名字* 完成日期:2001年5月10日示例5.1 版权和版本的声明5.1.2 头文件的结构头文件由三部分内容组成:(1)头文件开头处的版权和版本声明(参见示例5.1)。
(2)预处理块。
(3)函数声明等。
假设头文件名称为 graphics.h,头文件的结构参见示例5.2。
【规则5-1-2-1】为了防止头文件被重复引用,应当用ifndef/define/endif结构产生预处理块。
【规则5-1-2-2】用 #include <filename.h> 格式来引用标准库的头文件(编译器将从标准中国科学技术大学业余无线电协会6451 单片机C 语言编程入门——Easy 51 Kit Pro 配套学习资料库目录开始搜索)。
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(); // 调用赢牌的代码,且是赢得了双倍金钱}}。
好玩的c语言代码

好玩的c语言代码C语言是一种通用过程式编程语言,可以用于开发各种类型的应用程序。
它具有跨平台、高效、灵活等特点,因此被广泛用于系统软件、嵌入式系统、游戏开发等领域。
下面我将介绍一些好玩的C语言代码示例,这些示例可以帮助初学者更好地理解和掌握C语言的基本概念和语法。
1. Hello, World!这是C语言中经典的示例程序,用于输出"Hello, World!"到屏幕上:```#include <stdio.h>int main() {printf("Hello, World!\n");return 0;}```这个示例展示了C语言的头文件引用、主函数和输出函数的使用。
2. 猜数字游戏这是一个简单的猜数字游戏,用户需要通过键盘输入猜测的数字,如果猜对则游戏结束,否则提示用户继续猜测。
```#include <stdio.h>#include <stdlib.h>#include <time.h>int main() {int target, guess, count = 0;srand(time(0));target = rand() % 100 + 1;printf("猜数游戏开始!\n");do {printf("请输入一个1到100之间的整数:");scanf("%d", &guess);count++;if (guess > target) {printf("太大了,请再试一次。
\n");} else if (guess < target) {printf("太小了,请再试一次。
\n");} else {printf("恭喜你,猜对了!你一共猜了%d次。
\n", count); }} while (guess != target);return 0;}```这个示例展示了C语言的随机数生成、循环和条件分支语句的使用。
高质量C编程指南(1)

C语言21点纸牌游戏系统详细设计

C语言21点纸牌游戏系统详细设计主函数开键盘输产生随机键盘输输入H 输出统计选择是否循结图2.1 进入游戏后的游戏说明及规则2.2开始游戏图2.2 选择下注数目,然后随机发牌,只能见庄家一张牌,可以选择查看庄家隐藏的牌,此为游戏高级模式,开启后可选择想要的牌2.3作弊模式图2.4在作弊模式下取胜,可以接着要牌,但玩家的牌最多不能超过5张,取胜后会统计结果,询问是否继续游戏2.5非作弊模式图2.5 此情况为非作弊模式,不能见庄家的牌,只能知道结果,输掉游戏后统计结果,询问是否继续2.6退出游戏图2.6此为退出游戏,输入后自动关闭窗口3.源程序:#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 *);/*双方各发两张牌*/nCpuTurns=0,nMoney=0,nBet;/* nPlayer--玩家手中全部牌的点数和,nPlayerTurns--玩家手中牌的数量 char chAnswer;char mmm;int a;system("cls");/* 执行系统命令"清屏" */Rules_f();printf("确认是否开始游戏:");scanf("%c",&chAnswer);if((chAnswer=='y')||(chAnswer=='Y')){nMoney=100;printf("\t\t\t");Pause_f();}else{return(0);return(0);}Bet_f(&nBet,&nMoney);/*接受下注的数目*/Deal_f(&nPlayer,&nCpu,&nPlayerTurns,&nCpuTurns);/*双方各发两张牌*/printf("想看庄家牌吗?");scanf("%c",&mmm);if((mmm=='y')||(mmm=='Y')){printf("%d\n",nnn);printf("您想要哪一张牌?");scanf("%d",&a);}system("cls");do{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("\n");printf("\t\t\t\t");return(0);}void Rules_f() /*这个函数显示游戏规则*/{printf("欢迎来到21点纸牌游戏\n");printf("这里有一些简单的规则:\n");printf("1:你的几张牌中只能有一个最大的牌。
C#代码--编写高质量C#程序

1.1 换行的讲究 51.1.1 寻找最佳的断行位置51.1.2 每行只写一条语句71.1.3 分行定义变量81.2 避免代码过于拥挤91.2.1 使用空行分隔代码块101.2.2 使用空格降低代码密度131.3 如何缩进151.3.1 嵌套或包含关系引起的缩进171.3.2 因换行而产生的缩进201.3.3 使用空格还是Tab键211.4 大括号 221.4.1 大括号的位置221.4.2 空的大括号结构231.4.3 仅包含单个语句的结构体261.5 保持项目文件的条理性291.5.1 解决方案的结构呼应291.5.2 代码文件的结构291.5.3 使用#region标记32第2章养成良好的注释习惯332.1 何时需要注释332.1.1 解释代码的意图342.1.2 对局部变量的说明352.1.3 充当代码标题362.1.4 指出例外情况402.1.5 开发提示 402.2 注释的格式 412.2.1 单行注释422.2.2 多行注释442.3 正确使用XML文档注释452.3.1 结构与类的XML文档注释462.3.2 属性的XML文档注释472.3.3 方法的XML文档注释492.3.4 构造函数的XML文档注释502.3.5 事件的XML文档注释512.3.6 枚举类型的XML文档注释532.3.7 泛型的XML文档注释532.3.8 其他标记54总的来说,如果多看看MSDN自身的类库参考,你会发现其实XML文档注释最终形成的就是这样的结果。
MSDN本身就是一个最好的XML文档注释的范例,我们可以在实践过程中不断地进行模仿和学习。
第7章如何使用函数577.1 为什么要使用函数577.1.1 函数与方法577.1.2 代码复用597.1.3 隐藏细节617.2 函数重载657.2.1 重载的语义657.2.2 保持核心代码唯一707.3 参数的设计747.3.1 参数的命名747.3.2 不要使用保留项747.3.3 变长参数列表757.3.4 何时使用ref参数和out参数777.3.5 参数的顺序787.3.6 重载函数的参数一致797.4 参数检查827.4.1检查零值及空引用837.4.2 检查枚举类型847.4.3 防止数据被篡改857.4.4 在何处检查合法性877.5 函数出口887.5.1 返回值的用法887.5.2 离开函数的时机89假设我们写的是文章而不是程序,那么你一定觉得诸如文章应该分为若干个自然段、每段开头空两格之类的规则是理所当然的。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
设计题目: 21点游戏二十一点游戏是一款操作简单,老少皆宜,并且带有很强娱乐性的游戏。
二十一点游戏也是我们学习C语言过程中用来锻炼我们编程能力的一种简单有效的方式。
编写二十一点游戏的目的就是在编写过程中学习和掌握各种编程的能力,二十一点游戏中有我们经常接触到的一些头文件,和小的函数体,以及一些相关知识,能让我们在实践中得到锻炼。
一、设计目的与要求1、程序设计的源程序功能如下:(1)将所有的函数与相应的数据封装到类中,并改写主程序,使程序简化。
(2)程序中一共有13张扑克牌可以随机抽取,大于10的点数为10,现要求大于10的点数全部为半点。
(3)要求庄家永远不会暴庄,即庄家可以查看下一张牌,若大于21点,则拒绝,当然,在游戏规则上不能泄露这点秘密。
(4)高级玩家可以查看下一张牌,即按h键,然后按要求输入密码,密码正确可以看牌,并决定是否要牌。
(5)每次要牌后可以设定赔率,即如果开始下的是10元钱的注,如果牌好,你可以要求再下注,当然不能超过你所有的总钱数。
(6)将界面的提示说明改成中文界面,界面的解释详细友好,可以吸引更多的玩家。
二、课程设计(1)题目要求把源程序封装在类中,需要编写一个类来封装所有相关函数和变量,同时对main主函数进行改写,以完成题目的要求。
(2)题目要求把抽到的大于10点的牌的点数设为半点,只需要设计随机取下一张牌的成员函数时用if语句判定牌的点数是否大于10,大于10的牌对其重新赋值为0.5即可。
(3)题目要求超级玩家在输入密码后可以查看下一张牌,只需设计一成员函数,判定玩家是否继续要牌之前加入对玩家是否输入h键的判定,如果有输入,则调用为处理玩家输入密码而单独编写的成员函数,在该函数中,首先判定玩家的密码输入是否正确,不正确则返回0,正确则返回1。
(4)题目要求玩家可以根据手上的牌决定是否下注,只需设计一成员函数,检查玩家的赌注累计不超过他所拥有的总资本。
三、设计描述1、为满足上述第一条要求,在程序中增加了一个类play,封装到类中的函数有:void replay(); //检测是否重玩void hitplayer(); //玩家要牌void hitcpu();//电脑要牌void deal(); //开始发牌void BET(); //加注void print(); //打印最终结果void rules(); //显示规则void results();//判断获胜方int getmoney();//读取钱数至主程序void shuruans();//输入判断用字符(如Y,S等)void firstbet();//首次下注;void writer(void) ; //写入文本文件void load(void); //读取文本文件源程序已有的函数中,void replay(),void rules(), void results(), void print()已经较为完善,仅需将其英文界面部分改为中文即可,而其他函数则应按要求改写。
2、为满足上述第二条要求,按一般玩法>=10的数本应该都按10来算,而这里这些书全都算作0.5,所以if(card>=10)card=0.5;即可。
3、第三条要求较为困难,将原程序中的一个void hit(int &total)函数改编为void hitplayer()与void hitcpu()两个函数,分别用来运行玩家及庄家的要牌过程。
要求庄家永远不会暴庄,即是要让庄家可以看牌并判断要牌后点数是否超过21点。
因此函数改写为:void play::hitcpu(void){float total;for(;(cpu<16)&&(cputurns<6);cputurns++){cout<<endl;card=random(13,1);if(card>=10)card=0.5;total=cpu+card;if(total<=21){cpu=total;cout<<"\t\t\t庄家拿牌"<<endl;cout<<"\t\t\t这张牌是 :"<<card<<endl;cout<<"\t\t\t总共 :"<<cpu<<endl;wait(1350);}}}使用一个中间变量total即可判断且不影响庄家的实际总点数。
设置超级玩家即是设计一成员函数,判断是否输入正确密码。
由于要求要牌也是输入H,因此只需要将函数语句一并编入void hitplayer()中,函数如下:void play::hitplayer(void){char str[20];char c;nocheats=0;cout<<"请输入密码"<<endl;cin>>str;card=random(13,1);if(card>10) {card=0.5;}if(strcmp(str,Password)==0){cout<<"下张牌是"<<card<<endl;cout<<"输入y要牌,其他键不要"<<endl;cin>>c;if(c=='y'||c=='Y'){player=player+card;cout<<"\t\t\t牌面是:"<<card<<endl;}else{nocheats=1;}cout<<"\t\t\t总的牌面是:"<<player<<endl;}else{player=player+card;cout<<"\t\t\t牌面是:"<<card<<endl;cout<<"\t\t\t总的牌面是:"<<player<<endl;}}由strcmp()语句判断输入密码的正确性,若正确则可看牌并决定是否要牌,若不正确则由于已选择要牌(已输入了要牌指令“H”)则拿牌并计入总点数中。
加入参数nocheats是由于判断是否要牌的语句在主函数中,不加入参数则可能出现玩家不要牌后仍可再次拿牌(正确结果应是结束玩家拿牌,改由庄家拿牌)。
4、第四条要求中的总下注数不超过总资产数。
因此需设计函数使每次要牌后能输入加注数并加入总下注数中。
使用void BET()完成加注设定,函数如下:void play::BET(){int a=0;do{cout<<"\t\t\t你拥有"<<money<<"元"<<endl;cout<<"\t\t\t请继续下注";cin>>a;if(a<0){a=a*-1;}if(a>money){cout<<"你没有足够的钱!"<<endl;}}while(a>money);bet=bet+a;money=money-a;}也是使用中间变量a,目的在于判断总下注数不超过总资产数,若超出则将警告并要求重新输入,且不会误计入总下注数中。
又由于每局后下注金额应清零,且加注提示应与下注提示不同,故增设函数如下:void play::firstbet(void){cout<<"\t\t\t你拥有"<<money<<"元"<<endl;cout<<"\t\t\t请下注";cin>>bet;if(bet<0){bet=bet*-1;}money=money-bet;}即是为防止上一局的干扰。
此外,还需将游戏的数据存入文件中,方便下次使用,因此加入了函数void writer(void)和void load(void),分别用来将游戏的主要数据写入磁盘文件中,可以起到存盘与读盘的效果。
此外,函数int getmoney()用来读取类中的参数money,由于此参数为私有的,且又需在主函数中使用,故设置此函数。
但使用次数并不多是一个缺憾。
同时,函数void shuruans()用于在函数外输入参数ans(char ans; //判断用字符型)变量进行判断。
由于ans封装在类中,设置这个函数目的即在于此。
此程序运作方式如下:void play::shuruans(void){cin>>ans;}这样在类体外就可以通过void shuruans()对ans进行赋值.四、调试分析1、游戏开始2、游戏过程3、游戏结果五、总结主函数的设计显得有些繁冗杂乱,有很多的程序段有简化的空间。
但由于能力有限,不能给出更好的设计方案。
以至于函数中出现很多而且肢体庞大的条件、循环语句,程序的可读性也不强。
对简化函数体的设计要求没有达到。
密码设计段的知识运用有问题,因为对设计原理不太明白,只是按照题给提示编写该部分程序,或许存在设计上的错误。
两三个星期的调试程序,让我感受到C的魅力,也让我知道只有不断努力的尝试,最终都会成功。
六、答辩记录七、教师意见参考文献:【1】C程序设计(第四版)谭浩强清华大学出版社【2】Visual C++.NET完全手册 Chris H.Pappas, William H.Murray 电子工业出版社。