title: B-ship game follow up 2: AI and User IDK how to do alternating turns
body: <p>From <a href="https://stackoverflow.com/questions/28512486/b-ship-game-xs-and-os-are-just-screwing-up-python">this</a>  </p>
  
  <p>I have been making a b-ship game but I have run into a problem. It is 3x3, so it's a scaled down version of the actual game. We're using one ship for now.</p>
  
  <p>I currently have the user hitting the AI section done, but how can i make it switch to the Ai's turn after one hit?</p>
  
  <p>The AI, when it is his turn, knows that the user has part of a ship at [4] and that will stay that way for now. then He tries to hit [7], but if that doesn't work, try [1], or if that doesn't work, try [5]. And How can I do this for any edge??</p>
  
  <pre><code>import random
  
  def drawboard(hitboard,hitboard2):
      print(' Opponent\'s          Your')
      print('    Ships            Ships')
      print('|   |   |   |    |   |   |   |')
      print('| ' + hitboard[7] + ' | ' + hitboard[8] + ' | ' + hitboard[9] + ' |    | ' + hitboard2[7] + ' | ' + hitboard2[8] + ' | ' + hitboard2[9] + ' |')
      print('|   |   |   |    |   |   |   |')
      print('-------------    -------------')
      print('|   |   |   |    |   |   |   |')
      print('| ' + hitboard[4] + ' | ' + hitboard[5] + ' | ' + hitboard[6] + ' |    | ' + hitboard2[4] + ' | ' + hitboard2[5] + ' | ' + hitboard2[6] + ' |')
      print('|   |   |   |    |   |   |   |')
      print('-------------    -------------')
      print('|   |   |   |    |   |   |   |')
      print('| ' + hitboard[1] + ' | ' + hitboard[2] + ' | ' + hitboard[3] + ' |    | ' + hitboard2[1] + ' | ' + hitboard2[2] + ' | ' + hitboard2[4] + ' |')
      print('|   |   |   |    |   |   |   |')
  def aiships(hitboard,spot_hit,shipspots,hitboard2):
      if spot_hit in shipspots:
          hitboard[1] = 'x'
      else:
          hitboard[7] = 'o'
      drawboard(hitboard,hitboard2)
  
  def playerships(hitboard,hitboard2, spot_hit, usershipspots):
      hitboard2[7] = 'x'
      print("\nComputer's turn.\n")
      spot_hit = random.choice(usershipspots)
      hitboard2[spot_hit] = 'x'
      if spot_hit not in usershipspots:
          hitboard2[spot_hit] = 'o'
      drawboard(hitboard,hitboard2)
  
  def main():
      possiblespots = [[1,2],[2,3],[4,5],[5,6],[7,8],[8,9],[1,4],[4,7],[2,5],[5,8],[3,6],[6,9]]
      shipspots = random.choice(possiblespots)
  
      userspots = [[4,7],[4,1],[4,5]]
      usershipspots = random.choice(userspots)
      gameisplaying = True
      ai_spots = [4, 7, 1, 5]
      ai_index = 0
      while gameisplaying:
          hitboard = [' ' for i in range(10)]
          hitboard2 = [' ' for i in range(10)]
          hitboard2[usershipspots[0]] = 's'
          hitboard2[usershipspots[1]] = 's'
          userready = input('Place your ships. Type done when you finished placing it.')
          while not userready == 'done':
              userready = input('Type done when you locate your ship.  ')
          spot_hit = input('Where\'s the hit?: 1-9  ')
          while not (spot_hit in '1 2 3 4 5 6 7 8 9'.split()):
              spot_hit = input ('Please tell me where the hit is: 1-9  ')
          spot_hit = int(spot_hit)
          aiships(hitboard, spot_hit, shipspots, hitboard2)
          playerships(hitboard, hitboard2, ai_spots[ai_index], shipspots)
          ai_index += 1
  
  main()
  </code></pre>
  
  <p>But wait, there's more! (billy mays reference)</p>
  
  <p>No matter what number I put in, there will always be an O in the 7 space. Unless I put in the ship coordinates of the playerships (which is quite odd) which will create an X in the 1 space. Also there will always be an 's' in the 3 space on the 'your ships' board. (using numpad for numbers)</p>