# Share My CreationObject Oriented Pong - Full Source

My A.I. algorithm:
B4X:
``````'The zero plus below is for code beautification purposes only. :)
Dim stress     = 1 - (Abs(x - ball.PosX) / court.width) As Double
Dim confidence = 0 + (Abs(y - ball.PosY) / getHeight  ) As Double
Dim fitness    = If_ (ball.PosY < y, -lv, lv)           As Double
vY = Abs(ball.VelocityY * 3)
y  = y + (vY * stress * confidence * fitness)

'If the ball is above the paddle center, the fitness level will be the negative value of LV, in order to make the paddle go up.
'The paddle will otherwise stop or go down, again depending on the ball's vertical position.``````

Maximum velocity:
- The A.I.'s maximum velocity (vY) is always 3x greater than the ball's maximum velocity.

Stress:
- Represents the stress amount felt by the A.I. given how far the ball is, horizontally.
- Greater values represent high stress levels, hence increasing the movement velocity.

Confidence (lack of):
- The level of A.I. "confidence" that it will hit the ball, given by the difference between the ball's vertical position and the A.I.'s vertical position.
- Greater values represent low confidence, hence increasing the movement velocity.

Fitness Level:
- Given by the LV variable, in percentage (normalized).

Inline IF function:
B4X:
``````Private Sub If_(expression As Boolean, if_true As Double, if_false As Double) As Double
If expression Then Return if_true
Return if_false
End Sub``````

Enjoy!

#### Attachments

• Pong.jar
334.2 KB · Views: 286
• pong.zip
4.9 KB · Views: 329
Last edited:

#### inakigarm

##### Well-Known Member
AI player2 is demoniac !! Great job and code --very clean and tidy !

#### Erel

Staff member
@Erel, move to game dev?
No. This is the place to share creations. You can make a post in the game development forum and link to this one.

Replies
5
Views
214
B4J Tutorial H2 Database
Replies
13
Views
808
Replies
18
Views
5K
Replies
0
Views
967
Replies
0
Views
51K