lua 与c/c++的变量交换,调用方法
-
/*
-
* main.cpp
-
*
-
* Created on: 2013-1-9
-
* Author: root
-
*/
-
#include <lua.hpp>
-
#include <iostream>
-
-
using namespace std;
-
-
void InitLua(lua_State *L)
-
{
-
luaL_openlibs(L);
-
if(luaL_dofile(L,"test.lua"))
-
{
-
cout << "can't load test.lua" << endl;
-
}
-
}
-
-
//get global statues value from lua
-
template<class T>
-
class GetLuaData
-
{
-
public:
-
GetLuaData(){}
-
~GetLuaData(){}
-
-
inline T PopLuaNumber(lua_State* pL, const char* name)
-
{
-
lua_settop(pL,0);
-
lua_getglobal(pL,name);
-
//check variable is the correct type
-
if(!lua_isnumber(pL,1))
-
{
-
cout << "n[C++]:Error: Invalid type!";
-
}
-
T val = (T)lua_tonumber(pL,1);
-
lua_pop(pL,1);
-
return val;
-
}
-
-
inline string PopLuaTableString(lua_State* pL, const char* tableName,const char* var)
-
{
-
lua_settop(pL,0);
-
lua_getglobal(pL,tableName);//把表放堆栈上
-
if(!lua_istable(pL,1))
-
{
-
cout << "n[c++]:Error, " << tableName << " is not a valid table" << endl;
-
}
-
-
lua_pushstring(pL,var);//把键放堆栈上
-
if(!lua_isstring(pL,-1))
-
{
-
cout << "n[c++]:Error invalid type!" << endl;
-
}
-
lua_gettable(pL,-2);// 出棧键值,把键对应表里的数据放在堆栈里键刚才所在的位置
-
string sname;
-
if(lua_isstring(pL,-1))
-
{
-
sname = lua_tostring(pL,-1);
-
}
-
lua_pop(pL,1);
-
return sname;
-
// else if(lua_isnumber(pL,-1))
-
// {
-
// T name = (T)lua_tonumber(pL,-1);
-
// return name;
-
// }
-
}
-
-
};
-
-
-
int main(int argc,char* argv[])
-
{
-
// lua_State *L = lua.open();
-
lua_State *L = luaL_newstate();
-
// luaL_openlibs(L);
-
// luaL_dofile(L, "test.lua");
-
InitLua(L);
-
-
// lua_settop(L,0);
-
// lua_getglobal(L,"user_score");
-
// lua_getglobal(L,"comp_score");
-
// if(!lua_isnumber(L,1) || !lua_isstring(L,2))
-
// {
-
// cout << "n[C++]: Error: Invalid type!";
-
// }
-
-
// string a = lua_tostring(L,1);
-
// string b = lua_tostring(L,2);
-
// int a = (int)lua_tonumber(L,1);
-
// int b = (int)lua_tonumber(L,2);
-
GetLuaData<int> tmpLua;
-
int a = tmpLua.PopLuaNumber(L,"user_score");
-
int b = tmpLua.PopLuaNumber(L,"comp_score");
-
cout << " a = " << a << " b = " << b << endl;
-
-
string c = tmpLua.PopLuaTableString(L,"simple_table","name");
-
cout << " name = " << c << endl;
-
-
//lua function,C,C++中调用lua的函数
-
lua_getglobal(L,"add");
-
if(!lua_isfunction(L,-1))
-
{
-
cout << "nn[c++]:Ops! The lua function 'add' has not been defined";
-
}
-
else
-
{
-
lua_pushnumber(L,5);
-
lua_pushnumber(L,8);
-
lua_call(L,2,1);
-
int result = (int)lua_tonumber(L,-1);
-
lua_pop(L,1);
-
cout << "result = " << result << endl;
-
}
-
-
-
lua_close(L);
-
-
return 0;
-
}
-
//=========================C,C++函数暴露给lua,调用==============================
-
//C函数,不能直接被调用,需要使用一个包装函数以满足调用方式
-
void EvaluateTheGuess(string user_guess,string comp_guess,int &user_score,int &comp_score)
-
{
-
-
}
-
int cpp_EvaluateTheGuesses(lua_State* pL)
-
{
-
int n = lua_gettop(pL);
-
if( n != 4 )
-
{
-
std::cout << "n[c++]: Wrong number of arguments for cpp_EvaluateTheGuesses";
-
}
-
-
if(!lua_isstring(pL,1) || !lua_isstring(pL,2) ||
-
!lua_isnumber(pL,3) || !lua_isnumber(pL,4))
-
{
-
std::cout << "n[c++]: Error: Invalid types passed to cpp_evaluateTheGuesses";
-
}
-
-
std::string user_guess = lua_tostring(pL,1);
-
std::string comp_guess = lua_tostring(pL,1);
-
int user_score =(int)lua_tonumber(pL,3);
-
int comp_score = (int)lua_tonumber(pL,4);
-
-
EvaluateTheGuess(user_guess,comp_guess,user_score,comp_score);
-
-
lua_pushnumber(pL,user_score);
-
lua_pushnumber(pL,comp_score);
-
-
return 2;
-
}
-
// lua中注册后可以调用cpp_EvaluateTheGuesses函数
-
int regFunction(lua_State* pL)
-
{
-
lua_register(pL,"cpp_EvaluateTheGuesses",cpp_EvaluateTheGuesses);
-
return 1;
-
}
lua 测试脚本
-
simple_table = {name="Dan dare", age=20}
-
function add(a,b)
-
return (a+b)
-
end
-
math.randomseed(os.time())
-
user_score = 0
-
comp_score = 0
-
lookup = {};
-
lookup["rock"] = {rock = "draw",paper = "lose", scissors = "win" }
-
lookup["paper"] = {rock= "win", paper = "draw", scissors = "lose"}
-
lookup["scissors"] = {rock = "lose", paper = "win", scissors = "draw"}
-
-
function GetAIMove()
-
local int_to_name = {"scissors","rock","paper"}
-
return int_to_name[math.random(3)]
-
end
-
-
function EvaluateTheGuesses(user_guess,comp_guess)
-
print("user guess... "..user_guess.." comp guess... "..comp_guess)
-
if (lookup[user_guess][comp_guess] == "win") then
-
print("You Win the Round!")
-
user_score = user_score + 1
-
elseif (lookup[user_guess][comp_guess]) == "lose" then
-
print("Computer wins the Round")
-
comp_score = comp_score + 1
-
else
-
print("Draw!")
-
print(lookup[user_guess][comp_guess])
-
end
-
end
-
-
--[[主游戏循环]]
-
print("enter q to quit game");
-
print()
-
loop = true
-
while loop == true do
-
print("User: "..user_score.." computer: "..comp_score)
-
user_guess = io.stdin:read '*l'
-
local letter_to_string = {s = "scissors", r = "rock", p = "paper"}
-
if user_guess == "q" then
-
loop = false
-
elseif (user_guess == "r") or (user_guess == "p") or (user_guess == "s") then
-
comp_guess = GetAIMove()
-
EvaluateTheGuesses(letter_to_string[user_guess],comp_guess)
-
else
-
print("Invalid input, try again")
-
end
-
end
阅读(1134) | 评论(0) | 转发(0) |