<commit_before>//Translated from test_board_analyser.rb using babyruby2js
'use strict';

var main = require('../main');
var inherits = require('util').inherits;
var Group = require('../Group');
var GameLogic = require('../GameLogic');

var BLACK = main.BLACK, WHITE = main.WHITE;


/** @class Set main.debug to true for details
 */
function TestBoardAnalyser(testName) {
    main.TestCase.call(this, testName);
}
inherits(TestBoardAnalyser, main.TestCase);
module.exports = main.tests.add(TestBoardAnalyser);


TestBoardAnalyser.prototype.initBoard = function (gsize, handicap) {
    this.game = new GameLogic();
    this.game.newGame(gsize || 5, handicap || 0);
    this.goban = this.game.goban;
};

TestBoardAnalyser.prototype.checkGame = function (moves, expScore, gsize, finalPos) {
    this.initBoard(gsize || 5);
    if ('+O@'.indexOf(moves[0]) !== -1) {
        this.goban.loadImage(moves); // an image, not the list of moves
    } else {
        this.game.loadMoves(moves);
    }
    if (finalPos) this.assertEqual(finalPos, this.goban.image());
    this.boan = new main.defaultAi.BoardAnalyser();
    this.boan.countScore(this.goban);

    var score = this.goban.scoringGrid.image();
    if (score === expScore) return;
    this.showInUi('Expected scoring grid was:<br>' + expScore + ' but we got:<br>' + score);
    this.assertEqual(expScore, score);
};

TestBoardAnalyser.prototype.checkScore = function (prisoners, dead, score) {
    this.assertEqual(prisoners, Group.countPrisoners(this.goban), 'already prisoners');
    
    var futurePrisoners = this.boan.prisoners;
    this.assertEqual(dead[BLACK], futurePrisoners[BLACK] - prisoners[BLACK], 'BLACK dead');
    this.assertEqual(dead[WHITE], futurePrisoners[WHITE] - prisoners[WHITE], 'WHITE dead');

    this.assertEqual(score, this.boan.scores);
};

TestBoardAnalyser.prototype.showInUi = function (msg) {
    if (main.testUi) main.testUi.showTestGame(this.name, msg, this.game);
};

//---

TestBoardAnalyser.prototype.testUnconnectedBrothers = function () {
    this.checkGame('d4,c2,d2,e5,d6,e4,d5,d3,e3,c3,f4,f5,f6,f3,e6,e2,b5,b3,c4,a4,a5,a3,g6,pass,g4,g3,g5',
        '-------,---@@@@,@@-@OO@,O?@@O@@,OOOO?OO,::O&O::,:::::::', 7);
};

TestBoardAnalyser.prototype.testClearWinWithUnplayedMoves = function () {
    // Black wins clearly because he can make 2 eyes while white cannot.
    this.checkGame('b3,d3,c2,c3,b2,d2,c4,c1,d4,e4,d5,b1,e5,e3,b4,d1',
        '---@@,-@@@#,-@###,-@@#-,-###-');
};

TestBoardAnalyser.prototype.testSeeTwoGroupsSharingSingleEyeAreDead = function () {
    // 5 O&:&&
    // 4 O&:&&
    // 3 OO&&&
    // 2 :OOOO
    // 1 :::::
    //   abcde
    this.checkGame('b5,a5,b4,a4,d5,a3,d4,b3,c3,b2,d3,c2,e5,d2,e4,e2,e3',
        'O&:&&,O&:&&,OO&&&,:OOOO,:::::');
};

TestBoardAnalyser.prototype.testNoTwoEyes3_1 = function () {
    // 5 -----
    // 4 -----
    // 3 @@@@@
    // 2 ####@
    // 1 -@-#@
    //   abcde<commit_msg>Added 5 more scoring tests<commit_after>//Translated from test_board_analyser.rb using babyruby2js
'use strict';

var main = require('../main');
var inherits = require('util').inherits;
var Group = require('../Group');
var GameLogic = require('../GameLogic');

var BLACK = main.BLACK, WHITE = main.WHITE;


/** @class Set main.debug to true for details
 */
function TestBoardAnalyser(testName) {
    main.TestCase.call(this, testName);
}
inherits(TestBoardAnalyser, main.TestCase);
module.exports = main.tests.add(TestBoardAnalyser);


TestBoardAnalyser.prototype.initBoard = function (gsize, handicap) {
    this.game = new GameLogic();
    this.game.newGame(gsize || 5, handicap || 0);
    this.goban = this.game.goban;
};

TestBoardAnalyser.prototype.checkGame = function (moves, expScore, gsize, finalPos) {
    this.initBoard(gsize || 5);
    if ('+O@'.indexOf(moves[0]) !== -1) {
        this.goban.loadImage(moves); // an image, not the list of moves
    } else {
        this.game.loadMoves(moves);
    }
    if (finalPos) this.assertEqual(finalPos, this.goban.image());
    this.boan = new main.defaultAi.BoardAnalyser();
    this.boan.countScore(this.goban);

    var score = this.goban.scoringGrid.image();
    if (score === expScore) return;
    this.showInUi('Expected scoring grid was:<br>' + expScore + ' but we got:<br>' + score);
    this.assertEqual(expScore, score);
};

TestBoardAnalyser.prototype.checkScore = function (prisoners, dead, score) {
    this.assertEqual(prisoners, Group.countPrisoners(this.goban), 'already prisoners');
    
    var futurePrisoners = this.boan.prisoners;
    this.assertEqual(dead[BLACK], futurePrisoners[BLACK] - prisoners[BLACK], 'BLACK dead');
    this.assertEqual(dead[WHITE], futurePrisoners[WHITE] - prisoners[WHITE], 'WHITE dead');

    this.assertEqual(score, this.boan.scores);
};

TestBoardAnalyser.prototype.showInUi = function (msg) {
    if (main.testUi) main.testUi.showTestGame(this.name, msg, this.game);
};

//---


TestBoardAnalyser.prototype.testWeirdEmptyBoard = function () {
    // Just makes sure the analyser does not crash on empty boards.
    // Note that scoring these boards makes little sense so the 
    // result scoring grid is not really important.
    this.checkGame('', '+++++,+++++,+++++,+++++,+++++');
    this.checkGame('c3', '-----,-----,--@--,-----,-----');
    this.checkGame('c3,d5', '---#-,-----,--@--,-----,-----');
    this.checkGame('c3,d4', '?????,???#?,??&??,?????,?????');
};

TestBoardAnalyser.prototype.testDoomedGivesEye1 = function () {
    // White group is doomed; verify its eye is counted right
    this.checkGame('a2,b4,b2,c4,c2,d4,d2,e4,e2,b5,a3,c5,a4',
        '-##--,@####,@----,@@@@@,-----');
};

TestBoardAnalyser.prototype.testDoomedGivesEye2 = function () {
    // Same as above test but white's dead group does not "touch" black.
    // This triggers a special case in analyser (white is dead without "killer")
    this.checkGame('a2,a4,b2,b4,c2,b5,b1,c5,d2,c4,d1',
        '-##--,' +
        '###--,' +
        '-----,' +
        '@@@@-,' +
        '-@-@-');
};

TestBoardAnalyser.prototype.testDoomedGivesEye3 = function () {
    // Same as above but both groups have a single eye - they both die
    this.todo('Handle seki'); // both should stay alive instead
    this.checkGame('a2,a4,b2,b4,c2,b5,c3,pass,d3,pass,d4,pass,d5,pass,a1,pass,b1,pass,c1,pass,e3,pass,e4,pass,e5',
        '-#-&&,##-&&,--&&&,&&&::,&&&::');
};

TestBoardAnalyser.prototype.testTwoSigleEyeConnectedByFakeEye = function () {
    // Black SW and center groups survive depending on each other
    this.checkGame('a2,a3,b2,b3,c2,a4,b1,a5,c3,b6,b4,a6,b5,c6,c5,d6,d5,e6,e5,f6,f5,g5,f4,g4,f3,g3,d4,f2,e3,e2,pass,d2,pass,d3,d1,e1,c1,g1',
        ':::::::,' +
        'OOOOOO:,' +
        'O@@@@@O,' +
        'O@?@-@O,' +
        'OO@O@@O,' +
        '@@@OOO:,' +
        '-@@@O:O', 7);
    // and a variation that triggered a bug:
    this.checkGame('a2,a3,b2,b3,c2,a4,b1,a5,c3,b6,b4,a6,b5,c6,c5,d6,d5,e6,e5,f6,f5,g5,f4,g4,f3,g3,d4,f2,e3,e2,pass,d2,pass,d3,pass,g2',
        ':::::::,OOOOOO:,O@@@@@O,O@?@-@O,OO@O@@O,@@@OOOO,-@?????', 7);
};

TestBoardAnalyser.prototype.testUnconnectedBrothers = function () {
    this.checkGame('d4,c2,d2,e5,d6,e4,d5,d3,e3,c3,f4,f5,f6,f3,e6,e2,b5,b3,c4,a4,a5,a3,g6,pass,g4,g3,g5',
        '-------,---@@@@,@@-@OO@,O?@@O@@,OOOO?OO,::O&O::,:::::::', 7);
};

TestBoardAnalyser.prototype.testClearWinWithUnplayedMoves = function () {
    // Black wins clearly because he can make 2 eyes while white cannot.
    this.checkGame('b3,d3,c2,c3,b2,d2,c4,c1,d4,e4,d5,b1,e5,e3,b4,d1',
        '---@@,-@@@#,-@###,-@@#-,-###-');
};

TestBoardAnalyser.prototype.testSeeTwoGroupsSharingSingleEyeAreDead = function () {
    // 5 O&:&&
    // 4 O&:&&
    // 3 OO&&&
    // 2 :OOOO
    // 1 :::::
    //   abcde
    this.checkGame('b5,a5,b4,a4,d5,a3,d4,b3,c3,b2,d3,c2,e5,d2,e4,e2,e3',
        'O&:&&,O&:&&,OO&&&,:OOOO,:::::');
};

TestBoardAnalyser.prototype.testNoTwoEyes3_1 = function () {
    // 5 -----
    // 4 -----
    // 3 @@@@@
    // 2 ####@
    // 1 -@-#@
    //   abcde
{'fraction_non_alphanumeric': 0.21822721598002498, 'fraction_numerical': 0.03470661672908864, 'mean_word_length': 3.5724885844748857, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '747887', 'n_tokens_mistral': 3282, 'n_tokens_neox': 3229, 'n_words': 478}