문자열의 포맷팅
ex)
string name = "조성훈";
int age = 27;
Console.WriteLine($"나의 이름은 {name}이고 {age}세 입니다."}
출력값: 나의 이름은 조성훈이고 27세 입니다.
break >> 자신을 감싸고 있는 한개의 구문을 벗어남.
숫자 맞추기 게임 만들기
static void Main()
{
Random random = new Random();
int randomNum = random.Next(0,10);
Console.WriteLine("0~9까지 숫자를 입력해 주세요.");
int choiceNum = ChooseNum();
while (choiceNum != randomNum)
{
Inconsistency(choiceNum, randomNum);
choiceNum = ChooseNum();
}
Console.WriteLine("숫자를 맞추셨습니다.");
}
private static int ChooseNum()
{
int choiceNum = int.Parse(Console.ReadLine());
while (choiceNum < 0 || choiceNum > 9)
{
Console.WriteLine("0~9까지 숫자를 입력해 주세요.");
choiceNum = int.Parse(Console.ReadLine());
}
return choiceNum;
}
private static void Inconsistency(int choiceNum, int computerNum)
{
if(choiceNum > computerNum)
{
Console.WriteLine("보다 작습니다.");
}
else
{
Console.WriteLine("보다 큽니다.");
}
}
틱택토 게임 만들기
static void Main()
{
char[,] ticTacToe = new char[3, 3];
Random ramdom = new Random();
Console.WriteLine("O를 넣을 위치 숫자를 입력해주세요.\n 1 | 2 | 3\n-----------------\n 4 | 5 | 6\n-----------------\n 7 | 8 | 9");
int choicePos;
int computerChoice;
int whoWin=0;
List index = new List();
for (int i = 0; i < 9; i++)
index.Add(i);
while (index.Count != 0)
{
choicePos = ChoicePos(index);
index.Remove(choicePos);
Console.WriteLine("\n나의 선택");
ShowTicTacToe(choicePos, ticTacToe ,'O');
whoWin = GameOver(ticTacToe);
if (whoWin == 1 || index.Count == 0)
{
break;
}
computerChoice = ramdom.Next(9);
while (!index.Contains(computerChoice))
{
computerChoice = ramdom.Next(9);
}
index.Remove(computerChoice);
Console.WriteLine("\n\n컴퓨터의 선택");
ShowTicTacToe(computerChoice, ticTacToe, 'X');
whoWin = GameOver(ticTacToe);
if (whoWin == 2)
{
break;
}
Console.WriteLine("\n\nO를 넣을 위치 숫자를 입력해주세요.");
}
result(whoWin);
}
private static int ChoicePos(List index)
{
int choicePos;
int.TryParse(Console.ReadLine(), out choicePos);
bool isNULL = index.Contains(choicePos-1);
while ( !isNULL)
{
if (choicePos >= 1 && choicePos <= 9)
{
Console.WriteLine("해당 자리는 이미 입력되었습니다..");
}
else
{
Console.WriteLine("1~9까지의 숫자를 입력해 주세요.");
}
Console.WriteLine();
int.TryParse(Console.ReadLine(), out choicePos);
isNULL = index.Contains(choicePos-1);
}
return choicePos - 1;
}
private static void ShowTicTacToe(int choicePos, char[,] ticTacToe, char inChar)
{
ticTacToe[(choicePos / 3), (choicePos % 3)]= inChar;
for (int i = 0; i <3; i++)
{
for (int j = 0; j <3; j++)
{
if (ticTacToe[i,j]=='\0')
Console.Write(" ");
else
Console.Write(" {0} ", ticTacToe[i, j]);
if (j == 0 || j == 1)
Console.Write("|");
}
if(i == 0 || i == 1)
Console.WriteLine("\n-----------------");
}
}
private static int GameOver(char[,] ticTacToe)
{
int whoWin = 0;
if( (ticTacToe[0, 0] == ticTacToe[1, 0] && ticTacToe[1, 0] == ticTacToe[2, 0] && ticTacToe[2, 0] == 'O') ||
(ticTacToe[0, 1] == ticTacToe[1, 1] && ticTacToe[1, 1] == ticTacToe[2, 1] && ticTacToe[2, 1] == 'O') ||
(ticTacToe[0, 2] == ticTacToe[1, 2] && ticTacToe[1, 2] == ticTacToe[2, 2] && ticTacToe[2, 2] == 'O') ||
(ticTacToe[0, 0] == ticTacToe[0, 1] && ticTacToe[0, 1] == ticTacToe[0, 2] && ticTacToe[0, 2] == 'O') ||
(ticTacToe[1, 0] == ticTacToe[1, 1] && ticTacToe[1, 1] == ticTacToe[1, 2] && ticTacToe[1, 2] == 'O') ||
(ticTacToe[2, 0] == ticTacToe[2, 1] && ticTacToe[2, 1] == ticTacToe[2, 2] && ticTacToe[2, 2] == 'O') ||
(ticTacToe[0, 0] == ticTacToe[1, 1] && ticTacToe[1, 1] == ticTacToe[2, 2] && ticTacToe[2, 2] == 'O') ||
(ticTacToe[0, 2] == ticTacToe[1, 1] && ticTacToe[1, 1] == ticTacToe[2, 0] && ticTacToe[2, 0] == 'O')
)
{
whoWin = 1;
}
else if( (ticTacToe[0, 0] == ticTacToe[1, 0] && ticTacToe[1, 0] == ticTacToe[2, 0] && ticTacToe[2, 0] == 'X') ||
(ticTacToe[0, 1] == ticTacToe[1, 1] && ticTacToe[1, 1] == ticTacToe[2, 1] && ticTacToe[2, 1] == 'X') ||
(ticTacToe[0, 2] == ticTacToe[1, 2] && ticTacToe[1, 2] == ticTacToe[2, 2] && ticTacToe[2, 2] == 'X') ||
(ticTacToe[0, 0] == ticTacToe[0, 1] && ticTacToe[0, 1] == ticTacToe[0, 2] && ticTacToe[0, 2] == 'X') ||
(ticTacToe[1, 0] == ticTacToe[1, 1] && ticTacToe[1, 1] == ticTacToe[1, 2] && ticTacToe[1, 2] == 'X') ||
(ticTacToe[2, 0] == ticTacToe[2, 1] && ticTacToe[2, 1] == ticTacToe[2, 2] && ticTacToe[2, 2] == 'X') ||
(ticTacToe[0, 0] == ticTacToe[1, 1] && ticTacToe[1, 1] == ticTacToe[2, 2] && ticTacToe[2, 2] == 'X') ||
(ticTacToe[0, 2] == ticTacToe[1, 1] && ticTacToe[1, 1] == ticTacToe[2, 0] && ticTacToe[2, 0] == 'X')
)
{
whoWin = 2;
}
return whoWin;
}
private static void result(int whoWin)
{
if (whoWin == 1)
{
Console.WriteLine("사람이 이겼습니다.");
}
else if (whoWin == 2)
{
Console.WriteLine("컴퓨터가 이겼습니다..");
}
else
{
Console.WriteLine("무승부입니다.");
}
}