public override string ToString() { return (string.Format("{0} of {1}",this.val.ToString("G"), this.suit.ToString("G"))); } }// 扑克牌类 //==========================================class deck { public card[] cards=new card[53]; int next;
// deck() //Constructor for setting up a regular deck //========================================== public deck() { next = 1;
cards[0].val = 0; cards[0].suit = 0;
int currcard = 0; for ( int suitctr = 1; suitctr<5;suitctr++) { for ( int valctr=1;valctr<14;valctr++) { currcard=(valctr)+((suitctr-1)*13); cards[currcard].val=(CardValue)valctr; cards[currcard].suit=(CardSuit) suitctr; } } }
// Create the controls... Label myDateLabel = new Label(); Label myLabel = new Label();
lblThink = new Label(); lblPlayer = new Label(); lblComput = new Label(); lblPtotal = new Label(); lblPto = new Label(); lblCtotal = new Label(); lblPcard1 = new Label(); lblPcard2 = new Label(); lblPcard3 = new Label(); lblCcard1 = new Label(); lblCcard2 = new Label(); btnLicense = new Button(); btnWant = new Button(); btnShow = new Button(); btnReplay = new Button();
btnLicense.Location = new Point(150,100); btnLicense.AutoSize = true; btnLicense.Text = "显示发牌"; btnLicense.Click += new System.EventHandler(this.btnLicense_Click);
myLabel.Text = "This program was executed at:"; myLabel.AutoSize = true; myLabel.Left = 50; myLabel.Top = 20;
protected void btnShow_Click ( object sender, System.EventArgs e) { int pTotal = pHand[0].CardValue+pHand[1].CardValue+pHand[2].CardValue; int cTotal = cHand[0].CardValue+cHand[1].CardValue;
if (cTotal<17) { cHand[2] = mydeck.dealCard(); cTotal += cHand[2].CardValue; }
if (pTotal>21) { MessageBox.Show("You Busted!"); } else if (cTotal>21) { MessageBox.Show("You Win! Boss is Busted, It's " + cTotal.ToString() + " Its 3rd card is " + cHand[2].CardValue.ToString()); } else if (pTotal>cTotal) { MessageBox.Show("Congraduations! You Win! Boss is "+ cTotal.ToString() + " Its 3rd card is " + cHand[2].CardValue.ToString()); } else { MessageBox.Show("OH, Boss is " + cTotal.ToString() + ", You Lose!" + " Its 3rd card is " + cHand[2].CardValue.ToString()); } }
public static void Main() { int pCardCtr = 0; int pTotal = 0; int cTotal = 0;
CardGame frm = new CardGame(); Label lblShuffle = new Label(); Label lblDealing = new Label(); lblShuffle.Text = "正在洗牌...";
pTotal = 0; cTotal = 0; pCardCtr = 0;
for ( int ctr = 0; ctr<10; ctr++) { pHand[ctr].val = 0; pHand[ctr].suit = 0; } mydeck.shuffle();
是啊,用Application.Restart();窗口刷新那一下太明显了
就是扑克牌游戏,洗牌发牌都是单独的类,应该算是封装吧,不用管然后哪些算游戏环境初始化,窗体设置的那些label、button等算不算?是不是应该打下包,“重玩”button的事件程序里给调用一下?玩家和电脑手里的牌变量呢?应该怎么处理?
扑克牌类和发牌洗牌是书上弄好的,我只是给搞成窗体的,那么多label、button的处理代码感觉弄的很乱大家说的复位具体操作用什么语句实现?是用while循环吗?用goto行不行,我觉得while循环最晕了
这些就扯远了。一个游戏是一个class吧?哪怕你封装在控件中也是一个class。你的主程序重新new一个新的实例,不久重玩了嘛。
这个函数实现游戏逻辑的初始化,也就是把所有的东西回到刚开始游戏的状态
点击button时就调用initial();方法
不是,我只是把书上写的一个例子转成窗体形式的using System;
using System.Windows.Forms;
using System.Drawing;
// 窗体//扑克点数和花色public enum CardSuit
{
Zero_Error,
clubs,
diamonds,
hearts,
spades
}public enum CardValue
{
Zero_Error,
Ace,
two,
three,
four,
five,
six,
seven,
eight,
nine,
ten,
Jack,
Queen,
King
}// Structure:card
//===========================================struct card
{
public CardSuit suit;
public CardValue val;
public int CardValue
{
get
{
int retval;
if( (int) this.val>=10)
retval=10;
else
if ( (int) this.val==1)
retval=11;
else
retval=(int) this.val;
return retval;
}
}
public override string ToString()
{
return (string.Format("{0} of {1}",this.val.ToString("G"), this.suit.ToString("G")));
}
}// 扑克牌类
//==========================================class deck
{
public card[] cards=new card[53];
int next;
// deck()
//Constructor for setting up a regular deck
//==========================================
public deck()
{
next = 1;
cards[0].val = 0;
cards[0].suit = 0;
int currcard = 0;
for ( int suitctr = 1; suitctr<5;suitctr++)
{
for ( int valctr=1;valctr<14;valctr++)
{
currcard=(valctr)+((suitctr-1)*13);
cards[currcard].val=(CardValue)valctr;
cards[currcard].suit=(CardSuit) suitctr;
}
}
}
//shuffle()
//===========================================
public void shuffle()
{
Random rnd = new Random();
int sort1;
int sort2;
card tmpcard = new card();
for (int ctr = 0; ctr<100;ctr++)
{
sort1 = (int) ((rnd.NextDouble()*52) +1);
sort2 = (int) ((rnd.NextDouble()*52) +1);
tmpcard = this.cards[sort1];
this.cards[sort1]=this.cards[sort2];
this.cards[sort2] = tmpcard;
}
this.next = 1;
}
//dealCard()
// Returns next card in deck
// =============================================
public card dealCard()
{
if (next>52)
{
return (this.cards[0]);
}
else
{
return this.cards[next++];
}
}
}// 游戏主程序
//====================================================class CardGame : Form
{
static deck mydeck = new deck();
static card[] pHand = new card[10];
static card[] cHand = new card[10];
private Label lblThink;
private Label lblPlayer;
private Label lblComput;
private Label lblPtotal;
private Label lblPto;
private Label lblCtotal;
private Label lblPcard1;
private Label lblPcard2;
private Label lblPcard3;
private Label lblCcard1;
private Label lblCcard2;
private Button btnLicense;
private Button btnWant;
private Button btnShow;
private Button btnReplay;
public CardGame()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Text = "Hello haha!";
this.StartPosition = FormStartPosition.CenterScreen;
// Create the controls...
Label myDateLabel = new Label();
Label myLabel = new Label();
lblThink = new Label();
lblPlayer = new Label();
lblComput = new Label();
lblPtotal = new Label();
lblPto = new Label();
lblCtotal = new Label();
lblPcard1 = new Label();
lblPcard2 = new Label();
lblPcard3 = new Label();
lblCcard1 = new Label();
lblCcard2 = new Label();
btnLicense = new Button();
btnWant = new Button();
btnShow = new Button();
btnReplay = new Button();
btnLicense.Location = new Point(150,100);
btnLicense.AutoSize = true;
btnLicense.Text = "显示发牌";
btnLicense.Click += new System.EventHandler(this.btnLicense_Click);
myLabel.Text = "This program was executed at:";
myLabel.AutoSize = true;
myLabel.Left = 50;
myLabel.Top = 20;
lblPlayer.Text = "玩家手里:";
lblPlayer.AutoSize = true;
lblPlayer.Left = 50;
lblPlayer.Top = 180;
lblPcard1.Text = "第一张牌:";
lblPcard1.AutoSize = true;
lblPcard1.Left = 80;
lblPcard1.Top = 200;
lblPcard2.Text = "第二张牌:";
lblPcard2.AutoSize = true;
lblPcard2.Left = 80;
lblPcard2.Top = 220; lblPcard3.Text = "第三张牌:";
lblPcard3.AutoSize = true;
lblPcard3.Left = 300;
lblPcard3.Top = 220; lblComput.Text = "电脑手里:";
lblComput.AutoSize = true;
lblComput.Left = 50;
lblComput.Top = 250;
lblCcard1.Text = "第一张牌:";
lblCcard1.AutoSize = true;
lblCcard1.Left = 80;
lblCcard1.Top = 270;
lblCcard2.Text = "第二张牌:";
lblCcard2.AutoSize = true;
lblCcard2.Left = 80;
lblCcard2.Top = 290;
lblPtotal.Text = "玩家点数 ..";
lblPtotal.AutoSize = true;
lblPtotal.Left = 50;
lblPtotal.Top = 310;
lblCtotal.Text = "电脑点数 ..";
lblCtotal.AutoSize = true;
lblCtotal.Left = 50;
lblCtotal.Top = 330;
btnWant.Location = new Point(100,350);
btnWant.AutoSize = true;
btnWant.Text = "要牌";
btnWant.Click += new System.EventHandler(this.btnWant_Click);
btnShow.Location = new Point(350,350);
btnShow.AutoSize = true;
btnShow.Text = "开牌";
btnShow.Click += new System.EventHandler(this.btnShow_Click); btnReplay.Location = new Point(250,450);
btnReplay.AutoSize = true;
btnReplay.Text = "重玩";
btnReplay.Click += new System.EventHandler(this.btnReplay_Click);
lblPto.Text = "玩家最终点数 ..";
lblPto.AutoSize = true;
lblPto.Left = 50;
lblPto.Top = 400;
DateTime currDate = DateTime.Now;
myDateLabel.Text = currDate.ToString();
myDateLabel.AutoSize = true;
myDateLabel.Left = 50 + myLabel.PreferredWidth + 10;
myDateLabel.Top = 20;
this.Width = 500;
this.Height = 600;
//Add the control to the form... this.Controls.Add(btnLicense);
this.Controls.Add(btnWant);
this.Controls.Add(btnShow);
this.Controls.Add(btnReplay);
this.Controls.Add(myDateLabel);
this.Controls.Add(myLabel);
this.Controls.Add(lblPlayer);
this.Controls.Add(lblComput);
this.Controls.Add(lblPcard1);
this.Controls.Add(lblPcard2);
this.Controls.Add(lblPcard3);
this.Controls.Add(lblCcard1);
this.Controls.Add(lblCcard2);
this.Controls.Add(lblPtotal);
this.Controls.Add(lblPto);
this.Controls.Add(lblCtotal); }
protected void btnLicense_Click ( object sender, System.EventArgs e)
{ lblPcard1.Text += pHand[0].ToString();
lblPcard2.Text += pHand[1].ToString();
lblCcard1.Text += cHand[0].ToString();
lblCcard2.Text += cHand[1].ToString();
int pTotal = pHand[0].CardValue+pHand[1].CardValue;
lblPtotal.Text += pTotal.ToString();
int cTotal = cHand[0].CardValue;
lblCtotal.Text += cTotal.ToString();
//btnLicense.Visible = false;
} protected void btnReplay_Click ( object sender, System.EventArgs e)
{/* lblPcard1.Text = "第一张牌:";
lblPcard2.Text = "第二张牌:";
lblCcard1.Text = "第一张牌:";
lblCcard2.Text = "第二张牌:";
lblPtotal.Text = "玩家点数 ..";
lblCtotal.Text = "电脑点数 ..";
*/
Application.Restart();
}
protected void btnWant_Click ( object sender, System.EventArgs e)
{
pHand[2] = mydeck.dealCard();
int pTotal = pHand[0].CardValue+pHand[1].CardValue+pHand[2].CardValue;
lblPcard3.Text += pHand[2].ToString();
lblPto.Text += pTotal.ToString();
btnWant.Visible = false; }
protected void btnShow_Click ( object sender, System.EventArgs e)
{
int pTotal = pHand[0].CardValue+pHand[1].CardValue+pHand[2].CardValue;
int cTotal = cHand[0].CardValue+cHand[1].CardValue;
if (cTotal<17)
{
cHand[2] = mydeck.dealCard();
cTotal += cHand[2].CardValue;
}
if (pTotal>21)
{
MessageBox.Show("You Busted!");
}
else if (cTotal>21)
{
MessageBox.Show("You Win! Boss is Busted, It's " + cTotal.ToString() + " Its 3rd card is " + cHand[2].CardValue.ToString());
}
else if (pTotal>cTotal)
{
MessageBox.Show("Congraduations! You Win! Boss is "+ cTotal.ToString() + " Its 3rd card is " + cHand[2].CardValue.ToString());
}
else
{
MessageBox.Show("OH, Boss is " + cTotal.ToString() + ", You Lose!" + " Its 3rd card is " + cHand[2].CardValue.ToString());
}
}
public static void Main()
{
int pCardCtr = 0;
int pTotal = 0;
int cTotal = 0;
CardGame frm = new CardGame();
Label lblShuffle = new Label();
Label lblDealing = new Label();
lblShuffle.Text = "正在洗牌...";
pTotal = 0;
cTotal = 0;
pCardCtr = 0;
for ( int ctr = 0; ctr<10; ctr++)
{
pHand[ctr].val = 0;
pHand[ctr].suit = 0;
}
mydeck.shuffle();
lblShuffle.AutoSize = true;
lblShuffle.Left = 50;
lblShuffle.Top = 50;
lblDealing.Text = "正在发牌...";
pHand[0] = mydeck.dealCard();
cHand[0] = mydeck.dealCard();
pHand[1] = mydeck.dealCard();
cHand[1] = mydeck.dealCard(); do
{
pTotal += pHand[pCardCtr].CardValue;
pCardCtr++;
} while ((int)pHand[pCardCtr].val !=0);
cTotal = cHand[0].CardValue; lblDealing.AutoSize = true;
lblDealing.Left = 50;
lblDealing.Top = 70;
frm.Controls.Add(lblShuffle);
frm.Controls.Add(lblDealing);
Application.Run(frm);
}
}
看13楼源码,我的游戏主程序是不是不应该继承form类,窗体应该另写一个类?
看13楼源代码,动态的几个label(玩家手里的牌、电脑手里的牌、玩家点数、电脑点数等)是不是应该写进initial()?还有发牌洗牌也写进initial()?发牌洗牌是不是应该分别打包成方法,方便调用?