程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成的24點游戲實例詳解

C#完成的24點游戲實例詳解

編輯:C#入門知識

C#完成的24點游戲實例詳解。本站提示廣大學習愛好者:(C#完成的24點游戲實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成的24點游戲實例詳解正文


本文實例剖析了C#完成的24點游戲。分享給年夜家供年夜家參考。詳細以下:

1. 24點游戲規矩及算法

規矩:給出4個天然數,找出可以或許求出24的四則運算式,請求數字不克不及反復應用
剖析:

本算法是一種暴力圖解法:

給出隨意率性兩個數字,可以停止6種四則運算,求出最多6個值。以數字a和b為例,有:

加(a+b)、減(a-b)、被減(b-a)、乘以(a*b)、除以(a/b)和除(b/a)
abcd合計四個數,假如次序固定,則有5種盤算次序(★代表下面6種四則運算中的一種):

((a★b)★c)★d、(a★b)★(c★d)、(a★(b★c))★d、a★((b★c)★d)、a★(b★(c★d))

給出的四個數字,n1n2n3n4,有4*3*2=24種全分列的辦法

2. 相干函數

/// <summary>
/// 斷定abcd四個數停止隨意率性四則運算後能不克不及得出24,每一個數字只能用一次
/// </summary>
/// <param name="a">數字a</param>
/// <param name="b">數字b</param>
/// <param name="c">數字c</param>
/// <param name="d">數字d</param>
/// <param name="expression"></param>
/// <returns></returns>
private static bool Try24(int a, int b, int c, int d, ref string expression)
{
 //a字頭
 if (TryEach(a, b, c, d, ref expression)) return true;
 if (TryEach(a, b, d, c, ref expression)) return true;
 if (TryEach(a, c, b, d, ref expression)) return true;
 if (TryEach(a, c, d, b, ref expression)) return true;
 if (TryEach(a, d, b, c, ref expression)) return true;
 if (TryEach(a, d, c, b, ref expression)) return true;
 //b字頭
 if (TryEach(b, a, c, d, ref expression)) return true;
 if (TryEach(b, a, d, c, ref expression)) return true;
 if (TryEach(b, c, a, d, ref expression)) return true;
 if (TryEach(b, c, d, a, ref expression)) return true;
 if (TryEach(b, d, a, c, ref expression)) return true;
 if (TryEach(b, d, c, a, ref expression)) return true;
 //c字頭
 if (TryEach(c, a, b, d, ref expression)) return true;
 if (TryEach(c, a, d, b, ref expression)) return true;
 if (TryEach(c, b, a, c, ref expression)) return true;
 if (TryEach(c, b, c, a, ref expression)) return true;
 if (TryEach(c, d, a, b, ref expression)) return true;
 if (TryEach(c, d, b, a, ref expression)) return true;
 //d字頭
 if (TryEach(d, a, b, c, ref expression)) return true;
 if (TryEach(d, a, c, b, ref expression)) return true;
 if (TryEach(d, b, a, c, ref expression)) return true;
 if (TryEach(d, b, c, a, ref expression)) return true;
 if (TryEach(d, c, a, b, ref expression)) return true;
 if (TryEach(d, c, b, a, ref expression)) return true;
 return false;
}
/// <summary>
/// 斷定指定次序的四個數abcd停止隨意率性四則運算後能不克不及得出24,每一個數字只能用一次
/// </summary>
/// <param name="a">數字1</param>
/// <param name="b">數字2</param>
/// <param name="c">數字3</param>
/// <param name="d">數字4</param>
/// <param name="expression"></param>
/// <returns></returns>
private static bool TryEach(int a, int b, int c, int d, ref string expression)
{
 expression = "";
 //兩個數可以做6種運算:加、減、被減、乘以、除以、除
 //四個數共可以停止6*6*6=216種分歧順序的四則運算
 //初始化數組
 for (int i = 0; i < 6 * 6 * 6; i++)
 {
  //a與b間的運算符:i / 36
  //b與c間的運算符:i % 36 / 6
  //c與d間的運算符:i % 6
  //1.運算次序:a和b,再和c,再和d
  {
   string expression1 = "", expression2 = "";
   int temp1 = ResultOf(a, b, i / 36, ref expression1);
   int temp2 = ResultOf(temp1, c, i % 36 / 6, ref expression2, expression1);
   int result = ResultOf(temp2, d, i % 6, ref expression, expression2);
   if (result == 24) return true;
  }
  //2.運算次序:a和b,c和d,後面部門和前面部門
  {
   string expression1 = "", expression2 = "";
   int temp1 = ResultOf(a, b, i / 36, ref expression1);
   int temp2 = ResultOf(c, d, i % 6, ref expression2);
   int result = ResultOf(temp1, temp2, i % 36 / 6,
    ref expression, expression1, expression2);
   if (result == 24) return true;
  }
  //3.運算次序:b和c運算,再與a運算,再與d運算
  {
   string expression1 = "", expression2 = "";
   int temp1 = ResultOf(b, c, i % 36 / 6, ref expression1);
   int temp2 = ResultOf(a, temp1, i / 36, ref expression2, "", expression1);
   int result = ResultOf(temp2, d, i % 6, ref expression, expression2);
   if (result == 24) return true;
  }
  //4.運算次序:b和c運算,再與d運算,再與a運算
  {
   string expression1 = "", expression2 = "";
   int temp1 = ResultOf(b, c, i % 36 / 6, ref expression1);
   int temp2 = ResultOf(temp1, d, i % 6, ref expression2, expression1);
   int result = ResultOf(a, temp2, i / 36, ref expression, "", expression2);
   if (result == 24) return true;
  }
  //5.運算次序:c和d運算,再和b運算,再和a運算
  {
   string expression1 = "", expression2 = "";
   int temp1 = ResultOf(c, d, i % 6, ref expression1);
   int temp2 = ResultOf(b, temp1, i % 36 / 6, ref expression2, "", expression1);
   int result = ResultOf(a, temp2, i / 36, ref expression, "", expression2);
   if (result == 24) return true;
  }
 }
 expression = "Abandoned";
 return false;
}
/// <summary>
/// 求兩數停止某一四則運算後的成果
/// </summary>
/// <param name="x">數字1</param>
/// <param name="y">數字2</param>
/// <param name="method">(0-5分離代表:加、減、被減、乘以、除以、除)</param>
/// <param name="expression">前往的表達式</param>
/// <param name="expressionLeft">數字1表達式</param>
/// <param name="expressionRight">數字2表達式</param>
/// <returns></returns>
private static int ResultOf(int x, int y, int method,
 ref string expression, string expressionLeft = "", string expressionRight = "")
{
 //閣下表達式之前被剖斷為有效則不盤算,除數為0時不盤算
 if (expressionLeft == "Abandoned" || expressionRight == "Abandoned" ||
  (x == 0 && method == 5) || (y == 0 && method == 4))
 {
  expression = "Abandoned";
  return -1;
 }
 int result = 0;
 switch (method)
 {
  case 0:
   {
    //加
    result = x + y;
    expression = string.Format("{0}+{1}",
     expressionLeft == "" ? x.ToString() : expressionLeft,
     expressionRight == "" ? y.ToString() : expressionRight);
   }
   break;
  case 1:
   {
    //減
    result = x - y;
    expression = string.Format("{0}-{1}",
     expressionLeft == "" ? x.ToString() : expressionLeft,
     expressionRight == "" ? y.ToString() : expressionRight);
   }
   break;
  case 2:
   {
    //被減
    result = y - x;
    expression = string.Format("{1}-{0}",
     expressionLeft == "" ? x.ToString() : expressionLeft,
     expressionRight == "" ? y.ToString() : expressionRight);
   }
   break;
  case 3:
   {
    //乘以
    result = x * y;
    expression = string.Format("({0})*({1})",
     expressionLeft == "" ? x.ToString() : expressionLeft,
     expressionRight == "" ? y.ToString() : expressionRight);
   }
   break;
  case 4:
   {
    //除以
    if (x % y == 0)
    {
     result = x / y;
     expression = string.Format("({0})/({1})",
      expressionLeft == "" ? x.ToString() : expressionLeft,
      expressionRight == "" ? y.ToString() : expressionRight);
    }
    else
    {
     expression = "Abandoned";
    }
   }
   break;
  case 5:
   {
    //除
    if (y % x == 0)
    {
     result = y / x;
     expression = string.Format("({1})/({0})",
      expressionLeft == "" ? x.ToString() : expressionLeft,
      expressionRight == "" ? y.ToString() : expressionRight);
    }
    else
    {
     expression = "Abandoned";
    }
   }
   break;
 }
 //運算不正當,則前往-1,表達式為Abandoned,
 if (expression == "Abandoned")
 {
  return -1;
 }
 return result;
}

3. Main函數挪用

static void Main(string[] args)
{
 string s = "";
 bool try24;
 try24 = Try24(1, 2, 3, 4, ref s); Console.WriteLine("1,2,3,4: " + s);
 try24 = Try24(1, 3, 5, 7, ref s); Console.WriteLine("1,3,5,7: " + s);
 try24 = Try24(2, 3, 6, 4, ref s); Console.WriteLine("2,3,6,4: " + s);
 try24 = Try24(5, 7, 9, 4, ref s); Console.WriteLine("5,7,9,4: " + s);
 try24 = Try24(6, 3, 4, 5, ref s); Console.WriteLine("6,3,4,5: " + s);
 try24 = Try24(9, 5, 2, 7, ref s); Console.WriteLine("9,5,2,7: " + s);
 try24 = Try24(4, 9, 8, 3, ref s); Console.WriteLine("4,9,8,3: " + s);
 try24 = Try24(4, 9, 8, 2, ref s); Console.WriteLine("4,9,8,2: " + s);
 try24 = Try24(10, 10, 10, 10, ref s); Console.WriteLine("10,10,10,10: " + s);
 Console.ReadLine();
}

4. 法式運轉成果:

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved