我刚刚开始使用python,但对编码并不陌生。我使用Spyder IDE练习了几个不同的东西。我目前在战舰计划方面遇到了麻烦。
我第一次运行它工作正常。但是在我运行一次之后,大部分时间在我再次运行它之后,它不会根据代码中给出的新值更新一些变量。我的意思是它使用上一次运行的值,并且在我结束程序之前不会更新它们。
通常情况下,船舶位置和方向字不会更新。我还运行了部分代码来确定这些变量并打印它们而不是其他任何东西。这似乎工作正常,但似乎整个过程在整个过程中都会以某种方式弄乱。
我的代码如下。很多评论都是为了帮助理解我可能错综复杂的过程。有什么想法吗?
"""
Created on Thu Jan 12 16:20:01 2017
@author: zroland
"""
# need random for ship location and orientation
import random as rng
# make the board a list of lists
board = [['O' for x in range(10)] for y in range(10)]
# prints the board
def print_board(board):
i = 0
top = ''
for k in range(len(board)):
if k+1 < 10:
top += ' ' + str(k+1) + ' '
else:
top += ' ' + str(k+1) + ' '
print(top)
for row in board:
print(row, chr(97 + i))
i += 1
# sets the length of the ship and calls the print_board function
ship_size = 2
print_board(board)
"""Sets ship orientation and determins the location of the ship based on that
(the ship cannot be placed at the end of the board if it is more than 1 unit
long)"""
ship_or = rng.randint(1, 2)
if(ship_or == 1):
ship_or_w = 'Vertical'
ship_r = chr(rng.randint(97, 97 + len(board) - ship_size))
ship_c = rng.randint(1, len(board[0]))
else:
ship_or_w = 'Horizontal'
ship_r = chr(rng.randint(97, 97 + len(board) - 1))
ship_c = rng.randint(1, len(board[0])-ship_size + 1)
# initializes the variable that keeps track of how many times you hit the ship
hit_count = 0
# shows location of ship, orientation, and size for development purposes
print(ship_r, ship_c, ship_or_w, ship_size)
"""Nested while loop. This is the game. The hit_count is checked to see if
the ship has been hit a number of times equal to its length. Need to edit this
to make sure you don't cheat and just hit the same spot multiple times to
win."""
while(hit_count != ship_size):
"""Initializes the guess location so the code will ask you for intput. This
is to allow a the code to make sure you have a guess that is actually
on the board. If it is not, it will continue to ask you for values until
you do"""
row1 = chr(97 + len(board))
column = 1
while((97 > ord(row1) or ord(row1) >= 97 + len(board))
or (1 > column or column > len(board[0]))):
"""The first if statement is to make sure your column is good first.
If it is not, there is no point in asking for a row. The column guess
is initialized to be a correct value."""
if(1 <= column <= len(board[0])):
row1 = input("Enter your guess for the row: ")
if(97 <= ord(row1) < 97 + len(board)):
"""if the row is a correct value, it creates/updates the
variable we use to select the row in the board list."""
row = ord(row1) - 97
else:
print('You did not enter a correct letter. Please try again')
"""Same as above. Checks for a correct row. No reason to ask for the
column if the row is not right."""
if(97 <= ord(row1) < 97 + len(board)):
column = int(input("Enter your guess for the column: "))
if(1 > column or column > len(board[0])):
print("You did not enter a correct number. Please try again")
# end of while loop that asks for/checks you guess
"""This is how the game checks if you hit or miss. The last conditional
is the first one to think about. It determines the orientation of the
ship, which in turn affects which guesses result in a hit.
For the first if statement, if the orientation is 1 (vertical), it
checks to see if your guess hit the first part of the ship (the actual
location value determined at the start of the code) or any other
space taken up by its length. If you did in fact hit the ship,
it marks the board with an 'X' and tells you you hit it. It then
increments the hit counter. The second if statement is for horizontal
ships, and the third is in case you did not hit anything."""
if((ord(ship_r) <= row <= ord(ship_r) + ship_size - 1) and column == ship_c
and ship_or == 1):
board[row][column-1] = 'X'
print_board(board)
print("You hit!")
hit_count += 1
elif(row1 == ship_r and (ship_c <= column <= ship_c + ship_size - 1)
and ship_or == 2):
board[row][column-1] = 'X'
print_board(board)
print("You hit!")
hit_count += 1
else:
board[row][column-1] = '|'
print_board(board)
print('You missed :(')
# end of while loop that keeps asking you for guesses until you win
# prints hit count too to see if it is chekcking the value properly in the loop
print("You win!", hit_count)