三子棋C語言實現

2021-10-01 19:09:06 字數 3339 閱讀 8932

先讚後看  養成習慣

雖然題目是三子棋,但是其實我實現的是5*5棋盤的4子棋玩法

廢話不多說直接上**

一共三個檔案:

1.game.h 存放函式宣告等

2.game.c 存放函式的定義(實現)

3.test.c 遊戲主函式

#define _crt_secure_no_warnings 1

#include#include#include#include#define row 6 //棋盤的行數

#define col 6 //棋盤的列數

#define chess1 'o'//一號玩家的棋子形狀

#define chess2 'x'//二號玩家的棋子形狀

#define chesspc_p 'p'//玩家棋子形狀

#define chesspc_c 'c'//電腦棋子形狀

void chess(char chess[row][col], int row, int col);//初始化棋子陣列(可以下棋的位置)

void chessbroad(char chess[row][col], int row, int col);//列印棋盤

void playermove(char chess[row][col], int row, int col, int n);//玩家下棋

void computermove(char chess[row][col], int row, int col);//電腦走

char win(char chess[row][col], int row, int col);//判斷勝負

int full(char chess[row][col], int row, int col);//判斷棋盤是否為滿

#define _crt_secure_no_warnings 1

#include"game.h"

void chess(char chess[row][col], int row, int col)

void chessbroad(char chess[row][col], int row, int col)

printf("\n");

if (i < row - 1)

printf("\n");

} }}int r = 0;//玩家要下的行數

int c = 0;//玩家要下的列數

//我想用記錄每次玩家落字的座標這樣每次判斷只用判斷該子所在的行列斜線是否達到獲勝條件,但是我不會

void playermove(char chess[row][col], int row, int col, int n)

scanf("%d %d", &r, &c);

if (r > 0 && r <= row && c > 0 && c <= col)

else

} else }}

char win(char chess[row][col], int row, int col)

} //判斷列

for (i = 0; i < col - 1; i++)

} //判斷斜線 5*5 棋盤的 四子棋玩法判斷

//!!不會寫普遍的規律

if (chess[0][0] == chess[1][1] && chess[1][1] == chess[2][2] && chess[2][2] == chess[3][3] && chess[0][0] != ' ')

return 'w';

if (chess[1][1] == chess[2][2] && chess[2][2] == chess[3][3] && chess[3][3] == chess[4][4] && chess[1][1] != ' ')

return 'w';

if (chess[1][0] == chess[2][1] && chess[2][1] == chess[3][2] && chess[3][2] == chess[4][3] && chess[1][0] != ' ')

return 'w';

if (chess[0][1] == chess[1][2] && chess[1][2] == chess[2][3] && chess[2][3] == chess[3][4] && chess[0][1] != ' ')

return 'w';

if (full(chess, row, col))//full函式判斷棋盤是否為滿

return 'e';

}int full(char chess[row][col], int row, int col)

void computermove(char chess[row][col], int row, int col)

}}

#define _crt_secure_no_warnings 1

#include"game.h"

void menu1()

void menu2()

void game() ;//這個陣列存放可以下棋的位置,我們叫它棋子

chess(chess, row, col);//初始化棋子陣列

chessbroad(chess, row, col);//列印棋盤

while (1)

else if (judge == 'e')

playermove(chess, row, col, 2);//玩家2下棋

chessbroad(chess, row, col);

judge = win(chess, row, col);

if (judge == 'w')

else if (judge == 'e') }}

void game2() ;

chess(chess, row, col);

chessbroad(chess, row, col);

while (1)

else if (judge == 'e')

computermove(chess, row, col);

chessbroad(chess, row, col);

judge = win(chess, row, col);

if (judge == 'w')

else if (judge == 'e') }}

void test2()

} while (input);

}void test1()

} while (input);//0退出迴圈 其他輸入繼續迴圈

}int main()

對您有幫助不妨點乙個贊~

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...