I'm currently recreating a game of "Battleship" utilizing SQL databases and Python/Flask. I'm a bit stuck though on the feature that allows a user to keep trying to attack the computer's board until they miss. This is to say, at the beginning of the route's code, I define a variable "turn" that is either user or computer. If the user hits, I want it to stay on the user's turn and update the page with a display of the spots on the computer's board that the user has hit and missed (with ? marks in all the other spots). However, if they miss, I want to change the turn and allow for a computer turn until the computer hits/misses. This is quite complicated, and I'm unsure if there's a feature that allows for this without constantly redirecting to new pages. Relevant code is below. userboard is the user's board with the ships/sea, computerboard is the computer's board with the ships/sea, and userview is the board the user sees displaying ? marks where they haven't hit, and then the ships/seas they have hit. - is the sea, O is an unhit ship, and X is a hit ship.
@app.route("/post_place", methods=["GET", "POST"])
def post_place():
userboard = db.execute('SELECT * FROM userboard')
computerboard = db.execute('SELECT * FROM computerboard')
userview = db.execute('SELECT * FROM userview')
columnvalues = ['one','two','three','four','five','six','seven','eight']
lettervalues = ['A','B','C','D','E','F','G','H']
turn = 'user'
if request.method == "POST":
while (turn == 'user'):
attackletter = request.form.get("attackletter")
attacknumber = request.form.get("attacknumber")
conn = sqlite3.connect('boards.db')
cur = conn.cursor()
cur.execute("SELECT * FROM computerboard WHERE Letter=?", (attackletter,))
attackletterfetch = cur.fetchall()
cur.execute("SELECT * FROM userview WHERE Letter=?", (attackletter,))
attackletterfetchview = cur.fetchall()
if attacknumber == '1':
attacknumberstr = 'one'
if attacknumber == '2':
attacknumberstr = 'two'
if attacknumber == '3':
attacknumberstr = 'three'
if attacknumber == '4':
attacknumberstr = 'four'
if attacknumber == '5':
attacknumberstr = 'five'
if attacknumber == '6':
attacknumberstr = 'six'
if attacknumber == '7':
attacknumberstr = 'seven'
if attacknumber == '8':
attacknumberstr = 'eight'
if (((attackletterfetchview[0][int(attacknumber)] == '?'))):
if (((attackletterfetch[0][int(attacknumber)])) == '-'):
db.execute("UPDATE userview SET :attacknumberstr = '-' WHERE Letter=:attackletter",attacknumberstr = attacknumberstr, attackletter = attackletter)
print("miss")
turn = 'computer'
return redirect("/post_place")
if (((attackletterfetch[0][int(attacknumber)])) == 'O'):
db.execute("UPDATE userview SET :attacknumberstr = 'X' WHERE Letter=:attackletter",attacknumberstr = attacknumberstr, attackletter = attackletter)
turn = 'user'
return redirect("/post_place")
else:
print("error")
return render_template("post_place.html",userboard=userboard, userview=userview, turn=turn, turntext=turntext, msg=msg)
And then the HTML/Flask code just has a display of userview and then two drop down menus that ask for the row/column of a cell they'd want to attack (i.e: A6, B4, etc).
Any help is appreciated. Thanks!
Comments
Post a Comment