2011-1-19
 结构,struct类型是值类型,用来封装小型变量组,C#中所有值类型都继承自ValueType,后者继承object,结构的声明:
Struct  **
{
}
 方法,普通方法是实例化后调用,静态的方法用结构名调用
 枚举(menu)
定义:enum  Day
{
 星期一,
星期二,
…………
}
使用时可以加上[flag
] // enum Week
    //    {
    //        周一,
    //        周二,
    //        周三,
    //        周四=3,
    //        周五,
    //        周六,
    //        周日=64
    //    }

 

    //[Flags]
    //enum JJ
    //{
    //    春季 = 1,
    //    夏季 = 2,
    //    秋季 = 4,
    //    冬季 = 8
    //}

    //class Program
    //{
    //     static void Main(string[] args)
    //    {
            //Console.WriteLine((int)Week .周六);//输出的结果为4,不管赋值是多少都只加1
            //Console.WriteLine(Week .周四);//输出结果为周四
            //Week w = (Week)Enum.Parse(typeof(Week), "周日");//转换类型,把string类型转换
            //Console.WriteLine(w);//输出结果是周日

 

            Console.WriteLine(Enum.Format(typeof(Week ), 4, "G"));//输出结果是:周六
            Console.WriteLine(Enum.Format(typeof(Week ), 4, "X"));//输出结果是:00000004  ,   X以十六进制的格式表示
            Console.WriteLine(Enum.Format(typeof(Week), 4, "D"));//输出结果是:4   ,          以十进制的格式 表示
            Console.WriteLine(Enum.Format(typeof(Week), 4, "F"));//输出结果是周五


           

            //Console.WriteLine((JJ)7);//输入的结果为春季,夏季,秋季,7为这三季之和
            //Console.WriteLine(JJ .春季|JJ .夏季);// 输出结果为春季,夏季


 数组
//一维
            //int[] IntArr = new int[] {1,3,4,5,6 };
            //int[] IntArr = { 1, 2, 3, 4 };
            int[] IntArr = new int[4];
            for (int i = 0; i < IntArr.Length; i++)
            {
                IntArr[i] = i * 2;
            }
            string[] strArr = { "a", "b", "c" };
            //for (int i = 0; i < IntArr.GetLength(0); i++)
            //{
            //    Console.WriteLine(IntArr[i]);
            //}
            //foreach (int i in IntArr)
            //{
            //    Console.WriteLine(i);           
            //}
            //foreach (string s in strArr)
            //{
            //    Console.WriteLine(s);
            //}
            //二维数组
            //int[,] intArr = new int[,] {{1,2,3},{4,5,6},{7,8,9} };
            //int[,] intArr = new int[3, 3];
            //for (int i = 0; i < intArr.GetLength(0); i++)
            //{
            //    for (int j = 0; j < intArr.GetLength(1); j++)
            //    {
            //        intArr[i,j]=i+j;
            //    }
            //}

            //for (int i = 0; i < intArr.GetLength(0); i++)
            //{
            //    for (int j = 0; j < intArr.GetLength(1); j++)
            //    {
            //        Console.Write(intArr[i, j]+",");
            //    }
            //    Console.WriteLine();
            //}
            //Console.WriteLine("----------------------");
            //foreach (int i in intArr)
            //{
            //    Console.WriteLine(i);
            //}

 


            //交错
            //int[][] jcArr = new int[3][];
            //jcArr[0] = new int[] { 1, 2 };
            //jcArr[1] = new int[] { 3, 4, 5, 6 };
            //jcArr[2] = new int[] { 8, 9, 0 };

            //for (int i = 0; i < jcArr.GetLength(0); i++)
            //{
            //    for (int j = 0; j < jcArr[i].Length; j++)
            //    {
            //        Console.WriteLine(jcArr[i][j]);
            //    }
            //    Console.WriteLine("--");
            //}
            //foreach (int[] i in jcArr)
            //{
            //    foreach (int j in i)
            //    {
            //        Console.WriteLine(j);
            //    }
            //}


            Array a = Array.CreateInstance(typeof(string), 5);
            a.SetValue("dddddfasdfsefa", 0);

            Console.WriteLine(a.GetValue(0));

           int[] arr={ 3,4,56,2,3,5,2,2,5754,232,464};
           Array.Sort(arr);排序
           Array.Reverse(arr);
           foreach (int i in arr)
           {
               Console.WriteLine(i);
           }
 最后练习了五子棋这个项目,由于还没学到绘图部分,在这里的表格用“□”代替,把大量的这个符号累加在一起,作为棋盘,在这个项目中最难的就是怎么样判断输赢,我们以落子点作为远点,向横、竖、左上、右下方向判断是否够五个一样的子,若一样,就判断为赢,若不一样则继续下棋,落子时先判断这个位置是否已经落子,即判断这个坐标的符号是否还是“□”,来确定能否落子,以下部分是详细代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication5
{
    class Program
    {
        //□●○
        static void Main()
        {
            int number = 17;
            string[,] QP = new string[number, number];
            for (int i = 0; i < QP.GetLength(0); i++)
            {
                for (int j = 0; j < QP.GetLength(1); j++)
                {
                    QP[i, j] = "□";
                    Console.Write(QP[i, j]);
                }
                Console.WriteLine();
            }
            bool mark = true;
            while (true)
            {
                Console.WriteLine("请输入落子坐标(5,5):");
                string[] Points = Console.ReadLine().Split(',');
                Console.Clear();
                int x = int.Parse(Points[0]);
                int y = int.Parse(Points[1]);

                if (QP[x, y] == "□")
                {
                    if (mark)
                    {
                        QP[x, y] = "●";
                    }
                    else
                    {
                        QP[x, y] = "●";
                    }
                    mark = !mark;

                    PrintQP(QP);

                    if (PDSY(QP, x, y))
                    {
                        Console.WriteLine("五子连珠成功");
                        break;
                    }
                    else
                    {

                    }
                }
                else
                {
                    PrintQP(QP);
                    Console.WriteLine("这个地方不能落子");
                }
            }

        }

        /// <summary>
        /// 判断输赢
        /// </summary>
        /// <param name="QP">棋盘</param>
        /// <param name="x">横坐标</param>
        /// <param name="y">纵坐标</param>
        /// <returns></returns>
        private static bool PDSY(string[,] QP, int x, int y)
        {
            if (Heng(QP, x, y))
            {
                return true;
            }
            else
            {
                if (Zong(QP, x, y))
                {
                    return true;
                }
                else
                {
                    if (RightXie(QP, x, y))
                    {
                        return true;
                    }
                    else
                    {
                        if (LeftXie(QP, x, y))
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }

            }
        
        }
        /// <summary>
        /// 判断左下方
        /// </summary>
        /// <param name="QP">棋盘</param>
        /// <param name="x">横坐标</param>
        /// <param name="y">纵坐标</param>
        /// <returns></returns>
        private static bool LeftXie(string[,] QP, int x, int y)
        {
            string basepoint = QP[x, y];
            int count = 1;
            for (int X = x - 1, Y = y + 1; X >= 0 && Y <QP.GetLength (1); X--, Y++)
            {
                if (QP[X, Y] == basepoint)
                {
                    count++;
                    if (count == 5)
                    {
                        return true;
                    }
                }
            }
            for (int X = x + 1, Y = y - 1; X < QP.GetLength(0) && Y >=0; X++, Y--)
            {
                if (QP[X, Y] == basepoint)
                {
                    count++;
                    if (count == 5)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 判断右下
        /// </summary>
        /// <param name="QP">棋盘</param>
        /// <param name="x">横坐标</param>
        /// <param name="y">纵坐标</param>
        /// <returns></returns>
        private static bool RightXie(string[,] QP, int x, int y)
        {
            string basepoint = QP[x, y];
            int count = 1;
            for (int X = x - 1, Y = y - 1; X >= 0 && Y >= 0; X--, Y--)
            {
                if (QP[X, Y] == basepoint)
                {
                    count++;
                    if (count == 5)
                    {
                        return true;
                    }
                }
            }
            for (int X = x + 1, Y = y + 1; X < QP.GetLength(0) && Y < QP.GetLength(1); X++, Y++)
            {
                if (QP[X, Y] == basepoint)
                {
                    count++;
                    if (count == 5)
                    {
                        return true;
                    }
                }
            }

            return false;


        }
        /// <summary>
        /// 判断纵向
        /// </summary>
        /// <param name="QP">棋盘</param>
        /// <param name="x">横坐标</param>
        /// <param name="y">纵坐标</param>
        /// <returns></returns>
        private static bool Zong(string[,] QP, int x, int y)
        {
            string basepoint = QP[x, y];
            int count = 1;
            for (int i = x - 1; i >= 0; i--)
            {
                if (QP[i, y] == basepoint)
                {
                    count++;
                    if (count == 5)
                    {
                        return true;
                    }
                }
                else
                {
                    break;
                }
            }

            for (int i = x + 1; i < QP.GetLength(0); i++)
            {
                if (QP[i, y] == basepoint)
                {
                    count++;
                    if (count == 5)
                    {
                        return true;
                    }
                }
                else
                {
                    break;
                }
            }


            return false;

        }
        /// <summary>
        /// 判断横向
        /// </summary>
        /// <param name="QP">棋盘</param>
        /// <param name="x">横坐标</param>
        /// <param name="y">纵坐标</param>
        /// <returns></returns>
        private static bool Heng(string[,] QP, int x, int y)
        {
            string basepoint = QP[x, y];
            int count = 1;
            for (int i = y - 1; i >= 0; i--)
            {
                if (QP[x, i] == basepoint)
                {
                    count++;
                    if (count == 5)
                    {
                        return true;
                    }
                }
                else
                {
                    break;
                }
            }
            for (int i = y + 1; i < QP.GetLength(1); i++)
            {
                if (QP[x, i] == basepoint)
                {
                    count++;
                    if (count == 5)
                    {
                        return true;
                    }
                }
                else
                {
                    break;
                }
            }
            return false;
        }
        /// <summary>
        /// 显示棋盘
        /// </summary>
        /// <param name="QP"></param>
        static void PrintQP(string[,] QP)
        {
            for (int i = 0; i < QP.GetLength(0); i++)
            {
                for (int j = 0; j < QP.GetLength(1); j++)
                {
                    Console.Write(QP[i, j]);
                }
                Console.WriteLine();
            }
        }
    }
}