2012-11-08 39 views
0

所以這段代碼是我複製過來的在線博客遊戲的基本輪廓。 SOURCE:http://www.codingfriends.com/index.php/2010/06/10/boggle/C++有人可以解釋這些for循環是什麼意思?

bool findUsersWord(string findThis, Grid<char> &theBoard, Vector<cell> &theRoute, string alreadyFound, int placeY, int placeX) 
{ 
    // need to find the findThis base case 
    if (findThis == alreadyFound) 
    return true; 
    // need to find the first letter within the board and then progress around that. 
    if (alreadyFound.empty()) 
    { 
    for (int rows = 0; rows < theBoard.numRows(); rows++) 
     for (int cols = 0; cols < theBoard.numCols(); cols++) 
     // find the each character within the 
     if (theBoard[rows][cols] == findThis[0]) 
     { 
      alreadyFound = findThis[0]; 
      cell newR; 
      newR.row = rows; 
      newR.col = cols; 
      theRoute.add(newR); 
      if (findUsersWord(findThis, theBoard, theRoute, alreadyFound, rows, cols)) 
      return true; 
      else 
      // clear out the found Board 
      theRoute.clear(); 
     } 
    } 
    else 
    { 
    // try and find the next letters within the area around the base letter 
    // spin around the letter 3 * 3 grid 
    for (int y= (placeY > 0 ? placeY-1: placeY); y <=(placeY == (theBoard.numRows()-1) ? placeY : placeY+1);y++) 
     for (int x=(placeX > 0 ? placeX-1: placeX); x<=(placeX == (theBoard.numCols()-1) ? placeX : placeX+1); x++) 
     if ((theBoard[y][x] == findThis[alreadyFound.length()]) && (!(y==placeY && x ==placeX))) 
      // already used letter 
      if (!placeAlreadyUsed(y,x,theRoute)) 
      { 
      alreadyFound += findThis[alreadyFound.length()]; 
      cell newR; 
      newR.row = y; 
      newR.col = x; 
      theRoute.add(newR); 
      if (findUsersWord(findThis, theBoard,theRoute, alreadyFound, y, x)) 
       return true; 
      else 
      { 
       if (alreadyFound.length() > 1) 
       alreadyFound = alreadyFound.substr(0, alreadyFound.length()-1); 
       theRoute.removeAt(theRoute.size()-1); 
      } 
      } 
    return false; 
    } 
    return false; 
} 

下面的代碼是有問題的代碼,其是代碼的一部分的上方。

for (int y= (placeY > 0 ? placeY-1: placeY); y <=(placeY == (theBoard.numRows()-1) ? placeY : placeY+1);y++) 
    for (int x=(placeX > 0 ? placeX-1: placeX); x<=(placeX == (theBoard.numCols()-1) ? placeX : placeX+1) 

我想知道,如果有人可以把這個代碼轉換成更簡單的代碼,它的使用沒有涉及?然後。我知道它的簡單部分,比如「?」意味着回報,「:」表示下一行,但我失去了一個事實,即它是在一個被用於循環,事實上,它只是看起來像

if(placeY > 0) 
return playceY-1 
placeY; 

我在哪裏出了錯?

+0

我不確定你是否理解'''和':'運算符...基本上'placeY> 0是什麼? placeY-1:placeY)'是說是這樣的:「placeY大於0嗎?如果是,那麼這個表達式的結果是'playeY-1',否則它是'placeY'' – prelic

+0

@pretic:」operator「,而不是「運營商」。 '? :'是具有三個操作數表達式的單個操作符。 –

+0

@亨寧馬克霍姆 - 哎呀,是的,你說得對。 – prelic

回答

2

? :塊只是一個奇怪的看看如果陳述。如果你願意的話,這是一個內聯

這裏的格式

argument ? result evaluated to if true : result evaluated to if false 

下面是一個例子

1<2 ? "Hurray" : "boo" 

將評估爲"Hurray"因爲1 < 2是真實的。但是,如果我們將其切換到1>2它將評估爲"boo"

+1

'? :'塊根本不是一個聲明。 –

+0

考慮到OP的困惑,這可能不是最好的描述。 '?:在「停止執行當前函數並繼續執行調用者停止的地方」這個意義上,'不__返回「,就像'return'語句那樣。與所有其他表達式一樣,'':'的作用是_evaluate_這個或那個,而不是_return_的東西。 –

+0

@亨寧馬克漢姆好點。我會解決它。 – GraphicsMuncher

2

我知道它的簡單部分,如「?」表示返回,「:」表示下一行

嗯,沒有。這不是這意味着什麼。 ?:是一個具有三個操作數表達式的運算符,其中一個出現在?:之間。

placeY > 0 ? placeY-1 : placeY 

是指表達: 「如果placeY > 0然後評估placeY-1;否則評估placeY」。

該代碼的想法是,我們希望,由於某種原因,遍歷(placeX,placeY)旁邊的所有位置的董事會。這些位置形成一個矩形,並使用?:運算符來計算該矩形的左,右,上和下限。例如,上面引用的表達式是針對頂部座標的。它通常是placeY-1,除了如果placeY已經是0,在它上面的板上沒有行,並且在那種情況下placeY本身是最上面的行。

+0

所以我可以把if語句放在for循環中嗎? –

+1

@Kurt:您可以將if語句放在for循環的_body_中,但是在for(;;)'header的括號之間只允許_expressions_。 –

+0

重要的是,你的程序執行流程沒有變化。那整個'placeY> 0? placeY-1:placeY'將評估爲單個值,然後可將其分配給'y'。例如,因爲'if'語句看起來像'if(expression){...}',所以你可以合法地將該行放入'if'條件中。該表達式與if語句完全不同,即使我們在描述「?:」運算符的行爲時使用了「if」這個詞。 – prelic

相關問題