Published
Edited
May 23, 2022
1 star
Insert cell
# Matching Game AI with variable Memory Fuzziness
Insert cell
MEMORY_FUZZINESS=0.5// 0-1.0 probabilty of forgetting
Insert cell
train(randomWeights(),3)
Insert cell
function train(weights,iterations){
const MAX_MOVES = 1000
let history = []
for(let i = 0; i<iterations; i++){
let board = randomBoard()
//let memory = []
let ended = false
let misses = 0
let matches = 0
let moves = 0
let memory = {}
let flipped_indexes = []
let remembered_count = 0
while(!ended && moves < MAX_MOVES){
let remembered = -1
let guessIndex = getRandomInt(0,board.dealt.length-1)
if(board.flipped.length===1){
// do we remember where the match is?
for(let j in memory){
// check memory for same card in a different spot
if(remembered===-1 && j !== flipped_indexes[0]){
if(memory?.[j] === board.flipped[0]){
remembered = j
remembered_count++
}
}
}
}
if(remembered > -1 && Math.random() >= MEMORY_FUZZINESS){
guessIndex = remembered
}

moves++
let revealed = board.dealt[guessIndex]
memory[guessIndex]=revealed
board.flipped.push(revealed)
flipped_indexes.push(guessIndex)
if(board.flipped.length===2){
if(
board.flipped[0]
=== board.flipped[1]
){
// match
// remove from available array
board.dealt.splice(board.dealt.indexOf(board.flipped[0]),1)
board.dealt.splice(board.dealt.indexOf(board.flipped[1]),1)

board.flipped = []
flipped_indexes = []
matches++
}else{
// not a match
// leave and clear flipped
board.flipped = []
flipped_indexes = []
misses++
}
}

if(!board.dealt.length || moves >= MAX_MOVES-1){
history.push({
misses,
matches,
moves,
remembered_count,
board
})
ended = true
}
}
}
return{
weights,
history,
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more