猫史档案馆


【C++代码分享】井字棋(English)

用户:ofetofet查看:0 回复:0 评论:0 创建时间:2022-08-02T23:04:15


直接上代码:

#include <iostream>
using namespace std;

#define O 'O'
#define X 'X'
#define N ' '

class Board {
    public:
        char board[3][3];
        void init();
        char judge();
        void print();
        bool step(int i, int j, char str);
};
void Board::init() {
    int i, j;
    for(i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++) {
            board[i][j] = N;
        }
    }
}
char Board::judge() {
    int i;
    for(i = 0; i < 3; i++) {
        if((board[i][0] == board[i][1] && board[i][1] == board[i][2]) || (board[0][i] == board[1][i] && board[1][i]== board[2][i])) {
            return board[i][i];
        }
    }
    if((board[0][0] == board[1][1] && board[1][1] == board[2][2]) || (board[0][2] == board[1][1] && board[1][1] == board[2][0])) {
        return board[1][1];
    }
    return N;
}
void Board::print() {
    int i, j;
    string line;
    cout << "-------" << endl;
    for(i = 0; i < 3; i++) {
        cout << "|";
        for(j = 0; j < 3; j++) {
            cout << board[i][j]<< "|";
        }
        cout << "\n-------" << endl;
    }
}
bool Board::step(int i, int j, char str) {
    int _i, _j;
    if(j != 0) {
        _i = i - 1;
        _j = j - 1;
    }else {
        _i = (i - 1) / 3;
        _j = (i - 1) % 3;
    }
    if(board[_i][_j] == N) {
        board[_i][_j] = str;
        return true;
    }else {
        return false;
    }
}
Board board{};
void input(char grid) {
    int i, j;
    char id[4];
    cout << "Please player " << grid << " input the grid of " << grid << ":";
    cin >> id;
    if(id[1] == '-') {
        i = (int) id[0] - 48;
        j = (int) id[2] - 48;
    }else {
        i = (int) id[0] - 48;
        j = 0;
    }
    if (!board.step(i, j, grid)) {
        cout << "Input a empty grid!" << endl;
        input(grid);
    }
}
char _main() {
    board.init();
    board.print();
    int k;
    char grid;
    char win;
    for(k = 0; k < 9; k++) {
        grid = k % 2 == 0?O:X;
        input(grid);
        board.print();
        win = board.judge();
        if(win != N) {
            cout << "player " << win << " wins!" << endl;
            break;
        }
    }
    if(win == N) {
        cout << "No one wins.";
    }
    return win;
}
int main() {
    int games;
    int O_wins = 0,X_wins = 0;
    char win;
    cout << "TIC TAC TOE" << endl << "You can input the row number and column number,or the number from top to bottom and left to right,to step." << endl;
    cout << "Please input the number of games.";
    cin >> games;
    int i;
    char answer[17];
    bool ask = true;
    cout << "You can enter \"row number - column number\" or \"top to bottom left to right sequence number\" at a time to play in a grid." << endl;
    for(i = 0; i < games; i++) {
        cout << "Now it's the " << (i + 1) << "'th game." << endl;
        win = _main();
        if(win == O) {
            O_wins += 1;
        }else if(win == X){
            X_wins += 1;
        }
        cout << "Now,player O wins " << O_wins << " times,and player X wins " << X_wins << "times.";
        if(ask && O_wins > games /2) {
            cout << "We have knew that player will win.Break?\"yes\",\"no\" or \"don't ask again.\"";
            cin >> answer;
            if(answer == "yes") {
                break;
            }else if(answer == "don't ask again.") {
                ask = false;
            }
        }
    }
    if(O_wins > X_wins) {
        cout << "At last,player O wins!" << endl;
    }else if(X_wins > O_wins) {
        cout << "At last,player X wins!" << endl;
    }else {
        cout << "At last,no one wins." << endl;
    }
}


回复

上一页1 页 / 共 0下一页