Last active
April 15, 2016 22:00
-
-
Save billy8407/faa1105f7694c02d4a45ba708ebd0d83 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
修改中 | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<time.h> | |
//檢查要印出O還是X | |
char tablecheck(int position_x, | |
int position_y, | |
int table[3][3]) | |
{ | |
if(table[position_x][position_y]==1) | |
return 'O'; | |
else if(table[position_x][position_y]==2) | |
return 'X'; | |
else | |
return ' '; | |
} | |
//印出棋盤 | |
void printtable(int table[3][3]) | |
{ | |
printf("%c|%c|%c\n",tablecheck(0,0,table),tablecheck(0,1,table),tablecheck(0,2,table)); | |
printf("-+-+-\n"); | |
printf("%c|%c|%c\n",tablecheck(1,0,table),tablecheck(1,1,table),tablecheck(1,2,table)); | |
printf("-+-+-\n"); | |
printf("%c|%c|%c\n",tablecheck(2,0,table),tablecheck(2,1,table),tablecheck(2,2,table)); | |
} | |
//玩家下棋 | |
void playerputxo(int* turn, | |
int xo, | |
int table[3][3]) | |
{ | |
int position_x,position_y; | |
while(1) | |
{ | |
printf("請輸入(例:1 1) > "); | |
scanf(" %d %d",&position_x,&position_y); | |
if(table[position_x][position_y]!=0) | |
{ | |
printf("此坐標已經有畫記請重選\n"); | |
continue; | |
} | |
else if(table[position_x][position_y]==0) | |
{ | |
table[position_x][position_y]=xo; | |
printtable(table); | |
*turn=*turn+1; | |
break; | |
} | |
} | |
} | |
//電腦下棋 | |
void computxo(int* turn, | |
int xo, | |
int table[3][3]) | |
{ | |
int position_x,position_y; | |
if(xo==1) | |
xo=2; | |
else | |
xo=1; | |
printf("請等待\n"); | |
while(1) | |
{ | |
srand((unsigned)time(NULL)); | |
position_x=rand()%3; //電腦選擇X座標 | |
position_y=rand()%3; //電腦選擇Y座標 | |
if(table[position_x][position_y]!=0) //某坐標已經有畫記將重選 | |
continue; | |
else if(table[position_x][position_y]==0) | |
{ | |
printf("電腦選擇(%d,%d)\n",position_x,position_y); | |
table[position_x][position_y]=xo; | |
printtable(table); | |
*turn=*turn+1; | |
break; | |
} | |
} | |
} | |
//判斷連成一線 | |
void line(int* turn, | |
int table[3][3]) | |
{ | |
int i,j; | |
for(i=0,j=2 ; *turn<9,i<3 ; i++,j--) | |
{ | |
//橫線 | |
if(table[i][0]==table[i][1] && table[i][1]==table[i][2] && table[i][0]!=0) | |
{ | |
*turn=10; | |
break; | |
} | |
//直線 | |
if(table[0][i]==table[1][i] && table[1][i]==table[2][i] && table[0][i]!=0) | |
{ | |
*turn=10; | |
break; | |
} | |
//斜線 | |
if(table[0][i]==table[1][1] && table[1][1]==table[2][j] && table[1][1]!=0) | |
{ | |
*turn=10; | |
break; | |
} | |
} | |
} | |
/***********************************************************/ | |
int main(void) | |
{ | |
int table[3][3]={0}; //圈圈叉叉的九宮格 | |
int position_x=0,position_y=0; //座標X和座標Y | |
int priority; //優先權 | |
int xo; //圈圈或是叉叉 | |
int turn=0; //回合 | |
printf("玩家先輸入1,電腦先輸入2 > "); | |
scanf("%d",&priority); | |
printf("玩家選擇O輸入1,選擇X輸入2 > "); | |
scanf("%d",&xo); | |
printf("遊戲開始\n"); | |
printtable(table); | |
if(priority==1) | |
{ | |
while(1) | |
{ | |
playerputxo(&turn, xo, table); | |
line(&turn,table); | |
if(turn==9) | |
{ | |
printf("平手\n"); | |
break; | |
} | |
else if(turn==10) | |
{ | |
printf("玩家獲勝\n"); | |
break; | |
} | |
computxo(&turn, xo, table); | |
line(&turn,table); | |
if(turn==9) | |
{ | |
printf("平手\n"); | |
break; | |
} | |
else if(turn==10) | |
{ | |
printf("電腦獲勝\n"); | |
break; | |
} | |
} | |
} | |
else if(priority==2) | |
{ | |
while(1) | |
{ | |
computxo(&turn, xo, table); | |
line(&turn,table); | |
if(turn==9) | |
{ | |
printf("平手\n"); | |
break; | |
} | |
else if(turn==10) | |
{ | |
printf("電腦獲勝\n"); | |
break; | |
} | |
playerputxo(&turn, xo, table); | |
line(&turn,table); | |
if(turn==9) | |
{ | |
printf("平手\n"); | |
break; | |
} | |
else if(turn==10) | |
{ | |
printf("玩家獲勝\n"); | |
break; | |
} | |
} | |
} | |
system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment