2 <title>Finite Automata
</title>
4 <b>Your browser does not support JavaScript or JavaScript is disabled.
</b>
15 var stage
= new Array(); /* status of stage - all details stored here */
16 var store
= new Array(); /* status of input */
18 var stage_rows
= 8; /* number of rows on stage */
19 var stage_cols
= 9; /* number of columns on stage */
20 var store_bit_count
= 10; /* number of bits stored in machine */
22 var input_count
= 10; /* number of input bits */
24 var grid_status
= 1; /* turn grid lines on and off */
25 var grid_size
= 50; /* size in pixels of grid lines - both X and Y */
26 var canvas
; /* object id of canvas tag */
27 var canvas_input
; /* object id of input tag */
29 var ctx
; /* context of canvas */
30 var input
; /* context of input canvas */
32 /* each square on grid has an associated block of data tied to it */
33 function grid(type
, bit
, dir
, col
){
34 this.type
= type
; /* type of machine part */
35 this.bit
= bit
; /* data in this block - if any */
36 this.dir
= dir
; /* direction this item is turned */
37 this.col
= col
; /* color of machine part (if any) */
40 /* this is used to display all bits on all active items */
41 function test_types(){
42 var i
= 0, j
= 0, k
= 1, l
= 2, col
= 1;
43 for(i
= 0; i
< stage_rows
; i
++) {
44 for(j
= 0; j
< stage_rows
; j
++){
47 case 1: stage
[i
][j
].type
= "branch"; break;
48 case 2: stage
[i
][j
].type
= "bus"; break;
49 case 3: stage
[i
][j
].type
= "input"; break;
50 case 4: stage
[i
][j
].type
= "output"; break;
52 stage
[i
][j
].type
= "bitadd";
53 if(col
== 5) { col
= 1; }
55 case 1: stage
[i
][j
].col
= RED
; break;
56 case 2: stage
[i
][j
].col
= GREEN
; break;
57 case 3: stage
[i
][j
].col
= YELLOW
; break;
58 case 4: stage
[i
][j
].col
= BLUE
; break;
66 case 1: stage
[i
][j
].bit
= RED
; break;
67 case 2: stage
[i
][j
].bit
= GREEN
; break;
68 case 3: stage
[i
][j
].bit
= YELLOW
; break;
69 case 4: stage
[i
][j
].bit
= BLUE
; break;
76 /* create all output data structures and draw inital values on screen */
77 function init_stage(){
80 /* create blank grid data structure */
81 for(x
= 0; x
< stage_cols
; x
++) {
82 stage
[x
] = new Array();
83 for(y
= 0; y
< stage_rows
; y
++) {
84 stage
[x
][y
] = new grid('', '', '', '');
87 stage
[5][0].type
= "input";
88 stage
[5][0].bit
= "red";
89 stage
[5][1].type
= "branch";
90 stage
[5][2].type
= "bus";
91 stage
[5][6].type
= "branch";
92 stage
[5][7].type
= "branch";
93 stage
[5][8].type
= "output";
95 stage
[0][1].type
= "bus";
98 stage
[0][2].type
= "bus";
100 stage
[0][3].type
= "bus";
102 stage
[0][4].type
= "bus";
108 /* moves automata to next position */
109 function next_move(){
111 // determine what type of machine part we are on
113 // if we are on a bus move to next grid
115 // if we are on an input, make next bit dissappear from tape
116 // place bit on input
118 // if we are on a branch, determine which direction to move
121 // if we are on an output, remove bit and move to an input
124 /* set initial values for input */
125 function init_input(){
129 store
.push(GREEN
); /* add to end */
130 //store.unshift("red"); /* add to front */
131 //store.pop(); /* remove from end */
132 //store.shift(); /* remove from front */
135 /* draw faint gridlines on stage - used as a guide for the user */
136 function draw_grid(){
137 var x
, y
; /* current x and y position */
138 var offset
= 10; /* x and y maximum offset (far bottom or side of the window) */
139 ctx
.strokeStyle
= "#ccc";
141 /* draw vertical lines */
142 for(x
= grid_size
, y
= 0, offset
= window
.innerWidth
; x
< window
.innerWidth
; x
= x
+ grid_size
)
146 ctx
.lineTo(x
,y
+offset
);
150 /* draw horizontal lines */
151 for(x
= 0, y
= grid_size
, offset
= window
.innerWidth
; y
< window
.innerWidth
; y
= y
+ grid_size
)
155 ctx
.lineTo(x
+offset
,y
);
162 move through each grid in stage and draw contents.
163 this function can be used to refresh the screen at any time.
165 function draw_stage(){
167 /* loop through all grids on stage, drawing contents */
168 for(x
=0; x
< stage_cols
; x
++) {
169 for(y
= 0; y
< stage_rows
; y
++) {
175 function init_form(){
177 /* initalize canvas element for use */
178 canvas
= document
.getElementById("stage");
179 ctx
= canvas
.getContext("2d");
181 canvas_input
= document
.getElementById("input");
182 input
= canvas_input
.getContext("2d");
184 /* get width and height of window and set stage (canvas) with it. */
185 canvas
.height
= window
.innerHeight
-125;
186 canvas
.width
= window
.innerWidth
- 45;
187 if(grid_status
) {draw_grid(); }
194 /* returns coordinates of canvas in pixels */
195 function cnvs_get_coordinates(e
){
196 var x_offset
= canvas
.offsetLeft
;
197 var y_offset
= canvas
.offsetTop
;
198 if(canvas
== 'undefined') { alert("Canvas parameter is undefined"); }
199 x_offset
= e
.clientX
- x_offset
;
200 y_offset
= e
.clientY
- y_offset
;
201 document
.getElementById("xycoordinates").innerHTML
="Coordinates: (" + x_offset
+ "," + y_offset
+ ")";
202 return [x_offset
,y_offset
];
205 /* move through tape and draw bits */
206 function draw_tape(){
207 var i
= 0; var x
= 50;
208 input
.fillStyle
= "#f00";
209 while(i
< input_count
){
212 input
.fillStyle
= store
[i
];
213 input
.arc(x
,25,20,0,TWO_PI
,0);
216 input
.strokeStyle
= "#000";
219 input
.arc(x
,25,20,0,TWO_PI
,0);
227 /* (re)draws any map tile on grid */
228 function draw_tile(x
,y
){
230 ctx
.translate(grid_size
* x
, grid_size
* y
);
231 switch (stage
[x
][y
].type
){
233 draw_bitadd(stage
[x
][y
].col
);
239 draw_bus(stage
[x
][y
].dir
);
247 default: clear_square();
250 if(stage
[x
][y
].bit
){ draw_bit(stage
[x
][y
].bit
); }
254 /* draws small bit of correct color on grid */
255 function draw_bit(color
){
256 ctx
.fillStyle
= "#f00";
258 ctx
.fillStyle
= color
;
259 ctx
.arc(25,25,10,0,TWO_PI
,0);
262 ctx
.strokeStyle
= "#000";
265 ctx
.arc(25,25,10,0,TWO_PI
,0);
269 /* draw gray square with black outline */
270 function draw_input(){
272 ctx
.strokeStyle
= "#000";
273 ctx
.strokeRect(0,0,grid_size
,grid_size
);
274 ctx
.fillStyle
= "#aaa";
275 ctx
.fillRect(0,0,grid_size
,grid_size
);
278 /* a bus moves bits from one location to another */
279 function draw_bus(dir
){
282 ctx
.fillStyle
= "#aaa";
283 ctx
.strokeStyle
= "#000";
287 ctx
.translate(grid_size
,0);
291 ctx
.translate(grid_size
,grid_size
);
295 ctx
.translate(0,grid_size
);
301 if(i
== 1) { ctx
.save(); ctx
.translate(0, grid_size
/2); }
304 ctx
.lineTo(grid_size
/2,grid_size/2);
305 ctx
.lineTo(grid_size
,0);
306 ctx
.lineTo(grid_size
/2,grid_size/4);
312 ctx
.lineTo(grid_size
/2,grid_size/2);
313 ctx
.lineTo(grid_size
,0);
314 ctx
.lineTo(grid_size
/2,grid_size/4);
317 if(i
== 1) { ctx
.restore(); }
322 /* tiles branch movement of each bit */
323 function draw_branch(){
326 ctx
.fillStyle
= "#f00";
329 ctx
.lineTo(grid_size
/2,grid_size/2);
330 ctx
.lineTo(0,grid_size
);
335 ctx
.fillStyle
= "#000";
338 ctx
.lineTo(grid_size
/2,grid_size/2);
339 ctx
.lineTo(grid_size
,0);
345 ctx
.fillStyle
= "#00f";
347 ctx
.moveTo(grid_size
,0);
348 ctx
.lineTo(grid_size
/2,grid_size/2);
349 ctx
.lineTo(grid_size
,grid_size
);
354 ctx
.fillStyle
= "#aaa";
356 ctx
.moveTo(0,grid_size
)
357 ctx
.lineTo(grid_size
/2,grid_size/2);
358 ctx
.lineTo(grid_size
,grid_size
);
363 function draw_bitadd(color
){
367 ctx
.strokeStyle
= color
;
370 ctx
.strokeStyle
= "#000";
374 ctx
.moveTo(grid_size
/2,0);
375 ctx
.lineTo(grid_size
/2,grid_size
);
380 ctx
.moveTo(0, grid_size
/2);
381 ctx
.lineTo(grid_size
,grid_size
/2);
386 ctx
.strokeStyle
= "#000";
390 ctx
.lineTo(0,grid_size
);
391 ctx
.lineTo(grid_size
,grid_size
);
392 ctx
.lineTo(grid_size
,0);
398 /* clear this square by setting area to white */
399 function clear_square(){
400 ctx
.fillStyle
= "#fff";
401 ctx
.fillRect(1,1,grid_size
-2,grid_size
-2);
404 /* canvas has been clicked find out which grid and make correct change to square if needed. */
405 function cnvs_clicked(e
){}
409 <style type=
"text/css">
413 <body onLoad=
"init_form();">
415 <div id=
"topsection">
418 <button onClick='reset();'><img src="http://opentextbook.info/icons/32x32/resultset_first.png" title="Restart" alt="Restart"></button>
420 <button onClick='step_back();'><img src="http://opentextbook.info/icons/32x32/resultset_previous.png" title="Step Back" alt="Step Back"></button>
422 <button onClick='step_next();'><img src="http://opentextbook.info/icons/32x32/resultset_next.png" title="Next Step" alt="Next Step"></button>
424 <button onClick='run();'><img src="http://opentextbook.info/icons/32x32/resultset_last.png" title="Run" alt="Run"></button>
428 <button onClick='halt();'><img src="http://opentextbook.info/icons/32x32/cancel.png" title="Halt Execution" alt="Halt Execution"></button>
431 <button disabled><img src="http://opentextbook.info/icons/32x32/disk.png" title="Save Code" alt="Save Code"></button>
433 <button onClick='display_docs();'><img src="http://opentextbook.info/icons/32x32/book_open.png" title="Open Documentation" alt="Open Documentation"></button>
439 <div id=
"xycoordinates">Coordinates:
</div>
440 <canvas id=
"input" width=
"579" height=
"100"></canvas>
441 <canvas id=
"stage" width=
"579" height=
"770" onmousemove=
"cnvs_get_coordinates(event)" onclick=
"cnvs_clicked(event);">
442 Your browser does not support HTML5 Canvas.