一、编写思路🌍🌍
1、菜单函数🎯🎯
对于三子棋游戏需要首先给用户展现一个可供选择的菜单,就像游戏的开始界面,三子棋游戏有两个功能:下棋、退出。
void menu() { printf("-------------------------------------\n"); printf("---------- 1.下棋 -----------\n"); printf("---------- 2.退出 -----------\n"); printf("-------------------------------------\n"); }
2、初始化函数🐚🐚
创建一个二维数组,二维数组开始不存放任何数据,但是由于需要打印棋盘美观的情况下,就将二维数组的元素全部初始化为空格,可以用两层for循环来实现。
void init(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] = ' '; } } }
3、打印棋盘函数🌋🌋
三子棋游戏就需要将用户和电脑的下棋位置来打印到屏幕上,还提升用户的体验,同样利用for循环来实现,但是需要注意布局,增加‘_’和‘|’来增强美感。
void print(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) { printf("|"); } } printf("\n"); if (i < row - 1) { for (int j = 0; j < col; j++) { printf("---"); if (j < col - 1) { printf("|"); } } printf("\n"); } } }
效果演示:
4、用户下棋函数💎💎
用户下棋时,主要是输入下的棋坐标,用户的坐标从1开始,但是在实际的内存中是从0,故在判断越界等操作时需要对用户所输入的坐标进行减一操作,除了判断是否越界,还需要判断用户所输入的坐标是否已经被占用。(用户所下的棋用‘*’表示)
void player_move(char board[ROW][COL], int row, int col) { int m = 0 , n = 0; printf("请输入下的棋的坐标:"); scanf("%d%d", &m, &n); if (m - 1 < row && n - 1 < col) { if (board[m-1][n-1] == ' ') { board[m-1][n-1] = '*'; } else { printf("该位置已经被占用\n"); } } else { printf("输入的坐标非法!\n"); } }
效果演示:
5、电脑下棋函数🎮🎮
三子棋就是人机对弈游戏,但是本款游戏电脑下棋没有只能化,只是简单的随机下棋,产生两个随机数并判断越界和未被占用的问题。(电脑所下的棋用'#表示)
void computer_move(char board[ROW][COL], int row, int col) { srand((unsigned)time(NULL)); int m = 0, n = 0; while (1) { m = rand() % ROW; n = rand() % COL; if (m< row && n < col) { if (board[m][n] == ' ') { board[m ][n ] = '#'; break; } } } }
效果演示:
6、判断输赢函数⛳⛳
判断输赢是游戏的一个重要部分,三子棋判断输赢的标准就是看用户和电脑谁先连成一条线,可以是某一行或者某一列、对角线、反对角线,还有当棋盘满的话就是平局,否则就继续。
char is_win(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { int flag = 1; for (int j = 0; j< col; j++) { if (board[i][0] != board[i][j]) { flag = 0; break; } if (board[i][0] == ' ') { flag = 0; break; } } if (flag == 1) { return board[i][0]; } } for (int j = 0; j < col; j++) { int flag = 1; for (int i = 0; i < row; i++) { if (board[0][j] != board[i][j]) { flag = 0; break; } if (board[0][j] == ' ') { flag = 0; break; } } if (flag == 1) { return board[0][j]; } } int flag = 1; for (int i = 1,j=1; i < row&& j < col; i++,j++) { if (board[i][j] != board[i - 1][j - 1]) { flag = 0; break; } if (board[0][0]==' ') { flag = 0; break; } } if (flag == 1) { return board[0][0]; } int tag = 1; for (int i = 1, j = col-2; i < row && j>=0; i++, j--) { if (board[i][j] != board[i - 1][j +1]) { tag = 0; break; } if (board[0][col-1] == ' ') { tag = 0; break; } } if (tag== 1) { return board[0][col-1]; } for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (board[i][j] == ' ') return 'C'; } } return 'Q'; }
效果演示:
二、整体代码展示🏠🏠
1、game.h🚓🚓
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 3 #define COL 3 void menu(); void init(char board[ROW][COL], int row, int col); void print(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); char is_win(char board[ROW][COL], int row, int col);
2、game.c🚜🚜
#include"game.h" void menu() { printf("-------------------------------------\n"); printf("---------- 1.下棋 -----------\n"); printf("---------- 0.退出 -----------\n"); printf("-------------------------------------\n"); } void init(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 print(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) { printf("|"); } } printf("\n"); if (i < row - 1) { for (int j = 0; j < col; j++) { printf("---"); if (j < col - 1) { printf("|"); } } printf("\n"); } } } void player_move(char board[ROW][COL], int row, int col) { int m = 0 , n = 0; printf("请输入下的棋的坐标:"); scanf("%d%d", &m, &n); if (m - 1 < row && n - 1 < col) { if (board[m-1][n-1] == ' ') { board[m-1][n-1] = '*'; } else { printf("该位置已经被占用\n"); } } else { printf("输入的坐标非法!\n"); } } void computer_move(char board[ROW][COL], int row, int col) { srand((unsigned)time(NULL)); int m = 0, n = 0; while (1) { m = rand() % ROW; n = rand() % COL; if (m< row && n < col) { if (board[m][n] == ' ') { board[m ][n ] = '#'; break; } } } } char is_win(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { int flag = 1; for (int j = 0; j< col; j++) { if (board[i][0] != board[i][j]) { flag = 0; break; } if (board[i][0] == ' ') { flag = 0; break; } } if (flag == 1) { return board[i][0]; } } for (int j = 0; j < col; j++) { int flag = 1; for (int i = 0; i < row; i++) { if (board[0][j] != board[i][j]) { flag = 0; break; } if (board[0][j] == ' ') { flag = 0; break; } } if (flag == 1) { return board[0][j]; } } int flag = 1; for (int i = 1,j=1; i < row&& j < col; i++,j++) { if (board[i][j] != board[i - 1][j - 1]) { flag = 0; break; } if (board[0][0]==' ') { flag = 0; break; } } if (flag == 1) { return board[0][0]; } int tag = 1; for (int i = 1, j = col-2; i < row && j>=0; i++, j--) { if (board[i][j] != board[i - 1][j +1]) { tag = 0; break; } if (board[0][col-1] == ' ') { tag = 0; break; } } if (tag== 1) { return board[0][col-1]; } for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (board[i][j] == ' ') return 'C'; } } return 'Q'; }
3、test.c🚗🚗
#include"game.h" void game() { char board[ROW][COL] = { 0 }; char ret = 0; init(board, ROW, COL); print(board, ROW, COL); while (1) { player_move(board, ROW, COL); print(board, ROW, COL); ret = is_win(board, ROW, COL); if ('C' != ret) { break; } computer_move(board, ROW, COL); printf("电脑下棋:\n"); print(board, ROW, COL); if ('C' != ret) { break; } } if (ret == 'Q') { printf("平局!\n"); } else if (ret == '*') { printf("您赢了!\n"); } else if(ret == '#') { printf("电脑赢!\n"); } } int main() { char board[ROW][COL] = { 0 }; init(board, ROW, COL); int option = 0; do { menu(); printf("请选择:"); scanf("%d", &option); switch (option) { case 1: game(); break; case 0: printf("退出成功!\n"); break; default: printf("您的输入有误,请重新输入!\n"); } } while (option); return 0; }
总结:以上就是实现三子棋游戏的所有代码,也可以在宏定义修改,可以扩展为五子棋游戏等。