GoL working on Apple II
This commit is contained in:
parent
85d9f44888
commit
a48026f9a1
108
gol_apple2.c
108
gol_apple2.c
|
@ -11,7 +11,8 @@
|
|||
|
||||
void init_display( void ); /* Inits displayed playfield */
|
||||
void draw_cells( void ); /* lets the user draw some starting cells */
|
||||
void toggle_cell( const uint8_t x, const uint8_t y ); /* toggles the cell at the given coordinates */
|
||||
uint8_t toggle_cell( const uint8_t x, const uint8_t y ); /* toggles the cell at the given coordinates. \
|
||||
Returns the cursor X position */
|
||||
|
||||
void run( void ); /* runs the simulation */
|
||||
void update( void ); /* updates the simulation */
|
||||
|
@ -21,7 +22,7 @@ void quit( void );
|
|||
|
||||
/******************* CUSTOM TYPES AND VALUES DEFINITIONS ****************/
|
||||
|
||||
#define NB_LINES 20
|
||||
#define NB_LINES 23
|
||||
#define NB_COLUMNS 40
|
||||
|
||||
#define ALIVE '0'
|
||||
|
@ -47,10 +48,12 @@ int main( int argc, char** argv )
|
|||
|
||||
/* Initial state */
|
||||
memset( Cells, DEAD, sizeof(Cells) );
|
||||
|
||||
|
||||
init_display();
|
||||
|
||||
/* go */
|
||||
draw_cells();
|
||||
run();
|
||||
|
||||
quit();
|
||||
return 0;
|
||||
}
|
||||
|
@ -76,25 +79,49 @@ void init_display( void )
|
|||
cputcxy ( 0u , NB_LINES-1u,'+');
|
||||
cputcxy ( NB_COLUMNS-1u, 0u,'+');
|
||||
cputcxy ( NB_COLUMNS-1u, NB_LINES-1u,'+');
|
||||
/*for( i = 0u; i < NB_COLUMNS; ++i ) {
|
||||
cputcxy (i,0u,'+');
|
||||
}
|
||||
for( i = 0u; i < NB_COLUMNS; ++i ) {
|
||||
cputcxy (i,NB_LINES-1u,'+');
|
||||
}
|
||||
for( i = 0u; i < NB_LINES; ++i ) {
|
||||
cputcxy (0u,i,'+');
|
||||
}
|
||||
for( i = 0u; i < NB_LINES; ++i ) {
|
||||
cputcxy (NB_COLUMNS-1u,i,'+');
|
||||
}*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
void draw_cells( void )
|
||||
{
|
||||
/* TBD */
|
||||
#define KEY_LEFT 'j'
|
||||
#define KEY_DOWN 'k'
|
||||
#define KEY_UP 'i'
|
||||
#define KEY_RIGHT 'l'
|
||||
|
||||
uint8_t quit = 0;
|
||||
uint8_t ch;
|
||||
|
||||
uint8_t x = NB_COLUMNS >> 1u;
|
||||
uint8_t y = NB_LINES >> 1u;
|
||||
gotoxy(x,y);
|
||||
|
||||
while ( quit == 0)
|
||||
{
|
||||
cursor(1);
|
||||
ch = cgetc();
|
||||
switch (ch) {
|
||||
case KEY_LEFT:
|
||||
if( x > 1u ) { gotoxy( --x, y ); }
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
if( y < NB_LINES-2u ) { gotoxy( x, ++y ); }
|
||||
break;
|
||||
case KEY_UP:
|
||||
if( y > 1u ) { gotoxy( x, --y ); }
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
if( x < NB_COLUMNS-2u ) { gotoxy( ++x, y ); }
|
||||
break;
|
||||
case ' ':
|
||||
x = toggle_cell( x, y );
|
||||
break;
|
||||
case 'q':
|
||||
quit = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Cells was updated by the calls to toggle() */
|
||||
memcpy( Cells_Future, Cells, sizeof(Cells_Future) );
|
||||
|
@ -102,32 +129,61 @@ void draw_cells( void )
|
|||
}
|
||||
|
||||
|
||||
void toggle_cell( const uint8_t x, const uint8_t y )
|
||||
uint8_t toggle_cell( const uint8_t x, const uint8_t y )
|
||||
{
|
||||
char* cell;
|
||||
if( x == 0u || x >= NB_COLUMNS-1 || y == 0u || y >= NB_LINES-1 ) { return; }
|
||||
char* cell;
|
||||
if( x == 0u || x >= NB_COLUMNS-1u || y == 0u || y >= NB_LINES-1u ) { return x; }
|
||||
cell = &Cells[x][y];
|
||||
if( *cell == DEAD ) {
|
||||
*cell = ALIVE;
|
||||
} else {
|
||||
*cell = DEAD;
|
||||
}
|
||||
/*mvaddch(y,x,*cell);
|
||||
refresh();
|
||||
*/
|
||||
cputc( *cell ); /* shift the cursor */
|
||||
return wherex ();
|
||||
}
|
||||
|
||||
|
||||
void run( void )
|
||||
{
|
||||
#define PERIOD_MS 500000u
|
||||
/* TBD */
|
||||
char str_nb_iteration [5];
|
||||
uint16_t nb_iterations = 2u;
|
||||
char ch = '\0';
|
||||
cursor(0);
|
||||
|
||||
gotoxy( 0u, NB_LINES );
|
||||
printf("Iteration: 1");
|
||||
while( ch != 'q')
|
||||
{
|
||||
update();
|
||||
gotoxy(11u, NB_LINES);
|
||||
printf( itoa(nb_iterations++, str_nb_iteration, 10) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void update( void )
|
||||
{
|
||||
/* TBD */
|
||||
uint8_t nb_neighbours;
|
||||
uint8_t x, y;
|
||||
for( y = 1u; y < NB_LINES-1; ++y )
|
||||
{
|
||||
for( x = 1u; x < NB_COLUMNS-1; ++x)
|
||||
{
|
||||
nb_neighbours = count_neighbours(x,y);
|
||||
if( Cells[x][y] == ALIVE && \
|
||||
(nb_neighbours < 2u || nb_neighbours > 3u )
|
||||
) {
|
||||
Cells_Future[x][y] = DEAD;
|
||||
cputcxy( x, y, DEAD );
|
||||
}
|
||||
else if( Cells[x][y] == DEAD && nb_neighbours == 3u ) {
|
||||
Cells_Future[x][y] = ALIVE;
|
||||
cputcxy( x, y, ALIVE );
|
||||
}
|
||||
}
|
||||
}
|
||||
memcpy( Cells, Cells_Future, sizeof(Cells) );
|
||||
}
|
||||
|
||||
|
||||
|
|
14
gol_linux.c
14
gol_linux.c
|
@ -90,10 +90,10 @@ void draw_cells( void )
|
|||
int quit = 0;
|
||||
|
||||
int ch;
|
||||
int y = NB_COLUMNS / 2;
|
||||
int x = NB_LINES / 2;
|
||||
int y = NB_LINES / 2;
|
||||
int x = NB_COLUMNS / 2;
|
||||
|
||||
move(x,y);
|
||||
move(y,x);
|
||||
refresh();
|
||||
|
||||
while ( quit == 0)
|
||||
|
@ -102,16 +102,16 @@ void draw_cells( void )
|
|||
ch = getch();
|
||||
switch (ch) {
|
||||
case KEY_LEFT:
|
||||
if( x > 0 ) { move(y, --x); }
|
||||
if( x > 1u ) { move(y, --x); }
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
if( y < NB_LINES-1 ) { move(++y, x); }
|
||||
if( y < NB_LINES - 2u ) { move(++y, x); }
|
||||
break;
|
||||
case KEY_UP:
|
||||
if( y > 0 ) { move(--y, x); }
|
||||
if( y > 1u ) { move(--y, x); }
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
if( x < NB_COLUMNS-1 ) { move(y, ++x); }
|
||||
if( x < NB_COLUMNS - 2u ) { move(y, ++x); }
|
||||
break;
|
||||
case ' ':
|
||||
toggle_cell(x,y);
|
||||
|
|
Loading…
Reference in New Issue