[C #] Windows XP와 함께 제공되는 지뢰 찾기 게임 모방
1 주제 설명 : Windows XP와 함께 제공되는 지뢰 찾기 게임 모방
30x30 2 차원 배열을 정의하고 Windows XP와 함께 제공되는 지뢰 찾기 게임을 모방하여이 2 차원 배열을 무작위로 채굴하며 적어도 30 개의 지뢰가 필요합니다. 게임의 규칙은 다음과 같습니다 : 요소의 값은 한 주에있는 광산 수입니다 (인접한 8 개 위치).
2 자세한 소스 코드
using System;
using System.Collections;
namespace Csharp5_3
{
class Program
{
static void Main(string[] args)
{
ArrayList arrayList = new();
ArrayList arrayList_map = new();
Random rd = new();
for (int i = 0; i < 900; i++)
{
if (rd.Next() % 20 == 0)
{
arrayList.Add(1);
}
else
{
arrayList.Add(0);
}
arrayList_map.Add(0);
}
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 30; ++j)
{
Console.Write(arrayList[i * 30 + j]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.WriteLine();
for ( int i = 0; i < 30; i ++ )
{
for ( int j = 0; j < 30; j ++ )
{
// 判断八个方位是否有雷,将雷相加
// 判断上
if (((i - 1) * 30 + j) >= 0 && ((i - 1) * 30 + j) < 900 && j != 0) // 边界值判断
{
if (Convert.ToInt32(arrayList[i * 30 + j - 30]) == 1)
{
arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
}
}
// 判断下
if (((i + 1) * 30 + j) >= 0 && ((i + 1) * 30 + j) < 900 && j != 29) // 边界值判断
{
if (Convert.ToInt32(arrayList[i * 30 + j + 30]) == 1)
{
arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
}
}
// 判断左
if ((i * 30 + j - 1) >= 0 && (i * 30 + j -1) < 900 && j != 0) // 边界值判断
{
if (Convert.ToInt32(arrayList[i * 30 + j -1]) == 1)
{
arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
}
}
// 判断右
if ((i * 30 + j + 1) >= 0 && (i * 30 + j + 1) < 900 && j != 29) // 边界值判断
{
if (Convert.ToInt32(arrayList[i * 30 + j + 1]) == 1)
{
arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
}
}
// 判断左上
if (((i - 1) * 30 + j -1 ) >= 0 && ((i - 1) * 30 + j - 1) < 900 && j != 0) // 边界值判断
{
if (Convert.ToInt32(arrayList[(i - 1) * 30 + j - 1]) == 1)
{
arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
}
}
// 判断左下
if (((i + 1) * 30 + j - 1) >= 0 && ((i + 1) * 30 + j - 1) < 900 && j != 29) // 边界值判断
{
if (Convert.ToInt32(arrayList[(i + 1) * 30 + j - 1]) == 1)
{
arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
}
}
// 判断右上
if (((i - 1) * 30 + j + 1) >= 0 && ((i - 1) * 30 + j + 1) < 900 && j != 0) // 边界值判断
{
if (Convert.ToInt32(arrayList[(i - 1) * 30 + j + 1]) == 1)
{
arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
}
}
// 判断右下
if (((i + 1) * 30 + j + 1) >= 0 && ((i + 1) * 30 + j + 1) < 900 && j != 29) // 边界值判断
{
if (Convert.ToInt32(arrayList[(i + 1) * 30 + j + 1]) == 1)
{
arrayList_map[i * 30 + j] = Convert.ToInt32(arrayList[i * 30 + j]) + 1;
}
}
}
}
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 30; ++j)
{
Console.Write(arrayList_map[i * 30 + j]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.Read();
}
}
}