The robot plays ball
  How to program the ball
  One robot plays ball
  How to program several robots
  Basket-ball game
How to get the ball

* In this chapter, you are going to write a program to make the robot find the ball and throw it. First it is necessary to know how to make the robot go to a tile, the one where the ball is. (Ball programming is intoduced in next paragraph).

* So you are going to build a subroutine named GoToTileXY to make the robot go to the tile with coodinates (x, y). x and y are two variables set to proper values before the subroutine call.
While the robot is not on tile x, y, w'll move it towards x,y. This will be done by means of another subroutine named GoTowardsXY and a While loop

* Before beginning programming :
- Open a new program
- Select level 5
- Choose the ground Ground9x9.bog with the Ground menu.

* GoTowardsXY subroutine
If the robot is oriented towards x, y it moves one tile ahead in order to be closer from x, y, else it rotates by 90° to be better oriented.
- Create a new subroutine with the name GoTowardsXY
- Build the following flowchart :


Logical conditions to test the robot direction :

dyRobot=0 and (x-xRobot)*dxRobot > 0 :
The condition dyRobot = 0 is true if the robot direction is parallel to x axis. In such a case dxRobot value is 1 if the robot is facing the right of the ground and - 1 if it is facing the left of the ground.
The value of x - xRobot represents the move to be done along x axis, if it is positive the robot has to move towards right, if it is negative the robot has to move towards left.
So if the values of x - xRobot and dxRobot are both positive or negative, the product of those values is positive and the robot is in the good direction.

The condition dxRobot=0 and (y-yRobot)*dyRobot > 0 allows to test the robot direction along y axis. It can be explained in the same way

- Click the button "Verify flowchart" to ensure your flowchart is correct.

*Subroutine GoToTileXY
- Create a new subroutine called GoToTileXY
- Build the following flowchart :


* The While loop block has the same shape as the For loop block. The loop header contains the instruction While beginning with the keyword While followed by a logical condition (as in test block)
If the logical condition result is true, the flowchart inside the loop body is executed , then the Wend instruction makes the execution to continue at the block header, the logical condtion is again evaluated, etc ...
If the logical condition result is false, the loop body is not executed, the execution continues at the block following the loop

* In subroutine GoToTileXY, the loop body, that is the subroutine GoTowardsXY, is executed while the condition xRobot <> x or yRobot <> y returns the value true ; xRobot <> x is a logical expression comparing the values of xRobot and x, its result is true if the values of xRobot and x are different ; the expression yRobot <> y is true if yRobot is different from y ; so, the condition xRobot <> x or yRobot <> y is true if the robot isn't on tile x,y.

* Testing subroutine GoToTileXY :
- In main program, build the following flowchart.
- Run the program and verify the robot is on tile 2, 7 when execution ends
- Run again the program with a different initial position or direction of the robot
- Run again the program for diffferent values of x and y.

How to program the ball

*The ball is displayed on the ground only if the program contains a keyword related to the ball.
When program execution begins, the ball is randomly put on an empty tile.

* The keywords xBall and yBall give the ball position on the ground.
For instance you can test if the robot is on the same tile as the ball with the condition :
xRobot = xBall and yRobot = yBall

* Ball commands : GetBall, DropBall, ThrowBall
Those commands are to be written inside editable command blocks :


GetBall : if the robot is on the same tile as the ball, it takes the ball.
DropBall : the robot drops the ball on the tile it occupies.
ThrowBall : the robot throw the ball three tiles ahead. If the ball goes out of the ground, it is automatically thrown again at random into an empty tile.

One robot plays ball

* Before beginning :
- Choose Configuration > Level menu and select level 6 in order to use the ball

* Now you are going to write a program to make the robot find the ball and throw it.
To find the ball, use two variables x and y, set their values to xBall and yBall and execute the subroutine GoToTileXY
- replace main program with the following one :


  - run the program and enjoy...
How to program several robots

* At level 6, it is possible to execute several robot programs at the same time on the same ground.
Each robot is associated with a program window.
For the levels under level 6, when you open or create a new program, the current program is closed : there is only one open program at a time. At level 6, when you open or create a program, the previously opened programs remain open.

* The open programs are parts of the current project. You can see a list of the project programs by opening the project window with the Window > Project menu. With this window, you can display or hide or remove programs. At level 6, when you close a program window, the program remains in the project and is still executed even if it is not displayed. If you don't want the program to be executed you have to remove it from the project.

* At level 6, it is possible to make the robots play games together. Here the robots will play basket-ball game.
- Choose Configuration > Choose a game menu
- Select basket-ball game
- Click button Game rules to see the rules
- Click OK to choose the selected game.
You can verify that the purpose of the program is now "Throw the ball into the basket" : this is the game purpose, it can't be changed while a game is selected. The ground can't either be modified during a game. You can see the game ground by displaying the execution window. The basket is the central tile, its is surrounded by walls.

Basket-ball game
  * Run the previous program
- We've got a problem ! In some cases, the robot crashes against a wall in the ground center, we have to change the GoTowardsXY subroutine which was not taking care of walls in front of the robot.
- Change GoTowardsXY subroutine : replace the MoveForward block by a test allowing the robot to avoid a wall ahead.

- run the program : it's better, the robot doesn't crash against walls, but there is still a problem : it throws the ball anywhere in the ground !

* To score a point, the robot has to throw the ball into the basket. When it throws the ball, the ball goes three tiles ahead, so before throwing the ball, the robot has to go to a position three tiles away from the basket, for instance to the tile 2,5, then it has to turn itself in direction of the basket.
- change the main program :

  - run the program, now the robot scores points....

* Basket game with several robots :
- To have two robots playing together, you can save your program and then duplicate the file and open the duplicate file.
- When you run the game, you may encounter additional problems as the robots may crash one against the other. So you have to check if a another is ahead with the keyword TileAheadOccupied .- Another problem is that when a robot holds the ball, the ball coordinates change when the robot moves. So you have to modifiy the part of the program where the robot find the ball.