游戏介绍:
三子棋又名井字棋,相信大家小时候都玩过,在一个3x3的棋盘上下棋,最先实现三字连成一条线的玩家获得胜利。
一、思路
1.创建game.h 头文件用来放函数的声明
2.创建game.c文件来实现主要功能实现代码
3.创建test.c文件测试
二、步骤
1.创建菜单
void menu() { printf("****************************\n"); printf("********** 0:退出 *********\n"); printf("********** 1:开始游戏*******\n"); printf("****************************"); }
2.创建棋盘
void DisplayBoard(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { //1. 打印数据 int j = 0; for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); //2. 打印分割线 if (i < row - 1) { //printf("---|---|---\n"); int j = 0; for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) printf("|"); } printf("\n"); } } }
3.初始化棋盘
void initial(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { board[i][j] = ' '; } } }
4.玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col) { int x, y; printf("请输入要输入的坐标,中间空格分开"); while (1) { scanf("%d %d", &x, &y); //确保输入正确 if (x > 0 && x < row || y>0 && y <= col) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else printf("该位置已被占,请重新输入"); } else printf("请输入合理范围内数字1 - row/col"); } }
5.电脑自动下棋
在实现电脑自动下棋这一功能时,我们需要创建随机数,这就需要调用以下头文件:
同时,我们还需要使用:
srand((unsigned int)time(NULL)); 来进行初始化
因为random()函数可以返回一个整型随机数值,但是起点是一样的,
所以每次rand的随机值都是一样的
例如这次rand是 2 ,15 ,33 ,1,44,下次程序执行还是 2 ,15 ,33 ,1,44,
srand可以初始化rand的起点,这时候使用time函数给srand随机的值,rand就可以每次返回不同的整型数值了。
void ComputerMove(char board[ROW][COL], int row, int col) { srand((unsigned int)time(NULL)); int x, y; while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
6.判断赢
char IsWin(char board[ROW][COL], int row, int col) { //玩家赢 * //电脑赢 # //平局 Q //继续 C int i = 0; //行 for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') { return board[i][0]; } } //列 for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') { return board[0][i]; } } //对角线 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') return board[1][1]; if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') return board[1][1]; //平局 if (IsFull(board, row, col) == 1) { return 'Q'; } //继续 return 'C'; }
7.判断是否棋盘已满
//判断是否为满 int IsFull(char board[ROW][COL], int row, int col) { int i = 0,count = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { if (board[i][j] != ' ') { count++; } } } if (count == 9) return 1; else return 0; }
三、代码总体实现
game.h
#define _CRT_SECURE_NO_WARNINGS 1 #pragma once #include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 3 #define COL 3 void initial(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);//电脑下棋 int IsFull(char board[ROW][COL], int row, int col);//检查棋盘是否满 char IsWin(char board[ROW][COL], int row, int col);//判断是否为赢
game.c
#define _CRT_SECURE_NO_WARNINGS 1 #pragma once #include"game.h" //初始化棋盘为空格 void initial(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int 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++) { //1. 打印数据 int j = 0; for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); //2. 打印分割线 if (i < row - 1) { //printf("---|---|---\n"); int j = 0; 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, y; printf("请输入要输入的坐标,中间空格分开"); while (1) { scanf("%d %d", &x, &y); //确保输入正确 if (x > 0 && x < row || y>0 && y <= col) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else printf("该位置已被占,请重新输入"); } else printf("请输入合理范围内数字1 - row/col"); } } //电脑随机下棋 void ComputerMove(char board[ROW][COL], int row, int col) { srand((unsigned int)time(NULL)); int x, y; while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } //判断是否为满 int IsFull(char board[ROW][COL], int row, int col) { int i = 0,count = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { if (board[i][j] != ' ') { count++; } } } if (count == 9) return 1; else return 0; } //判断是否赢 char IsWin(char board[ROW][COL], int row, int col) { //玩家赢 * //电脑赢 # //平局 Q //继续 C int i = 0; //行 for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') { return board[i][0]; } } //列 for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') { return board[0][i]; } } //对角线 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') return board[1][1]; if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') return board[1][1]; //平局 if (IsFull(board, row, col) == 1) { return 'Q'; } //继续 return 'C'; }
test.c
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include "game.h" void menu() { printf("****************************\n"); printf("********** 0:退出 *********\n"); printf("********** 1:开始游戏*******\n"); printf("****************************"); } void game() { char board[ROW][COL] = { 0 }; //初始化全为零 initial(board, ROW,COL); //打印初始棋盘 DisplayBoard(board, ROW, COL); //开始对局 char ret = 0; while(1) { //玩家下棋 PlayerMove(board, ROW, COL); printf("玩家下棋:\n"); DisplayBoard(board, ROW, COL); //判断继续还是玩家赢 ret = IsWin(board, ROW, COL); if (ret != 'C') break; //电脑下棋 ComputerMove(board, ROW, COL); printf("电脑下棋:\n"); DisplayBoard(board, ROW, COL); //判断继续还是玩家赢 ret = IsWin(board, ROW, COL); if (ret != 'C') break; } if (ret == '*') printf("玩家获胜!!!\n请选择继续还是结束\n"); if (ret == '#') printf("电脑获胜!!!\n请选择继续还是结束\n"); if (ret == 'Q') printf("平局\n"); } int main() { menu(); int n; while(scanf("%d", &n)!= -1){ switch (n) { case 1:game(); menu(); break; case 0:printf("退出游戏!\n"); break; define:printf("输入有误!\n"); break; } } return 0; }
四、游戏运行