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{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
};
void display(){
int i, k;
for(i=0; i< 20; i++){
for(k=0; k< 20; k++){
switch(map1[i][k]){
case 0:
printf("□");
break;
case 1:
printf(" ");
break;
case 2:
printf("®");
break;
case 3:
printf("♡");
break;
case 4:
printf("♥");
break;
default:
break;
}
}
printf("\n");
}
}
int main(){
display();
return 0;
}
I'm trying to display my game map.
The right side of my game map is so weird. So, I change the font of console and I can get my right game map.
Why is it printing like this? Is it the font problem of console?
I'm using windows 11 and updated recently. Is it related?

You are giving double spacing for case 1 which is causing the issue.
Instead of there being 18 spaces, there's 36 making the rows and columns un-even.
Use this instead
case 1:
printf(" ");
break;

Related

Split 16x16 matrix in 4x4 in the same matrix

Hi guys I am doing a small exercise splitting a 16x16 matrix in 4x4 chunks, creating a sort of diagonal of small matrix. I am representing the 4x4 matrix with a value of 1 and the rest with a value of 0, painting them in the big matrix itself.
This what it should look like:
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
This is what I have achieved at me moment:
The red squares is what I am missing. These 4x4 matrix has to be "1" as well.
This is code (it is in javascript but I don't really care about the language).
let m = Array(16)
.fill("0")
.map(() => Array(16).fill("0"));
for (let i = 0; i < m.length; i++) {
for (let j = 0; j < m.length; j++) {
if (i % 8 < 4 && j % 8 < 4) {
m[i][j] = "1";
}
}
}
m.map((a) => console.log(JSON.stringify(a + "")));
Anyone know how to solve this problem? Thank You in advance!
I'd try something like that: it uses even/odd comparison to determine position for "1"
let m = Array(16)
.fill("")
.map(() => Array(16).fill(""));
for (let i = 0; i < m.length; i++) {
for (let j = 0; j < m.length; j++) {
let mi = parseInt(i/4);
let mj = parseInt(j/4);
m[i][j] = ((mi&1) == (mj&1))?"1":"0";
}
}
m.map((a) => console.log(JSON.stringify(a + "")));
In Ruby:
require 'matrix'
m = Matrix.build(16, 16) { |i,j| (i/4).even? == (j/4).even? ? 1 : 0 }
To convert the matrix object to an array:
m.to_a
#=> [[1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
# [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]]
See Matrix::build and Enumerable#to_a.
This should translate easily to other languages that have a matrix library, but even if such a library is not available, the clause
(i/4).even? == (j/4).even? ? 1 : 0
shows how the array can be constructed in an economic way.
Here's another way that does not use matrices.
a1 = [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0]
a2 = a1.reverse
#=> [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]
Array.new(16) { |i| ((i/4).even? ? a1 : a2).dup }
See Array::new and Kernel#dup.

How to divide column automatically?

I have multiple txt files (close 1000) and I want to import to excel. I used text import wizard then choose fixed width, my question is how to use the same format for each file that I don't need to adjust everytime.
This is the example link : http://www.fhwa.dot.gov/bridge/nbi/1992/AL92.txt
This is the record format: http://www.fhwa.dot.gov/bridge/nbi/format.cfm
To save you a bit of time I prepared the array (from the format web page linked above) for the querytables/import for you based on #RonRosenfeld's advice and quickly did a text file import (which I recorded).
Have to paste in here cause it is too big for comments.
You would, of course, have to edit to suit.
With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\AL92.txt", Destination:=Range("$A$1"))
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(3, 15, 1, 1, 1, 5, 1, 2, 3, 5, 24, 1, 18, 25, 4, 7, 1, 10, 2, 8, 9, 3, 1, 2, 2, 2, 4, 2, 2, 6, 4, 1, 4, 1, 2, 1, 1, 1, 1, 1, 1, 1, 4, 5, 1, 1, 1, 1, 2, 1, 2, 3, 4, 3, 5, 6, 3, 3, 4, 4, 4, 1, 4, 1, 3, 3, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 2, 1, 6, 4, 2, 3, 3, 3, 4, 4, 4, 6, 6, 6, 4, 3, 2, 15, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 2, 1, 1, 1, 1, 6, 4, 4)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

"Array index is less than one" error at Winbugs

I am working on a multilevel differential item functioning model using WinBUGS package. I have successfully built simpler models, but I have also gotten the error "array index is less than one". I'd be very pleased if you could help.
# Model
Model
{
for (l in 1:50){
y[l] ~ dbern(p[l])
logit(p[l])<- u2[stu[l]] - beta[x[l]] + gamma[tea[l], x[l]]*grp[l] + alpha1[x[l]]*geo[l] +
alpha2[x[l]]*conf[l] + alpha3[x[l]]*ses[l]
}
for (t in 1:10){
for (i in 1:10){
gamma[t,i] ~ dnorm(gamma.hat[t,i], tau.gamma[i])
gamma.hat[t,i]<-pi1[i] + pi2[i]*inq[t]
}
}
# fixed effect prior
for (i in 1:10){
beta[i] ~ dnorm(0, .0001)
alpha1[i] ~ dnorm(0, .0001)
alpha2[i] ~ dnorm(0, .0001)
alpha3[i] ~ dnorm(0, .0001)
pi1[i] ~ dnorm(0, .0001)
pi2[i] ~ dnorm(0, .0001)
}
# Random effect prior
for (s in 1:5){
u2[s] ~ dnorm(0,tau.u2)
}
tau.u2 <- pow(sigma.u2, -2)
sigma.u2 ~ dunif (0, 100)
for (i in 1:10){
tau.gamma[i] <- pow(sigma.gamma[i],-2)
sigma.gamma[i] ~ dunif(0,100)
}
}
# Data
list(y=c(0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0), ses=c(0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1), conf=c(1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1), geo=c(1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0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, 0), sigma.u2=1, gamma=structure(
.Data=c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim=c(10, 10)))
Indexes start from 1, not 0, in BUGS. The variables stu and tea are used for indexing, but they take the values 0,1 - they should be 1,2 instead.
Something going on with x. It is one shorter in length than the other variables, perhaps you are missing an observation.

2 Dimensional Array printing messed up

I was making a program that will print out the periodic table with [ ] for an element, and [*] for the element you're searching for...
void printmap(int x, int y)
{
int a, b, table[9][18] = {
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0
}; // 0 - Empty, 1 - [ ], 2 - [*]
table[x][y] = 2;
b = 0;
printf("\n*******************\n\n");
for( a = 1, printf(" \t"); a < 19; a++)
{
printf("%3d", (b + a));
}
printf("\n");
for(a = 0; a < 9; a++)
{
printf("\n%2d\t", a + 1);
for(b = 0; b < 18; b++)
{
switch(table[a][b])
{
case 0 :
printf(" ");
case 1 :
printf("[ ]");
break;
case 2 :
printf("[*]");
break;
default :
printf("");
}
}
}
}
For some reason, the output is messed up... ( If I pass the argument for helium, which should be 0 and 18 this is what I get : http://pastebin.com/YjhjzSi8
What am I doing wrong?
Thank you
That's not a 2D array, just because you make it LOOK like a 2D array, doesn't mean it is one.
This what you have
table[9][18] = {
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
...
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0
};
You should add more brackets accordingly
table[9][18] = {
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1),
....
{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}};
Been a while since I used C++, but, this is not how you initialize a 2D array. In fact I'm surprised it even compiles. Correct way is something like:
table[0] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
table[1] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1};
table[2] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1};
...
Another example: 2D array values C++
As the other respondents have said, you need to initialize your table array correctly.
Also, you have missed a "break" in your switch/case statemet, and you also missed printing a newline at the end of each row. I took the liberty of renaming your variables a,b to row,col and fixed your code, this should do what you want,
void printmap(int x, int y)
{
int row=0, col=0;
int was = table[x][y];
table[x][y] = 2;
col = 0;
printf("\n*******************\n\n");
printf(" \t");
row=0; //you were not initializing row
for( col = 0; col < 18; col++)
{
printf("%3d", (col+row+1));
}
printf("\n");
for(row = 0; row < 9; row++)
{
printf("\n%2d\t", row + 1);
char* rc = " ";
for(col = 0; col < 18; col++)
{
switch(table[row][col])
{
case 0 : rc=(" "); break; //was missing break
case 1 : rc=("[ ]"); break;
case 2 : rc=("[*]"); break;
default: rc=(" "); break; //was missing break
}
printf("%s",rc);
}
printf("\n"); //missing print newline
}
table[x][y] = was;
}
And here is the table, as others have suggested,
int table[9][18] = {
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 }
}; // 0=Empty, 1=[ ], 2=[*]

Issues spawning new levels for 2D platformer in AS3

I'm making a 2D platformer in Flash for a project, and everything's going swimmingly! It's been easy sailing up until I realize the assignment says to use 2 levels, and the only way I have done levels is via adding them as classes, while this project is using arrays.
I started using URLLoader to plop the levels in .txt files, but even then the example code I've been following only shows how to do this for one level, not for 2+.
Here's the code I'm using for the maps, I can supply more if this isn't sufficient:
public function init()
{
trace("init");
map = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 2, 2, 2, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 2, 2, 2, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 0, 2, 2, 0, 1, 1, 1, 1, 0, 2, 2, 0, 1, 1, 1, 0],
[0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0]]
container = new Sprite();
addChild(container);
tileArray = [Tile0, Tile1, Tile2];
buildMap();
createPlayer();
createWinSpace();
ArrowKeyInput.init(stage);
addEventListener(Event.ENTER_FRAME, update);
}
private function buildMap():void
{
trace("BUILD");
tileMap = [];
for (var i:int = 0; i < map.length; i++)
{
var row:Array = map[i];
var tileRow:Array = [];
tileMap.push(tileRow);
for (var j:int = 0; j < row.length; j++)
{
var tileNum:int = row[j];
var className:Class = tileArray[tileNum];
var tile:MovieClip = new className();
tile.width = tile.height = TILE_SIZE;
tile.x = TILE_SIZE * j;
tile.y = TILE_SIZE * i;
container.addChild(tile);
tileRow.push(tile);
}
}
}
I appreciate any sort of feedback I can get on this.
If I am understanding you correctly, you are wondering how to make your buildMap function be capable of building multiple maps.
What is important to understand about this issue, is that the map array is what the buildMap function utilizes as data to build the level. So whatever is in the map array defines what tiles will be used for each location on the level. So if you want to have multiple levels, you could define multiple maps as such with each having it's own array :
map_1 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 2, 2, 2, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 2, 2, 2, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 0, 2, 2, 0, 1, 1, 1, 1, 0, 2, 2, 0, 1, 1, 1, 0],
[0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0]];
map_2 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 2, 2, 2, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 2, 2, 2, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 0, 2, 2, 0, 1, 1, 1, 1, 0, 2, 2, 0, 1, 1, 1, 0],
[0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0]];
Those are your map definitions and you can have as many as you like. I just copied the data for the second map, but you can just create a new level in that definition.
Now, before you call the buildMap() function, just set the value of map to whichever level you wish to build :
map = map_1;
buildMap();
or
map = map_2;
buildMap();
Now, you have other things to think about as if you begin with map_1 and then want to build map_2, you need to remove the tiles that you used to build map_1.
From what I can tell in your code, they are all added to the container sprite. So what you could do is remove all those tiles by adding something like this to the beginning of the buildMap() function :
while (container.numChildren > 0)
{
container.removeChildAt(0);
}
Most likely there are other variables that you will need to reset when loading a new level, but this was just meant to be an example of one way you might manage that, with the very little that I know by looking at your code.

Resources