井字游戏

FBIBARRY

新成员
已加入
2020年11月5日
留言内容
4
编程经验
Beginner
大家好,
Can somebody explain to me how this code runs e.g. what does 板 mean? I am new to c# and am trying to understand this code so that I can appropriately annotate it.
谢谢





C#:
using System;
using System.Threading;

namespace TIC_TAC_TOE
{
    class Program
    {
        //making array and   
        //by default I am providing 0-9 where no use of zero 
        static char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        static int player = 1; //By default player 1 is set 
        static int choice; //This holds the choice at which position user want to mark   

        // The flag veriable checks who has won if it's value is 1 then some one has won the match if -1 then Match has Draw if 0 then match is still running 
        static int flag = 0;

        static void Main(string[] args)
        {
            do
            {
                Console.Clear();// whenever loop will be again start then screen will be clear 
                Console.WriteLine("Player1:X and Player2:O");
                Console.WriteLine("\n");
                if (player % 2 == 0)//checking the chance of the player 
                {
                    Console.WriteLine("Player 2 Chance");
                }
                else
                {
                    Console.WriteLine("Player 1 Chance");
                }
                Console.WriteLine("\n");
                Board();// calling the 板 Function 
                choice = int.Parse(Console.ReadLine());//Taking users choice   

                // checking that position where user want to run is marked (with X or O) or not 
                if (arr[choice] != 'X' && arr[choice] != 'O')
                {
                    if (player % 2 == 0) //if chance is of player 2 then mark O else mark X 
                    {
                        arr[choice] = 'O';
                        player++;
                    }
                    else
                    {
                        arr[choice] = 'X';
                        player++;
                    }
                }
                else //If there is any possition where user want to run and that is already marked then show message and load 板 again 
                {
                    Console.WriteLine("Sorry the row {0} is already marked with {1}", choice, arr[choice]);
                    Console.WriteLine("\n");
                    Console.WriteLine("Please wait 2 second 板 is loading again.....");
                    Thread.Sleep(2000);
                }
                flag = CheckWin();// calling of check win 
            } while (flag != 1 && flag != -1);// This loof will be run until all cell of the grid is not marked with X and O or some player is not win 

            Console.Clear();// clearing the console 
            Board();// getting filled 板 again 

            if (flag == 1)// if flag value is 1 then some one has win or means who played marked last time which has win 
            {
                Console.WriteLine("Player {0} has won", (player % 2) + 1);
            }
            else// if flag value is -1 the match will be draw and no one is winner 
            {
                Console.WriteLine("Draw");
            }
            Console.ReadLine();
        }
        // Board method which creats 板 
        private static void Board()
        {
            Console.WriteLine("     |     |      ");
            Console.WriteLine("  {0}  |  {1}  |  {2}", arr[1], arr[2], arr[3]);
            Console.WriteLine("_____|_____|_____ ");
            Console.WriteLine("     |     |      ");
            Console.WriteLine("  {0}  |  {1}  |  {2}", arr[4], arr[5], arr[6]);
            Console.WriteLine("_____|_____|_____ ");
            Console.WriteLine("     |     |      ");
            Console.WriteLine("  {0}  |  {1}  |  {2}", arr[7], arr[8], arr[9]);
            Console.WriteLine("     |     |      ");
        }

        //Checking that any player has won or not 
        private static int CheckWin()
        {
            #region Horzontal Winning Condtion
            //Winning Condition For First Row   
            if (arr[1] == arr[2] && arr[2] == arr[3])
            {
                return 1;
            }
            //Winning Condition For Second Row   
            else if (arr[4] == arr[5] && arr[5] == arr[6])
            {
                return 1;
            }
            //Winning Condition For Third Row   
            else if (arr[6] == arr[7] && arr[7] == arr[8])
            {
                return 1;
            }
            #endregion

            #region vertical Winning Condtion
            //Winning Condition For First Column       
            else if (arr[1] == arr[4] && arr[4] == arr[7])
            {
                return 1;
            }
            //Winning Condition For Second Column 
            else if (arr[2] == arr[5] && arr[5] == arr[8])
            {
                return 1;
            }
            //Winning Condition For Third Column 
            else if (arr[3] == arr[6] && arr[6] == arr[9])
            {
                return 1;
            }
            #endregion

            #region Diagonal Winning Condition
            else if (arr[1] == arr[5] && arr[5] == arr[9])
            {
                return 1;
            }
            else if (arr[3] == arr[5] && arr[5] == arr[7])
            {
                return 1;
            }
            #endregion

            #region Checking For Draw
            // If all the cells or values filled with X or O then any player has won the match 
            else if (arr[1] != '1' && arr[2] != '2' && arr[3] != '3' && arr[4] != '4' && arr[5] != '5' && arr[6] != '6' && arr[7] != '7' && arr[8] != '8' && arr[9] != '9')
            {
                return -1;
            }
            #endregion

            else
            {
                return 0;
            }
        }
    }
}
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,499
地点
悉尼,澳大利亚
编程经验
10+
根据您发布的内容,您几乎没有做出任何努力来帮助自己,只是希望我们为您做这一切。您是否考虑过手动玩游戏的逻辑以及代码与之对应的方式?您是否在代码的顶部设置了一个断点,并逐行浏览该断点,以了解其需要执行的过程以及使用了哪些数据?您了解哪些部分,而您不了解哪些特定部分?您应该可以向我们提出比具体问题更多的问题"这是一大段代码,向我解释一切".

至于有关的问题"board",这也相当模糊。您是在代码中专门讨论还是一般性地谈论?我不确定英语是否是您的母语,但是您是否没有听到"board games"? Board games are games that are played on a 板. When you open a Monopoly box, for instance, you find a 板, i.e. the cardboard playing surface, and the players' pieces that you will move around that 板. Any reference to "board" in the code for a 板 game is obviously for the digital equivalent of that physical playing surface. If you're asking about the physical game part then that's not a programming question. If you're asking about how it's used in the code then that suggests that you haven't really taken the time to learn the basics.
 

FBIBARRY

新成员
已加入
2020年11月5日
留言内容
4
编程经验
Beginner
根据您发布的内容,您几乎没有做出任何努力来帮助自己,只是希望我们为您做这一切。您是否考虑过手动玩游戏的逻辑以及代码与之对应的方式?您是否在代码的顶部设置了一个断点,并逐行浏览该断点,以了解其需要执行的过程以及使用了哪些数据?您了解哪些部分,而您不了解哪些特定部分?您应该可以向我们提出比具体问题更多的问题"这是一大段代码,向我解释一切".

至于有关的问题"board",这也相当模糊。您是在代码中专门讨论还是一般性地谈论?我不确定英语是否是您的母语,但是您是否没有听到"board games"? Board games are games that are played on a 板. When you open a Monopoly box, for instance, you find a 板, i.e. the cardboard playing surface, and the players' pieces that you will move around that 板. Any reference to "board" in the code for a 板 game is obviously for the digital equivalent of that physical playing surface. If you're asking about the physical game part then that's not a programming question. If you're asking about how it's used in the code then that suggests that you haven't really taken the time to learn the basics.
嗨,谢谢你的答复。是的,您是对的,因为我没有尝试帮助自己。我曾经在yt上浏览过有关代码的视频,现在对它的理解要好得多。感谢您的帮助 !
 

羊皮

退休程序员
工作人员
已加入
2018年9月5日
留言内容
1,921
地点
英国
编程经验
10+
我同意一切 @jmcilhinney 说过。而且,youtube是一种学习如何编码以及如何编程的可怕方式,应避免这种作案手法。如果多年来我纠正过的每个youtube视频都能赚到一分钱,那么我今天就会很有钱。 :)
 
最佳 底部