[Cs3] possible unintended behavior

Mikhail Nesterenko mikhail at cs.kent.edu
Sat Nov 5 14:18:40 EDT 2016


>    void playGame(const int playersCount = 0) {
>       if(playersCount != 0) playersCount_ = playersCount;
>       movesCount_=0;
> 
>       initializeGame();
> 
>       for(int i=0; !endOfGame(); i = (i+1) % playersCount_ ){
> 	 makeMove(i);
> 	 if (i==0) ++movesCount_;
>       }
> 
>       printWinner();
>    }
> Hey Dr. Nesterenko, I found the above code in
> templateMethodGames.cpp's Game definition to have some unexpected
> behavior.
> 
> The for loop cycles through players, enumerating them using i. When i
> == 0, a new cycle was encountered and movesCount_ increments. But
> before this new cycle check, it first makes a move using player i:
> makeMove( i ). As a result, the 0th player sees a different
> movesCount_  than every other player during that cycle; their
> movesCount_ is 1 less.
> 
> e.g. with 3 players, here's the first round of plies:
> 
> Player 0: makeMove( 0 ) invoked when movesCount_ == 0.
> 
> movesCount_ increments.
> 
> Player 1: makeMove( 1 ) invoked when movesCount_ == 1.
> 
> Player 2: makeMove( 2 ) invoked when movesCount_ == 1.
> 
> Wasn't sure that was your intention.

Hmm, it does look a bit weird. I changed the code to this:


      for(int i=0; !endOfGame(); i = (i+1) % playersCount_ ){
         makeMove(i);
         if (i==playersCount_-1) ++movesCount_;
      }

Thanks,
-- 
Mikhail


More information about the cs3 mailing list