赞
踩
产生效果 | 评分 |
---|---|
连2 | 10 |
死3 | 30 |
活3 | 40 |
死4 | 60 |
活4 | 200 |
连5 | 20000 |
产生效果 | 评分 |
---|---|
连1 | 5 |
连2 | 10 |
死3 | 25 |
活3 | 50 |
死4 | 55 |
活4 | 300 |
连5 | 30000 |
void calcScore(ChessData* data);
#include <string>
void calcScore(ChessData* data){ if(!data) return; //统计玩家或AI连子 int personNum = 0; //玩家 int botNum = 0; //AI int emptyNum = 0; //各方向空白位数 //清空评分数组 memset(data->scoreMap, 0, sizeof(data->scoreMap)); for (int row = 0; row < BOARD_GRAD_SIZE; row++){ for(int col = 0; col < BOARD_GRAD_SIZE; col++){ //空白点计算 if(row >= 0 && col >= 0 && data->chessMap[row][col] == 0){ //遍历四个方向,然后分别计算正反四个方向 int directs[4][2] = {{1,0}, {1,1}, {0,1}, {-1,1}}; for(int k = 0; k < 4; k++){ int x = directs[k][0]; int y = directs[k][1]; //重置 personNum = 0; botNum = 0; emptyNum = 0; //对黑棋评分(正向) for(int i = 1; i <= 4; i++){ if(row + i * y >= 0 && row + i * y < BOARD_GRAD_SIZE && col + i * x >= 0 && col + i * x < BOARD_GRAD_SIZE && data->chessMap[row + i * y][col + i * x] == 1){ //玩家的子 personNum++; }else if(row + i * y >= 0 && row + i * y < BOARD_GRAD_SIZE && col + i * x >= 0 && col + i * x < BOARD_GRAD_SIZE && data->chessMap[row + i * y][col + i * x] == 0){ //空白位 emptyNum++; break; //遇到空白位置停止该方向搜索 }else{ break; //出边界或遇到白棋停止搜索 } } //对黑棋评分(反向) for(int i = 1; i<= 4; i++){ if(row - i * y >= 0 && row - i * y < BOARD_GRAD_SIZE && col - i * x >= 0 && col - i * x <BOARD_GRAD_SIZE && data->chessMap[row - i * y][col - i * x] == 1){ personNum++; }else if(row - i * y >= 0 && row - i * y <BOARD_GRAD_SIZE && col - i * x >= 0 && col - i * x < BOARD_GRAD_SIZE && data->chessMap[row -i * y][col - i * x] == 0){ emptyNum++; break; }else{ break; } } if(personNum == 1){ data->scoreMap[row][col] += 10; }else if(personNum == 2){ if(emptyNum == 1){ //死3 data->scoreMap[row][col] += 30; }else if(emptyNum == 2){ //活3 data->scoreMap[row][col] += 40; } }else if(personNum == 3){ if(empty == 1){ //死4 data->scoreMap[row][col] += 60; }else if (emptyNum == 2){ //活4 data->scoreMap[row][col] += 200; } }else if(personNum == 4){ data->scoreMap[row][col] += 20000; } emptyNum = 0; //清空 //对白棋评分(正向) for(int i = 1; i <= 4; i++){ if(row + i * y > 0 && row + i * y < BOARD_GRAD_SIZE && col + i * x > 0 && col + i * x < BOARD_GARD_SIZE && data->chessMap[row + i * y][col + i * x == -1]){ botNum++; }else if(row + i * y >0 && row + i * y < BOARD_GRAD_SIZE && col + i * x > 0 && col + i * x < BOARD_GRAD_SIZE && data->chessMap[row + i * y][col + i *x] == 0){ emptyNum++; break; }else{ break; } } //白棋评分(反向) for(int i = 1; i <= 4; i++){ if(row - i * y > 0 && row - i * y <BOARD_GRAD_SIZE && col - i * x > 0 && col - i * x < BOARD_GRAD_SIZE && data->chessMap[row - i * y][col -i * x] == -1){ botNum++; }else if (row - i * y >0 && row - i * y < BOARD_GRAD_SIZE && col - i * x > 0 && col - i * x < BOARD_GRAD_SIZE && data->chessMap[row - i * y][col - i * x] == 0){ emptyNum++; break; }else{ break; //出边界 } } if(botNum == 0){ //连1 data->scoreMap[row][col] += 5; }else if(botNum == 1){ //活2 data->scoreMap[row][col] += 10; }else if(botNum == 2){ if(emptyNum == 1){ //死3 data->scoreMap[row][col] += 25; }else if(emptyNum == 2){ //活3 data->scoreMap[row][col] += 50; } }else if(botNum == 3){ if(emptyNum == 1){ //死4 data->scoreMap[row][col] += 55; }else if(botNum == 2){ //活4 data->scoreMap[row][col] += 300; } }else if(botNum >= 4){ //活5 data->scoreMap[row][col] += 30000; } } } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。