
-----------------------------------
Beastinonyou
Sat Jan 07, 2012 4:29 pm

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,

-----------------------------------
Raknarg
Sat Jan 07, 2012 4:42 pm

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
Sat Jan 07, 2012 4:44 pm

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
Sat Jan 07, 2012 4:55 pm

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
Sat Jan 07, 2012 5:03 pm

Re: 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?

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.

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
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.
        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
Sat Jan 07, 2012 8:50 pm

Re: Assistance with AI
-----------------------------------

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
Sat Jan 07, 2012 11:24 pm

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
Sun Jan 08, 2012 1:22 pm

Re: Assistance with AI
-----------------------------------
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
Mon Jan 09, 2012 7:03 am

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.
