1 頁 (共 1 頁)

BORLANDC 求救..

文章發表於 : 週三 6月 29, 2005 1:17 am
tehsuen
目前期末demo 我就沿用了O大的檔案
不過似乎很多地方沒完成..
自己完成他了..
但目前平手時的 " no winer " 跑不出來
也就是9公格平手情況下填滿後
就停在那邊了 @@ 不會顯示 no winer..
這個問題解不出來 有空的幫忙看一下吧
謝謝~!!
p.s 我不會用ai.. :ale: 所以只是給電腦跑random
所以..... 大家不要太認真的思考它能不能玩 :x
總之就是大概交一個程式給老師並解釋 orz
若ai加下去 不知又要暴增幾行了..
寫不出來就先不管了 :aa:

目前程式碼如下

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>

char spot[9]={'1','2','3','4','5','6','7','8','9'};
int oc[9]={0,0,0,0,0,0,0,0,0};

void screen();
void player();
void computer();


void main()
{
int win=0,i,s=0,k;
randomize();
while(1)
{
screen();
player();
screen();

for(i=0;i<9;i++)
if (oc[i]==1) s++;
if (s==9) {win=3;break;}


if (spot[0]=='O' && spot[1]=='O' && spot[2]=='O') {win=1;break;}
if (spot[3]=='O' && spot[4]=='O' && spot[5]=='O') {win=1;break;}
if (spot[6]=='O' && spot[7]=='O' && spot[8]=='O') {win=1;break;}
if (spot[0]=='O' && spot[3]=='O' && spot[6]=='O') {win=1;break;}
if (spot[1]=='O' && spot[4]=='O' && spot[7]=='O') {win=1;break;}
if (spot[2]=='O' && spot[5]=='O' && spot[8]=='O') {win=1;break;}
if (spot[0]=='O' && spot[4]=='O' && spot[8]=='O') {win=1;break;}
if (spot[2]=='O' && spot[4]=='O' && spot[6]=='O') {win=1;break;}

computer();
screen();
for(i=0;i<9;i++)
if (oc[i]==1) s++;
if (s==9) {win=3;break;}

if (spot[0]=='X' && spot[1]=='X' && spot[2]=='X') {win=2;break;}
if (spot[3]=='X' && spot[4]=='X' && spot[5]=='X') {win=2;break;}
if (spot[6]=='X' && spot[7]=='X' && spot[8]=='X') {win=2;break;}
if (spot[0]=='X' && spot[3]=='X' && spot[6]=='X') {win=2;break;}
if (spot[1]=='X' && spot[4]=='X' && spot[7]=='X') {win=2;break;}
if (spot[2]=='X' && spot[5]=='X' && spot[8]=='X') {win=2;break;}
if (spot[0]=='X' && spot[4]=='X' && spot[8]=='X') {win=2;break;}
if (spot[2]=='X' && spot[4]=='X' && spot[6]=='X') {win=2;break;}
}

if (win==1) printf("You win!");
if (win==2) printf("You lost!");
if (win==3) printf("No Winer!!");

while(1){
k=kbhit();
if (k!=0) break;}
}

void screen()
{
clrscr();
printf(" \n");
printf(" %c | %c | %c\n",spot[0],spot[1],spot[2]);
printf(" ___________\n");
printf(" %c | %c | %c\n",spot[3],spot[4],spot[5]);
printf(" ___________\n");
printf(" %c | %c | %c\n\n",spot[6],spot[7],spot[8]);
}


void player()
{
int s,t;
printf("Player's Move:");

do{
scanf("%d",&t);
s=t-1;
}while(oc[s]==1);

oc[s]=1;
spot[s]='O';
}

void computer()
{
int l;
do{
l=rand()%9;
}while(oc[l]==1);
oc[l]=1;
spot[l]='X';
}

文章發表於 : 週三 6月 29, 2005 9:54 am
windwalker
平手不是S==9...... :p
用debug去watch一下看看s最後會落在多少吧..... :ho:

文章發表於 : 週三 6月 29, 2005 11:25 am
RogerShih
更正.

在兩段 for(i=0;i<9;i++) 之前, 記得把 s 歸零.....不然你每次跑 s 都不可能等於 9.
s 每經一次 for() 迴圈的變化是 1、3、6、10、15、21.....etc

正確如下

s=0;
for(i=0;i<9;i++)
if (oc[i]==1) s++;
if (s==9) {win=3;break;}