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
Last edited: