Connect 4 Winning horizontal function - c++

I implemented this Connect 4 Winning horizontal function but I cannot for the life of me figure out why my c++ horizontal function will not work it is a 2d array
for (int j = 0; j < row - 3; j++) {
for (int i = 0; i < column; i++) {
if (Board[i][j] == charvar && Board[i][j + 1] == charvar && Board[i][j + 2] == charvar && dyBoard[i][j + 3] == charvar) {
return charvar;
}
}
}
Wouldnt my array go from [0][0] to [0][1] and so on? I already tried without the -3 but that dosnt seem to work.

The first array index is row, the second is column, so loops should look like this:
for (int i = 0; i < rowcount; i++) {
for (int j = 0; j < columncount - 3; j++) {...

Related

take 4*4 matrix from user to test if it includes 2*2 sub-matrix have the same value

i just need the user to enter 4*4 matrix of character, the out put will be yes or no according to if there is a 2 * 2 sub-matrix have the same input.
the code is always print false.
the code is:
#include <iostream>
using namespace std;
int main()
{
//input
char color[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cin >> color[i][j];
}
}
//for testing if there are a squar
// * * * *
// * * # #
// # # * *
// * * # #
// 'yes' as
// * *
// * *
// is a squar of 2*2
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (j != 3) {
if (color[i][j] == color[i][j + 1] == color[i + 1][j] == color[i + 1][j + 1]) {
cout << "yes";
break;
}
else cout << "no";
}
}
}
//for (int i = 0; i < 4; i++) {
//for (int j = 0; j < 4; j++) {
//if (j == 3)
//cout << colors[i][j] <<"\n";
//else
//cout << colors[i][j];
//}
//}
return 0;
}
This condition is wrong:
if (color[i][j] == color[i][j + 1] == color[i + 1][j] == color[i + 1][j + 1]
For simplicity, consider
if (a == b == c)
which is parsed as
if ( (a == b) == c)
The result of a==b is compared to c. The result of a==b is either true or false which can be converted to 1 or 0, respectively.
You would get "yes" printed on the screen when there is a 2x2 submatrix with all elements equal to 1, though it would be for the wrong reasons.
What you actually want is:
if ( a==b && a==c)
Do not ignore your compilers warnings! With the right flags, your code does not compile: https://godbolt.org/z/45oqcTEna.
Moreover, the loop goes outof bounds of the array. You prevent j going out of bounds but when i == 3 then color[i + 1][j] is trying to access an element that does not exist. Instead of iterating till <4 and then excluding j == 3, let the loop only iterate indices that are valid upper left corners of the submatrix:
const size_t size = 4;
const size_t sub_matrix_size = 1;
for (size_t i=0; i < size-sub_matrix_size; ++i) {
for (size_t j=0; j < size-sub_matrix_size; ++j) {
//...

C++ Access Violation - Possibly due to a table?

Within the main function I have the following.
int numRows = rowSequence.length() + 1;
int numCols = columnSequence.length() + 1;
int** twoDimTable = new int* [numRows];
for (int rowIndex = 0; rowIndex < numRows; rowIndex++)
{
twoDimTable[rowIndex] = new int [numCols];
}
//updating table
for (int i = 0; i <= numRows; i++)
{
for (int j = 0; j <= numCols; j++)
{
if (i == 0 || j == 0)
twoDimTable[i][j] = 0;
// when I start running my code I receive an unhandled exception right at the 'if'
// statement: Access violation writing location. I looked at other similar
// situations, but cannot seem to understand the specific issue
else if (rowSequence[i - 1] == columnSequence[j - 1])
twoDimTable[i][j] = twoDimTable[i - 1][j - 1] + 1;
else
twoDimTable[i][j] = max(twoDimTable[i - 1][j], twoDimTable[i][j - 1]);
}
}
One problem is that your for loops are wrong; numRows and numCols are not valid indices, so they should not be included in your iterations.
That is, instead of this:
for (int i = 0; i <= numRows; i++)
{
for (int j = 0; j <= numCols; j++)
{
[...]
}
}
... you should have this:
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
[...]
}
}

nqueens - checking with the board, instead of previous queens

There are so many questions regarding Nqueens problem here. However, my implementation is different. My code checks with the board if queen placement is possible, instead of checking with the position of the previous queen.
It goes like this:
initially, the board has all zeros filled. The algorithm starts with the position (0,0). Then, it checks
row-wise per column to find the first 0. After finding the first zero, it changes the zero to one.
From this point onward, my logic differs. Instead of going to the next column, it first disables all the
positions, which the currently placed queen attacks, i.e. writes -1 on those places, i.e., row, column,
upper diagonal and lower diagonal. Now, the column value increments, and instead of check with the previous queen,
it simply has to find the first zero. Then again, relative positions get disabled.... you get the idea.
The code:
#include <iostream>
int board[8][8];
void printBoard() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
std::cout << board[i][j] << " ";
}
std::cout << "\n";
}
}
void disablePositions(int row, int col) {
//disable row
for (int j = col + 1; j < 8; j++) {
board[row][j] = 2;
}
//disable column
for (int i = 0; i < 8; i++) {
if (board[i][col] == 1) {
continue;
}
board[i][col] = 2;
}
//disable upper diagonal
for (int i = row - 1, j = col + 1; i >= 0 || j < 8; i--, j++) {
board[i][j] = 2;
}
for (int i = row + 1, j = col + 1; i < 8 || j < 8; i++, j++) {
board[i][j] = 2;
}
}
void solve(int initial_row) {
int init = initial_row;
int row = 0;
int col = 0;
while (col != 8) {
for (row = init; row < 8; row++) {
if (board[row][col] == 0) {
board[row][col] = 1;
break;
}
}
if (row == 8) {
col = 0;
initial_row++;
init = initial_row;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = 0;
}
}
}
else {
init = 0;
disablePositions(row, col);
col++;
}
printBoard();
std::cout << std::endl;
}
}
int main() {
solve(0);
std::cout << std::endl;
}
This code is for 8-queens. The problem is, after it reaches the stage where it starts from [5][0], it just crashes. What is causing the issue?
Also, as it tries to make an optimal choice at every stage, would we call it greedy algorithm?
In your disable upper diagonal loops, you have the condition wrong. Using an || operation, the looping continues when either condition is true, which will lead to out-of-bounds access to the array.
Change the conditions in both for loops to be && (and).

looping through a 2D array (diagonal) c++

So I initialized an array as array[8][8] let's suppose that I'm at point (row, column) and for example, it is row 4 column 4 and I want to loop through every diagonal direction (southeast, southwest, northeast, northwest)
so I wrote 4 different functions to check each direction alone, and here is an example for Northeast
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
if(array[i - 1][j+1] == 'x')
{
count = count + 1;
}
is there is a way to loop in all diagonal directions at the same time?
another problem is what about getting out of bounds, like if the point is (7,7), then there will be no value in northeast because it will exceed the array bounds array[6][8], and that is out of array bounds. How can I deal with this problem? or does the compiler return an error when it happens?
You can of course check in each direction, e.g.
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
if (check_north_east(array, i, j))
++count;
if (check_north_west(array, i, j))
++count;
if (check_south_east(array, i, j))
++count;
if (check_south_west(array, i, j))
++count;
}
}
The compiler will happily go beyond the array bounds. So you must make sure, the code won't do it, and check yourself
const int NROWS = 8, NCOLS = 8;
bool check_north_east(char array[][NCOLS], int row, int col)
{
if (row <= 0 || col >= NCOLS - 1)
return false;
return array[row - 1][col + 1] == 'x';
}

Testing a 2d Array for Percolation (union find)

I have a 2d array created of size n that holds 1s and 0s representing closed and open spaces respectively.
Now I need to test the 2d array to see if it percolates and I'm not sure how to go about this.
I have the following code for creating the array and randomly assigning each spot to a 1 or 0.
int** grid = new int*[boardSize];
for (int i = 0; i < boardSize; ++i) {
grid[i] = new int[boardSize];
}
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
if (i == 0) {
grid[i][j] = 1;
}
else if (i == boardSize - 1) {
grid[i][j] = 1;
}
else if (j == 0) {
grid[i][j] = 1;
}
else if (j == boardSize - 1) {
grid[i][j] = 1;
}
else
grid[i][j] = random(delta);
}
}
grid[0][enter] = 0;
grid[boardSize - 1][exit] = 0;
This will create an array with closed borders (1s) and put 2 random entry/exit points (0s) on the top and bottom. Only part I'm missing is to test for percolation.
Any help is appreciated, thanks!