import java.util.function.IntConsumer;

import java.util.*;

public class GameEngine
{
    public GameEngine (Vector<String> instructions, boolean debug)
    {
        _computer = new Intcode(instructions, null, debug);
        _debug = debug;
        _theScreen = new Screen(_debug);
        _paddlePosition = null;
        _ballPosition = null;
        _stick = new Joystick();
    }

    public final boolean playGame ()
    {
        int[] output = null;
                
        /*
         * Initialise the screen. Run the game instructions
         * once.
         */

         // Replace entry 0 with 2 (unlimited lives)

        _computer.changeInstruction(0, "2");

        while (!_computer.waitingForInput())
        {
            output = getOutput();

            if (_debug)
                System.out.println("Tile information: <"+output[0]+", "+output[1]+"> and "+TileId.idToString(output[2]));

            System.out.println("**initial "+output[0]+" "+output[1]+" "+output[2]);

            Tile theTile = new Tile(new Coordinate(output[0], output[1]), output[2]);

            _theScreen.updateTile(theTile);

            if (theTile.getId() == TileId.BALL)
                _ballPosition = theTile.getPosition();
            else
            {
                if (theTile.getId() == TileId.PADDLE)
                    _paddlePosition = theTile.getPosition();
            }
        }

        /*
         * Where is the joystick?
         */

        if (_ballPosition.getX() > _paddlePosition.getX())
            _stick.setPosition(Joystick.TILTED_RIGHT);
        else
        {
            if (_ballPosition.getX() < _paddlePosition.getX())
                _stick.setPosition(Joystick.TILTED_LEFT);
        }

        System.out.println("**initial joystick "+_stick);

        if (_stick != null)
            return false;

        /*
         * Now let's play the game!
         */

        System.out.println("\n\nPLAYING");

        //_computer = new Intcode(instructions, INITIAL_INPUT, debug);

        while (!_computer.hasHalted())
        {
            _computer.setInput(Integer.toString(_stick.getPosition()));

            output = getOutput();

            if (_debug)
                System.out.println("Tile information: <"+output[0]+", "+output[1]+"> and "+TileId.idToString(output[2]));

            System.out.println("**gaming values "+output[0]+" "+output[1]+" "+output[2]);

            if ((output[0] == -1) && (output[1] == 0))
            {
                // update score

                _theScreen.getSegmentDisplay().setScore(output[2]);

                System.out.println("**score now "+_theScreen.getSegmentDisplay());
            }
            else
            {
                Tile theTile = new Tile(new Coordinate(output[0], output[1]), output[2]);

                _theScreen.updateTile(theTile);

                if(theTile.getId() == TileId.BALL)
                {
                    _ballPosition = theTile.getPosition();

                    if (_ballPosition.getX() > _paddlePosition.getX())
                        _stick.setPosition(Joystick.TILTED_RIGHT);
                    else
                    {
                        if (_ballPosition.getX() < _paddlePosition.getX())
                            _stick.setPosition(Joystick.TILTED_LEFT);
                        else
                            _stick.setPosition(Joystick.NEUTRAL_POSITION);
                    }

                    System.out.println("**gaming joystick "+_stick);
                }
                else
                {
                    if (theTile.getId() == TileId.PADDLE)
                        _paddlePosition = theTile.getPosition();
                }
            }
        }

        System.out.println("**score "+_theScreen.getSegmentDisplay());

        return true;
    }

    public final int getNumberOfBlocks ()
    {
        return _theScreen.numberOfBlocks();
    }

    private final int[] getOutput ()
    {
        int[] values = new int[3];

        System.out.println("**got "+_computer.hasPaused()+" "+_computer.hasOutput());

        while (!_computer.hasPaused() && !_computer.hasOutput())
        {
            _computer.singleStepExecution();
        }

        if (!_computer.hasPaused())
        {
            values[0] = Integer.parseInt(_computer.getOutput());

            while (!_computer.hasPaused() && !_computer.hasOutput())
            {
                _computer.singleStepExecution();
            }

            if (!_computer.hasPaused())
            {
                values[1] = Integer.parseInt(_computer.getOutput());

                while (!_computer.hasPaused() && !_computer.hasOutput())
                {
                    _computer.singleStepExecution();
                }

                values[2] = Integer.parseInt(_computer.getOutput());
            }
            else
            {
                System.out.println("Error - computer halted after outputing x value!");
            }
        }

        return values;
    }

    private Intcode _computer;
    private boolean _debug;
    private Screen _theScreen;
    private Coordinate _paddlePosition;
    private Coordinate _ballPosition;
    private Joystick _stick;
}
@@ -1,44 +1,4 @@
-import java.util.function.IntConsumer;%0A%0A
 impo
@@ -677,32 +677,117 @@
 ut())%0A        %7B%0A
+            System.out.println(%22**waiting for input %22+_computer.waitingForInput());%0A%0A
             outp
@@ -4149,64 +4149,11 @@
     
-while (!_computer.hasPaused() && !_computer.hasOutput())
+do 
 %0A   
@@ -4213,28 +4213,22 @@
        %7D
-%0A%0A        if
+ while
  (!_comp
@@ -4247,173 +4247,182 @@
 ed()
-)%0A        %7B%0A            values%5B0%5D = Integer.parseInt(_computer.getOutput());%0A%0A            while (!_computer.hasPaused() && !_computer.hasOutput())%0A            %7B%0A    
+ && !_computer.hasOutput());%0A%0A        System.out.println(%22**1 here %22+_computer.status());%0A%0A        values%5B0%5D = Integer.parseInt(_computer.getOutput());%0A%0A        do%0A        %7B%0A
     
@@ -4474,29 +4474,15 @@
     
-    %7D%0A%0A            if
+%7D while
  (!_
@@ -4505,34 +4505,107 @@
 ed()
-)%0A            %7B%0A  
+ && !_computer.hasOutput());%0A%0A        System.out.println(%22**2 here %22+_computer.status());%0A%0A
         
     
@@ -4596,30 +4596,24 @@
 );%0A%0A        
-      
 values%5B1%5D = 
@@ -4666,96 +4666,21 @@
     
-        while (!_computer.hasPaused() && !_computer.hasOutput())%0A                %7B%0A     
+do%0A        %7B%0A
     
@@ -4679,35 +4679,32 @@
   %7B%0A            
-   
 _computer.single
@@ -4732,64 +4732,44 @@
     
-        %7D%0A%0A                values%5B2%5D = Integer.parseInt(
+%7D while (!_computer.hasPaused() && !
 _com
@@ -4766,35 +4766,35 @@
 ) && !_computer.
-get
+has
 Output());%0A     
@@ -4792,164 +4792,130 @@
 ));%0A
-            %7D%0A            else%0A            %7B%0A                System.out.println(%22Error - computer halted after outputing x value!%22);%0A            %7D%0A        %7D
+%0A        System.out.println(%22**3 here %22+_computer.status());%0A%0A        values%5B2%5D = Integer.parseInt(_computer.getOutput());
 %0A%0A  

Update GameEngine.java