C语言实现井字棋小游戏

改文章为C语言实现井字棋小游戏的完整代码,可供大家自行参考喔

完整代码如下:

/*井字棋小游戏*/
#include<stdio.h>
#include<string.h>
#define ROW 3
#define COL 3
char chessBoard[ROW][COL]={0};
void menu()
{
    printf("*******************************************\n");
    printf("******       1.play     0.exit      *******\n");
    printf("*******************************************\n");
}

int begin(){
    int input=0;
    int flag=-1;
    do{
        menu();
        printf("请选择->");
        scanf("%d",&input);
        switch(input){
            case 1:
                printf("井字棋\n");
                flag=1;
                break;
            case 0:
                printf("退出游戏!\n");
                flag=0;
                break;
            default:
                printf("选择错误,请重新选择\n");
            //    break;
        }
    }while(input!=1 && input!=0);
    return flag;
}

void initchessBoard(char chessBoard[ROW][COL]){                    //初始化棋盘 
    for(int row=0;row<ROW;row++){
        for(int col=0;col<COL;col++){
            chessBoard[row][col]=' ';
        }
    }
}

void printchessBoard(){
    printf("\t+---+---+---+\n");
    for(int row=0;row<ROW;row++){
        printf("\t| %c | %c | %c |\n",chessBoard[row][0],chessBoard[row][1],chessBoard[row][2]);
        printf("\t+---+---+---+\n");
    }
    
}

void xplay(){
    while(1){
        int row=0;
        int col=0;
        printf("x玩家请下棋:");
        scanf("%d%d",&row,&col);
        if(row<0 || row>=ROW || col<0 || col>=COL){
            printf("您输入的坐标不合法!,请重新输入\n");
            continue;
        } 
        if(chessBoard[row][col]!=' '){
            printf("该位置已有棋子!\n"); 
            continue;
        }
        chessBoard[row][col]='x';
        break;
    }
    printchessBoard();
}


void oplay(){
    while(1){
        int row=0;
        int col=0;
        printf("o玩家请下棋:");
        scanf("%d%d",&row,&col);
        if(row<0 || row>=ROW || col<0 || col>=COL){
            printf("您输入的坐标不合法!,请重新输入\n");
            continue;
        } 
        if(chessBoard[row][col]!=' '){
            printf("该位置已有棋子!\n"); 
            continue;
        }
        chessBoard[row][col]='o';
        break;
    }
    printchessBoard();
}

int isfull(char chessBoard[ROW][COL]){
    int flag=1;
    for(int row=0;row<ROW;row++){
        for(int col;col<COL;col++){
            if(chessBoard[ROW][COL]==' '){
                flag=0;
            }
        }
    }
    return flag;     //如果返回1则棋盘已经下满,如果返回0则表示棋盘还未下满 
}

int iswin(char chessBoard[ROW][COL])
{
    int col,row;
    int win=2;
    for(int row=0;row<ROW;row++){
        if(chessBoard[row][0]!=' ' && chessBoard[row][0]==chessBoard[row][1] && chessBoard[row][1]==chessBoard[row][2]){
            if(chessBoard[row][0]=='x'){
                win=1;          //返回1表示x玩家赢  
            }
            if(chessBoard[row][0]=='o'){
                win=0;
            }
            return win;  
        } 
    } 
    
    for(int col=0;col<COL;col++){
        if(chessBoard[0][col]!=' ' && chessBoard[0][col]==chessBoard[1][col] && chessBoard[1][col]==chessBoard[2][col]){
            if(chessBoard[0][col]=='x'){
                win=1;
            }
            if(chessBoard[0][col]=='o'){
                win=0; 
            }
            return win;
        } 
    }
    if(chessBoard[0][0]!=' ' && chessBoard[0][0]==chessBoard[1][1] && chessBoard[1][1]==chessBoard[2][2]){
            if(chessBoard[0][col]=='x'){
                win=1;
            }
            if(chessBoard[0][col]=='o'){
                win=0;
            }
            return win;
    }
    if(chessBoard[2][0]!=' ' && chessBoard[2][0]==chessBoard[1][1] && chessBoard[1][1]==chessBoard[0][2]){
        if(chessBoard[0][col]=='x'){
            win=1;
        }
        if(chessBoard[0][col]=='o'){
            win=0;
        }
        return win;
    }
    if(isfull(chessBoard)){
        return win;
    }
 
}

void judge(){
    do{
        xplay();
        if(iswin(chessBoard)==1){
            printf("\n\t恭喜x玩家获胜!");
            break;
        }
        if(iswin(chessBoard)==0){
            printf("\n\t恭喜o玩家获胜!");
            break; 
        }
        oplay();
        if(iswin(chessBoard)==1){
            printf("\n\t恭喜x玩家获胜!");
            break;
        }
        if(iswin(chessBoard)==0){
            printf("\n\t恭喜o玩家获胜!");
            break; 
        }
    }while(iswin(chessBoard)==2);
}

int main(){
    int xflag;
     xflag=begin();
     if(xflag==1){
     initchessBoard(chessBoard);    //初始化棋盘
    printchessBoard();           //打印棋盘形状
    judge(); 
}

    return 0;
}

运行结果:

谢谢大家咯,如有改进欢迎指点喔

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是一个简单的井字棋的C语言实现: ``` #include <stdio.h> // 定义棋盘大小 #define ROWS 3 #define COLS 3 // 定义棋子类型 #define EMPTY 0 #define X 1 #define O 2 // 初始化棋盘 void init_board(int board[][COLS]) { int i, j; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { board[i][j] = EMPTY; } } } // 打印棋盘 void print_board(int board[][COLS]) { int i, j; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { if (board[i][j] == X) { printf("X "); } else if (board[i][j] == O) { printf("O "); } else { printf("- "); } } printf("\n"); } } // 判断是否有玩家获胜 int check_win(int board[][COLS], int player) { int i, j; for (i = 0; i < ROWS; i++) { if (board[i][0] == player && board[i][1] == player && board[i][2] == player) { return 1; } } for (j = 0; j < COLS; j++) { if (board[0][j] == player && board[1][j] == player && board[2][j] == player) { return 1; } } if (board[0][0] == player && board[1][1] == player && board[2][2] == player) { return 1; } if (board[0][2] == player && board[1][1] == player && board[2][0] == player) { return 1; } return 0; } // 判断是否平局 int check_tie(int board[][COLS]) { int i, j; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { if (board[i][j] == EMPTY) { return 0; } } } return 1; } // 玩家下棋 void player_move(int board[][COLS]) { int row, col; printf("Enter row and column (1-3): "); scanf("%d %d", &row, &col); row--; col--; if (row < 0 || row >= ROWS || col < 0 || col >= COLS || board[row][col] != EMPTY) { printf("Invalid move. Try again.\n"); player_move(board); } else { board[row][col] = X; } } // AI下棋 void ai_move(int board[][COLS]) { int row, col; printf("AI's turn:\n"); // 简单的AI策略:随机下一个可行的位置 do { row = rand() % ROWS; col = rand() % COLS; } while (board[row][col] != EMPTY); board[row][col] = O; } int main() { int board[ROWS][COLS]; init_board(board); printf("Welcome to Tic-Tac-Toe!\n"); printf("You are playing as X, AI is playing as O.\n"); printf("Enter row and column numbers to make your move.\n"); printf("Here is the initial board:\n"); print_board(board); while (1) { player_move(board); print_board(board); if (check_win(board, X)) { printf("Congratulations! You win!\n"); break; } if (check_tie(board)) { printf("It's a tie!\n"); break; } ai_move(board); print_board(board); if (check_win(board, O)) { printf("AI wins! Better luck next time.\n"); break; } if (check_tie(board)) { printf("It's a tie!\n"); break; } } return 0; } ``` 这个程序实现了一个简单的井字棋游戏,玩家使用 X 表示,AI使用 O 表示。玩家和AI轮流下棋,第一个在一行、一列或一条对角线上连成三个棋子的人获胜,如果棋盘填满了但没有人获胜,则为平局。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值