# ==============================================================================
"""LEAP FROG : implement the LeapFrog puzzle"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "2.0" # play game with keyboard (left and right arrow)
__date__    = "2022-11-12"
# ------------------------------------------------------------------------------
from ezTK import *
# ------------------------------------------------------------------------------
def main(frog:int=3):
  """create the main window and pack the widgets"""
  global win
  win = Win(title='LEAP FROG', op=3, key=on_key) # create main window
  images = tuple(Image(f"frog{c}.gif") for c in '012') # read images
  # ----------------------------------------------------------------------------
  board = Frame(win) # create frame to store board cells
  for loop in range(2*frog+1): # loop over all board cells
    Brick(board, image=images, border=2) # create cell with default state = 0
  Button(win, grow=False, text='RESET', command=on_reset) # create reset button
  # ----------------------------------------------------------------------------
  win.frog, win.board = frog, board # store frog and board as win properties
  on_reset(); win.loop() # set initial configuration and start event loop
# ------------------------------------------------------------------------------
def on_reset() -> None:
  """callback function for the 'RESET' button"""
  frog, board = win.frog, win.board # create local shortcuts
  # define initial cell states : 0 = empty, 1 = blue frog, 2 = green frog
  states = [1]*frog + [0] + [2]*frog # create list of initial cell states
  for n in range(2*frog+1): # loop over all board cells
    board[n].state = states[n] # set initial state for current board cell
# ------------------------------------------------------------------------------
def on_key(widget:object, code:str, mods:str) -> None:
  """callback function for all keyboard events"""
  if code not in ('Right','Left'): return # wrong key (= not a directional key)
  move(code) # apply move for frog in selected direction
# ------------------------------------------------------------------------------
def move(direction:str) -> None:
  """find empty cell and move its neighboring frog in provided direction"""
  frog, board = win.frog, win.board # create local shortcuts
  # first version : use while loop to find index of empty board cell
  n = 0
  while board[n].state != 0: n += 1 # loop while current cell is not empty
  # second version : use for loop to find index of empty board cell
  #for n in range(2*frog+1): # loop over all board cells
  #  if board[n].state == 0: break # break loop when empty cell has been found
  # third version : use the 'index' method to directly find index of empty cell
  #states = [cell.state for cell in board] # get state for each board cell
  #n = states.index(0) # call the 'index' method on the list of states
  # ----------------------------------------------------------------------------
  if direction == 'Right': # try to move or jump right
    if n > 0 and board[n-1].state == 1: # blue frog can move right
      board[n-1].state, board[n].state = 0, 1 # apply blue frog move 
    elif n > 1 and board[n-2].state == 1: # blue frog can jump right
      board[n-2].state, board[n].state = 0, 1 # apply blue frog jump
  elif direction == 'Left': # try to move or jump left
    if n < 2*frog and board[n+1].state == 2: # green frog can move left
      board[n+1].state, board[n].state = 0, 2 # apply green frog move 
    elif n < 2*frog-1 and board[n+2].state == 2: # green frog can jump left
      board[n+2].state, board[n].state = 0, 2 # apply green frog jump
# ==============================================================================
if __name__ == '__main__':
  main() # create window with default game parameters: frog=3
  #main(7) # try with 7 frogs per color  
# ==============================================================================
