/*
* 源代码
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 德州扑克
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 是否重复,应用在发牌过程
/// 后来没有用上
/// </summary>
/// <param name="ir">牌码</param>
/// <returns></returns>
bool IsRepeat(string[] ir)
{
for (int i = 0; i < ir.Length - 1; i++)
{
for (int j = i + 1; j < ir.Length; j++)
{
if (i == j) continue;
if (ir[i] == ir[j]) return false;
}
}
return true;
}
/// <summary>
/// 牌图设置
/// </summary>
void evaluate()
{
ptb1.ImageLocation = path + pic1 + extend;
ptb2.ImageLocation = path + pic2 + extend;
ptb3.ImageLocation = path + pic3 + extend;
ptb4.ImageLocation = path + pic4 + extend;
ptb5.ImageLocation = path + pic5 + extend;
}
/// <summary>
/// 发牌函数库,你能看懂么,看不懂不怪你,我都觉得太乱了,水平太菜了,相信你也知道如何优化了。
/// </summary>
void rand()
{
int s = unchecked((int)DateTime.Now.Ticks);//unchecked知道干什么吗?不要伪随机数
string[] temp = new string[5];
string[] pics = new string[5];
//生成第1张随机,首先判断是否已经翻开
if (ptb1.ImageLocation == path + "bg" + extend)
{
Ti += 1050;
pics[0] = pic1;
Random ra = new Random(++Ti + (++s));
pic1 = ra.Next(1, 5).ToString();//第一张牌获取随机数
while (true)
{
start:
;
Ti += 3;
ra = new Random(++Ti + (++s));
temps[0] = ra.Next(1, 14).ToString();//任取一张牌
switch (temps[0])
{
case "10": temp[0] = "t"; break;
case "11": temp[0] = "j"; break;
case "12": temp[0] = "q"; break;
case "13": temp[0] = "k"; break;
default: temp[0] = temps[0]; break;
}
pic1 = pic1[0].ToString() + temp[0];//第一张预生成
//重复性判断,比较弱智
if (temps[0] == temps[1])
{
if (pic1 == pic2)
{
goto start;
}
}
if (temps[0] == temps[2])
{
if (pic1 == pic3)
{
goto start;
}
}
if (temps[0] == temps[3])
{
if (pic1 == pic4)
{
goto start;
}
}
if (temps[0] == temps[4])
{
if (pic1 == pic5)
{
goto start;
}
}
if (pics[0] != " " && pics[1] != " " && pics[2] != " " && pics[3] != " " && pics[4] != " ")
{
if (pics[0] == pics[1] || pics[0] == pics[2] || pics[0] == pics[3] || pics[0] == pics[4])
{
goto start;//如果都不为空,且只要一个相同就重复发牌。goto我一定会扔掉
}
}
break;
}
}
//生成第2张随机
if (ptb2.ImageLocation == path + "bg" + extend)
{
Ti += 850;
pics[1] = pic2;
Random ra = new Random(++Ti + (++s));
pic2 = ra.Next(1, 5).ToString();
while (true)
{
start:
;
Ti += 570;
ra = new Random(++Ti + (++s));
temps[1] = ra.Next(1, 14).ToString();
switch (temps[1])
{
case "10": temp[1] = "t"; break;
case "11": temp[1] = "j"; break;
case "12": temp[1] = "q"; break;
case "13": temp[1] = "k"; break;
default: temp[1] = temps[1]; break;
}
pic2 = pic2[0].ToString() + temp[1];
if (temps[1] == temps[0])
{
if (pic2 == pic1)
{
goto start;
}
}
if (temps[1] == temps[2])
{
if (pic2 == pic3)
{
goto start;
}
}
if (temps[1] == temps[3])
{
if (pic2 == pic4)
{
goto start;
}
}
if (temps[1] == temps[4])
{
if (pic2 == pic5)
{
goto start;
}
}
if (pics[0] != " " && pics[1] != " " && pics[2] != " " && pics[3] != " " && pics[4] != " ")
{
if (pics[1] == pics[0] || pics[1] == pics[2] || pics[1] == pics[3] || pics[1] == pics[4])
{
goto start;
}
}
break;
}
}
//生成第3张随机
if (ptb3.ImageLocation == path + "bg" + extend)
{
Ti += 790;
pics[2] = pic3;
Random ra = new Random(++Ti + (++s));
pic3 = ra.Next(1, 5).ToString();//获取牌型
while (true)
{
start:
;
Ti += 210;
ra = new Random(++Ti + (++s));
temps[2] = ra.Next(1, 14).ToString();//获取牌号
switch (temps[2])
{
case "10": temp[2] = "t"; break;
case "11": temp[2] = "j"; break;
case "12": temp[2] = "q"; break;
case "13": temp[2] = "k"; break;
default: temp[2] = temps[2]; break;
}
pic3 = pic3[0].ToString() + temp[2];
if (temps[2] == temps[0])
{
if (pic3 == pic1)
{
goto start;
}
}
if (temps[2] == temps[1])
{
if (pic3 == pic2)
{
goto start;
}
}
if (temps[2] == temps[3])
{
if (pic3 == pic4)
{
goto start;
}
}
if (temps[2] == temps[4])
{
if (pic3 == pic5)
{
goto start;
}
}
if (pics[0] != " " && pics[1] != " " && pics[2] != " " && pics[3] != " " && pics[4] != " ")
{
if (pics[2] == pics[1] || pics[2] == pics[0] || pics[2] == pics[3] || pics[2] == pics[4])
{
goto start;
}
}
break;
}
}
//生成第4张随机
if (ptb4.ImageLocation == path + "bg" + extend)
{
Ti += 530;
pics[3] = pic4;
Random ra = new Random(++Ti + (++s));
pic4 = ra.Next(1, 5).ToString();
while (true)
{
start:
;
Ti += 150;
ra = new Random(++Ti + (++s));
temps[3] = ra.Next(1, 14).ToString();
switch (temps[3])
{
case "10": temp[3] = "t"; break;
case "11": temp[3] = "j"; break;
case "12": temp[3] = "q"; break;
case "13": temp[3] = "k"; break;
default: temp[3] = temps[3]; break;
}
pic4 = pic4[0].ToString() + temp[3];
if (temps[3] == temps[0])
{
if (pic4 == pic1)
{
goto start;
}
}
if (temps[3] == temps[1])
{
if (pic4 == pic2)
{
goto start;
}
}
if (temps[3] == temps[2])
{
if (pic4 == pic3)
{
goto start;
}
}
if (temps[3] == temps[4])
{
if (pic4 == pic5)
{
goto start;
}
}
if (pics[0] != " " && pics[1] != " " && pics[2] != " " && pics[3] != " " && pics[4] != " ")
{
if (pics[3] == pics[1] || pics[3] == pics[2] || pics[3] == pics[0] || pics[3] == pics[4])
{
goto start;
}
}
break;
}
}
//生成第5张随机
if (ptb5.ImageLocation == path + "bg" + extend)
{
Ti += 270;
pics[4] = pic5;
Random ra = new Random(++Ti + (++s));
pic5 = ra.Next(1, 5).ToString();
while (true)
{
start:
;
Ti += 0;
ra = new Random(++Ti + (++s));
temps[4] = ra.Next(1, 14).ToString();
switch (temps[4])
{
case "10": temp[4] = "t"; break;
case "11": temp[4] = "j"; break;
case "12": temp[4] = "q"; break;
case "13": temp[4] = "k"; break;
default: temp[4] = temps[4]; break;
}
pic5 = pic5[0].ToString() + temp[4];
if (temps[4] == temps[0])
{
if (pic5 == pic1)
{
goto start;
}
}
if (temps[4] == temps[1])
{
if (pic5 == pic2)
{
goto start;
}
}
if (temps[4] == temps[2])
{
if (pic5 == pic3)
{
goto start;
}
}
if (temps[4] == temps[3])
{
if (pic5 == pic4)
{
goto start;
}
}
if (pics[0] != " " && pics[1] != " " && pics[2] != " " && pics[3] != " " && pics[4] != " ")
{
if (pics[4] == pics[1] || pics[4] == pics[2] || pics[4] == pics[3] || pics[4] == pics[0])
{
goto start;
}
}
break;
}
}
}
//一大堆的变量,看到了吧,根本没有高内聚,不封装,不灵活,改个业务你试试,累死你,新手一般容易犯这个毛病,手比脑袋快。
string df = ""; //得分
int Ti = 50; //种子
int time = 0;//已玩次数
string[] temps = new string[5];
int marking = 10000; //初始化初时分数
string extend = ".jpg";//扩展名
string path = "image//";//路径
static string pic1 = " ", pic2 = " ", pic3 = " ", pic4 = " ", pic5 = " ";//5变量,存储图片的代码
static bool p1 = true, p2 = true, p3 = true, p4 = true, p5 = true;
#region 界面操作事件处理
private void Form1_Load(object sender, EventArgs e)
{
//初始加载
txtmarking.Text = marking.ToString(); //初始化得分
lbllevel.Text = "局数:" + (++time).ToString() + "局"; //初始化局数
ptb1.ImageLocation = path + "bg" + extend;
ptb2.ImageLocation = path + "bg" + extend;
ptb3.ImageLocation = path + "bg" + extend;
ptb4.ImageLocation = path + "bg" + extend;
ptb5.ImageLocation = path + "bg" + extend;
rand();
p1 = p2 = p3 = p4 = p5 = false;
}
/// <summary>
/// 点击第一张牌
///代码重用啊,太杯具了
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ptb1_Click(object sender, EventArgs e)
{
if (ptb1.ImageLocation != path + pic1 + extend)
{
ptb1.ImageLocation = path + pic1 + extend;
p1 = false;
}
else
{
ptb1.ImageLocation = path + "bg" + extend;
p1 = true;
}
}
/// <summary>
/// 点击第二张牌
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ptb2_Click(object sender, EventArgs e)
{
if (ptb2.ImageLocation != path + pic2 + extend)
{
ptb2.ImageLocation = path + pic2 + extend;
p2 = false;
}
else
{
ptb2.ImageLocation = path + "bg" + extend;
p2 = true;
}
}
/// <summary>
/// 点击第三张牌
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ptb3_Click(object sender, EventArgs e)
{
if (ptb3.ImageLocation != path + pic3 + extend)
{
ptb3.ImageLocation = path + pic3 + extend;
p3 = false;
}
else
{
ptb3.ImageLocation = path + "bg" + extend;
p3 = true;
}
}
/// <summary>
/// 点击第四张牌
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ptb4_Click(object sender, EventArgs e)
{
if (ptb4.ImageLocation != path + pic4 + extend)
{
ptb4.ImageLocation = path + pic4 + extend;
p4 = false;
}
else
{
ptb4.ImageLocation = path + "bg" + extend;
p4 = true;
}
}
/// <summary>
/// 点击第五张牌
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ptb5_Click(object sender, EventArgs e)
{
if (ptb5.ImageLocation != path + pic5 + extend)
{
ptb5.ImageLocation = path + pic5 + extend;
p5 = false;
}
else
{
ptb5.ImageLocation = path + "bg" + extend;
p5 = true;
}
}
/// <summary>
/// 下一轮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnnext_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(txtmarking.Text) <= 0)
{
lblresult.Text = "抱歉,你已经没有金币了!";
btnnext.Enabled = false;
}
else
{
btnok.Enabled = true;
txtmultiple.Text = "";
txtmoney.Text = "";
p1 = p2 = p3 = p4 = p5 = true;
ptb1.ImageLocation = path + "bg" + extend;
ptb2.ImageLocation = path + "bg" + extend;
ptb3.ImageLocation = path + "bg" + extend;
ptb4.ImageLocation = path + "bg" + extend;
ptb5.ImageLocation = path + "bg" + extend;
rand();
p1 = p2 = p3 = p4 = p5 = false;
lbllevel.Text = "局数:" + time++.ToString() + "局";
lblresult.Text = "";
btnnext.Enabled = false;
nummoney.Enabled = true;
}
}
/// <summary>
/// 换牌
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnend_Click(object sender, EventArgs e)
{
rand();
evaluate();
ptb1.Enabled = false;
ptb2.Enabled = false;
ptb3.Enabled = false;
ptb4.Enabled = false;
ptb5.Enabled = false;
btnend.Enabled = false;
System.Threading.Thread.Sleep(100);
//首先获取图片序列,准备开牌了,格式化数据输入
string s = "";
// s = pic1[1].ToString() + "," + pic2[1].ToString() + "," + pic3[1].ToString() + "," + pic4[1].ToString() + "," + pic5[1].ToString();
s = s + "," + parserType(pic1[0].ToString()) + parserValue(pic1[1].ToString());
s = s + "," + parserType(pic2[0].ToString()) + parserValue(pic2[1].ToString());
s = s + "," + parserType(pic3[0].ToString()) + parserValue(pic3[1].ToString());
s = s + "," + parserType(pic4[0].ToString()) + parserValue(pic4[1].ToString());
s = s + "," + parserType(pic5[0].ToString()) + parserValue(pic5[1].ToString());
s = "P" + s;
//开牌
Fwin(wins(s), s);
btnnext.Enabled = true;
}
/// <summary>
/// 开牌,一组五张牌,随机分,不能有重复
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnok_Click(object sender, EventArgs e)
{
if (nummoney.Value > Convert.ToInt32(txtmarking.Text))
{
nummoney.Value = Convert.ToInt32(txtmarking.Text);
}
btnok.Enabled = false;
btnend.Enabled = true;
nummoney.Enabled = false;
evaluate();
ptb1.Enabled = true;
ptb2.Enabled = true;
ptb3.Enabled = true;
ptb4.Enabled = true;
ptb5.Enabled = true;
}
/// <summary>
/// 排名算法,专门为合作推广部使用;
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSort_Click(object sender, EventArgs e)
{
frmSort fr = new frmSort();
fr.ShowDialog();
}
#endregion
/// <summary>
/// 多态wins,对于输入的五张牌进行类型的判断
/// 和上面的函数对比,封装类算法,调用新的类库,看看这是如何封装的,程序员如果没有抽象概念,就不会理解封装,也就不能体会到重用的妙处
/// </summary>
/// <param name="s">五张牌序列</param>
/// <returns>类型</returns>
string wins(string s)
{
string ret = "";
ret = (Lib.DeZhouPuKe.checkPaiType(s)).ToString();
return ret;
}
/// <summary>
/// 翻牌之后的处理,投注以及界面元素的逻辑处理,规则可以继续抽象出来,形成接口,再形成服务,规则可以配置,规则引擎就是这么来的,根本没有那么玄乎。
/// </summary>
/// <param name="win">牌的类型</param>
/// <param name="sLabel">辅助显示文字</param>
void Fwin(string win,string sLabel)
{
if (win != "高牌")
{
lblresult.Text = "恭喜你,本次牌为:" + win;
if (win == "皇家同花顺")
{
txtmultiple.Text = "90";//投注的90倍奖励
txtmoney.Text = (nummoney.Value * 90).ToString();
df = "+" + txtmoney.Text;
txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
}
if (win == "同花顺")
{
txtmultiple.Text = "80";//投注的80倍奖励
txtmoney.Text = (nummoney.Value * 80).ToString();
df = "+" + txtmoney.Text;
txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
}
if (win == "四条")
{
txtmultiple.Text = "70";//投注的70倍奖励
txtmoney.Text=(nummoney.Value*70).ToString();
df = "+" + txtmoney.Text;
txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
}
if (win == "葫芦")
{
txtmultiple.Text = "60";//投注的60倍奖励
txtmoney.Text = (nummoney.Value * 60).ToString();
df = "+" + txtmoney.Text;
txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
}
if (win == "同花")
{
txtmultiple.Text = "50";//投注的50倍奖励
txtmoney.Text = (nummoney.Value * 50).ToString();
df = "+" + txtmoney.Text;
txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
}
if (win == "顺子")
{
txtmultiple.Text = "40";//投注的40倍奖励
txtmoney.Text = (nummoney.Value * 40).ToString();
df = "+" + txtmoney.Text;
txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
}
if (win == "三条")
{
txtmultiple.Text = "30";//投注的30倍奖励
txtmoney.Text = (nummoney.Value * 30).ToString();
df = "+" + txtmoney.Text;
txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
}
if (win == "两对")
{
txtmultiple.Text = "20";//投注的20倍奖励
txtmoney.Text = (nummoney.Value * 20).ToString();
df = "+" + txtmoney.Text;
txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
}
if (win == "一对")
{
txtmultiple.Text = "10";//投注的10倍奖励
txtmoney.Text = (nummoney.Value * 10).ToString();
df = "+" + txtmoney.Text;
txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
}
}
else
{
//如果是高牌
lblresult.Text = win;
txtmultiple.Text = "0";//没有奖励,并扣除投注数量的50倍
txtmoney.Text = (nummoney.Value * 50).ToString();
txtmoney.Text = "-" + nummoney.Value.ToString();
df = txtmoney.Text;
txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
}
lblmarking.Text = "上局得分:" + df;
}
/// <summary>
/// 牌型的转换,用来在计算翻牌之后的数据解析处理,小工具一个
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private string parserType(string s)
{
string ret = "";
if (s.Equals("1"))
{
ret = "a";
}
else if (s.Equals("2"))
{
ret = "b";
}
else if (s.Equals("3"))
{
ret = "c";
}
else if (s.Equals("4"))
{
ret = "d";
}
else
{
ret = "m";
}
return ret;
}
/// <summary>
/// 牌点的转换,用来在计算翻牌之后的数据解析处理,小工具一个
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private string parserValue(string s)
{
string ret = "";
if (s.Equals("j"))
{
ret = "11";
}
else if (s.Equals("q"))
{
ret = "12";
}
else if (s.Equals("k"))
{
ret = "13";
}
else if (s.Equals("t"))
{
ret = "10";
}
else
{
ret = s;
}
return ret;
}
}
}
评价,基本功能实现了,但是代码一看就是堆上去的,效率很差,关键是没有面向对象的思想,看不到软件复用的痕迹,除了,除了,除了。。。那个函数:Lib.DeZhouPuKe.checkPaiType(s)
参看系列3 我的心哪