int localGame() { system("cls"); char winner = 0; TILE board[121]; memset(&board, 0, sizeof board); initBoard(board); char currentPlayer = 1; TILE t = {0,0,0,0}, g; do { jumpTo(0,0); printf("%s, make a move!\n", currentPlayer==1?"Attacker":"Defender"); printBoard(board); int r; t = selectPiece(board, currentPlayer, t); if (tileEmpty(t)) return 0; printIntense(t); g = selectNewPosition(board, t, &r); if (tileEmpty(g)) return 0; if (r) continue; makeMove(t, g, board); winner = hasWinner(board, board[getIndex(g)]); currentPlayer = currentPlayer % 2 + 1; t = g; } while (!winner); // reblit board to capture last move jumpTo(0,1); printBoard(board); return winner; }
bool BoardModel::moveTile(const vector2du32 &source, const vector2du32 &dest) { if (!tileEmpty(source)) { LOG_DEBUG("Moving tile from " << source.x << "," << source.y << " to " << dest.x << "," << dest.y); SITilePointer sourceTile{ getTile(source) }; WITilePointer destTile{ getTile(dest) }; LOG_DEBUG("Source Value: " << sourceTile->getData()); if (sourceTile) { // __ASSERT(!tileEmpty(xSource, ySource), "Tile " << xSource << "," << ySource << " is empty"); __ASSERT(!destTile.lock(), "Trying to move to a not empty tile: " << dest.x << "," << dest.y << " contains " << destTile.lock()->getData()); _setTile(dest, sourceTile); _setTile(source, WITilePointer()); if (p_tController) p_tController->tileMoved(source, dest, sourceTile); return true; } } else { LOG_DEBUG("Trying to move empty tile: " << source.x << "," << source.y << " ignoring it"); } return false; }
void BoardModel::deleteTile(const vector2du32 &position) { __ASSERT(!tileEmpty(position), "You can only delete not empty tiles"); WITilePointer current = getTile(position); _tiles[position.x][position.y].reset(); if (p_tController) p_tController->tileDeleted(position,current); }
void BoardModel::setTile(const lib::vector2du32 &tPosition, WITilePointer newTile) { __ASSERT(tileEmpty(tPosition), "You can only set data in empty tiles"); _setTile(tPosition, newTile); if (p_tController) p_tController->tileAdded(tPosition, newTile); }
TILE selectPiece(TILE board[], char currentPlayer, TILE last) { do { last = selectTile(board, last); if (tileEmpty(last)) break; printErrorString(ownsPiece(currentPlayer, last) ? 0 : 6); } while (!ownsPiece(currentPlayer, last)); return last; }
void BoardModel::changeTileData(const vector2du32 &source, const BoardTileData &nv) { __ASSERT(!tileEmpty(source), "You can only change data in not empty tiles"); auto tile = getTile(source).lock(); BoardTileData ov = tile->getData(); tile->setData(nv); if (p_tController) p_tController->tileChanged(source, tile, ov,nv); }
TILE selectNewPosition(TILE board[], TILE last, int* r) { TILE g; do { g = selectTile(board, last); if (tileEmpty(g)) break; *r = moveValidity(last, g, board); if (*r==1) break; printErrorString(*r); } while (*r != 0); return g; }