Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import sleep
- validlocations1 =['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10','b1','b2','b3','b4','b5','b6','b7','b8','b9','b10','c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','d1','d2','d3','d4','d5','d6','d7','d8','d9','d10','e1','e2','e3','e4','e5','e6','e7','e8','e9','e10','f1','f2','f3','f4','f5','f6','f7','f8','f9','f10','g1','g2','g3','g4','g5','g6','g7','g8','g9','g10','h1','h2','h3','h4','h5','h6','h7','h8','h9','h10','i1','i2','i3','i4','i5','i6','i7','i8','i9','i10','j1','j2','j3','j4','j5','j6','j7','j8','j9','j10']
- validlocations2 =['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10','b1','b2','b3','b4','b5','b6','b7','b8','b9','b10','c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','d1','d2','d3','d4','d5','d6','d7','d8','d9','d10','e1','e2','e3','e4','e5','e6','e7','e8','e9','e10','f1','f2','f3','f4','f5','f6','f7','f8','f9','f10','g1','g2','g3','g4','g5','g6','g7','g8','g9','g10','h1','h2','h3','h4','h5','h6','h7','h8','h9','h10','i1','i2','i3','i4','i5','i6','i7','i8','i9','i10','j1','j2','j3','j4','j5','j6','j7','j8','j9','j10']
- validlocations3 =['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10','b1','b2','b3','b4','b5','b6','b7','b8','b9','b10','c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','d1','d2','d3','d4','d5','d6','d7','d8','d9','d10','e1','e2','e3','e4','e5','e6','e7','e8','e9','e10','f1','f2','f3','f4','f5','f6','f7','f8','f9','f10','g1','g2','g3','g4','g5','g6','g7','g8','g9','g10','h1','h2','h3','h4','h5','h6','h7','h8','h9','h10','i1','i2','i3','i4','i5','i6','i7','i8','i9','i10','j1','j2','j3','j4','j5','j6','j7','j8','j9','j10']
- validlocations4 =['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10','b1','b2','b3','b4','b5','b6','b7','b8','b9','b10','c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','d1','d2','d3','d4','d5','d6','d7','d8','d9','d10','e1','e2','e3','e4','e5','e6','e7','e8','e9','e10','f1','f2','f3','f4','f5','f6','f7','f8','f9','f10','g1','g2','g3','g4','g5','g6','g7','g8','g9','g10','h1','h2','h3','h4','h5','h6','h7','h8','h9','h10','i1','i2','i3','i4','i5','i6','i7','i8','i9','i10','j1','j2','j3','j4','j5','j6','j7','j8','j9','j10']
- player1=input("Player one, input your name: ")
- player2=input("Player two, input your name: ")
- ships = ["destroyer", "submarine", "cruiser", "battleship", "carrier"]
- destroyerlocations1=[]
- sublocations1=[]
- cruiserlocations1=[]
- battleshiplocations1=[]
- carrierlocations1=[]
- destroyerlocations2=[]
- sublocations2=[]
- cruiserlocations2=[]
- battleshiplocations2=[]
- carrierlocations2=[]
- def newlocationup(recentlocation):
- newlocation = list(recentlocation)
- newlocation[-1]=int(newlocation[-1])-1
- newlocation[-1]=str(newlocation[-1])
- newlocation="".join(newlocation)
- return newlocation
- def newlocationdown(recentlocation):
- newlocation = list(recentlocation)
- newlocation[-1]=int(newlocation[-1])+1
- newlocation[-1]=str(newlocation[-1])
- newlocation="".join(newlocation)
- return newlocation
- def newlocationright(recentlocation):
- newlocation=list(recentlocation)
- newlocation[0]= chr(ord(newlocation[0])+1)
- newlocation="".join(newlocation)
- return newlocation
- def newlocationleft(recentlocation):
- newlocation=list(recentlocation)
- newlocation[0]= chr(ord(newlocation[0])-1)
- newlocation="".join(newlocation)
- return newlocation
- def asklocation(player, ship, validlocations):
- print(player, "where would you like your", ship + "?")
- shiplocation = input()
- while shiplocation not in validlocations:
- print('invalid location')
- shiplocation = input()
- return shiplocation
- def askdirection(player, ship, validlocations):
- print("Which direction should your", ship, "face?")
- shipdirection = input()
- while shipdirection != 'up' and shipdirection != 'down' and shipdirection != 'right' and shipdirection != 'left':
- print("Invalid direction.")
- shipdirection = input()
- return shipdirection
- def placingships(player,ship,length,locationslist,validlocations):
- goodlocation=False
- while not goodlocation:
- shiplocation = asklocation(player, ship, validlocations)
- shipdirection = askdirection(player, ship, validlocations)
- shiplocations = [shiplocation]
- if shipdirection == 'up':
- for i in range(1, length):
- shiplocations.append(newlocationup(shiplocations[-1]))
- elif shipdirection == 'down':
- for i in range(1, length):
- shiplocations.append(newlocationdown(shiplocations[-1]))
- elif shipdirection == 'left':
- for i in range(1, length):
- shiplocations.append(newlocationleft(shiplocations[-1]))
- elif shipdirection == 'right':
- for i in range(1, length):
- shiplocations.append(newlocationright(shiplocations[-1]))
- goodlocation=True
- for location in shiplocations:
- if location not in validlocations:
- goodlocation=False
- print(location, "is not valid.")
- for location in shiplocations:
- locationslist.append(location)
- validlocations.remove(location)
- return locationslist
- placingships(player1,ships[0],2,destroyerlocations1,validlocations1)
- print(destroyerlocations1)
- placingships(player1,ships[1],3,sublocations1,validlocations1)
- print(sublocations1)
- placingships(player1,ships[2],3,cruiserlocations1,validlocations1)
- print(cruiserlocations1)
- placingships(player1,ships[3],4,battleshiplocations1,validlocations1)
- print(battleshiplocations1)
- placingships(player1,ships[4],5,carrierlocations1,validlocations1)
- print(carrierlocations1)
- sleep(1)
- print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
- placingships(player2,ships[0],2,destroyerlocations2,validlocations2)
- print(destroyerlocations2)
- placingships(player2,ships[1],3,sublocations2,validlocations2)
- print(sublocations2)
- placingships(player2,ships[2],3,cruiserlocations2,validlocations2)
- print(cruiserlocations2)
- placingships(player2,ships[3],4,battleshiplocations2,validlocations2)
- print(battleshiplocations2)
- placingships(player2,ships[4],5,carrierlocations2,validlocations2)
- print(carrierlocations2)
- print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
- d1=0
- s1=0
- cr1=0
- b1=0
- ca1=0
- d2=0
- s2=0
- cr2=0
- b2=0
- ca2=0
- shots = []
- shots2 = []
- def hitormiss(list1, shot, ship):
- if shot in list1:
- list1.remove(shot)
- print("Hit " + ship + "!")
- def sinkingships(ship, list1):
- print(ship.title(), 'sunk')
- list1.append('donttypethis')
- while True:
- print("\n\nWhere would you like to shoot", player2)
- shot = input()
- while shot in shots or not shot in validlocations3:
- if shot == 'shotlist':
- print(shots.sort())
- print("You have already shot there, or that is an invalid location.")
- shot = input()
- shots.append(shot)
- validlocations3.remove(shot)
- hitormiss(destroyerlocations1, shot, 'destroyer')
- hitormiss(sublocations1, shot, 'submarine')
- hitormiss(cruiserlocations1, shot, 'cruiser')
- hitormiss(battleshiplocations1, shot, 'battleship')
- hitormiss(carrierlocations1, shot, 'carrier')
- if len(destroyerlocations1)==0:
- sinkingships('destroyer',destroyerlocations1)
- d1 = 1
- if len(sublocations1)==0:
- sinkingships('submarine',sublocations1)
- s1 = 1
- if len(cruiserlocations1)==0:
- sinkingships('cruiser',cruiserlocations1)
- cr1 = 1
- if len(battleshiplocations1)==0:
- sinkingships('battleship',battleshiplocations1)
- b1 = 1
- if len(carrierlocations1)==0:
- sinkingships('carrier',carrierlocations1)
- ca1 = 1
- shipsdestroyed=d1+s1+cr1+b1+ca1
- print(player2 + ', you have sunk', shipsdestroyed, 'ships.')
- if shipsdestroyed == 5:
- break
- print("\n\nWhere would you like to shoot", player1)
- shot = input()
- while shot in shots2 or not shot in validlocations4:
- if shot == 'shotlist':
- print(shots2.sort())
- print("You have already shot there, or that is an invalid location.")
- shot = input()
- shots2.append(shot)
- validlocations4.remove(shot)
- hitormiss(destroyerlocations2,shot,'destroyer')
- hitormiss(sublocations2,shot,'submarine')
- hitormiss(cruiserlocations2, shot, 'cruiser')
- hitormiss(battleshiplocations2,shot,'battleship')
- hitormiss(carrierlocations2, shot, 'carrier')
- if len(destroyerlocations2)==0:
- sinkingships('destroyer',destroyerlocations2)
- d2 = 1
- if len(sublocations2)==0:
- sinkingships('submarine',sublocations2)
- s2 = 1
- if len(cruiserlocations2)==0:
- sinkingships('cruiser',cruiserlocations2)
- cr2 = 1
- if len(battleshiplocations2)==0:
- sinkingships('battleship',battleshiplocations2)
- b2 = 1
- if len(carrierlocations2)==0:
- sinkingships('carrier',carrierlocations2)
- ca2 = 1
- shipsdestroyed2 = d2+s2+cr2+b2+ca2
- print(player1 + ', you have sunk', shipsdestroyed2, 'ships.')
- if shipsdestroyed2 == 5:
- break
- if shipsdestroyed2 == 5:
- print('\n\n'player1, "has sunk all of", player2 + "'s ships!")
- print(player1, "is the winner!")
- if shipsdestroyed == 5:
- print(player2, "has sunk all of", player1 + "'s ships!")
- print(player2, "is the winner!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement