This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
19 lines (19 loc) · 8.04 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>TPS viewer</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>
<script>
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?factory(exports,require("lodash")):typeof define==="function"&&define.amd?define(["exports","lodash"],factory):factory(global.TAK=global.TAK||{},global._)})(this,function(exports,lodash){"use strict";var babelHelpers={};babelHelpers.slicedToArray=function(){function sliceIterator(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break}}catch(err){_d=true;_e=err}finally{try{if(!_n&&_i["return"])_i["return"]()}finally{if(_d)throw _e}}return _arr}return function(arr,i){if(Array.isArray(arr)){return arr}else if(Symbol.iterator in Object(arr)){return sliceIterator(arr,i)}else{throw new TypeError("Invalid attempt to destructure non-iterable instance")}}}();babelHelpers.toConsumableArray=function(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++)arr2[i]=arr[i];return arr2}else{return Array.from(arr)}};babelHelpers;var RANKS="abcdefgh";var WHITE="1";var BLACK="2";function getInitialState(size){return{activeColor:WHITE,moveNumber:1,board:createEmptyBoard(size)}}function createEmptyBoard(size){return lodash.times(size,function(){return createEmptyRank(size)})}function createEmptyRank(size){return lodash.times(size,function(){return[]})}function getCoords(ptnMove){var matches=ptnMove.match(/([a-h])([1-8])/);var rank=matches[2]-1;var file=RANKS.indexOf(matches[1]);return[rank,file]}function tpsToJson(tps){var matches=tps.match(/\[TPS "(.*) (\d) (\d+)"\]/i);if(!matches){throw new Error(tps+" is not a valid TPS string")}var activeColor=matches[2];var moveNumber=Number(matches[3]);var tpsBoard=matches[1];var board=boardFromTps(tpsBoard);return{activeColor:activeColor,moveNumber:moveNumber,board:board}}function boardFromTps(tpsBoard){var tpsRanks=tpsBoard.split("/").reverse();return lodash.map(tpsRanks,rankFromTps)}function rankFromTps(tpsRank){var tpsSquares=tpsRank.split(",");return lodash.flatMap(tpsSquares,squaresFromTps)}function squaresFromTps(tpsSquares){var empty=tpsSquares.match(/x(\d)?/);if(empty){var numberOfSquares=empty[1]||1;return lodash.times(numberOfSquares,function(){return[]})}return[tpsSquares.match(/\d\D?/g)]}function jsonToTps(state){var tpsBoard=tpsFromBoard(state.board);tpsBoard=tpsBoard.replace(/x(,x)+/g,function(match){return"x"+Math.ceil(match.length/2)});return'[TPS "'+tpsBoard+" "+state.activeColor+" "+state.moveNumber+'"]'}function tpsFromBoard(board){return lodash.map(board,tpsFromRank).reverse().join("/")}function tpsFromRank(rank){return lodash.map(rank,tpsFromSquare).join(",")}function tpsFromSquare(square){return lodash.isEmpty(square)?"x":square.join("")}function getState(state,ptnMoves){return lodash.reduce(lodash.concat(ptnMoves),nextState,state)}function nextState(state,ptnMove){var newState=lodash.cloneDeep(state);var coords=getCoords(ptnMove);if(isPlacement(ptnMove)){var type=ptnMove.match(/^[CS]?/)[0];lodash.set(newState.board,coords,[getPlacedColor(state)+type])}else{var stones=lodash.get(newState.board,coords);var movingStoneCount=Number(ptnMove.match(/^\d*/)[0]||1);var movingStones=lodash.takeRight(stones,movingStoneCount);var dropCounts=getDropCounts(ptnMove);var direction=getMovementDirection(ptnMove);liftStones(newState.board,movingStoneCount,coords);moveStones(newState.board,movingStones,dropCounts,coords,direction)}if(state.activeColor===BLACK){newState.moveNumber++}newState.activeColor=invertColor(newState.activeColor);return newState}function liftStones(board,n,coords){lodash.update(board,coords,function(stones){return lodash.dropRight(stones,n)})}function moveStones(board,stones,dropCounts,fromCoords,direction){if(!lodash.isEmpty(dropCounts)){(function(){var dropCount=lodash.first(dropCounts);var droppedStones=lodash.take(stones,dropCount);var toCoords=getNextCoords(fromCoords,direction);lodash.update(board,toCoords,function(existingStones){var bottomStones=lodash.dropRight(existingStones,1);var topStone=lodash.last(existingStones);if(isCapstone(lodash.first(droppedStones))&&isStanding(topStone)){topStone=flatten(topStone)}if(topStone){bottomStones.push(topStone)}return[].concat(babelHelpers.toConsumableArray(bottomStones),babelHelpers.toConsumableArray(droppedStones))});moveStones(board,lodash.drop(stones,dropCount),lodash.drop(dropCounts),toCoords,direction)})()}}function isCapstone(stone){return stone==="1C"||stone==="2C"}function isStanding(stone){return stone==="1S"||stone==="2S"}function flatten(standingStone){return standingStone.replace("S","")}function getDropCounts(ptnMove){var movingStones=ptnMove.match(/^\d*/)[0]||"1";return(ptnMove.match(/[<>+-](\d*)/)[1]||movingStones).split("").map(Number)}function getPlacedColor(state){if(state.moveNumber===1){return invertColor(state.activeColor)}return state.activeColor}function invertColor(color){switch(color){case WHITE:return BLACK;case BLACK:return WHITE;default:return null}}function getNextCoords(coords,direction){return lodash.zipWith(coords,direction,function(a,b){return a+b})}function isPlacement(ptnMove){return!isMovement(ptnMove)}function isMovement(ptnMove){return/[<>+-]/.test(ptnMove)}var NORTH=[1,0];var SOUTH=[-1,0];var WEST=[0,-1];var EAST=[0,1];function getMovementDirection(ptnMove){var direction=ptnMove.match(/[<>+-]/)[0];switch(direction){case"+":return NORTH;case"-":return SOUTH;case"<":return WEST;case">":return EAST;default:return[0,0]}}function ptnToJson(ptn){var initialState=ptnToInitialState(ptn);var tags=ptnToTags(ptn);var _ptnToMovesAndComment=ptnToMovesAndComments(ptn);var moves=_ptnToMovesAndComment.moves;var comments=_ptnToMovesAndComment.comments;return{initialState:initialState,tags:tags,moves:moves,comments:comments}}function ptnToInitialState(ptn){var tps=ptn.match(/^\[TPS .*?\]/im);if(!tps){var boardSize=ptn.match(/^\[Size "(\d*?)"\]/im);if(!boardSize){throw Error("Unable to determine board size. The PTN must include TPS and/or Size.")}boardSize=Number(boardSize[1]);return getInitialState(boardSize)}return tpsToJson(tps[0])}function ptnToTags(ptn){var tags={};var tagsRegex=/^\[(?!.*TPS )(\S+)\s"(.*?)"]/gm;var nextTag=tagsRegex.exec(ptn);while(nextTag){var _nextTag=nextTag;var _nextTag2=babelHelpers.slicedToArray(_nextTag,3);var key=_nextTag2[1];var value=_nextTag2[2];tags[key.toLowerCase()]=value;nextTag=tagsRegex.exec(ptn)}return tags}function ptnToMovesAndComments(ptn){var moves={};var comments={};var movesRegex=/(\d+)\.+\s*([\w<>+-]+)\s+(?:{(.*?)})?\s*([\w<>+-]+)\s*(?:{(.*?)})?/gm;var nextMoves=movesRegex.exec(ptn);while(nextMoves){var _nextMoves=nextMoves;var _nextMoves2=babelHelpers.slicedToArray(_nextMoves,6);var moveNumber=_nextMoves2[1];var firstMove=_nextMoves2[2];var firstComment=_nextMoves2[3];var secondMove=_nextMoves2[4];var secondComment=_nextMoves2[5];var firstKey=moveNumber+" 1";var secondKey=moveNumber+" 2";if(secondMove){moves[firstKey]=firstMove;if(firstComment){comments[firstKey]=firstComment}moves[secondKey]=secondMove;if(secondComment){comments[secondKey]=secondComment}}else{moves[secondKey]=firstMove;if(firstComment){comments[secondKey]=firstComment}}nextMoves=movesRegex.exec(ptn)}return{moves:moves,comments:comments}}function getTps(tps,moves){var state=getState(tpsToJson(tps),moves);return jsonToTps(state)}exports.getTps=getTps;exports.getState=getState;exports.ptnToJson=ptnToJson;exports.jsonToTps=jsonToTps;exports.tpsToJson=tpsToJson;exports.getInitialState=getInitialState});
</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.0.28/css/bulma.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:400' rel='stylesheet' type='text/css'>
</head>
<body>
<app></app>
<!-- built files will be auto injected -->
</body>
</html>