将if else语句转换为开关

时间:2019-05-27 18:09:23

标签: c++

在将if-else语句转换为开关时遇到问题。我正在用C ++创建井字游戏。我不确定如何在开关本身内使用2D阵列矩阵。本练习的目的是用于单独切换。下面是我到目前为止编写代码的代码。

switch (a)
{
case 1:
    if(matrix[0][0] == '1')
    matrix[0][0] = player_1;

    else
    {
        cout << "Field is already in use. Try again" << endl;
        input(); //Calling function input to prompt user to key in value again and again
    }
    break;

case 2:
    if(matrix[0][1] == '2')
    matrix[0][1] = player_1;
    else {

        cout << "Field is already in use. Try again" << endl;
        input(); //Calling function input to prompt user to key in value again and again
    }
    break;
case 3:
    if(matrix[0][2] == '3')
    matrix[0][2] = player_1;
    else {

        cout << "Field is already in use. Try again" << endl;
        input(); //Calling function input to prompt user to key in value again and again
    }
    break;
case 4:
    if(matrix[1][0] == '4')
    matrix[1][0] = player_1;
    else {

        cout << "Field is already in use. Try again" << endl;
        input(); //Calling function input to prompt user to key in value again and again
    }
    break;
case 5:
    if(matrix[1][1] == '5')
    matrix[1][1] = player_1;
    else {

        cout << "Field is already in use. Try again" << endl;
        input();//Calling function input to prompt user to key in value again and again
    }
    break;
case 6:
    if(matrix[1][2] == '6')
    matrix[1][2] = player_1;
    else {

        cout << "Field is already in use. Try again" << endl;
        input();//Calling function input to prompt user to key in value again and again
    }
    break;
case 7:
    if(matrix[2][0] == '7')
    matrix[2][0] = player_1;
    else {

        cout << "Field is already in use.Try again" << endl;
        input();//Calling function input to prompt user to key in value again and again
    }
    break;
case 8:
    if(matrix[2][1] == '8')
    matrix[2][1] = player_1; 
    else {

        cout << "Field is already in use. Try again" << endl;
        input();//Calling function input to prompt user to key in value again and again
    }
    break;
case 9:
    if(matrix[2][2] == '9')
    matrix[2][2] = player_1;

    else {

        cout << "Field is already in use. Try again" << endl;
        input();//Calling function input to prompt user to key in value again and again
    }
        break;
default:
    cout << "Number inputted doesn't exist. Please try again" << endl;
    input(); //Calling function input to prompt user to key in value again and again
}

1 个答案:

答案 0 :(得分:0)

如果必须使用switch,则可以通过仅使用switch计算位置(行和列)来简化代码:

int row = 0;
int column = 0;
bool position_is_valid = true;
switch (a)
{
    case 1:  row = 0; column = 0; break;
    case 2:  row = 0; column = 1; break;
    case 3:  row = 0; column = 2; break;
    case 4:  row = 1; column = 0; break;
    case 5:  row = 1; column = 1; break;
    case 6:  row = 1; column = 2; break;
    case 7:  row = 2; column = 0; break;
    case 8:  row = 2; column = 1; break;
    case 9:  row = 2; column = 2; break;
    default: position_is_valid = false; break;
}
if (position_is_valid)
{
    if (matrix[row][column]) == '0' + a)
    {
        matrix[row][column] = player1;
    }
    else
    {
        std::cout << "Position is not empty.\n";
    }
}
else
{
    std::cout << "Position is invalid.\n";
}

通常,如果您重复代码或查看模式,则可以简化代码或简化设计。

编辑1:switch中的更多代码
您可以将空框字符的计算结果添加到开关中:

    char empty_field_value = ' ';
    switch (a)
    {
        case 1:  row = 0; column = 0; empty_field_value = '1'; break;
        case 2:  row = 0; column = 1; empty_field_value = '2'; break;
        case 3:  row = 0; column = 2; empty_field_value = '3'; break;
        case 4:  row = 1; column = 0; empty_field_value = '4'; break;
        case 5:  row = 1; column = 1; empty_field_value = '5'; break;
        case 6:  row = 1; column = 2; empty_field_value = '6'; break;
        case 7:  row = 2; column = 0; empty_field_value = '7'; break;
        case 8:  row = 2; column = 1; empty_field_value = '8'; break;
        case 9:  row = 2; column = 2; empty_field_value = '9'; break;
        default: position_is_valid = false; break;
    }
    if (position_is_valid)
    {
        if (matrix[row][column]) == empty_field_value)
        {
            matrix[row][column] = player1;
        }
        else
        {
            std::cout << "Position is not empty.\n";
        }
    }
    else
    {
        std::cout << "Position is invalid.\n";
    }

我建议不要将if语句放在switch中,因为它们不依赖于switch值。