C語言實現三子棋

2021-10-17 11:23:01 字數 1896 閱讀 6398

#define row 3

#define col 3

#include #include #include //宣告

void initboard(char board[row][col], int row, int col);

void displayboard(char board[row][col], int row, int col);

void playermove(char board[row][col], int row, int col);

void computermove(char board[row][col], int row, int col);

char iswin(char board[row][col], int row, int col);

#include "game.h"

void initboard(char board[row][col], int row, int col) }}

void displayboard(char board[row][col], int row, int col)

printf("\n");

//列印分隔行

if (i < row - 1)

printf("\n");

} }}void playermove(char board[row][col], int row, int col)

else

}else

}}void computermove(char board[row][col], int row, int col) }}

//返回1表示棋盤滿了

//返回0表示棋盤沒滿

int isfull(char board[row][col], int row, int col)

} }return 1;//滿了

}char iswin(char board[row][col], int row, int col)

} //豎三列

if (i = 0; i < col; i++)

}//兩個對角線

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

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

return board[1][1];

//是否平局

if (1 == isfull(board, row, col))

return 'c';

}

#include #include "game.h"

void menu()

//遊戲的整個演算法的實現

void game()

;//全部空格

//初始化棋盤

initboard(board, row, col);

//列印棋盤

displayboard(board, row, col);

//下棋

while (1)

//電腦下棋

computermove(board, row, col);

displayboard(board, row, col);

//判斷電腦是否贏

char ret = iswin();

if (ret != = 'c')

}}if (ret == '*')

else

}void test()

} while (input);

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