December 28 Application of a two-dimensional array: The first mini-game (push box)

Source: Internet
Author: User
Tags array definition

Mini game: ****** push box ********

static void Main (string[] args)
{
int I, J;
int[,] A = new int[10, 10]//two-D array definition type [,] Array name = new Type [number of rows, number of columns] {assignment}; or single assignment a[i,j] = 1;
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,2,0,0,8,0,0,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,0,0,0,1,0,0,0,1},//two-D array format in tabular form
{1,0,0,1,1,1,0,0,0,1},
{1,0,1,1,0,0,0,0,0,1},
{1,0,0,0,0,0,9,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
};

int hang = 2, lie = 2, temp = 0;

while (true)
{
Make a map
for (i = 0; i <; i++)
{
for (j = 0; J <; J + +)
{
if (A[i, j] = = 1)
{
Console.Write (""); The value of the two-dimensional array console.write (a[i,j]);  

}
if (A[i, j] = = 0)
{
Console.Write ("");
}
if (A[i, j] = = 2)
{
Console.Write ("♀");
} if (A[i, j] = = 9)
{
Console.Write ("★");
} if (A[i, j] = = 8)
{
Console.Write ("-");
}

}

Console.WriteLine ();
}

Console.Write ("Please press the left and right key on the keyboard to walk:");
Console.WriteLine ();

Consolekey s = Console.readkey (). Key;
Console.clear ();

Right
if (s.tostring () = = "RightArrow")
{
if (A[hang, lie+1] = = 0)
{
temp = A[hang, lie];
A[hang, lie] = A[hang, (lie + 1)];
A[hang, (Lie + 1)] = temp;
lie++;
}
else if (A[hang, lie + 1] = = 1)
{
Console.Write ("Banging on the Wall");
}
else if (A[hang, lie + 1] = = 8)
{
If (A[hang, Lie + 2]! = 1 && a[hang, lie-2]! = 9)
{
temp = A[hang, (Lie + 2)];
A[hang, Lie + 2] = A[hang, lie + 1];
A[hang, lie + 1] = A[hang, lie];
A[hang, lie] = temp;
lie++;
}
else if (A[hang, lie + 2] = = 9)
{
A[hang, Lie + 2] = A[hang, lie + 1];
A[hang, lie + 1] = A[hang, lie];
A[hang, Lie] = 0;
Console.Write ("You win, please go to the next level");
Break
}
else {Console.Write ("banging");}
}
}

//Left
Else if (s.tostring () = = "Leftarrow")
{
if (A[hang, lie-1] = = 0)
{
Temp = A[hang, lie];
A [Hang, lie] = A[hang, (lie-1)];
A[hang, (lie-1)] = temp;
lie--;
}
Else if (A[hang, lie-1] = = 1)
{
Console.Write ("banging");
}
Else if (A[hang, lie-1] = = 8)
{
if (A[hang, lie-2]! = 1 && a[hang, lie-2]! = 9)
{
Temp = A[hang, (lie-2)];
A[hang, lie-2] = A[hang, lie-1];
A[hang, lie-1] = A[hang, lie];
A[hang, lie] = temp;
lie--;
}
Else if (A[hang, lie-2] = = 9)
{
A[hang, lie-2] = A[hang, lie-1];
A[hang, lie-1] = A[hang, Lie];
A[hang, Lie] = 0;
Console.Write ("You win, please go to the next level");
break;
}
Else {console.write ("banging");}
}

}

//Up
Else if (s.tostring () = = "UpArrow")
{
if (a[hang-1, lie] = = 0)
{
Temp = A[hang, lie];
A[h Ang, Lie] = a[(hang-1), lie];
a[(hang-1), lie] = temp;
hang--;
}
Else if (a[hang-1, lie] = = 1)
{
Console.Write ("banging");
}
Else if (a[hang-1, lie] = = 8)
{
if (a[hang-2, lie]! = 1 && a[hang-2, lie]! = 9)
{
Temp = A[hang-2, lie];
A[hang-2, lie] = a[hang-1, lie];
A[hang-1, lie] = A[hang, lie];
A[hang, lie] = temp;
hang--;
}
Else if (a[hang-2, lie] = = 9)
{
A[hang-2, lie] = a[hang-1, lie];
a[hang-1, lie] = A[hang, Lie];
A[hang, Lie] = 0;
Console.Write ("You win, please go to the next level");
break;
}
Else {console.write ("banging");}
}
}

Down
else if (s.tostring () = = "DownArrow")
{
If (a[hang+1, lie] = = 0)
{
temp = A[hang, lie];
A[hang, Lie] = a[(hang + 1), lie];
a[(hang + 1), lie] = temp;
hang++;
}
else if (A[hang + 1, lie] = = 1)
{
Console.Write ("Banging on the Wall");
}
else if (A[hang + 1, lie] = = 8)
{
if (A[hang + 2, lie]! = 1 && A[hang + 2, lie]! = 9)
{
temp = A[hang + 2, lie];
A[hang + 2, lie] = A[hang + 1, lie];
A[hang + 1, lie] = A[hang, lie];
A[hang, lie] = temp;
hang++;
}
else if (A[hang + 2, lie] = = 9)
{
A[hang + 2, lie] = A[hang + 1, lie];
A[hang + 1, lie] = A[hang, lie];
A[hang, Lie] = 0;
Console.Write ("You win, please go to the next level");
Break
}
else {Console.Write ("banging");}
}
}

Console.WriteLine ();
}

December 28 Application of a two-dimensional array: The first mini-game (push box)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.