c语言小游戏——三子棋 代码功能优化

原网址:(1条消息) C语言实现三子棋(具体步骤和代码)_三子棋c语言_Hidden.Blueee的博客-CSDN博客

源代码:

#define _CRT_SECURE_NO_WARNINGS
#define MAX_ROW 3
#define MAX_COL 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void init(char chessBoard[MAX_ROW][MAX_COL]){
    for (int row = 0; row < MAX_ROW; row++){
        for (int col = 0; col < MAX_COL; col++){
            chessBoard[row][col] = ' ';
        }
    }
}
void print_chessBoard(char chessBoard[MAX_ROW][MAX_COL]){
    printf("+---+---+---+\n");
    for (int row = 0; row <MAX_ROW; row++) {
        printf("| %c | %c | %c |\n", chessBoard[row][0],
            chessBoard[row][1], chessBoard[row][2]);
        printf("+---+---+---+\n");
    }
}
void playerMove(char chessBoard[MAX_ROW][MAX_COL]){
    while (1){
        int row = 0;
        int col = 0;
        printf("请输入坐标(row col):");
        scanf("%d %d", &row, &col);
        if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL){
            printf("您的坐标不在合法范围内 [0, 2],请重新输入:\n");
            continue;
        }
        if (chessBoard[row][col] != ' '){
            printf("您的坐标位置已经有子了!\n");
            continue;
        }
        chessBoard[row][col] = 'x';
        break;
    }
}
void computerMove(char chessBoard[MAX_ROW][MAX_COL]){
    while (1){
        int row = rand() % MAX_ROW;
        int col = rand() % MAX_COL;
        if (chessBoard[row][col] != ' ') {
            continue;
        }
        chessBoard[row][col] = 'o';
        break;
    }
}
int isFull(char chessBoard[MAX_ROW][MAX_COL]){
    for (int row = 0; row < MAX_ROW; row++){
        for (int col = 0; col < MAX_COL; col++){
            if (chessBoard[row][col] == ' '){
                return 0;
            }

        }
    }
    return 1;
}
char isWin(char chessBoard[MAX_ROW][MAX_COL]){
    for (int row = 0; row < MAX_ROW; row++) {
        if (chessBoard[row][0] != ' '
            && chessBoard[row][0] == chessBoard[row][1]
            && chessBoard[row][0] == chessBoard[row][2]) {
            return chessBoard[row][0];
        }
    }
    for (int col = 0; col < MAX_COL; col++) {
        if (chessBoard[0][col] != ' '
            && chessBoard[0][col] == chessBoard[1][col]
            && chessBoard[0][col] == chessBoard[2][col]) {
            return chessBoard[0][col];
        }
    }
    if (chessBoard[0][0] != ' '
        && chessBoard[0][0] == chessBoard[1][1]
        && chessBoard[0][0] == chessBoard[2][2]) {
        return chessBoard[0][0];
    }
    if (chessBoard[2][0] != ' '
        && chessBoard[2][0] == chessBoard[1][1]
        && chessBoard[2][0] == chessBoard[0][2]) {
        return chessBoard[2][0];
    }
    if (isFull(chessBoard)) {
        return 'q';
    }
    return ' ';
}
void game(){
    char chessBoard[MAX_ROW][MAX_COL] = { 0 };
    init(chessBoard);
    char winner = ' ';
    while (1){
        system("cls");
        print_chessBoard(chessBoard);
        playerMove(chessBoard);
        winner = isWin(chessBoard);
        if (winner != ' ') {
            break;
        }
        computerMove(chessBoard);
        winner = isWin(chessBoard);
        if (winner != ' ') {
            break;
        }
    }
    print_chessBoard(chessBoard);
    if (winner == 'x') {
        printf("恭喜您, 您赢了!\n");
    }
    else if (winner == 'o') {
        printf("哈哈,您连人工智障都下不过!\n");
    }
    else {
        printf("您只能和人工智障打平手!!\n");
    }
}

int menu(){
    printf("--------------------------\n");
    printf("--------1.开始游戏--------\n");
    printf("--------0.退出游戏--------\n");
    printf("--------------------------\n");
    int choice = 0;
    printf("请输入你的选择:");
    scanf("%d", &choice);
    return choice;
}
int main()
{
    srand((unsigned int)time(0));
    while (1){
        int choice = menu();
        if (choice == 1){
            game();
        }
        else if (choice == 0){
            printf("退出游戏,GOODBYE!!!!!\n");
            break;
        }
        else{
            printf("输入错误!请重新输入!\n");
            continue;
        }
    }
    system("pause");
    return 0;
}

 

源代码结果展示:

 

 
 

 

 
 

 

 
 

 

 功能描述:该项目可以进行简单的人机三子棋游戏,由用户率先执棋,电脑则采用随机选取空余位置下棋的方式,并对三种战况分别作出了相应的结果,在代码中也设计了相应的菜单和棋盘矩阵布局结构。
 
一、源代码修改部分:
         (1)由于我是使用dev c++来进行源代码的修改和处理,在起初直接编译运行时出现差错,一些新的int类型数据放在for循环的()条件中进行定义,我无法直接进行成功编译,因此我便将int类拎到for循环外部进行定义,从而避免编译失败。
 
二、源代码增加部分:
         (1)在main函数中增添系统颜色变更,使系统更加美观。(标红处为增加代码)
int main()
{   system("color f0");
    srand((unsigned int)time(0));
    while (1){
        int choice = menu();
        if (choice == 1){
            game();
        }
        else if (choice == 0){
            printf("退出游戏,GOODBYE!!!!!\n");
            break;
        }
        else{
            printf("输入错误!请重新输入!\n");
            continue;
        }
    }
    system("pause");
    return 0;
}

         (2)由于先前的源码中,电脑下棋采用纯随机的方式,缺少难度,因此我在代码中加入了另俩项功能,代码稍显繁琐,是直接对矩阵进行逐个判断。功能一是判断电脑自己下一步是否能够取胜,能够取胜便下在制胜处,功能二是判断用户下一步是否能够取胜,若能够取胜便把棋堵在用户的下一步。

 

int computer_attack(char chessBoard[MAX_ROW][MAX_COL])
{
    int m = 0,i;
    for (i = 0; i <= 2; i++)
    {
        if (chessBoard[i][0] == chessBoard[i][1] && chessBoard[i][0] == 'o' && chessBoard[i][2] == ' ')
        {
            chessBoard[i][2] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[i][0] == chessBoard[i][2] && chessBoard[i][0] == 'o' && chessBoard[i][1] == ' ')
        {
            chessBoard[i][1] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[i][0] == chessBoard[i][2] && chessBoard[i][0] == 'o' && chessBoard[i][1] == ' ')
        {
            chessBoard[i][0] = 'o';
            m = 1;
            return m;
        }
    }
    for (i = 1; i <= 2; i++)
    {
        if (chessBoard[0][i] == chessBoard[1][i] && chessBoard[0][i] == 'o' && chessBoard[2][i] == ' ')
        {
            chessBoard[2][i] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[0][i] == chessBoard[2][i] && chessBoard[0][i] == 'o' && chessBoard[1][i] == ' ')
        {
            chessBoard[1][i] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[2][i] == chessBoard[1][i] && chessBoard[1][i] == 'o' && chessBoard[0][i] == ' ')
        {
            chessBoard[0][i] = 'o';
            m = 1;
            return m;
        }
    }
    if (chessBoard[0][0] == chessBoard[1][1] && chessBoard[0][0] == 'o' && chessBoard[2][2] == ' ')
    {
        chessBoard[2][2] = 'o';
        m = 1;
        return m;
    }
    if (chessBoard[0][0] == chessBoard[2][2] && chessBoard[0][0] == 'o' && chessBoard[1][1] == ' ')
    {
        chessBoard[1][1] = 'o';
        m = 1;
        return m;
    }
    if (chessBoard[1][1] == chessBoard[2][2] && chessBoard[1][1] == 'o' && chessBoard[0][0] == ' ')
    {
        chessBoard[0][0] = 'o';
        return m;
    }
    if (chessBoard[0][2] == chessBoard[1][1] && chessBoard[0][2] == 'o' && chessBoard[2][0] == ' ')
    {
        chessBoard[2][0] = 'o';
        m = 1;
        return m;
    }
    if (chessBoard[0][2] == chessBoard[2][0] && chessBoard[2][0] == 'o' && chessBoard[1][1] == ' ')
    {
        chessBoard[1][1] = 'o';
        m = 1;
        return m;
    }
    if (chessBoard[1][1] == chessBoard[2][0] && chessBoard[2][0] == 'o' && chessBoard[0][2] == ' ')
    {
        chessBoard[0][2] = 'o';
        m = 1;
        return m;
    }
    return m;
}
int computer_defend(char chessBoard[MAX_ROW][MAX_COL])
    {
        int m = 0,i;
        for (i = 0; i <= 2; i++)
        {
            if (chessBoard[i][0] == chessBoard[i][1] && chessBoard[i][0] == 'x' && chessBoard[i][2] == ' ')
            {
                chessBoard[i][2] = 'o';
                m = 1;
                return m;
            }
            if (chessBoard[i][0] == chessBoard[i][2] && chessBoard[i][2] == 'x' && chessBoard[i][1] == ' ')
            {
                chessBoard[i][1] = 'o';
                m = 1;
                return m;
            }
            if (chessBoard[i][1] == chessBoard[i][2] && chessBoard[i][1] == 'x' && chessBoard[i][0] == ' ')
            {
                chessBoard[i][0] = 'o';
                m = 1;
                return m;
            }
        }
        for (i = 0; i <= 2; i++)
        {
            if (chessBoard[0][i] == chessBoard[1][i] && chessBoard[1][i] == 'x' && chessBoard[2][i] == ' ')
            {
                chessBoard[2][i] = 'o';
                m = 1;
                return m;
            }
            if (chessBoard[0][i] == chessBoard[2][i] && chessBoard[2][i] == 'x' && chessBoard[1][i] == ' ')
            {
                chessBoard[1][i] = 'o';
                m = 1;
                return m;
            }
            if (chessBoard[2][i] == chessBoard[1][i] && chessBoard[1][i] == 'x' && chessBoard[0][i] == ' ')
            {
                chessBoard[0][i] = 'o';
                m = 1;
                return m;
            }
        }
        if (chessBoard[0][0] == chessBoard[1][1] && chessBoard[1][1] == 'x' && chessBoard[2][2] == ' ')
        {
            chessBoard[2][2] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[0][0] == chessBoard[2][2] && chessBoard[0][0] == 'x' && chessBoard[1][1] == ' ')
        {
            chessBoard[1][1] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[1][1] == chessBoard[2][2] && chessBoard[1][1] == 'x' && chessBoard[0][0] == ' ')
        {
            chessBoard[0][0] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[0][2] == chessBoard[1][1] && chessBoard[1][1] == 'x' && chessBoard[2][0] == ' ')
        {
            chessBoard[2][0] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[0][2] == chessBoard[2][0] && chessBoard[0][2] == 'x' && chessBoard[1][1] == ' ')
        {
            chessBoard[1][1] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[1][1] == chessBoard[2][0] && chessBoard[1][1] == 'x' && chessBoard[0][2] == ' ')
        {
            chessBoard[0][2] = 'o';
            m = 1;
            return m;
        }
        return m;
}

       

               在构建完俩个功能后,再对游戏运行的函数进行修改,把俩个条件判定语句加入game()函数中,替换掉原先单一的computerMove(chessBoard);语句。

        if(computer_attack(chessBoard)==0){
            if(computer_defend(chessBoard)==0){
                computerMove(chessBoard);
            }
        }

 

 三、总结:

                本次程序的优化与改进过程让我通过逆向思维来了解已完成的项目中函数各个都起到什么作用,个人水平有限,目前只能做到部分内容拓展,希望之后能学习到更多的知识。

 

四、修改后源代码:

#define _CRT_SECURE_NO_WARNINGS
#define MAX_ROW 3
#define MAX_COL 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void init(char chessBoard[MAX_ROW][MAX_COL]){
    int row,col;
    for ( row = 0; row < MAX_ROW; row++){
        for ( col = 0; col < MAX_COL; col++){
            chessBoard[row][col] = ' ';
        }
    }
}
void print_chessBoard(char chessBoard[MAX_ROW][MAX_COL]){
    printf("+---+---+---+\n");
    int row;
    for (row = 0; row <MAX_ROW; row++) {
        printf("| %c | %c | %c |\n", chessBoard[row][0],
            chessBoard[row][1], chessBoard[row][2]);
        printf("+---+---+---+\n");
    }
}
void playerMove(char chessBoard[MAX_ROW][MAX_COL]){
    while (1){
        int row = 0;
        int col = 0;
        printf("请输入坐标(row col):");
        scanf("%d %d", &row, &col);
        if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL){
            printf("您的坐标不在合法范围内 [0, 2],请重新输入:\n");
            continue;
        }
        if (chessBoard[row][col] != ' '){
            printf("您的坐标位置已经有子了!\n");
            continue;
        }
        chessBoard[row][col] = 'x';
        break;
    }
}
int computer_attack(char chessBoard[MAX_ROW][MAX_COL])
{
    int m = 0,i;
    for (i = 0; i <= 2; i++)
    {
        if (chessBoard[i][0] == chessBoard[i][1] && chessBoard[i][0] == 'o' && chessBoard[i][2] == ' ')
        {
            chessBoard[i][2] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[i][0] == chessBoard[i][2] && chessBoard[i][0] == 'o' && chessBoard[i][1] == ' ')
        {
            chessBoard[i][1] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[i][0] == chessBoard[i][2] && chessBoard[i][0] == 'o' && chessBoard[i][1] == ' ')
        {
            chessBoard[i][0] = 'o';
            m = 1;
            return m;
        }
    }
    for (i = 1; i <= 2; i++)
    {
        if (chessBoard[0][i] == chessBoard[1][i] && chessBoard[0][i] == 'o' && chessBoard[2][i] == ' ')
        {
            chessBoard[2][i] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[0][i] == chessBoard[2][i] && chessBoard[0][i] == 'o' && chessBoard[1][i] == ' ')
        {
            chessBoard[1][i] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[2][i] == chessBoard[1][i] && chessBoard[1][i] == 'o' && chessBoard[0][i] == ' ')
        {
            chessBoard[0][i] = 'o';
            m = 1;
            return m;
        }
    }
    if (chessBoard[0][0] == chessBoard[1][1] && chessBoard[0][0] == 'o' && chessBoard[2][2] == ' ')
    {
        chessBoard[2][2] = 'o';
        m = 1;
        return m;
    }
    if (chessBoard[0][0] == chessBoard[2][2] && chessBoard[0][0] == 'o' && chessBoard[1][1] == ' ')
    {
        chessBoard[1][1] = 'o';
        m = 1;
        return m;
    }
    if (chessBoard[1][1] == chessBoard[2][2] && chessBoard[1][1] == 'o' && chessBoard[0][0] == ' ')
    {
        chessBoard[0][0] = 'o';
        return m;
    }
    if (chessBoard[0][2] == chessBoard[1][1] && chessBoard[0][2] == 'o' && chessBoard[2][0] == ' ')
    {
        chessBoard[2][0] = 'o';
        m = 1;
        return m;
    }
    if (chessBoard[0][2] == chessBoard[2][0] && chessBoard[2][0] == 'o' && chessBoard[1][1] == ' ')
    {
        chessBoard[1][1] = 'o';
        m = 1;
        return m;
    }
    if (chessBoard[1][1] == chessBoard[2][0] && chessBoard[2][0] == 'o' && chessBoard[0][2] == ' ')
    {
        chessBoard[0][2] = 'o';
        m = 1;
        return m;
    }
    return m;
}
int computer_defend(char chessBoard[MAX_ROW][MAX_COL])
    {
        int m = 0,i;
        for (i = 0; i <= 2; i++)
        {
            if (chessBoard[i][0] == chessBoard[i][1] && chessBoard[i][0] == 'x' && chessBoard[i][2] == ' ')
            {
                chessBoard[i][2] = 'o';
                m = 1;
                return m;
            }
            if (chessBoard[i][0] == chessBoard[i][2] && chessBoard[i][2] == 'x' && chessBoard[i][1] == ' ')
            {
                chessBoard[i][1] = 'o';
                m = 1;
                return m;
            }
            if (chessBoard[i][1] == chessBoard[i][2] && chessBoard[i][1] == 'x' && chessBoard[i][0] == ' ')
            {
                chessBoard[i][0] = 'o';
                m = 1;
                return m;
            }
        }
        for (i = 0; i <= 2; i++)
        {
            if (chessBoard[0][i] == chessBoard[1][i] && chessBoard[1][i] == 'x' && chessBoard[2][i] == ' ')
            {
                chessBoard[2][i] = 'o';
                m = 1;
                return m;
            }
            if (chessBoard[0][i] == chessBoard[2][i] && chessBoard[2][i] == 'x' && chessBoard[1][i] == ' ')
            {
                chessBoard[1][i] = 'o';
                m = 1;
                return m;
            }
            if (chessBoard[2][i] == chessBoard[1][i] && chessBoard[1][i] == 'x' && chessBoard[0][i] == ' ')
            {
                chessBoard[0][i] = 'o';
                m = 1;
                return m;
            }
        }
        if (chessBoard[0][0] == chessBoard[1][1] && chessBoard[1][1] == 'x' && chessBoard[2][2] == ' ')
        {
            chessBoard[2][2] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[0][0] == chessBoard[2][2] && chessBoard[0][0] == 'x' && chessBoard[1][1] == ' ')
        {
            chessBoard[1][1] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[1][1] == chessBoard[2][2] && chessBoard[1][1] == 'x' && chessBoard[0][0] == ' ')
        {
            chessBoard[0][0] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[0][2] == chessBoard[1][1] && chessBoard[1][1] == 'x' && chessBoard[2][0] == ' ')
        {
            chessBoard[2][0] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[0][2] == chessBoard[2][0] && chessBoard[0][2] == 'x' && chessBoard[1][1] == ' ')
        {
            chessBoard[1][1] = 'o';
            m = 1;
            return m;
        }
        if (chessBoard[1][1] == chessBoard[2][0] && chessBoard[1][1] == 'x' && chessBoard[0][2] == ' ')
        {
            chessBoard[0][2] = 'o';
            m = 1;
            return m;
        }
        return m;
}
void computerMove(char chessBoard[MAX_ROW][MAX_COL]){
    while (1){
        int row = rand() % MAX_ROW;
        int col = rand() % MAX_COL;
        if (chessBoard[row][col] != ' ') {
            continue;
        }
        chessBoard[row][col] = 'o';
        break;
    }
}
int isFull(char chessBoard[MAX_ROW][MAX_COL]){
    int row,col;
    for ( row = 0; row < MAX_ROW; row++){
        for ( col = 0; col < MAX_COL; col++){
            if (chessBoard[row][col] == ' '){
                return 0;
            }

        }
    }
    return 1;
}
char isWin(char chessBoard[MAX_ROW][MAX_COL]){
    int row,col;
    for ( row = 0; row < MAX_ROW; row++) {
        if (chessBoard[row][0] != ' '
            && chessBoard[row][0] == chessBoard[row][1]
            && chessBoard[row][0] == chessBoard[row][2]) {
            return chessBoard[row][0];
        }
    }
    for ( col = 0; col < MAX_COL; col++) {
        if (chessBoard[0][col] != ' '
            && chessBoard[0][col] == chessBoard[1][col]
            && chessBoard[0][col] == chessBoard[2][col]) {
            return chessBoard[0][col];
        }
    }
    if (chessBoard[0][0] != ' '
        && chessBoard[0][0] == chessBoard[1][1]
        && chessBoard[0][0] == chessBoard[2][2]) {
        return chessBoard[0][0];
    }
    if (chessBoard[2][0] != ' '
        && chessBoard[2][0] == chessBoard[1][1]
        && chessBoard[2][0] == chessBoard[0][2]) {
        return chessBoard[2][0];
    }
    if (isFull(chessBoard)) {
        return 'q';
    }
    return ' ';
}
void game(){
    char chessBoard[MAX_ROW][MAX_COL] = { 0 };
    init(chessBoard);
    char winner = ' ';
    while (1){
        system("cls");
        print_chessBoard(chessBoard);
        playerMove(chessBoard);
        winner = isWin(chessBoard);
        if (winner != ' ') {
            break;
        }
        if(computer_attack(chessBoard)==0){
            if(computer_defend(chessBoard)==0){
                computerMove(chessBoard);
            }
        }
        winner = isWin(chessBoard);
        if (winner != ' ') {
            break;
        }
    }
    print_chessBoard(chessBoard);
    if (winner == 'x') {
        printf("恭喜您获胜!\n");
    }
    else if (winner == 'o') {
        printf("惜败!\n");
    }
    else {
        printf("平局!\n");
    }
}

int menu(){
    printf("--------------------------\n");
    printf("--------1.开始游戏--------\n");
    printf("--------0.退出游戏--------\n");
    printf("--------------------------\n");
    int choice = 0;
    printf("请输入你的选择:");
    scanf("%d", &choice);
    return choice;
}
int main()
{   system("color f0");
    srand((unsigned int)time(0));
    while (1){
        int choice = menu();
        if (choice == 1){
            game();
        }
        else if (choice == 0){
            printf("退出游戏,GOODBYE!!!!!\n");
            break;
        }
        else{
            printf("输入错误!请重新输入!\n");
            continue;
        }
    }
    system("pause");
    return 0;
}

 

结果展示:

 
注:在下完(0,0)(1,1)俩格后,电脑会直接堵在(2,2)格,防止用户直接简单取胜。

 

 

 

注:电脑在下完(2,0)(2,2)格后,若用户不去堵电脑的下一步,电脑会直接下在(2,1)格并取胜。

 

posted @ 2023-03-08 20:52  月光光心惶惶  阅读(104)  评论(0)    收藏  举报