我正在尝试用python创建具有以下内容的战舰游戏:
到目前为止,如果有人对下一步该怎么走有任何帮助或指导,我在将数据存储到数据库中时遇到问题,我将不胜感激!
from random import randint
import getpass
import sqlite3
#creating the database using SQLite
def createTable():
db = sqlite3.connect(':memory:')
db = sqlite3.connect('MyBSdb') #this folder was created
cursor = db.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS BSTable(name TEXT, password TEXT, wins INT, highscore INT)''')
db.commit()
db.close()
def save(name,password):
db = sqlite3.connect(':memory:')
db = sqlite3.connect('MyBSdb')
cursor = db.cursor()
cursor.execute('''INSERT INTO BSTable(name,password,wins,highscore) values(?,?)''', (name,password,wins,highscore))
db.commit()
db.close()
def get():
db = sqlite3.connect(':memory:')
db = sqlite3.connect('MyBSdb')
cursor = db.cursor()
cursor.execute('''SELECT name,password,wins,highscore from myTable''')
results = cursor.fetchall()
for result in results:
print (result)
if __name__ == '__main__':
createTable()
save('users','passw')
get()
def writeFile():
#create file handle, open file for append
file = open('Battleship.txt', 'a')
text = 'Bob,hello'
#write string s to file
file.write(text + '\n')
#close file handle after write operation
file.close()
def readFile():
#create file handle, open file for append
file = open('Battleship.txt', 'r')
text = file.readlines()
for line in text:
print(line.split(",")[0])
print(line.split(",")[1])
#close file handle after write operation
file.close()
if __name__ == '__main__':
writeFile()
readFile()
#creating a login and password
users = {}
status = ""
def displayMenu():
status = input("Are you registered user? y/n? ")
if status == "y":
oldUser()
elif status == "n":
newUser()
def newUser():
createLogin = input("Create login name: ")
if createLogin in users:
print("\nLogin name already exist!\n")
else:
createPassw = input("Create password: ")
users[createLogin] = createPassw
print("\nUser created\n")
def oldUser():
login = input("Enter login name: ")
passw = input("Enter password: ")
if login in users and users[login] == passw:
print("\nLogin successful!\n")
else:
print("\nUser doesn't exist or wrong password!\n")
while status != "q":
displayMenu() #cant stop the loop here or figure out why it wont move on
#building the gameboard
board = []
for x in range(5):
board.append(["-"] * 5)
def print_board(board):
print(" ", " ".join("12345"))
for letter, row in zip("ABCDE", board):
print(letter, " ".join(row))
#beginning the game
print("Let's play Battleship!")
print("Find and sink the ship!")
print_board(board)
score = 0 #using this to keep a score to store in the database
win = True
def wins():
if win == True:
win = score + 1
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
#the numbers and letters in this section are not aligning with the grid I created
for turn in range(9):
print ("Turn"), turn
guess_row = int(input("Guess Row:"))
guess_col = int(
input("Guess Col:"))
if guess_row == ship_row and guess_col == ship_col:
win == True
print("Congratulations! You sunk my battleship!")
print("Your score is " + score ) #keeping score but how to save in db?
break
else:
if (guess_row < 0 or guess_row > 5) or (guess_col < 0 or guess_col > 5):
print("Oops, that's not even in the ocean.")
elif(board[guess_row][guess_col] == "X"):
print("You guessed that one already.")
else:
print("You missed my battleship!")
board[guess_row][guess_col] = "X"
if turn == 8:
print("Game Over")
print("Your final score is " + score )
turn =+ 1
print_board(board)
#how to display games won and high score when user logs in?
答案 0 :(得分:0)
我认为您的问题是要解析的值的数量为2,而您给出4个值并期望4列有4个值。
尝试使用:-
cursor.execute('''INSERT INTO BSTable(name,password,wins,highscore) values(?,?,?,?)''', (name,password,wins,highscore))