三子棋(详解版)

  之前发布的“三子棋”那篇文章,总觉的不妥。我的同窗们也给我建议:“我既然是写博客,就应该力求细致,简洁明了。“所以今天就重发一篇详解版的三子棋,也算是分享一下自己当时的一些思路。

一、 棋盘的设计

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]){
  cout<<"+---+---+---+"<<endl;
  for (int row = 0; row <MAX_ROW; row++) {
    printf("| %c | %c | %c |\n", chessBoard[row][0],
      chessBoard[row][1], chessBoard[row][2]);
    cout<<"+---+---+---+"<<endl;
  }
}

 

棋盘的设计比较简单,没什么多说的主要得注意换行和符号之间的连接。

二 、落子的判定系统

void playerMove(char chessBoard[MAX_ROW][MAX_COL]) {
    while (1) {
        int row = 0;
        int col = 0;
        cout << "请输入坐标(row col):" << endl;
        cin >> row >> col;

        if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL)
        {
            cout << "您的棋都下到棋盘外了 请重新输入:";
            continue;
        }

        if (chessBoard[row][col] != ' ') {
            cout << "您的坐标位置已经有子了!" << endl;
            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;
    }


通过棋盘的范围来限制用户输入正确的坐标,如果落子位置有重复便会提醒用户。电脑落子我设成不超过棋盘规格的最大值。

三、胜负系统

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) {
        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') {
        cout<<"恭喜您, 您赢了!";
    }
    else if (winner == 'o') {
        cout<<"您连人工智障都下不过!(低情商) 您这局状态不好(高情商)!!"<<endl;
    }
    else {
        cout<<"你水平也就这样(低情商) 你居然能和AI达成平手(高情商)!!"<<endl;
    }
}

 

 

 

显示了三种可能的结果:赢,输,平局。通过isWin(chessBoard)函数的返回值来确实最后的赢家。

小结

  这里我并没有啰嗦的把每一句都注释,重点在于思路,知道需要那些模块,每个模块的功能是什么即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值