使用 C++ 在终极井字游戏中显示 3x3 网格

问题描述 投票:0回答:1

我试图让我的代码显示为 tic tac toe 游戏的 3x3 网格,但它只会显示在单列中,我知道此代码还会存在其他问题,但只会努力让它正确显示 atm,谢谢。 这是我的代码

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#include "TicTacToe.h"
int main() {
    NBTicTacToe game;
    game.displayBoards();
    return 0;
}

这是主要功能,大部分代码位于 NBTicTacToe 下的底部

#ifndef TICTACTOE_H_
#define TICTACTOE_H_
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int BOARDSIZE = 3;
class TicTacToe {
private:
    int board[BOARDSIZE][BOARDSIZE];
    int noOfMoves;
public:
    TicTacToe();
    bool isValidMove(int, int);
    bool getXMove(int&, int&);
    bool getOMove(int&, int&);
    void addMove(int, int, int);
    int gameStatus();
    int play();
    void displayBoard();
};
class NBTicTacToe {
    private:
        TicTacToe boards[BOARDSIZE][BOARDSIZE];
        int currentBoardX;
        int currentBoardY;
    public:
        NBTicTacToe();
        void displayBoards();
        TicTacToe& getCurrentBoard();
        void setCurrentBoard(int, int);
        void playCurrentBoard();

};
TicTacToe::TicTacToe() {
    for (int row = 0; row < 3; row++)
        for (int col = 0; col < 3; col++)
            board[row][col] = 0;
    noOfMoves = 0;
}
void TicTacToe::displayBoard() {
    cout << "   1    2    3" << endl << endl;
    for (int i = 0; i < BOARDSIZE; i++) {
        cout << i + 1;
        for (int j = 0; j < BOARDSIZE; j++) {
            char playerSymbol = ' ';
            if (board[i][j] == 1)
                playerSymbol = 'X';
            else if (board[i][j] == -1)
                playerSymbol = 'O';
            cout << setw(3) << playerSymbol;
            if (j != BOARDSIZE -1)
                cout << " |";
        }
        cout << endl;
        if (i != BOARDSIZE -1)
            cout << " ____|____|____" << endl << "     |    |    " << endl;
    }
    cout << endl;
}
bool TicTacToe::isValidMove(int x, int y) {
    if (board[x][y] == 1 || board[x][y] == -1){
        cout << "Please enter a valid move: \n";
    }
    else {
        return true;
    }
}
bool TicTacToe::getXMove(int &x, int &y) {
    if (noOfMoves >= BOARDSIZE * BOARDSIZE)
        return false;
    int row, col;
    do {
        row = rand() % BOARDSIZE;
        col = rand() % BOARDSIZE;
    } while (!isValidMove(row, col));
    x = row;
    y = col;
    return true;
}
bool TicTacToe::getOMove(int &x, int &y) {
    if (noOfMoves >= BOARDSIZE * BOARDSIZE)
        return false;
    int row, col;
    do {
        cin >> row >> col;
        cout << endl;
    } while (!isValidMove(row - 1, col - 1));
    x = row - 1;
    y = col - 1;
    return true;
}
void TicTacToe::addMove(int x, int y, int player) {
    board[x][y] = player;
}
int TicTacToe::gameStatus() {
//Check rows for a win
for (int i = 0; i < 3; i ++){
    if (board[i][0] == 1 && board[i][1] == 1 && board[i][2] == 1) {
        return 1;
    }else if (board[i][0] == -1 && board[i][1] == -1 && board[i][2] == -1){
        return -1;
    }
}
//Check columns for a win
for (int i = 0; i < 3; i ++){
    if (board[0][i] == 1 && board[1][i] == 1 && board[2][i] == 1) {
        return 1;
    }else if (board[0][i] == -1 && board[1][i] == -1 && board[2][i] == -1){
        return -1;
    }
}
//Check diagonals for a win
if (board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1) {
        return 1;
    }else if (board[0][2] == 1 && board[1][1] == 1 && board[2][0] == 1){
        return 1;
    }else if (board[0][0] == -1 && board[1][1] == -1 && board[2][2] == -1) {
        return -1;
    }else if (board[0][2] == -1 && board[1][1] == -1 && board[2][0] == -1){
        return -1;
    }
//check for Draw
bool draw = true;
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (board[i][j] == 0) {
            draw = false;
            break;
        }
    }
}
    if (draw)
        return 2;
    return 0;
}
int TicTacToe::play() {
    int player = 1;
    displayBoard();
    int done = 0;
    while (done == 0) {
        int x, y;
        if (player == 1) {
            cout << "player X is moving: \n";
            getXMove(x, y);
        } else {
            cout << "please enter your move: ";
            getOMove(x, y);
        }
        addMove(x, y, player);
        noOfMoves++;
        displayBoard();
        done = gameStatus();
        if (done == 1) {
            cout << "Player X wins!" << endl;
            return 1;
        } else if (done == -1) {
            cout << "Player O wins!" << endl;
            return -1;
        } else if (done == 2) {
            cout << "Draw game!" << endl;
            return 0;
        }
        if (player == 1)
            player = -1;
        else
            player = 1;
    }
    return 0;
}
NBTicTacToe::NBTicTacToe() : currentBoardX(0), currentBoardY(0) {
    for(int i = 0; i < BOARDSIZE; i++) {
        for(int j = 0; j < BOARDSIZE; j++) {
            boards[i][j] = TicTacToe();
        }
    }
}
void NBTicTacToe::displayBoards() {
    for (int r =0; r < BOARDSIZE; r++){// display rows
        for (int c =0; c < BOARDSIZE; c++){ 
            boards[r][c].displayBoard();
            if (c <BOARDSIZE) {
            cout << " - - - - - - - - \n" ;
            }
        }
        if (r < BOARDSIZE){
            for (int c = 0; c < BOARDSIZE; c++){
                if (c < BOARDSIZE) {
                    cout << " || ";
                }
            }
            cout << endl;
        }
    }
}
TicTacToe& NBTicTacToe::getCurrentBoard() {
    return boards[currentBoardX][currentBoardY];
}
void NBTicTacToe::setCurrentBoard(int x, int y) {
    currentBoardX = x;
    currentBoardY = y;
}
void NBTicTacToe::playCurrentBoard() {
    getCurrentBoard().play();
}
#endif

应该得到与此接近的东西: 终极井字棋

c++ oop tic-tac-toe
1个回答
0
投票

我建议的一种方法是处理字符串。

使类

TicTacToe
有一个函数“to_strings”,它返回
std::array
std::vector
s 的
std::string

然后在

display_board
函数中,您可以获得前 3 个 TicTacToe 棋盘,然后显示以 Enter 结尾的第一行。 然后是接下来的 3 个板,依此类推。

例如:

void display_boards()
{
    for (auto& boards_row : boards)
    {
        auto board1 = boards_row[0].to_strings();
        auto board2 = boards_row[1].to_strings();
        auto board3 = boards_row[2].to_strings();

        for (int line_idx = 0; line_idx < board1.size(); ++line_idx)
        {
            std::cout << board1[line_idx] << board2[line_idx] << board3[line_idx] << '\n';
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.