生命游戏(c/python)

52 篇文章 0 订阅
37 篇文章 0 订阅

生命游戏的规则可简化为如下:
邻居个数为0、1、4、5、6、7、8时,则该细胞下次状态为死亡。
邻居个数为2时,则该细胞下次状态为复活。
邻居个数为3时,则该细胞下次状态为稳定。

c代码

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define MAXROW 10
#define MAXCOL 25
#define DEAD 0
#define ALIVE 1

int map[MAXROW][MAXCOL], newmap[MAXROW][MAXCOL];

void print_map();
void print_newmap();
void initmap();
int get_neighbor(int row, int col);
int main()
{
    char c;
    int row, col;
    initmap();
    while (1)
    {
        print_map();
        for (row = 0; row < MAXROW; row++)
        {
            for (col = 0; col < MAXCOL; col++)
            {
                switch (get_neighbor(row, col))
                {
                case 0:
                case 1:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                    newmap[row][col] = DEAD;
                    break;
                case 3:
                    newmap[row][col] = ALIVE;
                    break;
                case 2:
                    newmap[row][col] = map[row][col];
                    break;
                }
            }
        }
        print_newmap();
        printf("\nContinue next Generation ? ");
        getchar();
        c = toupper(getchar());
        if (c != 'Y')
            break;
    }

    system("pause");
    return 0;
}
void initmap()
{
    int i, j;
    for (i = 0; i < MAXROW; i++)
    {
        for (j = 0; j < MAXCOL; j++)
        {
            map[i][j] = DEAD;
        }
    }
    puts("Game of life Program");
    puts("Enter x, y where x, y is living cell");
    printf("0 <= x <= %d,0 <= y <= %d\n",
        MAXROW - 1, MAXCOL - 1);
    puts("Terminate with x, y = -1, -1");

    while (1)
    {
        scanf_s("%d%d", &i, &j);
        if (i >= 0 && i < MAXROW && j >= 0 && j < MAXCOL)
        {
            map[i][j] = ALIVE;
        }
        else if (i == -1 || j == -1)
        {
            break;
        }
        else
        {
            printf("(x,y)exceeds map range");
        }
    }
}

int get_neighbor(int row, int col)
{
    int count = 0;
    int i, j;
    for (i = row - 1; i <= row + 1; i++)
    {
        for (j = col - 1; j <= col + 1; j++)
        {
            if (i<0 || j<0 || i>MAXROW || j>MAXCOL)
                continue;
            if(map[i][j] == ALIVE)
                count++;
        }
    }
    if (map[i][j] == ALIVE)
    {
            count--;
    }
    return count;
}

void print_map()
{
    for (int i = 0; i < MAXROW; i++)
    {
        for (int j = 0; j < MAXCOL; j++)
        {
            if (map[i][j] == ALIVE)
            {
                putchar('#');
            }
            else
                putchar('-');
        }
        printf("\n");

    }
}

void print_newmap()
{
    for (int i = 0; i < MAXROW; i++)
    {
        for (int j = 0; j < MAXCOL; j++)
        {
            map[i][j] = newmap[i][j];
        }
    }
}

python代码

# -*- coding: utf-8 -*-
"""
Created on Sat Nov  4 20:22:59 2017

@author: yangwenbin
"""
import numpy as np 
Map=np.zeros((10,15))
new_map=np.zeros_like(Map)
def init_map():
    flag=True
    for col in range(10):
        for row in range(15):
            Map[col][row]=0  #0表示死亡,1表示活着,对所有位置进行初始化
            pass
        pass
    print("Game of life Program")
    print("Enter x,y where x,y is living cell")
    print("0 <= x <= %d,0 <= y <= %d"%(9, 14))
    print("Terminate with x, y = -1, -1")
    pass

    while flag:
        col = input("input col:")
        row = input("input row:")
        col=int(col)
        row=int(row)
        if col>=0 and col<9 and row >=0 and row <15:
            Map[col][row]=1
            pass
        elif col==-1 or row ==-1:
            break
        else:
            print("exceed map range")
def print_map():
    for col in range(10):
        for row in range(15):
            if Map[col][row]==1:
                print("#",end='')
                pass
            else:
                print("-",end='')
                pass
            pass
        print("\n")
        pass
    pass
def neighbor(col,row):
    count=0
    for i in range(col-1,col+2):
        for j in range(row-1,row+2):
            if i<10 and j<15 and Map[i][j]==1:
                count+=1
                continue
            pass
        pass
    if Map[col][row]==1:
        count-=1
        pass
    return count

def update_map(col,row):
    a=neighbor(col,row)
    if a!=2 and neighbor(col,row)!=3:
        new_map[col][row]=0
        pass
    elif neighbor(col,row)==2:
        new_map[col][row]=1
        pass
    else:
        new_map[col][row]=Map[col][row]
        pass
    pass
def copy_map():
    for i in range(10):
        for j in range(15):
            Map[i][j]=new_map[i][j]
            pass
        pass
    pass
if __name__=="__main__":
    init_map()
    while True:
        print_map()
        for col in range(10):
            for row in range(15):        
                update_map(col,row)
        copy_map()
        c=input("want to continue:")
        if c.upper()!='Y':
            break
        pass 
    pass 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值