更快的方法把一个数组写到Console上?

问题描述 投票:0回答:1

最近我尝试着做了我自己版本的生命游戏,它可以工作,唯一的问题是打印到控制台,如果你执行它,你可以清楚地看到它从左到右写,大约需要0.3秒,我希望它能更快一点如果你想编译它,只需在第一个屏幕上输入0。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "console.h"
//#include <conio.h>

#define XSIZE 80 // max 240
#define YSIZE 25 // max 67

int clone(int array1[XSIZE][YSIZE], int array2[XSIZE][YSIZE]);
int print(int array[XSIZE][YSIZE]);
int getrandarray(int array[XSIZE][YSIZE]);
int countNachbar(int x, int y, int array[XSIZE][YSIZE]);
int calccells(int thisarray[XSIZE][YSIZE], int nextarray[XSIZE][YSIZE]);

int choice;

int main()
{
    setCursorType(0);
    srand(time(0));
    initConsole();
    //mode con: cols=XSIZE lines=YSIZE;
    int cells[XSIZE][YSIZE], nextcells[XSIZE][YSIZE] = {};
    int cellgen = 0;
    char c;
    printf("Nachbarmodus?\n1 = Rechteck(Randzellen bleiben immer gleich, stirbt nicht oft aus)\n0 = Torus(Die seiten des rechteckes werden verbunden, um einen Donut\ndarzustellen, der keine raender hat, stirbt oefter aus, ist der Originale Modus)\n");
    scanf("%d", &choice);
    clrscr();
    gotoxy(0, 0);
    getrandarray(cells);
    print(cells);
    while(c != 27)
    {
        calccells(cells, nextcells);                    //memcpy (cells, nextcells, YSIZE*XSIZE*sizeof(int));
        print(nextcells);
        cellgen++;
        gotoxy(0, YSIZE+1);
        printf("Generation %d", cellgen);
        clone(nextcells, cells);
        if(kbhit())
            c = getch();
    }
    return 0;
}

int print(int array[XSIZE][YSIZE])
{
    int x, y;
    for(x=0; x<XSIZE; x++)
    {
        for(y=0; y<YSIZE; y++)
        {
            gotoxy(x, y);
            if(array[x][y] == 1)
                printf("%c", 254);
            else if(array[x][y] == 0)
                printf(" ");
        }
    }
}

int getrandarray(int array[XSIZE][YSIZE])
{
    int x, y;
    for(x=0; x<XSIZE; x++)
    {
        for(y=0; y<YSIZE; y++)
        {
            array[x][y] = rand()%2;
            //array[x][y] = 1;

        }
    }
}

int countNachbar(int x, int y, int array[XSIZE][YSIZE])
{
    int sum=0, i, j, spalte, reihe;
    for(i=-1; i<2; i++)
    {
        for(j=-1; j<2; j++)
        {
            spalte = (x + i + XSIZE) % XSIZE;
            reihe = (y + j + YSIZE) % YSIZE;
            sum += array[spalte][reihe];
        }
    }
    sum-=array[x][y];
    return sum;
}

int clone(int array1[XSIZE][YSIZE], int array2[XSIZE][YSIZE])
{
    int x, y;
    for(x=0; x<XSIZE; x++)
    {
        for(y=0; y<YSIZE; y++)
        {
            array2[x][y] = array1[x][y];
        }
    }
}

int calccells(int thisarray[XSIZE][YSIZE], int nextarray[XSIZE][YSIZE])
{
    int x, y, state, nachbarn;
    for(x=0; x<XSIZE; x++)
    {
        for(y=0; y<YSIZE; y++)
        {
            state = thisarray[x][y];
            nachbarn = countNachbar(x, y, thisarray);
            if(state == 0 && nachbarn == 3)
            {
                nextarray[x][y] = 1;
            }
            if(state == 1 && (nachbarn < 2 || nachbarn > 3))
                nextarray[x][y] = 0;
            if(choice)
            {
                if(x == 0 || x == (XSIZE-1) || y == 0 || y == (YSIZE-1) )
                    nextarray[x][y] = thisarray[x][y];
            }

        }
    }

}


//console.h:
//https://www.mediafire.com/file/124e42w8mzy0o4z/console.h/file
//https://www.mediafire.com/file/89j8f9kf7ndyqp6/console.c/file
c arrays printing console conways-game-of-life
1个回答
1
投票

在对你的代码做了一个快速的剖析后,我发现占用时间最多的函数是 gotoxy()printf().

因此,我建议你尽量少调用这些.所以这里有一个你可以使用的改进列表。

  • 由于网格从位置(0, 0)开始,你可以简单地调用... ... gotoxy(0, 0); 一开始 print().
  • 为了减少拨打电话的次数 printf()你可以用你的数组建立一个单一的字符串。然后用一个函数调用来显示这个字符串。
  • 对于没有格式化的字符串,你可以使用 puts() 而非 printf().
  • 或者,要打印单个字符,可以使用 putc().

我希望这能帮助你! :)

© www.soinside.com 2019 - 2024. All rights reserved.