Qbasicnews.com

Full Version: Pong AI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5
I said it wasn't bad Rattra... Big Grin

Jay:
FOR THE LAST TIME it worked fine on my XP!!! :evil:
Quote:I said it wasn't bad Rattra... Big Grin

Jay:
FOR THE LAST TIME it worked fine on my XP!!! :evil:

I know, what i do not get is why it worked better on a slow computer than a fas one..what DOS emulator do you have.
XP's DOS.
My computer an't slow. What processing power do you have?
Smile Here is my extodinary simple AI, but it has that human edgyness in the players.. they pause, go too fast, ect,.. Please note this is in FB, which means it doesn't require checks for text that goes off screen..

It my run in QB minus (SLEEP 100) with no errs, but I'm sure the paddles go off screen a few times in their frantic moving.. Ball stays on screen mind..

Also has 3point paddles, minus they rarely work under the comps checks,. :wink:

Code:
'THIS WAS DONE IN FREEBASIC!
'With that siad,..
'Enjoy!

DECLARE SUB game(point1, point2)

SCREEN 13
PRINT "Pong AI system by Kevin(x.t.r.GRAPHICS)"
PRINT "Simulated to varible human reaction times.."
PRINT
PRINT "Press any key to continue...."
SLEEP
CLS
y1 = 1: y2 = 1
px = 20: py = 12
pxv = 1: pyv = 1
DO
   'Clear Screen
   LINE (0,0)-(320, 200), 0, BF
   'Set Score:
   LOCATE 1, 5: PRINT "C1:"; point1; ", C2:";point2
   'Set paddle 1
   LOCATE y1, 2: PRINT "|"
   LOCATE y1 + 1, 2: PRINT "|"
   LOCATE y1 + 2, 2: PRINT "|"
   'Set paddle 2
   LOCATE y2, 39: PRINT "|"
   LOCATE y2 + 1, 39: PRINT "|"
   LOCATE y2 + 2, 39: PRINT "|"
   'Set ball
   LOCATE py, px: PRINT "O"
   'Buffer Screen
   WAIT &H3DA, 8
   'Move Ball
   px = px + pxv
   py = py + pyv
   'Check Ball Wall
   IF px < 1 THEN point2 = point2 + 1: px = 20: pxv = 1: pyv = 1: y1 = 1: y2 = 1: CALL game(point1, point2)
   IF px > 39 THEN point1 = point1 + 1: px = 20: pxv = -1:pyv = 1: y1 = 1: y2 = 1: CALL game(point1, point2)
   IF py < 1 THEN pyv = 2: py = 1
   IF py > 22 THEN pyv = -2: py = 22
   'Check Ball Paddle
   'Paddle 1
   'IF py > y1 - 1 AND py < y1 + 3 AND px = 3 THEN pyv = -2: pxv = -1
   IF py = y1 AND px < 3 THEN pyv = -1: pxv = 1
   IF py = y1 + 1 AND px < 3 THEN pyv = 0: pxv = 1
   IF py = y1 + 2 AND px < 3 THEN pyv = 1: pxv = 1
   'Paddle 2
   IF py = y1 AND px > 38 THEN pyv = -1: pxv = -1
   IF py = y1 + 1 AND px > 38 THEN pyv = 0: pxv = -1
   IF py = y1 + 2 AND px > 38 THEN pyv = 1: pxv = -1
   'Paddle Reaction
   'Paddle1
   yv1 = INT(RND * 3) + 1
   IF py > y1 + 1 THEN y1 = y1 + yv1
   IF py < y1 + 1 THEN y1 = y1 - yv1
   IF py <> y1 AND yv1 = 3 THEN y1 = y1 + 1
   IF py <> y1 AND yv1 = 1 THEN y1 = y1 - 1

   'Paddle2
   yv2 = INT(RND * 3) + 1
   IF py > y2 + 1 THEN y2 = y2 + yv2
   IF py < y2 + 1 THEN y2 = y2 - yv2
   IF py <> y2 AND yv2 = 3 THEN y2 = y2 + 1
   IF py <> y2 AND yv2 = 1 THEN y2 = y2 - 1
   SLEEP 100
LOOP UNTIL INKEY$ = CHR$(27)

SUB game(point1, point2)
   IF point1 > 9 THEN
      IF point1 > point2 THEN CLS: PRINT "C1 WINS!!": SLEEP: END
   END IF
   IF point2 > 9 THEN
      IF point2 > point1 THEN CLS: PRINT "C2 WINS!!": SLEEP: END
   END IF
END SUB

Enjoy!! Big Grin
Quote:XP's DOS.
My computer an't slow. What processing power do you have?

I tried it again and it worked fine.
the computer does occationally start slow i have to stop one thing from working to get it too work at its normal speed...the thing stopped running at start up for a while so i assumed my dad had stopped it permantly, looks like this was not the case. I ran it again AFTER checking what was running and found/stopped the thing which slows it down and it runs fine.
That's good, you like it?
Try the first one.

If you want you can make it into a game. Big Grin
Just don't make it crappy. :wink:
Here are some ideas to include ontop of pong to make the game a bit more challenging. It will remove the need for "error" to be emulated (pretend AI with absolutely no learning).

1. Make the paddle velocity slow enough for even a perfect algorithm to intercept the ball.

2. Allow the paddles to be positioned at different angles; this may be a continuous angle, or a discrete angle. Either way, there is opportunity here for the program to actually -learn- rather than be programmed what to do.

The agent playing the game should be aware of at least 2 variables: the angle of the enemies paddle and the distance from itself to the ball (no sense of direction).

You could then use a basic artificial neural network (a perceptron would suffice, actually) to control the movement predictions and you could use a really simplistic genetic algorithm to decide which direction to move. Basically you want the bot to learn about the effect of the enemy paddle on the ball's deflection direction.

It might actually be quite fun to change the way pong is played a bit - have the paddles moving at a constant velocity and allow the players to modify the velocity of the paddles (taking into account a maximum speed, of course). When the paddles reach the top/bottom of the screen, they bounce back with elastic deflection, etc.

By adding this much complexity to the game, you could at least start to create a small opponent worthy of being said to "learn". Wink

These ideas would really be better suited for a graphical version of pong rather than textmode.

-shiftLynx
Nice ratrap but its a little jumpy i got the same problem but its qb but both the comps are pritty close in intelegence so they dont make as many mistakes

Code:
RANDOMIZE TIMER
CLS
SCREEN 13
ballx = 160
bally = 100
IF INT(RND * 2000) > 1000 THEN
ballobjx = 1
ELSE
ballobjx = -1
END IF
IF INT(RND * 2000) > 1000 THEN
ballobjy = 1
ELSE
ballobjy = -1
END IF


comp1s = 0
comp2s = 0
comp1 = 100
comp2 = 100
CLS
DO

LOCATE 1, 1: PRINT comp1s; comp2s
LINE (0, comp1 - 25)-(10, comp1 + 25), 0, BF
LINE (310, comp2 - 25)-(320, comp2 + 25), 0, BF


LINE (ballx - 15, bally - 15)-(ballx + 15, bally + 15), 0, BF
IF INKEY$ = CHR$(27) THEN END
CIRCLE (ballx, bally), 5, 7
LINE (5, comp1 - 20)-(5, comp1 + 20)
LINE (315, comp2 - 20)-(315, comp2 + 20)

IF INT(RND * 100) < 80 THEN
IF comp1 >= bally AND ballobjx = -1 THEN comp1 = comp1 - 1
IF comp1 <= bally AND ballobjx = -1 THEN comp1 = comp1 + 1
END IF

IF INT(RND * 100) < 70 THEN
IF comp2 >= bally AND ballobjx = 1 THEN comp2 = comp2 - 1
IF comp2 <= bally AND ballobjx = 1 THEN comp2 = comp2 + 1
END IF


'collision tests
IF bally <= 7 THEN ballobjy = 1
IF bally >= 195 THEN ballobjy = -1     'these two test the top
'next 2 test the paddles
IF ballx = 5 THEN
IF bally >= comp1 - 20 AND bally <= comp1 + 20 THEN ballobjx = -ballobjx
END IF
IF ballx = 315 THEN
IF bally >= comp2 - 20 AND bally <= comp2 + 20 THEN ballobjx = -ballobjx
END IF

IF ballx > 315 THEN

PRINT "Point, Stupid computer"
SLEEP 1
CLS
ballx = 160
bally = 100
comp1s = comp1s + 1
IF INT(RND * 2000) > 1000 THEN
ballobjx = 1
ELSE
ballobjx = -1
END IF
IF INT(RND * 2000) > 1000 THEN
ballobjy = 1
ELSE
ballobjy = -1

END IF

END IF


IF ballx < 5 THEN
PRINT "Point, Smart computer"
SLEEP 1
ballx = 160
bally = 100
comp2s = comp2s + 1
IF INT(RND * 2000) > 1000 THEN
ballobjx = 1
ELSE
ballobjx = -1
END IF
IF INT(RND * 2000) > 1000 THEN
ballobjy = 1
ELSE
ballobjy = -1
END IF


CLS
END IF


ballx = ballx + ballobjx
bally = bally + ballobjy
WAIT &H3DA, 8
WAIT &H3DA, 8, 8
LOOP
:o A lil to jumpy? You just insulted how I play Pong.. Big Grin .. jk

That what I was simulating... :wink: ... Nevious human reactions.. :lol:
yeah but humans moving the paddle will be more smooth like if it moves the wrong way like 2 or 3 times btw i edited it so check it again
Pages: 1 2 3 4 5