void getHumanMove( int player, unsigned int *board ) { int selectionx, selectiony; /* Get human move */ while (1) { selectionx = selectiony = -1; printf("Select a move for Player %d, type row column: ", player); scanf("%d", &selectionx); scanf("%d", &selectiony); printf("\n"); if ((selectionx >= 0) && (selectionx < MAX_BOARD_SIZE) && (selectiony >= 0) && (selectiony < MAX_BOARD_SIZE)) { if (moves && hasNeighbor(selectionx, selectiony, board) == 0) printf("cell has no neighbors -- choose again.\n"); else if (getCell(selectionx, selectiony, board)) printf("cell taken -- choose again.\n"); else break; } else { printf("bad input -- choose again.\n"); getchar(); } } putCell( player, selectionx, selectiony, board ); return; }
/** * Every node i sends to every neighbor a STATE message * that contains its current state value xi from time to time. * Each node also, from time to time, executes the update * rule (first equation in (13)) with the information it has * about other state values. */ void consensus_MR1P1(consensus_t *consensus) { float deltaSum = 0; int i; for(i=0;i<getNumberOfNeighborChannels();i++) { if(hasNeighbor(i)==1) { deltaSum += consensus->delta[i]; } } deltaSum+= consensus->z-consensus->x; consensus->x = consensus->x + consensus->gamma * deltaSum; }
/** * On link ij, if delta_ij is the sender, it executes the update rule * (second equation in (13)) from time to time. Whenever * delta_ij executes the update rule, it also sends to its receiver * delta_ji a REPLY message that contains the value of the * change it has made in the value of delta_ij. delta_ij will not * execute the update rule again until the TCP ACK of * this REPLY message comes back. */ void consensus_MR2(consensus_t *consensus) { int i; for(i=0;i<getNumberOfNeighborChannels();i++) { if(hasNeighbor(i)==1) { float diff = consensus->phi*(consensus->xNeighbors[i]-consensus->x); //problem: what if more than one neighbor per channel? consensus->delta[i] += diff; consensusMsg.msgType = CONSENSUS_MESSAGE; consensusMsg.from_label = consensus->label; consensusMsg.reply = 1; consensusMsg.value = diff; int success = sendMessageToNeighbor((char*)&consensusMsg, sizeof(consensusMsg), i); //should wait for reply (but I will not?) if(success!=1) { #ifdef PRINTF ase_printf("Warning: #1 in ConsensusAverage\n"); //the message was not send #endif } else { msgSend++; } } } }
int evaluateMaxMove( unsigned int *board, int depth ) { int value, x, y; int max = MIN_INFINITY-1; int scores1[MAX_BOARD_SIZE][MAX_BOARD_SIZE]; int scores2[MAX_BOARD_SIZE][MAX_BOARD_SIZE]; // printf("DEPTH=%d\n", depth); boards_checked++; /* for (i = 0; i < depth; i++) printf(" "); printf("%d min %d %d\n", depth, last_move_x, last_move_y); */ /* Player 1 (min) just made a move, so we evaluate that move here */ if (checkPlayerWin(X_PLAYER, board)) return MIN_INFINITY; /* If Player 2 has a winning position, take it */ for (x = 0; x < MAX_BOARD_SIZE; x++) for (y = 0; y < MAX_BOARD_SIZE; y++) { if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) { scores1[x][y] = countScore( O_PLAYER, x, y, board, 4); if (scores1[x][y] == MAX_INFINITY) { if (depth == 0) { computer_move_x = x; computer_move_y = y; } return MAX_INFINITY; } } } /* If Player 1 has a winning position, block it */ for (x = 0; x < MAX_BOARD_SIZE; x++) for (y = 0; y < MAX_BOARD_SIZE; y++) if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) { scores2[x][y] = countScore( X_PLAYER, x, y, board, 4); if (scores2[x][y] == MAX_INFINITY) { putCell( O_PLAYER, x, y, board ); if (depth == 0) { computer_move_x = x; computer_move_y = y; return 0; } max = evaluateMinMove( board, depth+1 ); resetCell( x, y, board); return max; } } /* Cutoff checking */ if (depth >= CUTOFF) { // printf("depth %d scores\n", depth); for (x = 0; x < MAX_BOARD_SIZE; x++) for (y = 0; y < MAX_BOARD_SIZE; y++) if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) { value = (scores2[x][y])+(scores1[x][y]<<1); // printf("max (%d %d) %d %d %d\n", x, y, value, scores1[x][y], scores2[x][y]); if (value > max) { max = value; } } return max; } for (x = 0; x < MAX_BOARD_SIZE; x++) for (y = 0; y < MAX_BOARD_SIZE; y++) if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) { putCell( O_PLAYER, x, y, board ); value = evaluateMinMove( board, depth+1 ); resetCell( x, y, board); // printf("depth=%d value = %d for %d %d\n", depth, value, x, y); if (value > max) { max = value; if (depth == 0) { computer_move_x = x; computer_move_y = y; } if (max == MAX_INFINITY) return max; } } /* No move is possible -- draw */ if (max == MIN_INFINITY-1) { return DRAW; } return max; }
int evaluateMinMove( unsigned int *board, int depth ) { int value, x, y; int min = MAX_INFINITY+1; int scores1[MAX_BOARD_SIZE][MAX_BOARD_SIZE]; int scores2[MAX_BOARD_SIZE][MAX_BOARD_SIZE]; // printf("DEPTH=%d\n", depth); boards_checked++; /* for (i = 0; i < depth; i++) printf(" "); printf("%d max %d %d\n", depth, last_move_x, last_move_y); */ /* Player 2 (max) just made a move, so we evaluate that move here */ if (checkPlayerWin(O_PLAYER, board)) return MAX_INFINITY; /* If Player 1 has a winning position, take it */ for (x = 0; x < MAX_BOARD_SIZE; x++) for (y = 0; y < MAX_BOARD_SIZE; y++) if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) { scores1[x][y] = countScore( X_PLAYER, x, y, board, 4); if (scores1[x][y] == MAX_INFINITY) return MIN_INFINITY; } /* If Player 2 has a winning position, block it */ for (x = 0; x < MAX_BOARD_SIZE; x++) for (y = 0; y < MAX_BOARD_SIZE; y++) if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) { scores2[x][y] = countScore( O_PLAYER, x, y, board, 4); if (scores2[x][y] == MAX_INFINITY) { putCell( X_PLAYER, x, y, board ); min = evaluateMaxMove( board, depth+1 ); resetCell( x, y, board); return min; } } /* Cutoff checking */ if (depth >= CUTOFF) { // printf("depth %d scores\n", depth); for (x = 0; x < MAX_BOARD_SIZE; x++) for (y = 0; y < MAX_BOARD_SIZE; y++) if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) { // the value of position (x, y) is the sum of its score when the opponent is placed // at that position plus the twice of the score when itself is placed at that position. value = (scores2[x][y])+(scores1[x][y]<<1); // printf("min (%d %d) %d %d %d\n", x, y, value, scores1[x][y], scores2[x][y]); // the value is negated for min-move value = -value; if (value < min) { min = value; } } return min; } for (x = 0; x < MAX_BOARD_SIZE; x++) for (y = 0; y < MAX_BOARD_SIZE; y++) if (getCell(x, y, board) == EMPTY && hasNeighbor(x, y, board)) { putCell( X_PLAYER, x, y, board ); value = evaluateMaxMove( board, depth+1 ); resetCell(x, y, board); // printf("depth=%d value = %d for %d %d\n", depth, value, x, y); if (value < min) { min = value; if (min == MIN_INFINITY) return min; } } /* No move is possible -- draw */ if (min == MAX_INFINITY+1) { return DRAW; } return min; }