main () { char gameBoard[4][9]; genBoard(gameBoard); int playing = 1; printBoard(gameBoard); while (playing) { printf("White's Turn\n\n"); placePiece(gameBoard, WHITE); printBoard(gameBoard); if(playing) { printf("White's Turn\n\n"); rotateSquare(gameBoard); printBoard(gameBoard); if (playing) { printf("Black's Turn\n\n"); placePiece(gameBoard, BLACK); printBoard(gameBoard); if(playing) { printf("Black's Turn\n\n"); rotateSquare(gameBoard); printBoard(gameBoard); } } } } return 0; }
int Game::tick() { if(stopped_) { return -1; } removePiece(piece_, px_, py_); int ny = py_ - 1; if(!doesPieceFit(piece_, px_, ny)) { // Must finish off with this piece placePiece(piece_, px_, py_); if(py_ >= board_height_) { // you lose. stopped_ = true; return -1; } else { int rm = collapse(); generateNewPiece(); return rm; } } else { placePiece(piece_, px_, ny); py_ = ny; return 0; } }
int Game::tick() { if(stopped_) { return -1; } removePiece(piece_, px_, py_); int ny = py_ - 1; if(!doesPieceFit(piece_, px_, ny)) { // Must finish off with this piece placePiece(piece_, px_, py_); if(py_ >= board_height_) { // you lose. stopped_ = true; return -1; } else { int rm = collapse(); int level = 1 + linesCleared_ / 10; switch (rm) { case 0: score_ += 10 * level; break; case 1: score_ += rm * 100 * level; break; case 2: score_ += rm * 300 * level; break; case 3: score_ += rm * 500 * level; break; case 4: score_ += rm * 800 * level; break; } linesCleared_ += rm; generateNewPiece(); return rm; } } else { placePiece(piece_, px_, ny); py_ = ny; return 0; } }
bool Game::rotateCCW() { removePiece(piece_, px_, py_); Piece npiece = piece_.rotateCCW(); if(doesPieceFit(npiece, px_, py_)) { placePiece(npiece, px_, py_); piece_ = npiece; return true; } else { placePiece(piece_, px_, py_); return false; } }
bool Game::moveRight() { int nx = px_ + 1; removePiece(piece_, px_, py_); if(doesPieceFit(piece_, nx, py_)) { placePiece(piece_, nx, py_); px_ = nx; return true; } else { placePiece(piece_, px_, py_); return false; } }
void solveCross(int row, int col) { if (!testPiece(row, col, 0)) { return; } placePiece(row, col, 0); if (!prune()) { const int rowMax = 8 - pieceMin[lastPiece + 1]; for (int orient = 0; orient < orientMax[lastPiece + 1]; ++orient) { if (pieceRow[0] == pieceCol[0] && pieceRow[1] == pieceCol[1] && 0 < orient) { // Special case: if symmetric about diagonal, use only one line orientation. break; } for (int row = 0; row <= rowMax; ++row) { for (int col = 0; col <= rowMax; ++col) { solvePiece(row, col, orient); } } } } removePiece(); }
void RockPaperScissors::setPosition(int lineNumber, const string& line, int playerNumber, Status& currentStatus) { Piece p; //split string by white spaces istringstream buf(line); istream_iterator<string> beg(buf), end; vector<string> tokens(beg, end); if (isPositionFormatCorrect(tokens)) { p = getPieceFromVector(tokens); } //illegal line else { cout << "Player " << playerNumber << " has a faulty format in line " << lineNumber + 1 << " : " << endl \ << line << endl \ << "Correct format is:" << endl \ << "<PIECE_CHAR> <X> <Y> or J <X> <Y> <PIECE_CHAR>" << endl; currentStatus.setStatus(playerNumber, PossibleStatus::input_File_Error, lineNumber + 1, line); return; } int row = stoi(tokens[1]) - 1; int column = stoi(tokens[2]) - 1; if (!placePiece(playerNumber, p, row, column, 0, 0, lineNumber, line, currentStatus)) return; }
void waitForParentMove() { //Get move from reading int columnRead = readTurn(receiveID); //Make this move on child board placePiece(columnRead, PARENT_PIECE); //Child does not check for winning }
void Game::dropPiece(int side) { int ny = py_ - 1; Piece temp = piece_; temp.removeHalf((side + 1)%2); removePiece(piece_, px_, py_); placePiece(temp, px_, py_); while(true) { if(get(ny-2, px_ + 1 + (side + 1)%2) != -1) { break; } --ny; } piece_.removeHalf(side); ++ny; placePiece(piece_, px_, ny); }
void Game::generateNewPiece() { piece_ = PIECES[ rand() % 8 ]; int xleft = (board_width_-3) / 2; px_ = xleft; py_ = board_height_ + 3 - piece_.getBottomMargin(); placePiece(piece_, px_, py_); }
bool Game::moveLeft() { // Most of the piece movement methods work like this: // 1. remove the piece from the board. // 2. does the piece fit in its new configuration? // 3a. if yes, add it to the board in its new configuration. // 3b. if no, put it back where it was. // Simple and sort of silly, but satisfactory. int nx = px_ - 1; removePiece(piece_, px_, py_); if(doesPieceFit(piece_, nx, py_)) { placePiece(piece_, nx, py_); px_ = nx; return true; } else { placePiece(piece_, px_, py_); return false; } }
void solveSquare(int row, int col) { placePiece(row, col, 0); if (row == 3 && col == 3) { // Square in centre -> cross in 1/8. for (int row = 0; row <= 2; ++row) { for (int col = 0; col <= row; ++col) { solveCross(row, col); } } } else if (row == col) { // Square on diagonal -> cross in 1/2. for (int row = 0; row <= 5; ++row) { for (int col = 0; col <= row; ++col) { solveCross(row, col); } } } else if (row == 3) { // Square on mid line -> cross in 1/2. for (int row = 0; row <= 2; ++row) { for (int col = 0; col <= 5; ++col) { solveCross(row, col); } } } else { // Square in interior -> cross anywhere. for (int row = 0; row <= 5; ++row) { for (int col = 0; col <= 5; ++col) { solveCross(row, col); } } } removePiece(); }
void game(){ struct point pos; char state,c; printBoard(); do{ do{ getInput(&pos); }while(!canPlace(pos)); c = turns%2==0?'X':'O'; placePiece(c, pos); printBoard(); }while((state=hasWon())==NULL); endGame(state); }
//Dumb play - a random move int dumbPlay(int pieceType) { int randomColumn; randomColumn = rand()%GRID_N;//0 to GRID_N -1 number //End if board is full if(boardFull()) { if(isParent) { gameOver(EMPTY_SPACE); return -1; } else { return -1; } } while(columnFull(randomColumn)) { //That column is full //Generate another random one //Reseed //srand(time(NULL)); randomColumn = rand()%GRID_N;//0 to GRID_N -1 number } //Got a column that is not full //Place piece there placePiece(randomColumn,pieceType); //Check that we did not just randomly win if(getWinner(pieceType) == pieceType) { if(isParent) { //Call game over function gameOver(pieceType); } else { return randomColumn; } } return randomColumn; }
void gameData::reverseMove() { if(selectedMoveNode->moveType == placing) { takeToken(selectedMoveNode->xCoord, selectedMoveNode->yCoord, selectedMoveNode->layNum, FALSE); } else if(selectedMoveNode->moveType == taking) { placePiece(selectedMoveNode->xCoord, selectedMoveNode->yCoord, selectedMoveNode->layNum, selectedMoveNode->moveColour, FALSE); } else if(selectedMoveNode->moveType == moving) { if(selectedMoveNode->movedCoord == x) moveToken(selectedMoveNode->xCoord, selectedMoveNode->yCoord, selectedMoveNode->layNum, selectedMoveNode->xCoord - selectedMoveNode->change, selectedMoveNode->yCoord, selectedMoveNode->layNum); else if(selectedMoveNode->movedCoord == y) moveToken(selectedMoveNode->xCoord, selectedMoveNode->yCoord, selectedMoveNode->layNum, selectedMoveNode->xCoord, selectedMoveNode->yCoord - selectedMoveNode->change, selectedMoveNode->layNum); else if(selectedMoveNode->movedCoord == layer) moveToken(selectedMoveNode->xCoord, selectedMoveNode->yCoord, selectedMoveNode->layNum, selectedMoveNode->xCoord, selectedMoveNode->yCoord, selectedMoveNode->layNum - selectedMoveNode->change); } }
bool Game::drop() { removePiece(piece_, px_, py_); int ny = py_; while(true) { --ny; if(!doesPieceFit(piece_, px_, ny)) { break; } } ++ny; placePiece(piece_, px_, ny); if(ny == py_) { return false; } else { py_ = ny; return true; } }
void waitForChildMove(int childIndex) { //Get move from reading int columnRead = readTurn(receiveIDs[childIndex]); //Make this move on parent board //Set the child board as the active board activeBoard = &parentBoardsArray[childIndex]; placePiece(columnRead, CHILD_PIECE); //Check for full and win if(getWinner(CHILD_PIECE) == CHILD_PIECE) { //Child won gameOver(CHILD_PIECE); } else if(boardFull()) { gameOver(EMPTY_SPACE); } }
void getPiecePlacement(Board *board, int piece, Move *move) { Point positions[100]; int maxScore = 0; int bestPos = 0; int nPositions = getPiecePlacements(board, piece, positions); for (int i = 0; i < nPositions; i++) { Board next; int ret = placePiece(board, &next, piece, &positions[i]); if (ret < 0) continue; int score = countBoardScore(&next); if (score > maxScore) { maxScore = score; bestPos = i; } } move->pieceId = piece; move->position.x = positions[bestPos].x; move->position.y = positions[bestPos].y; move->score = maxScore; }
void solvePiece(int row, int col, int orient) { if (!testPiece(row, col, orient)) { return; } placePiece(row, col, orient); if (lastPiece == 12) { if(isChess()){ ++solutionCount; printSolution(); printBoard(); } } else { if (!prune()) { const int rowMax = 8 - pieceMin[lastPiece + 1]; for (int orient = 0; orient < orientMax[lastPiece + 1]; ++orient) { for (int row = 0; row <= rowMax; ++row) { for (int col = 0; col <= rowMax; ++col) { solvePiece(row, col, orient); } } } } } removePiece(); }
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); } } }
int Game::tick() { if(stopped_) { return -1; } int returnVal; int level = linesCleared_/100; if (level > 12) level = 12; removePiece(piece_, px_, py_); markBlocksForClearing(); returnVal = collapse(); moveClearBar(); if (counter < COUNTER_SPACE - level) { counter++; placePiece(piece_, px_, py_); return returnVal; } if (py_ == board_height_ + 2 && atTheTop < 16) { atTheTop++; placePiece(piece_, px_, py_); return returnVal; } atTheTop = 0; counter = 0; int ny = py_ - 1; if(!doesPieceFit(piece_, px_, ny)) { // Must finish off with this piece placePiece(piece_, px_, py_); if(py_ >= board_height_ + 1) { // you lose. stopped_ = true; return -1; } else { // break piece and keep moving down if need be // The right side can drop more if(get(ny-2, px_+1) != -1 && get(ny-2, px_+2) == -1) { dropPiece(0); counter = COUNTER_SPACE; } else if(get(ny-2, px_+1) == -1 && get(ny-2, px_+2) != -1) { dropPiece(1); counter = COUNTER_SPACE; } generateNewPiece(); return returnVal; } } else { placePiece(piece_, px_, ny); sy_ = py_; py_ = ny; return returnVal; } }
int executeMove(Board *board, Board *nextBoard, Move *move) { return placePiece(board, nextBoard, move->pieceId, &(move->position)); }
bool gameData::gameFunction(unsigned int arrayXCoord, unsigned int arrayYCoord, unsigned int arrayLayNum) { outerWindow->messageBox2->clear(); outerWindow->messageBox3->clear(); if(saveData.saveStatus != false) saveData.saveStatus = false; bool movedBool = false; if((gameStatus == taking) && (board[arrayLayNum][arrayXCoord][arrayYCoord].mlinStatus < 1)) { if(takeToken(arrayXCoord, arrayYCoord, arrayLayNum)) { movedBool = true; } } if(gameStatus == placing) { if(valPos(arrayXCoord, arrayYCoord, arrayLayNum) == false) { return false; } else { if(placePiece(arrayXCoord, arrayYCoord, arrayLayNum, currentTurn)) { if(checkForNewMlin(arrayXCoord, arrayYCoord, arrayLayNum) < 1) { movedBool = true; appendMoveList(arrayLayNum, arrayXCoord, arrayYCoord, placing, false); } else appendMoveList(arrayLayNum, arrayXCoord, arrayYCoord, placing, true); } } } else if(gameStatus == moving) { if(moveSelect(arrayXCoord, arrayYCoord, arrayLayNum, currentTurn)) { if(checkForNewMlin(arrayXCoord, arrayYCoord, arrayLayNum) < 1) { movedBool = true; moveNode * tempNode = new moveNode(arrayLayNum, arrayXCoord, arrayYCoord, selectedPosition.arrayLayNum, selectedPosition.arrayXCoord, selectedPosition.arrayYCoord, currentMoveNode, currentTurn, false); currentMoveNode = tempNode; selectedMoveNode = currentMoveNode; } else { moveNode * tempNode = new moveNode(arrayLayNum, arrayXCoord, arrayYCoord, selectedPosition.arrayLayNum, selectedPosition.arrayXCoord, selectedPosition.arrayYCoord, currentMoveNode, currentTurn, true); currentMoveNode = tempNode; selectedMoveNode = currentMoveNode; } } } if((gameStatus == taking) && (movedBool == true)) { // If it should be "moving" instead, it'll change to that in changeTurns() gameStatus = placing; } if(movedBool == true) { changeTurns(); } outerWindow->setInterfaceWidgets(); return true; }
//Smart play - intelligently try to win or block child from winning //Return the colummn that was played int smartPlay(int pieceType) { //Trying placing piece in every column, checking for win, remove if not a win int i; for(i=0; i< GRID_N; i++) { if(columnFull(i)==0) { //Column is not full //Place peice placePiece(i,pieceType); //Check for win only if parent if(getWinner(pieceType) == pieceType) { //If win, move is done, game is over //Call game over function if(isParent) { gameOver(pieceType); } else { return i; } } else { //If not win, remove peice - go to next iteration removePiece(i); } } } //Trying placing piece in every column, checking for opponent win - block this by placing own piece there int opponentType; if(pieceType == PARENT_PIECE) { opponentType = CHILD_PIECE; } else if(pieceType == CHILD_PIECE) { opponentType = PARENT_PIECE; } else { printf("Sweet jesus! What kind of piece is that!?\n"); } for(i=0; i< GRID_N; i++) { if(columnFull(i)==0) { //Column is not full //Place opponent peice placePiece(i,opponentType); //Check for win by opponent if(getWinner(opponentType) == opponentType) { //If win, block this move by //Remove opponent peice removePiece(i); //Place your peice placePiece(i,pieceType); return i; } else { //If not win, remove opponent peice - go to next iteration removePiece(i); } } } //At this point you could not win and could not block opponent from winning //Do a random dumb move return dumbPlay(pieceType); }