How to make the cursor move for the Comp -Tc Tac Toe

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
Anand
Posts: 34
Joined: August 9th, 2012, 1:25 pm
Location: Chennai, India

How to make the cursor move for the Comp -Tc Tac Toe

Post by Anand » August 11th, 2012, 7:47 am

hi Chilli ,
I could manage coding a smart AI for the computer. But i am not able to make the cursor move.
Here is the coding did. I end up crashing the whole program saying ' Unhandled exception at 0x00111949 in Chili DirectX Framework.exe: 0xC0000005: Access violation writing location 0x0542fea8. '.



void Game::DoAITurnRand()
{
AIGetNextMoveRand();
CursorMove();
SetSquareState( AIMove ,activePlayer );
EndTurn();
}

void Game::CursorMove()
{

const int baseX=250;
const int baseY=150;
const int squareSize = 100;

int currentposX=cursorX, currentposY=cursorY, TargetposX = AIMove%3 , TargetposY=AIMove/3;
while(currentposY>TargetposY )
{
cursorY=cursorY-1;
DrawCursor( baseX + cursorX * squareSize,baseY + cursorY * squareSize);
}
while(currentposY<TargetposY )
{
cursorY=cursorY+1;
DrawCursor( baseX + cursorX * squareSize,baseY + cursorY * squareSize);
}
while(currentposX>TargetposX )
{
cursorX=cursorX-1;
DrawCursor( baseX + cursorX * squareSize,baseY + cursorY * squareSize);
}
while(currentposX<TargetposX )
{
cursorX=cursorX+1;
DrawCursor( baseX + cursorX * squareSize,baseY + cursorY * squareSize);
}
}

Please tell me where am i going wrong. And even though if i make this logic correct, i think that the computer would execute those while loops so fast, that i cannot actually see the cursor travel to its destination. Right??
Believe. Become.

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: How to make the cursor move for the Comp -Tc Tac Toe

Post by LuX » August 11th, 2012, 9:49 am

Can you upload the code after cleaning it? Read the READ THIS FIRST post.

The reason why you cant see the cursor moving is not because it's so fast, but because you don't present the frame in between. So basically the loop executes withing a frame, so you won't see it moving at all.
ʕ •ᴥ•ʔ

User avatar
Anand
Posts: 34
Joined: August 9th, 2012, 1:25 pm
Location: Chennai, India

Re: How to make the cursor move for the Comp -Tc Tac Toe

Post by Anand » August 11th, 2012, 10:59 am

Okay. Here is the solution attached. And yes you are right about the loop. Now how do make the cursor move slow. like should i be using sleep() or something like that ??

Thanks.
Attachments
Chili DirectX Framework Tic Tac Toe-vs Smart moving Computer.rar
(35.73 KiB) Downloaded 128 times
Believe. Become.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: How to make the cursor move for the Comp -Tc Tac Toe

Post by chili » August 11th, 2012, 12:41 pm

Sleep will stall the whole program so it's not the best solution.

You need to make it so that the cursor is moved, and then the game just does nothing for 30 or so frames, and then it is moved again. Not sleeping, but just running through the Game::Go function without changing anything on the screen.
Chili

User avatar
Anand
Posts: 34
Joined: August 9th, 2012, 1:25 pm
Location: Chennai, India

Re: How to make the cursor move for the Comp -Tc Tac Toe

Post by Anand » August 11th, 2012, 1:21 pm

I am not able to understand properly. Can you please attach the solution for it.
Believe. Become.

User avatar
Anand
Posts: 34
Joined: August 9th, 2012, 1:25 pm
Location: Chennai, India

Re: How to make the cursor move for the Comp -Tc Tac Toe

Post by Anand » August 11th, 2012, 2:35 pm

well.. now i got the cursor moving to the correct target position. but it moves way too fast. it moves one box every single frame. so its kinda fast. how do i make it move slow..

i have attached my updated coding.
Attachments
Chili DirectX Framework Tic Tac Toe-vs Smart moving Computer.rar
(36.03 KiB) Downloaded 150 times
Believe. Become.

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: How to make the cursor move for the Comp -Tc Tac Toe

Post by LuX » August 11th, 2012, 7:11 pm

You could make a variable "FrameCount" then each frame you add "FrameCount ++;"

Then where ever your movement code is:
"if (FrameCount == 30) /* ~Half a second */ {FrameCount = 0; /* Your movement code */}
ʕ •ᴥ•ʔ

User avatar
Anand
Posts: 34
Joined: August 9th, 2012, 1:25 pm
Location: Chennai, India

Re: How to make the cursor move for the Comp -Tc Tac Toe

Post by Anand » August 12th, 2012, 1:56 pm

Got it now.

Thanks :)
Believe. Become.

Post Reply