sub generate_ship_pos::any_ship { my $size = shift; my @ship; while (1) { # Randomly place a ship @ship = (); my $x = int(rand(10)); my $y = int(rand(10)); my $orient = int(rand(2)); given ($orient) { when (0) { push @ship, [ $x, $y++ ] for 1 .. $size; } when (1) { push @ship, [ $x++, $y ] for 1 .. $size; } } redo if $x>9 or $y>9; # Try again if we go past the edge # Verify that it doesn't intersect another ship my $collisions=0; for my $P (@ship) { ++$collisions if $main::com_ships::com_board[$$P[0]][$$P[1]] != 0; } last unless $collisions; } for my $ar (@ship) { $main::com_ships::com_board[$$ar[0]][$$ar[1]] = 1; push @main::com_ships::com_all_ships, "$$ar[0]$$ar[1]"; } #push @main::com_ships::com_all_ships, map { "$$_[0]$$_[1]" } @ship; }