C/C++与lua二维数组交互的经典范例

最近为了解决skynet的跑得快ai的核心算法性能问题,那么我们自然就用C来写核心算法,之前从来没做过二维数组交互,也是折腾了一番才能正常理解与运行。先看示例代码

//数组计算加一返回数组
int GetCardCal(lua_State* L)
{
	int arrayNum[20][20] = { 0 };
		lua_pushnil(L);
		int index = lua_gettop(L)-1;
		luaL_checktype(L, index, LUA_TTABLE); //检测传递过来的是否为table
		int indexA = 0;
		while (lua_next(L, index)) {
			int tempindex = lua_gettop(L);
			lua_pushnil(L);
			int count = 0;
			while (lua_next(L, tempindex)) {
				int card = luaL_checkinteger(L, -1);
				arrayNum[indexA][count++] = card;
				lua_pop(L, 1);				// pop value
			}
			indexA++;
			lua_pop(L, 1);				// pop value
		}
		for (int i = 0; i < 20;i++)
		{
			for (int j = 0; j < 20; j++)
			{
				if (arrayNum[i][j]>0)
				{
					arrayNum[i][j]++;
				}
			}
		}
		lua_newtable(L);
		lua_newtable(L);
		int arrayIndex1 = 1;
		for (int i = 0; i < 20; i++)
		{
			if (arrayNum[i][0]==0)
			{
			
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值