C
ProgrammingAt this point, complex programs can be splitted into reusable/shared "libraries":
Header (structures and programs declarations):
// cells.h
#define DIM 2
typedef char* CELLS;
CELLS create();
void play (CELLS cells, char c, int p);
void play2(CELLS cells, char c, int line, int col);
void print(CELLS cells);
Implementation:
// cells.c
#include "cells.h"
#include <stdio.h>
#include <stdlib.h>
CELLS create() {
char* cells = malloc(DIM*DIM);
for (int p=0; p<DIM*DIM; p++)
*(cells+p) = ' ';
return cells;
}
void play(CELLS cells, char c, int p) {
*(cells+p) = c; // OR: cells[p] = c;
}
void play2(CELLS cells, char c, int line, int col) {
play(cells,c,DIM*line+col);
}
void print(CELLS cells) {
for (int p=0; p<DIM*DIM; p++) {
printf("%c",cells[p]);
if (((p+1)%DIM)==0) printf("\n");
}
}
Sample use:
// sample.c
// Usage: gcc cells.c sample.c -o sample.exe && ./sample.exe
#include "cells.h"
void main() {
CELLS cells = create();
play2(cells,'X',0,1);
print(cells);
}