Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
Florian Bouillon 2021-02-17 09:24:45 +01:00
parent 77e16fe6bf
commit a2ed5c57f0
Signed by: Florian Bouillon
GPG Key ID: 50BD648F12C86AB6
10 changed files with 4972 additions and 224 deletions

View File

@ -8,5 +8,5 @@
<body>
</body>
<script src="/dist/main.js"></script>
<script src="/src/v2.ts"></script>
</html>

View File

@ -7,6 +7,7 @@
"license": "MIT",
"dependencies": {
"@dzeio/object-util": "^1.0.4",
"parcel": "^1.12.4",
"serve": "^11.3.2",
"ts-node": "^9.0.0",
"typescript": "^4.1.2",
@ -22,6 +23,6 @@
"scripts": {
"dev": "webpack --watch",
"build": "webpack",
"serve": "serve ."
"serve": "serve ./dist"
}
}

View File

@ -1,190 +0,0 @@
enum Colors {
YELLOW,
RED,
BLUE,
GREEN,
PINK,
ORANGE
}
interface ColorPosition {
index: number
item: Colors
}
// interface Game {
// public constructor(points?: Array<Color>): Game
// }
// 1234
// 1111 = 4 / 1
// 1222 = 4 / 2
// 1233 = 4 / 3
// 1234 = 4 / 4
// 9999
// 1122 = 0 / 0
// 3344 = 0 / 0
// 5566 = 0 / 0
// 7788 = 0 / 0
// 9999 = 4 / 4
// 4751
// 1122 = 2 / 0
// 3313 = 1 / 0
// 4455 = 2 / 2
// 4666 = 1 / 1
// 7577 = 4 / 0
// ColorPositions possible
// xx1x,xxx1,2xxx,x2xx
// xxx1 !
// 4xxx,x4xx,xx4x,5xxx,x5xx,xx5x
// 4xxx ! x5xx, xx5x
// xx5x ! x7xx !
// nombres possibles
// 1, 2
// 1
// 1, 4, 5
// nombres exclus
// nil
// 2, 3
// 2, 3
// 2,3,6
const toWin = [Colors.PINK, Colors.ORANGE, Colors.BLUE, Colors.YELLOW]
// const length = toWin.length
const run = (input: Array<Colors>) => {
let possible = 0
let score = 0
for (let i = 0; i < toWin.length; i++) {
const winItem = toWin[i];
const compareItem = input[i]
if (winItem === compareItem) {
score++
continue
}
if (toWin.includes(compareItem)) {
possible++
}
}
return [possible, score]
}
let toCheck: Array<ColorPosition> = []
let confirmed: Array<ColorPosition> = []
let toRun = [Colors.YELLOW, Colors.YELLOW, Colors.RED, Colors.RED]
let [possibilities, correct] = run([Colors.YELLOW, Colors.YELLOW, Colors.RED, Colors.RED])
toCheck = toCheck.concat(calculatePossibilities(toRun, possibilities, correct))
updatePreviousMove(toRun, possibilities, correct)
console.log(toCheck, possibilities, correct)
toRun = [Colors.BLUE, Colors.BLUE, Colors.YELLOW, Colors.BLUE]
;[possibilities, correct] = run([Colors.BLUE, Colors.BLUE, Colors.YELLOW, Colors.BLUE])
toCheck = toCheck.concat(calculatePossibilities(toRun, possibilities, correct))
updatePreviousMove(toRun, possibilities, correct)
console.log(toCheck, possibilities, correct)
compareAndFilter(toRun, possibilities, correct)
console.log(toCheck, possibilities, correct)
// while (correct !== 4) {
// }
function possibleColors(items: Array<Colors>, possible: number): Array<Colors> {
const colorCount: Array<number> = Array(toWin.length)
for (const item of items) {
if(!colorCount[item]) {
colorCount[item] = 1
} else {
colorCount[item]++
}
}
console.log(colorCount)
const res: Array<Colors> = []
for (let i = 0; i < colorCount.length; i++) {
const element = colorCount[i];
console.log(element)
if (typeof element !== 'number') {continue}
if (element === possible) {
res.push(i)
}
}
console.log(res)
return res
}
function count(items: Array<Colors>, color: Colors) {
return items.filter((col) => col === color).length
}
const knownAsFalse: Array<ColorPosition> = []
let previousMove: undefined | {
sentItems: Array<Colors>
possible: number
correct: number
} = undefined
function compareAndFilter(sentItems: Array<Colors>, possible: number, correct: number) {
const colorsUsed = possibleColors(sentItems, possible)
const previousColors = possibleColors(previousMove.sentItems, previousMove.possible)
console.log(colorsUsed, previousColors)
for (const color of previousColors) {
console.log(colorsUsed.includes(color),
count(previousMove.sentItems, color) === previousMove.possible ,
count(colorsUsed, color) === possible)
if (
colorsUsed.includes(color) &&
count(previousMove.sentItems, color) === previousMove.possible &&
count(colorsUsed, color) === possible
) {
const indexes = sentItems.map((item, key) => key).filter((key) => sentItems[key] === color)
toCheck = toCheck.filter((item) => {
if (item.item !== color) {
return true
}
return indexes.includes(item.index)
})
}
}
}
function updatePreviousMove(sentItems: Array<Colors>, possible: number, correct: number) {
previousMove = {
sentItems,
possible,
correct
}
}
function calculatePossibilities(sentItems: Array<Colors>, possible: number, correct: number) {
const res: Array<ColorPosition> = []
if (possible === possibleColors(sentItems, possible).length) {
for (const item of sentItems) {
for (let i = 0; i < sentItems.length; i++) {
const compared = sentItems[i]
const toPush: ColorPosition = {index: i, item: item}
const index = res.findIndex((t) => t.index === toPush.index && t.item === toPush.item)
if (item !== compared && index === -1) {
res.push(toPush)
}
}
}
}
return res
}

View File

View File

@ -1 +0,0 @@
console.log('Hello World from your main file!');

123
src/v2.ts Normal file
View File

@ -0,0 +1,123 @@
enum Colors {
YELLOW,
RED,
BLUE,
GREEN,
PINK
}
interface ColorPosition {
index: number
item: Colors
}
const answer: Array<Colors> = [randomInt(5), randomInt(5), randomInt(5), randomInt(5)]
function generatePossibilities() {
const v = Object.values(Colors).filter((i) => typeof i !== 'string') as Array<Colors>
const l: Array<Array<Colors>> = []
v.forEach((v1) => {
v.forEach((v2) => {
v.forEach((v3) => {
v.forEach((v4) => {
l.push([v1, v2, v3, v4])
})
})
})
})
return l
}
/**
*
* @param items the input item
* @returns [the number of correct items, the number of correct color but not in the corretc place]
*/
function compare(items: Array<Colors>) {
let correct = 0
let colorIn = 0
const checkForColor: Array<Colors> = []
const anComparor: Array<Colors> = []
for (let i = 0; i < (items || []).length; i++) {
const item = items[i];
if (item === answer[i]) {
correct++
} else if (!checkForColor.includes(item)) {
checkForColor.push(item)
anComparor.push(answer[i])
}
}
for (const color of checkForColor) {
const index = anComparor.indexOf(color)
if (index !== -1) {
colorIn++
anComparor.splice(index, 1)
}
}
return [correct, colorIn]
}
/**
* List of possibilities
*/
let list = generatePossibilities()
let posPossible: Array<Array<Colors>> = [[], [], [], []]
function resolve(): void {
addRow(answer)
let i = 0
let guess: Array<Colors> = []
let won = false
do {
const index = randomInt(list.length)
guess = list.splice(index, 1)[0]
addRow(guess)
console.log(++i)
console.log('compare results', compare(guess), guess)
console.log('list length', list.length)
won = asWon(guess)
if (!won && list.length === 0) {
list = generatePossibilities()
}
} while (!won);
console.log('Victoire', guess, answer)
}
function randomInt(max: number) {
return Math.floor(Math.random() * max)
}
function asWon(guess: Array<Colors>) {
const [correct, colorsIn] = compare(guess)
if (correct === 4) {
return true
}
if (correct === 0) {
console.log(posPossible)
list = list.filter((item) => {
for (let index = 0; index < item.length; index++) {
const color = item[index];
if (colorsIn === 0) {
if (guess.includes(color)) return false
} else {
if (guess[index] === color) return false
}
}
return true
})
}
return false
}
function addRow(colors: Array<Colors>) {
const tr = document.createElement('tr')
for (const color of colors) {
const td = document.createElement('td')
td.classList.add('color-' + color)
tr.appendChild(td)
}
document.querySelector('table').appendChild(tr)
}
window.onload = resolve

View File

@ -3,7 +3,7 @@
"allowSyntheticDefaultImports": true,
"noImplicitAny": true,
"module": "es6",
"target": "es5",
"target": "es2016",
"allowJs": true
}
}

View File

@ -32,7 +32,7 @@ const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
mode: 'development',
entry: './src/index.ts',
entry: './src/v2.ts',
plugins: [new webpack.ProgressPlugin()],
module: {

4873
yarn.lock

File diff suppressed because it is too large Load Diff