Title: Software Project Robots Battle
1Software ProjectRobots Battle
- Angela Enoshangela_at_tau.ac.il
- Office Schriber -18, 640-6114
- Web http//www.cs.tau.ac.il/angela/RobotsBattleS
rc/ ProjectSummer2005.htm
2Course outline
- Two classes at the beginning of the semester
- Work in groups of TWO.
- Address questions to me in person, by email or in
the forum. - Handout next week
- Initial design
- Sample UNIX program
3Outline cont.
- The project definition is in the web-page
- Get updated at the web page Every Week
- Programming language C or C
- Operating system linux in classroom 04
- 11-September-2005, the final project.
4Project overview
- Two robots are fighting each other on a battle
field. - Each robot tries to gain a victory by hitting the
enemy with a laser beam. - Robots are controlled by a strategy program
- It can Move, turns and fire.
- The robot programming language is an event driven
language.
5Project Overview
- Your program is composed of two major parts
- Understanding (parsing) the robot programs and
executes them - Simulating the flow of the game (collisions,
motion animation).
6The programming language
- Robot commands, such as Turn
- Event driven program
- Built-in variables
- User-defined variables
- User defined routines
- Simple arithmetic
7Sample program
User defined routine TurnAndFire
Turn(-25) Built-in routine Fire()
Built-in routine MyIdleStrategy
while(CurrentLifePoints gt 2) distance
random(0,359) Move(distance) Built In
RandomTurn() User Defined Fire()
Built-In routine endwhile while(true)
TurnAndFire() User Defined Endwhile
NWCH NorthWallCollisionHandler The robot
is too close to the north wall, turn
back turn(180) SWCH SouthWallCollisionHan
dler The robot is too close to the south
wall, turn back turn(-135)
Sample.str
8Sample Program (Cont)
Initialization code This is where the
program starts like main in C main
Register Events RegisterOnNorthWallCollisionAle
rt(NWCH, 1) RegisterOnSouthWallCollisionAlert(SWC
H, 1) Main Strategy its priority is the
smallest by default RegisterOnIdle(MyIdleStrategy
)
9Built-in Commands
- Robot Commands
- Move(distance)
- Turn(angle)
- Fire()
- Debug Commands
- Print("String")
- Print(variable)
- Exit()
10Event Built-in Commands
- Wall Collision Event Registration
- RegisterOnNorthWallCollistionAlert(function,
priority) - RegisterOnEastWallCollistionAlert(function,priorit
y) - RegisterOnWestWallCollistionAlert(function,priorit
y) - RegisterOnSouthWallCollistionAlert(function,priori
ty)
11Event Built-in Commands
- RegisterOnEnemyCollisionAlert(function,priority)
- RegisterOnLaserHit(function,priority)
- RegisterOnLaserAlert(function,priority)
- RegisterOnIdle(function)
12Robot Built-in Commands
-
- Turn(angle)
- Rotate the robot by the angle 0-360) around its
center. - The angle is in degrees.
- This is a relative angle with respect to the
current direction of the robot. -
- Positive angle - turn right , Negative angle -
turn left.
13Built-in Commands (Cont)
- Move(distance)
- Move in the current direction of the robot by the
specified distance and stop. - distance may be an integer or a variable of type
int - It is a continues command
14Built-in Commands (Cont)
- Fire()
- Fire laser beam from the center of the robot's
position. - The laser beam will move ahead in current robots
direction. - It is a continues command
15Events
- Types
- OnXWallCollisionAlert
- X North, East, West, South
- OnEnemyCollisionAlert
- OnLaserAlert
- OnLaserHit
- OnIdle
- Events have priority
- Only one event may be active at a time for each
robot
16Events Registration
- Event-handling-routines are registered
- Connect an event to a routine.
- Assign priority to event.
- It is possible to re-register an event.
WCH Wall Collision Handler Turn back to
avoid wall collision Turn(180)
Move(100) Main Initialization code Set
up event handlers RegisterOnNorthWallCollisionAle
rt(WCH, 1) RegisterOnEastWallCollisionAlert(WCH,
1) RegisterOnWestWallCollisionAlert(WCH,
1) RegisterOnSouthWallCollisionAlert(WCH, 1)
17Events Priority Policy
- If a high priority event occurs while a lower
priority event is - running, the lower priority event-handling-routi
ne is terminated - and the high priority event is executed.
- When the high priority event finishes, the
program turns to idle - If the same event occurs - it is ignored
- If a low or equal priority event occurs while a
high priority - event is running it is discarded.
18OnIdle Event
- Idle has priority that is smaller than any other
event. - If registered it is called when no other event
is running
MyIdleStrategy Cause this event to run
forever ... while (true) Turn(45) Fire()
endwhile Main RegisterOnIdle(MyIdleStrateg
y)
19Variables and Expressions
- Constant built-in Variables - sensors
- Built In Variables
- User Defined Variables
- Expressions
20Constant Variables - sensors
- Their values are defined in the source code (web)
- EnemyCollisionDistanceAlert ?
ENEMY_DISTANCE_ALERT -
- WallDistanceAlert ?
WALL_DISTANCE_ALERT -
- LaserDistanceAlert ?
LASER_DISTANCE_ALERT
21Built-in Variables
- There are several built-in variables
(Direction, CurrentLifePoints etc). - Built-in variables are read-only
- They cannot be changed by the robot program.
22Built-in Variables
- Direction
- CurrentPositionX
- CurrentPositionY
- NorthWallDistance
- EastWallDistance
- WestWallDistance
- SouthWallDistance
23Built-in Variables
- CurrentEnemyPositionX
- CurrentEnemyPositionY
- CurrentEnemyDirection
- CurrentLifePoints
- CurrentLaserBeamPositionX
- CurrentLaserBeamPositionY
- CurrentLaserBeamDirection
- IsEnemyLaserDataValid
- true
- false
24User Defined Variables
- A--Z(A--Z0--9)
- Strategy programmers can define variables.
- A variable holds real values (i.e. int float or
double). - Variables are always global.
- A variable is initialized when it is first used
and cannot be deleted
25Expressions
- Expressions are of the form
- ltValuegt (name number)
- ltValuegt ltopgt ltValuegt (BinOp)
- ltfunction gt (ltvaluegt) (e.g., cos, sin)
- ltfunction gt ( lt Value gt , lt Value gt ) (e.g.,
random(low,high))
x 5 y 17 x2 x x y2 y
y len2 x2 y2 len sqrt(len2) a
Sqrt(-3) this will return 0. Op , -, , /,
lt, gt, lt, gt, , !
26Flow Control - if
- if ( lt value BinOp gt ) else endif
- Controls the flow of a program.
- lt value gt is a constant or a value of a variable
- or BinOp.
-
- A 0 value means false, any other values are true.
- The else is optional.
27Flow Control - while
- while ( lt value BinOp gt ) endwhile
- Continue to execute the statements between the
while and endwhile as long as value ! 0.
Assignment ltvariablegtltexpgt Assign a value to
a variable. (e.g., xy5)
28Routines
- A--Z(A--Z0--9)
- Routines receive zero parameters and do not
return any value. - One routine can call another routine using the
following syntax FunctionName()''
29Routine Example
Compute the square of the variables dx and
dy and store the results in dx2 and dy2
respectively SQR2 dx2 dx dx
dy2 dy dy ComputeDistance dx
CurrentPositionX - CurrentEnemyPositionX
dy CurrentPositionY - CurrentEnemyPositionY
SQR2() d2 dx2 dy2 distance
sqrt(d2)
30LaserBeam
- Laser beam represented as a line segment with
distance of LASER_BEAM_LENGTH - struct LaserBeam
- double x, y //current position
- double direction//(0-360degree
- bool isVisible//set true when the laser need
- //to be drawn
31Robot
- Robot is represented as a triangle.
- Each robot can Turn, Fire and Move.
-
- struct Robot
- double x,y//current position
- double direction
- int hitPoints
- Status status//move stopped
- char name
- LaserBeam laserBeam
-
32Canonical Robot
P3 (0, h2/3)
(Cx,Cy)
P1(-w/2, -h/3)
P2(w/2, -h/3)
33SimpleTest
- int main (int argc, char argv)
- //Read Input
- //Parse
- //Create Internal Representation of the
- //strategies files.
- rs.robots0.setName("Megatron") //renderState
- rs.robots1.setName("Optimus")
- robotsBattleGrInit(animate)
- return 0
34SimpleTest (Cont)
- include "RobotsBattleGr.h"
- RenderSceneState rs
- static void animate()
-
- / Main Loop which update the current state
of - the scene each clock tick /
-
- /Update Scene Status/
- rs.robots0.Turn(2)
- rs.robots1.Turn(-7)
- rs.laserBeam(0).direction 45
- rs.laserBeam(0).x
- rs.laserBeam(0).y
- Render(rs)
35Simulation
- The simulation is performed on a two dimensional
RING_WIDTHRING_HEIGHT field. - the bottom-left corner of the ring is coordinate
(0,0). - Angles are clockwise, (Turn(90) ? turn right).
0,0
36RobotsBattleGr
- RobotsBattleGr handles the scene drawing of the
robots and laser beams. - Your task is to
- Update the robots and the laser beam
positions/directions - in world coordinate.
-
- Simulate motion for the robot and the laser
beam - Calculate collisions in the scene.
- Use SimpleTest as a starting point for your
project.
37Simulation (Cont)
- Input two robots programs (strategy files)
- The two robots programs run in parallel
- Work in time-steps (clock ticks)
- For every robot
- Execute a single instruction
- - Move robot if needed
- - Move laser beam if needed
38Simulation Command Line
- The command line is
- startbattle -c ltcyclesgt -s -n ltnumber of
battlesgt ltrobot's name gt ltits strategy file name
gt - ltrobot's name gt ltits strategy file namegt
- -c Set the maximal number of clock ticks for one
battle (default 2000). - -s (optional) run the simulation in a single step
mode. - -n ltnumber of battlesgt (optional) (default is
10). - For example
- startbattle Megatron Megatrone.str BumbaleBee
BumbaleBee.str ''
39Simulation Output
- The output of the simulation is as follows
- In case of an error (usually during parsing)
output - the following line and exit the program
- printf("Error Robot File Names lined,
- Descriptions\n", robot_file_name, line_number,
reason)
40Simulation Output
- Each simulation output should be appended to
BattleScore.res - --------------------------------------------------
------------------ - Battle Duration lttime duration of battle in
clock ticksgt - Battle Winner ltRobot Namegt
- Battle Score ltRobot A Life pointsgt ltRobot B
Life points gt - Battle Strategy ltRobot A Strategy File Namegt
ltRobot B Strategy File Namegt - Battle Remark ltcause of winning
- Enemy Destruction
Out of Ring Time outgt - --------------------------------------------------
------------------ - In case of a tie, the two robots' names will
appear in the "Battle Winner" line. - At the end of the file state the final winner
(after n battles)
41Requirements
- A working project
- Documentation
- A non-trivial robot program
42Developing software
Specifications
Design
User Manual
Implement
Documentation
Test
Final product Executable Documentation Examples
User Manual
43Design - Modules
- A set of related functions
- Manage a single entity
- Modules interact through an interface
- List module
- create
- insert
- delete
- find
44Modules
- You know your set of modules is OK if
- You can name your modules
- You can define the interaction between modules
- You can specify what services modules require
from one another (interface)
45The initial design
- Read the project definition carefully
- Include a diagram
- A short description of the modules
- List data structures that you will need
46Next week
- More about the project
- Software engineering
- Questions
47Makefile
myprogram f1.o f2.o f3.o f1.o f1.c
myslist.h f2.o f2.c graphics.h globals.h f3.o
f3.c globals.h
- Dependency tree
- DAG actually
myprogram
f1.c
mylist.h
f2.c
f3.c
globals.h
graphics.h
48Makefile
myprogram f1.o f2.o f3.o ltTABgt gcc o myprogram
f1.o f2.o f3.o f1.o f1.c myslist.h ltTABgt gcc c
Wall f1.c f2.o f2.c graphics.h
globals.h ltTABgt gcc c Wall f2.c f3.o f3.c
globals.h ltTABgt gcc c Wall f3.c
49Makefile
myprogram f1.o f2.o f3.o ltTABgt gcc o _at_
f1.o f1.c myslist.h ltTABgt gcc c Wall
lt f2.o f2.c graphics.h globals.h ltTABgt gcc c
Wall lt f3.o f3.c globals.h ltTABgt gcc c
Wall lt
- Dependency tree
- Commands
- Automatic variables
50Makefile
CFLAGS -c -g Wall LIBS -lm myprogram f1.o
f2.o f3.o ltTABgt gcc o _at_ (LIBS) f1.o f1.c
myslist.h ltTABgt gcc c (CFLAGS) lt f2.o f2.c
graphics.h globals.h ltTABgt gcc c (CFLAGS)
lt f3.o f3.c globals.h ltTABgt gcc c (CFLAGS)
lt
- Dependency tree
- Commands
- Automatic variables
- Variables
51Makefile
CFLAGS -g Wall LIBS -lm .c.o ltTABgt (CC)
c (CFLAGS) lt all myprogram myprogram f1.o
f2.o f3.o ltTABgt gcc o _at_ (LIBS) f1.o f1.c
myslist.h f2.o f2.c graphics.h globals.h f3.o
f3.c globals.h clean ltTABgt rm f .o
- Dependency tree
- Commands
- Automatic variables
- Variables
- Implicit rules
- Multiple targets
- Default make all
- make clean
- More information
- NOVA tkinfo make
52Debugging in Unix (gdb)
- Compile using gcc -g
- gdb myprogram
- l main - list the function main, l misc.cfoo -
list foo() in misc.c - b 52 - set a break point at line 52
- help
- where prints the stack
- up,down move in the stack, to inspect variables
of calling routines. - run the program
- n,s step over and step into
- Resources
- Online help while using GDB.
- Quick reference card, download from the web-page
- gt info GDB command on the unix