C# 과제 3 지뢰 찾기
using System;
using Com.JungBo.MineSet;
using Com.JungBo.Minedata;
namespace Com.JungBo.MineGames
{
public class Program
{
public static void Main(string[] args)
{
MineConsole mc = new MineConsole();
}
}
}
namespace Com.JungBo.Minedata
{
public enum BombState { EMPTY = 0, ONE, TWO, TREE, FOUR, FIVE, SIX, SEVEN, EIGHT, END = 10, FLAGEND, BOMB }
public enum OpenState { CLOSE = 0, OPEN, FLAG, QUESTION }
public struct Block
{
public BombState bState;
public OpenState oState;
public bool down;
}
public struct Position
{
public int row;
public int col;
}
public struct Order
{
public int first;
public int second;
public int third;
}
}
namespace Com.JungBo.MineSet
{
public class MineConsole
{
Block[,] board;
Position[] pos;
int boardsize = 10;
int mine = 10;
public MineConsole()
{
InitMine();
}
private void InitMine()
{
board = new Block[boardsize, boardsize];
pos = new Position[mine];
for (int i = 0; i < boardsize; i++)
{
for (int j = 0; j < boardsize; j++)
{
board[i, j].bState = BombState.EMPTY;
board[i, j].oState = OpenState.CLOSE;
board[i, j].down = false;
}
}
SetMine();
Print();
Console.WriteLine("");
CalMine();
Print();
Console.WriteLine("");
}
private void SetMine()
{
Random r = new Random();
int count = 0;
int row = 0;
int col = 0;
while (true)
{
if (count == mine) break;
row = r.Next(boardsize);
col = r.Next(boardsize);
if (board[row, col].bState != BombState.BOMB)
{
pos[count].row = row;
pos[count].col = col;
count++;
board[row, col].bState = BombState.BOMB;
}
}
Console.WriteLine(count);
}
private void CalMine()
{
for (int t = 0; t < mine; t++)
{
int x = pos[t].row;
int y = pos[t].col;
for(int i=x-1;i<=x+1;i++)
{
for(int j=y-1;j<=y+1;j++)
{
if (i >= 0 && j >= 0 && j <= (boardsize - 1) && i <= (boardsize - 1) && board[i, j].bState != BombState.BOMB) board[i, j].bState++;
}
}
}
}
public void Make()
{
SetMine();
Print();
Console.WriteLine();
CalMine();
}
public void Print()
{
for (int i = 0; i < boardsize; i++)
{
for (int j = 0; j < boardsize; j++)
{
Console.Write("{0,2}", (int)(board[i, j].bState));
}
Console.WriteLine();
}
}
}
}
using System;
using Com.JungBo.MineSet;
using Com.JungBo.Minedata;
namespace Com.JungBo.MineGames
{
public class Program
{
public static void Main(string[] args)
{
MineConsole mc = new MineConsole();
}
}
}
namespace Com.JungBo.Minedata
{
public enum BombState { EMPTY = 0, ONE, TWO, TREE, FOUR, FIVE, SIX, SEVEN, EIGHT, END = 10, FLAGEND, B0 }
public enum OpenState { CLOSE = 0, OPEN, FLAG, QUESTION }
public struct Block
{
public BombState bState;
public OpenState oState;
public bool down;
}
public struct Position
{
public int row;
public int col;
}
public struct Order
{
public int first;
public int second;
public int third;
}
}
namespace Com.JungBo.MineSet
{
public class MineConsole
{
Block[,] board;
Position[] pos;
int boardsize = 10;
int mine = 10;
public MineConsole()
{
InitMine();
}
private void InitMine()
{
board = new Block[boardsize, boardsize];
pos = new Position[mine];
for (int i = 0; i < boardsize; i++)
{
for (int j = 0; j < boardsize; j++)
{
board[i, j].bState = BombState.EMPTY;
board[i, j].oState = OpenState.CLOSE;
board[i, j].down = false;
}
}
SetMine();
Print();
Console.WriteLine("");
CalMine();
Print();
Console.WriteLine("");
SetMine();
Print();
Console.WriteLine("");
CalMine();
Print();
Console.WriteLine("");
}
private void SetMine()
{
Random r = new Random();
int count = 0;
int row = 0;
int col = 0;
while (true)
{
if (count == mine) break;
row = r.Next(boardsize);
col = r.Next(boardsize);
if (board[row, col].bState != BombState.B0)
{
pos[count].row = row;
pos[count].col = col;
count++;
board[row, col].bState = BombState.B0;
}
}
Console.WriteLine(count);
}
private void CalMine()
{
for (int t = 0; t < mine; t++)
{
int x = pos[t].row;
int y = pos[t].col;
for(int i=x-1;i<=x+1;i++)
{
for(int j=y-1;j<=y+1;j++)
{
if (i >= 0 && j >= 0 && i <= (boardsize - 1) && j <= (boardsize - 1) && board[i, j].bState != BombState.B0) board[i, j].bState++;
}
}
}
}
public void Make()
{
SetMine();
Print();
Console.WriteLine();
CalMine();
}
public void Print()
{
for (int i = 0; i < boardsize; i++)
{
for (int j = 0; j < boardsize; j++)
{
if ((int)board[i, j].bState == 12) Console.Write("{0,3}",board[i, j].bState);
else Console.Write("{0,3}", (int)(board[i, j].bState));
}
Console.WriteLine();
}
}
}
}
using System;
using Com.JungBo.MineSet; // 사용 하기위해 선언
using Com.JungBo.Minedata;
namespace Com.JungBo.MineGames
{
public class Program
{
public static void Main(string[] args)
{
MineConsole mc = new MineConsole(); // 객체 생성
}
}
}
namespace Com.JungBo.Minedata // 지뢰찾기의 기본 정보가 담김
{
public enum BombState { EMPTY = 0, ONE, TWO, TREE, FOUR, FIVE, SIX, SEVEN, EIGHT, END = 10, FLAGEND, B0 } //열거형 지뢰 정보를 선언
public enum OpenState { CLOSE = 0, OPEN, FLAG, QUESTION } // 상태 정보를 선언
public struct Block // 블록,위치정보를 구조체로 선언
{
public BombState bState;
public OpenState oState;
public bool down;
}
public struct Position // 지뢰의 위치정보
{
public int row;
public int col;
}
public struct Order // 뭔지 모르겠음.
{
public int first;
public int second;
public int third;
}
}
namespace Com.JungBo.MineSet
{
public class MineConsole // 지뢰를 설정함,출력함
{
Block[,] board; // 구조체 사용
Position[] pos;
int boardsize = 10;
int mine = 10;
public MineConsole() // 생성자
{
InitMine(); // 호출
}
private void InitMine() // 지뢰찾기 시작
{
board = new Block[boardsize, boardsize];
pos = new Position[mine];
for (int i = 0; i < boardsize; i++)
{
for (int j = 0; j < boardsize; j++) // 지뢰 정보와 상태를 초기화
{
board[i, j].bState = BombState.EMPTY;
board[i, j].oState = OpenState.CLOSE;
board[i, j].down = false;
}
}
Make(); // 함수가 있길레 사용해봄
Make();
}
private void SetMine()
{
Random r = new Random(); //난수 r 과 변수들 선언
int count = 0;
int row = 0;
int col = 0;
while (true)
{
if (count == mine) break; // 지뢰를 모두 입력하면 멈춤
row = r.Next(boardsize);
col = r.Next(boardsize);
if (board[row, col].bState != BombState.B0) // 지뢰가 없다면 등록
{
pos[count].row = row;
pos[count].col = col;
count++;
board[row, col].bState = BombState.B0;
}
}
Console.WriteLine(count); // 지뢰수(카운팅수) 표시
}
private void CalMine() // 지뢰 찾기
{
for (int t = 0; t < mine; t++) // 지뢰수 만큼 반복
{
int x = pos[t].row; // 지뢰의 위치정보를 읽음
int y = pos[t].col;
for(int i=x-1;i<=x+1;i++) // 행,열 수로 반복
{
for(int j=y-1;j<=y+1;j++)
{
if (i >= 0 && j >= 0 && i <= (boardsize - 1) && j <= (boardsize - 1) && board[i, j].bState != BombState.B0)board[i, j].bState++;
} // 만약 지뢰 위치 근처에 지뢰가 없다면 값을 증가시킴.
}
// Print(); 원리를 알기위해 사용해봄
// Console.WriteLine(); 위와 동문
}
}
public void Make() // 출력화면을 같게하기위해 살짝 바꿈
{
SetMine();
Print();
Console.WriteLine();
CalMine();
Print(); // 추가
Console.WriteLine(); // 추가
}
public void Print() // 출력부분
{
for (int i = 0; i < boardsize; i++) // 행,열 반복
{
for (int j = 0; j < boardsize; j++)
{
if ((int)board[i, j].bState == 12) Console.Write("{0,3}",board[i, j].bState); // 지뢰가 있다면 B0 출력
else Console.Write("{0,3}", (int)(board[i, j].bState)); // 없다면 상태 출력
}
Console.WriteLine(); // 줄바꿈
}
}
}
}
'1학년 2학기 > C언어' 카테고리의 다른 글
WINDOWS API PPT (0) | 2014.12.08 |
---|---|
[C#] 과제 2 - 마방진 이해하기 (0) | 2014.12.05 |
[C#] 과제 1 - 허수의 표현 (0) | 2014.12.05 |
성적처리기 (완료) (0) | 2014.09.28 |