Untitled

 avatar
user_9929031
plain_text
20 days ago
4.6 kB
0
Indexable
Never
Вот пример простой реализации игры "Крестики-нолики" на C#:

```csharp
using System;

class Program
{
    static char[] board = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    static int player = 1; // Player 1 starts
    static int choice;
    static int flag = 0; // Set to 1 when a player wins

    // Check win
    private static int CheckWin()
    {
        #region Horzontal Winning Condtion
        if (board[0] == board[1] && board[1] == board[2])
        {
            return 1;
        }
        else if (board[3] == board[4] && board[4] == board[5])
        {
            return 1;
        }
        else if (board[6] == board[7] && board[7] == board[8])
        {
            return 1;
        }
        #endregion

        #region Vertical Winning Condtion
        else if (board[0] == board[3] && board[3] == board[6])
        {
            return 1;
        }
        else if (board[1] == board[4] && board[4] == board[7])
        {
            return 1;
        }
        else if (board[2] == board[5] && board[5] == board[8])
        {
            return 1;
        }
        #endregion

        #region Diagonal Winning Condition
        else if (board[0] == board[4] && board[4] == board[8])
        {
            return 1;
        }
        else if (board[2] == board[4] && board[4] == board[6])
        {
            return 1;
        }
        #endregion

        #region Checking For Draw
        else if (board[0] != '1' && board[1] != '2' && board[2] != '3' && board[3] != '4' 
                 && board[4] != '5' && board[5] != '6' && board[6] != '7' && board[7] != '8' && board[8] != '9')
        {
            return -1; // It's a draw
        }
        #endregion

        else
        {
            return 0; // Continue the game
        }
    }

    // Draws the board
    private static void DrawBoard()
    {
        Console.WriteLine("     |     |      ");
        Console.WriteLine("  {0}  |  {1}  |  {2}", board[0], board[1], board[2]);
        Console.WriteLine("_____|_____|_____ ");
        Console.WriteLine("     |     |     ");
        Console.WriteLine("  {0}  |  {1}  |  {2}", board[3], board[4], board[5]);
        Console.WriteLine("_____|_____|_____ ");
        Console.WriteLine("     |     |     ");
        Console.WriteLine("  {0}  |  {1}  |  {2}", board[6], board[7], board[8]);
        Console.WriteLine("     |     |     ");
    }

    static void Main()
    {
        do
        {
            Console.Clear(); // Whenever loop will restart, screen will be clear
            Console.WriteLine("Player 1: X and Player 2: O");
            Console.WriteLine("\n");
            if (player % 2 == 0)
            {
                Console.WriteLine("Turn Player 2");
            }
            else
            {
                Console.WriteLine("Turn Player 1");
            }
            Console.WriteLine("\n");
            DrawBoard(); // board will be drawn after every input

            //Checking the winner
            flag = CheckWin();
            if (flag == 1)
            {
                Console.WriteLine("Player {0} has won!", (player % 2) + 1);
                Console.ReadLine();
            }
            else if (flag == -1)
            {
                Console.WriteLine("It's a Draw!");
                Console.ReadLine();
            }
            
            // Taking input from player
            bool validInput = int.TryParse(Console.ReadLine(), out choice);

            if (!validInput || choice < 1 || choice > 9 || board[choice - 1] == 'X' || board[choice - 1] == 'O')
            {
                Console.WriteLine("Invalid input. Please try again.");
                Console.ReadLine();
            }
            else
            {
                if (player % 2 == 0) 
                {
                    board[choice - 1] = 'O';
                    player++;
                }
                else
                {
                    board[choice - 1] = 'X';
                    player++;
                }
            }
        } while (flag != 1 && flag != -1); // Loop until someone wins or it's a draw
    }
}
```

//Этот код представляет простую консольную реализацию игры "Крестики-нолики" на C#. Игроки поочередно делают ходы, вводя номер клетки, в которую они хотят поставить свой символ (X или O). Игра продолжается до тех пор, пока кто-то не выиграет или пока не наступит ничья.
Leave a Comment