源代码🍊
test.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void menu() { printf("*********************\n"); printf("*** 1.play *****\n"); printf("*** 0.eixt *****\n"); printf("*********************\n"); } void game() { //存储数据的二维数组 char board[ROW][COL]; //初始化为space intiboard(board, ROW, COL); //打印棋盘 printboard(board,ROW,COL); char ret = 0; while (1) { //玩家下棋 player(board, ROW, COL); printboard(board, ROW, COL); ret = judge(board, ROW, COL); if (ret != 'c') { break; } //电脑下棋 computer(board, ROW, COL); printboard(board, ROW, COL); ret = judge(board, ROW, COL); if (ret != 'c') { break; } } if ('*' == ret) //单引号里面别加空格 { printf("玩家赢了\n"); } else if ('#' == ret) { printf("电脑赢了\n"); } else { printf("平局\n"); } } //printboard(board, ROW, COL); // 玩家赢 * //电脑赢 # //打平 e //继续 c void test() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("请输入\n"); scanf("%d", &input); switch (input) { case 1: printf("三子棋\n"); game(); break; case 0: printf("退出游戏\n"); break; default: printf("输入错误,请重新输入\n"); break; } } while (input); } int main() { test(); return 0; }
game.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void intiboard(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++) { board[i][j] = ' '; } } } //void printboard(char board[ROW][COL], int row, int col) //{ // int i = 0; // for (i = 0; i < row; i++) // { // // printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]); // if (i < row -1) // printf("---|---|---\n"); // // } // //} void printboard(char board[ROW][COL], int row, int col) { int j = 0; int i = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); for (j = 0; j < col; j++) { if (i < row - 1) { printf("---"); if (j < col - 1) printf("|"); } } printf("\n"); } } void player(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; while (1) { printf("请输入你要下的坐标"); scanf("%d %d", &x, &y); //判断坐标合法性 if (1 <= x && x <= ROW && 1 <= y && y <= COL) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("坐标被占用请重新输入\n"); } } else { printf("输入错误请重新输入\n"); } } } void computer(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } 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 judge(char board[ROW][COL], int row, int col) { int i = 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][0]; } } int j = 0; for (j = 0; j < col; j++) { if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' ') { return board[0][j]; } } //对角线 //3x3版本 /*if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] !=' ') { return board[0][0]; } if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1]!= ' ') { return board[1][1]; } */ //5x5版本 / 与 \ for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (i < row - 2 && j < col - 2) { if (board[i][j] == board[i + 1][j + 1] && board[i + 1][j + 1] == board[i + 2][j + 2] && board[i][j] != ' ') return board[i][j]; } if (j > 1 && i < row - 2) { if (board[i][j] == board[i - 1][j - 1] && board[i - 1][j - 1] == board[i - 2][j - 2] && board[i][j] != ' ') return board[i][j]; } } } if (1 == is_full(board, row, col)) { return 'e'; //满了 //平局 } return 'c'; }
game.h
#pragma once #define ROW 5 #define COL 5 #include<stdio.h> #include<stdlib.h> #include<time.h> //初始化 void intiboard (char board[ROW][COL],int row, int col); //打印棋盘 void printboard(char board[ROW][COL], int row, int col); //玩家下棋 void player(char board[ROW][COL], int row, int col); //电脑下棋 void computer(char board[ROW][COL],int row, int col); //判断输赢 char judge(char board[ROW][COL],int row, int col);
总结🍊
这篇博客我们系统的介绍了三子棋以及三子棋的推广,大家有没有感觉三子棋的推广有点熟悉呢?其实三子棋的推广就是五子棋的思路,如果有兴趣的小伙伴可以自己实现一下五子棋,虽然有些地方不一样,但是大体思路没有太大的改变。
最后如果这篇博客有帮助到你,欢迎点赞关注加收藏
如果本文有任何错误或者有疑点欢迎在评论区评论