在井字游戏中需要for循环帮助

时间:2019-05-28 22:23:24

标签: c++ oop

我的程序没有迭代就退出了。它会自动转到“ YOU WON”。没有冠军功能,程序运行正常。它可能缺少一些明显的错误。如果有人可以,我将不胜感激。

#include <iostream>
#include <string>

#define GRID_SIZE 3

class TicTacToe {
private:
    char map[GRID_SIZE][GRID_SIZE];

public:

    void champion() {
        const char *possiblities[8]{
                "123"
                "456"
                "789"
                "147"
                "159"
                "258"
                "369"
                "753"
        };

        for (int i = 0; i < 8; i++) {
            bool winner = true;
            char previous_pos = '0';
            const char *possible_moves = possiblities[i];

            for (int index = 0; index < GRID_SIZE; index++) {
                char character = possible_moves[i];
                int entered_num = character - '0';
                int grid_space = entered_num - 1;
                int row = index / GRID_SIZE;
                int col = index % GRID_SIZE;

                char grid_coordinate = map[row][col];

                if (previous_pos == '0') {
                    previous_pos = grid_coordinate;
                } else if
                        (previous_pos == grid_coordinate) {
                    continue;
                } else {
                    winner = false;
                    break;
                }
            }
            if (winner = true) {
                std::cout << "YOU WON" << std::endl;
                exit(0);
                break;
            }

        }


    }

    void playgame() {
        std::string input;

        while (true) {
            std::cout << "Go player one" << std::endl;
            getline(std::cin, input);
            if (input != " ") {
                char entered = input.c_str()[0];

                if (entered >= '1' && entered <= '9') {
                    int entered_num = entered - '0';
                    int index = entered_num - 1;
                    int row = index / 3;
                    int col = index % 3;
                    char grid_position = map[row][col];

                    if (grid_position == 'X' || grid_position == 'O') {
                        std::cout << "Space taken. Try again" << std::endl;
                    } else {
                        map[row][col] = (char) 'X';
                        break;
                    }

                } else {
                    std::cout << "Only numbers 1 - 9" << std::endl;
                }
            } else {
                std::cout << "Have to enter something, try again" << std::endl;
            }


        }
    }

    void generateGrid() {
        int number = 1;

        for (int x = 0; x < GRID_SIZE; x++) {
            for (int y = 0; y < GRID_SIZE; y++) {
                map[x][y] = std::to_string(number).c_str()[0];
                number += 1;
            }
        }
    }

    void tictacToeMap() {


        std::cout << std::endl;

        for (int x = 0; x < GRID_SIZE; x++) {
            for (int y = 0; y < GRID_SIZE; y++) {
                std::printf(" %c ", map[x][y]);
            }
            std::cout << std::endl;
        }

    }


    TicTacToe() {
        generateGrid();
        while (true) {
            tictacToeMap();
            playgame();
            champion();


        }
    }
};


int main() {

    TicTacToe tic;
    tic.playgame();


    return 0;


}

2 个答案:

答案 0 :(得分:0)

问题在这里:

if (winner = true) {
                std::cout << "YOU WON" << std::endl;
                exit(0);
                break;
            }

您可能是说:

if (winner == true) {
                std::cout << "YOU WON" << std::endl;
                exit(0);
                break;
            }

答案 1 :(得分:0)

首先,按照Max Meijer所说的,将if(winner = true)替换为if(winner == true)。但是程序仍然坏了。问题是在您的字符串数组中,您没有用逗号分隔每个字符串,因此当我的调试器命中const char *possible_moves时,它最终只是分配了整个串联在一起的数组。因此,只需用逗号分隔可能性数组中的每个字符串,如下所示:

const char *possiblities[8]{
                "123",
                "456",
                "789",
                "147",
                "159",
                "258",
                "369",
                "753"
        };