C++ C+中循环的组合+;

C++ C+中循环的组合+;,c++,for-loop,C++,For Loop,我正在做一个跳板,想看看那个黑人玩家是否赢了这场比赛。我做了四个循环来检查,是否有5块石头在水平、垂直或对角方向排成一行。然而,我想将它们结合起来,以节省一些代码行。怎么做?是否可以简单地使用相同的for循环检查白人玩家,或者我应该为白人玩家创建一个新的bool class goBoard { private: boardSquare* entrance; // A pointer containing the address of the boardSquare-object at the

我正在做一个跳板,想看看那个黑人玩家是否赢了这场比赛。我做了四个循环来检查,是否有5块石头在水平、垂直或对角方向排成一行。然而,我想将它们结合起来,以节省一些代码行。怎么做?是否可以简单地使用相同的for循环检查白人玩家,或者我应该为白人玩家创建一个新的bool

class goBoard {
private:
  boardSquare* entrance; // A pointer containing the address of the boardSquare-object at the top left of the grid.
  void zip (boardSquare*, boardSquare*);
  boardSquare* makeRow (); //(int amount)?
  int m, n;

public:
  //goBoard ();
  goBoard (int numberOfRows, int numberOfColumns);
  ~goBoard ();
  void build ();
  void computer (char colour);
  bool squareEmpty (int x, int y);
  void human (char colour);
  void print ();
  bool done ();
  bool won ();
  void makeMove (int x, int y, char colour);

};//class goBoardbool

goBoard::wonBlack () {
   boardSquare* currentSquare = entrance; //assuming that the player starts at the entrance
   bool nextSquare = true;
   if ((*currentSquare).colour == 'B') {
      for (int i = 0; i <= 4; i++) {
         if (nextSquare == true) {
            currentSquare = (*currentSquare).neighbours[2]; //.neighbours[2] is a pointer to the square to the right of the current square
            if ((*currentSquare).colour != 'B')
               nextSquare = false;
            }
      }
      for (int i = 0; i <= 4; i++) {
         if (nextSquare == true) {
            currentSquare = (*currentSquare).neighbours[4];
            if ((*currentSquare).colour != 'B')
               nextSquare = false;
            }
      }
      for (int i = 0; i <= 4; i++) {
         if (nextSquare == true) {
            currentSquare = (*(*currentSquare).neighbours[2]).neighbours[4];
            if ((*currentSquare).colour != 'B')
               nextSquare = false;
            }
      }
      for (int i = 0; i <= 4; i++) {
         if (nextSquare == true) {
            currentSquare = (*(*currentSquare).neighbours[6]).neighbours[4];
            if ((*currentSquare).colour != 'B')
               nextSquare = false;
            }
      }
      if (nextSquare == true)
         return true;
   }
   return false;
}//goBoard::won
class-goBoard{
私人:
boardSquare*Entry;//包含网格左上角boardSquare对象地址的指针。
空拉链(boardSquare*,boardSquare*);
boardSquare*makeRow();/(整数金额)?
int m,n;
公众:
//goBoard();
goBoard(int numberOfRows,int numberOfColumns);
~goBoard();
void build();
计算机(炭色);
布尔平方空(整数x,整数y);
空人(炭色);
作废印刷品();
booldone();
布尔元();
void makeMove(整数x,整数y,字符颜色);
};//类goBoardbool
goBoard::原黑(){
boardSquare*currentSquare=Entry;//假设玩家从入口开始
bool nextSquare=true;
如果((*currentSquare).color=='B'){

对于(int i=0;i如果您想减少行数,我会在这行中选择一些内容:

enum class Direction {vertical, horizontal, downRight, upRight};
enum class Sign {negative, zero, positive}
enum class Semen {white, black};

template <typename Elem>
unsigned int howManyInARow(Direction direction, Sign sign, Elem elem, Semen semen){
    unsigned int ret = 0;
    if(elem == semen){
        ret = 1;
    }
    if(negative != sign)
        ret += howManInARow(direction, positive, elem.getNeighbour(direction, positive), semen);
    if(positive != sign)
        ret += howManInARow(direction, negative, elem.getNeighbour(direction, negative), semen);

    return ret;
}
enum类方向{垂直、水平、向下、垂直};
枚举类符号{负,零,正}
枚举类精液{白,黑};
模板
未签名的int HOWMANYNAROW(方向、符号符号、元素、精液){
无符号整数ret=0;
if(elem==精液){
ret=1;
}
如果(负!=符号)
ret+=howManInARow(方向,阳性,元素GetNeighbor(方向,阳性),精液);
如果(正!=符号)
ret+=howManInARow(方向,阴性,元素GetNexthbor(方向,阴性),精液);
返回ret;
}

无法测试它,因为我没有元素。请尝试一下,如果您喜欢,我可以详细说明它

索引
I
在哪里使用?哪里都没有,这是我使用的循环字母的标准。对不起,我大部分是新手,所以我不了解这些类型的变量…您介意我将我的整个.cc-fi发送给您吗乐?