sub place_ship { my ($x, $y, $size, $orient, $type) = @_; my @ship; print "$x, $y\n"; given (uc($orient)) { when ('U') { push @ship, [--$x, $y] for 1 .. $size } when ('D') { push @ship, [++$x, $y] for 1 .. $size } when ('L') { push @ship, [$x, --$y] for 1 .. $size } when ('R') { push @ship, [$x, $y++] for 1 .. $size } when ('X') { push @ship, [$x++, $y++] for 1 .. $size } when ('Y') { push @ship, [$x--, $y++] for 1 .. $size } } print "$x, $y\n"; return 0 if $x<0 or $y<0 or $x>9 or $y>9; my $collisions=0; for my $P (@ship) { ++$collisions if $main::user_ships::user_board[$$P[0]][$$P[1]] != 0; } print "col=$collisions\n"; return 0 if $collisions; for my $P (@ship) { $main::user_ships::user_board[$$P[0]][$$P[1]] = substr($type,0,1); push @main::user_ships::all_ships, "$$P[0]$$P[1]"; } 1; } sub get_user_ship_pos::any_size { my ($name, $size) = @_; print_board(\@main::user_ships::user_board); while (1) { print "Position your $name: ('A5 r' starts at A5 and goes right):"; chomp(my $input = ); if ($input =~ m/([A-J]\d0?)\s*([UDLRXY])/i) { # Coordinates look good, try placing the ship my ($start, $dir) = ($1, $2); my ($let, $num) = split '', $start; $let = letter_to_coord($let); last if place_ship($let, $num, 3, $dir, $name); } print "You have entered invalid input. Please try again.\n\n"; } }