You can now go back in time! With "!hex undo" !

This commit is contained in:
jona605a
2020-08-08 14:16:31 +02:00
parent 08851182a7
commit 8be15b6dd5
2 changed files with 29 additions and 16 deletions

View File

@ -26,7 +26,7 @@ HEX_DIRECTIONS = [(0,1),(-1,1),(-1,0),(0,-1),(1,-1),(1,0)]
# Parses command # Parses command
def parseHex(command, channel, user): def parseHex(command, channel, user):
commands = command.split() commands = command.lower().split()
if command == "" or command == " ": if command == "" or command == " ":
return "I didn't get that. Use \"!hex start [opponent]\" to start a game.", False, False, False, False return "I didn't get that. Use \"!hex start [opponent]\" to start a game.", False, False, False, False
@ -108,7 +108,7 @@ def hexStart(channel, user, opponent):
players = [user,opponent] players = [user,opponent]
random.shuffle(players) # random starting player random.shuffle(players) # random starting player
winningPieces = [[""],[""],[""]] # etc. winningPieces = [[""],[""],[""]] # etc.
lastMove = (5,5) lastMove = (-1,-1)
data[channel] = {"board":board, "winner":0, data[channel] = {"board":board, "winner":0,
"players":players, "winningPieces":winningPieces, "turn":1, "difficulty":difficulty, "lastMove":lastMove} "players":players, "winningPieces":winningPieces, "turn":1, "difficulty":difficulty, "lastMove":lastMove}
@ -266,14 +266,37 @@ def evaluateBoard(board):
return score, isWon return score, isWon
def undoHex(channel, user): 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):
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"]
data[channel]["board"][lastMove[0]][lastMove[1]] = 0
data[channel]["turn"] = turn%2 + 1
# Update the board
hexDraw.drawHexPlacement(channel,0,"abcdefghijk"[lastMove[1]]+str(lastMove[0]+1))
return "You undid", True, True, False, False
else:
# Sassy
return "Nice try. You can't undo your opponent's move. ", False, False, False, False
else:
# Sassy
return "Really? You undo right at the start of the game?", False, False, False, False
else:
return "You're not a player in the game", False, False, False, False
message = "yup" message = "yup"
gwendoturn = False gwendoturn = False
return message, True, True, False, gwendoturn return message, True, True, False, gwendoturn
# Plays as the AI # Plays as the AI
def hexAI(channel): def hexAI(channel):
logThis("Figuring out best move") logThis("Figuring out best move")

View File

@ -16,7 +16,7 @@ HEXAGONHEIGHT = 1.5 * SIDELENGTH # not really the entire height, but the height
FONTSIZE = 45 FONTSIZE = 45
TEXTCOLOR = (0,0,0) TEXTCOLOR = (0,0,0)
fnt = ImageFont.truetype('resources/futura-bold.ttf', FONTSIZE) fnt = ImageFont.truetype('resources/futura-bold.ttf', FONTSIZE)
PIECECOLOR = {1:(237,41,57),2:(0,165,255)} # player1 is red, player2 is blue
LINETHICKNESS = 15 LINETHICKNESS = 15
HEXTHICKNESS = 6 HEXTHICKNESS = 6
X_THICKNESS = HEXTHICKNESS * math.cos(math.pi/6) X_THICKNESS = HEXTHICKNESS * math.cos(math.pi/6)
@ -24,6 +24,7 @@ Y_THICKNESS = HEXTHICKNESS * math.sin(math.pi/6)
BACKGROUND_COLOR = (230,230,230) BACKGROUND_COLOR = (230,230,230)
BETWEEN_COLOR = BACKGROUND_COLOR BETWEEN_COLOR = BACKGROUND_COLOR
BLANK_COLOR = "lightgrey" # maybe lighter? BLANK_COLOR = "lightgrey" # maybe lighter?
PIECECOLOR = {1:(237,41,57),2:(0,165,255),0:BLANK_COLOR} # player1 is red, player2 is blue
BOARDCOORDINATES = [ [(X_OFFSET + HEXAGONWIDTH*(column + row/2),Y_OFFSET + HEXAGONHEIGHT*row) for column in range(11)] for row in range(11)] # These are the coordinates for the upperleft corner of every hex BOARDCOORDINATES = [ [(X_OFFSET + HEXAGONWIDTH*(column + row/2),Y_OFFSET + HEXAGONHEIGHT*row) for column in range(11)] for row in range(11)] # These are the coordinates for the upperleft corner of every hex
COLHEXTHICKNESS = 4 COLHEXTHICKNESS = 4
COLX_THICKNESS = COLHEXTHICKNESS * math.cos(math.pi/6) COLX_THICKNESS = COLHEXTHICKNESS * math.cos(math.pi/6)
@ -164,14 +165,3 @@ def drawHexPlacement(channel,player,position):
im.save(FILEPATH) im.save(FILEPATH)
except: except:
logThis("Error drawing new hex on board (error code 1541") logThis("Error drawing new hex on board (error code 1541")
if __name__ == '__main__':
drawBoard("resources/games/hexBoards/boardTest.png")
drawHexPlacement("resources/games/hexBoards/boardTest.png",1,"f7")
drawHexPlacement("resources/games/hexBoards/boardTest.png",2,"f8")
drawHexPlacement("resources/games/hexBoards/boardTest.png",1,"h6")
drawHexPlacement("resources/games/hexBoards/boardTest.png",2,"e8")
drawHexPlacement("resources/games/hexBoards/boardTest.png",1,"e7")
drawHexPlacement("resources/games/hexBoards/boardTest.png",2,"c9")
drawHexPlacement("resources/games/hexBoards/boardTest.png",1,"g8")
drawHexPlacement("resources/games/hexBoards/boardTest.png",2,"h4")