主函式:main.c
#include
"game.h"
intmain()
}system
("pause");
return0;
}
子函式:game.c
#include
"game.h"
//引入標頭檔案game.h
void
meun()
void
game()
computer_move
(arr)
;//電腦下棋if(
(ret =
check_win
(arr))!=
' ')
//裡面條件成立說明誰贏了或平局,那就跳出迴圈判斷
print_borad
(arr)
;//列印出這次你和電腦的走位
printf
("\n");
}if(ret ==
'x')
else
if(ret ==
'0')
else
if(ret ==
'q')
print_borad
(arr)
;//列印出遊戲結果
}void
init_borad
(char arr[row]
[col]
)//初始化棋盤}}
void
print_borad
(char arr[row]
[col]
)//列印棋盤}}
void
player_move
(char arr[row]
[col]
)//玩家下棋
else}}
void
computer_move
(char arr[row]
[col]
)//電腦下棋}}
intis_full
(char arr[row]
[col]
)//檢測棋盤是否滿了}}
return1;
}char
check_win
(char arr[row]
[col]
)//檢測不同的結果並返回不同的值
}for
(i =
0; i < col; i++
)//檢測列是否相同 }if
(arr[0]
[0]== arr[1]
[1]&& arr[1]
[1]== arr[2]
[2]&& arr[0]
[0]!=
' ')
//檢測對角線
if(arr[0]
[2]== arr[1]
[1]&& arr[1]
[1]== arr[2]
[0]&& arr[0]
[2]!=
' ')
//檢測對角線if(
is_full
(arr)
)return
' ';
}
函式宣告:game.h
#ifndef __game_h__
#define __game__h__
#include
#include
#include
#include
#pragma warning(disable:4996)
#define row 3
#define col 3
void
game()
;void
meun()
;void
init_borad
(char arr[row]
[col]);
void
print_borad
(char arr[row]
[col]);
char
check_win
(char arr[row]
[col]);
void
player_move
(char arr[row]
[col]);
void
computer_move
(char arr[row]
[col]);
intis_full
(char arr[row]
[col]);
#endif
//__game_h__
C語言實現三子棋
game.h define crt secure no warnings 1 ifndef game h define game h include include include include define rows 3 define cols 3 void init board char bo...
三子棋C語言實現
要寫這個三子棋的程式我們分為三個部分首先是宣告函式的標頭檔案,我們分別宣告了五個函式,初始化棋盤,列印棋盤,玩家走,電腦走,檢查是否贏了。之後我們寫測試 然後分別來實現這五個函式 define crt secure no warnings 1 ifndef game h define game h ...
C語言實現三子棋
實現三子棋程式,只要我們能夠理清楚思路,就可以知道其實它的做法並不難,重點在於實際寫 時需要多關注細節。這裡我們可以寫完一塊就可以立馬執行程式檢查是否如我們所想的效果出現,如若不是便可立即查錯糾錯。如下。test.c include include include include game.h vo...