c語言實現三子棋主要用到二維陣列的相關知識,所以我們使用多檔案程式設計來實現這個程式,需要建立乙個標頭檔案,兩個原始檔來實現:
1.game.h–>函式的申明與巨集定義
2.game.c–>遊戲各個功能的實現
3.main.c–>函式的定義
思路:
1.將各個函式的執行放在遊戲執行函式game函式中,將game函式放在迴圈中以便我們遊戲結束後還可以玩。
2.選擇結構判斷繼續玩玩遊戲還是退出遊戲,得有乙個main()函式實現,我們把這個框架放在main.c中:
具體**模組
main.c
game.h#include
"game.h"
#define _crt_secure_no_warnings 1
void
menu()
intmain()
}printf
("byebye!\n");
system
("pause");
}
game.c#ifndef __gaem_h__
#define __game_h__
#include
#include
#include
//隨機數
#pragma warning (disable:4996)
//巨集定義放在標頭檔案
#define row 3
#define col 3
#define player_c 'x'
#define computer_c 'o'
#define init ' '
//在game.c裡實現
void
initboard
(char board[
][col]
,int row,
int col)
;//行可以省略,列不可以
void
showboard
(char board[
][col]
,int row,
int col)
;void
computermove
(char board[
][col]
,int row,
int col)
;char
judge
(char board[
][col]
,int row,
int col)
;void
game()
;#endif
測試效果#include
"game.h"
void
initboard
(char board[
][col]
,int row,
int col)
//行可以省略,列不可以}}
void
showboard
(char board[
][col]
,int row,
int col)
printf
("\n");
printf
("---------------\n");
}/*printf("3 |");
for (int j = 0; j < col; j++)
printf("\n");
printf("---------------\n");*/
}void
computermove
(char board[
][col]
,int row,
int col)}}
char
judge
(char board[
][col]
,int row,
int col)
}for
(i =
0; i < col; i++)}
if(board[0]
[0]== board[1]
[1]&& \
board[1]
[1]!=
' '&& \
board[1]
[1]== board[2]
[2])
if(board[0]
[2]== board[1]
[1]&& \
board[1]
[1]!=
' '&& \
board[1]
[1]==board[2]
[0])
for(i =
0; i < row; i++)}
}return
'f';
}void
game()
if(board[x -1]
[y -1]
!=' '
) board[x -1]
[y -1]
= player_c;
result =
judge
(board, row, col);if
(result !=
'n')
computermove
(board, row, col)
; result =
judge
(board, row, col);if
(result !=
'n')
}while(1
);if(result == player_c)
else
if(result == computer_c)
else
printf
("你玩的還不錯,要不要再來一把!\n");
}
![](https://pic.w3help.cc/0d4/c0738da6a6d71e42c00bf52980f90.jpeg)
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...