一.框架(源.c)
#include"game.h" #include<stdio.h> #include<time.h> void game() { int ret = 0; char board[ROW][COL] = { 0 }; init_board(board, ROW, COL);//初始化棋盘,使都为空格 print_board(board, ROW, COL);//打印棋盘 //玩家赢了 * //电脑赢了 # //满了 1 //继续 0 while (1) { player_board(board);//玩家下棋 print_board(board, ROW, COL);//打印棋盘 ret = is_win(board); if (ret != 0) { break; } computer_board(board,ROW,COL);//电脑下棋 print_board(board, ROW, COL);//打印棋盘 ret = is_win(board); if (ret != 0) { break; } } if (ret == '*') { printf("玩家胜利\n"); } else if (ret == '#') { printf("电脑胜利\n"); } else { printf("平局\n"); } } int is_play() { printf("************************\n"); printf("******** 1.play ********\n"); printf("******** 0.exit ********\n"); printf("************************\n"); int input; scanf_s("%d", &input); return input; } int main()// { int input; do { input = is_play(); switch (input) { case 1:game(); case 0:printf("游戏结束\n"); break; default:printf("输入错误,请重新输入\n"); } } while (1); return 0; }
二.game功能模块(game.c)
1.#include内容
1. #include<stdio.h> 2. #include"game.h" 3. #include<stdlib.h> 4. #include<time.h>
2.初始化棋盘,使棋盘内的都为空格
void init_board(char board[ROW][COL], int x, int y) { int i = 0; for (i = 0; i < ROW; i++) { int j = 0; for (int j = 0; j < COL ; j++) { board[i][j]= ' '; } } printf("\n"); }
3.打印棋盘
void print_board(char board[ROW][COL], int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { printf(" %c ", board[i][j]); if (j < COL - 1) { printf("|"); } } printf("\n"); if (i < x - 1) { for (int k = 0; k < y; k++) { printf("---"); if (k < x - 1) { printf("|"); } } printf("\n"); } } printf("\n"); }
4.玩家下棋
void player_board(char board[ROW][COL]) { int x, y; printf("请输入你要下棋的位置:"); scanf_s("%d %d", &x, &y); if (x >= 1 && x <= 3 && board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; } else { printf("输入错误,请重新输入\n"); } }
5.电脑下棋
void computer_board(char board[ROW][COL],int row,int col) { int x = 0; int y = 0; srand((unsigned int)time(NULL)); printf("电脑下棋:>\n"); while (1) { x = rand() % row;//0~2 y = rand() % col;//0~2 if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
6.判断是否符合结束游戏的条件
static int is_full(char board[ROW][COL],int row,int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { if (board[i][j] == ' ') { return 0;//没满 } } } return 1;//满了 } int is_win(char board[ROW][COL]) { //判断行 for (int i = 0; i < ROW; i++) { if (board[i][1] == board[i][0] && board[i][2] == board[i][0] && board[1][1] != ' ') { return board[i][1]; } } //判断列 for (int i = 0; i < COL; i++) { if (board[1][i] == board[2][i] && board[0][i] == board[1][i] && board[1][1] != ' ') { return board[1][i]; } } //判断对角线 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') { return board[1][1]; } else if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ') { return board[1][1]; } //判断可以继续下棋 is_full(board,ROW,COL); if (is_full(board,ROW,COL) == 0)//棋盘未满 { return 0; } else { return 1; } }
三.头文件
#pragma once #include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 3//定义行 #define COL 3//定义列 //初始化棋盘,使棋盘内的都为空格 void init_board(char board[ROW][COL], int x, int y); //打印棋盘 void print_board(char board[ROW][COL], int x, int y); //玩家下棋 void player_board(char board[ROW][COL]); //电脑下棋 void computer_board(char board[ROW][COL], int row, int col); //判断是否符合结束游戏的条件 int is_win(char board[ROW][COL]);