程序突然出现cpu使用率峰值,看起来好像暂停了
我增加了两个功能:程序突然出现cpu使用率峰值,看起来好像暂停了,c,arrays,cpu-usage,C,Arrays,Cpu Usage,我增加了两个功能: int aiCheckScore(char arr[7][7], int inp, int height, Player player) int aiFindMostRelevant(char arr[7][7], Player player) 第一个为二维数组中的给定位置打分。分数等于如果我们在这个位置添加一个元素(不包括刚才添加的元素),我们在一行中会有多少相同类型的元素(垂直、水平或对角,并保持这三个元素中最好的一个) 第二个函数一次检查7个位置,找到得分最高的位置
int aiCheckScore(char arr[7][7], int inp, int height, Player player)
int aiFindMostRelevant(char arr[7][7], Player player)
第一个为二维数组中的给定位置打分。分数等于如果我们在这个位置添加一个元素(不包括刚才添加的元素),我们在一行中会有多少相同类型的元素(垂直、水平或对角,并保持这三个元素中最好的一个)
第二个函数一次检查7个位置,找到得分最高的位置并返回该位置。我尝试添加一点随机性,这样如果两个位置的分数相同,程序会在30%的时间内选择最后一个位置(这样就不会总是选择第一个位置)
没有我添加随机性的代码运行得很好。我一添加它,程序就在第12次调用第一个函数后立即停止。此外,该程序的CPU使用率突然飙升,从以前的5%以下保持在50%
我已经修改了代码,创建了随机性几次,但似乎没有任何改变。我甚至不能理解为什么会引起这样的问题
我的两项职能是:
int aiCheckScore(char arr[7][7], int inp, int height, Player player) {
int i, j;
int score[4] = { 0 };
//check horizontal score
for (i = inp - 1; i >= 0; i--) { //everything left
if (arr[height][i] != player.symb)
break;
++score[0];
}
for (i = inp + 1; i <= 6; i) { //everything right
if (arr[height][i] != player.symb)
break;
++score[0];
}
//check vertical score (we only have to check down)
for (i = height + 1; i <= 6; i++) {
if (arr[i][inp] != player.symb)
break;
++score[1];
}
//check diagonal (which starts left and above and goes down and right)
j = height - 1;
for (i = inp - 1; i >= 0 && j >= 0; i--) { //above and left
if (arr[j][i] != player.symb)
break;
++score[2];
--j;
}
j = height + 1;
for (i = inp + 1; i <= 6 && j <= 6; i++) { //down and right
if (arr[j][i] != player.symb)
break;
++score[2];
++j;
}
//check diagonal (which starts left and down and goes up and right)
j = height + 1;
for (i = inp - 1; i >= 0 && j <= 6; i--) { //down and left
if (arr[j][i] != player.symb)
break;
++score[3];
++j;
}
j = height - 1;
for (i = inp + 1; i <= 6 && j >= 0; i++) { //up and right
if (arr[j][i] != player.symb)
break;
++score[3];
--j;
}
int bestscore = score[0];
for (i = 0; i <= 3; i++) {
if (score[i] > bestscore)
bestscore = score[i];
}
printf("%d", bestscore);
return bestscore;
}
int aiFindMostRelevant(char arr[7][7], Player player) {
int i, height;
int score[7] = { 0 };
for (i = 0; i <= 6; i++) {
height = findHeight(arr, i);
if (height == -1) {//skip the columns that are full
score[i] = -100; //and give them a very bad score
}
else {
score[i] = aiCheckScore(arr, i, height, player);
}
}
int bestscore = score[0];
int bestposition = 0;
int num;
for (i = 0; i <= 6; i++) {
num = (int)rand() % 10;
if (score[i] == bestscore) { //if 2 positions have the same score
if (num >= 7) { //there is a 30% chance the ai will take the new one to add some variety
bestposition = i;
}
}
if (score[i] > bestscore) { //always take the position with the best score
bestscore = score[i];
bestposition = i;
}
}
return bestposition;
}
int-aiCheckScore(字符arr[7][7],int-inp,int-height,Player){
int i,j;
整数分数[4]={0};
//检查水平分数
对于(i=inp-1;i>=0;i--){//剩下的一切
如果(arr[height][i]!=player.symb)
打破
++得分[0];
}
对于(i=inp+1;i=0;i--){//左上方
如果(arr[j][i]!=player.symb)
打破
++得分[2];
--j;
}
j=高度+1;
对于(i=inp+1;i,其中一个循环中似乎没有增量。
更改:
for(i=inp+1;i导致此类问题的原因)通常可以通过调试器找到。我建议花一些时间学习调试器的基本用法。从长远来看,这将节省大量时间。