void drawGameState(GContext* ctx, const DemoGameState gameState) { // Note: Because the layer is blanked before we're called, we also need to // redraw all the player moves preceeding the current move each time // we're called. // If we've reached the final part of the sequence don't draw anything. if (gameState.SEQUENCE[gameState.sequenceOffset] == DRAW_BLANK) { return; } unsigned short activePlayer = 0; for (unsigned short currentOffset = 0; currentOffset <= gameState.sequenceOffset; currentOffset++) { switch (gameState.SEQUENCE[currentOffset]) { case DRAW_BLANK: // Do nothing as a DRAW_BLANK value in a non terminal position // is just padding so the sequence has the correct length. break; case DRAW_WIN_LINE: // TODO: Don't hard code this--calculate the winner from the board. if (gameState.SEQUENCE == SEQUENCE_SECOND_PLAYER_WINS) { drawWinLine(ctx, getCellCenter(0), getCellCenter(6)); } else { // Assumes first player wins drawWinLine(ctx, getCellCenter(6), getCellCenter(2)); } break; default: // The value is a cell location offset. // Draw the marker for the current player who is active in this turn. (activePlayer ? drawCrossPlayer : drawCirclePlayer)(ctx, getCellCenter(gameState.SEQUENCE[currentOffset])); activePlayer = (activePlayer + 1) % 2; break; } } }
bool TicTacToe::checkForWinner(CCObject* obj) { Mark* m = (Mark*) obj; CCLog("checking line with [%d, %d]: %d", m->getX(), m->getY(), m->getState()); int16_t winner = -1; LINE_DIRECTION dir; for(int16_t d = 0; d < DIR_END; d++) { dir = LINE_DIRECTION(d); auto s = getLineState(m->getX(), m->getY(), dir); winner = checkFor3Marks(s); CCLog("dir: %d, winner: %d", dir, winner); if (winner >= 0) break; } if (winner>=0) { drawWinLine(m->getX(), m->getY(), dir, m); } CCLog("Found winner: %s (%d)", winner >= 0 ? "yes" : "no", winner); return false; }
void Render::draw(DrawVariables* dw) { if (m_engine == nullptr) return; drawBoard(dw, m_engine->getBoard().getRows(), m_engine->getBoard().getCols()); drawPosition(dw); if (m_engine->getGameState() == GameState::GAME_OVER) { // draw the win line over the winners pieces static const std::map<WinDir, Coordinate> s_winDir{ { WinDir::EAST, {0,1} }, { WinDir::SOUTH, {1,0} }, { WinDir::SOUTH_EAST, {1,1} }, { WinDir::SOUTH_WEST, {1,-1} } }; PosInfo winInfo = m_engine->getGameOverInfo(); Coordinate coordStart, coordEnd; coordStart = winInfo.winpos; coordEnd = { coordStart.x + s_winDir.at(winInfo.windir).x * (m_engine->getWinRowLenght() - 1), coordStart.y + s_winDir.at(winInfo.windir).y * (m_engine->getWinRowLenght() - 1) }; drawWinLine(dw, coordStart, coordEnd); } }