📝 Replaced "lastMove" with "gameHistory", so you can now undo several times in a row

This commit is contained in:
jona605a
2020-08-09 23:26:18 +02:00
parent 437c3e7668
commit bc11c5dd24
2 changed files with 13 additions and 31 deletions

View File

@ -97,11 +97,10 @@ def hexStart(channel, user, opponent):
board = [ [ 0 for i in range(BOARDWIDTH) ] for j in range(BOARDWIDTH) ]
players = [user,opponent]
random.shuffle(players) # random starting player
winningPieces = [[""],[""],[""]] # etc.
lastMove = (-1,-1)
gameHistory = []
data[channel] = {"board":board, "winner":0,
"players":players, "winningPieces":winningPieces, "turn":1, "difficulty":difficulty, "lastMove":lastMove}
"players":players, "turn":1, "difficulty":difficulty, "gameHistory":gameHistory}
with open("resources/games/hexGames.json", "w") as f:
json.dump(data,f,indent=4)
@ -154,7 +153,7 @@ def placeHex(channel : str,position : str, user):
winAmount = data[channel]["difficulty"]*10
message += " Adding "+str(winAmount)+" GwendoBucks to their account."
data[channel]["lastMove"] = (int(position[1])-1, ord(position[0])-97)
data[channel]["gameHistory"].append((int(position[1])-1, ord(position[0])-97))
# Is it now Gwendolyn's turn?
gwendoTurn = False
@ -189,6 +188,7 @@ def placeHex(channel : str,position : str, user):
def placeOnHexBoard(board,player,position):
# Translates the position
position = position.lower()
# Error handling
try:
column = ord(position[0]) - 97 # ord() translates from letter to number
row = int(position[1:]) - 1
@ -198,7 +198,6 @@ def placeOnHexBoard(board,player,position):
except:
logThis("Invalid position (error code 1531)")
return "Error. The position should be a letter followed by a number, e.g. \"e2\"."
# Place at the position
if board[row][column] == 0:
board[row][column] = player
@ -213,16 +212,19 @@ def undoHex(channel, user):
with open("resources/games/hexGames.json", "r") as f:
data = json.load(f)
if user in data[channel]["players"]:
if data[channel]["lastMove"] != (-1,-1):
if len(data[channel]["gameHistory"]):
turn = data[channel]["turn"]
# You can only undo after your turn, which is the opponent's turn.
if user == data[channel]["players"][(turn % 2)]: # If it's not your turn
logThis("Undoing {}'s last move".format(getName(user)))
lastMove = data[channel]["lastMove"]
lastMove = data[channel]["gameHistory"].pop()
data[channel]["board"][lastMove[0]][lastMove[1]] = 0
data[channel]["turn"] = turn%2 + 1
# Save the data
with open("resources/games/hexGames.json", "w") as f:
json.dump(data,f,indent=4)
# Update the board
hexDraw.drawHexPlacement(channel,0,"abcdefghijk"[lastMove[1]]+str(lastMove[0]+1)) # The zero makes the hex disappear
return "You undid", True, True, False, False
@ -246,7 +248,7 @@ def hexAI(channel):
board = data[channel]["board"]
player = data[channel]["players"].index("Gwendolyn")+1
#difficulty = data[channel]["difficulty"]
lastMove = data[channel]["lastMove"]
lastMove = data[channel]["gameHistory"][-1]
# These moves are the last move +- 2.
moves = [[(lastMove[0]+j-2,lastMove[1]+i-2) for i in range(5) if lastMove[1]+i-2 in range(11)] for j in range(5) if lastMove[0]+j-2 in range(11)]