2D字符串数组和生命游戏

时间:2013-04-22 21:05:35

标签: c++ arrays char multidimensional-array

所有我正在寻找的是向我解释的一些概念。也许是一个简短的例子。

我的任务是使用以下参数创建一个廉价的生命游戏。 1)25x25板,四面包裹。 2)生活规则(我相信我知道如何解决这个问题。) 3)已经实现了开始形状的阵列。描述为基本(* * *)正方形和滑翔机。

我很困惑的是2D字符数组。我主要学习如何使用int数组,但我似乎无法携带字符串。我担心我不完全理解这个概念。如果有人愿意提出任何建议,我将非常感激你。我也遇到了一个相当奇怪的错误,我不知道如何处理。当程序要求初始状态的数字时。如果用户输入一个字母(比如E),它将循环到无穷大。我试过禁止字符输入,但它已经变得很糟糕了。

我遇到的问题是,例如, 1)25x25 * s的数组 2)25x25阵列的3's strangly 3)错误cout输出 4)循环*和错误输出到无穷大

对于好奇的人,我带来了下面的源代码:

#include <iostream>
#include <fstream> 
#include <stdio.h>  
#include <stdlib.h>  
#include <time.h>  
#include <string>


using namespace std;
//Put all global constants here
////////////////////////////////////////////////////////////////////
//Constant for size of array
const int NROWS = 25;
const int NCOLS = 25;
int seed =0;
const char LIVE = '*'; //life cells
const char DEAD = ' '; //dead cells


//Put all functions here.
////////////////////////////////////////////////////////////////////


//create array for basic


//create array for square


//Create array for Glider


//Create function for next state







////////////////////////////////////////////////////////////////////



//Begin main
int main()
{

//Call all variables here
////////////////////////////////////////////////////////////////////
int choice_number, generations, i, j;
bool programOn= true;
char basic_array[NROWS][NCOLS];

////////////////////////////////////////////////////////////////////

while (programOn != false)
{

//Intro
////////////////////////////////////////////////////////////////////
    cout << "Hello user!\n" << endl;

    cout << "This program is in no way a cheap software knock ";
    cout << "off of Horton Conway's Game of Life Software." << endl;
    cout << "please forward all lawsuits to our computer professor.\n" << endl;
    cout << "The entire program is based on the 'rules' of life.\n " << endl;


    cout << "The Rules of Life:" << endl;
    cout << "******************" << endl;
    cout << "1. Any live cell with fewer than two live neighbors dies, as if by loneliness." << endl;
    cout << "2. Any live cell with more than three live neighbors dies, as if by \n\tovercrowding." << endl;
    cout << "3. Any live cell with two or three live neighbors lives, unchanged." << endl;
    cout << "4. Any dead cell with exactly three live neighbors comes to life.\n\n" << endl << endl;



    cout << "Throughout this main menu you will find different ";
    cout << "and unique 25x25 environments for your simple ";
    cout << "artificial life.\n\n" << endl;



    cout << "Pick basic row which starts your first generation ";
    cout << "in a group of three. \n" << endl;

    cout << "Pick square pattern to start your first generation ";
    cout << "in a 2x2 square. \n" << endl;

    cout << "Pick glider to start your first generation in an ";
    cout << "arrow patten group of 5. \n\n" << endl;


////////////////////////////////////////////////////////////////////




//Ask user for preferred pattern
////////////////////////////////////////////////////////////////////

    cout << "So which pattern will you choose?" << endl;
    cout << "Please put in number 1, 2 or 3. ";
    cout << "4 is to stop the program \n" << endl;
    cout << "\t 1) Basic " << endl;
    cout << "\t 2) Square" << endl;
    cout << "\t 3) Glider" << endl;
    cout << "\t 4) End Program" << endl;

    cin >> choice_number;
////////////////////////////////////////////////////////////////////


    switch (choice_number)
    {

    //case 1
    /////////
    case 1:
        cout << "\n\nYou picked Basic!\n" << endl;

        //ask for number of generations
        cout << "How many generations would you like to have for Basic?" << endl;
        cin >> generations;

        cout << "\nThe number of generations you desire is: " << generations << endl;


    cout << basic_array;



        break;

    //case 2
    /////////
    case 2:
        cout << "\n\nYou picked Square!\n" << endl;

        //ask for number of generations
        cout << "How many generations would you like to have for Square?" << endl;
        cin >> generations;

        cout << "\nThe number of generations you desire is: " << generations << endl;


        //Square Pattern code



        break;

    //case 3
    /////////
    case 3:
        cout << "\n\nYou picked Glider!\n " << endl;

        //ask for number of generations
        cout << "How many generations would you like to have for Glider?" << endl;
        cin >> generations;

        cout << "\nThe number of generations you desire is: " << generations << endl;


        //Glider Pattern code

        break;

    //case 4
    /////////
    case 4:
        cout << "\n\nCome back soon!\n " << endl;

        programOn= false;
        break;

    //

    //default
    /////////
    default:
        cout << "That's not 1, 2, 3 or 4. Try picking one of those. " << endl ;
        cout << "Choose again.\n " << endl;

        cin >> choice_number;
        break;
        }

    }

    //Terminate program
    system ("pause");
    return 0;

    }

0 个答案:

没有答案