Skip to content

Instantly share code, notes, and snippets.

@t-katsushima
Created May 8, 2020 06:09
Show Gist options
  • Select an option

  • Save t-katsushima/c8218720564b320ab59430c2500fcbd0 to your computer and use it in GitHub Desktop.

Select an option

Save t-katsushima/c8218720564b320ab59430c2500fcbd0 to your computer and use it in GitHub Desktop.
https://digital-shift-sisisin.unubo.app/ の操作 AI のスターターキット。ランダムジャンプするAIと、基本操作一式が入っている。
// Digital Shift Sisisin の操作AI
//
// 座標は { x: number; y: number } で表現する
// x座標は右がプラス、y座標は下がプラス
// sisisinのy座標は常に0
// フィールドオブジェクトElement達の取得
const rocks = document.getElementsByClassName('rock')
const p1chips = document.getElementsByClassName('point')
const p10chips = document.getElementsByClassName('daisuke')
const px2chips = document.getElementsByClassName('noboru')
const parseAxisLabel = str => str.split(/[:;]/)[1] // input example: "--x:123.45;"
// フィールドオブジェクトElementから座標の取り出し
const getPoint = elm => {
const lst = elm.getAttribute('style').split(' ')
return { x: parseAxisLabel(lst[0]), y: parseAxisLabel(lst[1]) }
}
// sisisin の座標の取得
const getSisisinCSSPoint = () => {
const mapToCSS = n => n * 0.45 // layerX * 0.45 == CSS_X
const simeX = parseAxisLabel(document.body.firstChild.children[0].getAttribute('style'))
return { x: mapToCSS(simeX), y: 0.0 }
}
// sisisin の操作
const rightJump = () => document.dispatchEvent(new KeyboardEvent('keydown', { shiftKey: true }))
const leftJump = () => document.dispatchEvent(new KeyboardEvent('keydown', { shiftKey: true, code: 'ShiftLeft' }))
// ユークリッド距離の算出
const calcDistance = (co1, co2) => Math.sqrt((co1.x - co2.x) ** 2 + (co1.y - co2.y) ** 2)
// 操作の実行
const run = () => {
// sisisin の座標
const sisisinPoint = getSisisinCSSPoint()
// 各フィールドオブジェクト達の座標の配列
const rockPoints = Array.from(rocks).map(getPoint)
const p1chipPoints = Array.from(p1chips).map(getPoint)
const p10chipPoints = Array.from(p10chips).map(getPoint)
const px2chipPoints = Array.from(px2chips).map(getPoint)
// 左右ジャンプをランダムに実行
const rand = Math.random()
if (rand < 0.333) rightJump()
else if (rand < 0.666) leftJump()
}
setInterval(run, 200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment