Skip to content

Commit

Permalink
fixed training of 8Puzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
KenN7 committed Aug 28, 2023
1 parent c551846 commit 001a677
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 88 deletions.
10 changes: 9 additions & 1 deletion 8Puzzle/EightPuzzle_RL.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,13 +423,21 @@ def generateGame(self):
for i in range(9):
newTileIndex = randint(0, 8 - i)
game.append(tiles.pop(newTileIndex))
solvableGame = self.puzzle.isSolvable()
solvableGame = self.isSolvable(game)
# We convert the state, encoded as a list, into an integer.
stateAsInt = 0
for j in range(9):
stateAsInt += (10 ** (8 - j)) * game[j]
return str(stateAsInt)

def isSolvable(self, tiles):
count = 0
for i in range(len(tiles) - 1):
for j in range(i + 1, len(tiles)):
if tiles[i] > tiles[j] and tiles[i] != 9:
count += 1
return True if (count % 2 == 0 and count != 0) else False

def initLearning(self, typeGame, nbGames):
"""
An initial learning of the AI agent. We play a given number of games,
Expand Down
Loading

0 comments on commit 001a677

Please sign in to comment.