Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Is this a good way to implement a Battleship game?

by wickedxter (Beadle)
on Mar 31, 2006 at 19:14 UTC ( [id://540536]=perlquestion: print w/replies, xml ) Need Help??

wickedxter has asked for the wisdom of the Perl Monks concerning the following question:

Is this a good way of going by this?

The code snipplet is part of a game for calulating battles, it goes ship by ship each searching (randomly) targets of the other side and caluating if the ship has more then one weapon. If this doesnt explain enough just ask and i'll go through and break the code down.

It's kinda long.

## Battleship if($d_batt){ for my $x (1..$d_batt){ my $rand_ss = int rand(4); if($rand_ss eq 1 && $a_crv ne 0){ #weak target my $weak = int rand 2; if($weak eq 0 || $weak eq 2){ for (1..16){ my $w_s = int rand(100); next if $w_s gt $config->{percent_hit}{batt}; my $y = int rand $a_crv; $y++ if $y eq 0; push(@{$data->{shots}{defender}},"$y|crv|batt"); $data->fleet_d("defender","attacker"); } }else { for (1..16){ my $w_s = int rand(100); next if $w_s gt $config->{percent_hit}{batt}; my $weakest = $data->damaged("crv",$config->{armor}{cr +v},"attacker"); my ($num,$class) = split(/\|/,$weakest); push(@{$data->{shots}{defender}},"$num|$class|batt"); $data->fleet_d("defender","attacker"); } } }elsif ($rand_ss eq 2 && $a_des ne 0){ #next if $data->{shots}{$y}; #weak target my $weak = int rand 2; if($weak eq 0 || $weak eq 2){ for (1..16){ my $w_s = int rand(100); next if $w_s gt $config->{percent_hit}{batt}; my $y = int rand $a_des; $y++ if $y eq 0; push(@{$data->{shots}{defender}},"$y|des|batt"); $data->fleet_d("defender","attacker"); } }else { for (1..16){ my $w_s = int rand(100); next if $w_s gt $config->{percent_hit}{batt}; my $weakest = $data->damaged("des",$config->{armor}{de +s},"attacker"); my ($num,$class) = split(/\|/,$weakest); push(@{$data->{shots}{defender}},"$num|$class|batt"); $data->fleet_d("defender","attacker"); } } }elsif($rand_ss eq 3 && $a_corvette ne 0){ #next if $data->{shots}{$y}; for (1..16){ my $w_s = int rand(100); next if $w_s gt $config->{percent_hit}{batt}; my $y = int rand $a_corvette; $y++ if $y eq 0; push(@{$data->{shots}{defender}},"$y|corvette|batt"); $data->fleet_d("defender","attacker"); } }else { for (1..16){ my $w_s = int rand(100); next if $w_s gt $config->{percent_hit}{batt}; my $r_shipD = $data->random_ship("attacker","defender"); my ($num, $ship) = split(/\|/,$r_shipD); push(@{$data->{shots}{defender}},"$num|$ship|batt"); $data->fleet_d("defender","attacker"); } } }

READMORE added by Arunbear

2006-04-02 Retitled by planetscape, as per Monastery guidelines
Original title: 'This a good way?'

Replies are listed 'Best First'.
Re: Is this a good way to implement a Battleship game?
by lima1 (Curate) on Mar 31, 2006 at 19:32 UTC
    how large is your "move space", that means how many possible moves are there? normally, you give moves a score and choose then the best one. if the move spaces is small enough, you can evaluate all. then you can choose a optimization strategy, e.g. choose the n best moves and evaluate all (or good) opponents moves. and you should write
    if ($i == 1)
    for numbers.
    if ($i eq '1')
    for strings.
Re: Is this a good way to implement a Battleship game?
by CountZero (Bishop) on Mar 31, 2006 at 19:58 UTC
    Is this a good way

    It all depends: are you trying to optimise for speed or low memory consumption or a clean and expandable design or ... ? Strange as it may be some people even go for a maximum of obfuscation!

    I'm afraid that without some design goals no-one can tell you if you succeeded in reaching that goal.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Is this a good way to implement a Battleship game?
by nobull (Friar) on Apr 01, 2006 at 12:20 UTC
    It has already been pointed out that using eq to test the equality of numbers is wasteful affected.

    I should also add that using gt to compare numbers will not do what you probably intended because 3 gt 20 is true.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-18 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found