//***************************************************************************** // name: move * // description: finds a location to place a piece for the current move of * // Othello given the board. * // return type: void * // pre: the board information is valid and reflects the current state of the * // game. * // post: the move reference passed is changed to the decided piece placement * // returns: nothing * //***************************************************************************** void TeamNULL::move(Board b, pair<int,int> &move) { int color = getColor(); int opponent; if (color == WHITE) { opponent = BLACK; } else { opponent = WHITE; } vector<pair<int, int> > oppPieces; for (int i = 0; i < b.getRow(); i++) { for (int j = 0; j < b.getCol(); j++) { if (b[i][j] == opponent){ oppPieces.push_back(make_pair(i, j)); } } } set<Move> moves = findMoves(color, move, oppPieces, b); if (!moves.empty()) { move = moves.begin()->position; } }
static VALUE matching_find_moves_native(VALUE self, VALUE board, VALUE bonus, VALUE tiles, VALUE playerColor, VALUE lowerBound, VALUE upperBound) { MatchingContext context; char* boardArr = StringValuePtr(board); char* bonusArr = StringValuePtr(bonus); int row, col; for(row = 0; row < BOARD_SIZE; row++) { for(col = 0; col < BOARD_SIZE; col++) { int index = row * BOARD_SIZE + col; context.board[row][col] = boardArr[index]; context.bonus[row][col] = atoi_single_digit(bonusArr[index]); } } char* tilesArr = StringValuePtr(tiles); int i, j; for(i = 0; i < NUM_TILES; i++) { for(j = 0; j < TILE_SIZE; j++) { context.tiles[i][j] = tilesArr[i * TILE_SIZE + j]; } } context.playerColor = StringValuePtr(playerColor)[0]; context.lowerBound = NUM2INT(lowerBound); context.upperBound = NUM2INT(upperBound); context.midPoint = (context.upperBound + context.lowerBound) / 2; int len = NUM_TILES * 2; for(i = 0; i < len; i++) { context.bestMove[i] = -1; } context.bestScore = -1; context.bestScoreDiff = 2488320 * 3; findMoves(&context, 0); VALUE arrToReturn = rb_ary_new(); for(i = 0; i < len; i++) { rb_ary_push(arrToReturn, INT2NUM(context.bestMove[i])); } rb_ary_push(arrToReturn, INT2NUM(context.bestScore)); return arrToReturn; }
void test(const std::string &w) { StrMap moves; cout << "TESTING " << w << ":\n"; print(add, "ADD"); print(del, "DEL"); findMoves(add, del, moves); cout << "---\n"; print(add, "ADD"); print(del, "DEL"); print(moves, "MOVES"); add.clear(); del.clear(); resetAll(); cout << endl; }
int main(int argc, char** argv) { MatchingContext context; char* boardArr = argv[1]; char* bonusArr = argv[2]; int row, col; for(row = 0; row < BOARD_SIZE; row++) { for(col = 0; col < BOARD_SIZE; col++) { int index = row * BOARD_SIZE + col; context.board[row][col] = boardArr[index]; context.bonus[row][col] = atoi_single_digit(bonusArr[index]); } } char* tilesArr = argv[3]; int i, j; for(i = 0; i < NUM_TILES; i++) { for(j = 0; j < TILE_SIZE; j++) { context.tiles[i][j] = tilesArr[i * TILE_SIZE + j]; } } context.playerColor = argv[4][0]; context.lowerBound = parseInt(argv[5]); context.upperBound = parseInt(argv[6]); context.midPoint = (context.upperBound + context.lowerBound) / 2; int len = NUM_TILES * 2; for(i = 0; i < len; i++) { context.bestMove[i] = -1; } context.bestScore = -1; context.bestScoreDiff = 2488320 * 3; findMoves(&context, 0); for(i = 0; i < NUM_TILES; i++) { printf("%d %d :: %d %d %s\n", context.bestMove[2 * i], context.bestMove[2 * i + 1], context.bestMove[2 * i] / BOARD_SIZE, context.bestMove[2 * i] % BOARD_SIZE, dirToStr(context.bestMove[2 * i + 1])); } printf("Score : %d\n", context.bestScore); return 0; }
boolean findMoves(MatchingContext_t context, int currentTileIndex) { if(currentTileIndex >= NUM_TILES) { int moveScore = calculateScore(context); int scoreDiff = context->midPoint - moveScore; if(scoreDiff < 0) { scoreDiff *= -1; } boolean inRange = moveScore >= context->lowerBound && moveScore <= context->upperBound; if(inRange || scoreDiff < context->bestScoreDiff) { context->bestScoreDiff = scoreDiff; int i; int len = NUM_TILES * 2; for(i = 0; i < len; i++) { context->bestMove[i] = context->currentMove[i]; } context->bestScore = moveScore; } return inRange; } int row, col, dir; char *currentTile = context->tiles[currentTileIndex]; for(row = 0; row < BOARD_SIZE; row++) { for(col = 0; col < BOARD_SIZE; col++) { for(dir = 0; dir < 4; dir++) { if(checkAndPlaceTile(context, currentTile, row, col, dir)) { int index = 2 * currentTileIndex; context->currentMove[index] = row * BOARD_SIZE + col; context->currentMove[index + 1] = dir; if(findMoves(context, currentTileIndex + 1) == TRUE) { return TRUE; } removeTile(context, currentTile, row, col, dir); context->currentMove[index] = -1; context->currentMove[index + 1] = -1; } } } } return FALSE; }
int generateMoves(char board[12][12], int moveList[1000][6]) { int n = 0; //Check each square for a piece for(int i = 2; i < 10; i++) { for(int j = 2; j < 10; j++) { char selfPiece = board[i][j]; int current[2] = {i,j}; findMoves(selfPiece,current,board,moveList); }; printf("\n"); }; return 0; }
void decideMove(char board[26][26], int n, char colour, int *numberOfMoves){ int bestRow, bestCol, i, j; double highScore, currentScore; bool largeMove = true; int scoreBoard[26][26][2]; scoreTheBoard(board, scoreBoard, n, colour); int moves = findMoves(board, n, colour); if(*numberOfMoves < (n*n)/4) largeMove = false; highScore = currentScore = 0; for(i = 0; i < n && highScore >= 0; i++){ for(j = 0; j < n && highScore >= 0; j++){ if(scoreBoard[i][j][0] == 0) currentScore = 0; else { if(scoreBoard[i][j][0] == -1){ printf("Computer places %c at %c%c.\n", colour, i + 'a', j + 'a'); makeMove(board, n, i, j, colour); return; } else if(largeMove) currentScore = (scoreBoard[i][j][0])/2 + scoreBoard[i][j][1]; else currentScore = 1 / (double)scoreBoard[i][j][0] + (double)scoreBoard[i][j][1] / 7; } if(currentScore > highScore){ bestRow = i; bestCol = j; highScore = currentScore; } } } printf("Computer places %c at %c%c.\n", colour, bestRow + 'a', bestCol + 'a'); makeMove(board, n, bestRow, bestCol, colour); }