gpt4 book ai didi

c - 如何在二维字符数组中搜索相同的字段?(在线游戏中的五个)在 C 中

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:26 25 4
gpt4 key购买 nike

我是 C 和编程的新手,我的家庭作业有问题。邹氏都知道游戏叫“五排”。我的工作是创建一个程序,在每次移动后评估玩家的位置。它必须说明哪条线最长(垂直、水平和对角线方向),并且还打印字段中有多少条这样的线。所以输入是位置,输出应该是这样的:玩家“X”有最长的线 3(3 次)。我有一个这样的字段:

#define MAX_LINE 26
#define MAX_COLUMN 26

char playField[MAX_LINE][MAX_COLUMN]

经过几步之后,我遇到了这样的情况:

playField[1][2]=='X'
playField[1][3]=='O'
playField[2][2]=='X'
playField[1][4]=='O'
playField[9][5]=='X'

那么如何搜索最长的行呢?这只是一个例子,字段可以有更多的元素。我的想法是用一些周期来做,但似乎不可能为我写出来。感谢帮助

最佳答案

int maxO = 0; //max length of O's
int maxX = 0; //max length of X's


char last; //the last player we checked (or nothing)
int lastCount;//the last count for the "last"

检查线

//lines along lines
/*
_ _ _ _ _ _ _
_ _ X X X _ _
_ _ _ _ _ _ _

for each line
for each column
check for longest X/0

*/
for(int i = 0; i < MAX_LINE; ++i) {
last = 0;
lastCount = 0;
for(int j = 0; j < MAX_COLUMN; ++j) {
assert(i >= 0 && i < MAX_LINE && j >= 0 && j < MAX_COLUMN);
//check if same player
if (last == playField[i][j]) {
//same player
lastCount += 1;
} else {
//not same player, set counted length for the right player
if (last == 'O' && maxO < lastCount) {
//it's player O
maxO = lastCount;
} else if (last == 'X' && maxX < lastCount) {
//it's player X
maxX = lastCount;
}
//update for next player (or nothing)
lastCount = 1;
last = playField[i][j];
}
}
if (last == 'O' && maxO < lastCount) {
//it's player O
maxO = lastCount;
} else if (last == 'X' && maxX < lastCount) {
//it's player X
maxX = lastCount;
}
}

检查列

//lines along columns
/*
_ _ _ X _ _ _
_ _ _ X _ _ _
_ _ _ X _ _ _

for each column
for each line
check for longest X/0

*/
for(int j = 0; j < MAX_COLUMN; ++j) {
last = 0;
lastCount = 0;
for(int i = 0; i < MAX_LINE; ++i) {
assert(i >= 0 && i < MAX_LINE && j >= 0 && j < MAX_COLUMN);
if (last == playField[i][j]) {
lastCount += 1;
} else {
if (last == 'O' && maxO < lastCount) {
maxO = lastCount;
} else if (last == 'X' && maxX < lastCount) {
maxX = lastCount;
}
lastCount = 1;
last = playField[i][j];
}
}
if (last == 'O' && maxO < lastCount) {
//it's player O
maxO = lastCount;
} else if (last == 'X' && maxX < lastCount) {
//it's player X
maxX = lastCount;
}
}

检查对角线

//lines along diagonals
/*
_ _ _ X _ _ _
_ _ X _ _ _ _
_ X _ _ _ _ _

for each line + column
for each column
shift coordinates
0 1 2 3 4 5 6
_ _ _ X _ _ _
_ _ X _ _ _ _
_ X _ _ _ _ _
to
0 1 2 3 4 5 6 7 8
_ _ _ X _ _ _
_ _ X _ _ _ _
_ X _ _ _ _ _
check if shifted coordiantes are in range
check for longest X/0

*/
for(int i = 0; i < MAX_LINE + MAX_COLUMN - 1; ++i) {
last = 0;
lastCount = 0;
for(int j = 0; j < MAX_COLUMN; ++j) {
int iShift = i - j;
if (iShift >= 0 && iShift < MAX_LINE) {
assert(iShift >= 0 && iShift < MAX_LINE && j >= 0 && j < MAX_COLUMN);
if (last == playField[iShift][j]) {
lastCount += 1;
} else {
if (last == 'O' && maxO < lastCount) {
maxO = lastCount;

} else if (last == 'X' && maxX < lastCount) {
maxX = lastCount;
}
lastCount = 1;
last = playField[iShift][j];
}
}
}
if (last == 'O' && maxO < lastCount) {
//it's player O
maxO = lastCount;
} else if (last == 'X' && maxX < lastCount) {
//it's player X
maxX = lastCount;
}
}

检查对角线 2

//lines along diagonals 2
/*
_ _ _ X _ _ _
_ _ _ _ X _ _
_ _ _ _ _ X _

for each line + column
for each column
shift coordinates
0 1 2 3 4 5 6
_ _ _ X _ _ _
_ _ _ _ X _ _
_ _ _ _ _ X _

to
-2-1 0 1 2 3 4 5 6
_ _ _ X _ _ _
_ _ _ _ X _ _
_ _ _ _ _ X _

check if shifted coordiantes are in range
check for longest X/0

*/
for(int i = -MAX_COLUMN + 1; i < MAX_LINE; ++i) {
last = 0;
lastCount = 0;
for(int j = 0; j < MAX_COLUMN; ++j) {
int iShift = i + j;
if (iShift >= 0 && iShift < MAX_LINE) {
assert(iShift >= 0 && iShift < MAX_LINE && j >= 0 && j < MAX_COLUMN);
if (last == playField[iShift][j]) {
lastCount += 1;
} else {
if (last == 'O' && maxO < lastCount) {
maxO = lastCount;
} else if (last == 'X' && maxX < lastCount) {
maxX = lastCount;
}
lastCount = 1;
last = playField[iShift][j];
}
}
}
if (last == 'O' && maxO < lastCount) {
//it's player O
maxO = lastCount;
} else if (last == 'X' && maxX < lastCount) {
//it's player X
maxX = lastCount;
}
}

打印结果

//print result
printf("Player X longest line %d\n", maxX);
printf("Player O longest line %d\n", maxO);

关于c - 如何在二维字符数组中搜索相同的字段?(在线游戏中的五个)在 C 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27064768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com