五子棋程序

using System;


class Program
{
    public static void Main()
    {
        string UserMove;
        int one, two;
        
        string[,] chess = new string[10, 10];
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                chess[i, j] = "- ";
            }
        }

        bool win = false;
        int turn = 0;
        do
        {

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    Console.Write(chess[i, j]);
                }
                Console.WriteLine();
            }

            do
            {
                Console.Write("Please enter your next move (Example: 0, 2): ");
                UserMove = Console.ReadLine();
                string[] inputSplit = UserMove.Split(',');
                one = int.Parse(inputSplit[0]);
                two = int.Parse(inputSplit[1]);


                if (chess[one, two] == "0 " || chess[one, two] == "1 ")
                {
                    Console.WriteLine("Invalid step, please try again:");
                }

            } while (chess[one, two] == "0 " || chess[one, two] == "1 ");

            if (turn % 2 == 0)
            {
                chess[one, two] = "0 ";
            }
            else if (turn % 2 == 1)
            {
                chess[one, two] = "1 ";
            }

            turn++;

            int flag = 0;
            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 6; j++)
                {
                    string ij = chess[i, j];
                    string i1j = chess[i + 1, j];
                    string i2j = chess[i + 2, j];
                    string i3j = chess[i + 3, j];
                    string i4j = chess[i + 4, j];

                    string ij1 = chess[i, j + 1];
                    string ij2 = chess[i, j + 2];
                    string ij3 = chess[i, j + 3];
                    string ij4 = chess[i, j + 4];

                    string i1j1 = chess[i + 1, j + 1];
                    string i2j2 = chess[i + 2, j + 2];
                    string i3j3 = chess[i + 3, j + 3];
                    string i4j4 = chess[i + 4, j + 4];

                    for (int a = 5; a < 10; a++)
                    {
                        for (int b = 0; b < 6; b++)
                        {
                            string ijslant = chess[a, b];
                            string if1j1 = chess[a - 1, b + 1];
                            string if2j2 = chess[a - 2, b + 2];
                            string if3j3 = chess[a - 3, b + 3];
                            string if4j4 = chess[a - 4, b + 4];

                            if (ijslant == if1j1 && if1j1 == if2j2 && if2j2 == if3j3 && if3j3 == if4j4 && ijslant != "- ")
                            {
                                for (int x = 0; x < 10; x++)
                                {
                                    for (int y = 0; y < 10; y++)
                                    {
                                        Console.Write(chess[x, y]);
                                    }
                                    Console.WriteLine();
                                }
                                
                                Console.WriteLine("You Win!");
                                flag = 1;
                                break;
                            }
                        }
                        if (flag == 1)
                        {
                            break;
                        }
                    }
                    if (flag == 1)
                    {
                        break;
                    }
                    if (ij == i1j && i1j == i2j && i2j == i3j && i3j == i4j && ij != "- ")
                    {
                        for (int x = 0; x < 10; x++)
                        {
                            for (int y = 0; y < 10; y++)
                            {
                                Console.Write(chess[x, y]);
                            }
                            Console.WriteLine();
                        }

                        Console.WriteLine("You Win!");
                        flag = 1;
                        break;
                    }
                    else if (ij == ij1 && ij1 == ij2 && ij2 == ij3 && ij3 == ij4 && ij != "- ")
                    {
                        for (int x = 0; x < 10; x++)
                        {
                            for (int y = 0; y < 10; y++)
                            {
                                Console.Write(chess[x, y]);
                            }
                            Console.WriteLine();
                        }

                        Console.WriteLine("You Win");
                        flag = 1;
                        break;
                    }
                    else if (ij == i1j1 && i1j1 == i2j2 && i2j2 == i3j3 && i3j3 == i4j4 && ij != "- ")
                    {
                        for (int x = 0; x < 10; x++)
                        {
                            for (int y = 0; y < 10; y++)
                            {
                                Console.Write(chess[x, y]);
                            }
                            Console.WriteLine();
                        }

                        Console.WriteLine("You Win");
                        flag = 1;
                        break;
                    }
                    
                }
                if (flag == 1)
                {
                    break;
                }
            }
            if (flag == 1)
            {
                break;
            }
        } while (win == false);

        Console.WriteLine("Congratulations!");
        Console.WriteLine("Thank you for playing");

    }
}

猜你喜欢

转载自blog.csdn.net/charliexie10/article/details/107668214