Minimax with Alpha-beta pruning, getting the result - actionscript-3

I have followed the pseducode on the wikipedia article, and I think I got it working. However, it returns the score, and that doesn't exactly help when I want to know what move I want to make.
I tried what I think would be a way to get the best move, but I don't think it is working as when I actually try to play against it (chess), the AI makes somewhat retarded moves with a depth level of 3.
Here is my function:
public static function alphaBeta(node, depth, alph, beta, team, tellTheMove:Boolean = false):* {
var pointer:ChessMove;
if (depth == 0) {
return scoreOf(node);
}
var childrenOf:Vector.<ChessMove > = returnPossibleMoves(node,team);
if (childrenOf.length == 0) {
return scoreOf(node);
}
if (team == 0) {
for (var i in childrenOf) {
var that:Number = alphaBeta(childrenOf[i],depth - 1,alph,beta,1);
if(tellTheMove){
}
if (that > alph) {
alph = that;
if(tellTheMove){
pointer = childrenOf[i];
}
}
if (beta <= alph) {
break;
}
}
if(tellTheMove){
return pointer; //Returns the move that's score last exceeded alpha.
}
return alph;
} else {
for (var j in childrenOf) {
var that2:Number = alphaBeta(childrenOf[j],depth - 1,alph,beta,0);
if (that2 < beta) {
beta = that2;
}
if (beta <= alph) {
break;
}
}
return beta;
}
}

Depth 3 is very little for a problem like chess. At this depth most of the power depends on your final evaluation function. This evaluation function is very hard to do in way that it can predict the value of the board efficiently.
Try something simpler, which can be solved efficiently at a lower depth. Tic-Tac-Toe is a very good game for a first attempt at Min-Max. This is because the final outcome is well known. If you get your algorithm correctly you should not be able to beat it at all. If you do Tic-Tac-Toe and the algorithm is loosing, you know that you have a mistake.
Also note that in some cases Min-Max plays optimal, but still will look retarded to a human opponent. For example if there is no chance at winning, Min-Max will start to play randomly and do very dumb moves. This is the case , because Min-Max expects the opponent to also play perfect, which is usually not the case with humans. There are some simple changes that can be done to the algorithm to change this behavior and have min-max play "less retarded" in such cases.

Related

Nape Moving Platform

Okay Im relatively new to nape and Im in the process of making a game, I've made a Body called platform of type KINEMATIC, and I simply want to move it back a forth in a certain range on the stage. Can somebody please see where im going wrong , thanks.
private function enterFrameHandler(ev:Event):void
{
if (movingPlatform.position.x <= 150 )
{
movingPlatform.position.x += 10;
}
if (movingPlatform.position.x >= 260)
{
movingPlatform.velocity.x -= 10;
}
}
First of in one of the if blocks you are incrementing position.x by 10 in the other one you are decrementing velocity.x by 10. I guess you meant position.x in both.
Secondly, imagine movingPlatform.position.x is 150 and your enterFrameHandler runs once. movingPlatform.position.x will become 160 and on the next time enterFrameHandler is called none of the if blocks will execute since 160 is neither less than or equal to 150 or greater than or equal to 260.
You can use the velocity to indicate the side its moving and invert it once you go beyond an edge, something like :
// assuming velocity is (1,0)
private function enterFrameHandler(ev:Event):void {
if (movingPlatform.position.x <= 150 || movingPlatform.position.x >= 260) {
movingPlatform.velocity.x = -movingPlatform.velocity.x;
}
movingPlatform.position.x += movingPlatform.velocity.x;
}
Obviously this might cause problems if the object is already at let's say x=100, it will just keep inverting it's velocity, so either make sure you place it between 150-260 or add additional checks to prevent it from inverting it's direction more than once.
This might be a better way of doing it :
// assuming velocity is (1,0)
private function enterFrameHandler(ev:Event):void {
if (movingPlatform.position.x <= 150) {
movingPlatform.velocity.x = 1;
} else if (movingPlatform.position.x >= 260) {
movingPlatform.velocity.x = -1;
}
movingPlatform.position.x += movingPlatform.velocity.x;
}
In general:
Kinematic bodies are supposed to be moved solely with velocity, if you change their position directly then they are not really moving as much as they are 'teleporting' and as far as the physics is concerned their velocity is still exactly 0 so things like collisions and friction will not work as you might expect.
If you want to still work with positions instead of velocities, then there's the method setVelocityFromTarget on the Body class which is designed for kinematics:
body.setVelocityFromTarget(targetPosition, targetRotation, deltaTime);
where deltaTime is the time step you're about to use in the following call to space.step();
All this is really doing is setting an appropriate velocity and angularVel based on the current position/rotation, the target position/rotation and the amount of time it should take to get there.

ActionScript 3.0 HitTestObject on an Array

Good afternoon everyone!
I have a small problem in ActionScript 3.0 with hitTestObject.
I would like to chechk if my character hits a platform (I'm making a simple platform game.).
I have a platform object exproted for action script , and i add childs frim this to an array.
Until this point everything goes well, i can put them on stage etc.
I have written a cycle to chechk if my caharacter hits the platform but it doesn't works correctly. My character falls throught the first platforms and only stop's falling when it hits the last platform. (So for the last in the array it works well.)
And now here is this part from my code, i hope someone can help me with it. :)
import flash.events.Event;
import flash.geom.Rectangle;
stop();
var vy:Number=0;
var gv:Number=1;
var sebesseg:Number=4;
var jumped:Boolean=false;
var stay:Boolean=false;
var level:Array=new Array ;
var gravity:Number=2;
var velocity:Number=1.1;
var platform0:MovieClip=new platform ;
level.push(addChild(platform0));
level[0].x=200;
level[0].y=450;
var platform1:MovieClip=new platform ;
level.push(addChild(platform1));
level[1].x=700;
level[1].y=650;
var platform2:MovieClip=new platform ;
level.push(addChild(platform2));
level[2].x=1000;
level[2].y=800;
stage.addEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
function cameraFollowCharacter(evt:Event) {
root.scrollRect=new Rectangle(PORK1_mc.x-(stage.stageWidth/2)+320,PORK1_mc.y-(stage.stageHeight/2)-50,stage.stageWidth,stage.stageHeight);
}
stage.addEventListener(KeyboardEvent.KEY_DOWN,gomb);
function gomb(k:KeyboardEvent):void {
trace(k);
if (k.keyCode==37) {
if (sebesseg==2) {
sebesseg=sebesseg+0;
} else {
sebesseg-=1;
}
} else if (k.keyCode==39) {
sebesseg+=1;
} else if (k.keyCode==Keyboard.ESCAPE) {
stop();
root.scrollRect=new Rectangle (stage.x,stage.y,stage.stageWidth,stage.stageHeight);
gotoAndStop(2);
} else if (k.keyCode==Keyboard.F1 && stay==false) {
stage.frameRate=0;
stay=true;
} else if (k.keyCode==Keyboard.F1 && stay==true) {
stage.frameRate=24;
stay=false;
}
}
stage.addEventListener(Event.ENTER_FRAME, megy);
function megy(e:Event):void {
PORK1_mc.x+=sebesseg;
gravity*=velocity;
PORK1_mc.y+=gravity;
//trace(velocity);
}
THIS PART IS THE PROBLEM, HITTESOBJECT ONLY WORKS FOR THE LAST PLATFORM
PORK1_mc.hitPork_mc this is my character (hitPork_mc is an invisible rectangle for better HitTestObject. So as i wrote my char. falls throught the platfroms until the last one, he falls on tha last and stops falling, so for the last platform in the array it works perfectly.
I wouldn't like to change a lot on my code, only on the hitTest part if its possible.
this.addEventListener(Event.ENTER_FRAME, handleEnterFrame);
function handleEnterFrame(e:Event):void {
for (var i:int = 0; i < level.length; i++) {
if (level[i].hitTestObject(PORK1_mc.hitPork_mc)) {
velocity=0;
} else {
velocity=1.1;
gravity=4;
}
}
}
Thanks for every help in advance!
With your original code, you do the following for each platform:
If there is a collision, set velocity to zero.
If not, set velocity to 1.1.
Since you do this in order, here's what happens when you collide with platform 1 but not 2 or 3:
The collision with 1 sets your velocity to zero.
The collision with 2 sets your velocity to 1.1.
The collision with 3 sets your velocity to 1.1.
Since these happen all in a row in the same frame, the end result is that your character never stops unless the collision is with platform 3.
By returning after setting velocity to zero, you are breaking out of the loop, preventing your collision work from being undone. What you are doing will work, but you can also do it another way which is less concise but may make more sense:
function handleEnterFrame(e:Event):void
{
var collided:Boolean = false; //This will record if you collided or not.
for (var i:int = 0; i < level.length; i++)
{
if (level[i].hitTestObject(PORK1_mc.hitPork_mc))
{
collided = true;
}
}
if(collided) velocity = 0;
else
{
velocity=1.1;
gravity=4;
}
}
Note that here we're not applying velocity and gravity inside the for loop because we don't want it to happen for each object we might collide with. Instead, we set it once, after we've determined if we collide with any objects.
This also helps show that you are setting gravity every frame you don't collide even though there doesn't seem to be any point at which you set it to zero; I suspect this is something you want to fix.

Collision Check

I'm trying to test for collision between bullets within a vector and enemies within another vector. I can access the data fine, but the problem lies in the actual detection of the collision. Funny thing is, it works perfectly when I use hitTestObject, so I don't see why this shouldn't be working. I might be overlooking something, but I'm having a hard time finding it.
Code:
for each(var i in eManager.enemyArray)
{
for each(var j in gManager.gunVector)
{
for each (var k in j.bManager.bulletVector)
{
// Basically using Pythagorean's theorem but with both sides squared
// to minimize any process-heavy operations
if(((i.x - k.x)*(i.x - k.x))+((i.y - k.y)*(i.y - k.y)) <= 4)
{
// Note that when this happens, the enemy dies
i.kill = true;
}
}
}
}
Whoops. Turns out that I was testing the collision between 2 points, not between a point and a shape. My mistake.

Do I need a closure to make this TweenLite example work as desired?

I seem to recall something about pass-by-reference and the Greensock TweenLite class. It's the only thing I can think of that is causing this bit of code to be not working as I intend:
for (var i = 0; i < 10; ++i) {
addItem();
}
public function addItem():MovieClip {
var item:MovieClip = getNewItem();
item.y = item.height / 2;
var newPosY = _origHeight - _items.length * item.height;
_items.push(item);
new TweenLite(item, ITEM_DROP_TIME, { y: newPosY } ); trace(newPosY);
addChild(item);
}
The trace is outputting the values I expect: an incremental sequence where each number is greater than the last (by the height of the item). However, what I see visually is that all the items end up at the same location (as if newPosY were the same for all instances of the tween.)
The only thing that comes to mind is that newPosY is being passed by reference, so each instance of the tween is actually referencing the very same value. Am I missing something? Or do I need some kind of closure to isolate the scope of my tween property's value?
EDIT
Suggesting this question be closed as I guess the answer is "No" and the issue was elsewhere in my code. I doubt that any further exposition of the problem would be relevant to others. Thanks to those who responded!
I think your TweenLite syntax is wrong. Try:
TweenLite.to (item, ITEM_DROP_TIME, { y: newPosY } );

Ideas for jumping in 2D with Actionscript 3 [included attempt]

So, I'm working on the basics of Actionscript 3; making games and such.
I designed a little space where everything is based on location of boundaries, using pixel-by-pixel movement, etc.
So far, my guy can push a box around, and stops when running into the border, or when try to the push the box when it's against the border.
So, next, I wanted to make it so when I bumped into the other box, it shot forward; a small jump sideways.
I attempted to use this (foolishly) at first:
// When right and left borders collide.
if( (box1.x + box1.width/2) == (box2.x - box2.width/2) ) {
// Nine times through
for (var a:int = 1; a < 10; a++) {
// Adds 1, 2, 3, 4, 5, 4, 3, 2, 1.
if (a <= 5) {
box2.x += a; }
else {
box2.x += a - (a - 5)*2 } } }
Though, using this in the function I had for the movement (constantly checking for keys up, etc) does this all at once.
Where should I start going about a frame-by-frame movement like that? Further more, it's not actually frames in the scene, just in the movement.
This is a massive pile of garbage, I apologize, but any help would be appreciated.
try doing something like: (note ev.target is the box that you assigned the listener to)
var boxJumpDistance:Number = 0;
function jumpBox(ev:Event){
if (boxJumpDistance<= 5) {
ev.target.x += boxJumpDistance; }
else if(boxJumpDistance<=10){
ev.target.x += boxJumpDistance - (boxJumpDistance - 5)*2
}
else{
boxJumpDistance = 0;
ev.target.removeEventListener(Event.ENTER_FRAME, jumpBox);
}
}
then instead of running the loop, just add a listener:
box2.addEventListener(Event.ENTER_FRAME, jumpBox);
although this at the moment only works for a single box at a time (as it is only using one tracking variable for the speed), what you would really want to do is have that function internally to the box class, but im unsure how your structure goes. the other option would be to make an array for the boxes movement perhaps? loop through the array every frame. boxesMoveArray[1] >=5 for box 1, etc.