C語言實現三子棋

2021-09-11 18:40:33 字數 1364 閱讀 7578

#define _crt_secure_no_warnings

#include #include //三子棋

#define row 3

#define col 3

int g_chess_board[row][col];

//棋牌初始化

void init() }}

//列印棋牌

void print() }}

//玩家請落子

void plarmove()

if (g_chess_board[row][col] != ' ')

g_chess_board[row][col] = 'x';

break;

} }//電腦請落子

//電腦落子完全是靠隨機數產生乙個行,隨機數產生乙個列。

//如果當前座標已經有子了,就再產生乙個行和列。

void computermove() }}

int isfull()

} }return 1;

}//檢測遊戲是否結束

//使用x表示玩家,用o表示電腦

//約定checkwinner函式,放回x表示玩家勝利,返回o表示電腦勝利,返回乙個q表示和棋

//如果返回空格表示勝負未分

char checkwinner()

} for (int col = 0; col < col; ++col)

} if (g_chess_board[0][0] == g_chess_board[1][1]

&& g_chess_board[0][0] == g_chess_board[2][2]

&& g_chess_board[0][0] != ' ')

if (g_chess_board[0][2] == g_chess_board[1][1]

&& g_chess_board[0][2] == g_chess_board[2][0]

&& g_chess_board[0][2] != ' ')

if (isfull())

return ' ';

}int main()

//4.請電腦落子,順便檢查一下遊戲是否結束

computermove();

winner = checkwinner();

if (winner != ' ')

} if (winner == 'x')

else if (winner == 'o')

else if (winner == 'q')

//5.遊戲贏家是誰(可能是電腦,可能是玩家,可能是和棋,)

system("pause");

return 0;

}

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