1. upDownGame
int targetNumber = new Random().Next(1, 1001); ;
int guess = 0;
int count = 0;
Console.WriteLine("1부터 1000 사이의 숫자를 맞춰보세요.");
while (guess != targetNumber)
{
Console.Write("추측한 숫자를 입력하세요: ");
guess = int.Parse(Console.ReadLine());
count++;
if (guess < targetNumber)
{
Console.WriteLine("좀 더 큰 숫자를 입력하세요.");
}
else if (guess > targetNumber)
{
Console.WriteLine("좀 더 작은 숫자를 입력하세요.");
}
else
{
Console.WriteLine("축하합니다! 숫자를 맞추셨습니다.");
Console.WriteLine("시도한 횟수: " + count);
}
}
랜덤으로 숫자가 결정됩니다.
그 값을 유추하여 숫자를 입력합니다.
적은 숫자가 보다 큰 지 작은 지 알려줍니다.
반복 -> 정답~~!
2. baseBallGame
Random random = new Random(); // 랜덤 객체 생성
int[] numbers = new int[3]; // 3개의 숫자를 저장할 배열
// 3개의 랜덤 숫자 생성하여 배열에 저장
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = random.Next(1, 10);
}
int attempt = 0; // 시도 횟수 초기화
while (true)
{
Console.Write("3개의 숫자를 입력하세요 (1~9): ");
int[] guesses = new int[3]; // 입력한 숫자를 guesses 에 저장
// 사용자가 입력한 숫자 배열에 저장
for (int i = 0; i < guesses.Length; i++)
{
guesses[i] = int.Parse(Console.ReadLine());
}
int correct = 0; // 맞춘 숫자의 개수 초기화
// 숫자 비교 및 맞춘 개수 계산
for (int i = 0; i < numbers.Length; i++)
{
for (int j = 0; j < guesses.Length; j++)
{
if (numbers[i] == guesses[j])
{
correct++;
break;
}
}
}
attempt++; // 시도 횟수 증가
Console.WriteLine("시도 #" + attempt + ": " + correct + "스트라이크!");
// 모든 숫자를 맞춘 경우 게임 종료
if (correct == 3)
{
Console.WriteLine("아웃!!!!");
break;
}
}
숫자를 랜덤으로 3개 정합니다.
그 숫자를 유추하여 3번에 걸쳐 숫자를 입력합니다. (1~9)
맞춘 갯수 = (n스트라이크) 라고 나옵니다.
3개 다 한번에 맞출 때 까지 플레이~
다 맞추면 아웃~
이게임을 만들면서 어렸을 때 많이 했던 숫자야구 게임이 생각나서 그렇게 이름 붙여 봤다.
한가지 아쉬운 점은 야구 게임에 '볼'을 넣었어야 했나 이런 생각이다.
다른 공부 하기 바빠서 다음에 시간나면 만들어 보긴 해야겠다.
3. tickTackToe
namespace tickTackToe
{
internal class Program
{
static char[] choice = { '1', '2', '3', '4', '5', '6', '7', '8', '9' }; //좌표지정
static int turn = 1; //누구의 차례인지 판별하기 위해
static int turnCount = 1; // 천장만들라고
static int playerInput = 0; //입력값
static int fail = 0; //입력값 벗어난 값 잡기
static int isGameOver = 0;
static bool playerOneWin = false;
static bool playerTwoWin = false;
static char winOX = ' '; //승패 판별
static void Mapping()
{
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2} ", choice[0], choice[1], choice[2]);
Console.WriteLine("_____|_____|______");
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2} ", choice[3], choice[4], choice[5]);
Console.WriteLine("_____|_____|______");
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2} ", choice[6], choice[7], choice[8]);
Console.WriteLine(" | | \n");
}//Mapping()
static void Main(string[] args)
{
//do ~while = do 부분 무조건 한번은 실행 후 while(조건)
do
{
Console.Clear();
Console.WriteLine("1p : O , 2p : X\n");
Console.WriteLine("{0}p turn\n", turn);
Mapping(); //맵 찍기
switch (fail) //입력한 값 벗어나면 실행
{
case 1:
Console.WriteLine("이선좌;;\n");
break;
case 2:
Console.WriteLine("1~9만 선택하세요~\n");
break;
}
playerInput = int.Parse(Console.ReadLine()); // 입력 값 int 로 변환
Select();
IsGameOver();
} while (isGameOver == 0); //true 값 미리 세팅
}//Main()
static void Select()
{
if (playerInput < 1 || playerInput > 9)
fail = 2;
else if (choice[playerInput - 1] == 'O' || choice[playerInput - 1] == 'X')
{
fail = 1;
}
else
{
if (turn == 1)
{
choice[playerInput - 1] = 'O';
}
else if (turn == 2)
{
choice[playerInput - 1] = 'X';
}
//좌표 지정 완료
//종료 시점 체크
turnCount++;
fail = 0;
//turn 1일때 2됨 2일때 1됨
switch (turn)
{
case 1:
turn = 2;
break;
case 2:
turn = 1;
break;
}
}
}//Select()
//012,345,678 가로
//036,147,258 세로
//048,246 대각ㄷ
static void IsGameOver()
{
if (fail == 0)
{
if ((choice[0] == choice[1]) && (choice[0] == choice[2]))
{
isGameOver = 1;
winOX = choice[0];
}
else if ((choice[3] == choice[4]) && (choice[3] == choice[5]))
{
isGameOver = 1;
winOX = choice[3];
}
else if ((choice[6] == choice[7]) && (choice[6] == choice[8]))
{
isGameOver = 1;
winOX = choice[6];
}
else if ((choice[0] == choice[3]) && (choice[0] == choice[6]))
{
isGameOver = 1;
winOX = choice[0];
}
else if ((choice[1] == choice[4]) && (choice[1] == choice[7]))
{
isGameOver = 1;
winOX = choice[1];
}
else if ((choice[2] == choice[5]) && (choice[2] == choice[8]))
{
isGameOver = 1;
winOX = choice[2];
}
else if ((choice[0] == choice[4]) && (choice[0] == choice[8]))
{
isGameOver = 1;
winOX = choice[0];
}
else if ((choice[2] == choice[4]) && (choice[2] == choice[6]))
{
isGameOver = 1;
winOX = choice[2];
}
// 9칸 다 채워지면 게임 종료
else if ((isGameOver == 0) && (turnCount >= 9))
{
isGameOver = 1;
Console.WriteLine("무승부 입니다.");
}
if (winOX == 'O')
playerOneWin = true;
else if (winOX == 'X')
playerTwoWin = true;
if (playerOneWin)
Console.WriteLine("플레이어 1이 이겼습니다.");
else if (playerTwoWin)
Console.WriteLine("플레이어 2이 이겼습니다.");
}
}//IsGameOver()
}
}
게임이 시작되고 첫번째 선택
원하는 숫자 입력 (1~9) 그 외 숫자 입력시 오류메세지 나옵니다 (1~9만 선택하세요~)
알 맞게 입력하면 그자리 숫자대신 1턴 0 지워지고 2턴은 x 새겨진다
다시 1턴 선택 이미 입력한 숫자 입력시 오류메세지 나옵니다 (이선좌;;)
줄 잘 맞춰서 이으면 승리~
'Today I Learned' 카테고리의 다른 글
[내일배움캠프] C# 문법 종합반 (인터페이스와 열거형) (0) | 2023.11.09 |
---|---|
[내일배움캠프] C# 문법 종합반 - 메서드(Methods) (0) | 2023.11.08 |
[내일배움캠프] C# 문법 종합반 - 1주차 숙제풀이 (1) | 2023.11.06 |
<TIL> [내일배움캠프] 본과정 5일차 (미니프로젝트 마무리) (0) | 2023.11.03 |
<TIL> [내일배움캠프] 본과정 4일차 (0) | 2023.11.02 |