# My AI Is Invincible :( [SOLVED]

This topic is 2906 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am making a pong game :)
But my AI is too hard :(
Here is my code: it is being called every frame
- (void)stepAI {	//check which direction	if (delta.y > 0) {		//ball is going toward me		int compY = pComp.y-8;		int ballY = pBall.y-8;		int time =  (compY-ballY/*distance*/)/delta.y;//divided by speed		int latX =  time*delta.x+pBall.x; //no wall		//bounce		if (latX > [self getWidth]) {			latX -= [self getWidth];			latX = [self getWidth]-latX;		}		latX = abs(latX);		if (abs(latX - pComp.x) >= ESPEED) {			NSLog(@"%i",latX);			if (latX < pComp.x) {				pComp.x -= ESPEED;			} else {				pComp.x += ESPEED;			}		} else {			pComp.x = latX;		}	} else {		int latX = wSize.width/2;		if (abs(latX - pComp.x) >= ECENTER) {			NSLog(@"%i",latX);			if (latX < pComp.x) {				pComp.x -= ECENTER;			} else {				pComp.x += ECENTER;			}		} else {			pComp.x = latX;		}			}}

VARIABLE EXPLANATION
delta = speed
pComp, pBall: points of the center of the ball and computer
wSize: view size
Good enough :P
EDIT: This is my first post
EDIT:
latX: later x :P
ESPEED: enemy speed
ECENTER: Speed to move toward center (set to -1 currently, still too hard)

[Edited by - firefly431 on December 29, 2010 1:54:03 PM]

##### Share on other sites
A few of your available options:

A. When the ball is moving towards the AI paddle:

1. The AI calculates exactly where it will hit and moves there with some speed. This AI is only beatable if the speed is not fast enough for the paddle to move there from its current position in the time it takes for the ball to move across the screen. To make it easier: decrease the paddle speed or increase the ball speed.

2. Follow the ball: The AI tries to have the center of the paddle at the same Y as the ball center. This is generally much easier to beat, as the AI can follow the ball to one edge and then miss it after a bounce.

B. When the ball is moving away from the AI paddle:

1. Stand still.

3. Stay at the center. If combined with perfect calculation, it is unbeatable unless the ball is fast enough to move across the screen in less than the time it takes for the AI to move its paddle from the middle to the edge of the screen. Also, if it should be beatable, then if the AI speed is constant every ball aimed perfectly at the edge will score and that's also the only way to score, so it doesn't make for too fun matches.

I would recommend a combination, for example:
When the ball is coming towards the AI, follow the ball when it's on the player's half of the screen, and move towards the calculated collision point when it's on the AI's side.
When the ball is moving away from the AI, stand still or move to the center while it's on the AI's side, and follow it when it's on the player's side.

##### Share on other sites
@InnocuousFox:
How would I make it do that without it jiggling? (I'm calling it every frame)
@Erik:
That is what I am doing but I don't want to make the AI too easy... (and the player/computer side thing seems to limit the intelligence)

Oh and could you give me code examples?

##### Share on other sites
Shouldn't be too easy. You can calibrate it by changing the distance to the ball where it switches to perfect AI as well as the speed of the ball and the AI's paddle. So if you treat the entire playing field as the "AI's side", then you have the perfect AI. If you treat the entire field as the player's side, then you have a very easy AI. So you can choose a distance that is reasonable.
if(ball on AI's side) {	if(ball moving towards AI)		return perfectAI();	else		return moveTowardsCenter();}else	return followBall();

##### Share on other sites
Quote:
 Original post by Erik RufeltShouldn't be too easy. You can calibrate it by changing the distance to the ball where it switches to perfect AI as well as the speed of the ball and the AI's paddle. So if you treat the entire playing field as the "AI's side", then you have the perfect AI. If you treat the entire field as the player's side, then you have a very easy AI. So you can choose a distance that is reasonable.*** Source Snippet Removed ***

BTW this is objective-c
So i could do:

- (void)stepAI {	//check which direction	if (delta.y > 0) {		if (pBall.y > AIBORDER) {			[self perfectAI];		} else {			[self AIFollow];		}	} else {		int latX = wSize.width/2;		if (abs(latX - pComp.x) >= ECENTER) {			NSLog(@"%i",latX);			if (latX < pComp.x) {				pComp.x -= ECENTER;			} else {				pComp.x += ECENTER;			}		} else {			pComp.x = latX;		}			}}- (void)perfectAI {	//ball is going toward me	int compY = pComp.y-8;	int ballY = pBall.y-8;	int time =  (compY-ballY/*distance*/)/delta.y;//divided by speed	int latX =  time*delta.x+pBall.x; //no wall	//bounce	if (latX > [self getWidth]) {		latX -= [self getWidth];		latX = [self getWidth]-latX;	}	latX = abs(latX);	if (abs(latX - pComp.x) >= ESPEED) {		NSLog(@"%i",latX);		if (latX < pComp.x) {			pComp.x -= ESPEED;		} else {			pComp.x += ESPEED;		}	} else {		pComp.x = latX;	}}- (void)AIFollow {	if (abs(pBall.x - pComp.x) >= ESPEED) {		if (pBall.x < pComp.x) {			pComp.x -= ESPEED;		} else {			pComp.x += ESPEED;		}	} else {		pComp.x = pBall.x;	}}

EDIT: Hard to find a perfect border...
EDIT: Player having 120 pixels and computer having whatever's left works well
EDIT 115 works best
EDIT: 115 is too hard, 120

[Edited by - firefly431 on December 28, 2010 8:11:22 PM]

##### Share on other sites
I assume you have a point system.
Implement method, such that the computer has two modes of play, or two behaviors.

First, normal default, 'easy' mode
Second, 'points lag accounting mode' where if computer is lagging behind too much in scores, it decides to be smarter for a number of plays. that number of smart plays could be randomized. You have the smart mode down in your post. Get The 'dumbed down' mode implemented.

##### Share on other sites
I already solved it... but then it would be impossible to win anyway, as my original AI is invincible

##### Share on other sites
What I did in mine was to make the AI go at 3 different speed levels based on the distance from the ball. When the ball was very far, or on the other player's side, it would move very slowly if at all. When the player entered the AI's field, it would already go faster. If the ball had gone through half of the field, then the AI would speed up to catch it in time. It lead to some fun matches even though the scoring points were all the same, but that can be solved by introducing some random factors, like for example variating the distance to which the AI should speed up.

##### Share on other sites
Yep. You found places to put random noise. After all, that's the major defining factor between computers and humans... we aren't perfect. Therefore, simulating imperfection is the best bang-for-buck in creating AI.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633709
• Total Posts
3013481
×