C語言小遊戲 三子棋

2021-07-24 22:31:59 字數 2615 閱讀 8970

三子棋小遊戲:

三子棋的實現是當玩家或者電腦自身所下的位置在同一條線時,判斷輸贏。

在編寫該遊戲**時應注意到以下幾個方面的問題:

(1)首先應該列印棋盤以及初始化棋盤

(2)在玩家和電腦落子之後均要列印一次棋盤

(3)每次落子之前列印棋盤後,均要判斷是否已產生贏家

(4)判斷輸贏之後,應當判斷此時棋盤是否已滿

(5)電腦的落子位置是隨機的,所以此時應當判斷該隨機的位置在之前是否已經被占用。若被占用則需要重新生成隨機位置,直到該隨機位置未被占用。

標頭檔案:

game.h

#ifndef __game_h__

#define __game_h__

#define _crt_secure_no_warnings

#include #include #include #define rows 3

#define cols 3

void init_chessbord(char board[rows][cols], int rows, int cols);

void print_chessboard(char board[rows][cols], int rows, int cols);

void player_move(char board[rows][cols], int rows, int cols);

void computer_move(char board[rows][cols], int rows, int cols);

char check_winner(char board[rows][cols], int rows, int cols);

#endif //__game_h__

game.c

#define _crt_secure_no_warnings

#include

#include

#include "game.h"

#include

void init_chessbord(char board[rows][cols], int rows, int cols)

void print_chessboard(char board[rows][cols], int rows, int cols)}}

static int is_full(char board[rows][cols], int rows, int cols)

}return -1;//此時已滿

}char check_winner(char board[rows][cols], int rows, int cols)

}for (j = 0; j < cols; j++)

}if ((board[0][0] == board[1][1])

&& (board[1][1] == board[2][2])

&& (board[1][1] != ' '))

if ((board[0][2] == board[1][1])

&& (board[1][1] == board[2][0])

&& (board[1][1] == ' '))

if (is_full(board, rows, cols))

return ' ';//此時遊戲繼續

}void player_move(char board[rows][cols], int rows, int cols)

else

}else

printf("the position isn't legal,please enter again!\n");}}

void computer_move(char board[rows][cols], int rows, int cols)}}

test.c

#define _crt_secure_no_warnings

#include

#include

#include "game.h"

void menu()//列印選單

enum option//列舉,列出選項

;void game()//遊戲函式

;char ret = 0;

init_chessbord(board, rows, cols);

print_chessboard(board, rows, cols);

while (1)

printf("waitting for computer:\n");

computer_move(board, rows, cols);

print_chessboard(board, rows, cols);

ret = check_winner(board, rows, cols);

if (ret != ' ')

}if (ret == '*')

printf("player win!\n");

else if (ret == '@')

printf("computer win!\n");

else

printf("no winner.\n");

}int main()

}while (input);

return 0;}

C語言小遊戲 三子棋

c語言三子棋小遊戲,通過二維陣列構建棋盤.利用七個個函式 void init 構建棋盤 void meau 遊戲選單 void display 構建棋盤 void player 玩家進行 void computer 電腦進行 char iswin 判斷輸贏 int isfull 判斷棋盤是否下滿 來...

C語言小遊戲 三子棋

將整個 分裝在三個檔案中,分別為原始檔 test.c game.c game.h 如下 game.h ifndef game h define game h define row 3 define col 3 定義棋盤行數和列數 include include include include voi...

三子棋小遊戲(C語言)

我們在c語言階段,學的差不多就應該有能力寫一些小遊戲來檢測我們c語言到底學的咋樣,恰巧三子棋和掃雷應該是每乙個程式設計師都會的,簡單的c語言小遊戲。拿到這個題目,我們首先應該要有乙個清晰的思路,三子棋我們應該都是很熟悉的,就是乙個簡單的棋盤,然後需要兩個人對弈,一人走一步,誰先將三顆棋子連成一條線誰...