有沒有熟析陣列和字元的可以幫我改一下程式

2009-04-15 11:51 pm
#include <stdio.h>
#include <stdlib.h>

int a[3][3];

void show_position(void);
int check(void);

int main()
{
int i, j, player, pp, done;
char buf[3];

for (i=0; i< 3; i++)
for (j=0; j<3; j++)
a[i][j] = 0;

pp = 0;
done = 0;
player = 0;
do
{
show_position();
printf("Player %d select a position (like 1A; 0 to exit.): ", player);
scanf("%s", &buf);
if (buf[0] == '0') break;
i = buf[0] - '1';
j = buf[1] - 'A';
if ((i < 0) || (2 < i) || (j < 0) || (2 < j)) continue;
if (a[i][j] > 0) continue;
a[i][j] = player+1;
player = 1 - player;
done = check();
pp++;
} while ((done == 0) && (pp < 9));

printf("\n");
show_position();
player = 1 - player;

if (done > 0)
printf("Player %d WON!\n", player+1);
else
printf("It is TIE!\n");

return 0;
}

void show_position()
{
int i, j;

printf(" A B C\n");
for (i=0; i< 3; i++)
{
printf("%d ", i+1);
for (j=0; j<3; j++)
if (a[i][j] == 1)
printf("O ");
else if (a[i][j] == 2)
printf("X ");
else
printf("_ ");
printf("\n");
}
}

int check()
{
int i, j;

for (i=0; i< 3; i++)
if ((a[i][0] == a[i][1]) && (a[i][0] == a[i][2]))
if (a[i][0] > 0) return a[i][0];
for (j=0; j< 3; j++)
if ((a[0][j] == a[1][j]) && (a[0][j] == a[2][j]))
if (a[0][j] > 0) return a[0][j];
if ((a[0][0] == a[1][1]) && (a[0][0] == a[2][2]))
if (a[0][0] > 0) return a[0][0];
if ((a[0][2] == a[1][1]) && (a[0][2] == a[2][0]))
if (a[0][2] > 0) return a[0][2];
return(0);

}



我想改成這樣
1 2 3
4 5 6
7 8 9

然後輸入1就會變成
O 2 3
4 5 6
7 8 9

有人知道怎麼改嗎?

回答 (2)

2009-04-16 6:24 am
✔ 最佳答案
既然你都用二維陣列了,何不改成:
--------------------
|(1,1)|(1,2)|(1,3)|
--------------------
|(2,1)|(2,2)|(2,3)|
--------------------
|(3,1)|(3,2)|(3,3)|
--------------------
程式碼在此:http://src.wtgstudio.com/?i23SYp

2009-04-15 22:26:59 補充:
解說在另一篇知識:
http://tw.knowledge.yahoo.com/question/question?qid=1609041308855

2009-04-15 22:40:57 補充:
你們的程式其實有一些缺點:1.可以下在重複的地方2.沒有判斷輸贏或平手3.棋盤會一直重複出現,這些缺點在我的程式中都加以改進,如果你堅持要用1~9來表示輸入的話也是可行,只需要把輸入的地方用一個變數存,再轉換成二維陣列,程式內實際上還是用二維陣列判斷就好。
參考: 我的頭腦
2009-04-16 6:08 am
// send me an e-mail if this introduces a bug
#include <stdio.h>
#include <stdlib.h>

int a[3][3];

void show_position(void);
int check(void);

int main()
{
int i, j, player, pp, done;
char buf[3];

for (i=0; i< 3; i++) {
for (j=0; j<3; j++) {
a[i][j] = 0;
}
}

pp = 0;
done = 0;
player = 0;

do {
show_position();
printf("Player %d select a position (like 1A; 0 to exit.): ", player);
scanf("%s", &buf);

if (buf[0] == '0') break;
i = (buf[0] - '1') / 3;
j = (buf[0] - '1') % 3 ;
if ((i < 0) || (2 < i) || (j < 0) || (2 < j)) continue;
if (a[i][j] > 0) continue;

a[i][j] = player+11;
player = 1 - player;

done = check();
pp++;

} while ((done == 0) && (pp < 9));

printf("\n");
show_position();
player = 1 - player;

if (done > 0)
printf("Player %d WON!\n", player+1);
else
printf("It is TIE!\n");

return 0;
}

void show_position()
{
int i, j;

for (i=0; i< 3; i++) {
for (j=0; j<3; j++)
if (a[i][j] == 11) {
printf("O ");
} else if (a[i][j] == 12) {
printf("X ");
} else {
printf("%d ",i*3+j+1);
}
printf("\n");
}
}

int check()
{
int i, j;

for (i=0; i< 3; i++) {
if ((a[i][0] == a[i][1]) && (a[i][0] == a[i][2])) {
if (a[i][0] > 0) return a[i][0];
}
}
for (j=0; j< 3; j++) {
if ((a[0][j] == a[1][j]) && (a[0][j] == a[2][j])) {
if (a[0][j] > 0) return a[0][j];
}
}

if ((a[0][0] == a[1][1]) && (a[0][0] == a[2][2]))
if (a[0][0] > 0) return a[0][0];
if ((a[0][2] == a[1][1]) && (a[0][2] == a[2][0]))
if (a[0][2] > 0) return a[0][2];

return(0);
}






收錄日期: 2021-04-30 12:55:16
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090415000016KK05561

檢視 Wayback Machine 備份