using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Collections;namespace _24Point{public partial class Form1 : Form{private int A, B, C, D;private int NumberA, NumberB, NumberC, NumberD;private int topCard;// 下载于#region 关于一副牌的生成private card[] Deck; //一副牌struct card{public int face; //牌面大小public int suit; //牌面花色public int count; //牌面点数public bool faceup;//牌面是否向上}private void GetDeck() //生成一副牌{Deck = new card[53];int i;int j;for (i = 0; i < 13; i++){for (j = 1; j <= 4; j++){Deck[j + i * 4].face = i + 1;Deck[j + i * 4].suit = j;if (i < 10){Deck[j + i * 4].count = i + 1;}else{Deck[j + i * 4].count = 10;}Deck[j + i * 4].faceup = false;}}}private void Shuffle() //洗牌{Random random = new Random((int)DateTime.Now.Ticks);card middleCard;int j, k;for (int i = 0; i < 1000; i++){j = (int)random.Next(1, 52);k = (int)random.Next(1, 52);middleCard = Deck[j];Deck[j] = Deck[k];Deck[k] = middleCard;}}private void btnStart_Click(object sender, EventArgs e) //开始游戏的时候发四张牌{lblInput.Text = "";GetDeck();Shuffle();topCard = 0;int imageNum;string path;//画第一张牌topCard = topCard = 1;pictureBox1.Visible = true;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox1.Image = Image.FromFile(path);NumberA = Convert.ToInt32(Deck[topCard].face);btnNumber1.Text = NumberA.ToString();//画第二张牌topCard = topCard + 1;pictureBox2.Visible = true;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox2.Image = Image.FromFile(path);NumberB = Convert.ToInt32(Deck[topCard].face);btnNumber2.Text = NumberB.ToString();//画第三张牌topCard = topCard + 1;pictureBox3.Visible = true;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox3.Image = Image.FromFile(path);NumberC = Convert.ToInt32(Deck[topCard].face);btnNumber3.Text = NumberC.ToString();//画第四张牌topCard = topCard + 1;pictureBox4.Visible = true;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox4.Image = Image.FromFile(path);NumberD = Convert.ToInt32(Deck[topCard].face);btnNumber4.Text = NumberD.ToString();btnStart.Visible = false;groupBox1.Visible = true;lblAnswer.Visible = true;}#endregionpublic Form1(){InitializeComponent();Initial();}private void Initial(){pictureBox1.Visible = false;pictureBox2.Visible = false;pictureBox3.Visible = false;pictureBox4.Visible = false;groupBox1.Visible = false;lblAnswer.Visible = false;lblResult.Visible = false;}private void Form1_Load(object sender, EventArgs e){}private void btnNext_Click(object sender, EventArgs e){btnAdd.Enabled = true;btnMinus.Enabled = true;btnDivide.Enabled = true;btnMulti.Enabled = true;btnNumber1.Enabled = true;btnNumber2.Enabled = true;btnNumber3.Enabled = true;btnNumber4.Enabled = true;btnDelete.Enabled = true;btnClear.Enabled = true;btnLeft.Enabled = true;btnRight.Enabled = true;btnEnter.Enabled = true;lblInput.Text = "";lblAnswer.Text = "答案";lblResult.Visible = false;int imageNum;string path;//画第一张牌if (topCard >= 52){topCard = 0;Shuffle();}topCard = topCard + 1;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox1.Image = Image.FromFile(path);NumberA = Convert.ToInt32(Deck[topCard].face);btnNumber1.Text = NumberA.ToString();//画第二张牌if (topCard >= 52){topCard = 0;}topCard = topCard + 1;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox2.Image = Image.FromFile(path);NumberB = Convert.ToInt32(Deck[topCard].face);btnNumber2.Text = NumberB.ToString();//画第三张牌if (topCard >= 52){topCard = 0;Shuffle();}topCard = topCard + 1;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox3.Image = Image.FromFile(path);NumberC = Convert.ToInt32(Deck[topCard].face);btnNumber3.Text = NumberC.ToString();//画第四张牌if (topCard >= 52){topCard = 0;Shuffle();}topCard = topCard + 1;imageNum = Deck[topCard].suit + (Deck[topCard].face - 1) * 4;path = Directory.GetCurrentDirectory() + @"\images\" + imageNum.ToString() + ".bmp";pictureBox4.Image = Image.FromFile(path);NumberD = Convert.ToInt32(Deck[topCard].face);btnNumber4.Text = NumberD.ToString();}public void ChangeLoc24(int i){//24种转换switch (i){case 1:A = NumberA;B = NumberB;C = NumberC;D = NumberD;case 2:A = NumberA;B = NumberB; D = NumberC;C = NumberD;break;case 3:A = NumberA; C = NumberB;B = NumberC; D = NumberD;break;case 4:A = NumberA; C = NumberB; D = NumberC;B = NumberD;break;case 5:A = NumberA; D = NumberB;B = NumberC;C = NumberD;break;case 6:A = NumberA; D = NumberB; C = NumberC;B = NumberD;break;case 7:B = NumberA; A = NumberB;C = NumberC;D = NumberD;break;case 8:B = NumberA; A = NumberB; D = NumberC;C = NumberD;break;case 9:B = NumberA;C = NumberB; A = NumberC;D = NumberD;break;case 10:B = NumberA;C = NumberB;D = NumberC; A = NumberD;break;case 11:B = NumberA; D = NumberB; A = NumberC;C = NumberD;break;case 12:B = NumberA; D = NumberB;C = NumberC; A = NumberD;break;case 13:C = NumberA; A = NumberB; B = NumberC;D = NumberD;break;case 14:C = NumberA; A = NumberB;D = NumberC; B = NumberD;break;case 15:C = NumberA; B = NumberB; A = NumberC;D = NumberD;break;C = NumberA; B = NumberB;D = NumberC; A = NumberD;break;case 17:C = NumberA;D = NumberB; A = NumberC; B = NumberD;break;case 18:C = NumberA;D = NumberB; B = NumberC; A = NumberD;break;case 19:D = NumberA; A = NumberB; B = NumberC; C = NumberD;break;case 20:D = NumberA; A = NumberB; C = NumberC; B = NumberD;break;case 21:D = NumberA; B = NumberB; A = NumberC; C = NumberD;break;case 22:D = NumberA; B = NumberB; C = NumberC; A = NumberD;break;case 23:D = NumberA; C = NumberB; A = NumberC; B = NumberD;break;case 24:D = NumberA; C = NumberB; B = NumberC; A = NumberD;break;}}private void btnAnswer_Click(object sender, EventArgs e){btnAdd.Enabled = false;btnMinus.Enabled = false;btnDivide.Enabled = false;btnMulti.Enabled = false;btnNumber1.Enabled = false;btnNumber2.Enabled = false;btnNumber3.Enabled = false;btnNumber4.Enabled = false;btnDelete.Enabled = false;btnClear.Enabled = false;btnLeft.Enabled = false;btnRight.Enabled = false;btnEnter.Enabled = false;lblAnswer.Text = "答案";#region 24种情况的遍历for (int i = 1; i <= 24; i++){ChangeLoc24(i);ArrayList first =new ArrayList();ArrayList firstStr=new ArrayList();first.Add(A.ToString());firstStr.Add("A");cal(ref first, ref firstStr, B,'B');cal(ref first, ref firstStr, C,'C');cal(ref first, ref firstStr, D,'D');for (int j = 0; j < first.Count; j++){if (Convert.ToInt32(Convert.ToDouble(first[j].ToString())) == 24){firstStr[j] = replaceString(firstStr[j].ToString(), 'A', A);firstStr[j] = replaceString(firstStr[j].ToString(), 'B', B);firstStr[j] = replaceString(firstStr[j].ToString(), 'C', C);firstStr[j] = replaceString(firstStr[j].ToString(), 'D', D);lblAnswer.Text = "答案:" + firstStr[j].ToString() + "=24;";return;}}}#endregionif (lblAnswer.Text == "答案"){lblAnswer.Text = "此题无解";}}private string replaceString(string str, char myChar, int num){int loc=str.IndexOf(myChar);string first = str.Substring(0, loc);str = first + Convert.ToInt16(num) + str.Substring(loc + 1);return str;}private void cal(ref ArrayList num,ref ArrayList numStr, int num2,char myChar){ArrayList newNum = new ArrayList();ArrayList newNumStr = new ArrayList();int temp;for (int i = 0; i < num.Count; i++){int num1 = Convert.ToInt32(num[i].ToString());//加法的情况temp = num1 + num2;newNum.Add(temp.ToString());newNumStr.Add(numStr[i].ToString() + "+" + myChar);//减法的情况if (num1 > num2){temp = num1 - num2;newNum.Add(temp.ToString());newNumStr.Add(numStr[i].ToString() + "-" + myChar);}else{temp = num2 - num1;newNum.Add(temp.ToString());if (numStr[i].ToString().IndexOf('+') != -1 || numStr[i].ToString().IndexOf('-')!=-1){newNumStr.Add(myChar + "-" + "(" + numStr[i].ToString() + ")");}elsenewNumStr.Add(myChar + "-" + numStr[i].ToString());}temp = num1 * num2;newNum.Add(temp.ToString());if (numStr[i].ToString().IndexOf("+") == -1 && numStr[i].ToString().IndexOf("-") == -1){newNumStr.Add(numStr[i].ToString() + "*" + myChar);}else{newNumStr.Add("(" + numStr[i].ToString() + ")" + "*" + myChar);}#region 除法的情况if (num1 > num2){if (num2 != 0 && num1 % num2 == 0){temp = num1 / num2;newNum.Add(temp.ToString());if (numStr[i].ToString().IndexOf("+") == -1 && numStr[i].ToString().IndexOf("-") == -1){newNumStr.Add(numStr[i].ToString() + "/" + myChar);}else{newNumStr.Add("(" + numStr[i].ToString() + ")" + "/" + myChar);}}}else{if (num1 != 0 && num2 % num1 == 0){temp = num2 / num1;newNum.Add(temp.ToString());if (numStr[i].ToString().IndexOf("+") == -1 && numStr[i].ToString().IndexOf("-") == -1){newNumStr.Add(myChar + "/" + numStr[i].ToString());}else{newNumStr.Add(myChar + "/" + "(" + numStr[i].ToString() + ")");}}}#endregion}num = newNum;numStr = newNumStr;}private void btnEnter_Click(object sender, EventArgs e){if (CheckForNumbers(lblInput.Text.ToString().Trim())){int result=Deal(lblInput.Text.ToString().Trim());lblResult.Visible = true;if (result == 24){lblResult.Text = "<<---恭喜您,答对了!--->>";}else{lblResult.Text = "<<--抱歉,您的回答有误!-->>";}}}private void btnNumber1_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnNumber1.Text.ToString().Trim(); }private void btnNumber2_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnNumber2.Text.ToString().Trim(); }private void btnNumber3_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnNumber3.Text.ToString().Trim(); }private void btnNumber4_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnNumber4.Text.ToString().Trim(); }private void btnAdd_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnAdd.Text.ToString().Trim();}private void btnMinus_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnMinus.Text.ToString().Trim();}private void btnMulti_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnMulti.Text.ToString().Trim();}private void btnDivide_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnDivide.Text.ToString().Trim();}private void btnLeft_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnLeft.Text.ToString().Trim();}private void btnRight_Click(object sender, EventArgs e){lblInput.Text = lblInput.Text + btnRight.Text.ToString().Trim();}private void btnDelete_Click(object sender, EventArgs e){string input = lblInput.Text.ToString().Trim();lblInput.Text = input.Substring(0, input.Length - 1);}private void btnClear_Click(object sender, EventArgs e){lblInput.Text = "";}public int Deal(string input)//处理谓词表达式中的括号{int result = 0;while (input.IndexOf(')') != -1){int rightLoc = input.IndexOf(')');string temp = input.Substring(0, rightLoc);int leftLoc = stIndexOf('(');string first = input.Substring(0, leftLoc);string middle = input.Substring(leftLoc + 1, rightLoc - leftLoc - 1);string last = input.Substring(rightLoc + 1);input = first + Formular(middle).ToString() + last;}result = Formular(input);return result;}protected int Formular(string Inputs)//最简式运算{int Len = Inputs.Length;ArrayList OpeLoc = new ArrayList();//记录操作符位置ArrayList Ope = new ArrayList();//记录操作符int i;for (i = 0; i < Len; i++){if (IsOperator(Inputs[i]))//获取算符组信息{OpeLoc.Add(i);Ope.Add(Inputs[i]);}}if (OpeLoc.Count == 0) return int.Parse(Inputs);//处理无算符的情况RebuildOperator(ref OpeLoc, ref Ope);//算符重组,区分负号和减号if (!CheckFunction(OpeLoc, Len)) return 0;//判断算符组是否合法ArrayList Val = new ArrayList();//记录数值内容int j = 0;for (i = 0; i < OpeLoc.Count; i++){V al.Add(int.Parse(Inputs.Substring(j, Convert.ToInt32(OpeLoc[i]) - j)));j = Convert.ToInt32(OpeLoc[i]) + 1;}Val.Add(int.Parse(Inputs.Substring(j, Len - j)));//处理最后一个数据的录入return Calculate(Ope, Val);}protected bool IsOperator(char chr)//判断一个符号是否是基本算符{if (chr == '+' | chr == '-' | chr == '*' | chr == '/')return true;//判断是否是四则混合运算算符else return false;}protected void RebuildOperator(ref ArrayList OpeLoc, ref ArrayList Ope)//对负号的处理和重构{ArrayList DelItem = new ArrayList();if (Convert.ToInt32(OpeLoc[0].ToString()) == 0 & Convert.ToChar(Ope[0]) == '-')//判断第一个符号是否是负号{DelItem.Add(0);}int i;for (i = 1; i < OpeLoc.Count; i++){//判断是否有相邻的算符且后一个是负号if (Convert.ToChar(Ope[i]) == '-' && Convert.ToChar(Ope[i - 1]) != '-' && (Convert.ToInt32(OpeLoc[i]) - Convert.ToInt32(OpeLoc[i - 1])) == 1){DelItem.Add(i);}}for (i = DelItem.Count - 1; i >= 0; i--)//将负号和减号分开处理{Ope.RemoveAt(Convert.ToInt32(DelItem[i]));OpeLoc.RemoveAt(Convert.ToInt32(DelItem[i]));}}protected int Calculate(ArrayList Ope, ArrayList Values)//处理四则混合运算等基础运算{int i;for (i = 0; i < Ope.Count; i++)//处理乘法、除法{switch (Convert.ToChar(Ope[i])){case '*':Values[i] = Convert.ToInt32(Values[i]) * Convert.ToInt32(Values[i+1]);Values.RemoveAt(i + 1);Ope.RemoveAt(i);i--;break;case '/':Values[i] = Convert.ToInt32(Values[i]) / Convert.ToInt32(Values[i + 1]);Values.RemoveAt(i + 1);Ope.RemoveAt(i);i--;break;}}for (i = 0; i < Ope.Count; i++)//处理加法和减法{switch ((char)Ope[i]){case '+':Values[i] = Convert.ToInt32(Values[i]) + Convert.ToInt32(Values[i + 1]);Values.RemoveAt(i + 1);Ope.RemoveAt(i);i--;break;case '-':Values[i] = Convert.ToInt32(Values[i]) - Convert.ToInt32(Values[i+1]);;Values.RemoveAt(i + 1);Ope.RemoveAt(i);i--;break;}}return Convert.ToInt32(Values[0].ToString());}protected bool CheckFunction(ArrayList Loc, int Len)//判断算符组是否合法{if (Convert.ToInt32(Loc[0]) == 0)return false;int i;for (i = 1; i < Loc.Count; i++){if (Convert.ToInt32(Loc[i]) - Convert.ToInt32(Loc[i - 1]) == 1)return false;}if (Convert.ToInt32(Loc[Loc.Count - 1]) == Len - 1)return false;return true;}protected bool CheckForNumbers(string input){bool result = true;string[] str = input.Split(new char[] { '+', '-', '/', '*', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);if (str.Length != 4){MessageBox.Show("抱歉,输入有误!请重新输入");result = false;return result;}int[] numbers=new int[4];int[] nums = { NumberA,NumberB, NumberC, NumberD };Array.Sort(nums);for (int i = 0; i < 4; i++){numbers[i] = Convert.ToInt32(str[i]);}Array.Sort(numbers);for (int i = 0; i < 4; i++){if (nums[i] != numbers[i]){result = false;MessageBox.Show("抱歉,每张牌只能使用一次!");return result;}}return result;}}}。