Skip to content

Instantly share code, notes, and snippets.

@goldeneggg
Created September 12, 2011 14:02
Show Gist options
  • Save goldeneggg/1211323 to your computer and use it in GitHub Desktop.
Save goldeneggg/1211323 to your computer and use it in GitHub Desktop.
DevQuiz 2011 神経衰弱の解答(Chrome Extension)
{
"name": "ChromeExtensionSolverHint",
"version": "1.0",
"description": "Open the first card and show background color of the card.",
"content_scripts": [
{
"matches": [
"http://gdd-2011-quiz-japan.appspot.com/webgame/problem*"
],
"js": [
"solver.js"
]
}
],
"permissions": [
]
}
if(!Array.prototype.contains ) {
Array.prototype.contains = function( value ){
for(var i in this){
if( this.hasOwnProperty(i) && this[i] === value){
return true;
}
}
return false;
}
}
function checkEnd(i) {
return document.getElementById('card' + i) == null;
}
function getNextI(i, complete) {
while(true) {
if (complete.contains(i)) {
i++;
if (checkEnd(i)) {
// もう要素がない
break;
}
} else {
// まだ勝負付けが済んでない
break;
}
}
return i;
}
function fight(bc, i, myevent) {
var e = document.getElementById('card' + i)
e.dispatchEvent(myevent);
return bc == e.style.backgroundColor;
}
var element = document.getElementById('card0');
if (element == null) {
alert('Card element is not found. Check element id.');
} else {
var myevent = document.createEvent('MouseEvents');
myevent.initEvent('click', false, true);
element.dispatchEvent(myevent);
var bi = 0;
var be = document.getElementById('card' + bi);
var bc = be.style.backgroundColor
var i = 1;
var complete = [];
var isEnd = false;
while(!isEnd) {
if (checkEnd(i)) {
// おわり
isEnd = true;
} else {
// 勝負開始or勝負
if (bc != null) {
// 勝負!
if (fight(bc, i, myevent)) {
// 勝ち
complete.push(bi);
complete.push(i);
if (bi == (i - 1)) {
// 隣り合ったカードで勝ったとき
bi = getNextI(bi + 2, complete);
} else {
bi = getNextI(bi + 1, complete);
}
i = getNextI(bi + 1, complete);
bc = null;
} else {
// 負け
i++;
}
var firstElem = document.getElementById('card' + bi)
firstElem.dispatchEvent(myevent);
} else {
// 勝負開始
bc = document.getElementById('card' + bi).style.backgroundColor;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment