Last active
January 1, 2016 15:39
-
-
Save mizuhara/8165347 to your computer and use it in GitHub Desktop.
An answer of http://nabetani.sakura.ne.jp/hena/1/
This file contains 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
var _ = require("./node_modules/underscore"); | |
exports.solve = function(src) { | |
'use strict'; | |
var first = [], | |
second = [], | |
curPlayer = [], | |
n = 0, | |
result = ''; | |
for(var i = 0; i < src.length; ++i) { | |
n = parseInt(src[i], 10); | |
curPlayer = { 0: first, 1: second }[i % 2]; | |
if( isLined(curPlayer.concat(n)) ) { | |
result = { 0: 'o won.', 1: 'x won.' }[i % 2]; | |
break; | |
} | |
if( isFoul(n, first, second) ) { | |
result = { 0: 'Foul : x won.', 1: 'Foul : o won.' }[i % 2]; | |
break; | |
} | |
curPlayer.push(n); | |
if( isGameFinished( first.concat(second) ) ) { | |
break; | |
} | |
} | |
return result === '' ? 'Draw game.' : result; | |
}; | |
function isLined(hand) { | |
'use strict'; | |
var a = [ | |
[1, 2, 3], [4, 5, 6], [7, 8, 9], | |
[1, 4, 7], [2, 5, 8], [3, 6, 9], | |
[1, 5, 9], [3, 5, 7] | |
]; | |
for(var i = 0; i < a.length; ++i) { | |
if(_.contains(hand, a[i][0]) && | |
_.contains(hand, a[i][1]) && | |
_.contains(hand, a[i][2])) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function isFoul(n, first, second) { | |
'use strict'; | |
var field = first.concat(second); | |
return _.contains(field, n); | |
} | |
function isGameFinished(field) { | |
'use strict'; | |
return field.sort(function(x, y) { | |
return y < x; | |
}).join() === '1,2,3,4,5,6,7,8,9'; | |
} |
This file contains 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
var tick_tack_toe = require('./tick_tack_toe'); | |
describe("tick_tack_toe.solve", function() { | |
var inputs = [ | |
['79538246', 'x won.'], | |
['35497162193', 'x won.'], | |
['61978543', 'x won.'], | |
['254961323121', 'x won.'], | |
['6134278187', 'x won.'], | |
['4319581', 'Foul : x won.'], | |
['9625663381', 'Foul : x won.'], | |
['7975662', 'Foul : x won.'], | |
['2368799597', 'Foul : x won.'], | |
['18652368566', 'Foul : x won.'], | |
['965715', 'o won.'], | |
['38745796', 'o won.'], | |
['371929', 'o won.'], | |
['758698769', 'o won.'], | |
['42683953', 'o won.'], | |
['618843927', 'Foul : o won.'], | |
['36535224', 'Foul : o won.'], | |
['882973', 'Foul : o won.'], | |
['653675681', 'Foul : o won.'], | |
['9729934662', 'Foul : o won.'], | |
['972651483927', 'Draw game.'], | |
['5439126787', 'Draw game.'], | |
['142583697', 'Draw game.'], | |
['42198637563', 'Draw game.'], | |
['657391482', 'Draw game.'] | |
]; | |
for(var i = 0; i < inputs.length; ++i) { | |
(function(src, expected) { | |
it("should return " + expected + " (" + i + ").", function() { | |
var actual = tick_tack_toe.solve(src); | |
expect(actual).toBe(expected); | |
}); | |
})(inputs[i][0], inputs[i][1]); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment