利用函数链接
1.text.c(游戏进程代码)
#define _CRT_SECURE_NO_WARNINGS_1#include"game.h"
int c = 0;void menu()//菜单
{printf("你好欢迎进入三子棋游戏:");printf("\n----------------------\n");printf(" 游戏准备开始:\n");printf("---------------------");printf("\n输入(1)开始游戏 输入(0)退出游戏");
}void game()//游戏流程代码
{char ret;char board[ROW][COL] = { 0 };Initboard(board, ROW, COL);//初始化棋盘displayboard(board, ROW, COL);//打印棋while (1){playermove(board, ROW, COL);displayboard(board, ROW, COL);ret = iswin(board, ROW, COL);if (ret != 'C')break;computermove(board, ROW, COL);displayboard(board, ROW, COL);ret = iswin(board, ROW, COL);if (ret != 'C')break;}if (ret == '*')printf("玩家赢!\n");else if (ret == '#')printf("电脑赢!\n");elseprintf("平局!\n");}void test()//判断是否进入游戏
{int intput = 0;srand((unsigned int)time(NULL));do{menu();printf("\n请选择:\n");scanf_s("%d", &intput);printf("\n 游戏开始,下图为棋盘(*代表玩家,#代表电脑)\n");switch (intput){case 1: game();case 0:printf("退出游戏\n"); break;default:printf("输入错误,请重新输入!"); break;}} while (intput);
}main()
{test();return 0;
}
2.game.c(游戏内容代码)
#include"game.h"
void Initboard(char board[ROW][COL], int row, int col)//初始化棋盘
{int i = 0, j = 0;for (i = 0; i < row; i++){for (j = 0; j < col; j++)board[i][j] = ' ';}
}
void displayboard(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++){printf(" %c ", board[i][j]);//打印一行数据if (j < col - 1)printf("|");}printf("\n");if (i < row - 1){for (j = 0; j < col; j++){printf("---");if (j < col - 1)printf("|");}printf("\n");}}
}
void playermove(char board[ROW][COL], int row, int col)//玩家走棋{int x = 0;int y = 0;printf("玩家走>\n");while(1){printf("请输入坐标:");scanf_s("%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;}elseprintf("该位置已经被占领");}elseprintf("坐标非法,请重新输入");}}
void computermove(char board[ROW][COL], int row, int col)//电脑走棋
{int x ;int y ;printf("电脑走:\n");while (1){x = rand() % ROW;y = rand() % COL;if (board[x - 1][y - 1] == ' '){board[x - 1][y - 1] = '#';break;}}}
char iswin(char board[ROW][COL], int row, int col)//判断胜利者
{int i = 0,c=0;for(i=0;i<ROW;i++)//横三行{if(board[i][0]== board[i][1]&& board[i][1] == board[i][2]&&board[i][1]!=' '){return board[i][1]; c++;}}for (i = 0; i < ROW; i++)//竖三行{if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' '){return board[1][i]; c++;}}//两个斜三行if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' '){return board[1][1]; c++;}if (board[2][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' '){return board[1][1]; c++;}//是否平局if (1 == isfull(board, ROW, COL)){return 'Q';c++;}c++;return 'C';}int isfull(char board[ROW][COL], int row, int col)//判断是否平局
{int i , j ;//返回1,满了;返回0,没满。for (i = 0; i < ROW; i++){for (j = 0; j < COL; j++){if (board[i][j] == ' ')return 0;}}return 1;
}
3.game.h(函数调用)
#define _CRT_SECURE_NO_WARNINGS_1
#pragma once
#include<stdio.h>
#define ROW 3
#define COL 3
#include<time.h>
#include<stdlib.h>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);
//返回四种状态
//玩家赢 ‘*’
//电脑赢 ‘#’
//平局 ‘Q’
//继续 ‘C’
打印输出
游戏输出