Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[13기 표은서] C# 기초 세션 1차시 과제 제출 #94

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
282 changes: 282 additions & 0 deletions TextRPG_1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
using System;

namespace TextRPG_1
{
class Program
{
enum ClassType
{
None = 0,
Knight = 1,
Archer = 2,
Mage = 3
}

struct Player
{
public int hp;
public int attack;
}

// ClassType, Player를 참고해 MonsterType과 Monster 구조체를 완성해보세요.
// 몬스터 종류
// Slime -> 체력: 20 / 공격력: 2
// Orc -> 체력: 40 / 공격력: 5
// Skeleton -> 체력: 30 / 공격력: 7
enum MonsterType
{
// TODO
None = 0,
Slime = 1,
Orc = 2,
Skeleton = 3
}

struct Monster
{
// TODO
public int hp;
public int attack;
}

// 플레이어 직업 선택 함수
static ClassType ChooseClass()
{
Console.WriteLine("직업을 선택하세요!");
Console.WriteLine("[1]. 검사");
Console.WriteLine("[2]. 궁수");
Console.WriteLine("[3]. 법사");

ClassType choice = ClassType.None;

string input = Console.ReadLine();

switch (input)
{
case "1":
choice = ClassType.Knight;
break;
case "2":
choice = ClassType.Archer;
break;
case "3":
choice = ClassType.Mage;
break;
}

return choice;
}

// 플레이어 생성 함수
static void CreatePlayer(ClassType choice, out Player player)
{
switch (choice)
{
case ClassType.Knight:
player.hp = 100;
player.attack = 10;
break;
case ClassType.Archer:
player.hp = 85;
player.attack = 12;
break;
case ClassType.Mage:
player.hp = 60;
player.attack = 15;
break;
default:
player.hp = 0;
player.attack = 0;
break;
}
}

// 플레이어 생성 함수 (CreatePlayer)를 참고해 몬스터 랜덤 생성 함수 (CreateRandomMonster)를 완성해보세요.
// 1. 1~3 중 랜덤으로 수를 하나 뽑습니다.
// 2. 뽑은 숫자가 1일 경우에는 슬라임, 2일 경우에는 오크, 3일 경우에는 스켈레톤을 생성하는 코드를 짭니다. (CreatePlayer 참고)
// default의 경우 몬스터의 체력을 0, 공격력을 0이로 초기화만 해줍니다.
// 2-1. 이 때 공격력과 체력을 초기화해주는 코드가 case 실행문에 반복적으로 나타납니다. 함수로 따로 빼서 구현해봅시다.
// 3. "(몬스터 이름)이 생성되었습니다."라는 문구를 출력해줍니다. (default의 경우 생략)

// 몬스터 종류
// Slime -> 체력: 20 / 공격력: 2
// Orc -> 체력: 40 / 공격력: 5
// Skeleton -> 체력: 30 / 공격력: 7


static void ResetMonster(MonsterType monsterType, out int hp, out int attack)
{
hp = 0;
attack = 0;

switch (monsterType)
{
case MonsterType.Slime:
hp = 20;
attack = 2;
Console.WriteLine("슬라임이 생성되었습니다.");
break;
case MonsterType.Orc:
hp = 40;
attack = 5;
Console.WriteLine("오크가 생성되었습니다.");
break;
case MonsterType.Skeleton:
hp = 30;
attack = 7;
Console.WriteLine("스켈레톤이 생성되었습니다.");
break;
default:
hp = 0;
attack = 0;
break;
}

}

// 몬스터 랜덤 생성 함수
static void CreateRandomMonster(out Monster monster)
{
// TODO
// Random 객체 생성
Random random = new Random();
// 1~3까지 숫자 랜덤으로 선택
int randomNumber = random.Next(1, 4);

monster.hp = 0;
monster.attack = 0;

// 몬스터 생성
MonsterType monsterType = (MonsterType)randomNumber;
ResetMonster(monsterType, out monster.hp, out monster.attack);
}

// 게임 접속 함수
static void EnterGame(ref Player player)
{
while(true)
{
Console.WriteLine("마을에 접속했습니다!");
Console.WriteLine("[1]. 필드로 간다");
Console.WriteLine("[2]. 로비로 돌아가기");

string choice = Console.ReadLine();
switch (choice)
{
case "1":
EnterField(ref player);
break;
case "2":
return;
}
}
}

// 필드 접속 함수
static void EnterField(ref Player player)
{
Console.WriteLine("필드에 접속했습니다!");

while (true)
{
// 랜덤 몬스터 등장
Monster monster;
CreateRandomMonster(out monster);

Console.WriteLine("[1]. 전투 모드로 돌입");
Console.WriteLine("[2]. 일정 확률로 마을로 도망");

// 사용자 입력을 받아 input에 저장
string input = Console.ReadLine();

if (input == "1")
{
Fight(ref player, ref monster);

//continue를 이용해 플레이어가 사망상태가 아니라면 루프를 계속 돌고 ,
//사망한 상태라면 break로 EnterField를 빠져나가 마을로 가게 함.
if (player.hp > 0)
continue;
break;
}
else
{
// 30퍼센트 확률로 도망에 성공
Random rand = new Random();
int randValue = rand.Next(0, 101);
if (randValue<=33)
{
Console.WriteLine("도망쳤습니다!");
break;
}
// 도망에 실패했다면 전투에 돌입한다.
else
{
Console.WriteLine("도망치지 못합니다!");
Fight(ref player, ref monster);
if (player.hp > 0)
continue;
break;
}
}
}
}

// 무한루프 안을 채워 플레이어 vs 몬스터 전투 함수를 완성해보세요.
// 1. 플레이어가 먼저 몬스터를 공격한 뒤, 몬스터가 플레이어를 공격합니다. 플레이어가 몬스터를 공격할 경우, 몬스터의 체력에서 플레이어의 공격력 수치만큼을 뺍니다. 몬스터가 플레이어를 공격할 경우, 플레이어의 체력에서 몬스터의 공격력 수치만큼을 뺍니다.
// 2. 플레이어가 몬스터를 공격했을 때, 몬스터의 체력이 0 이하가 된다면
// 1. "적을 무찔렀습니다!" 라는 문구를 출력합니다
// 2. 플레이어의 남은 체력을 출력해줍니다 (ex. 남은 체력: 87)
// 3. 무한루프를 빠져나갑니다.
// 3. 몬스터가 플레이어를 공격했을 때, 플레이어의 체력이 0 이하가 된다면 "Game Over... 마을로 돌아갑니다..."를 출력한 뒤 무한루프를 빠져나갑니다.

// 플레이어 vs 몬스터 전투 함수
static void Fight(ref Player player, ref Monster monster)
{
while (true)
{
// TODO
monster.hp -= player.attack;
if (monster.hp > 0 && player.hp > 0)
{
player.hp -= monster.attack;
}
else if (monster.hp <= 0 && player.hp > 0)
{
Console.WriteLine("적을 무찔렀습니다!");
Console.WriteLine("남은 체력: "+player.hp);
break;
}
else if (player.hp <= 0)
{
Console.WriteLine("Game Over... 마을로 돌아갑니다...");
break;
}
}
}

// 메인 함수
static void Main(string[] args)
{
ClassType choice = ClassType.None;

while (true)
{
// 플레이어의 직업을 선택
choice = ChooseClass();

// continue 이용해 ClassType선택이 완료되지 않은 상태라면 이후 코드로 넘어가지 않도록 함.
if (choice == ClassType.None)
continue;

// 플레이어 생성 (체력과 공격력 정보 초기화)
Player player;
CreatePlayer(choice, out player);
Console.WriteLine($"HP:{player.hp} Attack:{player.attack}");

// 게임 시작
EnterGame(ref player);
}
}
}
}
35 changes: 35 additions & 0 deletions TextRPG_1/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TextRPG_1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TextRPG_1")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("0682F3D5-AA47-4112-874C-EF02E2DFF465")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
55 changes: 55 additions & 0 deletions TextRPG_1/TextRPG_1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props"
Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{0682F3D5-AA47-4112-874C-EF02E2DFF465}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TextRPG_1</RootNamespace>
<AssemblyName>TextRPG_1</AssemblyName>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System"/>
<Reference Include="System.Core"/>
<Reference Include="System.Data"/>
<Reference Include="System.Xml"/>
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs"/>
<Compile Include="Properties\AssemblyInfo.cs"/>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->

</Project>
Binary file added TextRPG_1/bin/Debug/TextRPG_1.exe
Binary file not shown.
Binary file added TextRPG_1/bin/Debug/TextRPG_1.pdb
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
27e020e97ae9d4148c555a3db50878e8476112f8
6 changes: 6 additions & 0 deletions TextRPG_1/obj/Debug/TextRPG_1.csproj.FileListAbsolute.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
C:\Users\USER\RiderProjects\TextRPG_1\TextRPG_1\bin\Debug\TextRPG_1.exe
C:\Users\USER\RiderProjects\TextRPG_1\TextRPG_1\bin\Debug\TextRPG_1.pdb
C:\Users\USER\RiderProjects\TextRPG_1\TextRPG_1\obj\Debug\TextRPG_1.csproj.AssemblyReference.cache
C:\Users\USER\RiderProjects\TextRPG_1\TextRPG_1\obj\Debug\TextRPG_1.csproj.CoreCompileInputs.cache
C:\Users\USER\RiderProjects\TextRPG_1\TextRPG_1\obj\Debug\TextRPG_1.exe
C:\Users\USER\RiderProjects\TextRPG_1\TextRPG_1\obj\Debug\TextRPG_1.pdb
Binary file added TextRPG_1/obj/Debug/TextRPG_1.exe
Binary file not shown.
Binary file added TextRPG_1/obj/Debug/TextRPG_1.pdb
Binary file not shown.
Binary file not shown.