Robots Battle Project The project consists of two major parts: Parse the robot strategy files...
-
Upload
zechariah-weller -
Category
Documents
-
view
225 -
download
0
Transcript of Robots Battle Project The project consists of two major parts: Parse the robot strategy files...
![Page 1: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/1.jpg)
Robots Battle Project
The project consists of two major parts:
• Parse the robot strategy files
• Simulate the robots battle main flow– Execute robot strategy – Events– Animation (Interpolation)– Game Rules– Output Results
![Page 2: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/2.jpg)
The Simulator System Diagram
Input: Robot programs
WinnerGraphic output
Parse
Report errorExit
Simulator
Internal representation
Already Supplied
![Page 3: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/3.jpg)
Lexemes Tokens
• Define the strategy available tokens
For example “TURN” , “ASSIGN”,”PLUS” etc.
• You need to produce a well known defined tokens stream as input for the parsing process.
![Page 4: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/4.jpg)
Parsing
• Parse is one of the major task in the project.• Use good design in order to make your task
easier.• Before parsing, you need to lexical analyze the
strategy file to produce a well known tokens (lexemes)
• The parsing process will convert the tokens stream into some internal representation.
• A design proposal will presented next.
![Page 5: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/5.jpg)
Design hints (major modules)
Lexical analyzer
Parser
Simulator
• Lexical analyzer– Input program (ascii)
tokens
• Parser– Tokens Code
• Simulator– Code actions
Lexemes
Code
![Page 6: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/6.jpg)
Lexical analyzerComputeDistance{
$x2 = $x * $xprint(“Hello world”)exit()
}
#define NAME 1#define OP_PLUS 2#define OP_TIMES 3#define PRINT 4#define STOP 5#define OPEN 6#define CLOSE 7#define CURLY_OPEN 8#define CURLY CLOSE 9#define VARIABLE 10
Tokens.h
Lex.strLexemes Stream
Lexeme Value
NAME ComputeDistance
CURLY_OPEN
VARIABLE x2
OP_EQUALS
VARIABLE x
OP_TIMES
VARIABLE x
OPEN
STRING Hello World
CLOSE
EXIT
OPEN
CLOSE
CURLY_CLOSE
![Page 7: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/7.jpg)
Parsing• Input a “token’s stream”• Examine the validity of
the program• Produce code
#123456
CommandAssignAssignExpAssignExpTurnMoveReturn
P1$xTIMESGREATER10$x2
P27$x2$tmp
P3
$x$x2
ParseSample{
$x=7$x2 = $x * $x$tmp = $x2 > 100Turn(10)Move($x2)
}
Parse.str
P4
$x100
Next23456-1
#define ASSIGN 1#define ASSIGNEXP 2#define TURN 3#define MOVE 4#define RETURN 5#define IF 6...
Commands.h
![Page 8: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/8.jpg)
Parsing if
#1234567
CommandAssignAssignExpAssignExpIfFirePrintStringReturn
P1$xTIMESGREATER$tmp
“Done”
P27$x2$tmp6
P3
$x$x2
ParseIfSample{
$x=7$x2 = $x * $x$tmp = $x2 > 100if $tmp
Fire()endifprint(“Done”)
}
ParseIf.str
P4
$x100
Next234567-1
![Page 9: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/9.jpg)
Parsing While
#1234
CommandAssignwhileAssignExpReturn
P1$x$xMINUS
P274$x
P3
$x
ParseSample{
$x=7while $x
$x=$x-1endwhilereturn
}
P4
1
Next232-1
ParseWhile.str
![Page 10: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/10.jpg)
Parsing function call#123456
CommandAssignReturnAssignCallPrintExpReturn
P1TIMES
$x1$y
P2$y
7
P3$x
Sqr{
$y=x*x}
ParseSample{
$x=7Sqr()print($y)
}
P4$x
Next2-1456-1
ParseFunc.str
Function LineSqr 1ParseSample 3
![Page 11: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/11.jpg)
System Simulator
• Main purpose is to simulate the battle flow.
• Execute robots strategy files.
• Simulate motion animation (laser beam, robots).
• Manage the event system.
Identify the event and call to the relevant routine handling.
![Page 12: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/12.jpg)
Main Game Loop
• Infinite loop which called every CLOCK_PER_SECOND_TICK
• Execute robots programs• Manage the game rules• Manages the motions of the game
– Collision– Position– Events
![Page 13: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/13.jpg)
Main Game Loop Pseudo Code
tick=1
ForeverFor each robot
Execute one instruction
Move robot (If needed)
Move laser beam (If needed)
Check collision, end game?, events calling
Render Scene
++tick
main loop
![Page 14: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/14.jpg)
Motion Interpolation• In order to simulate the motion of the robots and the
laser beam you need to use linear interpolation as follows:
#define MOVE_SPEED_FACTOR //number of pixels for each tick
#define MOVE_TIME_STEPS(d) 1.0/d * MOVE_SPEED_FACTOR// center coordinates of the robotStartPositionX = Robot.xStartPositionY = Robot.y// (X,Y) destination coordinates after moving by dist with //respect to the (0,0)dest_X = dist*sin(robot.direction*PI/180)dest_Y = dist*cos(robot.direction*PI/180)time = [0.0,1.0]While(time < 1){ time += MOVE_TIME_STEPS(dist) Robot.x = StartPositionX + dest_X * time Robot.y = StartPositionY + dest_Y * time}
![Page 15: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/15.jpg)
Interpolation
T = 0.0
T = 1.0
T = 0.5
Distance To Travel
![Page 16: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/16.jpg)
Software engineering
• Modularity
• Functionality
• Documentation
• Naming convention
• Data structures
• Design-Implement-Test
![Page 17: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/17.jpg)
Functionality
• Functions do one “thing”• Repeated code is a
function• The name of the function
should be self explanatory
• Functions are typically short < 60 lines
• Line length should be <= 80 characters
void fndel(void* item){
node* p = root; while (item != p->value)
p = p->next;
if (p != NULL){
p->next->prev = p->prev;p->prev->next = p->next;free(p);
}}
bad.c
void RemoveItem(void* item){
node* ItemsNode = Find(item);if (ItemsNode != NULL){
Delete(ItemsNode);}
}
good.c
![Page 18: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/18.jpg)
Naming and Documentation
• Document by writing “good” code– Naming convention
– Meaningful names
– Simple and working code
• Remarks when appropriate– Module header
– Function header
– Special parts of code
/* * Description: * Search the list for the * input item and delete it * if the item is in the list * Input: * item - to look for and * delete. * Remarks: * May change root to NULL if * the list contains one item. */void RemoveItem(void* item){
node* ItemsNode = Find(item)if (ItemsNode != NULL){
Delete(ItemsNode);}
}
documentation.c
![Page 19: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/19.jpg)
Simple Code
• Simple - working code is preferable.– Less bugs– Easy for others to read– Easy to maintain
void strcpy(char* d, char* s){
for (;*d++=*s++;);}
unreadable.c
void strcpy(char* d, char* s){
int i;
for (i=0; s[i] != ‘\0’; ++i)d[i] = s[i];
d[i] = ‘\0’;}
simple.c
![Page 20: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/20.jpg)
Modularity• A set of functions that manage
one entity– List (data structure)– Parser
• Purpose:– Design tool, divide a difficult
(large) problem to easier and smaller problems
– Distribute work among team members
– No code duplication• Less code• Fix a bug in one place• Ability to change design and
improve incrementally– Code re-use by writing
standalone modules
Graphics
List Hash
Error handling
Lexicalanalyzer
Parser Simulator
![Page 21: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/21.jpg)
Modularity
Implementation1. .h module interface
• Data structures of the module that the user uses• Function prototypes• Constants
2. .cpp implementation• Implementation of the functions that were
defined in the .h• Prototypes, functions and constants that are
internal to the module
![Page 22: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/22.jpg)
Modularity - implementation#ifndef __MYLIST_H#define __MYLIST_H
typedef struct _List{
struct _List *next;void *data;
} List;
List* NewList();void ListInsert(List* head, void
*item);
void ListDelete(List* head);
#endif /* __MYLIST_H */
list.h#include “list.h”
/* * Allocates and prepares a new * list. * Return: NULL in case of an error * or a pointer to the head of * the list. */List* NewList(){ List* new_list; new_list=malloc(sizeof(LIST));
if (new_list == NULL) return NULL;
new_list->next = NULL; new_list->data = NULL; return new_list;}
list.c
![Page 23: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/23.jpg)
Data Structures
Should be:• Generic• Handled through
functions that manipulate them.
{Node* new;
...new = malloc(sizeof(Node));new->item = item;new->next = list->rootlist->root = new;
...new = malloc(sizeof(Node));new->item = item2;new->next = list->rootlist->root = new;
}
ds.c
{...
ListInsert(list, item);...
ListInsert(list, item2);}
ds-function.c
![Page 24: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/24.jpg)
Design-Implement-Test
• Spend time on design before starting to code.
• Leave enough time for debugging and unexpected design flaws.
Design
Implement
Test/Debug
![Page 25: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/25.jpg)
Suggested schedule
• Design 1w• Implement 4w• integration (port) 2w• Testing 2w• Write Documentation [2w] In parallel
-------• Total 9w
![Page 26: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/26.jpg)
Asserts/* * Description: * Set an array value * Input: * array - pointer to the * “array structure” * item - to insert * * NOTES: * assume the position is * valid */void Insert(ARRAY* a, int
position, void* item){
assert(position >= 0);assert(position < a->size);a->data[position] = item;
}
array1.c/* * Description: * Set an array value * Input: * array - pointer to the array * structure * item - to insert * * NOTES: * validate that the position * is valid */void Insert(ARRAY* a, int position,
void* item){
if ( (position >= 0) &&(position < a->size) )
{a->data[position] = item;
}}
array2.c
![Page 27: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/27.jpg)
How to use asserts
• For every parameter of a function– assert (parameter meets assumption)
• For every call to a function– assert(result meets assumptinos)
• Validate results of computations
• Validate access to NULL points
![Page 28: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/28.jpg)
Using global variables
#include “FileManager.h”
FilesList* files;...
FileManager.c
#include “FileManager.h”
{...
Write(files, 1, data);...}
Process.c
#ifndef __FILEMANAGER_H#define __FILEMANAGER_H
typedef struct _FilesList{
. . .} FilesList;
extern FilesList* files;#endif /* __FILEMANAGER_H */
FileManager.h
![Page 29: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/29.jpg)
Common pitfalls
• Plan your time– Start working early (now)
• Test your project – Try testing individual modules
![Page 30: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/30.jpg)
Partners
• Choose a partner you can work with
• Meet every week or so– See you’re both on track– Things change: synchronize
![Page 31: Robots Battle Project The project consists of two major parts: Parse the robot strategy files Simulate the robots battle main flow –Execute robot strategy.](https://reader030.fdocuments.in/reader030/viewer/2022033023/56649c7c5503460f9492fde8/html5/thumbnails/31.jpg)
Unix tools
• Editors:– emacs– vi– pico (easier to learn)
• Development environment– kdevelop
• Debuggers all based on gdb– gdb: command line debuger– ddd: gui to gdb
• Help– info or tkinfo– man pages