Langton's Ant

本文分享了一个简单的Langton's Ant模拟程序实现。通过C++编程语言,文章详细介绍了如何设置棋盘初始状态、选择起始位置及方向,并运行指定步数后的棋盘变化情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作业写个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 array
inline 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 choices
inline 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 choose
inline 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 choose
inline 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)
	original	
	up(1)		right				left
	down(2)		left				right
	right(3)	down				up
	left(4)		up				down
*/
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 movement
inline 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 running
inline 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 right
			board[ant_pos.pos_x][ant_pos.pos_y]=1;
		}
		else if(board[ant_pos.pos_x][ant_pos.pos_y]==1)
		{
			turn=1;	//turn left
			board[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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值