Improve Your Aim with Artillery Duel: Play Against the Computer Now!

  • Thread starter MBMorrow
  • Start date
In summary, in this conversation, the speaker discusses adding a new dimension to their game, a randomly changing cannon height, and their current struggles with incorporating this into the projectile calculation. They also share the code for their game, including the main program written in Korn Shell and two other routines. The speaker asks for help understanding how to introduce the elevation variable and mentions that they got the original formulas and understanding of the physics from online forums.
  • #1
MBMorrow
3
0
Hi,
I recently wrote a text version of the old DOS Artillery Duel or Atari's Cannon Duel game. In this version, you play against the computer and can control the projectile's angle and velocity.

I'd now like to add one more dimension whereby the height of the cannons randomly changes for each game instantiation. (For example, the human's cannon might be at a 40 foot elevation while the computer's is at 10 feet).

The main calculation for the projectile is in the shot.awk script below. I've also included the other two routines in this game for anyone interested.


cannon.ksh - main program written in Korn Shell. Computer and human take turns shooting at one another

gettarget.awk - a simple awk script to get the target location (in this case the target is your computer enemy)

shot.awk - this is the routine (immediately below) where the distance is calculated based upon angle and velocity; and returns the cannon shot (to the main routine, cannon.ksh). I originally got the formulas and a basic understanding of the physics from forums like this one. However, I do not understand how to introduce the additional variable of elevation into the equation. I also don't understand many of the advanced notations and symbols used in formulas that do have a height variable. If you could use the word rather than the symbol (as you see coded in shot.awk) it would help me immensly.

From what I've read, I think I might have to introduce time into the equation. I can see how this would be required if I were plotting a point (on the screen) for graphics; however, I'm not sure it's necessary for this text version where we just print out how close the shot was to the target.

Thanks if you can help! -MB


shot.awk
---------
BEGIN {

# cannon.ksh passes in user-supplied angle and velocity
angle=ARGV[1]
ARGV[1]=""
velocity=ARGV[2]
ARGV[2]=""

gravity=(9.8*-1)

# convert angle to degrees
angle=(angle*3.14159265)/180

# original vector for x and y
vx=velocity*cos(angle)
vy=velocity*sin(angle)

# Midflight (velocity=0, no rise, no fall)
mf=(vy*-1)/gravity

# apex - used for graphics. Here for reference only
#apex=(vy*mf)+(0.5*gravity*(mf*mf))

# Fullflight is total time of flight
ff=mf*2

distance=vx*ff

printf("%3.0f\n",distance)
}


cannon.ksh
-----------
#! /bin/ksh

currentshot=0
keepshooting=1

# Computer variables
comp_angle=45
comp_hi=100.00
comp_low=1
currentguess=50.00


target=`awk -f gettarget.awk`

# Set level of difficulty
echo " "
echo "Select a level of difficulty: "
echo " "
echo "(e)asy: Displays both computer and human parameters"
echo "(m)edium: Displays only human parameters"
echo "(h)ard: Displays only + or - for human"
echo "(v)ery hard: Same as hard but does not even show you the distance to enemy"
echo " "
read difficulty
if [ "$difficulty" != "v" ]
then
echo " "
echo "Target distance: $target"
fi
echo " "
echo "Input angle (0-90) and velocity (1-100)"
echo " "

while [ $keepshooting -eq 1 ]
do
currentshot=`expr $currentshot + 1`

#
# Human shot
#

echo " "
read parms

angle=`echo $parms | awk ' { print $1 } '`
velocity=`echo $parms | awk ' { print $2 } '`

distance=`awk -f shot.awk $angle $velocity`

if [ $distance -eq $target ]
then
echo " "
echo " " # <--- this is Control-G to make the computer BEEP
echo " "
echo " "
echo " "
banner "BOOM!" # <--- banner is an old Unix utility to display the text in larger banner font
echo " "
echo "You hit the target at $distance feet in $currentshot shots! "
echo "Computer was using $comp_angle $currentguess"
echo " "
keepshooting=0
else

echo " "

if [ $distance -gt target ]
then
delta=`expr $distance - $target`
if [ "$difficulty" = "e" ] || [ "$difficulty" = "m" ]
then
echo "+${delta}"
else
if [ "$difficulty" = "h" ] || [ "$difficulty" = "v" ]
then
echo "+ you overshot"
fi
fi
else
delta=`expr $target - $distance`
if [ "$difficulty" = "e" ] || [ "$difficulty" = "m" ]
then
echo "-${delta}"
else
if [ "$difficulty" = "h" ] || [ "$difficulty" = "v" ]
then
echo "- you undershot"
fi
fi
fi

fi

#
# Computer shot
#

# Only take a shot if the human didn't just hit us
if [ $keepshooting -eq 1 ]
then
distance=`awk -f shot.awk $comp_angle $currentguess`

if [ $distance -eq $target ]
then
echo " "
echo " "
echo " "
echo " "
echo " "
banner "BAM!"
echo " "
echo "The computer got you at $comp_angle $currentguess in $currentshot shots! Distance: $target"
echo " "
keepshooting=0
else

echo " "

if [ $distance -gt target ]
then
delta=`expr $distance - $target`
echo " "

if [ "$difficulty" = "e" ]
then
echo "Computer: $comp_angle $currentguess +${delta}"
else
echo "Computer missed!"
fi

# low routine from guess
comp_hi=$currentguess
# need awk for fractions
currentguess=`echo $currentguess $comp_low | awk ' { printf("%2.2f",($1+$2)/2 ) } ' `

else
delta=`expr $target - $distance`
echo " "
if [ "$difficulty" = "e" ]
then
echo "Computer: $comp_angle $currentguess -${delta}"
else
echo "Computer missed!"
fi

# hi routine from guess
comp_low=$currentguess
# need awk for fractions
currentguess=`echo $currentguess $comp_hi | awk ' { printf("%2.2f",($1+$2)/2 ) } ' `

# Check for 99.99 (in order to guess 100) - must convert to int
tcurrentguess=`echo $currentguess | awk ' { printf("%d",$1*100) } ' `
if [ $tcurrentguess -eq 9999 ]
then
currentguess=100
fi
fi

fi
fi

done

exit

gettarget.awk
-------------
BEGIN {
trand=srand()
target=int(1000*rand())+1
printf("%d\n",target)
}
 
Physics news on Phys.org
  • #2
Cannon on a hill, lobbing the shot:
distance=vx*(vy/gravity+(sqrt((vy*vy)/(gravity*gravity)-2*height/gravity)))

Steep angle shot:
<snip> vx=velocity*cos(angle)
vy=velocity*sin(angle)

time=tgt_distance/vx

height=vy*time-.5*gravity*(time*time)

Steep angle downhill shot:

vx=velocity*cos(angle)
vy=velocity*sin(angle)

# For downhill tight shot, vertical velocity is negative
vy = vy * -1

time=tgt_distance/vx

height=vy*time-.5*gravity*(time*time)
 
  • #3



Hi, thank you for sharing your game with us. It sounds like a fun and challenging game to play. Adding the additional dimension of changing the height of the cannons will definitely make the game more interesting and require players to adjust their aim accordingly.

To introduce the elevation variable into the equation, you would need to use the Pythagorean theorem to calculate the height component of the projectile's trajectory. This would require you to use the initial velocity, angle, and time variables in the formula. The time variable is necessary because the height of the cannons will change throughout the flight of the projectile.

In terms of understanding the advanced notations and symbols used in the formulas, I suggest doing some online research or consulting a physics textbook for a better understanding. Additionally, you can try reaching out to physics forums for clarification or assistance in incorporating the elevation variable into your game.

Overall, I think the addition of the elevation variable will make your game even more exciting and challenging. Keep up the good work!
 

1. What is "Artillery Duel"?

Artillery Duel is a two-player game where each player takes control of an artillery tank and tries to destroy the opponent's tank by adjusting the angle and power of their shots.

2. How do you play "Artillery Duel"?

The game is typically played on a two-dimensional grid, where each player takes turns adjusting the angle and power of their shots. The goal is to hit the opponent's tank with your shots before they can hit yours.

3. Is "Artillery Duel" a physical or digital game?

"Artillery Duel" was originally a physical game released in the 1970s, but it has since been adapted into a digital format and is now available on various platforms such as computers and mobile devices.

4. Can "Artillery Duel" be played by more than two players?

No, "Artillery Duel" is designed to be played by two players only. However, there are variations of the game that allow for more players to join in, such as "Artillery Duel Tournament" where multiple players compete against each other in a tournament style.

5. Is there a strategy to winning "Artillery Duel"?

Yes, there are various strategies that can be used to increase your chances of winning in "Artillery Duel". These include adjusting your shots based on the terrain, using obstacles to your advantage, and predicting your opponent's moves. However, luck also plays a significant role in the game.

Similar threads

  • Other Physics Topics
Replies
2
Views
6K
Back
Top