Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Battleships!
- import random
- import time
- #Asigns Playing to True
- playing = True
- hits = []
- comp_hits = []
- already = []
- values = []
- def checkpos(computer_values, values):
- values = computer_values
- if len(computer_values) == 6:
- for i in range(6):
- if int(ord(computer_values[i][0]))-65 > 5:
- computer_values = input('Invalid - Enter 6 positions')
- computer_values = computer_values.split()
- checkpos(computer_values, values)
- elif int(computer_values[i][1]) > 5:
- computer_values = input('Invalid - Enter 6 positions')
- computer_values = computer_values.split()
- checkpos(computer_values, values)
- else:
- computer_values = input('Invalid - Enter 6 positions')
- computer_values = computer_values.split()
- checkpos(computer_values, values)
- return
- def compvals(comp_hits, values):
- comp_hits = values
- comptest(comp_hits, values)
- return
- def comptest(comp_hits, values):
- for i in range(len(comp_hits)):
- if comp_hits.count(comp_hits[i]) > 1:
- comp_hits.clear()
- pick(comp_hits)
- return
- def comp_shot(grid2, comp_hits):
- first = random.randint(65, 69)
- first = chr(first)
- second = random.randint(1,5)
- second = str(second)
- pos = first+second
- if int(pos[1]) > 5:
- pos = input('Guess a Position: ').upper()
- shotpos(pos, grid2)
- row = int(pos[1])
- column = int(ord(pos[0]))-65
- if pos in comp_hits:
- for i in range(len(comp_hits)):
- if pos == comp_hits[i]:
- break
- grid2[column][row] = 'X'
- comp_hits.remove(pos)
- elif pos not in comp_hits:
- grid2[column][row] = 'O'
- for num in range(6):
- for num2 in range(6):
- print(' ', grid[num][num2], end='')
- print('t ', end='')
- for num2 in range(6):
- print(' ', grid2[num][num2], end='')
- print()
- return
- #Determines Row number and Position from coordinates
- def shotpos(pos, grid, hits):
- try:
- int(pos[1])
- except:
- pos = input('Invalid - Guess a Position: ').upper()
- shotpos(pos, grid, hits)
- else:
- if int(pos[1]) > 5:
- pos = input('Invalid - Guess a Position: ').upper()
- shotpos(pos, grid, hits)
- row = int(pos[1])
- column = int(ord(pos[0]))-65
- if column > 5:
- pos = input('Invalid - Guess a Position: ').upper()
- shotpos(pos, grid, hits)
- if pos in hits:
- for i in range(len(hits)):
- if pos == hits[i]:
- break
- grid[column][row] = 'X'
- hits.remove(pos)
- elif pos not in hits:
- grid[column][row] = 'O'
- for num in range(6):
- for num2 in range(6):
- print(' ', grid[num][num2], end='')
- print('t ', end='')
- for num2 in range(6):
- print(' ', grid2[num][num2], end='')
- print()
- return
- def test(hits):
- for i in range(len(hits)):
- if hits.count(hits[i]) > 1:
- hits.clear()
- pick(hits)
- def pick(hits):
- for i in range(6):
- first = random.randint(65, 69)
- first = chr(first)
- second = random.randint(1,5)
- second = str(second)
- both = first+second
- hits.append(both)
- test(hits)
- def compshot(grid2, comp_hits, already):
- row = random.randint(1,5)
- column = chr(random.randint(65,69))
- pos = column+str(row)
- column = ord(column)-65
- if pos in already:
- compshot(grid2, comp_hits, already)
- elif pos in comp_hits:
- for i in range(len(comp_hits)):
- if pos == comp_hits[i]:
- break
- grid2[column][row] = 'X'
- comp_hits.remove(pos)
- already.append(pos)
- elif pos not in comp_hits:
- grid2[column][row] = 'O'
- already.append(pos)
- for num in range(6):
- for num2 in range(6):
- print(' ', grid[num][num2], end='')
- print('t ', end='')
- for num2 in range(6):
- print(' ', grid2[num][num2], end='')
- print()
- return
- #Takes User name and Capitalize it.
- user_name = (input('Please Enter Your Name: ')+'.').capitalize()
- #Outputs the user_name and asks if wanting to play.
- print('Hello,', user_name, 'Are You ready to play Battleships?')
- #Gets User answer and Checks if want to play
- if (input(' '))[0].lower() == 'n':
- #If the answer is no, Quit the Game
- print('Ok, That's Fine')
- SystemExit()
- #Grid of Asterisks That makes up the Playing Field
- grid = [['A ','*','*','*','*','*'],
- ['B ','*','*','*','*','*'],
- ['C ','*','*','*','*','*'],
- ['D ','*','*','*','*','*'],
- ['E ','*','*','*','*','*'],
- [' ','1','2','3','4','5']]
- grid2 = [['A ','*','*','*','*','*'],
- ['B ','*','*','*','*','*'],
- ['C ','*','*','*','*','*'],
- ['D ','*','*','*','*','*'],
- ['E ','*','*','*','*','*'],
- [' ','1','2','3','4','5']]
- computer_values = input('Enter 6 positions')
- computer_values = computer_values.split()
- values = computer_values
- checkpos(computer_values, values)
- compvals(comp_hits, values)
- #Prints out Grid
- for num in range(6):
- for num2 in range(6):
- print(' ', grid[num][num2], end='')
- print('t ', end='')
- for num2 in range(6):
- print(' ', grid2[num][num2], end='')
- print()
- pick(hits)
- while hits:
- pos = input('Guess a position: ').upper()
- shotpos(pos, grid, hits)
- print('Computer is thinking...')
- time.sleep(1)
- compshot(grid2, comp_hits, already)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement