Skip to content

Instantly share code, notes, and snippets.

@Gumball12
Last active April 21, 2019 04:23
Show Gist options
  • Save Gumball12/521f6dcac42282e84d1ff98a9cce4863 to your computer and use it in GitHub Desktop.
Save Gumball12/521f6dcac42282e84d1ff98a9cce4863 to your computer and use it in GitHub Desktop.
c# assignment week7
using System;
public abstract class Game
{
// instance variables
protected int userWin, computerWin, drawn;
// constructor
public Game ()
{
userWin = computerWin = drawn = 0;
}
public void recordPrint ()
{
Console.WriteLine($"당신은 컴퓨터와의 게임에서 {userWin}승 {computerWin}패 {drawn}무승부 입니다.");
}
// declare abstract method
public abstract void result();
// destructor
~Game ()
{
recordPrint();
}
}
using System;
public class Mjb: Game
{
// instance variables
private bool userTurn = true;
private int user, computer;
// methods
public void play ()
{
do
{
if (input())
{
if (userTurn)
{
Console.WriteLine("공격자는 유저입니다.");
} else
{
Console.WriteLine("공격자는 컴퓨터입니다.");
}
com();
result();
}
} while (user != 0);
// recordPrint();
}
public bool input()
{
Console.Write("\n입력하세요 [가위(1), 바위(2), 보(3), 종료(0)]: ");
user = Convert.ToInt32(Console.ReadLine());
return user != 0;
}
public void com()
{
// get random value ([1-3])
computer = new Random().Next(1, 4);
comPrint(computer);
}
public void comPrint(int com)
{
Console.Write("\n>> 컴퓨터는 ");
switch (com)
{
case 1:
Console.Write("가위");
break;
case 2:
Console.Write("바위");
break;
case 3:
Console.Write("보");
break;
}
Console.WriteLine("를 냈습니다.");
}
public int judgement()
{
if (user == computer)
{
Console.WriteLine(">> 비겼습니다.");
return ++drawn;
}
else if (
(user == 1 && computer == 3) ||
(user == 2 && computer == 1) ||
(user == 3 && computer == 2)
)
{
Console.WriteLine(">> 이겼습니다.");
if (userTurn == true)
{
userWin++;
} else
{
userTurn = true;
}
return userWin;
}
else
{
Console.WriteLine(">> 졌습니다.");
if (userTurn == false)
{
computerWin++;
} else
{
userTurn = true;
}
return computerWin;
}
}
public override void result()
{
judgement();
}
}
using System;
class Problem_1
{
static void Main(string[] args)
{
// create DarkTempler instance
ProtossUnit dt = new DarkTempler();
// get states
int damage = dt.getDamage();
int strength = dt.getStrength();
int shield = dt.getShield();
// write states
Console.WriteLine($"종족: {dt.getClan()}");
Console.WriteLine($"이름: {dt.getName()}");
Console.WriteLine($"쉴드: {shield}");
Console.WriteLine($"체력: {strength}");
Console.WriteLine($"데미지: {damage.ToString()}");
Console.WriteLine($"스킬: {dt.getSkill()}");
Console.WriteLine(); // break line
// create Nexus instances
Nexus n1 = new Nexus(40);
Nexus m1 = new Nexus(30, 30);
Console.WriteLine(); // break line
// create Nexus instances
Nexus n2 = new Nexus(50);
Nexus m2 = new Nexus(300, 300);
Console.WriteLine(); // break line
}
}
// define Unit interface
interface Unit
{
// declare abstract methods
int getStrength();
string getName();
string getSkill();
string getClan();
int getDamage();
}
// deinfe ProtossUnit (inheritance Unit interface)
abstract class ProtossUnit: Unit
{
// instance variable
private string clan;
// constructor
public ProtossUnit (string clan)
{
this.clan = clan;
}
// declare abstract methods
public abstract int getStrength();
public abstract string getName();
public abstract int getDamage();
public abstract string getSkill();
public abstract int getShield();
// implementing abstract methods
public string getClan ()
{
return clan;
}
}
// define DarkTempler class (inheritance ProtossUnit)
class DarkTempler : ProtossUnit
{
// instnace variable
private string name;
// constructor
public DarkTempler (): base ("프로토스")
{
this.name = "다크템플러";
}
// implementing abstract methods
public override int getStrength()
{
return 40;
}
public override string getName()
{
return name;
}
public override int getDamage()
{
return 45;
}
public override string getSkill()
{
return "클로킹";
}
public override int getShield ()
{
return 80;
}
}
// define Nexus class
class Nexus
{
// constructor
public Nexus (int mineral)
{
if (mineral >= 50)
{
Console.WriteLine("프로브 생산");
} else
{
Console.WriteLine("광물이 부족합니다.");
}
}
// constructor (overloading)
public Nexus (int mineral, int gas)
{
if (mineral >= 300 && gas >= 300)
{
Console.WriteLine("모선 생산");
} else
{
Console.WriteLine("광물 또는 가스가 부족합니다.");
}
}
}
using System;
class Problem_2
{
static void Main(string[] args)
{
Console.Write("선택하세요 [가위바위보(1), 묵찌빠(2)]: ");
switch (Console.ReadLine())
{
case "1":
new Srp().play();
break;
case "2":
new Mjb().play();
break;
default:
Console.WriteLine("잘못 입력하셨습니다.");
break;
}
}
}
using System;
public class Srp: Game
{
// define constant
protected const int MAX = 3;
// define instance variables
protected int user, computer;
// define methods
public void play ()
{
do
{
if (input())
{
com();
result();
}
} while (user != 0);
// recordPrint();
}
public bool input ()
{
Console.Write("\n입력하세요 [가위(1), 바위(2), 보(3), 종료(0)]: ");
user = Convert.ToInt32(Console.ReadLine());
return user != 0;
}
public void com ()
{
// get random value ([1-3])
computer = new Random().Next(1, MAX + 1);
comPrint(computer);
}
public void comPrint (int com)
{
Console.Write("\n>> 컴퓨터는 ");
switch (com)
{
case 1:
Console.Write("가위");
break;
case 2:
Console.Write("바위");
break;
case 3:
Console.Write("보");
break;
}
Console.WriteLine("를 냈습니다.");
}
public int judgement ()
{
if (checkDraw())
{
Console.WriteLine(">> 비겼습니다.");
return ++drawn;
} else if (
(user == 1 && computer == 3) ||
(user == 2 && computer == 1) ||
(user == 3 && computer == 2)
)
{
Console.WriteLine(">> 이겼습니다.");
return ++userWin;
} else
{
Console.WriteLine(">> 졌습니다.");
return ++computerWin;
}
}
public bool checkDraw ()
{
return user == computer;
}
public override void result ()
{
judgement();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment