簡體   English   中英

為什么我不能更改 C++ 中的多維 char 數組的值?

[英]Why can't I change the value of a multidimensional char array in C++?

在觀看 Coding Train 使用 JavaScript 在他的視頻中制作之后,我正在 C++ 中制作井字游戲。 我對編程並不陌生,但我對 C++ 很陌生。

當我嘗試更改 2D char 變量時出現了問題。

#include <iostream>
#include <string>
#include <vector>

constexpr unsigned int board_x = 3, board_y = 3;
const std::string divider = "--------------------------------------------------";

char board[board_x][board_y]
{
    {'-', '-', '-'},
    {'-', '-', '-'},
    {'-', '-', '-'}
};

std::vector<std::string> available_locations{ "A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3" };

bool is_location_valid(const std::string&);
void change_board(std::string, unsigned short);
void print_available_locations();
void print_board();
void switch_turn(unsigned short&);
void take_turn(unsigned short&, std::string&);

int main()
{
    unsigned short player_turn = 1;
    std::string user_input;

    print_available_locations();
    take_turn(player_turn, user_input);
}

bool is_location_valid(const std::string& input)
{
    for (std::string& available_location : available_locations)
    {
        if (available_location == input) return true;
    }

    std::cout << "Unknown location" << std::endl << std::endl;
    return false;
}

void change_board(std::string input, const unsigned short player_turn)
{
    if (input.find('A') == 0) input[0] = '1';
    else if (input.find('B') == 0) input[0] = '2';
    else if (input.find('C') == 0) input[0] = '3';

    board[static_cast<int>(input[0]) - 1][static_cast<int>(input[1]) - 1] = player_turn == 1 ? 'X' : 'O';
}

void print_available_locations()
{
    std::cout << "Available location: ";
    for (unsigned int i = 0; i < available_locations.size(); i++)
    {
        if (i != available_locations.size() - 1) std::cout << available_locations[i] << ", ";
        else std::cout << available_locations[i] << ".\n";
    }
}

void print_board()
{
    for (char(&i)[3] : board)
    {
        for (char j : i)
        {
            std::cout << j << "\t";
        }

        std::cout << std::endl;
    }
}

void switch_turn(unsigned short& player_turn)
{
    switch (player_turn)
    {
        case 1:
            player_turn = 2;
            break;
        case 2:
            player_turn = 1;
            break;
        default:
            player_turn = 1;
            break;
    }
}

void take_turn(unsigned short& player_turn, std::string& user_input)
{
    do
    {
        std::cout << "Player " << player_turn << ", enter board location...\n";
        std::getline(std::cin, user_input);
    } while (!is_location_valid(user_input));

    change_board(user_input, player_turn);
    print_board();
    std::cout << divider << std::endl;
    switch_turn(player_turn);
}

我不知道為什么,但是board[static_cast<int>(input[0]) - 1][static_cast<int>(input[1]) - 1] = player_turn == 1? 'X': 'O'; board[static_cast<int>(input[0]) - 1][static_cast<int>(input[1]) - 1] = player_turn == 1? 'X': 'O'; 似乎沒有改變頂部的board變量。 我應該添加或更改什么才能使其正常工作?

您似乎認為static_cast<int>('1')是 1。事實並非如此。 字符由 integer 值編碼,無論您的系統使用何種編碼,用於表示字符“1”的值都不是 1。

在普遍存在的 ASCII 編碼中,“1”由值 49 表示。索引 49-1 即 48 超出了數組的范圍。

一種可能的解決方法:

input[0] = 1

替代修復:

board[input[0] - '1']

這些修復程序是專有的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM