Created
December 9, 2018 12:24
-
-
Save lefuturiste/e7659f0cf2ce4acaae7166311def27fb to your computer and use it in GitHub Desktop.
Morpion with microbit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* WORK IN PROGRESS | |
* | |
* @author lefuturiste <[email protected]> | |
* @date 2018-12-09 | |
*/ | |
// 0: off | |
// 1: low luminosity (ennemi points) | |
// 2: blink quick (cursor) | |
// 3: full (me) | |
let state: Array<number> = [ | |
1, | |
1, | |
1, | |
1, | |
1, | |
1, | |
1, | |
1, | |
1 | |
] | |
let cursor = 0 | |
let blinkRegistration: Array<number> = [] | |
// 0 = game not started | |
// 1 = en attente de l'adversaire | |
// 2 = en attente du joueur | |
// 3 = game over: win | |
// 4 = game over: loose | |
let gameState = 0 | |
let isMaster = false | |
let posibilities: Array<Array<number>> = [ | |
[0, 1, 2], | |
[3, 4, 5], | |
[6, 7, 8], | |
[0, 3, 4], | |
[1, 4, 5], | |
[2, 5, 6], | |
[0, 4, 8], | |
[2, 4, 6] | |
] | |
let played: Array<number> = [] | |
let playedOther: Array<number> = [] | |
let minWinCount = 5 | |
let count = 0 | |
function registerBlink(index: number, x: number, y: number) { | |
if (blinkRegistration.filter((i: number) => i === index).length === 0) { | |
blinkRegistration.push(index) | |
basic.forever(() => { | |
if (state[index] == 2) { | |
led.toggle(x, y) | |
basic.pause(state[index] == 1 ? 250 : 150) | |
} | |
}) | |
} | |
} | |
function renderMatrice(): void { | |
basic.clearScreen() | |
state.forEach((value: number, index: number) => { | |
// convert to coordinates | |
let y = index >= 0 && index <= 2 ? 1 : index >= 3 && index <= 5 ? 2 : 3; | |
let x = index === 0 || index === 3 || index === 6 ? 1 : index === 1 || index === 4 || index === 7 ? 2 : 3; | |
if (value === 0) { | |
led.unplot(x, y) | |
} | |
if (value === 1) { | |
led.plot(x, y) | |
led.plotBrightness(x, y, 128) | |
} | |
if (value === 2) { | |
registerBlink(index, x, y) | |
} | |
if (value === 3) { | |
led.plot(x, y) | |
led.plotBrightness(x, y, 255) | |
} | |
}) | |
} | |
function resetScreen() { | |
state = state.map(function (value: number) { | |
return 0 | |
}) | |
renderMatrice() | |
} | |
function startTheGame(): void { | |
resetScreen() | |
gameState = 2 | |
state[0] = 2 | |
isMaster = true | |
radio.sendValue('started', 1) | |
renderMatrice() | |
} | |
// direction : 0: left; 1: right | |
function moveCursor(direction: number) { | |
if (gameState > 1) { | |
if (state[cursor] === 2) { | |
state[cursor] = 0; | |
} | |
if (direction === 0) { | |
if (cursor !== 0) { | |
cursor = cursor - 1; | |
} else { | |
cursor = 0; | |
} | |
} else { | |
if (cursor !== 8) { | |
cursor = cursor + 1; | |
} else { | |
cursor = 8; | |
} | |
} | |
if (state[cursor] === 0) { | |
state[cursor] = 2; | |
} | |
renderMatrice() | |
} | |
} | |
renderMatrice() | |
input.onButtonPressed(Button.B, function () { | |
moveCursor(1) | |
}) | |
input.onButtonPressed(Button.A, function () { | |
moveCursor(0) | |
}) | |
input.onButtonPressed(Button.AB, function () { | |
if (gameState == 0) { | |
startTheGame() | |
} else { | |
if (gameState == 2) { | |
// verify if the position is taken | |
if (state[cursor] != 1 && state[cursor] != 3) { | |
gameState = 1 | |
played.push(cursor) | |
radio.sendValue("played", cursor) | |
count = count + 1 | |
state[cursor] = 3 | |
renderMatrice() | |
led.plot(0, 0) | |
} | |
} | |
} | |
}) | |
radio.onReceivedValue((name: string, value: number) => { | |
if (name === 'started') { | |
console.log("game has started") | |
resetScreen() | |
gameState = 1 | |
isMaster = false | |
renderMatrice() | |
led.plot(0, 0) | |
} | |
if (name === 'played') { | |
console.log('played') | |
gameState = 2 | |
led.unplot(0, 0) | |
if (cursor === 0 && count === 0) { | |
state[0] = 2 | |
} | |
state[value] = 1 | |
count = count + 1 | |
playedOther.push(value) | |
if (count >= minWinCount) { | |
// verify win | |
let haveWin = posibilities.filter((value: number[]) => { | |
return value === played | |
}).length !== 0 | |
let haveLoose = posibilities.filter((value: number[]) => { | |
return value === playedOther | |
}).length !== 0 | |
console.log(`have win : ${haveWin}`) | |
console.log(`have win : ${haveLoose}`) | |
} | |
renderMatrice() | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment