c # консольная проверка - это пробел или х?не строка в наборе - PullRequest
0 голосов
/ 23 октября 2018

Я делаю домашнее задание, похожее на игру в крестики-нолики, и у меня большая проблема.Когда я помещаю X в место, которое уже заполнено, игра должна выдавать ошибку или что-то в этом роде.Я не могу проверить, является ли он нулевым или полным.Что мне делать?

Я ставлю X с помощью команды установки положения курсора, но не могу проверить их, потому что они не являются строками.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp18
{
class Program
{
    static void Main(string[] args)
    {
        int xcor,ycor,counter;
        bool win=false,a,b,c,d,e,f,g,h,i;
        counter = 0;

        Console.SetCursorPosition(2, 2);
        Console.Write("-------- Turn " + counter + " --------");
        Console.SetCursorPosition(3, 6);
        Console.Write("  1  2  3 ");
        Console.SetCursorPosition(2, 7);
        Console.Write(" + -  -  - +");
        Console.SetCursorPosition(2, 8);
        Console.Write("1|         |");
        Console.SetCursorPosition(2, 9);
        Console.Write("2|         |");
        Console.SetCursorPosition(2, 10);
        Console.Write("3|         |");
        Console.SetCursorPosition(2, 11);
        Console.Write(" + -  -  - +");
        Console.SetCursorPosition(20, 3);
        Console.Write("generated piece");

        do
        {
            /*üretim*/
            generatepieces:
            counter++;
            Console.SetCursorPosition(2, 2);
            Console.Write("-------- Turn " + counter + " --------");

            Console.SetCursorPosition(20, 5);
            Console.Write("               ");
            Console.SetCursorPosition(20, 6);
            Console.Write("               ");
            Console.SetCursorPosition(20, 7);
            Console.Write("               ");

            Random rnd = new Random();
            int x = rnd.Next(1,6);
            Console.SetCursorPosition(20, 5);
            if (x == 1)
            {
                Console.SetCursorPosition(20, 5);
                Console.WriteLine(" x ");
            }
            else if (x == 2)
            {
                Console.SetCursorPosition(20, 5);
                Console.WriteLine(" x x ");
            }
            else if (x == 3)
            {
                Console.SetCursorPosition(20, 5);
                Console.WriteLine(" x x x ");
            }
            else if (x == 4)
            {
                Console.SetCursorPosition(20, 5);
                Console.WriteLine(" x ");
                Console.SetCursorPosition(20, 6);
                Console.WriteLine(" x ");
            }
            else if (x == 5)
            {
                Console.SetCursorPosition(20, 5);
                Console.WriteLine(" x ");
                Console.SetCursorPosition(20, 6);
                Console.WriteLine(" x ");
                Console.SetCursorPosition(20, 7);
                Console.WriteLine(" x ");
            }

            /*isteme*/

            bastangir:
            Console.SetCursorPosition(2, 12);
            Console.Write(new String(' ', Console.BufferWidth - 1));
            Console.SetCursorPosition(2, 13);
            Console.Write(new String(' ', Console.BufferWidth - 1));
            Console.SetCursorPosition(2, 12);
            Console.Write("x coordinate = ");
            xcor = Convert.ToInt32(Console.ReadLine());
            Console.SetCursorPosition(0, 15);
            Console.Write(new string(' ', Console.BufferWidth - Console.CursorLeft));
            Console.SetCursorPosition(2, 13);
            Console.Write("y coordinate = ");
            ycor = Convert.ToInt32(Console.ReadLine());

            /*yazma*/

            if (xcor > 3 || ycor > 3)
            {
                Console.WriteLine();
                Console.Write("Hatalı girdi");
                goto bastangir;
            }
            if (xcor == 0 && ycor == 0)
            {
                Console.WriteLine();
                Console.WriteLine("Pas geçti");
                goto generatepieces;
            }

            else if (x == 2 && xcor == 3)

            {
                Console.WriteLine();
                Console.WriteLine("Hatalı Girdi");
                goto bastangir;
            }
            else if (x == 3 && xcor != 1)
            {
                Console.WriteLine();
                Console.WriteLine("Hatalı Girdi");
                goto bastangir;
            }

            else if (x == 4 && ycor == 3)
            {
                Console.WriteLine();
                Console.WriteLine("Hatalı Girdi");
                goto bastangir;
            }

            else if (x == 5 && ycor != 1)
            {
                Console.WriteLine();
                Console.WriteLine("Hatalı Girdi");
                goto bastangir;
            }

            else if (x == 1)

            {
                Console.SetCursorPosition(1 + 3 * xcor, 7 + ycor);
                Console.Write(" x ");
            }

            else if (x == 2)
            {
                Console.SetCursorPosition(1 + 3 * xcor, 7 + ycor);
                Console.Write(" x  x ");
            }

            else if (x == 3)
            {
                Console.SetCursorPosition(1 + 3 * xcor, 7 + ycor);
                Console.Write(" x  x  x ");
            }
            else if (x == 4)

            {
                Console.SetCursorPosition(1 + 3 * xcor, 7 + ycor);
                Console.Write(" x ");
                Console.SetCursorPosition(1 + 3 * xcor, 8 + ycor);
                Console.Write(" x ");

            }
            else if (x == 5)
            {
                Console.SetCursorPosition(1 + 3 * xcor, 7 + ycor);
                Console.Write(" x ");
                Console.SetCursorPosition(1 + 3 * xcor, 8 + ycor);
                Console.Write(" x ");
                Console.SetCursorPosition(1 + 3 * xcor, 9 + ycor);
                Console.Write(" x ");
            }
        } while (win==false);

    Console.Read();

    }
}
}

1 Ответ

0 голосов
/ 23 октября 2018

Ничего себе, этот код немного ... накладные расходы:)

Для крестики-нолики вы можете использовать, например, матрицу ... скажем,

int[][] field = new int[3][3];

0) Позволяетскажем, что x равно 1, а o равно 0, а значение по умолчанию равно -1 1) назначить значения по умолчанию для -1 2) напечатать таблицу для пользователя 3) спросить пользователя, куда он хочет поместить x (вам нужны координаты x и y) 4)field [xcord-1] [ycord-1] = 1, если это поле имеет значение по умолчанию 5) напечатать таблицу для пользователя 6) проверить, выиграл ли пользователь игру (с циклом!)

и повторить операторы ..и да ... такие вещи, как печать таблицы для пользователя ... использовать функцию;)

Веселиться

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...