Javascript: Brain.js Tic Tac Toe

92 Views Asked by At

I want create a basic AI that for each move (in input) return the best one (in output). It's just for learn and play wit Brain.js

const net = new brain.recurrent.LSTM();


net.train([{
    input: [
        0, 0, 0,
        0, 0, 0,
        0, 0, 0
    ],
    output: [
        0, 0, 0,
        0, 1, 0,
        0, 0, 0
    ]
},
{
    input: [
        0, 0, 0,
        0, 1, 0,
        0, 0, 0
    ],
    output: [
        -1, 0, 0,
        0, 1, 0,
        0, 0, 0
    ]
},
{
    input: [
        -1, 0, 0,
        0, 1, 0,
        0, 0, 0
    ],
    output: [
        -1, 1, 0,
        0, 1, 0,
        0, 0, 0
    ]
},
{
    input: [
        -1, 1, 0,
        0, 1, 0,
        0, 0, 0
    ],
    output: [
        -1, 1, 0,
        0, 1, 0,
        0, -1, 0
    ]
},
{
    input: [
        -1, 1, 0,
        0, 1, 0,
        0, -1, 0
    ],
    output: [
        -1, 1, 1,
        0, 1, 0,
        0, -1, 0
    ]
},
{
    input: [
        -1, 1, 1,
        0, 1, 0,
        0, -1, 0
    ],
    output: [
        -1, 1, 1,
        0, 1, 0,
        -1, -1, 0
    ]
},
{
    input: [
        -1, 1, 1,
        0, 1, 0,
        -1, -1, 0
    ],
    output: [
        -1, 1, 1,
        1, 1, 0,
        -1, -1, 0
    ]
},
{
    input: [
        -1, 1, 1,
        1, 1, 0,
        -1, -1, 0
    ],
    output: [
        -1, 1, 1,
        1, 1, -1,
        -1, -1, 0
    ]
},
{
    input: [
        -1, 1, 1,
        1, 1, -1,
        -1, -1, 0
    ],
    output: [
        -1, 1, 1,
        1, 1, -1,
        -1, -1, 1
    ]
},
]);

const output = net.run([0, 0, 0, 0, 0, 0, 0, 0, 0]);

console.log(output)
<script src="https://unpkg.com/brain.js"></script>

the output is

000 010 000

Is my code right?

1

There are 1 best solutions below

0
Haroldo_OK On

It's a beginning. It learned that it should start at the middle if it can. Now, train it with lots of examples, so it can learn how to play in full.

Also, if you happen to have a "perfect" tic tac toe player algorithm at hand, you could use it to generate the labeled training data for the model.