Пример #1
0
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;
}
Пример #2
0
static int
putReplace (const ContractionTableRule *rule, wchar_t character) {
  const BYTE *cells = (BYTE *)&rule->findrep[rule->findlen];
  int count = rule->replen;

  if ((prefs.capitalizationMode == CTB_CAP_DOT7) &&
      testCharacter(character, CTC_UpperCase)) {
    if (!putCell(*cells++ | BRL_DOT7)) return 0;
    if (!(count -= 1)) return 1;
  }

  return putCells(cells, count);
}
Пример #3
0
static int
writeCharacters (const wchar_t *inputLine, size_t inputLength, void *data) {
  const wchar_t *inputBuffer = inputLine;

  while (inputLength) {
    int inputCount = inputLength;
    int outputCount = outputWidth;

    if (!outputBuffer) {
      if (!(outputBuffer = malloc(outputWidth))) {
        noMemory(data);
        return 0;
      }
    }

    contractText(contractionTable,
                 inputBuffer, &inputCount,
                 outputBuffer, &outputCount,
                 NULL, CTB_NO_CURSOR);

    if ((inputCount < inputLength) && outputExtend) {
      free(outputBuffer);
      outputBuffer = NULL;
      outputWidth <<= 1;
    } else {
      {
        int index;

        for (index=0; index<outputCount; index+=1)
          if (!putCell(outputBuffer[index], data))
            return 0;
      }

      inputBuffer += inputCount;
      inputLength -= inputCount;

      if (inputLength)
        if (!putCharacter('\n', data))
          return 0;
    }
  }

  return 1;
}
Пример #4
0
void getComputerMove( unsigned int *board )
{
    int value = 0;
    boards_checked = 0;
    
    if (moves == 1) {
        if (last_move_x < MAX_BOARD_SIZE/2) 
            computer_move_x = last_move_x+1; else computer_move_x = last_move_x-1;
        if (last_move_y < MAX_BOARD_SIZE/2) 
            computer_move_y = last_move_y+1; else computer_move_y = last_move_y-1;
    } else
        value = evaluateMaxMove( board, 0 );
    
    printf("Player 2's move is %d %d (%d boards checked; score is %d)\n", 
           computer_move_x, computer_move_y, boards_checked, value);
    
    putCell( O_PLAYER, computer_move_x, computer_move_y, board );
    
    return;
}
Пример #5
0
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;
}
Пример #6
0
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;
}
Пример #7
0
/*!
	\brief Prints string with foreground and background styles.
	This function is overload of putCell(unsigned int x, unsigned int y, int w, QString str, QTermboxStyle fg, QTermboxStyle bg)
	uses clear foreground and background.
	\see setClearAttributes
	*/
void putCell(unsigned int x, unsigned int y, int w, QString str){
	putCell(x, y, w, str, clearFg, clearBg);
}
Пример #8
0
/*!
	This function is overload of putCell(unsigned int x, unsigned int y, int w, int h, QString str, QTermboxStyle fg, QTermboxStyle bg)
	with height = -1;
	*/
void putCell(unsigned int x, unsigned int y, int w, QString str, QTermboxStyle fg, QTermboxStyle bg){
	return putCell(x, y, w, -1, str, fg, bg);
}
Пример #9
0
/*!
	\brief Prints string with foreground and background styles.
	This function is overload of putCell(unsigned int x, unsigned int y, int w, QString str, QTermboxStyle fg, QTermboxStyle bg)
	with width = -1;
	*/
void putCell(unsigned int x, unsigned int y, QString str, QTermboxStyle fg, QTermboxStyle bg){
	Q_ASSERT_X(QTermboxCorePrivate::wasInitialized(), "putCell", "Termbox was not initialized.");

	putCell(x, y, -1, str, fg, bg);
}
Пример #10
0
/*!
	\brief Puts cell at specified position.
	Uses clear foreground and background.

	\param x - position from left side of the screen.
	\param y - position from top of the screen.
	\param ch - char of cell.
	*/
void putCell(unsigned int x, unsigned int y, QChar ch){
	putCell(x, y, ch, clearFg, clearBg);
}