大家好,我目前正在尝试使用不同的ai玩家在地图上创建一个简单的游戏。
我来计算计算以计算出一定数量的健康损失,虽然我确信我的数学背后的数学是正确的,但c的实现似乎并没有起作用!我可能错过了一个简单的观点,但我会把代码的提取物放在预期的工作中。
random1 = Numbergen(50);
cal1 = (100 - random1)/100;
random2 = Numbergen(50);
cal2 = (100 - random2) / 100;
PtrPlayer[attacker].health = (double)(PtrPlayer[attacker].health * cal1);
PtrPlayer[ncount].health = (double)(PtrPlayer[ncount].health * cal2);
printf("There was a battle but both players drawed and retreated.\n");
return 1;
Numbergen是一个根据时间种子计算随机数的函数,50是我希望它返回的最大数。
cal1和2应该存储一个十进制数,例如0.75,以取消25%的健康状况,这是cal计算应该做的事情,但是当调试它们显示的值为零时,无论随机数是多少。
这应该通过取随机数让我们说从25到25离开75,然后除以100得到一个十进制乘数,然后可以用来减少25%的健康。健康从100开始,所以例如它将导致100 * 0.75,这将使我得到75健康,而是cal一个存储0,结果健康下降到零。
要清楚,cal1和2都是浮点数以允许小数位。
如果有人能指出我可能出错的地方,我将非常感激! 如果我错过了重要的内容,请告诉我,我会尝试解释。
另请注意我只是编程的初学者所以请不要用超复杂的代码打我!
根据要求,random1和2都是整数
PtrPlayer []。健康设置为整数
cal1和2设置为浮点数
答案 0 :(得分:3)
cal1 = (100 - random1)/100;
由于random1
是int
,100
是int
,(100 - random1)
也是int
。在两个整数操作数上使用/
运算符时,将执行截断整数除法。
如果您想要浮点除法,请将/
的至少一侧转换为浮点类型:
cal1 = (100 - random1) / 100.0f;
// float literal ^^^^^^
......或......
cal1 = (float)(100 - random1) / 100;
简单地转换除法的结果将没有效果,因为C表达式的类型是由内而外严格确定的。
答案 1 :(得分:0)
尝试将两个100中的任何一个更改为100.0
@if (User.IsInRole("admin")) { }
或者
<?php
session_start();
include_once("config.php");
//empty cart by distroying current session
if(isset($_GET["emptycart"]) && $_GET["emptycart"]==1)
{
$return_url = base64_decode($_GET["return_url"]); //return url
session_destroy();
header('Location:'.$return_url);
}
//add item in shopping cart
if(isset($_POST["type"]) && $_POST["type"]=='add')
{
$product_code = filter_var($_POST["product_code"], FILTER_SANITIZE_STRING); //product code
$product_qty = filter_var($_POST["product_qty"], FILTER_SANITIZE_NUMBER_INT); //product code
$return_url = base64_decode($_POST["return_url"]); //return url
//MySqli query - get details of item from db using product code
$results = $mysqli->query("SELECT product_name,price FROM products WHERE product_code='$product_code' LIMIT 1");
$obj = $results->fetch_object();
if ($results) { //we have the product info
//prepare array for the session variable
$new_product = array(array('name'=>$obj->product_name, 'code'=>$product_code, 'qty'=>$product_qty, 'price'=>$obj->price));
if(isset($_SESSION["products"])) //if we have the session
{
$found = false; //set found item to false
foreach ($_SESSION["products"] as $cart_itm) //loop through session array
{
if($cart_itm["code"] == $product_code){ //the item exist in array
$product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$product_qty, 'price'=>$cart_itm["price"]);
$found = true;
}else{
//item doesn't exist in the list, just retrive old info and prepare array for session var
$product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);
}
}
if($found == false) //we didn't find item in array
{
//add new user item in array
$_SESSION["products"] = array_merge($product, $new_product);
}else{
//found user item in array list, and increased the quantity
$_SESSION["products"] = $product;
}
}else{
//create a new session var if does not exist
$_SESSION["products"] = $new_product;
}
}
//redirect back to original page
header('Location:'.$return_url);
}
//remove item from shopping cart
if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["products"]))
{
$product_code = $_GET["removep"]; //get the product code to remove
$return_url = base64_decode($_GET["return_url"]); //get return url
foreach ($_SESSION["products"] as $cart_itm) //loop through session array var
{
if($cart_itm["code"]!=$product_code){ //item does,t exist in the list
$product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);
}
//create a new product list for cart
$_SESSION["products"] = $product;
}
//redirect back to original page
header('Location:'.$return_url);
}
?>
这是最简单的选择。