Resolved Test code with loops not working.

Status
Not open for further replies.

Polski Arrow

New member
Joined
Dec 1, 2019
Messages
3
Programming Experience
Beginner
Hi, I wrote that code below and built and compiled it to console program.
That's my test code for strategy game in console. There I have to choose area by typing X and Y coordinates. Areas are int variables which can represent empty area (0) ad area with number of pawn (1-25).

How should it work?
When I choose area[x][y] int by typing coordinates, program checks is that area value. If empty (0), program gives error info and repeats question. If value is 1 to 25, program check empty areas around chosen one to give me a choice of way to move.

How it actually works?
When I choose area[x][y], program gives me info message about error no matter I choose empty area or 1-25.

Thanks to all who help me. :)

Code below:
C#:
#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <time.h>
#include <stdio.h>
#include <iomanip>
#include <conio.h>

using namespace std;

int area[15][15];
int armour[15][15];
int damage[15][15];
int soldier[5];
int vehicle[5];
int tank[5];
int cannon[5];
int rocket[5];
int counter, i, x, y;
bool up;
bool down;
bool l;
bool r;
bool lup;
bool rup;
bool ldown;
bool rdown;

int main()
{
    for (counter = 0; counter <= 4; counter += 1)
    {
        soldier[counter] = counter + 1;
        vehicle[counter] = counter + 6;
        tank[counter] = counter + 11;
        cannon[counter] = counter + 16;
        rocket[counter] = counter + 21;
    }

    for (x = 0; x <= 14; x += 1)
    {
        area[x][13] = 1;
    }

    for (x = 0; x <= 2; x += 1)
    {
        area[x][14] = 6;
    }

    for (x = 3; x <= 4; x += 1)
    {
        area[x][14] = 11;
    }

    for (x = 5; x <= 6; x += 1)
    {
        area[x][14] = 16;
    }

    area[7][14] = 21;

    for (x = 8; x <= 9; x += 1)
    {
        area[x][14] = 16;
    }

    for (x = 10; x <= 11; x += 1)
    {
        area[x][14] = 11;
    }

    for (x = 12; x <= 14; x += 1)
    {
        area[x][14] = 6;
    }

    for (y = 0; y <= 14; y += 1)
    {
        if (y < 10)
        {
            cout << "| Y" << y << "  |";
        }
        else
        {
            cout << "| Y" << y << " |";
        }
        for (x = 0; x <= 14; x += 1)
        {
            if (area[x][y] < 10)
            {
                cout << "| " << area[x][y] << "  |";
            }
            else
            {
                cout << "| " << area[x][y] << " |";
            }
        }
        cout << endl;
    }
    cout << endl << "       ";
    for (x = 0; x <= 14; x += 1)
    {
        if (x < 10)
        {
            cout << "| X" << x << " |";
        }
        else
        {
            cout << "| X" << x << "|";
        }
    }
    cout << endl << endl;

    for (y = 0; y <= 14; y += 1)
    {
        for (x = 0; x <= 14; x += 1)
        {
            if (area[x][y] = 0)
            {
                armour[x][y] = 0;
                damage[x][y] = 0;
            }
            else if (area[x][y] >= 1 && area[x][y] <= 5)
            {
                armour[x][y] = area[x][y];
                damage[x][y] = area[x][y];
            }
            else if (area[x][y] >= 6 && area[x][y] <= 10)
            {
                armour[x][y] = (area[x][y] - 6) * 2;
                damage[x][y] = (area[x][y] - 6) * 2;
            }
            else if (area[x][y] >= 11 && area[x][y] <= 15)
            {
                armour[x][y] = (area[x][y] - 11) * 3;
                damage[x][y] = (area[x][y] - 11) * 3;
            }
            else if (area[x][y] >= 16 && area[x][y] <= 20)
            {
                armour[x][y] = (area[x][y] - 16) * 4;
                damage[x][y] = (area[x][y] - 16) * 4;
            }
            else if (area[x][y] >= 21 && area[x][y] <= 25)
            {
                armour[x][y] = (area[x][y] - 21) * 5;
                damage[x][y] = (area[x][y] - 21) * 5;
            }
        }
    }

    cout << "Choose area (X): ";
    cin >> x;
    cout << endl;
    cout << "Choose area (Y): ";
    cin >> y;
    cout << endl << endl;

    while (area[x][y] == 0)
    {
        cout << "ERROR! Chosen area can not be equal to 0!" << endl << endl;
        cout << "Choose area (X): ";
        cin >> x;
        cout << endl;
        cout << "Choose area (Y): ";
        cin >> y;
        cout << endl << endl;
    }

    if (area[x][y] > 0 && area[x][y] <= 20)
    {
        up = false;
        down = false;
        l = false;
        r = false;
        lup = false;
        rup = false;
        ldown = false;
        rdown = false;
        if (area[x][y-1] == 0)
        {
            up = true;
        }
        if (area[x+1][y-1] == 0)
        {
            rup = true;
        }
        if (area[x+1][y] == 0)
        {
            r = true;
        }
        if (area[x+1][y+1] == 0)
        {
            rdown = true;
        }
        if (area[x][y+1] == 0)
        {
            down = true;
        }
        if (area[x-1][y+1] == 0)
        {
            ldown = true;
        }
        if (area[x-1][y] == 0)
        {
            l = true;
        }
        if (area[x-1][y-1] == 0)
        {
            lup = true;
        }
    }

    cout << "You can move: " << endl;

    if (up == true)
    {
        cout << "up" << endl;
    }
    if (rup == true)
    {
        cout << "up right" << endl;
    }
    if (r == true)
    {
        cout << "right" << endl;
    }
    if (rdown == true)
    {
        cout << "down right" << endl;
    }
    if (down == true)
    {
        cout << "down" << endl;
    }
    if (ldown == true)
    {
        cout << "down left" << endl;
    }
    if (l == true)
    {
        cout << "left" << endl;
    }
    if (lup == true)
    {
        cout << "up left" << endl;
    }
    return 0;
}

Additional info:
Coding program: Code::Blocks v20.03
Chosen language: C++
Compiler: GNU GCC Compiler
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,175
Location
Chesapeake, VA
Programming Experience
10+
C# and C++ is the same language but they both are different VERSIONS of THE SAME language. If this moderator doesn't unlock further replies, I stop using this site and I will also do everything to take reputation of this site down as much as possible. I would not like to do that but I feel it will be necessary. Closing threads for no reason is not fair.

Saying that C# and C++ are the same language is like saying that French and Spanish are version of the same Romance/Latin language. Yes, C# and C++ may both have originated from the C, but both languages have diverged enough that you wouldn't expect a French speaker to automatically understand Spanish and vice versa. In reality, though, a lot of the C# concepts draw more from Java rather than C.
 
Status
Not open for further replies.
Top Bottom