Skip to content

Commit

Permalink
Merge eb282bc into 6fd520c
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasEi committed Dec 2, 2019
2 parents 6fd520c + eb282bc commit b04fcd1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
31 changes: 27 additions & 4 deletions lib/gameState.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import PTNFileParser = require('ptn-file-parser')
import TakBoard = require('./takBoard')
import PTNFileParser = require('ptn-file-parser');
import TakBoard = require('./takBoard');
import Move = require('./move');

class GameState {
public size: number;
public playerOne: string = "Unkown";
public playerTwo: string = "Unkown";
public isGameOver: boolean = false;
public result: string = "";
private _board: TakBoard;

constructor(state: object = {size: 5}){
Expand All @@ -12,9 +17,27 @@ class GameState {

static fromPtnFile(file: string): GameState {
let ptnFile: any = PTNFileParser.parsePtnFile(file)
let size = new Number(ptnFile.tags['Size'])
let size = +ptnFile.tags['Size'];
let state = new GameState({size})
if(ptnFile.tags['Player1']) state.playerOne = ptnFile.tags['Player1'];
if(ptnFile.tags['Player2']) state.playerTwo = ptnFile.tags['Player2'];
// possible outcomes are R-0/0-R (road win), F-0/0-F (flat win), 1-0/0-1 (resignation), and 1/2-1/2 (draw)
if(ptnFile.tags['Result'] && (ptnFile.tags['Result'] as string).search(/[RF1]/gi) >= 0) {
state.isGameOver = true;
state.result = ptnFile.tags['Result'];
}
// ToDo! loop through moves and place pieces
return new GameState({size})
let moveNr = 0;
while(ptnFile.whiteMoves[moveNr]) {
state.move(Move.fromPtn(ptnFile.whiteMoves[moveNr]))
if(!ptnFile.blackMoves[moveNr]) break;
state.move(Move.fromPtn(ptnFile.blackMoves[moveNr]))
}
return state;
}

move(move: Move) {
throw new Error("Method not implemented.");
}
}

Expand Down
19 changes: 19 additions & 0 deletions lib/move.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const moveRegex = /(\d)?([CSF])?([a-h][1-8])(?:([<>+-])([1-8]+))?/g;

class Move {
private stone: string = "F";
constructor(ptn: string) {
let parsed = ptn.match(moveRegex);
if(!parsed) throw new Error("Invalid PTN string");
if(parsed[2]) {
this.stone = parsed[2];
}

}

static fromPtn(ptn: string): Move {
return new Move(ptn);
}
}

export = Move;
9 changes: 9 additions & 0 deletions test/gameStateTests.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
"use strict";
const expect = require("chai").expect;
const GameState = require("../dist/gameState")
const ptnFile = require('./resources/ptnFile')

describe("GameState", () => {
it("should assume a board size of 5", () => {
var state = new GameState();
expect(state.size).to.equal(5);
});
it("should read data from PTN file", () => {
var state = GameState.fromPtnFile(ptnFile.simplePtnFile);
expect(state.size).to.equal(6);
expect(state.playerOne).to.equal("NohatCoder");
expect(state.playerTwo).to.equal("fwwwwibib");
expect(state.isGameOver).to.be.true;
expect(state.result).to.equal("R-0");
});
});
20 changes: 20 additions & 0 deletions test/resources/ptnFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
exports.simplePtnFile = `[Site "PlayTak.com"]
[Event "Online Play"]
[Date "2018.10.28"]
[Time "16:10:44"]
[Player1 "NohatCoder"]
[Player2 "fwwwwibib"]
[Clock "10:0 +20"]
[Result "R-0"]
[Size "6"]
1. a6 f6
2. d4 c4
3. d3 c3
4. d5 c5
5. d2 Ce4
6. c2 e3
7. e2 b2
8. Cb3 1e4<1
9. 1d3<1 Sd1
10. a3 1d1+1`

0 comments on commit b04fcd1

Please sign in to comment.