Win conditions for a connect-4 like game

后端 未结 3 1622
时光说笑
时光说笑 2021-01-06 16:48

I have an 5x10 array that is populated with random values 1-5. I want to be able to check when 3 numbers, either horizontally, or vertically, match. I can\'t figure out a wa

3条回答
  •  礼貌的吻别
    2021-01-06 17:18

    for the record:

    I think you mean

    for(i = 0; i < 5; i++)
    {
        board[row][i] = rand()%5 + 1; 
        cout << board[row][i] << " ";
    }
    

    And since others posted Code, here is how I would do it :

    for(int i = 0 ; i < 8 ; i++)
    {
        for(int j = 0 ; j < 3 ; j++)
        {
    
            if( ((board[i][j] == board[i][j+1]) && (board[i][j+1] == board[i][j+2])))
                std::cout << "Found a horizontal match on " << i << " " << j << std::endl;
    
            if((board[i][j] == board[i+1][j]) && (board[i+1][j] == board[i+2][j]))
                std::cout << "Found a vertical match on " << i << " " << j << std::endl;
        }
    }
    

提交回复
热议问题