Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4584450
  • 博文数量: 1214
  • 博客积分: 13195
  • 博客等级: 上将
  • 技术积分: 9105
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-19 14:41
个人简介

C++,python,热爱算法和机器学习

文章分类

全部博文(1214)

文章存档

2021年(13)

2020年(49)

2019年(14)

2018年(27)

2017年(69)

2016年(100)

2015年(106)

2014年(240)

2013年(5)

2012年(193)

2011年(155)

2010年(93)

2009年(62)

2008年(51)

2007年(37)

分类: Delphi

2011-02-02 19:54:30

  •  C
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>

  4. #define MAXROW 10
  5. #define MAXCOL 25
  6. #define DEAD 0
  7. #define ALIVE 1

  8. void output();
  9. void copy(int[][MAXCOL], int[][MAXCOL]);
  10. void init(int[][MAXCOL]);
  11. int neighbors(int[][MAXCOL], int, int);

  12. int main() {
  13.    puts("Game of life Program");
  14.    puts("Enter x, y where x, y is living cell");
  15.    printf("0 <= x <= %d, 0 <= y <= %d\n", MAXROW-1, MAXCOL-1);
  16.    puts("Terminate with x, y = -1 -1");
  17.  
  18.    int map[MAXROW][MAXCOL];
  19.    int newmap[MAXROW][MAXCOL];
  20.    
  21.    init(map);

  22.    while(1) {
  23.       output(map);
  24.       
  25.       int row, col;
  26.       for(row = 0; row < MAXROW; row++) {
  27.          for(col = 0; col < MAXCOL; col++) {
  28.             switch (neighbors(map, row, col)) {
  29.                case 0: case 1: case 4: case 5: case 6: case 7: case 8:
  30.                   newmap[row][col] = DEAD; break;
  31.                case 2:
  32.                   newmap[row][col] = map[row][col]; break;
  33.                case 3:
  34.                   newmap[row][col] = ALIVE; break;
  35.             }
  36.          }
  37.       }

  38.       copy(map, newmap);

  39.       printf("\nContinue next Generation ? ");
  40.       getchar();
  41.       char ans = toupper(getchar());
  42.       if(ans != 'Y')
  43.           break;
  44.    }
  45.     
  46.    return 0;
  47. }

  48. void init(int map[][MAXCOL]) {
  49.    int row, col;
  50.    for(row = 0; row < MAXROW; row++)
  51.       for(col = 0; col < MAXCOL; col++)
  52.          map[row][col] = DEAD;

  53.    while(1) {
  54.       scanf("%d %d", &row, &col);
  55.       if(0 <= row && row < MAXROW &&
  56.          0 <= col && col < MAXCOL)
  57.          map[row][col] = ALIVE;
  58.       else if(row == -1 || col == -1)
  59.          break;
  60.       else
  61.          printf("(x, y) exceeds map ranage!");
  62.    }
  63. }

  64. int neighbors(int map[][MAXCOL], int row, int col) {
  65.    int count = 0;
  66.    
  67.    int c, r;
  68.    for(r = row-1; r <= row+1; r++)
  69.       for(c = col-1; c <= col+1; c++) {
  70.          if(r < 0 || r >= MAXROW || c < 0 || c >= MAXCOL)
  71.             continue;
  72.          if(map[r][c] == ALIVE)
  73.             count++;
  74.       }

  75.    if(map[row][col] == ALIVE)
  76.       count--;
  77.     
  78.    return count;
  79. }

  80. void output(int map[][MAXCOL]) {
  81.    printf("\n\nGame of life cell status\n");
  82.    
  83.    int row, col;
  84.    for(row = 0; row < MAXROW; row++) {
  85.       printf("\n%20c", ' ');
  86.       for(col = 0; col < MAXCOL; col++)
  87.          if(map[row][col] == ALIVE)
  88.             putchar('#');
  89.          else
  90.             putchar('-');
  91.    }
  92. }

  93. void copy(int map[][MAXCOL], int newmap[][MAXCOL]) {
  94.    int row, col;
  95.    for(row = 0; row < MAXROW; row++)
  96.       for(col = 0; col < MAXCOL; col++)
  97.          map[row][col] = newmap[row][col];
  98. }
  •  Java
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;

  4. public class LifeGame {
  5.     private boolean[][] map;
  6.     private boolean[][] newmap;
  7.     
  8.     public LifeGame(int maxRow, int maxColumn) {
  9.         map = new boolean[maxRow][maxColumn];
  10.         newmap = new boolean[maxRow][maxColumn];
  11.     }
  12.     
  13.     public void cell(int x, int y) {
  14.         map[x][y] = true;
  15.     }
  16.     
  17.     public void next() {
  18.         for(int row = 0; row < map.length; row++) {
  19.             for(int col = 0; col < map[0].length; col++) {
  20.                switch (neighbors(row, col)) {
  21.                   case 0:
  22.                   case 1:
  23.                   case 4:
  24.                   case 5:
  25.                   case 6:
  26.                   case 7:
  27.                   case 8:
  28.                      newmap[row][col] = false;
  29.                      break;
  30.                   case 2:
  31.                      newmap[row][col] = map[row][col];
  32.                      break;
  33.                   case 3:
  34.                      newmap[row][col] = true;
  35.                      break;
  36.                }
  37.             }
  38.          }
  39.         
  40.          for(int row = 0; row < map.length; row++)
  41.            for(int col = 0; col < map[0].length; col++)
  42.               map[row][col] = newmap[row][col];
  43.     }

  44.     public void print() throws IOException {
  45.         System.out.println("\n\nGame of life cell status");
  46.         for(int row = 0; row < map.length; row++) {
  47.             System.out.println();
  48.            for(int col = 0; col < map[0].length; col++)
  49.               System.out.printf("%c", map[row][col] ? '#' : '-');
  50.         }
  51.     }
  52.     
  53.     private int neighbors(int row, int col) {
  54.         int count = 0;

  55.         for(int r = row-1; r <= row+1; r++)
  56.            for(int c = col-1; c <= col+1; c++) {
  57.               if(r < 0 || r >= map.length ||
  58.                  c < 0 || c >= map[0].length)
  59.                  continue;
  60.               if(map[r][c])
  61.                  count++;
  62.            }

  63.         if(map[row][col])
  64.            count--;
  65.          
  66.         return count;
  67.     }
  68.     
  69.     public static void main(String[] args)
  70.                  throws NumberFormatException, IOException {
  71.         BufferedReader bufReader =
  72.             new BufferedReader(
  73.                     new InputStreamReader(System.in));
  74.         
  75.         LifeGame game = new LifeGame(10, 25);
  76.         
  77.         System.out.println("Game of life Program");
  78.         System.out.println(
  79.                    "Enter x y where x y is living cell");
  80.         System.out.println("0 <= x < 10, 0 <= y < 25");
  81.         System.out.println("Terminate with x y = -1 -1");
  82.         
  83.         int row = -1;
  84.         int col = -1;
  85.         do {
  86.             String[] strs = bufReader.readLine().split(" ");
  87.             row = Integer.parseInt(strs[0]);
  88.             col = Integer.parseInt(strs[1]);
  89.             if(0 <= row && row < 10 && 0 <= col && col < 25)
  90.                 game.cell(row, col);
  91.         } while(col != -1);
  92.         
  93.         while(true) {
  94.             game.print();
  95.             game.next();

  96.             System.out.print(
  97.                          "\nContinue next Generation ? ");
  98.             
  99.             String ans = bufReader.readLine().toUpperCase();

  100.             if(ans.equals("N"))
  101.                 break;
  102.         }
  103.     }
  104. }

  • Python
  1. class LifeGame:
  2.     def __init__(self, maxRow, maxColumn):
  3.         self.__map = []
  4.         self.__newMap = []
  5.         for i in range(maxRow):
  6.             self.__map.append([False] * maxColumn)
  7.             self.__newMap.append([False] * maxColumn)
  8.     def cell(self, x, y):
  9.         self.__map[x][y] = True
  10.     def next(self):
  11.         for row in range(len(self.__map)):
  12.             for col in range(len(self.__map[0])):
  13.                 n = self.__neighbors(row, col)
  14.                 if n in [0, 1, 4, 5, 6, 7, 8]:
  15.                     self.__newMap[row][col] = False
  16.                 elif n == 2:
  17.                     self.__newMap[row][col] = self.__map[row][col]
  18.                 elif n == 3:
  19.                     self.__newMap[row][col] = True
  20.         for row in range(len(self.__map)):
  21.             for col in range(len(self.__map[0])):
  22.                 self.__map[row][col] = self.__newMap[row][col]
  23.     def print(self):
  24.         print("\nGame of life cell status")
  25.         for row in range(len(self.__map)):
  26.             print()
  27.             for col in range(len(self.__map[0])):
  28.                 if self.__map[row][col]:
  29.                     print("#", end="")
  30.                 else:
  31.                     print("-", end="")
  32.     def __neighbors(self, row, col):
  33.         count = 0
  34.         for r in range(row - 1, row + 2):
  35.             for c in range(col - 1, col + 2):
  36.                 rc = r < 0 or r >= len(self.__map)
  37.                 cc = c < 0 or c >= len(self.__map[0])
  38.                 if rc or cc:
  39.                     continue
  40.                 if self.__map[r][c]:
  41.                     count += 1
  42.         if self.__map[row][col] == True:
  43.             count -= 1
  44.         return count
  45.     
  46. game = LifeGame(10, 25)
  47. print("Game of life Program")
  48. print("Enter x, y where x, y is living cell")
  49. print("0 <= x < 10, 0 <= y < 25")
  50. print("Terminate with x, y = -1, -1")
  51. while(True):
  52.     strs = input().split(",")
  53.     row = int(strs[0])
  54.     col = int(strs[1])
  55.     if row >= 0 and row < 10 and col >= 0 and col < 25:
  56.         game.cell(row, col)
  57.     elif row == -1 or col == -1:
  58.         break

  59. while(True):
  60.     game.print()
  61.     game.next()
  62.     print("\nContinue next Generation ?", end="")
  63.     if input().upper() == "N":
  64.         break
  • Scala
  1. class LifeGame(maxRow: Int, maxColumn: Int) {
  2.     val map = new Array[Array[Boolean]](maxRow, maxColumn)
  3.     val newmap = new Array[Array[Boolean]](maxRow, maxColumn)
  4.     def cell(x: Int, y: Int) { map(x)(y) = true }
  5.     def next() {
  6.         for{
  7.             row <- 0 until map.length;
  8.             col <- 0 until map(0).length
  9.         } neighbors(row, col) match {
  10.             case 0|1|4|5|6|7|8 => newmap(row)(col) = false
  11.             case 2 => newmap(row)(col) = map(row)(col)
  12.             case 3 => newmap(row)(col) = true
  13.         }
  14.         for{
  15.             row <- 0 until map.length;
  16.             col <- 0 until map(0).length
  17.         } map(row)(col) = newmap(row)(col)
  18.     }
  19.     
  20.     private def neighbors(row: Int, col: Int) = {
  21.         var count = 0
  22.         for(r <- (row - 1) to (row + 1);
  23.             c <- (col - 1) until (col + 1)
  24.         ) if(r >= 0 && r < map.length &&
  25.              c >= 0 && c < map(0).length
  26.              && map(r)(c)) {
  27.              count += 1
  28.         }
  29.             
  30.         if(map(row)(col)) count -= 1
  31.         count
  32.     }
  33.     
  34.     def print() {
  35.         println("\n\nGame of life cell status");
  36.         for(row <- 0 until map.length) {
  37.             println()
  38.             for(col <- 0 until map(0).length) {
  39.                 printf(if(map(row)(col)) "#" else "-")
  40.             }
  41.         }
  42.     }
  43. }

  44. println("Game of life Program")
  45. println("Enter x y where x y is living cell")
  46. println("0 <= x < 10, 0 <= y < 25")
  47. println("Terminate with x y = -1 -1")

  48. val game = new LifeGame(10, 25)
  49. var row = -1
  50. var col = -1
  51. do {
  52.     val strs = readLine split(" ")
  53.     row = strs(0).toInt
  54.     col = strs(1).toInt
  55.     if(0 <= row && row < 10 && 0 <= col && row < 25)
  56.         game.cell(row, col)
  57. } while(row != -1)

  58. do {
  59.     game.print()
  60.     game.next()
  61.     print("\nContinue next Generation ? ")
  62. } while(readLine().toUpperCase() != "N")
阅读(861) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~