Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Assistance with AI
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Beastinonyou




PostPosted: Sat Jan 07, 2012 4:29 pm   Post subject: Assistance with AI

I've been trying to work out my AI for my Battleship game, in which you play against the computer.

I've tried several times, but each has it's flaws in which, sometimes it works as intended, and others it would go out of the intended order.

If someone could help me fix it up a bit, and possibly comment on other non-AI related things.. it's not totally finished yet, for I've been trying to get this AI worked out for quite a while, and haven't really continued past it.


It's better to have the entire thing, and it's fairly lengthy, so I'm attaching it. The Area for AI is within the procedure for the computer's turn.


thanks in advance,



Battleship.rar
 Description:
Battleship_t.t is the more recent, with the regular Battleship.t being an older, but similar approach

Download
 Filename:  Battleship.rar
 Filesize:  1.37 MB
 Downloaded:  606 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Sat Jan 07, 2012 4:42 pm   Post subject: RE:Assistance with AI

I think it would be best if you explained your approach. What algorithm/rules does it use? Is there a difficulty level? I'll try to help, but it would be nice to have a bit of background first. Just the basic idea
Beastinonyou




PostPosted: Sat Jan 07, 2012 4:44 pm   Post subject: Re: Assistance with AI

Indeed, I've described the process it should go through in the turing file (Battleship_t.t) near the section of code that needs to be overlooked. It's quite hard to miss if you scroll down. And as far as difficulty level, there is only 1.

also, note that pressing the spacebar will rotate your ships when placing them.

thanks,
Raknarg




PostPosted: Sat Jan 07, 2012 4:55 pm   Post subject: RE:Assistance with AI

Alright. So when you say flaws, what is it doing wrong? Is all you know that it's simply not doing it's job, or do you know what part it messes up on?
Beastinonyou




PostPosted: Sat Jan 07, 2012 5:03 pm   Post subject: Re: RE:Assistance with AI

Raknarg @ Sat Jan 07, 2012 4:55 pm wrote:
Alright. So when you say flaws, what is it doing wrong? Is all you know that it's simply not doing it's job, or do you know what part it messes up on?


well, when testing, sometimes it will work without flaws, in such it will start left, and go through the things described. But other times, it will, say go left twice, then up.. others it will go left being a miss, then skip to the right side or downwards.
the most bizarre seems to be that I've encountered that it has attempted to go in a direction where the spot next to it has been guessed, and it will go in that direction. In such the instance, after an initial hit, it missed left, then skipped upwards, and the spot to the right of the initial hit was previously guessed by the computer, however it guessed in that direction, past the used spot, several times, despite a line in the code that should prevent guessing in that direction.

I've used the debugger and seen the tracer as it entered this if statement, meanwhile, the spot to the right was previously guessed by the computer. It went to guess right for several for turns.
Turing:

if posX + 1 <= 20 and pUsedSpots (posX + 1, posY) = false then   % Ensure that the next guessed spot doesn't exceed grid max, and hasn't been guessed before


you might encounter some if you try running it, placing all your ships, and take turns.. If you want to speed up the testing, change the delay in the main program for the turns... it should be for 1.5 seconds at the moment
Raknarg




PostPosted: Sat Jan 07, 2012 5:23 pm   Post subject: RE:Assistance with AI

Couple questions:
What does conHits do, exactly?
are sPosX and sPosY the origins?
Beastinonyou




PostPosted: Sat Jan 07, 2012 5:30 pm   Post subject: Re: RE:Assistance with AI

Raknarg @ Sat Jan 07, 2012 5:23 pm wrote:
Couple questions:
What does conHits do, exactly?
are sPosX and sPosY the origins?


sPosX and sPosY are the origins, yes, which are used when changing directions, to revert to it's original position.

conHits is short for consecutive hits. It's used as a counter for every consecutive hit in one direction, if there are consecutive hits in one direction, rather than an immediate miss after an initial hit, that means when it reaches the miss in that direction, it should flip to the opposite direction instead of attempting to go upwards, and then when it reaches a miss to the right, it finishes.
Raknarg




PostPosted: Sat Jan 07, 2012 5:32 pm   Post subject: RE:Assistance with AI

Oh i see so it basically tracks if the ship is in a certain direction. That may be your problem then; the issue when it messes up seems to be that it randomly goes in a different direction for no reason. If conHits is a directional controller, that could screw with the program if it's not doing exactly what you want.
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Sat Jan 07, 2012 5:33 pm   Post subject: RE:Assistance with AI

The reason? I don't know yet, I'll look into it.
Beastinonyou




PostPosted: Sat Jan 07, 2012 5:40 pm   Post subject: Re: Assistance with AI

dirGuess is used for the directions.. 1 = left, 2 = up, 3 = right, 4 = down

What i intend conHits to do, is used for determining the end of a ship in a direction. If the computer hits a ship, then the next turn it guesses to the left, if it misses, conHits isn't really used, and the direction should skip to upwards. If it was a hit, conHits is used to determine when to switch to the other side to destroy the ship. when consecutive hits (in a direction) is 0, which is what happens when you miss on a "Streak" sort of speak, the ship must not continue in that direction, and there may possibly be more parts of the ship on the opposite side.
Dreadnought




PostPosted: Sat Jan 07, 2012 7:08 pm   Post subject: Re: Assistance with AI

WOOOHOOO I FOUND IT!
*calms down*

Ok, first off, nice game, it looks and plays very well.

You actually have 2 little typos screwing with your AI as well as another problem I will talk about after.

Turing:
if dirGuess = 1 then
            % Initial direction of left
            if posX - 1 > 0 and pUsedSpots (posX - 1, posY) = false then

% You use pUsedSpots in all directions
 
% Later in code
if cUsedSpots (posX, posY) = false then

% And later
cUsedSpots (posX, posY) := true

% You're using cUsedSpots


Second
Turing:
if dirGuess = 4 then
            % Last direction, Downwards
            if posY - 1 > 0 and pUsedSpots (posX, posY + 1) = false then
%                                                     ^
% This is checking upwards (where the arrow  is)      |


Finally (the other problem), you're using randomize.
Turing:

        randomize
        randint (posX, 1, 20)
        randint (posY, 1, 20)

This randomizes the pseudo-random number sequence that Turing will use (well, it used to do this). The idea is to choose a different sequence each time the program runs, not each time you want random numbers (there may only be 10 sequences in Turing).
I assume your teacher taught you to use this procedure. You shouldn't. It's obsolete, Turing does this on its own each time you run code. This is causing a lot of lag, since Turing is trying to guess a spot it hasn't guess but with limited random number sequences. You may also have noticed that the computer seems to have a pattern to its guesses after a while (kinda like diagonal line of guesses on the screen) this is because you're using randomize. Basically I'm saying you don't need it and shouldn't use it.
Beastinonyou




PostPosted: Sat Jan 07, 2012 8:50 pm   Post subject: Re: Assistance with AI

Dreadnought @ Sat Jan 07, 2012 7:08 pm wrote:

Ok, first off, nice game, it looks and plays very well.

You actually have 2 little typos screwing with your AI as well as another problem I will talk about after.

Finally (the other problem), you're using randomize.

This randomizes the pseudo-random number sequence that Turing will use (well, it used to do this). The idea is to choose a different sequence each time the program runs, not each time you want random numbers (there may only be 10 sequences in Turing).
I assume your teacher taught you to use this procedure. You shouldn't. It's obsolete, Turing does this on its own each time you run code. This is causing a lot of lag, since Turing is trying to guess a spot it hasn't guess but with limited random number sequences. You may also have noticed that the computer seems to have a pattern to its guesses after a while (kinda like diagonal line of guesses on the screen) this is because you're using randomize. Basically I'm saying you don't need it and shouldn't use it.


thanks a bunch... It works great now, no errors I've encountered. stupid little typo's...

and yes, we were taught to use randomize when we need random numbers.. not necessarily every time we call a random number, but at least once in the program.
Beastinonyou




PostPosted: Sat Jan 07, 2012 11:24 pm   Post subject: Re: Assistance with AI

hmm...

Now that the AI is working fairly well, I'm trying to get the detecting destroyed ships to work.

I've easily found a way to track player ships destroyed by the computer (i think it's best option) through the AI. However, it's more difficult to determine when a player destroys a computer ship.

How would I go about properly tracking destroyed computer ships by the player?
Dreadnought




PostPosted: Sun Jan 08, 2012 1:22 pm   Post subject: Re: Assistance with AI

Beastinonyou wrote:
How would I go about properly tracking destroyed computer ships by the player?

Well if all you care about is checking if the player won, the easiest way I can think of, is to track the number of times the player hit a computer ship. The ships are 5, 4, 3, 3 and 2 squares long, so if the player has hit the enemy ships 17 times he must have won.

If you want to know at any time which ships are destroyed, that's tougher. Personally, the way I would do it, is give each ship a different tile value (I think ships are all a value of 2 in your grid). Once you have a different tile value for each ship, just use a variable to keep track of how many times each ship has been hit.

In my opinion tracking the number of hits is the easiest way to track destroyed ships.
Beastinonyou




PostPosted: Mon Jan 09, 2012 7:03 am   Post subject: Re: Assistance with AI

Thanks for your approach... I've already solved it quite easily.

By storing initial x and y coordinates on player and computer placed ships, as well as the direction in which it is, i can use some repetitive structures to see whether all spots in it's traveling direction have been hit, respectively to it's length, wherever it may be on the grids.

I'll be posting it later, after I comment the rest of it out.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 15 Posts ]
Jump to:   


Style:  
Search: