Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- board = []
- def boardmaker(board):
- rows = int(input("How many rows? "))
- cols = int(input("How many columns? "))
- for x in range(rows):
- board.append("O" * cols)
- return board
- def print_board(board):
- for x in range(len(board)):
- print(" ".join(board[x]))
- #shipgetter will take user inputs to create a number of ships for the game
- def shipgetter(board):
- #Here we caluculate the area of the game board to check if there's enough room for the ships
- area = len(board) * len(board[0])
- carriers = 0
- battleships = 0
- submarines = 0
- destroyers = 0
- patrols = 0
- print(area)
- counter = 0
- while counter == 0:
- if len(board) >= 5 or len(board[0]) >= 5:
- carriers = int(input("How many aircraft carriers (5 length)? "))
- if len(board) >= 4 or len(board[0]) >= 4:
- battleships = int(input("How many battleships (4 length)? "))
- if len(board) >= 3 or len(board[0]) >= 3:
- submarines = int(input("How many submarines (3 length)? "))
- if len(board) >= 2 or len(board[0]) >= 2:
- destroyers = int(input("How many destroyers (2 length)? "))
- if len(board) >= 1 or len(board[0]) >= 1:
- patrols = int(input("How many patrol boats (1 length)? "))
- if ((carriers * 5) + (battleships * 4) + (submarines * 3) + (destroyers * 2) + patrols) <= area:
- counter = 1
- else:
- print("Sorry, but there is not enough room for all those ships!")
- again = str.lower(input("Would you like to play again? y/n: "))
- if again == "no" or again == "n":
- break
- print(str(carriers) + " carrier(s).")
- print(str(battleships) + " battleship(s).")
- print(str(submarines) + " submarine(s).")
- print(str(destroyers) + " destroyer(s).")
- print(str(patrols) + " patrol boat(s).")
- return(board, carriers, battleships, submarines, destroyers, patrols)
- def boardchecker(answerboard, shiplen, vert, randrows, randcols):
- row = random.choice(randrows)
- col = random.choice(randcols)
- counter = 0
- if vert == 0:
- while counter < 0:
- if answerboard[row[col]] == "O":
- answerboard[row[col]] = "X"
- col += 1
- counter += 1
- else:
- return 0
- if vert == 1:
- while counter < 0:
- if answerboard[row[col]] == "O":
- answerboard[row[col]] + "X"
- row += 1
- counter += 1
- else:
- return 0
- return answerboard
- def shipmaker(board, crr, btl, sub, dst, ptr):
- randrows = []
- randcols = []
- shiplist = [[crr, 5], [btl, 4], [sub, 3], [dst, 2], [ptr, 1]]
- print(shiplist)
- answerboard = board
- #shiptotal is the total number of all ships on the board
- shiptotal = (crr + btl + sub + dst + ptr)
- #When vert = 0, ships will be made horizontally. If vert = 1, ships are made vertically
- vert = 0
- for x in range(len(board)):
- randrows.append(x)
- for x in range(len(board[0])):
- randcols.append(x)
- print(randrows)
- print(randcols)
- while shiptotal > 0:
- for x in range(len(shiplist)):
- count = 0
- while shiplist[x[0]]> 0:
- while count == 0:
- if boardchecker(answerboard, shiplist[x[1]], randint(0, 1), randrows, randcols) != 0:
- count += 1
- shiplist[x[0]] -= 1
- shiptotal -= 1
- print(answerboard)
- boardmaker(board)
- print_board(board)
- board, crr, btl, sub, dst, ptr = shipgetter(board)
- print(str(crr) + " carrier(s).")
- print(str(btl) + " battleship(s).")
- print(str(sub) + " submarine(s).")
- print(str(dst) + " destroyer(s).")
- print(str(ptr) + " patrol boat(s).")
- shipmaker(board, crr, btl, sub, dst, ptr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement