使C ++程序发挥四分之一,数组元素有一个问题

时间:2015-03-07 02:15:04

标签: c++ arrays

这些函数特别是我遇到的问题,Piece是在文件中其他地方实现的结构,只包含一个名为“name”的4个字符的字符串。我正在尝试获取我的代码,以便它可以连续比较4个(任何方向),通过逐个比较它们的4个字符。编译时出现的错误表明它无法将我输入的字符串转换为需要进一步操作的字符串。

string comparePieces(string a, string b, string c, string d)
{
    string sharedTrait;

    if(a[0] == b[0] && a[0] == c[0] && a[0] == d[0])
        sharedTrait = "true";
    else if(a[1] == b[1] && a[1] == c[1] && a[1] == d[1])
        sharedTrait = "true";
    else if(a[2] == b[2] && a[2] == c[2] && a[2] == d[2])
        sharedTrait = "true";
    else if(a[3] == b[3] && a[3] == c[3] && a[3] == d[3])
        sharedTrait = "true";
    else
        sharedTrait = "false";

    return sharedTrait;
}

string getSpace(string space)
{
    int r;
    int c;
    space == board[r][c].name;
    return space;
}

string checkWinner()
{
    string win = "false";
    for(int i = 0; i<= 4; i++)
    {
        int r = i;
        if( comparePieces(  (getSpace(board[r][0].name)) ,  (getSpace(board[r][1].name)) , (getSpace(board[r][2].name)) , (getSpace(board[r][3].name)) ) == "true")
            win = "true";
        if( comparePieces(  (getSpace(board[0][r].name)) ,  (getSpace(board[1][r].name)) , (getSpace(board[2][r].name)) , (getSpace(board[3][r].name)) ) == "true")
            win = "true";
        if( comparePieces(  (getSpace(board[0][0].name)) ,  (getSpace(board[1][1].name)) , (getSpace(board[2][2].name)) , (getSpace(board[3][3].name)) ) == "true")
            win = "true";
        if( comparePieces(  (getSpace(board[0][3].name)) ,  (getSpace(board[1][2].name)) , (getSpace(board[2][1].name)) , (getSpace(board[3][0].name)) ) == "true")
            win = "true";
    }
    return win;
}

0 个答案:

没有答案