C語言實現三子棋實現

2021-09-30 20:47:38 字數 3274 閱讀 2923

標頭檔案.h

#include#include#include#include#define rows 3			 //控制行的大小

#define cols 3 //控制列的大小

void init_board(char arr[rows][cols], int row , int col); //初始化棋盤

void display_board(char arr[rows][cols], int row , int col); //列印棋盤

void player_move(char arr[rows][cols], int row, int col); //玩家走

char checkwin(char arr[rows][cols], int row, int col); //判斷輸贏

void computer_move(char arr[rows][cols],int row, int col); //電腦走

進行遊戲所需要的原始檔.c 對頭檔案的函式進行實現

#define _crt_secure_no_warnings 1

#include#include#include#include"tic-tac-toe.h"

void init_board(char arr[rows][cols], int row, int col) //初始化棋盤

void display_board(char arr[rows][cols], int row, int col) //列印棋盤

} printf("\n");

if (i < row - 1) //列印行的第二部分

}} printf("\n"); }}

void player_move(char arr[rows][cols], int row, int col) //玩家走

if (arr[a - 1][b - 1] != ' ') //接收的座標比實際陣列下標大一

if (arr[a - 1][b - 1] == ' ')

}}char full_borad(char arr[rows][cols], int row, int col) //判斷是否滿了

} return 'q';

}char checkwin(char arr[rows][cols], int row, int col) //判斷輸贏 '*'玩家獲勝 'q'平局 '#'電腦獲勝

if (computer == row)

people = col_win(arr, row, col, '*'); //判斷列獲勝

computer = col_win(arr, row, col, '#');

if (people == col)

return '*';

if (computer == col)

return '#';

people = sideways(arr, row, col, '*'); //判斷斜右獲勝

computer = sideways(arr, row, col, '#');

if (people == col)

return '*';

if (computer == col)

return '#';

people = sideways2(arr, rows, cols, '*'); //判斷斜左獲勝

computer = sideways2(arr, rows, cols, '#');

if (people == cols)

return '*';

if (computer == cols)

return '#';

full = full_borad(arr, rows, cols); //判斷是否平局

if (full == 'q')

return ' ';

return 'q';

}int row_win(char arr[rows][cols], int row, int col, char c) //橫著獲勝返回長度,不然返回0

if (count == rows) //判斷每一行

break;

else

count = 0;

} return count;

}int col_win(char arr[rows][cols], int row, int col, char c) //豎著獲勝返回長度,不然返回0

if (count == cols) //判斷每一列

break;

else

count = 0;

} return count;

}int sideways(char arr[rows][cols], int row, int col, char c)//斜著向右下

if (count != rows)

count = 0;

return count;

}int sideways2(char arr[rows][cols], int row, int col, char c)//斜著向左下

if (count != rows)

count = 0;

return count;

}void computer_move(char arr[rows][cols], int row, int col) //電腦走

}}

執行遊戲的原始檔.c

#define _crt_secure_no_warnings 1

#include#include#include#include"tic-tac-toe.h"

void menu()

void tic_tac()

computer_move(arr, rows, cols); //電腦走

display_board(arr, rows, cols); //顯示棋盤

ret = checkwin(arr, rows, cols); //判斷輸贏

if (ret != 'q')

}if (ret == '#')

if (ret == '*')

if (ret == ' ') }

void test()

} while (number);

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