19 svar
1566 visningar
Iamnotsmart 22 – Fd. Medlem
Postad: 18 okt 2020 20:06

game of life

Hej! Jag behöver hjälp med att komma vidare på denna uppgift, just nu som jag förstår det så fastnar programmet på cnt++ och jag vet inte hur jag kommer vidare... 

Programmet ser ut så här för tillfället: 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* Constants, representation of states */
#define ALIVE 'X'
#define DEAD '.'

/* 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 clearField(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]);
int countNeighbors(const int rows, const int cols, cell field[rows][cols], int row, int col);
int getState(int rows, int cols, cell field[rows][cols], int row, int col);
void liveOrDie(const int rows, const int cols, int array[rows][cols], cell field[rows][cols]);
void updateWorld(const int rows, const int cols, cell field [rows][cols]);


/* Function: main
* Description: Start and run simulations, interact with the user.
* Lets the user choose initial structure and whether to step
* or exit. Writes information to the user, and the game field
* in each step.
*/

int main(void) {

const int rows = 20;
const int cols = 20;
cell field[rows][cols];

initField(rows, cols, field);

printfield(rows, cols, field);

 

return 0;
}
/* Function: initField
* Description: Loads a structure that the user selects
* Input: rows - the number of rows in the field
* cols - the number of columns in the field
* field - the field array
* Output: The field array is updated.
*/
void initField(const int rows, const int cols, cell field[rows][cols]) {

clearField(rows,cols,field);

printf("Select field spec to load ([G]lider, [S]emaphore, [R]andom ");
printf("or [C]ustom): ");

int ch = getchar();

/* Ignore following newline */
if (ch != '\n') {
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;
}
}
/* Function: clearField
* Description: Initialize all the cells in the field to dead
* Input: rows - the number of rows in the field
* cols - the number of columns in the field
* field - the field array
* Output: The field array is updated.
*/
void clearField(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;
}
}
}
/* Function: loadGlider
* Description: Inserts a glider into the field.
* Input: rows - the number of rows in the field
* cols - the number of columns in the field
* field - the field array
* 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: rows - the number of rows in the field
* cols - the number of columns in the field
* field - the field array
* 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: rows - the number of rows in the field
* cols - the number of columns in the field
* field - the field array
* 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));
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; 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: rows - the number of rows in the field
* cols - the number of columns in the field
* field - the field array
* 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');
}

void printfield(const int rows, const int cols, cell field[rows][cols]){

do {
for (int r = 0; r < rows; r++) {
if (r !=0) {
printf("\n");
}
for (int c = 0; c < cols; c++) {
if (field[r][c].current==ALIVE) {
printf("X ");
}
else {
printf(". ");
}
}
}
printf("\n");
printf("Select one of the following options:\n (enter) Step\n");
printf(" (any) Exit \n");

} while (getchar() == '\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 r = 0; r <= rows; r++){
for (int c = 0; c <= cols; c++) {
array[r][c] = countNeighbors(rows, cols, field, r, c);
}
}
}

int countNeighbors(const int rows, const int cols, cell field[rows][cols], int row, int col){

int cnt = 0;

for (int r = row - 1; r <= row + 1; r++) {
for (int c = col - 1; c <= col + 1; c++) {
if ((row != r || col != c) &&
getState (rows, cols, field, r, c) == ALIVE) {
cnt++;
}
}
}

return cnt;
}

int getState(const int rows, const int cols, cell field[rows][cols], int row, int col)
{
if (row >= 0 && row < rows && col >= 0 && col < cols){
return field[row][col].current;
}
else {
return DEAD;
}

}
void liveOrDie(const int rows, const int cols, int array[rows][cols], cell field[rows][cols]){

countNeighborhood(rows, cols, array, field);

for (int r = 0; r < rows; r++){
for (int c = 0; c < cols; c++){
if (field[r][c].current == ALIVE && array[r][c] == 2){
field[r][c].next = ALIVE;
}
else if (array[r][c] == 3){
field[r][c].next = ALIVE;
}
else{
field[r][c].next = DEAD;
}
}

}
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
printf("%d ", array[i][j]);
}
printf("\n");
}
updateWorld (rows, cols, field);
}
void updateWorld(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 = field[r][c].next;
}
}
}

Laguna Online 28597
Postad: 18 okt 2020 20:09

Säger kompilatorn något, eller kan du köra programmet?

Iamnotsmart 22 – Fd. Medlem
Postad: 18 okt 2020 20:12
Laguna skrev:

Säger kompilatorn något, eller kan du köra programmet?

Nej det gör den inte

Laguna Online 28597
Postad: 18 okt 2020 20:18

Du får berätta mer om man ska kunna hjälpa dig. Nu har jag kompilerat och kört programmet. Det laddar en konfiguration, men ingenting ändrar sig när jag trycker Enter.

Vad menar du med "fastnar på cnt++"?

Iamnotsmart 22 – Fd. Medlem
Postad: 18 okt 2020 20:33
Laguna skrev:

Du får berätta mer om man ska kunna hjälpa dig. Nu har jag kompilerat och kört programmet. Det laddar en konfiguration, men ingenting ändrar sig när jag trycker Enter.

Vad menar du med "fastnar på cnt++"?

Det är de jag menar med att den fastnar på cnt++ eftersom den då hoppar ur loopen och inte gör något mer.... fattar inte hur jag ska komma vidare efter det för den ska ju ändra på sig allt eftersom man trycker på Enter

Iamnotsmart 22 – Fd. Medlem
Postad: 18 okt 2020 20:39
Iamnotsmart skrev:
Laguna skrev:

Du får berätta mer om man ska kunna hjälpa dig. Nu har jag kompilerat och kört programmet. Det laddar en konfiguration, men ingenting ändrar sig när jag trycker Enter.

Vad menar du med "fastnar på cnt++"?

Det är de jag menar med att den fastnar på cnt++ eftersom den då hoppar ur loopen och inte gör något mer.... fattar inte hur jag ska komma vidare efter det för den ska ju ändra på sig allt eftersom man trycker på Enter

Dessutom undrar jag om jag ens är på rätt spår??

joculator 5285 – F.d. Moderator
Postad: 18 okt 2020 20:41

Vad menar du att denna del av koden skall göra?

int ch = getchar();

/* Ignore following newline */
if (ch != '\n') {
getchar();
}

Laguna Online 28597
Postad: 18 okt 2020 20:48 Redigerad: 18 okt 2020 20:50

Jag tror du ska anropa funktionen liveOrDie nånstans.

Hur kom jag fram till det? Jag satte in en printf-sats där man räknar ut vad som ska leva, och såg att man inte kom dit, sen konstaterade jag att man inte ens kom in i den funktionen.

Iamnotsmart 22 – Fd. Medlem
Postad: 18 okt 2020 20:49
joculator skrev:

Vad menar du att denna del av koden skall göra?

int ch = getchar();

/* Ignore following newline */
if (ch != '\n') {
getchar();
}

Den delen av koden fick jag givet till mig.. funktionerna som var givna och som inte får ändras alls är:

initField
clearField
loadGlider
loadSemaphore
loadCustom

Den enda som var given men fick/var tvungen att ändra var loadRandom... 
Dessutom så skulle man fylla på main och sedan skriva egna funktioner

Iamnotsmart 22 – Fd. Medlem
Postad: 18 okt 2020 20:56
Laguna skrev:

Jag tror du ska anropa funktionen liveOrDie nånstans.

Hur kom jag fram till det? Jag satte in en printf-sats där man räknar ut vad som ska leva, och såg att man inte kom dit, sen konstaterade jag att man inte ens kom in i den funktionen.

Jag gjorde det nu och de gjorde jag i denna funktion så att den ser ut så här: 


void printfield(const int rows, const int cols, cell field[rows][cols]){

int array[rows][cols];

do {
for (int r = 0; r < rows; r++) {
if (r !=0) {
printf("\n");
}
for (int c = 0; c < cols; c++) {
if (field[r][c].current==ALIVE) {
printf("X ");
}
else {
printf(". ");
}
}
}
printf("\n");
printf("Select one of the following options:\n (enter) Step\n");
printf(" (any) Exit \n");
liveOrDie(rows, cols, array, field);

} while (getchar() == '\n');
}

Iamnotsmart 22 – Fd. Medlem
Postad: 18 okt 2020 20:58
Iamnotsmart skrev:
Laguna skrev:

Jag tror du ska anropa funktionen liveOrDie nånstans.

Hur kom jag fram till det? Jag satte in en printf-sats där man räknar ut vad som ska leva, och såg att man inte kom dit, sen konstaterade jag att man inte ens kom in i den funktionen.

Jag gjorde det nu och de gjorde jag i denna funktion så att den ser ut så här: 


void printfield(const int rows, const int cols, cell field[rows][cols]){

int array[rows][cols];

do {
for (int r = 0; r < rows; r++) {
if (r !=0) {
printf("\n");
}
for (int c = 0; c < cols; c++) {
if (field[r][c].current==ALIVE) {
printf("X ");
}
else {
printf(". ");
}
}
}
printf("\n");
printf("Select one of the following options:\n (enter) Step\n");
printf(" (any) Exit \n");
liveOrDie(rows, cols, array, field);

} while (getchar() == '\n');
}

däremot är det något annat fel i koden som jag inte hittar... :(

Laguna Online 28597
Postad: 18 okt 2020 21:56

"ett fel som du inte hittar". Du måste tala om precis vad som blir fel.

Nu ser jag att 'array' inte är definierad, så det blir ett kompileringsfel. Du får definiera den på lämpligt ställe.

Iamnotsmart 22 – Fd. Medlem
Postad: 18 okt 2020 22:01
Laguna skrev:

"ett fel som du inte hittar". Du måste tala om precis vad som blir fel.

Nu ser jag att 'array' inte är definierad, så det blir ett kompileringsfel. Du får definiera den på lämpligt ställe.

När man kör koden skriver den ut det den ska samt massa siffror.. dessutom kan man bara trycka enter 2 ggr och sedan lägger den av

Laguna Online 28597
Postad: 18 okt 2020 23:53

Siffrorna kommer ifrån funktionen som räknar ut hur många grannar som finns. Du hittar säkert stället i koden.

Vad betyder "lägger av"? Avslutar sig som om du tryckte nåt annat än enter, eller kraschar, eller står och gör ingenting?

Iamnotsmart 22 – Fd. Medlem
Postad: 19 okt 2020 10:11
Laguna skrev:

Siffrorna kommer ifrån funktionen som räknar ut hur många grannar som finns. Du hittar säkert stället i koden.

Vad betyder "lägger av"? Avslutar sig som om du tryckte nåt annat än enter, eller kraschar, eller står och gör ingenting?

Efter att jag tryckt in ett av casen så skriver den ut det och om jag trycker Enter igen avslutar den programmet

Laguna Online 28597
Postad: 19 okt 2020 11:17

Då har vi nog inte riktigt samma program längre, men jag har inte ändrat nåt med input, bara definierat 'array'. Eventuellt har det att göra med vilket operativsystem vi använder, men det borde inte det. 

Du kanske kan visa hela programmet igen.

Iamnotsmart 22 – Fd. Medlem
Postad: 19 okt 2020 11:19
Laguna skrev:

Då har vi nog inte riktigt samma program längre, men jag har inte ändrat nåt med input, bara definierat 'array'. Eventuellt har det att göra med vilket operativsystem vi använder, men det borde inte det. 

Du kanske kan visa hela programmet igen.

Jag har ändrat lite saker men just nu är de bara uträkningen som inte stämmer på hur den ska föröka sig/ dö till nästa gen. Så programmet ser ut så här nu:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* Constants, representation of states */
#define ALIVE 'X'
#define DEAD '.'

/* 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 clearField(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, int array[rows][cols], cell field[rows][cols]);
void countNeighborhood(const int rows, const int cols, int array[rows][cols], cell field[rows][cols]);
int countNeighbors(const int rows, const int cols, cell field[rows][cols], int row, int col);
int getState(int rows, int cols, cell field[rows][cols], int row, int col);
void liveOrDie(const int rows, const int cols, int array[rows][cols], cell field[rows][cols]);
void updateWorld(const int rows, const int cols, cell field [rows][cols]);


/* Function: main
* Description: Start and run simulations, interact with the user.
* Lets the user choose initial structure and whether to step
* or exit. Writes information to the user, and the game field
* in each step.
*/

int main(void) {

const int rows = 20;
const int cols = 20;
cell field[rows][cols];
int array[rows][cols];

initField(rows, cols, field);
printfield(rows, cols, array, field);


return 0;
}
/* Function: initField
* Description: Loads a structure that the user selects
* Input: rows - the number of rows in the field
* cols - the number of columns in the field
* field - the field array
* Output: The field array is updated.
*/
void initField(const int rows, const int cols, cell field[rows][cols]) {

clearField(rows,cols,field);

printf("Select field spec to load ([G]lider, [S]emaphore, [R]andom ");
printf("or [C]ustom): ");

int ch = getchar();

/* Ignore following newline */
if (ch != '\n') {
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;
}
}
/* Function: clearField
* Description: Initialize all the cells in the field to dead
* Input: rows - the number of rows in the field
* cols - the number of columns in the field
* field - the field array
* Output: The field array is updated.
*/
void clearField(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;
}
}
}
/* Function: loadGlider
* Description: Inserts a glider into the field.
* Input: rows - the number of rows in the field
* cols - the number of columns in the field
* field - the field array
* 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: rows - the number of rows in the field
* cols - the number of columns in the field
* field - the field array
* 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: rows - the number of rows in the field
* cols - the number of columns in the field
* field - the field array
* 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));
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; 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: rows - the number of rows in the field
* cols - the number of columns in the field
* field - the field array
* 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');
}

void printfield(const int rows, const int cols, int array[rows][cols], cell field[rows][cols]){


do {
for (int r = 0; r < rows; r++) {
if (r !=0) {
printf("\n");
}
for (int c = 0; c < cols; c++) {
if (field[r][c].current==ALIVE) {
printf("X ");
}
else {
printf(". ");
}
}
}
printf("\n");
printf("Select one of the following options:\n (enter) Step\n");
printf(" (any) Exit \n");

liveOrDie(rows, cols, array, field);

} while (getchar() == '\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]){

for (int r = 0; r <= rows; r++){
for (int c = 0; c <= cols; c++) {
array[r][c] = countNeighbors(rows, cols, field, r, c);
}
}
}

int countNeighbors(const int rows, const int cols, cell field[rows][cols], int row, int col){

int cnt = 0;

for (int r = row - 1; r <= row + 1; r++) {
for (int c = col; c <= col + 1; c++) {
if ((row != r || col != c) &&
getState (rows, cols, field, r, c) == ALIVE) {
cnt++;
}
}
}

return cnt;
}

int getState(const int rows, const int cols, cell field[rows][cols], int row, int col)
{
if (row >= 0 && row < rows && col >= 0 && col < cols){
return field[row][col].current;
}
else {
return DEAD;
}

}

void liveOrDie(const int rows, const int cols, int array[rows][cols], cell field[rows][cols]){

countNeighborhood(rows, cols, array, field);

for (int r = 0; r < rows; r++){
for (int c = 0; c < cols; c++){
if (field[r][c].current == ALIVE && array[r][c] == 2){
field[r][c].next = ALIVE;
}
else if (array[r][c] == 3){
field[r][c].next = ALIVE;
}
else{
field[r][c].next = DEAD;
}
}

}
updateWorld (rows, cols, field);
}

void updateWorld(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 = field[r][c].next;
}
}
}

Laguna Online 28597
Postad: 19 okt 2020 11:57

Jämför med programmet du hade från början. Du har nog ändrat fel nånstans.

Iamnotsmart 22 – Fd. Medlem
Postad: 19 okt 2020 12:16
Laguna skrev:

Jämför med programmet du hade från början. Du har nog ändrat fel nånstans.

Men jag fick ändå inte programmet att fungera som det ska på första även om jag deklarerade arreyen.. 

Laguna Online 28597
Postad: 19 okt 2020 12:24

Det borde vara allt som behövs. Hur betedde sig programmet då?

Svara Avbryt
Close