Professional Documents
Culture Documents
print("\nwelcome to battleships!\n")
print("Both players have a 10x10 board filled with randomly positioned ships,
neither players know where")
print("the ships are and have to guess where the other persons ships are unitl they
are all destroyed.\n\n")
p1Board = []
p2Board = []
p1ships = {}
p2ships = {}
def printBoard(board):
temp1 = []
for stuff in range(len(board)):
temp1.append(board[stuff])
temp = []
for i in range(len(temp1[0])):
temp.append(str((i+1)))
temp1.insert(0, temp)
for i in range(len(temp1)):
if i < 10:
temp1[i].insert(0, str(i) + " ")
else:
temp1[i].insert(0, str(i))
for row in temp1:
print(" ".join(row))
def listShips(ships):
for x, y in ships.items():
print(x, y)
def LoseCondition(ships):
shipnum = 0
for ship in ships:
shipnum += 1
if shipnum == 0:
return True
StillGame = True
while StillGame:
counter += 1
if counter % 2 == 0:
turn = 0
inverse = 1
name = player1
iname = player2
else:
turn = 1
inverse = 0
name = player2
iname = player1
if LoseCondition(allItems[turn][0]):
print("Oh no {0} you have lost all 15 of your ships and {1} has won! How
could you let this happen!")
break
print("Attack: {0} :: {1} This is your board and where you have
attacked".format(counter-1, name))
print("{0} has {1} ships Left!\n".format(iname, len(allItems[inverse][0])))
sleep(1)
printBoard(allItems[turn][1])
print("\n")
#listShips(allItems[inverse][0])
var1 = True
attac = []
attac.append(int(input("Please enter row number(1 - 10): ")) - 1)
attac.append(int(input("Please enter column number(1 - 10): ")) - 1)
if 0 <= attac[0] <= 9 and 0 <= attac[1] <= 9:
pass
else:
print("You have to enter a number between 1 and 10")
attac = []
attac.append(int(input("Please enter row number(1 - 10): ")) - 1)
attac.append(int(input("Please enter column number(1 - 10): ")) - 1)
editBoard(turn, inverse, attac)