Langton's Ant

来源:互联网 发布:vp矢量化软件 编辑:程序博客网 时间:2024/06/07 15:47

作业写个Langtong's Ant, 就是为了好玩, 挺简单的, 上代码。

#include<iostream>#include<fstream>#include<time.h>#include<string>#include<cstdlib>static std::ofstream outf("output.txt");static struct{int pos_x;//[0, 81]int pos_y;//[0, 100]int orient;//1:up, 2:down, 3:right, 4:left}ant_pos;//display the content of arrayinline static void initialDisplay(bool board[82][101], int flag){if(flag==0){outf << "\nThis is the initial board.\n";}if(flag==1){outf << "\nThis is the final board.\n";}for(int i=0; i<101; i++){//int live_cnt=0;outf<<"line " << i <<":\t";for(int j=0; j<82; j++){outf<<board[j][i]<<' ';//if(board[j][i]==1)//live_cnt+=1;}//outf<<",live: " <<live_cnt<<".";outf <<'\n';}}//initialize array with five choicesinline static void initialBoard(bool board[82][101]){int choice;std::cout <<"Possible initial choice:\n1. All white"<<'\n'<<"2. All black" <<'\n'<<"3. Checker board" <<'\n'<<"4. Horizontal stripes"<<'\n'<<"5. Random setting";std::cout<<"\nWhich you want(Please choose from 1,2,3,4,5,numerical value only): ";std::cin >> choice;std::cout <<"The value of your choice: " <<choice;switch(choice){case 1:initialDisplay(board, 0);break;case 2:for(int i=0; i<82; i++){for(int j=0; j<101; j++){board[i][j]=1;}}initialDisplay(board,0);break;case 3:for(int i=0; i<82; i++){for(int j=0; j<101; j++){if((i+1)%2==1){if((j+1)%2==1){board[i][j]=1;}}else{if((j+1)%2==0){board[i][j]=1;}}}}initialDisplay(board,0);break;case 4:for(int i=0; i<82; i++){for(int j=0; j<101; j++){if((i+1)%2==1){board[i][j]=1;}}}initialDisplay(board,0);break;case 5: std::srand(time(0));for(int i=0; i<82; i++){for(int j=0; j<101; j++){int x=1+(int)(100.0*std::rand()/(RAND_MAX+1.0));if(x>50){board[i][j]=1;}}}default:break;}}//starting position chooseinline static void startPosition(bool board[82][101]){std::cout<<"\nPlease choose your starting position: \n";std::cout<<"Horizontal Position(0-81): ";std::cin >> ant_pos.pos_x;std::cout<<"Vertical Position(0-100): ";std::cin >> ant_pos.pos_y;std::cout <<"The position you have choose is: x=" <<ant_pos.pos_x <<", y=" <<ant_pos.pos_y <<std::endl;}//starting orientation chooseinline static void startOrientation(){std::cout <<"Orientation Detail: up(1), down(2), right(3), left(4).\n";std::cout <<"Please choose your staring orientation(1,2,3,4, numberical value only): ";std::cin >> ant_pos.orient;}//update the direction/*turn right(turn==0)turn left(turn==1)originalup(1)rightleftdown(2)leftrightright(3)downupleft(4)updown*/inline static void updateDirection(int orientation, int turn){if(turn==0){if(orientation==1){ant_pos.orient=3;}else if(orientation==2){ant_pos.orient=4;}else if(orientation==3){ant_pos.orient=2;}else if(orientation==4){ant_pos.orient=1;}else{std::cout <<"There are errors in function updateDirection!\n";}}if(turn==1){if(orientation==1){ant_pos.orient=4;}else if(orientation==2){ant_pos.orient=3;}else if(orientation==3){ant_pos.orient=1;}else if(orientation==4){ant_pos.orient=2;}else{std::cout <<"There are errors in function updateDirection!\n";}}}//update the movementinline static void updateMovement(int orientation){if(orientation==1)//up(1){if(ant_pos.pos_y==0){ant_pos.pos_y=100;}else{--ant_pos.pos_y;}}else if(orientation==2)//down(2){if(ant_pos.pos_y==100){ant_pos.pos_y=0;}else{++ant_pos.pos_y;}}else if(orientation==3)//right(3){if(ant_pos.pos_x==81){ant_pos.pos_x=0;}else{++ant_pos.pos_x;}}else if(orientation==4)//left(4){if(ant_pos.pos_x==0){ant_pos.pos_x=81;}else{--ant_pos.pos_x;}}else{std::cout <<"Error occur on function updateMovement!\n"; }}//ant runninginline static void antRunning(bool board[82][101], int steps){int turn;for(int i=0; i<steps; i++){if(board[ant_pos.pos_x][ant_pos.pos_y]==0){turn=0;//turn rightboard[ant_pos.pos_x][ant_pos.pos_y]=1;}else if(board[ant_pos.pos_x][ant_pos.pos_y]==1){turn=1;//turn leftboard[ant_pos.pos_x][ant_pos.pos_y]=0;}else{std::cout <<board[ant_pos.pos_x][ant_pos.pos_y]<<'\t';std::cout <<"\nThere are some errors from function antRunning!\n";}updateDirection(ant_pos.orient, turn);updateMovement(ant_pos.orient);}}inline void welcome(){std::cout <<"\n*******************************************************\n";std::cout << "\nHi, This is program Langton's Ant! Welcome! ^-^\n\n";std::cout <<"*******************************************************\n";}int main(){int steps;bool board[82][101]={0};welcome();initialBoard(board);startPosition(board);startOrientation();std::cout<<"Steps you would like the ant to run: ";std::cin >> steps;antRunning(board, steps);initialDisplay(board, 1);std::cout <<"\nThe output file is in your programm current directory!\n";return 0;}


0 0
原创粉丝点击