Hello,
to solve the calculation bug of 999 + 1000/2 = 999 I preferred to use two methods of Math: Ceiling and Floor, instead of adding 1 to the guess variable in Start method
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NumberWizard : MonoBehaviour
{
static int Max = 1000;
static int Min = 1;
int guess = CalculateGuess();
// Start is called before the first frame update
void Start()
{
Debug.Log("Welcome to Number Wizard");
Debug.Log("Pick a number");
Debug.Log("The highest number you can pick is " + Max);
Debug.Log("The lowest number you can pick is " + Min);
Question();
Debug.Log("Push Up = Higher; Push Down = Lower; Push Return = Correct");
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Debug.Log("up arrow key is held down");
Min = guess;
guess = CalculateGuess("ceil");
Question();
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
Debug.Log("down arrow key is held down");
Max = guess;
guess = CalculateGuess();
Question();
}
else if (Input.GetKeyDown(KeyCode.Return))
{
Debug.Log("return key is held down");
}
}
void Question()
{
Debug.Log("(" + Min + " + " + Max + ") / 2 = " + guess);
Debug.Log("Is your picked number higher or lower than " + guess + "?");
}
static int CalculateGuess(string op = "")
{
if (op == "ceil")
{
return (int)Math.Ceiling((Min + Max) / 2f);
}
else
{
return (int)Math.Floor((Min + Max) / 2f);
}
}
}