Advertisement
Guest User

Untitled

a guest
Feb 17th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. import random
  2.  
  3. gameboard = []
  4.  
  5. threeShip = []
  6. twoshipcoords = []
  7. oneshipcoord = []
  8.  
  9. for x in range(5):
  10. gameboard.append(["O"] * 5)
  11.  
  12. battleships = gameboard
  13.  
  14. def print_board(board):
  15. for row in board:
  16. print(" ".join(row))
  17.  
  18. def random_row(board):
  19. return random.randint(0, len(board) - 1)
  20.  
  21.  
  22. def random_col(board):
  23. return random.randint(0, len(board[0]) - 1)
  24.  
  25. def place_oneship(board): #place a one "peg" ship
  26. x = random_row(board)
  27. y = random_col(board)
  28. battleships[y][x] = "B"
  29. oneshipcoord.append([x, y])
  30.  
  31. def place_twoship(board):
  32.  
  33. twoship = 0 #how many "pegs" the ship has.
  34. while twoship == 0:
  35.  
  36. #x = random_row(board)
  37. #y = random_col(board)
  38. x = 2
  39. y = 4
  40.  
  41.  
  42. if board[y][x] != "B": #check if the spot is empty for a ship
  43. board[y][x] = "B"
  44. twoship = 1
  45. twoshipcoords.append([x, y])
  46.  
  47. while twoship == 1: #Start checking if surrounding areas are clear for the ship to expand one space
  48. #Checks for direction, then make sure index isn't out of bound.
  49.  
  50. direction = "N"#random.choice("NSEW") #Code by Kevin below from Stackoverflow, thank you
  51. print(direction)
  52. if direction == "N" and x + 1 < 5 and y - 1 >=0:
  53. if board[y-1][x] != "B":
  54. board[y-1][x] = "B"
  55. twoship = 2
  56. twoshipcoords.append([x, y - 1])
  57. elif direction == "S"and x - 1 >= 0 and y + 1 <5:
  58. if board[y+1][x] != "B":
  59. board[y+1][x] = "B"
  60. twoship = 2
  61. twoshipcoords.append([x , y + 1])
  62. elif direction == "E"and y <5 and x + 1 <5:
  63. if board[y][x+1] != "B":
  64. board[y][x+1] = "B"
  65. twoship = 2
  66. twoshipcoords.append([x + 1 , y])
  67. elif direction == "W" and y - 1 >=0:
  68. if board[y][x-1] != "B":
  69. board[y][x-1] = "B"
  70. twoship = 2
  71. twoshipcoords.append([x - 1 , y])
  72.  
  73.  
  74. place_oneship(battleships)
  75. place_twoship(battleships)
  76. print(twoshipcoords)
  77.  
  78. print_board(battleships)
  79. print ("Let's play Battleship!")
  80. print_board(gameboard)
  81.  
  82. for turn in range(4):
  83. guess_row = int(input("Guess Row: "))
  84. guess_col = int(input("Guess Col:"))
  85. guess_point =[guess_row, guess_col]
  86.  
  87. if guess_point == "H" or guess_point == "X":
  88. print("That spot has already been chosen")
  89.  
  90. for coords in zip(twoshipcoords[:], oneshipcoord[:]):
  91. if guess_point == coords:
  92. gameboard[guess_col][guess_row] = "H"
  93. twoshipcoords.remove(coords)
  94. break
  95. else:
  96. print("You missed my battleship!")
  97. gameboard[guess_col][guess_row] = "X"
  98.  
  99. print_board(gameboard)
  100. print(twoshipcoords)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement