Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Inputing vectors into a scrabble-esque game

by tybalt89 (Monsignor)
on Nov 02, 2019 at 15:46 UTC ( [id://11108247]=note: print w/replies, xml ) Need Help??


in reply to Inputing vectors into a scrabble-esque game

For input, how about just entering a string consisting of

a row digit of starting position of word
a column digit ditto
either a 'h' or a 'v' for whether the word is horizontal or vertical
the word

Example program:

#!/usr/bin/perl use strict; use warnings; my $board = ('.' x 10 . "\n") x 10; print "$board\n"; my $input = '23hacross'; # simulated read from STDIN place($input); print "$board\n"; $input = '16vdown'; # simulated read from STDIN place($input); print "$board\n"; sub place { my $input = shift; my ($row, $column, $direction, $word) = $input =~ /^(\d)(\d)(v|h)(.* +)/; my $position = 11 * $row + $column; for ( split //, $word ) { substr $board, $position, 1, $_; if( $direction eq 'h' ) { $position++; } else { $position += 11; } } }

Outputs:

.......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... ...across. .......... .......... .......... .......... .......... .......... .......... .......... ......d... ...across. ......w... ......n... .......... .......... .......... .......... ..........

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11108247]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found