A
A
Alexander Dorofeev2015-11-02 19:48:26
Java
Alexander Dorofeev, 2015-11-02 19:48:26

What is the problem with my implementation of the Guess Number game?

It is necessary to implement the Guess Number game. The player mentally guesses a number, and the computer must
guess this number in 10 questions. The number is not entered into the program. Each question can be answered "Yes" or "No".
It would seem an elementary task for binary search, but for some reason I don’t understand! What is my mistake?

public class Finder {

    public static void main(String[] args) throws IOException {
        int[] array = new int[1000];
       //заполняем массив
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int num = 0;
        int elements = array.length;
        int lowerBound = 0;
        int upperBound = elements - 1;
        int curIn;
        int step = 0;
        boolean isFounded = false;
        while (true) {
            System.out.println(upperBound - lowerBound);
            curIn = (lowerBound + upperBound) / 2 ;
            System.out.println("Is it more than " + array[curIn] + " ?");
            String answer = br.readLine();
            if(lowerBound> upperBound)
            {
                System.out.println(array[curIn]);
                isFounded = true;
            }
            else if(upperBound - lowerBound == 1)
            {
                System.out.println(array[curIn]);
                isFounded = true;


            }
            else if(upperBound - lowerBound == 0)
            {
                System.out.println(array[curIn]);
                isFounded = true;

            }

            else
            {
                if (answer.equals("Yes"))
                {
                    lowerBound = curIn + 1;
                }
                else if (answer.equals("No"))
                {
                    upperBound = curIn - 1;

                }
            }
            if(isFounded)break;
            step++;
        }
    }

}

Please, help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Dorofeev, 2015-11-03
@TechCloud

Thanks to everyone who participated! I found the problem myself, and it was not the initialization of the array.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class Finder {

    public static void main(String[] args) throws IOException {
        int[] array = fillArray();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int elements = array.length;
        int lowerBound = 0;
        int upperBound = elements - 1;
        int curIn;
        int step = 0;
        Set<Integer> integerSet = new HashSet<>();
        while (true) {
            curIn = (lowerBound + upperBound) / 2;
            /*if (lowerBound > upperBound) {
                System.out.println("You are lying!Your number is " + array[curIn]);
                break;
            }*/
           if (integerSet.size() == 1000) {
                System.out.println("Your number is " + array[curIn]);
               System.out.println(String.format("Computer used %s steps",step));
                break;
            } else if (integerSet.size() == 999) {
                System.out.println("Your number is " + array[curIn + 1]);
               System.out.println(String.format("Computer used %s steps",step));
                break;
            }
            System.out.println(integerSet.size());
            System.out.println(String.format("Is it more than %s ?", array[curIn]));
            String answer = br.readLine();

            if (answer.equals("Yes")) {
                lowerBound = curIn + 1;
                for (int i = array[curIn]; i > 0; i--) {
                    integerSet.add(array[i]);
                }

            } else if (answer.equals("No")) {
                upperBound = curIn - 1;
                for (int i = array[curIn]; i < 1000; i++) {
                    integerSet.add(array[i]);
                }

            }

            step++;
        }

    }

    public static int[] fillArray() {
        int[] array = new int[1000];
        for (int i = 0; i < 1000; i++) {
            array[i] = i + 1;
        }
        return array;
    }
}

Clumsy, but I didn't need the perfect code)

E
Evgeny Kornachev, 2015-11-02
@zelan

what immediately catches your eye - the array array is filled with zeros.
Add explicit initialization of array elements.

int[] array = new int[1000];
for (int i = 0; i < array.length; i++) {
    array[i] = i;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question