Software Project Robots Battle - PowerPoint PPT Presentation

1 / 52
About This Presentation
Title:

Software Project Robots Battle

Description:

... Project. Robots Battle. Angela Enosh. angela_at_tau.ac.il. Office: ... Web: http://www.cs.tau.ac.il/~angela/RobotsBattleSrc/ ProjectSummer2005.htm. Course outline ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 53
Provided by: shac9
Category:

less

Transcript and Presenter's Notes

Title: Software Project Robots Battle


1
Software 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

2
Course 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

3
Outline 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.

4
Project 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.

5
Project 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).

6
The programming language
  • Robot commands, such as Turn
  • Event driven program
  • Built-in variables
  • User-defined variables
  • User defined routines
  • Simple arithmetic

7
Sample 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
8
Sample 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
)
9
Built-in Commands
  • Robot Commands
  • Move(distance)
  • Turn(angle)
  • Fire()
  • Debug Commands
  • Print("String")
  • Print(variable)
  • Exit()

10
Event Built-in Commands
  • Wall Collision Event Registration
  • RegisterOnNorthWallCollistionAlert(function,
    priority)
  • RegisterOnEastWallCollistionAlert(function,priorit
    y)
  • RegisterOnWestWallCollistionAlert(function,priorit
    y)
  • RegisterOnSouthWallCollistionAlert(function,priori
    ty)

11
Event Built-in Commands
  • RegisterOnEnemyCollisionAlert(function,priority)
  • RegisterOnLaserHit(function,priority)
  • RegisterOnLaserAlert(function,priority)
  • RegisterOnIdle(function)

12
Robot 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.

13
Built-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

14
Built-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

15
Events
  • 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

16
Events 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)
17
Events 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.

18
OnIdle 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)
19
Variables and Expressions
  • Constant built-in Variables - sensors
  • Built In Variables
  • User Defined Variables
  • Expressions

20
Constant Variables - sensors
  • Their values are defined in the source code (web)
  • EnemyCollisionDistanceAlert ?
    ENEMY_DISTANCE_ALERT
  • WallDistanceAlert ?
    WALL_DISTANCE_ALERT
  • LaserDistanceAlert ?
    LASER_DISTANCE_ALERT

21
Built-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.

22
Built-in Variables
  • Direction
  • CurrentPositionX
  • CurrentPositionY
  • NorthWallDistance
  • EastWallDistance
  • WestWallDistance
  • SouthWallDistance

23
Built-in Variables
  • CurrentEnemyPositionX
  • CurrentEnemyPositionY
  • CurrentEnemyDirection
  • CurrentLifePoints
  • CurrentLaserBeamPositionX
  • CurrentLaserBeamPositionY
  • CurrentLaserBeamDirection
  • IsEnemyLaserDataValid
  • true
  • false

24
User 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

25
Expressions
  • 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, , !
26
Flow 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.

27
Flow 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)
28
Routines
  • 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()''

29
Routine 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)
30
LaserBeam
  • 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

31
Robot
  • 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

32
Canonical Robot
P3 (0, h2/3)
(Cx,Cy)
P1(-w/2, -h/3)
P2(w/2, -h/3)
33
SimpleTest
  • 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

34
SimpleTest (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)

35
Simulation
  • 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
36
RobotsBattleGr
  • 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.

37
Simulation (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

38
Simulation 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 ''

39
Simulation 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)

40
Simulation 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)

41
Requirements
  • A working project
  • Documentation
  • A non-trivial robot program

42
Developing software
Specifications
Design
User Manual
Implement
Documentation
Test
Final product Executable Documentation Examples
User Manual
43
Design - Modules
  • A set of related functions
  • Manage a single entity
  • Modules interact through an interface
  • List module
  • create
  • insert
  • delete
  • find

44
Modules
  • 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)

45
The initial design
  • Read the project definition carefully
  • Include a diagram
  • A short description of the modules
  • List data structures that you will need

46
Next week
  • More about the project
  • Software engineering
  • Questions

47
Makefile
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
48
Makefile
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
  • Dependency tree
  • Commands

49
Makefile
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

50
Makefile
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

51
Makefile
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

52
Debugging 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
Write a Comment
User Comments (0)
About PowerShow.com