Games-GUI-Battleship

 view release on metacpan or  search on metacpan

lib/Games/GUI/Battleship/AI/Parity.pm  view on Meta::CPAN

        unify( $x1, $x2 );
    }

    # Priority 1: Line Extension (Horizontal)
    rule select_target( $x, $y, $mode ) {
        fresh my ( $x1, $x2, $yh );
        shot_hit( $x1, $yh );
        is_goal( $x2, [$x1], sub ($v) { $v + 1 } );
        shot_hit( $x2, $yh );
        unify( $y, $yh );
        line_endpoint_h( $x1, $x2, $x );
        cell_valid( $x, $y );
        unify( $mode, 'Line Extension (Horizontal)' );
    }
    rule line_endpoint_h( $x1, $x2, $x ) {
        is_goal( $x, [$x2], sub ($v) { $v + 1 } );
    }
    rule line_endpoint_h( $x1, $x2, $x ) {
        is_goal( $x, [$x1], sub ($v) { $v - 1 } );
    }

    # Priority 1: Line Extension (Vertical)
    rule select_target( $x, $y, $mode ) {
        fresh my ( $y1, $y2, $xh );
        shot_hit( $xh, $y1 );
        is_goal( $y2, [$y1], sub ($v) { $v + 1 } );
        shot_hit( $xh, $y2 );
        unify( $x, $xh );
        line_endpoint_v( $y1, $y2, $y );
        cell_valid( $x, $y );
        unify( $mode, 'Line Extension (Vertical)' );
    }
    rule line_endpoint_v( $y1, $y2, $y ) {
        is_goal( $y, [$y2], sub ($v) { $v + 1 } );
    }
    rule line_endpoint_v( $y1, $y2, $y ) {
        is_goal( $y, [$y1], sub ($v) { $v - 1 } );
    }

    # Priority 2: Orthogonal Neighbor Probe
    rule select_target( $x, $y, $mode ) {
        fresh my ( $hx, $hy );
        shot_hit( $hx, $hy );
        adjacent( $hx, $hy, $x, $y );
        cell_valid( $x, $y );
        unify( $mode, 'Orthogonal Target Probe' );

t/03-ai.t  view on Meta::CPAN


    my ( $x2, $y2, $mode2 ) = $ai->select_target;
    is(
        $mode2,
        'Line Extension (Horizontal)',
        'two horizontal hits trigger horizontal line extension'
    );

    # Endpoint should be either (3, 4) or (6, 4)
    is( $y2, 4, 'y coordinate stays aligned on row 4' );
    ok( $x2 == 3 || $x2 == 6, 'x coordinate is at a line endpoint (3 or 6)' );

    # Simulate sinking the ship
    my $destroyer = Games::GUI::Battleship::Ship->new(
        type => 'Destroyer',
        x    => 4,
        y    => 4,
        dir  => 'H',
    );
    $destroyer->record_hit( 4, 4 );
    $destroyer->record_hit( 5, 4 );

t/03-ai.t  view on Meta::CPAN

    # Simulate second hit at (6, 5)
    $ai->record_result( x => 6, y => 5, result => 'hit' );
    my ( $x3, $y3, $mode3 ) = $ai->select_target;
    is(
        $mode3,
        'Target Heatmap (Line Extension)',
        'switches to line extension after 2 collinear hits'
    );
    is( $y3, 5, 'y coordinate remains aligned with row 5' );
    ok( $x3 == 4 || $x3 == 7,
        "x coordinate is line endpoint (4 or 7), got $x3" );

    # Sinking Destroyer at (5, 5) and (6, 5)
    my $destroyer = Games::GUI::Battleship::Ship->new(
        type => 'Destroyer',
        x    => 5,
        y    => 5,
        dir  => 'H',
    );
    $destroyer->record_hit( 5, 5 );
    $destroyer->record_hit( 6, 5 );



( run in 2.598 seconds using v1.01-cache-2.11-cpan-cb04b646d8c )