Program to move box in a jar - what is wrong?
I am writing this program to move a box in a jar(a larger array). First, I overlap the box into the array, then I print the array and attempt to move it. But for some reason, the moving part of the code only works when the box is set entirely to true and not for the set up I have now. Can anyone see what I am doing wrong? - the code compiles, so this should help visualizing what the code is about.
Code:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
#define MAX_HEIGHT 20
#define MAX_WIDTH 10
#define MAX_SIZE 4
void main()
{
int k =0;
int l =0;
int firstRow = 0; //these will come as parameters
int firstCol = 3;
//------------------------For testing-------------------//
bool jar[MAX_HEIGHT][MAX_WIDTH];
for(int i = 0; i < MAX_HEIGHT; i++)
{
for(int j = 0; j < MAX_WIDTH; j++)
{
jar[i][j] = false;
}
}
bool box[MAX_SIZE][MAX_SIZE];
for(int i = 0; i < MAX_SIZE; i++)
for(int j = 0; j < MAX_SIZE; j++)
{
box[i][j] = false;
}
box[1][2] = true;
box[1][3] = true;
box[2][1] = true;
box[2][2] = true;
//print box
for(int row =0; row < MAX_SIZE; row++)
{
for (int col = 0; col < MAX_SIZE; col ++)
cout << box[row][col];
cout << endl;
}
//-------------------------------------------------------------//
for(k = 0; k < MAX_SIZE; k++) //assigns the box to that location of the jar
for(l = 0; l < MAX_SIZE; l++)
for(int n = firstRow; n < firstRow + 4; n++)
for(int m = firstCol; m < firstCol + 4; m++)
{
if(jar [k][l+3] == false && box [k][l] == true)
{jar [k][l+3] = true;
}
if(jar [k][l+3] == true && box [k][l] == false)
{jar [k][l+3] = false;
}
}
//prints the original array for testing
for (int i = 0; i < MAX_HEIGHT; i++)
{
for(int j = 0; j < MAX_WIDTH; j++)
{
if(jar[i][j] == true)
{
cout << "H";
}
else
{
cout << ".";
}
}
cout << endl;
}
//Main function: After key is input, move array on jar.
char direction;
cin >> direction;
if(direction == 's')
{
for(int y = firstRow; y < firstRow + 4; y++)
for(int x = firstCol; x < firstCol + 4; x++)
{
jar[y+1][x] = jar[y][x];
}
for(int z = firstCol; z < firstCol + 4; z++)
jar[firstRow][z] = false;
}
else if(direction == 'd')
{
for(int y = firstRow; y < firstRow + 4; y++)
for(int x = firstCol; x < firstCol + 4; x++)
{
jar[y][x+1] = jar[y][x];
}
for(int z = firstRow; z < firstRow + 4; z++)
jar[z][firstCol] = false;
}
else if(direction == 'a')
{
for(int y = firstRow; y < firstRow + 4; y++)
for(int x = firstCol; x < firstCol + 4; x++)
{
jar[y][x-1] = jar[y][x];
}
for(int z = firstRow; z < firstRow + 4; z++)
jar[z][firstCol + 3] = false;
}
//---------------Prints array again for testing----------------------//
for (int i = 0; i < MAX_HEIGHT; i++)
{
for(int j = 0; j < MAX_WIDTH; j++)
{
if(jar[i][j] == true)
{
cout << "H";
}
else
{
cout << ".";
}
}
cout << endl;
}
}
Thank you.