N
N
Nico Dillinger2021-03-12 19:21:47
Java
Nico Dillinger, 2021-03-12 19:21:47

The game "Bulls and Cows", is it possible to do better?

I'm new to programming, then I want to know if my code is good and how bad it is. Moreover, I had problems in calculating the number of bulls.
Hidden number: 6990 User
number: 9911, for example,
the number of bulls shows: 4
I understand that there is something wrong with this part

spoiler

boolean[] itersCompare = new boolean[4];
    boolean[] bullsIters = new boolean[4];

    programm.bullscows[0] = 0; // При вызове метода обнуляем быков
    programm.bullscows[1] = 0; // и коров

    for (int i = 0; i < 4; i++)
    {
      if ( arrayInput[i].equals(arraySecret[i]) ) 
      {
        itersCompare[i] = true; 
        if (itersCompare[i] == true)
          programm.bullscows[1] += 1; // Если числа совпадают на позициях -- корова++

      }
      for ( int it = 0; it < 4; it++ ) // пробег числа для выявления быков
      { 
        if ( arrayInput[i].equals(arraySecret[it]) ) 
        {
          bullsIters[i] = true; programm.bullscows[0] += 1;  // быки
        }
      
      }	
    }


In this part, the correspondence of arrays is also calculated - arrayInput, arraySecret, where and the replenishment of cows in addition to the correspondence of array elements.
Actually, the whole code is on github: https://github.com/NIDILLIN/bullsCows
The above fragment is on lines 169-217, the code is large, because there are cheats.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-03-12
@vabka

0. This is outside the scope of the resource
1. There are problems with the code style. This can already be completed.
2. In general, the code looks like shit:

// |						|
    // |						|
    // V Основная работа метода V

    else { // Если чит не найден в введенной строке, то продолжаем игру как играли

      switch (numsInput.length()) // Проверка чтобы избежать exception
      {							// Избегаю его, потому что просить пользака вводить именно 4 символа
        case 3: condition = 3;  // не позволит использовать читы без больших костылей
        break;

        case 2: condition = 2;
        break;

        case 1: condition = 1; 
      }	

      for (int i = 0; i < condition; i++) // Заполнение массива числами. 
        Input[i] = numsInput.substring(i, i + 1); // i-й элемент массива есть строка-символ input-а
    }

Read the book "Clean Code"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question