井字游戏中的比赛计数器

时间:2019-06-30 20:09:09

标签: c#

我在大学时从C#开始,我不知道如何处理这段代码,我只需要清除棋盘,以便在比赛结束时另一人开始清理棋盘。

Board();和CheckWinner();在这里无关紧要,但是如果需要,我可以在这里上传。我已经尝试在“ while”开始时声明变量,但是它不允许我输入X或O,现在我真的迷路了。

using System;
using System.Threading;

namespace tictactoec#
{
class Program
{
    static char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    static string player1, player2;
    static int player = 1;
    static int chance;
    static int flag = 0;
    static int wins_p1, wins_p2, match_numbers;
    static int match_counter = 0;

    static void Main(string[] args)
    {

        Console.Write("How many matches?: ");
        match_numbers = int.Parse(Console.ReadLine());

        Console.Write("Player 1 nick: ");
        player1 = Console.ReadLine();

        Console.Write("Player 2 nick: ");
        player2 = Console.ReadLine();

        while (match_counter < match_numbers)
        {
            do
            {
                Console.Clear();
                Console.WriteLine("Score: ");
                Console.WriteLine("{0}: {1}", player1, wins_p1);
                Console.WriteLine("{0}: {1}", player2, wins_p2);
                Console.WriteLine("{0} is X and {1} is O!", player1, player2);
                Console.WriteLine("");
                if (player% 2 == 0)
                {

                    Console.WriteLine("{0} turn", player2);

                }

                else
                {

                    Console.WriteLine("{0} turn", player1);

                }

                Console.WriteLine("");
                Board();
                chance = int.Parse(Console.ReadLine());
                if (arr[chance] != 'X' && arr[chance] != '0')
                {
                    if (player % 2 == 0)
                    {
                        arr[chance] = 'O';
                        player++;
                    }
                    else
                    {
                        arr[chance] = 'X';
                        player++;
                    }
                }
                else
                {
                    Console.WriteLine("This place {0} is marked with {1}", chance, arr[chance]);
                    Console.WriteLine("");
                    Console.WriteLine("Loading.....");
                    Thread.Sleep(2000);
                }

                flag = CheckWinner();

            } while (flag != 1 && flag != -1);

            Console.Clear();
            Board();

            if (flag == 1 && ((player % 2) + 1 == 1))
            {
                wins_p1++;
                 Console.WriteLine("Score: ");
                Console.WriteLine("{0}: {1}", player1, wins_p1);
                Console.WriteLine("{0}: {1}", player2, wins_p2);

                Console.WriteLine("{0} wins!", player1);
            }

            else if (flag == 1 && ((player % 2) + 1 != 1))
            {
                wins_p2++;
                Console.WriteLine("Score: ");
                Console.WriteLine("{0}: {1}", player1, wins_p1);
                Console.WriteLine("{0}: {1}", player2, wins_p2);

                Console.WriteLine("{0} wins!", player2);
            }
            else

            {
                Console.WriteLine("Score: ");
                Console.WriteLine("{0}: {1}", player1, wins_p1);
                Console.WriteLine("{0}: {1}", player2, wins_p2);

                Console.WriteLine("Draw!");
            }

            Console.ReadLine();

            match_counter++;

        }

        if (wins_p1 <= wins_p2)
        {
            Console.WriteLine("{0} wins!", player2);
            Console.WriteLine("Score: ");
                Console.WriteLine("{0}: {1}", player1, wins_p1);
                Console.WriteLine("{0}: {1}", player2, wins_p2);
        }
        else
        {
            Console.WriteLine("{0} wins!", player1);
           Console.WriteLine("Score: ");
                Console.WriteLine("{0}: {1}", player1, wins_p1);
                Console.WriteLine("{0}: {1}", player2, wins_p2);
        }


    }

1 个答案:

答案 0 :(得分:1)

太棒了!我是在@SinOfficial帮助下完成的:

 while (match_counter < match_numbers)
        {       arr = new char[10] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
                flag = 0;
            do ___rest of the code___

非常感谢