Fixed the order of printing out which ships were hit. Original game printed out ships that were hit AFTER updating the board, not during. getting close to done!

This commit is contained in:
Todd Kaiser
2021-03-16 19:28:55 -06:00
parent e6e6a52646
commit 7222546bcf

View File

@@ -293,14 +293,12 @@ def execute_shot(turn, board, x, y):
global current_turn
square = board[x-1][y-1]
ship_hit = -1
if square is not None:
if square >= 0 and square < len(SHIPS):
if turn == COMPUTER:
print("I HIT YOUR", SHIPS[square][0])
else:
print("YOU HIT MY", SHIPS[square][0])
ship_hit = square
board[x-1][y-1] = 10 + current_turn
return ship_hit
# calculate_shots
@@ -423,28 +421,6 @@ first_turn = PLAYER
second_turn = COMPUTER
def player_turn():
print("YOU HAVE", num_player_shots, "SHOTS.")
global active_turn
active_turn = PLAYER
shots = []
for shot in range(num_player_shots):
valid_shot = False
while not valid_shot:
x, y = input_coord()
square = computer_board[x-1][y-1]
if square is not None:
if square > 10:
if active_turn == PLAYER:
print("YOU SHOT THERE BEFORE ON TURN", square - 10)
continue
shots.append((x, y))
valid_shot = True
for shot in shots:
execute_shot(computer_board, shot[0], shot[1])
def execute_turn(turn):
global num_computer_shots
@@ -486,12 +462,22 @@ def execute_turn(turn):
shots.append((x, y))
valid_shot = True
hits = []
for shot in shots:
execute_shot(turn, board, shot[0], shot[1])
hit = execute_shot(turn, board, shot[0], shot[1])
if hit >= 0:
hits.append(hit)
if turn == COMPUTER:
if print_computer_shots:
print(shot[0], shot[1])
for hit in hits:
if turn == COMPUTER:
print("I HIT YOUR", SHIPS[hit][0])
else:
print("YOU HIT MY", SHIPS[hit][0])
if turn == COMPUTER:
num_player_shots = calculate_shots(board)
return num_player_shots