📝 Replaced "lastMove" with "gameHistory", so you can now undo several times in a row
This commit is contained in:
@@ -97,11 +97,10 @@ def hexStart(channel, user, opponent):
|
|||||||
board = [ [ 0 for i in range(BOARDWIDTH) ] for j in range(BOARDWIDTH) ]
|
board = [ [ 0 for i in range(BOARDWIDTH) ] for j in range(BOARDWIDTH) ]
|
||||||
players = [user,opponent]
|
players = [user,opponent]
|
||||||
random.shuffle(players) # random starting player
|
random.shuffle(players) # random starting player
|
||||||
winningPieces = [[""],[""],[""]] # etc.
|
gameHistory = []
|
||||||
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, "turn":1, "difficulty":difficulty, "gameHistory":gameHistory}
|
||||||
|
|
||||||
with open("resources/games/hexGames.json", "w") as f:
|
with open("resources/games/hexGames.json", "w") as f:
|
||||||
json.dump(data,f,indent=4)
|
json.dump(data,f,indent=4)
|
||||||
@@ -154,7 +153,7 @@ def placeHex(channel : str,position : str, user):
|
|||||||
winAmount = data[channel]["difficulty"]*10
|
winAmount = data[channel]["difficulty"]*10
|
||||||
message += " Adding "+str(winAmount)+" GwendoBucks to their account."
|
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?
|
# Is it now Gwendolyn's turn?
|
||||||
gwendoTurn = False
|
gwendoTurn = False
|
||||||
@@ -189,6 +188,7 @@ def placeHex(channel : str,position : str, user):
|
|||||||
def placeOnHexBoard(board,player,position):
|
def placeOnHexBoard(board,player,position):
|
||||||
# Translates the position
|
# Translates the position
|
||||||
position = position.lower()
|
position = position.lower()
|
||||||
|
# Error handling
|
||||||
try:
|
try:
|
||||||
column = ord(position[0]) - 97 # ord() translates from letter to number
|
column = ord(position[0]) - 97 # ord() translates from letter to number
|
||||||
row = int(position[1:]) - 1
|
row = int(position[1:]) - 1
|
||||||
@@ -198,7 +198,6 @@ def placeOnHexBoard(board,player,position):
|
|||||||
except:
|
except:
|
||||||
logThis("Invalid position (error code 1531)")
|
logThis("Invalid position (error code 1531)")
|
||||||
return "Error. The position should be a letter followed by a number, e.g. \"e2\"."
|
return "Error. The position should be a letter followed by a number, e.g. \"e2\"."
|
||||||
|
|
||||||
# Place at the position
|
# Place at the position
|
||||||
if board[row][column] == 0:
|
if board[row][column] == 0:
|
||||||
board[row][column] = player
|
board[row][column] = player
|
||||||
@@ -213,16 +212,19 @@ def undoHex(channel, user):
|
|||||||
with open("resources/games/hexGames.json", "r") as f:
|
with open("resources/games/hexGames.json", "r") as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
if user in data[channel]["players"]:
|
if user in data[channel]["players"]:
|
||||||
if data[channel]["lastMove"] != (-1,-1):
|
if len(data[channel]["gameHistory"]):
|
||||||
turn = data[channel]["turn"]
|
turn = data[channel]["turn"]
|
||||||
# You can only undo after your turn, which is the opponent's 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
|
if user == data[channel]["players"][(turn % 2)]: # If it's not your turn
|
||||||
logThis("Undoing {}'s last move".format(getName(user)))
|
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]["board"][lastMove[0]][lastMove[1]] = 0
|
||||||
data[channel]["turn"] = turn%2 + 1
|
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
|
# Update the board
|
||||||
hexDraw.drawHexPlacement(channel,0,"abcdefghijk"[lastMove[1]]+str(lastMove[0]+1)) # The zero makes the hex disappear
|
hexDraw.drawHexPlacement(channel,0,"abcdefghijk"[lastMove[1]]+str(lastMove[0]+1)) # The zero makes the hex disappear
|
||||||
return "You undid", True, True, False, False
|
return "You undid", True, True, False, False
|
||||||
@@ -246,7 +248,7 @@ def hexAI(channel):
|
|||||||
board = data[channel]["board"]
|
board = data[channel]["board"]
|
||||||
player = data[channel]["players"].index("Gwendolyn")+1
|
player = data[channel]["players"].index("Gwendolyn")+1
|
||||||
#difficulty = data[channel]["difficulty"]
|
#difficulty = data[channel]["difficulty"]
|
||||||
lastMove = data[channel]["lastMove"]
|
lastMove = data[channel]["gameHistory"][-1]
|
||||||
|
|
||||||
# These moves are the last move +- 2.
|
# 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)]
|
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)]
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ LINETHICKNESS = 15
|
|||||||
HEXTHICKNESS = 6 # This is half the width of the background lining between every hex
|
HEXTHICKNESS = 6 # This is half the width of the background lining between every hex
|
||||||
X_THICKNESS = HEXTHICKNESS * math.cos(math.pi/6)
|
X_THICKNESS = HEXTHICKNESS * math.cos(math.pi/6)
|
||||||
Y_THICKNESS = HEXTHICKNESS * math.sin(math.pi/6)
|
Y_THICKNESS = HEXTHICKNESS * math.sin(math.pi/6)
|
||||||
BACKGROUND_COLOR = (230,230,230)
|
BACKGROUND_COLOR = (235,235,235)
|
||||||
BETWEEN_COLOR = BACKGROUND_COLOR
|
BETWEEN_COLOR = BACKGROUND_COLOR
|
||||||
BLANK_COLOR = "lightgrey"
|
BLANK_COLOR = "lightgrey"
|
||||||
PIECECOLOR = {1:(237,41,57),2:(0,165,255),0:BLANK_COLOR} # player1 is red, player2 is blue
|
PIECECOLOR = {1:(237,41,57),2:(0,165,255),0:BLANK_COLOR} # player1 is red, player2 is blue
|
||||||
@@ -41,16 +41,12 @@ SMOL_SIDELENGTH = SIDELENGTH * 0.6
|
|||||||
def drawBoard(channel):
|
def drawBoard(channel):
|
||||||
logThis("Drawing empty Hex board")
|
logThis("Drawing empty Hex board")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Creates the empty image
|
# Creates the empty image
|
||||||
im = Image.new('RGB', size=(CANVAS_WIDTH, CANVAS_HEIGHT),color = BACKGROUND_COLOR)
|
im = Image.new('RGB', size=(CANVAS_WIDTH, CANVAS_HEIGHT),color = BACKGROUND_COLOR)
|
||||||
# 'd' is a shortcut to drawing on the image
|
# 'd' is a shortcut to drawing on the image
|
||||||
d = ImageDraw.Draw(im,"RGBA")
|
d = ImageDraw.Draw(im,"RGBA")
|
||||||
|
|
||||||
|
# Drawing all the hexagons
|
||||||
|
|
||||||
# The board is indexed with [row][column]
|
|
||||||
for column in BOARDCOORDINATES:
|
for column in BOARDCOORDINATES:
|
||||||
for startingPoint in column:
|
for startingPoint in column:
|
||||||
x = startingPoint[0]
|
x = startingPoint[0]
|
||||||
@@ -98,22 +94,6 @@ def drawBoard(channel):
|
|||||||
d.text( (X_OFFSET + HEXAGONWIDTH*(i/2+11) + 30 , Y_OFFSET + 6 + i*HEXAGONHEIGHT) , str(i+1), font=fnt, fill=TEXTCOLOR)
|
d.text( (X_OFFSET + HEXAGONWIDTH*(i/2+11) + 30 , Y_OFFSET + 6 + i*HEXAGONHEIGHT) , str(i+1), font=fnt, fill=TEXTCOLOR)
|
||||||
|
|
||||||
# Write player names and color
|
# Write player names and color
|
||||||
"""
|
|
||||||
with open("resources/games/hexGames.json", "r") as f:
|
|
||||||
data = json.load(f)
|
|
||||||
|
|
||||||
board = data[channel]["board"]
|
|
||||||
# Getting player names
|
|
||||||
players = data[channel]["players"]
|
|
||||||
if players[0] == "Gwendolyn":
|
|
||||||
player1 = "Gwendolyn"
|
|
||||||
else:
|
|
||||||
player1 = getName(players[0])
|
|
||||||
if players[1] == "Gwendolyn":
|
|
||||||
player2 = "Gwendolyn"
|
|
||||||
else:
|
|
||||||
player2 = getName(players[1])
|
|
||||||
"""
|
|
||||||
with open("resources/games/hexGames.json", "r") as f:
|
with open("resources/games/hexGames.json", "r") as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
for p in [1,2]:
|
for p in [1,2]:
|
||||||
|
|||||||
Reference in New Issue
Block a user