周易八卦——数字卦预测的程序实现

 1         /// <summary>
 2         /// 根据数字确定八卦的上下卦,以及爻
 3         /// </summary>
 4         /// <param name="numbs"></param>
 5         /// <returns></returns>
 6         private static List<int> ComputerEightDiagrams(List<int> numbs)
 7         {
 8             List<int> dias = new List<int>();
 9 
10             for (int i = 0; i < numbs.Count; i++)
11             {
12                 if (i != numbs.Count - 1)
13                 {
14                     var m = numbs[i] % 8;
15 
16                     if (m == 0) m = 8;
17 
18                     dias.Add(m);
19 
20                 }
21                 else
22                 {
23                     var n = numbs[i] % 6;
24 
25                     if (n == 0) n = 6;
26 
27                     dias.Add(n);
28                 }
29             }
30             return dias;
31         }

根据用户的输入确定是哪一卦,哪一爻。

下面的代码是如何在控制台打印八卦图:

 1         private static void Print(List<int> diagrams)
 2         {
 3             //存储
 4             string[] keys = { "111", "011", "101", "001", "110", "010", "100", "000" };
 5 
 6             var up = diagrams[1];
 7             var down = diagrams[0];
 8 
 9             var upStr = keys[up - 1];
10             var downStr = keys[down - 1];
11 
12             n = 0;
13             PrintYao(upStr.ToCharArray(), diagrams[2]);
14             PrintYao(downStr.ToCharArray(), diagrams[2]);
15 
16             Console.ResetColor();
17         }
18 
19         private static int n = 0;
20         private static void PrintYao(char[] chars, int yao)
21         {
22             bool isYao=false;
23 
24             foreach (var item in chars)
25             {
26                 n++;
27 
28                 Console.WriteLine("");
29 
30                 isYao = n == 6 - yao + 1;
31 
32                 if (item == '1')
33                 {
34                     Console.BackgroundColor =isYao? ConsoleColor.Yellow:ConsoleColor.Blue;
35                     Console.WriteLine("            ");
36                 }
37                 else if (item == '0')
38                 {
39                     Console.BackgroundColor = isYao ? ConsoleColor.Yellow : ConsoleColor.DarkRed;
40                     
41                     var color = Console.BackgroundColor;
42 
43                     Console.Write("     ");
44 
45                     Console.ResetColor();
46                     Console.Write("  ");
47 
48                     Console.BackgroundColor = color;
49                     Console.Write("     ");
50                     Console.Write("\r\n");
51                 }
52             }
53         }

diagrams,存储了上卦和下卦的顺序,
其中keys数组中存储了八卦的爻信息,1:阳爻,0:阴爻。比如 111代表乾卦。
此程序运行效果如下:

高亮:代表算出的爻。下载完整的代码:https://github.com/wangqiang3311/mywork

 

posted @ 2017-07-06 11:32  micDavid  阅读(2091)  评论(0编辑  收藏  举报