Game of Life i C
Hej!
Jag sitter här med en smärre hjärnblödning över en uppgift kallad Game of Life (https://sv.wikipedia.org/wiki/Game_of_Life).
Jag skriver programmet i C.
Koden ser ut enligt följande i sin helhet (jag tror dock jag vet vart de största problemen är just nu och klipper ut dem nedan):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* Constants, representation of states */
#define ALIVE 'X'
#define DEAD '.'
#define FIELDSIZE 5
/* Declaration of data structure */
typedef struct{
char current;
char next;
} cell;
/* Declaration of functions */
void initField(const int rows, const int cols, cell field[rows][cols]);
void loadGlider(const int rows, const int cols, cell field[rows][cols]);
void loadSemaphore(const int rows, const int cols, cell field[rows][cols]);
void loadRandom(const int rows, const int cols, cell field[rows][cols]);
void loadCustom(const int rows, const int cols, cell field[rows][cols]);
void printField(const int rows, const int cols, cell field[rows][cols]);
void countNeighborhood(const int rows, const int cols,
int array[rows][cols], cell field[rows][cols]);
/* Function: main
* Description: Start and run games, interact with the user.
* Input: About what initial structure and whether to step or exit.
* Output: Information to the user, and the game field in each step.
*/
int main(void) {
cell world[FIELDSIZE][FIELDSIZE];
int countNeighbor [FIELDSIZE+1][FIELDSIZE+1] = {{0}};
initField(FIELDSIZE,FIELDSIZE, world);
printField (FIELDSIZE, FIELDSIZE, world);
countNeighborhood(FIELDSIZE, FIELDSIZE, countNeighbor, world);
printf("\n");
/*Skriver ut antalet grannar till en cell endast för felsökning*/
for (int i = 0; i < FIELDSIZE; i++){
for (int j = 0; j < FIELDSIZE; j++){
printf("%d ", countNeighbor[i][j]);
}
printf("\n");
}
return 0;
}
/* Function: initField
* Description: Initialize all the cells to dead, then asks the user about
* which structure to load, and finally load the structure.
* Input: The field array and its size.
* Output: The field array is updated.
*/
void initField(const int rows, const int cols, cell field[rows][cols]) {
for (int r = 0 ; r < rows ; r++) {
for (int c = 0 ; c < cols ; c++) {
field[r][c].current = DEAD;
}
}
printf("Select field spec to load ([G]lider, [S]emaphore, [R]andom ");
printf("or [C]ustom): ");
int ch = getchar();
switch (ch) {
case 'g':
case 'G':
loadGlider(rows, cols, field);
break;
case 's':
case 'S':
loadSemaphore(rows, cols, field);
break;
case 'r':
case 'R':
loadRandom(rows, cols, field);
break;
case 'c':
case 'C':
default:
loadCustom(rows, cols, field);
break;
}
/* Ignore following newline */
if (ch != '\n') {
getchar();
}
}
/* Function: loadGlider
* Description: Inserts a glider into the field.
* Input: The field array and its size.
* Output: The field array is updated.
*/
void loadGlider(const int rows, const int cols, cell field[rows][cols]) {
field[0][1].current = ALIVE;
field[1][2].current = ALIVE;
field[2][0].current = ALIVE;
field[2][1].current = ALIVE;
field[2][2].current = ALIVE;
}
/* Function: loadSemaphore
* Description: Inserts a semaphore into the field.
* Input: The field array and its size.
* Output: The field array is updated.
*/
void loadSemaphore(const int rows, const int cols, cell field[rows][cols]) {
field[8][1].current = ALIVE;
field[8][2].current = ALIVE;
field[8][3].current = ALIVE;
}
/* Function: loadRandom
* Description: Inserts a random structure into the field.
* Input: The field array and its size.
* Output: The field array is updated. There is a 50 % chance that a cell
* is alive.
*/
void loadRandom(const int rows, const int cols, cell field[rows][cols]) {
/*srand(time(NULL)); bortkommenterad för att få samma resultat varje gång*/
for (int r = 0; r < FIELDSIZE; r++){
for (int c = 0; c < FIELDSIZE; c++){
int temp = rand( ) % 2;
if (temp == 0){
field[r][c].current = DEAD;
}
else{
field[r][c].current = ALIVE;
}
}
}
}
/* Function: loadCustom
* Description: Lets the user specify a structure that then is inserted into
* the field.
* Input: The field array and its size.
* Output: The field array is updated.
*/
void loadCustom(const int rows, const int cols, cell field[rows][cols]) {
printf("Give custom format string: ");
do {
int r, c;
scanf("%d,%d", &r, &c);
field[r][c].current = ALIVE;
} while (getchar() != '\n');
}
/* Function: PrintWorld
* Description: Prints the field to the screen.
* Input: The field array and its size.
* Output: N/A
*/
void printField(const int rows, const int cols, cell field[rows][cols]){
/*char choice;*/
/*do{*/
for (int rows = 0; rows < FIELDSIZE; rows++){
for (int cols = 0; cols < FIELDSIZE; cols++){
printf("%c ", field[rows][cols].current);
}
printf("\n");
}
/*printf("Select one of the following options: \n");
printf(" (enter) Step\n");
printf(" (any) Exit\n");
scanf("%c", &choice);
} while (choice == '\n'); */
}
/* Function: countNeighborhood
* Description: Counts the number of live cells one cell is surrounded by.
* Input: The field array and its size.
* Output: The neighborCount array.
*/
void countNeighborhood(const int rows, const int cols, int array[rows][cols],
cell field[rows][cols]){
/*Search for neighbors begins at locCoordj and locCoordj (Local Coordinate)
* represents rows and columns where the cell is located.*/
for (int i = 0; i <= FIELDSIZE; i++){
for (int j = 0; j <= FIELDSIZE; j++){
int locCoordj = j;
int locCoordi = i;
/*Which coordinates to search represented by
* srci and srcj (Search Coordinate)
* if neighbors are found the function adds 1 to an array value */
for (int srci = locCoordi-1; srci <= (locCoordi+1); srci++){
for(int srcj = locCoordj-1; srcj <= (locCoordj+1); srcj++){
if (field[srcj][srci].current == ALIVE && srci > 0 &&
srci < FIELDSIZE && srcj >= 0 && srcj < FIELDSIZE ){
array[j][i] += 1;
}
}
}
/*If the Cell is alive the search function counts it as a
* neighbor, this function compensates for that.*/
if (field[locCoordj][locCoordi].current == ALIVE){
array[j][i] -= 1;
}
}
}
}
Funktionerna initField, loadGlider, loadSemaphore och loadCustom är fullständiga och får ej ändras (enligt uppgiften).
Just nu när jag kör koden så är det denna funktion som strular, den räknar inte rätt längs kanterna.
/* Function: countNeighborhood
* Description: Counts the number of live cells one cell is surrounded by.
* Input: The field array and its size.
* Output: The neighborCount array.
*/
void countNeighborhood(const int rows, const int cols, int array[rows][cols],
cell field[rows][cols]){
/*Search for neighbors begins at locCoordj and locCoordj (Local Coordinate)
* represents rows and columns where the cell is located.*/
for (int i = 0; i <= FIELDSIZE; i++){
for (int j = 0; j <= FIELDSIZE; j++){
int locCoordj = j;
int locCoordi = i;
/*Which coordinates to search represented by
* srci and srcj (Search Coordinate)
* if neighbors are found the function adds 1 to an array value */
for (int srci = locCoordi-1; srci <= (locCoordi+1); srci++){
for(int srcj = locCoordj-1; srcj <= (locCoordj+1); srcj++){
if (field[srcj][srci].current == ALIVE && srci > 0 &&
srci < FIELDSIZE && srcj >= 0 && srcj < FIELDSIZE ){
array[j][i] += 1;
}
}
}
/*If the Cell is alive the search function counts it as a
* neighbor, this function compensates for that.*/
if (field[locCoordj][locCoordi].current == ALIVE){
array[j][i] -= 1;
}
}
}
}
Jag tror det beror på att mina villkor i for- loopen som är fucked up (se nedan) men jag kan ha fel.
for (int srcj = locCoordj-1; srcj <= (locCoordj+1); srcj++){
if (field[srcj][srci].current == ALIVE && srci > 0 &&
srci < FIELDSIZE && srcj >= 0 && srcj < FIELDSIZE)
All hjälp uppskattas.
Lurkar - läser mycket skriver lite. Vill du få min uppmärksamhet är det citat eller pm som gäller.
Jag anser att AIO-kylare har en plats i världen men det är INTE i fullstora ATX-system.