package Games::ABC_Path::Generator::RiddleObj;
$Games::ABC_Path::Generator::RiddleObj::VERSION = '0.6.0';
use 5.006;

use strict;
use warnings;

use Carp;

use integer;

use parent 'Games::ABC_Path::Solver::Base';

use Games::ABC_Path::Solver::Constants;


sub _solution
{
    my $self = shift;

    if (@_)
    {
        $self->{_solution} = shift;
    }

    return $self->{_solution};
}

sub _clues
{
    my $self = shift;

    if (@_)
    {
        $self->{_clues} = shift;
    }

    return $self->{_clues};
}

sub _A_pos
{
    my $self = shift;

    if (@_)
    {
        $self->{_A_pos} = shift;
    }

    return $self->{_A_pos};
}

sub _init
{
    my $self = shift;
    my $args = shift;

    $self->_solution( $args->{solution} );
    $self->_clues( $args->{clues} );
    $self->_A_pos( $args->{A_pos} );

    return;
}


sub get_letters_of_clue
{
    my ( $self, $args ) = @_;

    my $get_index = sub {
        my $i = $args->{index};

        if ( $i !~ m{\A[01234]\z} )
        {
            Carp::confess('index must be in the range 0-4');
        }

        return $i;
    };

    my $clue_idx;
    my $type = $args->{type};

    if ( $type eq 'col' )
    {
        $clue_idx = 2 + $LEN + $get_index->();
    }
    elsif ( $type eq 'row' )
    {
        $clue_idx = 2 + $get_index->();
    }
    elsif ( $type eq 'diag' )
    {
        $clue_idx = 0;
    }
    elsif ( $type eq 'antidiag' )
    {
        $clue_idx = 1;
    }
    else
    {
        Carp::confess("Unknown type $type.");
    }

    return [ map { $letters[ $_ - 1 ] } @{ $self->_clues->[$clue_idx] } ];
}


sub get_riddle_v1_string
{
    my ($self) = @_;

    my $s = ( ( ' ' x 7 ) . "\n" ) x 7;

    substr( $s, ( $self->_A_pos->y + 1 ) * 8 + $self->_A_pos->x + 1, 1 ) = 'A';

    my $clues = $self->_clues();
    foreach my $clue_idx ( 0 .. $NUM_CLUES - 1 )
    {
        my @pos =
              ( $clue_idx == 0 ) ? ( [ 0, 0 ], [ 6, 6 ] )
            : ( $clue_idx == 1 ) ? ( [ 0, 6 ], [ 6, 0 ] )
            : ( $clue_idx < ( 2 + 5 ) )
            ? ( [ 1 + $clue_idx - (2), 0 ], [ 1 + $clue_idx - (2), 6 ] )
            : (
            [ 0, 1 + $clue_idx - ( 2 + 5 ) ],
            [ 6, 1 + $clue_idx - ( 2 + 5 ) ]
            );

        foreach my $i ( 0 .. 1 )
        {
            substr( $s, $pos[$i][0] * 8 + $pos[$i][1], 1 ) =
                $letters[ $clues->[$clue_idx]->[$i] - 1 ];
        }
    }

    return $s;
}


sub get_final_layout
{
    my ($self) = @_;

    return $self->_solution;
}


sub get_final_layout_as_string
{
    my ( $self, $args ) = @_;

    return $self->_solution->as_string($args);
}


1;    # End of Games::ABC_Path::Generator

__END__

=pod

=encoding UTF-8

=head1 NAME

Games::ABC_Path::Generator::RiddleObj - represents a riddle.

=head1 VERSION

version 0.6.0

=head1 SYNOPSIS

    use Games::ABC_Path::Generator;

    my $gen = Games::ABC_Path::Generator->new({seed => 1});

    # Returns a Games::ABC_Path::Generator::RiddleObj object.
    my $riddle = $gen->calc_riddle();

    print $riddle->get_riddle_v1_string();
    print $riddle->get_riddle_string_with_header();

=head1 SUBROUTINES/METHODS

=head2 my $riddle = Games::ABC_Path::Generator::RiddleObj->new({%args});

Initialised a new riddle. Arguments are:

=over 4

=item * solution

The solution layout.

=item * clues

An array of the clues.

=item * A_pos

The position of the A cell.

=back

=head2 my [$letter1, $letter2] = $riddle->get_letters_of_clue({ type => $type, index => $index, })

Returns the two letters (as an array reference) associated with the
clue represented by the hash reference. Type can be:

=over 4

=item * 'col'

A column clue. C<'index'> points to the X coordinate.

=item * 'row'

A row clue. C<'index'> points to the Y coordinate.

=item * 'diag'

The diagonal clue. C<'index'> is ignored.

=item * 'antidiag'

The anti-diagonal clue. C<'index'> is ignored.

=back

Some examples:

    my $letters_aref = $riddle->get_letters_of_clue({ type => 'col', index => 2, });
    my $letters_aref = $riddle->get_letters_of_clue({ type => 'row', index => 1, });
    my $letters_aref = $riddle->get_letters_of_clue({ type => 'diag', });
    my $letters_aref = $riddle->get_letters_of_clue({ type => 'antidiag', });

=head2 my $string = $riddle->get_riddle_v1_string()

Returns the riddle version 1 string (without the header). See the documentation
of L<Games::ABC_Path::Solver::Board> for explanation.

=head2 my $string = $riddle->get_final_layout()

Returns the final layout as a L<Games::ABC_Path::Generator::FinalLayoutObj> .

=head2 my $string = $riddle->get_final_layout_as_string({%args})

Returns the final layout as a string. %args is included for future extension.

=head1 AUTHOR

Shlomi Fish, L<http://www.shlomifish.org/> .

=for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan

=head1 SUPPORT

=head2 Websites

The following websites have more information about this module, and may be of help to you. As always,
in addition to those websites please use your favorite search engine to discover more resources.

=over 4

=item *

MetaCPAN

A modern, open-source CPAN search engine, useful to view POD in HTML format.

L<https://metacpan.org/release/Games-ABC_Path-Generator>

=item *

RT: CPAN's Bug Tracker

The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.

L<https://rt.cpan.org/Public/Dist/Display.html?Name=Games-ABC_Path-Generator>

=item *

CPANTS

The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.

L<http://cpants.cpanauthors.org/dist/Games-ABC_Path-Generator>

=item *

CPAN Testers

The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.

L<http://www.cpantesters.org/distro/G/Games-ABC_Path-Generator>

=item *

CPAN Testers Matrix

The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.

L<http://matrix.cpantesters.org/?dist=Games-ABC_Path-Generator>

=item *

CPAN Testers Dependencies

The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.

L<http://deps.cpantesters.org/?module=Games::ABC_Path::Generator>

=back

=head2 Bugs / Feature Requests

Please report any bugs or feature requests by email to C<bug-games-abc_path-generator at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=Games-ABC_Path-Generator>. You will be automatically notified of any
progress on the request by the system.

=head2 Source Code

The code is open to the world, and available for you to hack on. Please feel free to browse it and play
with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
from your repository :)

L<https://github.com/shlomif/abc-path>

  git clone https://github.com/shlomif/abc-path

=head1 AUTHOR

Shlomi Fish <shlomif@cpan.org>

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website
L<https://github.com/shlomif/abc-path/issues>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2021 by Shlomi Fish.

This is free software, licensed under:

  The MIT (X11) License

=cut