bool Game::modSquare(string mod, int color) { if(mod.length() != 3) return false; int loc = Board::toInts(mod.substr(1,2)); if(loc < 0) return false; int x = loc / 10; int y = loc % 10; Piece * nPiece; switch(mod.at(0)) { case 'P': case 'p': nPiece = new Pawn(color, this); if(!(color == WHITE && y == 1) && !(color == BLACK && y == 6)) static_cast<Pawn *>(nPiece)->hasMoved = true; break; case 'N': case 'n': nPiece = new Knight(color, this); break; case 'B': case 'b': nPiece = new Bishop(color, this); break; case 'R': case 'r': nPiece = new Rook(color, this); if(!(color == WHITE && y == 0 && (x == 0 || x == 7)) && !(color == BLACK && y == 7 && (x == 0 || x == 7))) static_cast<Rook *>(nPiece)->hasMoved = true; break; case 'Q': case 'q': nPiece = new Queen(color, this); break; case 'K': case 'k': nPiece = new King(color, this); setKing(color, nPiece); if(!(color == WHITE && y == 0 && x == 4) && !(color == BLACK && y == 7 && x == 4)) static_cast<King *>(nPiece)->hasMoved = true; break; case 'X': case 'x': default: nPiece = NULL; break; } if(board->pieces[x][y] != NULL) { //is this where the bug was? /*bool add = true; for(unsigned int i = 0; i < changes[place].size(); i++){ if(changes[place][i].moded == board->pieces[x][y]){ add = false; } } if(add){*/ change_t c; c.moded = board->pieces[x][y]; c.oldLoc =x*10 + y; c.newLoc = c.oldLoc; c.captured = true; c.firstMove = false; c.ep = false; addChange(c); //} } if(nPiece != NULL) { nPiece->setLocation(x, y); board->pieces[x][y] = nPiece; change_t c; c.moded = nPiece; c.oldLoc = -11; c.newLoc = x*10+y; c.captured = false; c.firstMove = false; c.ep = false; addChange(c); } else board->pieces[x][y] = NULL; return true; }
SS3OPiece* CS3OParser::LoadPiece(S3DModel* model, SS3OPiece* parent, unsigned char* buf, int offset) { model->numPieces++; Piece* fp = (Piece*)&buf[offset]; fp->swap(); // Does it matter we mess with the original buffer here? Don't hope so. SS3OPiece* piece = new SS3OPiece(); piece->offset.x = fp->xoffset; piece->offset.y = fp->yoffset; piece->offset.z = fp->zoffset; piece->primType = fp->primitiveType; piece->name = (char*) &buf[fp->name]; piece->parent = parent; if (parent != NULL) { piece->parentName = parent->name; } piece->SetVertexCount(fp->numVertices); piece->SetVertexDrawIndexCount(fp->vertexTableSize); // retrieve each vertex int vertexOffset = fp->vertices; for (int a = 0; a < fp->numVertices; ++a) { Vertex* v = reinterpret_cast<Vertex*>(&buf[vertexOffset]); v->swap(); SS3OVertex sv; sv.pos = float3(v->xpos, v->ypos, v->zpos); sv.normal = float3(v->xnormal, v->ynormal, v->znormal).SafeANormalize(); sv.texCoord = float2(v->texu, v->texv); piece->SetVertex(a, sv); vertexOffset += sizeof(Vertex); } // retrieve the draw order for the vertices int vertexTableOffset = fp->vertexTable; for (int a = 0; a < fp->vertexTableSize; ++a) { const int vertexDrawIdx = swabDWord(*(int*) &buf[vertexTableOffset]); piece->SetVertexDrawIndex(a, vertexDrawIdx); vertexTableOffset += sizeof(int); } piece->goffset = piece->offset + ((parent != NULL)? parent->goffset: ZeroVector); piece->SetHasGeometryData(piece->GetVertexDrawIndexCount() != 0); piece->SetVertexTangents(); piece->SetMinMaxExtends(); model->mins = std::min(piece->goffset + piece->mins, model->mins); model->maxs = std::max(piece->goffset + piece->maxs, model->maxs); piece->SetCollisionVolume(new CollisionVolume("box", piece->maxs - piece->mins, (piece->maxs + piece->mins) * 0.5f)); int childTableOffset = fp->children; for (int a = 0; a < fp->numchildren; ++a) { int childOffset = swabDWord(*(int*) &buf[childTableOffset]); SS3OPiece* childPiece = LoadPiece(model, piece, buf, childOffset); piece->children.push_back(childPiece); childTableOffset += sizeof(int); } return piece; }
Piece RockPaperScissors::getPieceFromVector(const vector<string>& tokens) const { Piece p; //format: <PIECE_CHAR> <X> <Y> switch (tokens[0].at(0)) { case 'R': p.setrpc(RPC::Rock); break; case 'P': p.setrpc(RPC::Paper); break; case 'S': p.setrpc(RPC::Scissors); break; case 'B': p.setrpc(RPC::Bomb); break; case 'F': p.setrpc(RPC::Flag); break; case 'J': p.setJoker(true); //format: J <X> <Y> <PIECE_CHAR> switch (tokens[3].at(0)) { case 'R': p.setrpc(RPC::Rock); break; case 'P': p.setrpc(RPC::Paper); break; case 'S': p.setrpc(RPC::Scissors); break; case 'B': p.setrpc(RPC::Bomb); break; } break; } return p; }
void Board::draw(const Piece &piece) { flood(piece, piece.getType()); }
void RockPaperScissors::setMove(int lineNumber, const string& line, int playerNumber, Status& currentStatus) { //split string by white spaces istringstream buf(line); istream_iterator<string> beg(buf), end; vector<string> tokens(beg, end); int fromRow, fromCol, toRow, toCol; if (isMoveFormatCorrect(tokens)) { fromRow = stoi(tokens[0]) - 1; fromCol = stoi(tokens[1]) - 1; toRow = stoi(tokens[2]) - 1; toCol = stoi(tokens[3]) - 1; //player doesn't have a piece at source if (getPlayerOwningCell(fromRow, fromCol) != playerNumber) { cout << "Player " << playerNumber << " has no pieces to move at " << fromRow + 1 << "," << fromCol + 1 << " when executing line number " << lineNumber + 1 << ":" << endl \ << line << endl; currentStatus.setStatus(playerNumber, PossibleStatus::Move_File_Error, lineNumber, line); return; } //piece at source isn't a moving piece else if (getPieceAt(fromRow, fromCol).getrpc() != RPC::Rock && \ getPieceAt(fromRow, fromCol).getrpc() != RPC::Paper && \ getPieceAt(fromRow, fromCol).getrpc() != RPC::Scissors) { cout << "Player " << playerNumber << " has no mobile pieces at " << fromRow + 1 << "," << fromCol + 1 << " when executing line " << lineNumber + 1 << ":" << endl \ << line << endl; currentStatus.setStatus(playerNumber, PossibleStatus::Move_File_Error, lineNumber+1, line); return; } //player already has a piece at destination else if (getPlayerOwningCell(toRow, toCol) == playerNumber) { cout << "Player " << playerNumber << " already has a " << getPieceAt(toRow, toCol) << " piece at " << toRow + 1 << "," << toCol + 1 << " when executing line number " << lineNumber + 1 << ":" << endl \ << line << endl; currentStatus.setStatus(playerNumber, PossibleStatus::Move_File_Error, lineNumber, line); return; } } //illegal format else { cout << "Player " << playerNumber << " has a faulty format in line " << lineNumber + 1 << " : " << endl \ << line << endl \ << "Correct format is:" << endl \ << "<FROM_X> <FROM_Y> <TO_X> <TO_Y> [J: <Joker_X> <Joker_Y> <NEW_REP>]" << endl; currentStatus.setStatus(playerNumber, PossibleStatus::Move_File_Error, lineNumber, line); return; } //move the piece //place piece at new cell if (!placePiece(playerNumber, Piece(), fromRow, fromCol, toRow, toCol, lineNumber, line, currentStatus)) return; if (tokens.size() == 8) { Cell& c = board[stoi(tokens[5]) - 1][stoi(tokens[6]) - 1]; if (c.getPlayerOwning() == playerNumber && c.getCellPiece().getisJoker() == true) { Piece pTo; pTo.setJoker(true); switch (tokens[7].at(0)) { case 'R': pTo.setrpc(RPC::Rock); break; case 'P': pTo.setrpc(RPC::Paper); break; case 'S': pTo.setrpc(RPC::Scissors); break; case 'B': pTo.setrpc(RPC::Bomb); break; } c.setCell(pTo, playerNumber); } else { cout << "Player " << playerNumber << " has no joker piece at " << stoi(tokens[5]) << "," << stoi(tokens[6]) << " when executing line number " << lineNumber + 1 << ":" << endl \ << line << endl; currentStatus.setStatus(playerNumber, PossibleStatus::Move_File_Error, lineNumber+1, line); } } }
void RockPaperScissors::printBoard() { char chr = '_'; for (int i = 0; i < NUM_OF_ROWS; ++i) { for (int j = 0; j < NUM_OF_COLS; ++j) { Cell& cel = board[i][j]; Piece p = cel.getCellPiece(); int playerOwn = cel.getPlayerOwning(); if (playerOwn == PLAYER_ONE) { if (p.getisJoker()) chr = 'J'; else { switch (p.getrpc()) { case RPC::Rock: chr = 'R'; break; case RPC::Paper: chr = 'P'; break; case RPC::Scissors: chr = 'S'; break; case RPC::Bomb: chr = 'B'; break; case RPC::Flag: chr = 'F'; break; default: break; } } } else if (playerOwn == PLAYER_TWO) { if (p.getisJoker()) chr = 'j'; else { switch (p.getrpc()) { case RPC::Rock: chr = 'r'; break; case RPC::Paper: chr = 'p'; break; case RPC::Scissors: chr = 's'; break; case RPC::Bomb: chr = 'b'; break; case RPC::Flag: chr = 'f'; break; default: break; } } } else { chr = '_'; } cout << chr; } cout << endl; } cout << endl; }
bool SfenParser::parsePosition(const char* arg1, const char* arg2, const char* arg3, const char* /*arg4*/, Position& position) { MutablePosition mp; { int file = 9; int rank = 1; for (const char* p = arg1; p[0] != '\0'; p++) { if (p[0] == '/') { if (file != 0) { LOG(warning) << "invalid format: '" << arg1 << '\''; return false; } if (rank == 9) { LOG(warning) << "invalid format: '" << arg1 << '\''; return false; } file = 9; rank = rank + 1; } else if (isdigit(p[0])) { int len = p[0] - '0'; for (int i = 0; i < len; i++) { if (file == 0) { LOG(warning) << "invalid format: '" << arg1 << '\''; return false; } mp.board[Square(file, rank).raw()] = Piece::empty(); file = file - 1; } } else { Piece piece = Piece::parseSFEN(p); if (piece.isEmpty()) { LOG(warning) << "invalid format: '" << arg1 << '\''; return false; } mp.board[Square(file, rank).raw()] = piece; file = file - 1; if (p[0] == '+') { p++; } } } if (file != 0 || rank != 9) { LOG(warning) << "invalid format: '" << arg1 << '\''; return false; } } { if (strcmp(arg2, "b") == 0) { mp.turn = Turn::Black; } else if (strcmp(arg2, "w") == 0) { mp.turn = Turn::White; } else { LOG(warning) << "invalid format: '" << arg2 << '\''; return false; } } { HAND_EACH(piece) { mp.blackHand.set(piece, 0); mp.whiteHand.set(piece, 0); } if (strcmp(arg3, "-") != 0) { for (const char* p = arg3; p[0] != '\0'; p++) { int num = 1; if (isdigit(p[0])) { num = strtol(p, nullptr, 10); p = isdigit(p[1]) ? p + 2 : p + 1; } Piece piece = Piece::parseSFEN(p); if (piece.isEmpty()) { LOG(warning) << "invalid format: '" << arg3 << '\''; return false; } if (piece.isBlack()) { mp.blackHand.set(piece.type(), num); } else { mp.whiteHand.set(piece.type(), num); } } } } position.initialize(mp); return true; }
//***************************************************** bool Board::isValidScoutMove(Piece* const scout, Piece* const destination){ // temporary piece Piece* tmpPiece = 0; // valid and found booleans bool valid = true, found = false; // counter variable int i = 0; // direction to destination // up - 1, right - 2, down - 3, left - 4 int direction = 0; if((scout->getBoardSpace() % 10) == (destination->getBoardSpace() % 10)){ if(scout->getBoardSpace() > destination->getBoardSpace()){ direction = 1; }else{ direction = 3; } }else{ if(scout->getBoardSpace() > destination->getBoardSpace()){ direction = 4; }else{ direction = 2; } } switch(direction){ // above case 1: i = scout->getBoardSpace() - 10; while(!found && i > -1){ tmpPiece = findPieceAtBoardSpace(i); if(tmpPiece != 0){ if(tmpPiece->getBoardSpace() == destination->getBoardSpace()){ found = true; } if(tmpPiece->getBoardSpace() != destination->getBoardSpace() && tmpPiece->getRank() != 0){ valid = false; } }else{ found = true; valid = false; } i -= 10; } break; // to the right of case 2: i = scout->getBoardSpace() + 1; while(!found && i < (((scout->getBoardSpace() / 10) + 1) * 10)){ tmpPiece = findPieceAtBoardSpace(i); if(tmpPiece != 0){ if(tmpPiece->getBoardSpace() == destination->getBoardSpace()){ found = true; } if(tmpPiece->getBoardSpace() != destination->getBoardSpace() && tmpPiece->getRank() != 0){ valid = false; } }else{ found = true; valid = false; } i++; } break; //below case 3: i = scout->getBoardSpace() + 10; while(!found && i < 100){ tmpPiece = findPieceAtBoardSpace(i); if(tmpPiece != 0){ if(tmpPiece->getBoardSpace() == destination->getBoardSpace()){ found = true; } if(tmpPiece->getBoardSpace() != destination->getBoardSpace() && tmpPiece->getRank() != 0){ valid = false; } }else{ found = true; valid = false; } i += 10; } break; //to the left of case 4: i = scout->getBoardSpace() - 1; while(!found && i > (((scout->getBoardSpace() / 10) * 10)) - 1){ tmpPiece = findPieceAtBoardSpace(i); if(tmpPiece != 0){ if(tmpPiece->getBoardSpace() == destination->getBoardSpace()){ found = true; } if(tmpPiece->getBoardSpace() != destination->getBoardSpace() && tmpPiece->getRank() != 0){ valid = false; } }else{ found = true; valid = false; } i--; } break; } return valid; }
// Given a position in the field, it gets the piece on it and determines its possible moves std::vector<Position> Board::moves(const Position& position) const { std::vector<Position> possibleMoves; Piece* p = this->getPiece(position); if ((!p) || (!p->isUnit())) { return possibleMoves; // case empty or not a Unit (cannot move) } Position from = p->getPosition(); // Scout case if(p->getValue()==SCOUT) { for(int i=1+from.x;i<10;i++) { // go right if(this->isCorrectRelativeMove(from, Position(i, from.y))) { possibleMoves.push_back(Position(i, from.y)); if(!this->isCaseFree(Position(i, from.y))) { break; // stop if the position contains something } } else { break; } } for(int i=from.x-1;i>=0;i--) { // go left if(this->isCorrectRelativeMove(from, Position(i, from.y))) { possibleMoves.push_back(Position(i, from.y)); if(!this->isCaseFree(Position(i, from.y))) { break; // stop if the position contains something } } else { break; } } for(int i=1+from.y;i<10;i++) { // go up if(this->isCorrectRelativeMove(from, Position(from.x, i))) { possibleMoves.push_back(Position(from.x, i)); if(!this->isCaseFree(Position(from.x, i))) { break; // stop if the position contains something } } else { break; } } for(int i=from.y-1;i>=0;i--) { // go down if(this->isCorrectRelativeMove(from, Position(from.x, i))) { possibleMoves.push_back(Position(from.x, i)); if(!this->isCaseFree(Position(from.x, i))) { break; // stop if the position contains something } } else { break; } } } else { // normal case Position toRight = Position(1+from.x, from.y); if(this->isCorrectRelativeMove(from, toRight)) { possibleMoves.push_back(toRight); } Position toLeft = Position(from.x-1, from.y); if(this->isCorrectRelativeMove(from, toLeft)) { possibleMoves.push_back(toLeft); } Position toUp = Position(from.x, 1+from.y); if(this->isCorrectRelativeMove(from, toUp)) { possibleMoves.push_back(toUp); } Position toDown = Position(from.x, from.y-1); if(this->isCorrectRelativeMove(from, toDown)) { possibleMoves.push_back(toDown); } } return possibleMoves; }
void MakePieces() { Piece t = Piece(3, 0.0, 0.0, 0.0); t.Set(0, 0, true); t.Set(1, 0, true); t.Set(2, 0, true); t.Set(1, 1, true); t.Set(1, 2, true); Piece i = Piece(3, 0.0, 0.0, 0.0); i.Set(0, 0, true); i.Set(0, 1, true); i.Set(0, 2, true); Piece lr = Piece(3, 0.0, 0.0, 0.0); lr.Set(0, 0, true); lr.Set(0, 1, true); lr.Set(0, 2, true); lr.Set(1, 2, true); Piece ll = Piece(3, 0.0, 0.0, 0.0); ll.Set(1, 0, true); ll.Set(1, 1, true); ll.Set(1, 2, true); ll.Set(0, 2, true); Piece sqr = Piece(2, 0.0, 0.0, 0.0); sqr.Set(0, 0, true); sqr.Set(0, 1, true); sqr.Set(1, 0, true); sqr.Set(1, 1, true); Piece zl = Piece(3, 0.0, 0.0, 0.0); zl.Set(0, 0, true); zl.Set(0, 1, true); zl.Set(1, 1, true); zl.Set(2, 1, true); Piece zr = Piece(3, 0.0, 0.0, 0.0); zr.Set(2, 0, true); zr.Set(1, 0, true); zr.Set(1, 1, true); zr.Set(0, 1, true); pieces.clear(); pieces.push_back(sqr); pieces.push_back(t); pieces.push_back(i); pieces.push_back(lr); pieces.push_back(ll); pieces.push_back(zl); pieces.push_back(zr); }
void NextPiece::Draw(Bitmap &canvas) { if (piece) { canvas.Clear(skin->c_face); piece->Draw(canvas, 0, 0); } }
// Taking turns void test_piece_turntaking(ErrorContext &ec, unsigned int numRuns) { bool pass; // Run at least once!! assert(numRuns > 0); ec.DESC("--- Test - Piece - Taking turns ---"); for (int run = 0; run < numRuns; run++) { ec.DESC("3x3, manual, simple agent going for a resource"); { Game g(3, 3); // populate the game environment g.addFood(0, 0); g.addAdvantage(2, 1); // create a Simple, passing it the game and a position Simple s(g, Position(1, 1), Game::STARTING_AGENT_ENERGY); // create an upcast pointer to the agent for polymorphic turn taking Piece *piece = &s; // get the Surroundings for the agent's position from the Game Surroundings surr = g.getSurroundings(Position(1, 1)); // call takeTurn on the Piece pointer to the agent ActionType action = piece->takeTurn(surr); // if there is a resource, it should ask to there // and so on... pass = (action == ActionType::NW) || (action == ActionType::S); if (! pass) std::cout << action << std::endl; ec.result(pass); } ec.DESC("3x3, manual, simple agent moving to an empty position"); { Game g(3, 3); // populate the game environment g.addStrategic(0, 1); g.addSimple(1, 1); // create a Simple, passing it the game and a position Simple s(g, Position(0, 2), Game::STARTING_AGENT_ENERGY); // create an upcast pointer to the agent for polymorphic turn taking Piece *piece = &s; // get the Surroundings for the agent's position from the Game Surroundings surr = g.getSurroundings(Position(0, 2)); // call takeTurn on the Piece pointer to the agent ActionType action = piece->takeTurn(surr); // if there is a resource, it should ask to there // and so on... pass = (action == ActionType::S); if (! pass) std::cout << action << std::endl; ec.result(pass); } ec.DESC("3x3, manual, simple agent staying in place, not attacking"); { Game g(3, 3); // populate the game environment g.addStrategic(0, 1); g.addSimple(1, 1); g.addSimple(1, 2); // create a Simple, passing it the game and a position Simple s(g, Position(0, 2), Game::STARTING_AGENT_ENERGY); // create an upcast pointer to the agent for polymorphic turn taking Piece *piece = &s; // get the Surroundings for the agent's position from the Game Surroundings surr = g.getSurroundings(Position(0, 2)); // call takeTurn on the Piece pointer to the agent ActionType action = piece->takeTurn(surr); // if there is a resource, it should ask to there // and so on... pass = (action == ActionType::STAY); if (! pass) std::cout << action << std::endl; ec.result(pass); } ec.DESC("3x3, manual, resources don't move"); { Game g(3, 3); Food f(g, Position(0, 0), Game::STARTING_RESOURCE_CAPACITY); Advantage a(g, Position(2, 1), Game::STARTING_RESOURCE_CAPACITY); // create an upcast pointer to the agent for polymorphic turn taking Piece *piece[2] = { &f, &a }; // get the Surroundings for a Piece's position Surroundings surr[2] = { g.getSurroundings(Position(0, 0)), g.getSurroundings(Position(2, 1)) }; // call takeTurn on the Piece pointer ActionType actions[2] = { piece[0]->takeTurn(surr[0]), piece[1]->takeTurn(surr[1]) }; pass = (actions[0] == ActionType::STAY) && (actions[1] == ActionType::STAY); ec.result(pass); } ec.DESC("3x3, manual, default strategic agent choosing an advantage"); { Game g(3, 3); // populate the game environment g.addFood(0, 0); g.addAdvantage(2, 1); // create a default Strategic, passing it the game and a position Strategic s(g, Position(1, 1), Game::STARTING_AGENT_ENERGY); // create an upcast pointer to the agent for polymorphic turn taking Piece *piece = &s; // get the Surroundings for the agent's position from the Game Surroundings surr = g.getSurroundings(Position(1, 1)); // call takeTurn on the Piece pointer to the agent ActionType action = piece->takeTurn(surr); // if there is a resource, it should ask to there // and so on... pass = (action == ActionType::S); if (! pass) std::cout << action << std::endl; ec.result(pass); } ec.DESC("3x3, manual, hemmed in default strategic challenging a simple"); { Game g(3, 3); // populate the game environment g.addSimple(0, 0); g.addSimple(0, 1); g.addSimple(0, 2); g.addStrategic(1, 0); g.addStrategic(1, 2); g.addSimple(2, 0); g.addSimple(2, 1); g.addSimple(2, 2); // create a default Strategic, passing it the game and a position Strategic s(g, Position(1, 1), Game::STARTING_AGENT_ENERGY); // create an upcast pointer to the agent for polymorphic turn taking Piece *piece = &s; // get the Surroundings for the agent's position from the Game Surroundings surr = g.getSurroundings(Position(1, 1)); // call takeTurn on the Piece pointer to the agent ActionType action = piece->takeTurn(surr); // if there is a resource, it should ask to there // and so on... pass = (action == ActionType::NW) || (action == ActionType::N) || (action == ActionType::NE) || (action == ActionType::SW) || (action == ActionType::S) || (action == ActionType::SE); if (! pass) std::cout << action << std::endl; ec.result(pass); } ec.DESC("3x3, manual, hemmed in default strategic staying put"); { Game g(3, 3); // populate the game environment g.addStrategic(0, 0); g.addStrategic(0, 1); g.addStrategic(0, 2); g.addStrategic(1, 0); g.addStrategic(1, 2); g.addStrategic(2, 0); g.addStrategic(2, 1); g.addStrategic(2, 2); // create a default Strategic, passing it the game and a position Strategic s(g, Position(1, 1), Game::STARTING_AGENT_ENERGY); // create an upcast pointer to the agent for polymorphic turn taking Piece *piece = &s; // get the Surroundings for the agent's position from the Game Surroundings surr = g.getSurroundings(Position(1, 1)); // call takeTurn on the Piece pointer to the agent ActionType action = piece->takeTurn(surr); // if there is a resource, it should ask to there // and so on... pass = (action == ActionType::STAY); if (! pass) std::cout << action << std::endl; ec.result(pass); } ec.DESC("3x3, manual, hemmed in default strategic fleeing"); { Game g(3, 3); // populate the game environment g.addStrategic(0, 0); g.addStrategic(0, 1); g.addStrategic(0, 2); g.addStrategic(1, 0); g.addStrategic(2, 0); g.addStrategic(2, 1); g.addStrategic(2, 2); // create a default Strategic, passing it the game and a position Strategic s(g, Position(1, 1), Game::STARTING_AGENT_ENERGY); // create an upcast pointer to the agent for polymorphic turn taking Piece *piece = &s; // get the Surroundings for the agent's position from the Game Surroundings surr = g.getSurroundings(Position(1, 1)); // call takeTurn on the Piece pointer to the agent ActionType action = piece->takeTurn(surr); // if there is a resource, it should ask to there // and so on... pass = (action == ActionType::E); if (! pass) std::cout << action << std::endl; ec.result(pass); } ec.DESC("3x3, manual, hemmed in aggressive strategic attacking"); { Game g(3, 3); // populate the game environment g.addStrategic(0, 0); g.addStrategic(0, 1); g.addStrategic(0, 2); g.addStrategic(1, 0); g.addStrategic(2, 0); g.addStrategic(2, 1); g.addStrategic(2, 2); // create an aggressive Strategic, passing it the game and a position Strategic s(g, Position(1, 1), Game::STARTING_AGENT_ENERGY, new AggressiveAgentStrategy(Game::STARTING_AGENT_ENERGY)); // create an upcast pointer to the agent for polymorphic turn taking Piece *piece = &s; // get the Surroundings for the agent's position from the Game Surroundings surr = g.getSurroundings(Position(1, 1)); // call takeTurn on the Piece pointer to the agent ActionType action = piece->takeTurn(surr); // if there is a resource, it should ask to there // and so on... pass = (action != ActionType::E); if (! pass) std::cout << action << std::endl; ec.result(pass); } ec.DESC("3x3, manual, strong aggressive strategic prefers to attack"); { Game g(3, 3); // populate the game environment g.addFood(0, 0); g.addAdvantage(0, 2); g.addStrategic(1, 0); g.addFood(2, 1); g.addSimple(2, 2); // create an aggressive Strategic, passing it the game and a position Strategic s(g, Position(1, 1), Game::STARTING_AGENT_ENERGY, new AggressiveAgentStrategy(Game::STARTING_AGENT_ENERGY)); // create an upcast pointer to the agent for polymorphic turn taking Piece *piece = &s; // get the Surroundings for the agent's position from the Game Surroundings surr = g.getSurroundings(Position(1, 1)); // call takeTurn on the Piece pointer to the agent ActionType action = piece->takeTurn(surr); // if there is a resource, it should ask to there // and so on... pass = (action == ActionType::W) || (action == ActionType::SE); if (! pass) std::cout << action << std::endl; ec.result(pass); } ec.DESC("3x3, manual, weak aggressive strategic goes for an advantage"); { Game g(3, 3); // populate the game environment g.addFood(0, 0); g.addAdvantage(0, 2); g.addStrategic(1, 0); g.addFood(2, 1); g.addSimple(2, 2); // create an aggressive Strategic, passing it the game and a position double energy = Game::STARTING_AGENT_ENERGY / 2; // weaken the agent Strategic s(g, Position(1, 1), energy, new AggressiveAgentStrategy(energy)); // create an upcast pointer to the agent for polymorphic turn taking Piece *piece = &s; // get the Surroundings for the agent's position from the Game Surroundings surr = g.getSurroundings(Position(1, 1)); // call takeTurn on the Piece pointer to the agent ActionType action = piece->takeTurn(surr); // if there is a resource, it should ask to there // and so on... pass = (action == ActionType::NE); if (! pass) std::cout << action << std::endl; ec.result(pass); } } }
/** @return The result of the action the player made. WRONG_COLOR, PIECE_SELECTED, PIECE_MOVED, INVALID_POSITION. */ ActionResult Player::performAction() { // Translate mouse coordinate to sqaure coordinates Position pos = mBoard->toGridPos(gInput->mousePosition()); ActionResult action; bool moved = false; // No piece is selected if(gInput->keyPressed(VK_LBUTTON) && mSelectedPiece == NULL) { Piece* piece = mBoard->getPieceAt(pos.x, pos.y); // A piece was pressed if(piece != NULL) { // The piece has another color than the players if(piece->getColor() != getColor()) { // Return wrong color msg action = ActionResult(WRONG_COLOR, pos); gSound->playEffect(ILLEGAL_SOUND); } else { mSelectedPiece = piece; mBoard->setSelected(pos.x, pos.y); // Return piece selected msg action = ActionResult(PIECE_SELECTED, pos); } } } // A piece is already selected else if(gInput->keyPressed(VK_LBUTTON) && mSelectedPiece != NULL) { Piece* piece = mBoard->getPieceAt(pos.x, pos.y); Position oldPos = mSelectedPiece->getPos(); // Was it a piece of the same color that was pressed? - Change the selected piece if(piece != NULL && piece->getColor() == getColor()) { mSelectedPiece = piece; action = ActionResult(PIECE_SELECTED, pos); } // Didn't select a piece of the same color as self else { // The piece can move to the pressed location if(mBoard->validMove(mSelectedPiece, pos.x, pos.y)) { // A piece was captured if(piece != NULL) { handleCapture(piece->getColor(), piece->getType()); mBoard->removePiece(piece); pieceCapturedSound(); } // Return piece moved msg action = ActionResult(PIECE_MOVED, pos, mSelectedPiece->getPos()); // Update the selected piece's position mSelectedPiece->setPos(pos.x, pos.y); mSelectedPiece->moved(); // Was it castling? if(mSelectedPiece->getType() == KING && abs(mSelectedPiece->getPos().x - oldPos.x) > 1) { handleCastling(mSelectedPiece); gSound->playEffect(CASTLE_SOUND); } else pieceMovedSound(); mSelectedPiece = NULL; } // Can't move to the pressed location else { // Return invalid position or checked msg if(mBoard->resultsInCheck(mSelectedPiece, pos.x, pos.y)) action = ActionResult(GETS_CHECKED, mBoard->getPiece(KING, mSelectedPiece->getColor())->getPos()); else { if(pos.x >= 0 && pos.x <= 7 && pos.y >= 0 && pos.y <= 7) action = ActionResult(INVALID_POSITION, pos); else action = ActionResult(PIECE_SELECTED, Position(-10, -10)); } gSound->playEffect(ILLEGAL_SOUND); mSelectedPiece = NULL; } } } // Return the action return action; }
SnakesAndLadders::SnakesAndLadders():Game(2,10,10), amountOfSystemItems(2) { // Set srand a more random die roll. srand(time(NULL)); // systemItems holds the snakes and ladders. this->systemItems = new Player[amountOfSystemItems]; // This is simply just used an a reference instead of trying to convert // Coords to square ids. this->squareRefs = new Square*[100]; // Setup the Players. const int playerTypes = 1; const int maxPlayerPieces = 1; vector<string> player1PieceTypes(playerTypes); vector<string> player2PieceTypes(maxPlayerPieces); // Set their character representations. player1PieceTypes[0] = FRED "◎"; player2PieceTypes[0] = FBLUE "◎"; players[0] = new SLPlayer(playerTypes,player1PieceTypes,maxPlayerPieces); players[1] = new SLPlayer(playerTypes,player2PieceTypes,maxPlayerPieces); // Setup the snakes and ladders. const int systemTypes = 2; const int maxSystemPieces = 16; vector <string> system1PieceTypes(systemTypes); vector <string> system2PieceTypes(systemTypes); // Set the start point snake/ladder character representations. system1PieceTypes[0] = FVIOLET "S"; system1PieceTypes[1] = FGREEN "L"; // Set the end point snake/ladder character representations. system2PieceTypes[0] = FORANGE "S"; system2PieceTypes[1] = FMAGENTA "L"; systemItems[0] = Player(systemTypes,system1PieceTypes,maxSystemPieces); systemItems[1] = Player(systemTypes,system2PieceTypes,maxSystemPieces); // Setup all the squares. int totalPlayers = amountOfPlayers + amountOfSystemItems; string start[] = {BBLACK,BGRAY}; string end = RESET; int identifier = 0; int counter = 100; for(int i=0; i<rows; i++) { for(int j=0; j<columns; j++) { grid[i][j] = Square(counter,start[identifier],end,totalPlayers, Coord(j,i)); squareRefs[counter-1] = &grid[i][j]; counter += (i % 2 == 0) ? -1 : 1; identifier = (identifier + 1)%2; } counter -= (i % 2 == 0) ? 9 : 11; identifier = (identifier + 1)%2; } // Add the players to the starting square. if(players[0]->hasRoomForPiece() && players[1]->hasRoomForPiece()) { Piece* player1Piece = new SrcPiece(*players[0],Coord(0,9)); Piece* player2Piece = new SrcPiece(*players[1],Coord(0,9)); squareRefs[0]->addPiece(0, players[0]->addPiece(player1Piece)); squareRefs[0]->addPiece(1, players[1]->addPiece(player2Piece)); } Coord snakes[maxSystemPieces/2][2] = { {squareToCoordinate(20),squareToCoordinate(17)}, {squareToCoordinate(33),squareToCoordinate(7)}, {squareToCoordinate(44),squareToCoordinate(22)}, {squareToCoordinate(52),squareToCoordinate(31)}, {squareToCoordinate(63),squareToCoordinate(54)}, {squareToCoordinate(85),squareToCoordinate(67)}, {squareToCoordinate(94),squareToCoordinate(71)}, {squareToCoordinate(99),squareToCoordinate(61)}, }; Coord ladders[maxSystemPieces/2][2] = { {squareToCoordinate(9),squareToCoordinate(29)}, {squareToCoordinate(15),squareToCoordinate(26)}, {squareToCoordinate(24),squareToCoordinate(46)}, {squareToCoordinate(40),squareToCoordinate(60)}, {squareToCoordinate(64),squareToCoordinate(76)}, {squareToCoordinate(69),squareToCoordinate(87)}, {squareToCoordinate(83),squareToCoordinate(98)}, {squareToCoordinate(48),squareToCoordinate(68)} }; Piece* source; Piece* destination; // Place all the snakes onto the board. for(int i=0; i<maxSystemPieces/amountOfSystemItems; i++) { if(systemItems[0].hasRoomForPiece() && systemItems[1].hasRoomForPiece()) { source = new SystemPiece(systemItems[0],snakes[i][0],snakes[i][1],i); destination = new IDPiece(systemItems[1],i); // Snake start point. grid[snakes[i][0].y][snakes[i][0].x].addPiece(2,*source); // Snake end point. grid[snakes[i][1].y][snakes[i][1].x].addPiece(3,*destination); } } // Places all the ladders onto the board. for(int i=0; i<maxSystemPieces/2; i++) { if(systemItems[0].hasRoomForPiece() && systemItems[1].hasRoomForPiece()) { source = new SystemPiece(systemItems[0],ladders[i][0],ladders[i][1],i); destination = new IDPiece(systemItems[1],i); // Sets the piece to use the ladder character representation. source->setType(1); destination->setType(1); // Ladder start point. grid[ladders[i][0].y][ladders[i][0].x].addPiece(2,*source); // Ladder end point. grid[ladders[i][1].y][ladders[i][1].x].addPiece(3,*destination); } } }
void GameMechanics::spawnNextPieceFromQueue() { Piece nextFromQueue = _pieceQueue.PushNewPiece(_pieceFactory.CreateRandomPiece(GameSettings::BlockSizeForQueue)); spawnSpecificPiece(nextFromQueue.GetPieceType()); }
SS3OPiece* CS3OParser::LoadPiece(S3DModel* model, SS3OPiece* parent, unsigned char* buf, int offset) { model->numPieces++; // retrieve piece data Piece* fp = (Piece*)&buf[offset]; fp->swap(); // Does it matter we mess with the original buffer here? Don't hope so. Vertex* vertexList = reinterpret_cast<Vertex*>(&buf[fp->vertices]); int* indexList = reinterpret_cast<int*>(&buf[fp->vertexTable]); int* childList = reinterpret_cast<int*>(&buf[fp->children]); // create piece SS3OPiece* piece = new SS3OPiece(); piece->offset.x = fp->xoffset; piece->offset.y = fp->yoffset; piece->offset.z = fp->zoffset; piece->primType = fp->primitiveType; piece->name = (char*) &buf[fp->name]; piece->parent = parent; // retrieve vertices piece->SetVertexCount(fp->numVertices); for (int a = 0; a < fp->numVertices; ++a) { Vertex* v = (vertexList++); v->swap(); SS3OVertex sv; sv.pos = float3(v->xpos, v->ypos, v->zpos); sv.normal = float3(v->xnormal, v->ynormal, v->znormal).SafeANormalize(); sv.texCoords[0] = float2(v->texu, v->texv); sv.texCoords[1] = float2(v->texu, v->texv); piece->SetVertex(a, sv); } // retrieve draw indices piece->SetIndexCount(fp->vertexTableSize); for (int a = 0; a < fp->vertexTableSize; ++a) { const int vertexDrawIdx = swabDWord(*(indexList++)); piece->SetIndex(a, vertexDrawIdx); } // post process the piece { piece->goffset = piece->offset + ((parent != NULL)? parent->goffset: ZeroVector); piece->Trianglize(); piece->SetVertexTangents(); piece->SetMinMaxExtends(); model->mins = float3::min(piece->goffset + piece->mins, model->mins); model->maxs = float3::max(piece->goffset + piece->maxs, model->maxs); piece->SetCollisionVolume(CollisionVolume("box", piece->maxs - piece->mins, (piece->maxs + piece->mins) * 0.5f)); } // load children pieces piece->children.reserve(fp->numchildren); for (int a = 0; a < fp->numchildren; ++a) { int childOffset = swabDWord(*(childList++)); SS3OPiece* childPiece = LoadPiece(model, piece, buf, childOffset); piece->children.push_back(childPiece); } return piece; }
void GameGUI::rafraichir(const SujetDObservation *sdo) { game = (Game *) sdo; Board board= game->getBoard(); QPixmap * pix; QLabel *tmp= new QLabel(); Piece *p; auto size=game->getBoard().getSize()*2-1; for (unsigned i = 0; i < size; i++) { icon[i].resize(size); for (unsigned j = 0; j < size; j++) { p= board.getBoard()[i][j]; if(i%2==0 && j%2==0){ if(p->isEmpty()){ std::string im = "images/Empty.png"; tmp= new QLabel(); pix =new QPixmap(im.c_str()); tmp->setPixmap(*pix); icon[i][j]=tmp; }else{ string im; switch(p->getSide()){ case Side::NORTH: im = "images/pawn1.png"; break; case Side::SOUTH: im = "images/pawn2.png"; break; case Side::WEST: im = "images/pawn3.png"; break; case Side::EST: im = "images/pawn4.png"; break; } tmp= new QLabel(); pix =new QPixmap(im.c_str()); tmp->setPixmap(*pix); icon[i][j] =tmp; } }else{ if(p->isEmpty()){ std::string im = "images/EmptyWall.png"; tmp=new QLabel(); pix =new QPixmap(im.c_str()); tmp->setPixmap(*pix); icon[i][j]=tmp; }else{ string im; if(p->getAlignement()==Alignement::HORIZONTAL){ im = "images/wallH.png"; }else{ im = "images/WallV.png"; } tmp= new QLabel(); pix =new QPixmap(im.c_str()); tmp->setPixmap(*pix); icon[i][j] =tmp; } } lay->addWidget(icon[i][j], i, j); } } }