Small problem with Battleship AI

This is a discussion on Small problem with Battleship AI within the Game Programming forums, part of the General Programming Boards category; Code: aiMove::aiBomb() { // 13 == Miss; // 11 == Already fire apon; m_oldRandY = m_randY; m_oldRandX = m_randX; whichBoard->bombCoords(randX, ...

  1. #1
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    Small problem with Battleship AI

    Code:
    aiMove::aiBomb()
    {
    	// 13 == Miss;
    	// 11 == Already fire apon;
    
    	m_oldRandY = m_randY;
    	m_oldRandX = m_randX;
    	whichBoard->bombCoords(randX, randY);
    	do 
    	{
    		short int bombCoord = whichBoard->bombCoords(m_randX, m_randY);
    		if (!lastMoveHit)
    		{
    
    			m_randY = 1 + rand() % (m_xCoord - 1);
    			m_randX = 1 + rand() % (m_yCoord - 1);
    			short int bombCoord = whichBoard->bombCoords(randX, randY);
    			if (bombCoord != 13 && bombCoord != 11 && bombCoord => 10)
    			{
    				m_lastMoveHit = 1;
    			} else {
    				m_lastMoveHit = 0;
    			}
    
    		} else {
    
    	// If we hit something last time, but didn't sink it
    	// we need to check around that position..
    	// Gawd this hurts my head..
    
    		}
    
    
    	} while (bombCoord == 11);

    Where you see the comments, I need to implement some system of checking around the location of the ship.. I don't really require code.. as that would require me to post most of my OO Battleship game (~900 lines)

    I can almost get it.. but I've put this project aside for a week because of this difficulty; so I guess it's time to ask for help.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    If last move (x,y) a hit

    4 possible locations
    (x-1,y), (x+1,y) , (x,y-1) , (x,y+1)
    need to test each until get 2 hits on ship.

    if (two_hits or more) first (x,y) second (x+1,y) to (x+n,y)
    (two possible outcomes after two hits)

    sunk ship -> start again at random

    or

    two possible locations
    (x-1,y) and (x+n+1,y)
    (may have previously tested these so need to keep track of 'missed' shots)

    maybe recursive function is the answer here?

    Also need to record missed shots and locations that it is not possible to have a ship (eg less than minimum remaining ship length apart) This should be tested before the hitting function called and if not a valid point, a new loction generated and tested.

    Sort of........
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User JTtheCPPgod's Avatar
    Join Date
    Dec 2001
    Posts
    44
    Wow, 900 lines for battleship without any ai?
    jeez, post the exe or source, i wanna see this.
    "No! I must have my delicious cupcakes, my sweet cakey treasures, piping hot from their 40 watt WOMB!!!"
    --Captain Murphy

  4. #4
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    I can make a 900 line hello world program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small Problem with double and integer adding?
    By Nathan the noob in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2009, 05:16 PM
  2. Visual C++ small problem
    By gadu in forum C++ Programming
    Replies: 0
    Last Post: 03-10-2009, 11:45 PM
  3. Small problem with this array...
    By Merholtz in forum C Programming
    Replies: 7
    Last Post: 11-03-2008, 04:16 PM
  4. Help with a small problem (beginner)
    By piffo in forum C Programming
    Replies: 13
    Last Post: 09-29-2008, 05:37 PM
  5. a very small problem, i'm new... please help?
    By Uberverse in forum C++ Programming
    Replies: 9
    Last Post: 11-10-2007, 10:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21