逻辑问题...... [英] Logic question...

查看:57
本文介绍了逻辑问题......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个游戏,我正在接受我的战斗挑战

功能。我想要做的就是找出如何将

中的碎片分组到相同的空间。有两个方面和所有单位在

相同的太空战斗中。我想在同一个空间中加上攻击因子并保护

因子,然后计算出几率,这样我就可以在赔率表上滚动

。基本上每件都有自己的x和y loc。

这就是我现在所拥有的:


无效战斗(向量< unit>& at, vector< unit>& dt,

board * b,terrain * trn){


vector< fightclassfighting;


/ *将攻击单位添加到战斗类向量中,如果单位在

相同的空间中,则它们将分组在同一个战斗类elemet中



矢量。 * /


for(int atu = 0; atu!= at.size(); atu ++){

if(fighting.size()== 0){

fightclass ft;

ft.addAu(at [atu]);

fighting.push_back(ft);

} else {

for(int lp = 0; lp!= fighting.size(); lp ++){

if(at [atu]。 getXloc()!=战斗[lp] .getX()&&

at [atu] .getYloc()!= fight [lp] .getY()){

fightclass ft;

ft.addAu(at [atu]);

fighting.push_back(ft);

} else {

战斗[lp] .addAu(在[atu]);

}

}

}

}

/ *将防御单位添加到fightclass数组。如果x和y位置与攻击位置(在同一空间中)相同,则它们是

添加到战斗阵列中* /

for(int dtu = 0; dtu!= dt.size(); dtu ++){

for(int lp = 0; lp!= fighting.size(); lp ++) {

if(dt [dtu] .getXloc()== fight [lp] .getX()&&

dt [dtu] .getYloc()= =战斗[lp] .getY()){

战斗[lp] .addDu(dt [dtu]);

}

}

}


//作战常规


for(int lp = 0; lp!= fighting.size (); lp ++){//处理战斗

如果(战斗[lp] .canfight()){

int df = b-> GetSpace(战斗[lp ] .getX(),战斗[lp] .getY());

浮动赔率=

战斗[lp] .getAtk()/战斗[lp] .getDef ();

//获得空间中地形的防御加值

//战斗发生

int roll = rand( ) - trn [df] .defend();

//为地形修改模具

赔率=战斗[l p] .getAtk()/ fighting [lp] .getDef();

//获得攻击防御比率。

if(odds< .5){

MessageBox(NULL," Fighting!1:3"," Info!",MB_OK);

return;

}

if(赔率< 1){

MessageBox(NULL,Fighting!1:2,Info!,MB_OK) ;

返回;

}

if(赔率< 2){

MessageBox(NULL,"战斗!1:1"," Info!",MB_OK);

return;

}

if(赔率< 3 ){

MessageBox(NULL,Fighting!2:1,Info!,MB_OK);

return;

}

if(赔率< 4){

MessageBox(NULL,Fighting!3:1,Info!,MB_OK);

返回;

}

if(赔率< 5){

MessageBox(NULL,战斗! 4:1"," Info!",MB_OK);

return;

}

if(odds< 6){

MessageBox(NULL," Fighting!5:1"," Info!",MB_OK);

return;

}


}

}

for(int lp = 0; lp!= fighting.size(); lp ++){

格斗[lp] .done();

}

fighting.clear();

}


class fightclass {

/ * Fightclass拥有两个单元阵列。两个阵列代表

单位

在相同的空间中可以进行战斗。数组代表

攻击

强制,dt代表防御单位* /

int xl;

int yl;


bool sides2; / *表示攻击中是否有单位和

防守

如果攻击和防守都有单位那么

战斗
将在对立双方之间进行。 * /


vector< unit> at; //攻击单位

vector< unit> dt; //卫冕单位


public:

fightclass();

void addAu(unit au); //添加一个addacking单元

void addDu(unit du); //添加一个防御单位

int getX(){return xl;} //返回x坐标

int getY(){return yl;} //返回y coord

float getAtk(); //返回一个空格的总攻击因子

float getDef(); //返回一个空间的总防御因子

bool canfight(); //如果有攻击和防御单位

void done(); //转弯后清除vectores


};


fightclass :: fightclass(){

xl = 0;

yl = 0;

}


void fightclass :: addAu(unit u){

at.push_back(u);

xl = u.getXloc();

yl = u.getYloc();


}


void fightclass :: addDu(unit u){


dt.push_back(u);


}


bool fightclass :: canfight(){

if(at.size()&& dt。 size()){

返回true;

} else {return false;}

}


float fightclass :: getAtk(){

浮动总数;

for(int lp = 0; lp!= at.size(); lp ++){

总计+ =在[lp] .getAttack();

}

返回总额;

}


我知道逻辑不是最好的,但它是我能做的最好的。这是非常棘手的,我很困惑。我想找到一些

的方法,使逻辑步骤更容易。我可能会忽略一些简单的错误,或者它可能完全搞砸了。

I am writing a game and I am having a challenge with my combat
function. All I want to do is find out how to group pieces that are in
the same space. There are two sides and all the units that are in the
same space fight. I want to add up the attack factors and defending
factors in the same space then figure out the odds so I can roll
against an odds table. Basically each piece holds its own x and y loc.
Here is what I have right now:

void fight(vector<unit>& at, vector<unit>& dt,
board * b , terrain * trn ){

vector<fightclassfighting;

/* adds attacking units to the fightclass vector if units are in the
same space they are to grouped in the same fight class elemet of
the
vector. */

for(int atu = 0; atu != at.size(); atu++){
if(fighting.size() == 0){
fightclass ft;
ft.addAu(at[atu]);
fighting.push_back(ft);
} else {
for(int lp = 0; lp != fighting.size(); lp++){
if(at[atu].getXloc() != fighting[lp].getX() &&
at[atu].getYloc() != fighting[lp].getY()){
fightclass ft;
ft.addAu(at[atu]);
fighting.push_back(ft);
} else {
fighting[lp].addAu(at[atu]);
}
}
}
}
/* Adds defending units to the fightclass array. If x and y locs are
the same as attacking locations (are in the same space) they are
added to array for combat */

for(int dtu = 0; dtu != dt.size(); dtu++){
for(int lp = 0; lp != fighting.size(); lp++){
if(dt[dtu].getXloc() == fighting[lp].getX() &&
dt[dtu].getYloc() == fighting[lp].getY()){
fighting[lp].addDu(dt[dtu]);
}
}
}

// Combat routine

for(int lp = 0; lp != fighting.size(); lp++){ //handles combat
if(fighting[lp].canfight()){
int df = b->GetSpace(fighting[lp].getX(), fighting[lp].getY());
float odds =
fighting[lp].getAtk()/fighting[lp].getDef();
//gets the defense bonus for the terrain in the space where
//combat takes place
int roll = rand() - trn[df].defend();
//get the die roll modified for terrain
odds = fighting[lp].getAtk() / fighting[lp].getDef();
//gets the attack to defence ratio.
if(odds < .5){
MessageBox(NULL, "Fighting! 1:3", "Info!", MB_OK);
return;
}
if(odds < 1){
MessageBox(NULL, "Fighting! 1:2", "Info!", MB_OK);
return;
}
if(odds < 2){
MessageBox(NULL, "Fighting! 1:1", "Info!", MB_OK);
return;
}
if(odds < 3){
MessageBox(NULL, "Fighting! 2:1", "Info!", MB_OK);
return;
}
if(odds < 4){
MessageBox(NULL, "Fighting! 3:1", "Info!", MB_OK);
return;
}
if(odds < 5){
MessageBox(NULL, "Fighting! 4:1", "Info!", MB_OK);
return;
}
if(odds < 6){
MessageBox(NULL, "Fighting! 5:1", "Info!", MB_OK);
return;
}

}
}
for(int lp = 0; lp != fighting.size(); lp++){
fighting[lp].done();
}
fighting.clear();
}

class fightclass{
/* Fightclass holds two arrays of units. The two arrays represent
units
in the same space elgible for combat. Array at represents the
attacking
forces and dt represents the defending units */
int xl;
int yl;

bool sides2; /* indicates if there are units in both attack and
defend
if there are units in both attack and defend then
combat
is to take place between the opposing sides. */

vector<unit>at; //attack units
vector<unit>dt; //defending units

public:
fightclass();
void addAu(unit au); //adds an addacking unit
void addDu(unit du); //adds a defending unit
int getX(){return xl;} //return the x coord
int getY(){return yl;} //returns the y coord
float getAtk(); //returns the total attack factors for one space
float getDef(); //returns the total defending factors for one space
bool canfight(); //if there are both attacking and defending units
void done(); //clears vectores after the turn

};

fightclass::fightclass(){
xl = 0;
yl = 0;
}

void fightclass::addAu(unit u){
at.push_back(u);
xl = u.getXloc();
yl = u.getYloc();

}

void fightclass::addDu(unit u){

dt.push_back(u);

}

bool fightclass::canfight(){
if(at.size() && dt.size()){
return true;
}else {return false;}
}

float fightclass::getAtk(){
float total;
for(int lp = 0; lp != at.size(); lp++){
total +=at[lp].getAttack();
}
return total;
}

I know the logic is not the best but it is the best I can do. This is
tricky and I am pretty confused. I would like to find some way of
making the logic steps easier. There may be some simple error that I
overlooked or it can be totally screwed up.

推荐答案




comp.games.development.programming可能更适合这个

的问题。


- -

问候,Ron AF Greve

http: //moonlit.xs4all.nl


" JoeC" < en ***** @ yahoo.com写信息

news:11 ********************** @ p79g2000cwp。 googlegr oups.com ...
Hi,

comp.games.development.programming is probably more appropiate for this
question.

--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"JoeC" <en*****@yahoo.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...

>我正在写一个游戏,我正在挑战我的战斗

功能。我想要做的就是找出如何将

中的碎片分组到相同的空间。有两个方面和所有单位在

相同的太空战斗中。我想在同一个空间中加上攻击因子并保护

因子,然后计算出几率,这样我就可以在赔率表上滚动

。基本上每件都有自己的x和y loc。


这就是我现在所拥有的:


void fight(vector< unit> & at,vector< unit>& dt,

board * b,terrain * trn){


vector< fightclassfighting;


/ *将攻击单位添加到战斗类向量中,如果单位在

相同的空间中,则它们将分组在同一个战斗类elemet中



向量。 * /


for(int atu = 0; atu!= at.size(); atu ++){

if(fighting.size()== 0){

fightclass ft;

ft.addAu(at [atu]);

fighting.push_back(ft);

} else {

for(int lp = 0; lp!= fighting.size(); lp ++){

if(at [atu]。 getXloc()!=战斗[lp] .getX()&&

at [atu] .getYloc()!= fight [lp] .getY()){

fightclass ft;

ft.addAu(at [atu]);

fighting.push_back(ft);

} else {

战斗[lp] .addAu(在[atu]);

}

}

}

}

/ *将防御单位添加到fightclass数组。如果x和y位置与攻击位置(在同一空间中)相同,则它们是

添加到战斗阵列中* /

for(int dtu = 0; dtu!= dt.size(); dtu ++){

for(int lp = 0; lp!= fighting.size(); lp ++) {

if(dt [dtu] .getXloc()== fight [lp] .getX()&&

dt [dtu] .getYloc()= =战斗[lp] .getY()){

战斗[lp] .addDu(dt [dtu]);

}

}

}


//作战常规


for(int lp = 0; lp!= fighting.size (); lp ++){//处理战斗

如果(战斗[lp] .canfight()){

int df = b-> GetSpace(战斗[lp ] .getX(),战斗[lp] .getY());

浮动赔率=

战斗[lp] .getAtk()/战斗[lp] .getDef ();

//获得空间中的地形防御奖励

//战斗发生

int roll = rand() - trn [df] .defend();

//为地形修改模具

赔率=战斗[lp]。 getAtk()/ fighting [lp] .getDef();

//获得攻击防御比率。

if(odds< .5){

MessageBox(NULL," Fighting!1:3"," Info!",MB_OK);

return;

}

if(赔率< 1){

MessageBox(NULL,Fighting!1:2,Info!,MB_OK) ;

返回;

}

if(赔率< 2){

MessageBox(NULL,"战斗!1:1"," Info!",MB_OK);

return;

}

if(赔率< 3 ){

MessageBox(NULL,Fighting!2:1,Info!,MB_OK);

return;

}

if(赔率< 4){

MessageBox(NULL,Fighting!3:1,Info!,MB_OK);

返回;

}

if(赔率< 5){

MessageBox(NULL,战斗! 4:1"," Info!",MB_OK);

return;

}

if(odds< 6){

MessageBox(NULL,战斗! 5:1"," Info!",MB_OK);

return;

}


}

}

for(int lp = 0; lp!= fighting.size(); lp ++){

fight [lp] .done();

}

fighting.clear();

}


class fightclass {

/ * Fightclass拥有两个单元阵列。两个阵列代表

单位

在相同的空间中可以进行战斗。数组代表

攻击

力量和dt代表防御单位* /


int xl;

int yl;


bool sides2; / *表示攻击中是否有单位和

防守

如果攻击和防守都有单位那么

战斗
将在对立双方之间进行。 * /


vector< unit> at; //攻击单位

vector< unit> dt; //卫冕单位


public:

fightclass();

void addAu(unit au); //添加一个addacking单元

void addDu(unit du); //添加一个防御单位

int getX(){return xl;} //返回x坐标

int getY(){return yl;} //返回y coord

float getAtk(); //返回一个空格的总攻击因子

float getDef(); //返回一个空间的总防御因子

bool canfight(); //如果有攻击和防御单位

void done(); //转弯后清除vectores


};


fightclass :: fightclass(){

xl = 0;

yl = 0;

}


void fightclass :: addAu(unit u){

at.push_back(u);

xl = u.getXloc();

yl = u.getYloc();


}


void fightclass :: addDu(unit u){


dt.push_back(u);


}


bool fightclass :: canfight(){

if(at.size()&& dt。 size()){

返回true;

} else {return false;}

}


float fightclass :: getAtk(){

浮动总数;

for(int lp = 0; lp!= at.size(); lp ++){

总计+ =在[lp] .getAttack();

}

返回总额;

}


我知道逻辑不是最好的,但它是我能做的最好的。这是非常棘手的,我很困惑。我想找到一些

的方法,使逻辑步骤更容易。我可能会忽略一些简单的错误,或者它可能完全搞砸了。
>I am writing a game and I am having a challenge with my combat
function. All I want to do is find out how to group pieces that are in
the same space. There are two sides and all the units that are in the
same space fight. I want to add up the attack factors and defending
factors in the same space then figure out the odds so I can roll
against an odds table. Basically each piece holds its own x and y loc.
Here is what I have right now:

void fight(vector<unit>& at, vector<unit>& dt,
board * b , terrain * trn ){

vector<fightclassfighting;

/* adds attacking units to the fightclass vector if units are in the
same space they are to grouped in the same fight class elemet of
the
vector. */

for(int atu = 0; atu != at.size(); atu++){
if(fighting.size() == 0){
fightclass ft;
ft.addAu(at[atu]);
fighting.push_back(ft);
} else {
for(int lp = 0; lp != fighting.size(); lp++){
if(at[atu].getXloc() != fighting[lp].getX() &&
at[atu].getYloc() != fighting[lp].getY()){
fightclass ft;
ft.addAu(at[atu]);
fighting.push_back(ft);
} else {
fighting[lp].addAu(at[atu]);
}
}
}
}
/* Adds defending units to the fightclass array. If x and y locs are
the same as attacking locations (are in the same space) they are
added to array for combat */

for(int dtu = 0; dtu != dt.size(); dtu++){
for(int lp = 0; lp != fighting.size(); lp++){
if(dt[dtu].getXloc() == fighting[lp].getX() &&
dt[dtu].getYloc() == fighting[lp].getY()){
fighting[lp].addDu(dt[dtu]);
}
}
}

// Combat routine

for(int lp = 0; lp != fighting.size(); lp++){ //handles combat
if(fighting[lp].canfight()){
int df = b->GetSpace(fighting[lp].getX(), fighting[lp].getY());
float odds =
fighting[lp].getAtk()/fighting[lp].getDef();
//gets the defense bonus for the terrain in the space where
//combat takes place
int roll = rand() - trn[df].defend();
//get the die roll modified for terrain
odds = fighting[lp].getAtk() / fighting[lp].getDef();
//gets the attack to defence ratio.
if(odds < .5){
MessageBox(NULL, "Fighting! 1:3", "Info!", MB_OK);
return;
}
if(odds < 1){
MessageBox(NULL, "Fighting! 1:2", "Info!", MB_OK);
return;
}
if(odds < 2){
MessageBox(NULL, "Fighting! 1:1", "Info!", MB_OK);
return;
}
if(odds < 3){
MessageBox(NULL, "Fighting! 2:1", "Info!", MB_OK);
return;
}
if(odds < 4){
MessageBox(NULL, "Fighting! 3:1", "Info!", MB_OK);
return;
}
if(odds < 5){
MessageBox(NULL, "Fighting! 4:1", "Info!", MB_OK);
return;
}
if(odds < 6){
MessageBox(NULL, "Fighting! 5:1", "Info!", MB_OK);
return;
}

}
}
for(int lp = 0; lp != fighting.size(); lp++){
fighting[lp].done();
}
fighting.clear();
}

class fightclass{
/* Fightclass holds two arrays of units. The two arrays represent
units
in the same space elgible for combat. Array at represents the
attacking
forces and dt represents the defending units */
int xl;
int yl;

bool sides2; /* indicates if there are units in both attack and
defend
if there are units in both attack and defend then
combat
is to take place between the opposing sides. */

vector<unit>at; //attack units
vector<unit>dt; //defending units

public:
fightclass();
void addAu(unit au); //adds an addacking unit
void addDu(unit du); //adds a defending unit
int getX(){return xl;} //return the x coord
int getY(){return yl;} //returns the y coord
float getAtk(); //returns the total attack factors for one space
float getDef(); //returns the total defending factors for one space
bool canfight(); //if there are both attacking and defending units
void done(); //clears vectores after the turn

};

fightclass::fightclass(){
xl = 0;
yl = 0;
}

void fightclass::addAu(unit u){
at.push_back(u);
xl = u.getXloc();
yl = u.getYloc();

}

void fightclass::addDu(unit u){

dt.push_back(u);

}

bool fightclass::canfight(){
if(at.size() && dt.size()){
return true;
}else {return false;}
}

float fightclass::getAtk(){
float total;
for(int lp = 0; lp != at.size(); lp++){
total +=at[lp].getAttack();
}
return total;
}

I know the logic is not the best but it is the best I can do. This is
tricky and I am pretty confused. I would like to find some way of
making the logic steps easier. There may be some simple error that I
overlooked or it can be totally screwed up.





谢谢,我不知道那个小组。我会在那里问这个问题。

仍然是编程,我正在尝试通过

编程游戏来学习编程。


Thanks, I didn''t know about that group. I will ask the question there.
Still it is programming and I am trying to learn programming by
programming games.


在文章< 11 ********************** @ p79g2000cwp.googlegroups .com> ;,
en ***** @ yahoo.com 说...


[...]
In article <11**********************@p79g2000cwp.googlegroups .com>,
en*****@yahoo.com says...

[ ... ]

void fight(vector< unit>& at,vector< unit>& dt,

board * b,terrain * trn ){$ / $

vector< fightclassfighting;


/ *如果单位在
$ b中,则向攻击类向量添加攻击单位$ b相同的空间他们被分组在



向量的同一战斗类elemet中。 * /


for(int atu = 0; atu!= at.size(); atu ++){

if(fighting.size()== 0){
void fight(vector<unit>& at, vector<unit>& dt,
board * b , terrain * trn ){

vector<fightclassfighting;

/* adds attacking units to the fightclass vector if units are in the
same space they are to grouped in the same fight class elemet of
the
vector. */

for(int atu = 0; atu != at.size(); atu++){
if(fighting.size() == 0){



一个小细节,但写得更好:

if(fighting.empty()){

鉴于这是一个向量,它可能没有任何区别,

但IMO,它仍然值得做。

A minor detail, but this is better written as:
if (fighting.empty()) {
Given that this is a vector, it probably doesn''t make any difference,
but IMO, it''s still worth doing.


fightclass ft;

ft.addAu(at [atu]);

fighting.push_back(ft);

} else {

for(int lp = 0; lp!= fighting.size(); lp ++){
fightclass ft;
ft.addAu(at[atu]);
fighting.push_back(ft);
} else {
for(int lp = 0; lp != fighting.size(); lp++){



只看从这里开始的一点点:

Looking just at the tiny bit starting here:


if(at [atu] .getXloc()!= fight [lp] .getX()&&
在[atu] .getYloc()的
!=战斗[lp] .getY()){
if(at[atu].getXloc() != fighting[lp].getX() &&
at[atu].getYloc() != fighting[lp].getY()){



我看到什么看起来像是一个机会改进:创建一个

类(或结构)来表示一个位置,然后无论你在哪里使用

都有职位。在这种情况下,这将成为:

if(at [atu] .pos == fight [lp] .pos)

//做任何事情


我也会尝试使用一致的命名 - 无论你是否决定

''pos''或''position''或''location''或' 坐标或者其他什么,尝试

在任何地方使用相同的名称来处理那种

的事情。在上面的代码中,你有getXloc和getX,两者显然都在做同样的事情。虽然

这两件事情都很明显,但是当你编写代码时,如果

就可以了提前猜测会员的名字是什么?
而不是每次都要检查它的位置,位置,

等等特殊情况。

I see what looks like an opportunity for a bit of improvement: create a
class (or struct) to represent a position, and then use it wherever you
have positions. In that case, this would become:
if (at[atu].pos == fighting[lp].pos)
// do whatever

I''d also attempt to use consistent naming -- whether you decide on
''pos'' or ''position'' or ''location'' or ''coordinate'' or whatever, attempt
to use the same name everywhere you have to deal with that kind of
thing. In the code above, you''ve got getXloc and getX, both apparently
doing the same kind of thing. While it''s obvious after the fact that
both do the same kinds of things, when you''re writing code, it''s nice if
you can guess ahead of time what the name of a member is going to be
instead of having to check every time whether it''s location, position,
etc. in this particular case.


fightclass ft;

ft.addAu(at [atu]);

格斗。的push_back(英尺);
fightclass ft;
ft.addAu(at[atu]);
fighting.push_back(ft);



在这种情况下,''what''部分也可以简化一点。我会
创建一个以单位作为参数的战斗类ctor,所以它使用单个攻击单元创建一个战斗类对象,然后使用

在这里:

fighting.push_back(fightclass(at [atu]));

In this case, the ''whatever'' part can be simplified a bit as well. I''d
create a fightclass ctor that takes a unit as its argument, so it
creates a fightclass object with a single attacking unit, and then use
it here:
fighting.push_back(fightclass(at[atu]));


} else {

战斗[lp] .addAu(在[atu]);

}

}

}

}
} else {
fighting[lp].addAu(at[atu]);
}
}
}
}



至少如果我正确地阅读了意图,我会考虑这么多

足够一个函数,虽然函数可能包括以下

块:

At least if I''m reading the intent correctly, I''d consider this much
enough for one function, though the function might include the following
chunk as well:


/ *将防御单位添加到fightclass数组。如果x和y位置与攻击位置(在同一空间中)相同,则它们是

添加到战斗阵列中* /

for(int dtu = 0; dtu!= dt.size(); dtu ++){

for(int lp = 0; lp!= fighting.size(); lp ++) {

if(dt [dtu] .getXloc()== fight [lp] .getX()&&

dt [dtu] .getYloc()= =战斗[lp] .getY()){

战斗[lp] .addDu(dt [dtu]);

}

}

}
/* Adds defending units to the fightclass array. If x and y locs are
the same as attacking locations (are in the same space) they are
added to array for combat */

for(int dtu = 0; dtu != dt.size(); dtu++){
for(int lp = 0; lp != fighting.size(); lp++){
if(dt[dtu].getXloc() == fighting[lp].getX() &&
dt[dtu].getYloc() == fighting[lp].getY()){
fighting[lp].addDu(dt[dtu]);
}
}
}



到此为止。为了实现我理解的逻辑,我会做两个

函数,但要实现我认为你真正想要的逻辑,我会做

一个。有关详细信息,请参阅下面的评论。

Down to here. To implement the logic as I understand it, I''d make it two
functions, but to implement the logic I think you really want, I''d make
it one. See comments below for more on that.


//作战例程


for(int lp = 0; lp!= fighting.size(); lp ++){//处理战斗
// Combat routine

for(int lp = 0; lp != fighting.size(); lp++){ //handles combat



从这里开始:

Starting from here:


if (战斗[lp] .canfight()){

int df = b-> GetSpace(战斗[lp] .getX(),战斗[lp] .getY());

浮动赔率=

格斗[lp] .getAtk()/格斗[lp] .getDef();

//获得防御加值空间中的地形

//战斗发生

int roll = rand() - trn [df] .defend();

//为地形修改模具

赔率=战斗[lp] .getAtk()/ fighting [lp] .getDef();

//获得攻击防御比率。
if(fighting[lp].canfight()){
int df = b->GetSpace(fighting[lp].getX(), fighting[lp].getY());
float odds =
fighting[lp].getAtk()/fighting[lp].getDef();
//gets the defense bonus for the terrain in the space where
//combat takes place
int roll = rand() - trn[df].defend();
//get the die roll modified for terrain
odds = fighting[lp].getAtk() / fighting[lp].getDef();
//gets the attack to defence ratio.



....并且下到这里可能应该是另一个功能。

.... and going down to here probably deserves to be yet another function.


if (赔率<.5){

MessageBox(NULL,Fighting!1:3,Info!,MB_OK);

return;

}

if(赔率< 1){

MessageBox(NULL," Fighting!1:2"," Info!" ;,MB_OK);

返回;

}

if(赔率< 2){

MessageBox( NULL,Fighting!1:1,Info!,MB_OK);

return;

}

if(赔率< 3){

MessageBox(NULL,Fighting!2:1,Info!,MB_OK);

return;

}

if(赔率< 4){

MessageBox(NULL,Fighting!3:1,Info!, MB_OK);

返回;

}

if(赔率< 5){

MessageBox(NULL,Fighting!4:1,Info!,MB_OK);

return;

}

if(赔率< 6){

MessageBox(NULL," Fighting!5:1"," Info!",MB_OK);

返回;

}
if(odds < .5){
MessageBox(NULL, "Fighting! 1:3", "Info!", MB_OK);
return;
}
if(odds < 1){
MessageBox(NULL, "Fighting! 1:2", "Info!", MB_OK);
return;
}
if(odds < 2){
MessageBox(NULL, "Fighting! 1:1", "Info!", MB_OK);
return;
}
if(odds < 3){
MessageBox(NULL, "Fighting! 2:1", "Info!", MB_OK);
return;
}
if(odds < 4){
MessageBox(NULL, "Fighting! 3:1", "Info!", MB_OK);
return;
}
if(odds < 5){
MessageBox(NULL, "Fighting! 4:1", "Info!", MB_OK);
return;
}
if(odds < 6){
MessageBox(NULL, "Fighting! 5:1", "Info!", MB_OK);
return;
}



这个块可能也应该是一个功能 - 但我'' d写一个

有点不同,例如:


void show_odds(double odds){

//

//目前我假设赔率总是在0..6范围内。

//目前,我已经作弊,并且混淆了1 :3和1:2一起进入

//outnumbered。

//

static char * strings [] = {

Outnumbered,1:1,2:1,3:1,4:1,5:1};


字符串输出(" Fighting!");

输出+ =字符串[(i nt)赔率];

MessageBox(NULL,输出," Info!",MB_OK);

}


以下主要是伪代码,而不是C ++。机会

是我遗漏了一些真正需要的东西(例如

对fightclass :: done的调用)。尽管如此,添加这样的东西不应该太糟糕了。


虚假战斗(矢量< unit& at,vector< unit& dt,board * b,terrain * trn)

{

vector< fightclassfighting;


add_attacking_units(at.begin(),at。结束(),

std :: back_inserter(格斗));


add_defending_units(dt.begin(),dt.end(),
std :: back_inserter(格斗));


std :: vector< doubleodds_values(fighting.size());


std :: transform(fighting.begin(),fighting.end(),

odds_values.begin(),bind1st(figure_odds,trn));


std :: for_each(odd_values.begin(),odds_values.end(),show_odds);

}


目前,我'已经使用了你的函数签名,但很可能是

相当不错(例如)所有参数应该引用const

值。也许攻击/防御部队可以被杀死所以那些可以被修改,但几乎可以肯定的是,董事会和

可能是地形也应该是''改变 - 除非你得到比大多数更复杂的

,并考虑到诸如

火炮稍微改变地形的事情。


至少在我这里提出这个想法的时候,我可能也会稍微更改

基本算法。除非我误解了它,现在你正在为每个攻击单位添加

a fightclass对象到战斗向量,

是否有一个防御单位在相同的位置与否。然后

你在任何地方都有攻击单位添加防御单位。我相信你的真实意图是单位只相互争斗

他们在同一个地方,所以你真的想要一个交集的

两个 - 即代表那些地方的一组战斗对象

同一个地方的攻击者和防御者。


如果是这样的话,我会考虑做一些不同的事情。一个

的可能性是将每个部队保持在多个集合中,单位按位置排序

。要移动一个单元,你将它从多重集中删除,然后再将它们重新插入新位置。在这种情况下,找到两个力之间的交叉点(即它们在相同的

位置中具有的单位)可能就像对std:set_intersection的单次调用一样简单。


我完全不确定为什么板和/或地形也是一个指针 - 它显示这些可能是向量的引用,或者那个上面的东西

订单。目前,我正在独自离开,但我怀疑一些

重新审视这一点可能是值得的。


应用这些评论,我们可以最终得到一个类似的函数:


void fight(multiset< unitconst& at,//攻击单位

multiset< unitconst& dt,/ /防守单位

board const * b,

terrain const * trn)

{

vector< fightclassfighting;


find_fighting_units(at.begin(),at.end(),

dt.begin(),dt.end(),

std :: back_inserter(格斗));


vector< doubleodds_values(fighting.size());


transform( fighting.begin(),fighting.end(),

odds_values.begin(),bind1st(figure_odds,trn));


for_each(odd_values。 begin(),odds_values.end(),show_odds);

}


虽然它或多或少偏离主题,但它似乎也是如此使用MessageBox

来显示赔率是一个糟糕的主意 - 如果部队之间有很多交叉点

,那么用户将不得不点击很多

的框,并且尽快他点击了OK,他丢失了那些信息,所以

对他来说很难有任何整体情况。


-

后来,

杰瑞。


宇宙是自己想象的虚构。

This chunk probably also deserves to be a function -- but I''d write it a
bit differently, something like:

void show_odds(double odds) {
//
// For the moment I''m assuming that odds is always in the range 0..6.
// For the moment, I''ve cheated, and conflated 1:3 and 1:2 together into
// "outnumbered".
//
static char *strings[] = {
"Outnumbered", "1:1", "2:1", "3:1", "4:1", "5:1"};

string output("Fighting! ");
output += strings[(int)odds];
MessageBox(NULL, output, "Info!", MB_OK);
}

The following is meant primarily as pseudo-code, not really C++. Chances
are that I''ve left out a few things that really need to be there (like
the calls to fightclass::done). Still, adding things like that shouldn''t
be too horrible.

void fight(vector<unit&at, vector<unit&dt, board *b, terrain *trn)
{
vector<fightclassfighting;

add_attacking_units(at.begin(), at.end(),
std::back_inserter(fighting));

add_defending_units(dt.begin(), dt.end(),
std::back_inserter(fighting));

std::vector<doubleodds_values(fighting.size());

std::transform(fighting.begin(), fighting.end(),
odds_values.begin(), bind1st(figure_odds, trn));

std::for_each(odd_values.begin(), odds_values.end(), show_odds);
}

For the moment, I''ve used your function signature, but chances are
pretty good that (for example) all the parameters should refer to const
values. Perhaps the attacking/defending forces can be killed so those
vectors can be modified, but it''s almost certain that the board and
probably terrain as well shouldn''t be changed -- unless you''re getting a
lot more sophisticated than most, and taking into account things like
artillery changing the terrain slightly.

At least as I undertand the idea here, I''d probably also change the
basic algorithm a bit. Unless I''m misreadin it, right now you''re adding
a fightclass object to the fighting vector for every attacking unit,
whether there''s a defending unit at the same position or not. Then
you''re adding defending units everywhere there are attacking units. I
believe your real intent is that units only fight each other where
they''re at the same location, so you really want an intersection of the
two -- i.e. a set of fight objects representing those places there''s
both an attacker and a defender in the same place.

If that''s the case, I''d consider doing things a bit differently. One
possibility would be to keep each force in a multiset, with the units
sorted by position. To move a unit, you remove it from the multiset, and
then insert it back in at the new position. In this case, finding the
intersection between two forces (i.e. the units they have in the same
positions) might be as simple as a single call to std:set_intersection.

I''m not at all sure why board and/or terrain is a pointer either -- it
appears these could be references to vectors, or something on that
order. For the moment, I''m leaving the alone, but I suspect some
reexamination of this point might be worthwhile.

Applying those comments, we could end up with a function something like:

void fight(multiset<unitconst &at, // attacking units
multiset<unitconst &dt, // defending units
board const *b,
terrain const *trn)
{
vector<fightclassfighting;

find_fighting_units(at.begin(), at.end(),
dt.begin(), dt.end(),
std::back_inserter(fighting));

vector<doubleodds_values(fighting.size());

transform(fighting.begin(), fighting.end(),
odds_values.begin(), bind1st(figure_odds, trn));

for_each(odd_values.begin(), odds_values.end(), show_odds);
}

Though it''s more or less off-topic, it also seems like using MessageBox
for showing the odds is a poor idea -- if there are many intersections
between the forces, the user is going to have to click through a lot of
boxes, and as soon as he clicks OK on one, he loses that information, so
it''s hard for him to any kind of overall picture.

--
Later,
Jerry.

The universe is a figment of its own imagination.


这篇关于逻辑问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆