Trouble Printing Individual Array Elements
Jun 20, 2014 at 7:10am UTC
I am attempting to print the array to the console, while having different characters in the array print as different colours. However, only the altered '@' character prints while rapidly clearing the console. Previously, the only 'cout' command was the one present inside the commented 'for' loop that would print entire lines at a time, which I attempted to replace with the other 'cout' commands inside the switch statement.
Essentially, I'm wondering how I can print the individual array elements to the screen without all of them taking on the same text colour.
Thank you for all help in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
#include <iostream>
#include <conio.h>
#include <cmath>
#include <windows.h>
#include "clearscreen.h"
#include "rlutil.h"
using namespace std;
using namespace rlutil;
#define MAPSIZE 10
char Map[MAPSIZE][MAPSIZE] = {"#########" ,
"# @ !#" ,
"# #" ,
"# Z #" ,
"# #" ,
"# C #" ,
"# #" ,
"# #" ,
"# #" ,
"#########" };
int Gamespeed = 75;
bool stopgame = false ;
int main() {
while (stopgame == false )
{
/*for (int y = 0; y < MAPSIZE; y++)
{
cout << Map[y] << endl;
}*/
for (int y = 0; y < MAPSIZE; y++)
{
for (int x = 0; x < MAPSIZE; x++)
{
//Move printing command into this loop to allow differently coloured text
switch (Map[y][x])
{
case '#' :
{
setColor(6);
Map[y][x] = 219;
cout << Map[y][x];
if (x == (MAPSIZE-1)){
cout << endl;
}
}break ;
case '█' :
{
setColor(6);
cout << Map[y][x];
if (x == (MAPSIZE-1)){
cout << endl;
}
}break ;
case 'C' :
{
setColor(1);
Map[y][x] = 128;
cout << Map[y][x];
if (x == (MAPSIZE-1)){
cout << endl;
}
}break ;
case 'Ç' :
{
setColor(1);
cout << Map[y][x];
if (x == (MAPSIZE-1)){
cout << endl;
}
}break ;
case 'Z' :
{
setColor(2);
Map[y][x] = 165;
cout << Map[y][x];
if (x == (MAPSIZE-1)){
cout << endl;
}
}break ;
case 'Ñ' :
{
setColor(2);
cout << Map[y][x];
if (x == (MAPSIZE-1)){
cout << endl;
}
/*Use this algorithm for Zombie AI until A* implementation
srand(time(NULL));
x = rand()%100+1;*/
}break ;
case '@' :
{
setColor(3);
Map[y][x] = 01;
cout << Map[y][x];
if (x == (MAPSIZE-1)){
cout << endl;
}
}
case '(This is alt code 01 in the program, but won' t show up here)':
{
setColor(3);
cout << Map[y][x];
if (x == (MAPSIZE-1)){
cout << endl;
}
if (GetAsyncKeyState(VK_UP) != 0)
{
int y2 = (y-1);
switch (Map[y2][x])
{
case ' ':
{
Map[y][x] = ' ';
y -= 1;
Map[y2][x] = ' ';
}break;
}
} else if (GetAsyncKeyState(VK_RIGHT) != 0)
{
int x2 = (x+1);
switch (Map[y][x2])
{
case ' ':
{
Map[y][x] = ' ';
x += 1;
Map[y][x2] = ' ';
}break;
}
} else if (GetAsyncKeyState(VK_LEFT) != 0)
{
int x2 = (x-1);
switch (Map[y][x2])
{
case ' ':
{
Map[y][x] = ' ';
x -= 1;
Map[y][x2] = ' ';
}break;
}
} else if (GetAsyncKeyState(VK_DOWN) != 0)
{
int y2 = (y+1);
switch (Map[y2][x])
{
case ' ':
{
Map[y][x] = ' ';
y += 1;
Map[y2][x] = ' ';
}break;
}
} else if (GetAsyncKeyState(VK_ESCAPE) != 0)
{
stopgame = true;
}
}break;
}
}
}
Sleep(Gamespeed);
cls();
}
return 0;
}
Last edited on Jun 20, 2014 at 7:11am UTC
Jun 20, 2014 at 1:27pm UTC
Don't know if this your problem or not, but some of your case label values are greater than single character constant . Better to use the actual numerical value, instead of using 'Ç' for example
Jun 22, 2014 at 1:13am UTC
I'll give that a try. Thank you :)
This was meant to be a 15 minute test, but I found a problem and I didn't want to leave it unsolved.
Topic archived. No new replies allowed.