问题描述
我仍然是编程的初学者,我尝试过编写康威的生命游戏。到目前为止,这一切都做得很好。现在我希望能够通过文本文件选择启动状态。 举个例子:
* * ** * **
* *** * **
* *** * ***
然后程序应该从这个启动状态继续运行。
但是,我不知道该怎么做。 我希望有人能在这里帮助我... 谢谢:)
主要
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include "world.h"
#pragma warning(disable : 4996)
#define MOORE_SEARCH_DEPTH 1
#define WORLD_WIDTH 50
#define WORLD_HEIGHT 50
#define LOOP_ADVANCE_DELAY 250
void Hauptmenü()
{
//Begrüßung und Auswahl
printf("Wikkommen im Game of Life\n");
printf("Bitte waehlen Sie eine Option,in dem Sie die dazugehoerige Zahl eingeben und mit Enter bestaetigen:\n");
printf("1- GameofLife im Randommodus starten\n");
printf("2- Eingabe der Prozentzahl an lebenden Zellen,danach GameofLife starten\n");
printf("3- Datei laden\n");
printf("4- life,the universe and everything\n");
}
// Gibt eine Darstellung eines nicht unregelmäßigen 2D-Arrays mit den angegebenen Abmessungen zurück,bei denen alle Zellen tot sind
bool* newDeadCells(size_t width,size_t height)
{
bool* cells = malloc(sizeof(bool) * width * height);
for (size_t i = 0; i < (width * height); i++)
{
cells[i] = false;
}
return cells;
}
World* newWorld(size_t width,size_t height)
{
World* w = malloc(sizeof(World));
w->width = width;
w->height = height;
w->readCells = newDeadCells(width,height);
w->writeCells = newDeadCells(width,height);
return w;
}
// Randomisiert die Zellen so,dass jede Zelle eine Chance hat,am Leben zu sein
void randomizeCells(World* world,float aliveChance)
{
size_t width = world->width;
size_t height = world->height;
for (size_t i = 0; i < (width * height); i++)
{
world->readCells[i] = (rand() % 100) < aliveChance * 100;
}
}
// Überschreibt die lesbaren Zellen mit den beschreibbaren Zellen
void copyWritabletoReadable(World* world)
{
memcpy(world->readCells,world->writeCells,sizeof(bool) * world->width * world->height);
}
// Befreit die gegebene Welt und jede damit verbundene memory
void freeWorld(World* world)
{
free(world->readCells);
free(world->writeCells);
free(world);
}
size_t indexOf(size_t width,size_t x,size_t y)
{
return width * y + x;
}
void setCell(World* world,size_t y,bool isAlive)
{
size_t index = indexOf(world->width,x,y);
world->writeCells[index] = isAlive;
}
bool getCell(World* world,size_t y)
{
int index = indexOf(world->width,y);
return world->readCells[index];
}
// Gibt die Anzahl der lebenden Nachbarn zurück,die die angegebene Position umgeben.
// Die Tiefe gibt zurück,wie viele Quadrate in jede Richtung zu schauen sind. 1 = Standard Moore Nachbarschaft.
size_t nAliveNeighboRSSurrounding(World* world,size_t depth)
{
size_t xBound = min(x + depth + 1,world->width);
size_t yBound = min(y + depth + 1,world->height);
size_t aliveCount = 0;
for (size_t ny = max(0,y - depth); ny < yBound; ny++)
{
for (size_t nx = max(0,x - depth); nx < xBound; nx++)
{
if (getCell(world,nx,ny) && !(nx == x && ny == y))
{
aliveCount++;
}
}
}
return aliveCount;
}
bool cellShouldLive(bool isAlive,size_t nNeighbors)
{
return (isAlive && nNeighbors >= 2 && nNeighbors <= 3)
|| (!isAlive && nNeighbors == 3);
}
// Entscheidet,ob eine Zelle leben oder sterben soll,und legt sie entsprechend fest
void advanceCellAt(World* world,size_t y)
{
size_t nNeighbors = nAliveNeighboRSSurrounding(world,y,MOORE_SEARCH_DEPTH);
bool isAlive = getCell(world,y);
setCell(world,cellShouldLive(isAlive,nNeighbors));
}
void advanceWorld(World* world)
{
size_t width = world->width;
size_t height = world->height;
for (size_t y = 0; y < height; y++)
{
for (size_t x = 0; x < width; x++)
{
advanceCellAt(world,y);
}
}
copyWritabletoReadable(world);
}
char* formatWorld(World* world)
{
size_t width = world->width;
size_t height = world->height;
size_t nCells = width * height;
//Insgesamt benötigte Zellen + extra für newlines + NL-term
size_t buffSize = sizeof(char) * nCells + height + 1;
char* buffer = malloc(buffSize);
buffer[buffSize - 1] = '\0';
size_t i = 0;
for (size_t y = 0; y < height; y++)
{
for (size_t x = 0; x < width; x++)
{
bool isAlive = getCell(world,y);
char rep = isAlive ? '*' : ' ';
buffer[i] = rep;
i++;
}
buffer[i] = '\n';
i++;
}
return buffer;
}
void printWorld(World* world)
{
char* formatted = formatWorld(world);
printf("%s",formatted);
free(formatted);
}
void ConsoleRoutine(float Wahrscheinlichkeit,bool b1)
{
srand(NULL);
World* world = newWorld(WORLD_WIDTH,WORLD_HEIGHT);
randomizeCells(world,Wahrscheinlichkeit);
// counter
if(b1==true)
for (size_t i = 0; ; i++)
{
printWorld(world);
advanceWorld(world);
Sleep(LOOP_ADVANCE_DELAY);
system("cls");
}
else
{
for (size_t i = 0; ; i++)
{
printWorld(world);
advanceWorld(world);
system("pause");
system("cls");
}
}
}
void ConsoleRoutineData(fp)
{
srand(NULL);
}
void Menüentscheidung()
{
int m;
scanf("%i",&m);
float w;
float nw = rand() % 100;
FILE *fp;
//Entscheidungen des Benutzers
switch (m)
{
case 1:
nw = nw / 100;
printf("Moechten Sie eine fliessende Abarbeitung<f> oder eine schrittweise Abarbeitung<s>\n");
char f;
scanf("%s",&f);
if (f == 'f')
{
bool b1 = true;
ConsoleRoutine(nw,b1);
}
else
{
bool b1 = false;
ConsoleRoutine(nw,b1);
}
break;
case 2: printf("Bitte Wahrscheinlichkeit eingeben\n");
scanf("%f",&w);
w = w / 100;
printf("Moechten Sie eine fliessende Abarbeitung<f> oder eine schrittweise Abarbeitung<s>\n");
char g;
scanf("%s",&g);
if (g == 'f')
{
bool b1 = true;
ConsoleRoutine(w,b1);
}
else
{
bool b1 = false;
ConsoleRoutine(w,b1);
}
break;
case 3: printf("Moechten Sie Datei1<1>,Datei2<2> oder Datei3<3> laden?\n");
int a;
scanf("%i",&a);
if (a == 1)
{
bool b1 = true;
fp = fopen("Datei1.txt","r");
//ConsoleRoutinedata(fp);
system("PAUSE");
}
else if (a == 2)
{
fp = fopen("datei2.txt","r");
}
else if (a == 3)
{
fp = fopen("datei2.txt","r");
}
break;
case 4: printf("42\n");
system("PAUSE");
break;
}
}
int main() {
Hauptmenü();
Menüentscheidung();
}
世界
#pragma once
#include <stdbool.h>
#include <stdlib.h>
typedef struct
{
bool *readCells;
bool *writeCells;
size_t width;
size_t height;
} World;
// Alle Schreibvorgänge werden für writeArray ausgeführt,und alle Lesevorgänge werden aus dem read array gelesen.
// Neue Welt mit angegebenen Dimensionen
World* newWorld(size_t width,size_t height);
// Gets/Sets the Cell at the given position
void setCell(World* world,bool isAlive);
bool getCell(World*,size_t y);
// Gibt char array aus,dass die Welt repräsentiert
char* formatWorld(World*);
// Schreibt das array,dass von der Funktion wiedergegben wurde
void printWorld(World*);
// Forführung der Welt bei einem tick
void advanceWorld(World*);
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)