C語言實現簡單的三子棋小程式

2021-08-15 17:19:40 字數 3170 閱讀 9153

1 game.h中用於函式定義

#ifndef __game_h__

#define __game_h__

#include #include #include #include #define row 6

#define col 6

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);

#endif //__game_h__

2.game.c實現遊戲的函式

#define _crt_secure_no_warnings

#include "game.h"

void initboard(char board[row][col], int row, int col) //初始化棋盤

else

}else

}}void computermove(char board[row][col], int row, int col) //電腦走的函式 }}

static int is_full(char board[row][col], int row, int col)

} return 1; //棋盤已滿

}char iswin(char board[row][col], int row, int col) //判斷是否贏了

} for(i=0;i//斜向三子贏了

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

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

//玩家贏 電腦贏

//int i = 0;

int j = 0;

for (i = 0; iif ((board[i][j] == board[i + 1][j] &&

board[i + 1][j] == board[i + 2][j] && board[i][j] != ' ') ||

(board[i][j] == board[i + 1][j] &&

board[i + 1][j] == board[i - 1][j] && board[i][j] != ' ') ||

(board[i][j] == board[i - 1][j] &&

board[i - 1][j] == board[i - 2][j] && board[i][j] != ' '))

//斜向三子贏了

if ((board[i][j] == board[i - 1][j + 1] &&

board[i - 1][j + 1] == board[i + 1][j - 1] && board[i][j] != ' ') ||

(board[i][j] == board[i - 1][j + 1] &&

board[i - 1][j + 1] == board[i + 1][j - 1] && board[i][j] != ' ') ||

(board[i][j] == board[i + 1][j - 1] &&

board[i + 1][j - 1] == board[i + 2][j - 2] && board[i][j] != ' '))

if ((board[i][j] == board[i + 1][j + 1] &&

board[i + 1][j + 1] == board[i + 2][j + 2] && board[i][j] != ' ') ||

(board[i][j] == board[i - 1][j - 1] &&

board[i - 1][j - 1] == board[i + 1][j + 1] && board[i][j] != ' ') ||

(board[i][j] == board[i - 1][j - 1] &&

board[i - 1][j - 1] == board[i - 2][j - 2] && board[i][j] != ' '))

}} if (is_full(board, row, col))

return ' '; //繼續遊戲

}3.test.c用來存放主函式

#define _crt_secure_no_warnings

#include "game.h"

void menu() //列印選單

void game()

; initboard(board, row, col);

displayboard(board, row, col);

printf("1 玩家先走 2 電腦先走 0 退出遊戲\n");

scanf("%d", &c);

switch (c)

case 2:

printf("電腦先走:\n");

computermove(board, row, col);

displayboard(board, row, col);

win = iswin(board, row, col);

if (win != ' ')

}while (1)

printf("電腦走》\n");

computermove(board, row, col);

displayboard(board, row, col);

win = iswin(board, row, col);

if (win != ' ')

}if (win == 'x')

else if (win == '0')

else }

void test()

} while (input);

}int main()

C語言實現簡單的三子棋

一 主要思想 1 建立乙個3 3的棋盤 使用字元陣列 2 初始化棋盤 用空格填充 3 列印棋盤 使其有可見的邊框 4 玩家落子,用x表示 檢驗是否越界,是否已經落子,是否贏 5 電腦落子,用o表示 檢驗是否已經落子,是否贏 注 電腦在有效範圍內隨機落子,使用當前時間戳設定隨機種子即srand tim...

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