#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void init_board(char board[ROW][COL], int row, int col) { int i, j ; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { board[i][j] = ' '; } } } void print_board(char board[ROW][COL], int row, int col) { int i, j; for (j = 0; j < col - 1; j++) { for (i = 0; i < row - 1; i++) { printf(" %c |", board[i][j]); } printf(" %c \n", board[i][j]); for (i = 0; i < row - 1; i++) { printf("---|"); } printf("---\n"); } for (i = 0; i < row - 1; i++) { printf(" %c |", board[i][j]); } printf(" %c \n", board[i][j]); } void player_move(char board[ROW][COL], int row, int col) { printf("棋盘大小为%d×%d,%d子连线为赢,现在你为先手。\n", ROW, COL, WIN); printf("玩家,“#”为你的棋子,“*”为我的棋子\n"); printf("请输入你的想下的位置:>"); while (1) { int x = 0, y = 0; scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '#'; break; } else printf("你输入的位置已经被填过了!,请重新输入:>"); } else printf("输入错误!\n"); } } void computer_move(char board[ROW][COL], int row, int col) { printf("我思考一下....\n"); Sleep(1500); while (1) { int x = rand() % row; int y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '*'; break; } } } static int is_full(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (board[i][j] == ' ') return 0; } } return 1; } char who_win(char board[ROW][COL], int row, int col) { int i, j; int j1,j2; for (j1 = 0; j1 < col; j1++) { for (i = 0; i <= row - WIN; i++) { for (j = i + 1; j < i + WIN; j++) { if (board[j][j1] != board[i][j1]) break; } if (j == i + WIN && board[i][j1] != ' ') return board[i][j1]; } } for (i = 0; i < row; i++) { for (j1 = 0; j1 <= col - WIN; j1++) { for (j2 = j1 + 1; j2 < j1 + WIN; j2++) { if (board[i][j2] != board[i][j1]) break; } if (j2 == j1 + WIN && board[i][j1] != ' ') return board[i][j1]; } } for (j1 = 0; j1 <= col - WIN; j1++) { for (i = 0; i <= row - WIN; i++) { for (j = i + 1, j2 = j1 + 1; j2 < j1 + WIN && j < i + WIN; j++, j2++) { if (board[j][j2] != board[i][j1]) break; } if (j2 == j1 + WIN && j == i + WIN && board[i][j1] != ' ') return board[i][j1]; } } for (j1 = col - 1; j1 >= COL - WIN - 1; j1--) { for (i = 0; i <= row - WIN; i++) { for (j = i + 1, j2 = j1 - 1; j2 > j1 - WIN && j < i + WIN; j++, j2--) { if (board[j][j2] != board[i][j1]) break; } if (j2 == j1 - WIN && j == i + WIN && board[i][j1] != ' ') return board[i][j1]; } } if (is_full(board, row, col)) return 'Q'; return 'C'; } void menu() { printf("##########################\n"); printf("##########################\n"); printf("######请选择一个数字######\n"); printf("####1.进入三子棋游戏######\n"); printf("#######0.退出游戏#########\n"); printf("##########################\n"); printf("##########################\n"); printf("########################>"); } void game() { srand((unsigned int)time(NULL)); char board[ROW][COL]; char ret; init_board(board, ROW, COL); print_board(board, ROW, COL); while (1) { player_move(board, ROW, COL); system("cls"); print_board(board, ROW, COL); ret = who_win(board, ROW, COL); if (ret != 'C') { break; } computer_move(board, ROW, COL); system("cls"); print_board(board, ROW, COL); ret = who_win(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '#') printf("玩家你太牛了\n再来一局吧!\n"); else if (ret == '*') printf("嘻嘻,我赢了\n再来一局吧!\n"); else if (ret == 'Q') printf("平局了...\n再来一局吧!\n"); } void do_you_want() { int option = 0, count = 0; do { menu(); scanf("%d", &option); switch (option) { case 1: game(); break; case 0: { printf("退出游戏!\n"); break; } default: printf("请重新输入\n"); break; } } while (option); }
这是在源文件定义,这里我优化了三子棋,可以是四子棋等等,棋盘也可以是任何形式。
#define _CRT_SECURE_NO_WARNINGS 1 #define ROW 10 //你需要多少列的棋盘? #define COL 10 //你需要多少行的棋盘? #define WIN 5 //你想几子连成线为赢? #include<stdio.h> #include<time.h> #include<stdlib.h> #include<Windows.h> void init_board(char board[ROW][COL], int row, int col); void print_board(char board[ROW][COL], int row, int col); void player_move(char board[ROW][COL], int row, int col); void computer_move(char board[ROW][COL], int row, int col); static int is_full(char board[ROW][COL], int row, int col); char who_win(char board[ROW][COL], int row, int col); void menu(); void game(); void do_you_want();
这个是在头文件申明
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" int main() { do_you_want(); return 0; }