Jump to content



0

Inprove AI


4 replies to this topic

#1 Primordial Ooze OFFLINE  

Primordial Ooze

    Dragonstomper

  • 504 posts
  • Quacker Blaster Lead Programmer
  • Location:United States of America

Posted Thu May 1, 2008 7:41 AM

I am almost done with my game of pong and am having a bit of trouble on my computers ai. Here is my code:
   rem Open Source Pong
   rem an Open Source Pong Remake
   rem Updated: April 11, 2008
   rem Website: http://opensourcepong.freepgs.com

   rem setup all the game variables
   
   rem set the rom size
   set romsize 2k
   
   rem turn on smartbranching
   set smartbranching on

   rem define the number of player scores
   const playerscores = 2
   
   rem define noscores constant
   const noscore = 1
   
   rem set kernel options
   rem readpaddle - set it so the paddle controlers are read
   set kernel_options no_blank_lines readpaddle

   rem set an alias for the ball's x velocity
   dim ballvx = a

   rem set an alias for the ball's y velocity
   dim ballvy = b

   rem set an alias to determine if sound effects
   rem should be playing
   dim sfxplaying = c

   rem set an alias to keep track of how many frames the
   rem sound effects have played
   dim sfxtimer = d

   rem set an alias to hold wither this is a two player game
   dim vsplayer = e
   
   rem color the background green
   COLUBK = 198

   rem set the color of the playfield to white
   COLUPF = 14

   rem set player 2's score color to his sprite color
   player0scorecolor = $1C

   rem set player 1's score color to his sprite color
   player1scorecolor = $8C

   rem clear the playfield
   pfclear

   rem draw the top border of the playfield
   pfhline 0 0 31 on

   rem draw the bottom border of the playfield
   pfhline 0 11 31 on

   rem enable the "High Definition" Playfield
   pfscroll upup : pfscroll upup

   rem define player 1's sprite
   player0:
   %00011000
   %00011000
   %00011000
   %00011000
   %00011000
   %00011000
   %00011000
   %00011000
end

   rem define player 2's sprite
   player1:
   %00011000
   %00011000
   %00011000
   %00011000
   %00011000
   %00011000
   %00011000
   %00011000
end

   rem finished seting up the game variables
   
   rem start a new game and reset everything that was changed during
   rem the course of the game
startNewGame
   rem set player 1's inital x positon
   player0x = 140

   rem set player 1's inital y positon
   player0y = 45
   
   rem set player2's inital x positon
   player1x = 15

   rem set player2's inital y positon
   player1y = 45

   rem set ball's inital x positon
   ballx = 80

   rem set ball's inital y positon
   bally = 45

   rem if the left switch is on a then this is a two player game
   if !switchleftb then vsplayer = 1 else vsplayer = 0

   rem randomly choose the balls x velocity
   temp1 = rand

   rem if the number is less then 128 then the balls x velocity is 1
   rem otherwise the balls x velocity is -1(or -1)
   if temp1 < 128 then ballvx = 1 else ballvx = 255
   
   rem randomly choose the balls y velocity
   temp2 = rand

   rem if the number is less then 128 then the balls y velocity is 1
   rem otherwise the balls y velocity is 255(or -1)
   if temp2 < 128 then ballvy = 1 else ballvy = 255
   
   
   rem if the right difficulty switch is set to a set
   rem give player 1 a handicap by setting player 2's 
   rem score to 3, player 1 & 2's score to 0 
   if !switchrightb then player0score = $03 : player1score = $00 else player0score = $00 : player1score = $00
   
   rem reset the sound playing boolean to 0
   sfxplaying = 0
   
   rem reset the sound effects timer
   sfxtimer = 0

   rem turn off all sound effects from the previous game
   AUDV0 = 0
   
   rem finished reseting all the game variables changed during
   rem the course of the game
   
   rem start of the game loop
gameLoop
   rem if the user presses the reset switch, start a new game
   if switchreset then startNewGame

pauseloop
   rem color player 1's paddle
   COLUP0 = 140

   rem color player 2's paddle
   COLUP1 = 28

   rem if the colorswitch is in the black/white position pause
   rem the game and stop any sound effects already playing
   if switchbw then drawscreen : AUDV0 = 0: goto pauseloop
   
   rem read paddle 0, use it to position player 1 across the screen
   rem if this is a two player game, alternate the paddle reading
   if vsplayer = 0 then firstpaddle
   
   rem this alternates paddles per frame
   currentpaddle=currentpaddle ^ 1
   
   rem if currentpaddle equals 2 then position the second paddle
   if currentpaddle = 1 then secondpaddle

firstpaddle
  
   rem read paddle 0, use it to position player 1's paddle across the screen
   currentpaddle=0

   rem draw the game screen
   drawscreen
  
   rem set player1's y position to the paddle control's axis
   rem plus 8
   player0y = paddle + 8
  
   rem make sure player 1 doesn't go off the top screen
   if player0y < 12 then player0y = 12
  
   rem make sure player 1 doesn't go off the bottom screen
   if player0y > 83 then player0y = 83
   goto computerai

secondpaddle  
   rem if the 2 player mode is activated then paddle 1 controls the
   rem second paddle
   
   rem read paddle 1, use it to position player 2's paddle across the screen
   currentpaddle = 1
   
   rem draw the game screen
   drawscreen
   
   rem set player 2's y position to the paddle 2's control's axis
   rem plus 8
   player1y = paddle + 8
   
computerai
   rem if the left switch is on b then this is a
   rem one player game, so set player 2's y position
   rem to the balls y position
   if vsplayer = 0 then player1y = bally + 1

   rem make sure player 2 doesn't go off the top screen
   if player1y < 12 then player1y = 12

   rem make sure player 2 doesn't go off the bottom screen
   if player1y > 83 then player1y = 83

   rem move the ball
   ballx = ballx + ballvx
   bally = bally + ballvy

   rem make sure the ball doesn't go off the top screen
   if bally < 5 then ballvy = -ballvy

   rem make sure the ball doesn't go off the bottom screen
   if bally > 81 then ballvy = -ballvy

   rem if the ball collides with player 1's paddle
   rem play the pure tone with a frequency of 4 and volume of 10
   rem move the ball a bit so it doesn't stick
   rem and reverse its x velocity
   if collision(player0, ball) then sfxplaying = 1 : AUDC0 = 4 : AUDF0 = 31 : AUDV0 = 10 : ballx = ballx - 3 : ballvx = 0 - ballvx

   rem if the ball collides with player 2's paddle
   rem play the pure tone with a frequency of 5 and volume of 10
   rem move the ball a bit so it doesn't stick
   rem and reverse its x velocity
   if collision(player1, ball) then sfxplaying = 1 : AUDC0 = 4 : AUDF0 = 31 : AUDV0 = 10 : ballx = ballx + 3 : ballvx = 0 - ballvx

   rem if the ball goes past player 1's paddle
   rem increase player 2's score by 1
   rem play the score sound
   rem have the player serve the ball
   rem reverse the balls x velocity
   if ballx > 160 then player0score = addbcd(player0score,1) : sfxplaying = 1 : AUDC0 = 10 : AUDF0 = 9 : AUDV0 = 10 : ballx = player0x - 3 : bally = player0y - 3 : ballvx = 0 - ballvx

   rem if the ball goes past the computer paddle
   rem increase player 1's score by 1
   rem play the score sound
   rem have the computer serve the ball
   rem reverse the balls x velocity
   if ballx < 1 then player1score = addbcd(player1score,1) : sfxplaying = 1 : AUDC0 = 10 : AUDF0 = 9 : AUDV0 = 10 : ballx = player1x + 6 : bally = player1y - 3 : ballvx = 0 - ballvx

   rem if the a effect is playing update the sound timer
   if sfxplaying = 1 then sfxtimer = sfxtimer + 1

   rem if the sound effect timer is 60 frames, stop the sound effect
   if sfxtimer = 30 then sfxplaying = 0 : sfxtimer = 0 : AUDV0 = 0

   rem if player 1 or player 2 scores 11 points then stop all sound effects
   rem and then its gameover
   if player0score > $10 || player1score > $10 then sfxplaying = 0 : sfxtimer = 0 : AUDV0 = 0 : goto gameover

   goto gameLoop

   rem end of the game loop code, restart the game loop

   rem code for handling when either players score  reaches 11
   rem and its gameover
gameover
   rem if the user presses the reset switch, start a new game
   if switchreset then startNewGame
   
   rem color player 1's paddle
   COLUP0 = 140

   rem color player 2's paddle
   COLUP1 = 28   

   rem draw the screen
   drawscreen

   rem if this is a one or two player game and player 1 
   rem presses the fire button, start a new game
   if joy0right then startNewGame

   rem if this is a two player game and player 2 
   rem presses the fire button, start a new game
   if vsplayer <> 0 && joy0left then startNewGame

   rem otherwise just keep looping
   goto gameover

   rem inline bcd math helper
   inline bcd_math.asm

   rem inline for player scores multikernel
   inline playerscores.asm

The code for my computers ai is:
computerai
   rem if the left switch is on b then this is a
   rem one player game, so set player 2's y position
   rem to the balls y position
   if vsplayer = 0 then player1y = bally + 1

Interesting enough if i change it to:
computerai
   rem if the left switch is on b then this is a
   rem one player game, so set player 2's y position
   rem to the balls y position
   if vsplayer = 0 then player1y = bally

Then my computer becomes dumber, but if i change it to:

computerai
   rem if the left switch is on b then this is a
   rem one player game, so set player 2's y position
   rem to the balls y position
   if vsplayer = 0 then player1y = bally + 2

I get the perfect computer player. I have tried to use a float number like so:
computerai
   rem if the left switch is on b then this is a
   rem one player game, so set player 2's y position
   rem to the balls y position
   if vsplayer = 0 then player1y = bally + 1.2

To make the computer smarter, but as it seems the Atari 2600/bataribasic doesn't like them or doesn't know how to use them.

How can i improve my computers players ai, noting that i only have 92 bytes of rom space left?

Sincerely,

Open Source Pong

Edited by Open Source Pong, Thu May 1, 2008 8:17 AM.


#2 Fort Apocalypse OFFLINE  

Fort Apocalypse

    Stargunner

  • 1,593 posts

Posted Thu May 1, 2008 5:30 PM

a couple of things...

* you can attach your source as a file to your post instead of including the whole program each time if you'd like. You can also remove a file from the post by clicking on the X next to it.
* bB does handle decimals but you have to define the variable it goes into like:

dim mynumber = a.b
(so it uses up two variables)

it is a little tricky to use decimals-
* to set, for example use mynumber = 1.5
* to compare, you can compare to an integer (if I remember correctly) like mynumber > 15

there is probably a much better topic somewhere on this

* for your ai, I would just add to ai player y based on the position of the ball instead of setting it to equal some multiple of the ball's y. how you decide when to add/subtract to ai player y is up to you.

#3 Primordial Ooze OFFLINE  

Primordial Ooze

    Dragonstomper

  • 504 posts
  • Quacker Blaster Lead Programmer
  • Location:United States of America

Posted Thu May 1, 2008 5:39 PM

Currently the ai is the computer paddle's y position is equal to the balls y position minus one. By subtracting 2 instead of one the computer paddle's y position is equal to the balls y position hence, why the computer player would be perfect.

Quote

for your ai, I would just add to ai player y based on the position of the ball instead of setting it to equal some multiple of the ball's y. how you decide when to add/subtract to ai player y is up to you.

Could you explain this a little better, i don't understand what you mean.

Sincerely,

Open Source Pong

Edited by Open Source Pong, Thu May 1, 2008 5:40 PM.


#4 MausGames OFFLINE  

MausGames

    Dragonstomper

  • 851 posts
  • Location:MO, USA

Posted Thu May 1, 2008 7:18 PM

you can do something like this:

if level = 1 then b = 16
if level = 2 then b = 12 (etc)
a = rand
if a < b then goto NoMove
if player1y < bally then player1y = player1y - 1 else player1y = player1y + 1
NoMove

The rand part is just one example of how to manipulate the computer's response based on the value of "level", there are a number of ways that you can handicap the computer player.

Edited by MausGames, Thu May 1, 2008 7:19 PM.


#5 Primordial Ooze OFFLINE  

Primordial Ooze

    Dragonstomper

  • 504 posts
  • Quacker Blaster Lead Programmer
  • Location:United States of America

Posted Wed May 7, 2008 3:48 PM

I have tried inproving the AI, but for some reason the computer remains dopy no matter how intelligent i make it. This time i will only provide the new code, everything else stays the same.
firstpaddle
  
   rem read paddle 0, use it to position player 1's paddle across the screen
   currentpaddle=0

   rem draw the game screen
   drawscreen
  
   rem set player1's y position to the paddle control's axis
   rem plus 8
   player0y = paddle + 8
  
   rem make sure player 1 doesn't go off the top screen
   if player0y < 12 then player0y = 12
  
   rem make sure player 1 doesn't go off the bottom screen
   if player0y > 83 then player0y = 83
   goto computerai

secondpaddle  
   rem if the 2 player mode is activated then paddle 1 controls the
   rem second paddle
   
   rem read paddle 1, use it to position player 2's paddle across the screen
   currentpaddle = 1
   
   rem draw the game screen
   drawscreen
   
   rem set player 2's y position to the paddle 2's control's axis
   rem plus 8
   player1y = paddle + 8
   
   goto skipcomputerai
   
computerai
   rem if the left switch is on b then this is a
   rem one player game, so move player 2's y position
   rem based on the the balls' y position relate to
   rem the player 2's y position
   
   rem if the ball is above the player 2's paddle move
   rem player 2's paddle up
   if bally > player1y then player1y = player1y + 2
   
   rem if the ball is below the player 2's paddle move
   rem player 2's paddle down
   if bally < player1y then player1y = player1y - 2
   
   rem if the ball is equal to player 2's paddle
   rem don't move player 2's paddle
   if bally = player1y then player1y = player1y + 0
   
skipcomputerai   

   rem make sure player 2 doesn't go off the top screen
   if player1y < 12 then player1y = 12

   rem make sure player 2 doesn't go off the bottom screen
   if player1y > 83 then player1y = 83

Any ideas?

Sincerely,

Open Source Pong




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users