69,368
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
char chess[][6] = {
' ', 'X', 'X', ' ', 'O', ' ',
' ', 'X', 'X', 'O', 'O', 'O',
' ', 'O', 'X', 'X', 'O', ' ',
' ', 'O', 'O', 'O', ' ', ' ',
' ', 'O', 'O', 'X', 'O', 'X',
' ', 'O', 'X', 'X', 'O', ' ',
};
int victory(void)
{
int row[6][6] = {0}; //横向连子数
int col[6][6] = {0}; //竖向连子数
int rlean[6][6] = {0}; //右斜连子数
int llean[6][6] = {0}; //左斜连子数
int i, j;
for(i = 0; i < 6; i++){
for(j = 0; j < 6; j++){
row[i][j] = 1,
col[i][j] = 1,
rlean[i][j] = 1,
llean[i][j] = 1;
}
}
for(i = 0; i < 6; i++){
for(j = 0; j < 6; j++){
if(row[i][j] == 5 || col[i][j] == 5 || rlean[i][j] == 5 || llean[i][j] == 5){
if(chess[i][j] == 'X'){
printf("红方胜\n");
return 0;
}else if(chess[i][j] == 'O'){
printf("蓝方胜\n");
return 0;
}
}
if(j < 5 && chess[i][j] == chess[i][j + 1]){
row[i][j + 1] = row[i][j] + 1;
}
if(i < 5 && chess[i][j] == chess[i + 1][j]){
col[i + 1][j] = col[i][j] + 1;
}
if(i < 5 && j < 5 && chess[i][j] == chess[i + 1][j + 1]){
rlean[i + 1][j + 1] = rlean[i][j] + 1;
}
if(i < 5 && j > 1 && chess[i][j] == chess[i + 1][j - 1]){
llean[i + 1][j - 1] = llean[i][j] + 1;
}
}
}
printf("未分胜负,继续\n");
return -1;
}
int main(void)
{
victory();
return 0;
}
四方向,未分胜负全通过了#include <stdio.h>
char chess[][6] = {
' ', 'X', 'X', ' ', 'O', ' ',
' ', 'X', 'X', 'O', 'O', ' ',
' ', 'O', 'X', 'X', 'O', ' ',
' ', 'O', 'O', 'O', 'X', ' ',
' ', 'O', 'X', 'X', 'O', 'X',
' ', 'O', 'X', 'X', 'O', ' ',
};
int victory(void)
{
int row[6][6] = {0}; //横向连子数
int col[6][6] = {0}; //竖向连子数
int rlean[6][6] = {0}; //右斜连子数
int llean[6][6] = {0}; //左斜连子数
int i, j;
for(i = 0; i < 6; i++){
for(j = 0; j < 6; j++){
row[i][j] = 1,
col[i][j] = 1,
rlean[i][j] = 1,
llean[i][j] = 1;
}
}
for(i = 0; i < 6; i++){
for(j = 0; j < 6; j++){
if(row[i][j] == 5 || col[i][j] == 5 || rlean[i][j] == 5 || llean[i][j] == 5){
if(chess[i][j] == 'X'){
printf("红方胜\n");
return 0;
}else if(chess[i][j] == 'O'){
printf("蓝方胜\n");
return 0;
}
}
if(j < 5 && chess[i][j] == chess[i][j + 1]){
row[i][j + 1] = row[i][j] + 1;
}
if(i < 5 && chess[i][j] == chess[i + 1][j]){
col[i + 1][j] = col[i][j] + 1;
}
if(i < 5 && j < 5 && chess[i][j] == chess[i + 1][j + 1]){
rlean[i + 1][j + 1] = rlean[i][j] + 1;
}
if(i < 5 && j > 1 && chess[i][j] == chess[i + 1][j - 1]){
llean[i + 1][j - 1] = rlean[i][j] + 1;
}
}
}
printf("未分胜负,继续\n");
return -1;
}
int main(void)
{
victory();
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <time.h>
int victory(void)
{
int row[6][6] = {0}; //横向连子数
int col[6][6] = {0}; //竖向连子数
int rlean[6][6] = {0}; //右斜连子数
int llean[6][6] = {0}; //左斜连子数
int i, j;
for(i = 0; i < 6; i++){
for(j = 0; j < 6; i++){
row[i][j] = 1,
col[i][j] = 1,
rlean[i][j] = 1,
llean[i][j] = 1;
}
}
for(i = 0; i < 6; i++){
for(j = 0; j < 6; j++){
if(row[i][j] == 5 || col[i][j] == 5 || rlean[i][j] == 5 || llean[i][j] == 5){
if(chess[i][j] == 'X'){
printf("红方胜\n");
return 0;
}else if(chess[i][j] == 'O'){
printf("蓝方胜\n");
return 0;
}
}
if(j < 5 && chess[i][j] == chess[i][j + 1]){
row[i][j + 1] = row[i][j] + 1;
}
if(i < 5 && chess[i][j] == chess[i + 1][j]){
col[i + 1][j] = col[i][j] + 1;
}
if(i < 5 && j < 5 && chess[i][j] == chess[i + 1][j + 1]){
rlean[i + 1][j + 1] = rlean[i][j] + 1;
}
if(i < 5 && j > 1chess[i][j] == chess[i + 1][j - 1]){
llean[i + 1][j - 1] = rlean[i][j] + 1;
}
}
}
printf("未分胜负,继续\n");
return -1;
}
光标停在if语句不代表条件满足了,只是表示当前在执行判断动作而已。要确认是否条件被满足,还是要看当时的变量值,或者你把if语句这么写:如果能跟踪到光标进入大括号内部,就说明条件满足了。if (cond) { ... }
结构体还没学是吧 我的意思是二位数组后面加一个小数点是什么意思,没学过 row是横排,col竖列,rlean右斜,llean左斜 [quote=引用 21 楼 weixin_41581673 的回复:] 二维数组后面的.row什么的是什么
结构体还没学是吧 我的意思是二位数组后面加一个小数点是什么意思,没学过 row是横排,col竖列,rlean右斜,llean左斜 二维数组后面的.row什么的是什么
我的意思是二位数组后面加一个小数点是什么意思,没学过 row是横排,col竖列,rlean右斜,llean左斜 二维数组后面的.row什么的是什么
多自己思考问题,想不出来再去google,最后才是求助别人
row是横排,col竖列,rlean右斜,llean左斜 二维数组后面的.row什么的是什么
if (cond)
{
...
}
如果能跟踪到光标进入大括号内部,就说明条件满足了。二维数组后面的.row什么的是什么
struct{
int row; /* - */
int col; /* | */
int rlean; /* \ */
int llean; /* / */
char color;
}chess[6][6];
int victory(void)
{
int i, j;
for(i = 0; i < 6; i++){
for(j = 0; j < 6; j++)
if(chess[i][j].row == 5 || chess[i][j].col == 5 || chess[i][j].rlean == 5 || chess[i][j].llean == 5){
if(chess[i][j].color == 'r'){
printf("Read Player is The Winner\n");
return;
}else if(chess[i][j].color == 'b'){
printf("Black Player is The Winner\n");
return;
}
}
if(chess[i][j].color == chess[i][(j + 1) % 6].color){
chess[i][(j + 1) % 6].row = chess[i][j].row + 1;
}
if(chess[i][j].color == chess[(i + 1) % 6][j].color){
chess[(i + 1) % 6][j].col = chess[i][j].col + 1;
}
if(chess[i][j].color == chess[(i + 1) % 6][(j + 1) % 6].color){
chess[(i + 1) % 6][(j + 1) % 6].rlean = chess[i][j].rlean + 1;
}
if(chess[i][j].color == chess[(i + 1) % 6][(j + 1) % 6].color){
chess[(i + 1) % 6][(j - 1 + 6) % 6].llean = chess[i][j].llean + 1;
}
}
printf("nobody wins the game\n");
}
四个方向 水平 垂直 右斜 左斜你把每个棋子的row,col,lean初始化为1,然后下棋就给color赋值,下完了调用就行了
struct{
int row; // -
int col; // |
int rlean; // \
int llean // /
char color;
}chess[6][6];
int victory(void)
{
int i, j;
for(i = 0; i < 6; i++){
for(j = 0; j < 6; j++)
if(chess[i][j].row == 5 || chess[i][j].col == 5 || chess[i][j].lean == 5){
if(chess[i][j].color == 'r'){
printf("Read Player is The Winner\n");
return;
}else if(chess[i][j].color == 'b'){
printf("Black Player is The Winner\n");
return;
}
}
if(chess[i][j].color == chess[i][(j + 1) % 6].color){
chess[i][(j + 1) % 6].row = chess[i][j].row + 1;
}
if(chess[i][j].color == chess[(i + 1) % 6][j].color){
chess[(i + 1) % 6][j].col = chess[i][j].col + 1;
}
if(chess[i][j].color == chess[(i + 1) % 6][(j + 1) % 6].color){
chess[(i + 1) % 6][(j + 1) % 6].rlean = chess[i][j].rlean + 1;
}
if(chess[i][j].color == chess[(i + 1) % 6][(j + 1) % 6].color){
chess[(i + 1) % 6][(j - 1 + 6) % 6].llean = chess[i][j].llean + 1;
}
}
printf("nobody wins the game\n");
}
每个点四个方向就行了帮你重写了一下,你看能不能用 [quote=引用 10 楼 weixin_41581673 的回复:] 附一张图片比较容易看一点struct{ int row; int col; int lean; char color; }chess[6][6]; int victory(void) { int i, j; for(i = 0; i < 6; i++){ for(j = 0; j < 6; j++) if(chess[i][j].row == 5 || chess[i][j].col == 5 || chess[i][j].lean == 5){ if(chess[i][j].color == 'r'){ printf("Read Player Win"); }else{ printf("Black Player Win"); } } if(chess[i][j].color == chess[i][(j + 1) % 6].color){ chess[i][(j + 1) % 6].row = chess[i][j].row + 1; } if(chess[i][j].color == chess[(i + 1) % 6][j].color){ chess[(i + 1) % 6][j].col = chess[i][j].col + 1; } if(chess[i][j].color == chess[(i + 1) % 6][(j + 1) % 6].color){ chess[(i + 1) % 6][(j + 1) % 6].lean = chess[i][j].lean + 1; } } }
附一张图片比较容易看一点
struct{
int row;
int col;
int lean;
char color;
}chess[6][6];
int victory(void)
{
int i, j;
for(i = 0; i < 6; i++){
for(j = 0; j < 6; j++)
if(chess[i][j].row == 5 || chess[i][j].col == 5 || chess[i][j].lean == 5){
if(chess[i][j].color == 'r'){
printf("Read Player Win");
}else{
printf("Black Player Win");
}
}
if(chess[i][j].color == chess[i][(j + 1) % 6].color){
chess[i][(j + 1) % 6].row = chess[i][j].row + 1;
}
if(chess[i][j].color == chess[(i + 1) % 6][j].color){
chess[(i + 1) % 6][j].col = chess[i][j].col + 1;
}
if(chess[i][j].color == chess[(i + 1) % 6][(j + 1) % 6].color){
chess[(i + 1) % 6][(j + 1) % 6].lean = chess[i][j].lean + 1;
}
}
}
试下这个