Member Avatar for 111100/11000
/*
Made in code::blocks 12.11
-randomly generated terrain mirrored to be equal for bought players
-
-
*/
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <fstream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <cctype>
using namespace std;

//Public functions
char printMap(char terraniValue);
int spawnUnit(int positionOfPiece, int unitType);
int createBoard(int xLenght, int yLenght);
string OutputCharacterWithColor(string character);
void black();
void red();
void green();
void blue();
void white();



//Pubic variables
int Gold = 300;
int rowLength, columnLenght;
string board;


int main()
{
    createBoard(10, 8);
    printf("1.Infantry(Defence=6, Attack=1)\n");
    printf("2.Sniper\n");
    printf("3.Artillery\n");
    printf("4.Tank\n");
    printf("5.General\n");
    printf("Enter pieces you want to buy or type \"end\" to finish buying pieces:\n");
    string unitSelection = "choosing";
    int unitType[99], positionOfPiece, nextPieceChoice = 0;
    for(int i=0; i<99; i++)
    {
        unitType[i] = 99;
    }
    while(unitSelection != "end")//Somewhere in this while loop is the problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    {
        cin >> unitSelection;
        if(unitSelection == "1" || unitSelection == "2" || unitSelection == "3" || unitSelection == "4" || unitSelection == "5")
        {
            stringstream convert;
            convert << unitSelection;//put string into stringstream
            convert >> unitType[nextPieceChoice];//give the value to integer unitType[nextPieceChoice] from stringstream
            nextPieceChoice++;
            convert.str("");
            convert.clear();
        }
    }
    positionOfPiece = (rowLength/2) - (nextPieceChoice/2);//nextPieceChoice gives number of pieces that was chosen
    int nextUnit = 0;
    while(unitType[nextUnit] != 99);
    {
        spawnUnit(positionOfPiece+nextUnit, unitType[nextUnit]);
        nextUnit++;
    }
    printf("Program not finished...\n");
    system("pause");
    return 0;
}
int spawnUnit(int positionOfPiece, int unitType)
{
    string boardWithPieces[rowLength*columnLenght];
    int counter = 1;
    switch(unitType)
    {
        case 1:
        {
            if(Gold >= 10)
            {
                for(int i=0; i<board.length()+1; i++)
                {
                    boardWithPieces[i] = board[i];
                }
                board.clear();
                boardWithPieces[positionOfPiece] = "@";//replace random piece with first unit type
                for(int i=0; i<=(rowLength*columnLenght); i++)//upper side of the board
                {
                    if((i == (rowLength*counter)) && i != (rowLength*columnLenght)-1)//if end of row and that row is not last
                    {
                        black();
                        printf("|\n");
                        counter++;
                    }
                    if(i != (rowLength*columnLenght))//not last position in last row after '|' sign
                    {
                        OutputCharacterWithColor(boardWithPieces[i]);
                        board += boardWithPieces[i];//assign boardWithPieces back to board progress
                    }
                }
                counter -= 1;
                for(int i=(rowLength*columnLenght); i>=0; i--)//lower side of the board(horizontally and vertically mirrored upper side)
                {
                    if((i == (rowLength*counter)-rowLength))//if end of row and that row is not first one
                    {
                        black();
                        printf("|\n");
                        counter--;
                    }
                    if(i != 0)//not last position in last row after '|' sign
                    {
                        OutputCharacterWithColor(boardWithPieces[i-1]);
                        board += boardWithPieces[i-1];//assign boardWithPieces back to board progress
                    }
                }
            }
            break;
        }
        case 2:
        {
            if(Gold >= 25)
            {

            }
            break;
        }
        case 3:
        {
            if(Gold >= 30)
            {

            }
            break;
        }
        case 4:
        {
            if(Gold >= 40)
            {

            }
            break;
        }
        case 5:
        {
            if(Gold >= 50)
            {

            }
            break;
        }
        case 99:
        {

            break;
        }
        default:
        {
            printf("Error:wrong unit type\n");
            system("pause");
            break;
        }
    }
    return 0;
}
int createBoard(int xLength, int yLenght)
{
    rowLength = xLength;
    columnLenght = yLenght;
    int randomNumber = 0;
    srand(time(0));
    for(int i=0; i<columnLenght; i++)
    {
        printf("%i%s", i, ".|");
        for(int j=0; j<rowLength; j++)
        {
            randomNumber = rand()%5+1;
            switch(randomNumber)
            {
                case 1:
                {
                    printMap('!');
                    break;
                }
                case 2:
                {
                    printMap('^');
                    break;
                }
                case 3:
                {
                    printMap('#');
                    break;
                }
                case 4:
                {
                    printMap('*');
                    break;
                }
                case 5:
                {
                    printMap(' ');
                    break;
                }
                default:
                {
                    printf("Error:terrain not found\n");
                    system("pause");
                }
            }
        }
        printf("|\n");
    }
    return 0;
}
char printMap(char terraniValue)
{
    board += terraniValue;
    printf("%c", terraniValue);
    return 0;
}
//start of colour related functions
string OutputCharacterWithColor(string character)
{
    if(character == "#")
    {
        white();
        cout << character;
        black();
    }
    if(character == "!")
    {
        red();
        cout << character;
        black();
    }
    else if(character == "@")
    {
        green();
        cout << character;
        black();
    }
    else if(character == "^")
    {
        blue();
        cout << character;
        black();
    }
    else
    {
        cout << character;
    }
    return "";
}
void white()
{
    HANDLE hConsole;
    hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute
    (hConsole, BACKGROUND_INTENSITY);
}
void black()
{
    HANDLE hConsole;
    hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute
    (hConsole, FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
}
void red()
{
    HANDLE hConsole;
    hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute
    (hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
}
void green()
{
    HANDLE hConsole;
    hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute
    (hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
}
void blue()
{
    HANDLE hConsole;
    hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute
    (hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
}
//end of colour related functions

Recommended Answers

All 11 Replies

You have several compile errors ...

In line 81:

ISO C++ forbids variable length array 'boardWithPieces'

You could use a ...

vector < string > vecStr( size ); // calls constructor
// if you really need an 'array of strings'

You also have redundant and several C (not C++ type includes)

Member Avatar for 111100/11000

My compiler doesn't show any errors

You need to turn them on, on your compiler ... to see them.

#include <windows.h>

//#include <stdio.h> // C usage style
#include <cstdio>    // C++ usage style

//#include <string.h>
include <cstring>

#include <cstdlib>
#include <ctime>

//#include <ctype.h>
#include <cctype>


#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

That's a new feature of c++11 iso standards. Some compilers such as Visual Studio have not implemented it yet, while others such as g++ may need special flags.

For compiles that don't support that, just change line 81 to this:

string* boardWithPieces = new string[rowLength*columnLenght];

As for the freezing problem -- look at line 75 and see if you can figure out what's wrong with it (Note: very common error)

Member Avatar for 111100/11000

Program doesn't reach line 75.After inputing '1' and 'end' as unitSelection it stops working but doesn't crash.

Program doesn't reach line 75.

That's because of the problem on line 70. Sorry, my previous post was incorrect, it should have said line 70 not line 75.

Member Avatar for 111100/11000

Why is line 70 a problem?

Look at that line very carefully -- the while statement is an infinite loop.

See that semicolon at the end of line 70?
That's your problem!
Thanks to the semicolon, you've effectively set up an infinite loop at line 70.

Member Avatar for 111100/11000

But nextUnit is incremented and eventually unitType should be equal to '99' from line 53 at which time loop should be broken

Member Avatar for 111100/11000

removed semicolon and it works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.