챕터3: 과제
📌 스네이크 게임
namespace SnakeGame
{
using System;
using System.Collections.Generic;
using System.Threading;
class Program
{
#region Main
static void Main(string[] args)
{
ConsoleKeyInfo keys;
WallDraw();
Point p = new Point(4, 5, '*');
Snake snake = new Snake(p, 4, Direction.RIGHT);
snake.Draw();
FoodCreator foodCreator = new FoodCreator(80, 20, '$');
Point food = foodCreator.CreateFood();
food.Draw();
while (true)
{
if (Console.KeyAvailable)
{
keys = Console.ReadKey(true);
switch(keys.Key)
{
case ConsoleKey.UpArrow:
snake.direction = Direction.UP;
break;
case ConsoleKey.DownArrow:
snake.direction = Direction.DOWN;
break;
case ConsoleKey.LeftArrow:
snake.direction = Direction.LEFT;
break;
case ConsoleKey.RightArrow:
snake.direction = Direction.RIGHT;
break;
default:
break;
}
}
snake.Move();
if (snake.CrashWall() || snake.CrashBody()) break;
Point snakeHead = snake.body[0];
if(food.IsHit(snakeHead))
{
snake.EatFood();
food = foodCreator.CreateFood();
food.Draw();
}
snake.Draw();
Thread.Sleep(100);
Console.SetCursorPosition(0, 22);
Console.Write($"현재 몸의 길이 : {snake.body.Count} 먹은 음식의 수 : {snake.foodCount}");
}
}
static void WallDraw()
{
for(int x = 0; x < 80; x++)
{
Console.SetCursorPosition(x, 0);
Console.Write('#');
Console.SetCursorPosition(x, 20);
Console.Write('#');
}
for(int y = 0; y < 21; y++)
{
Console.SetCursorPosition(0, y);
Console.Write('#');
Console.SetCursorPosition(80, y);
Console.Write('#');
}
}
}
#endregion
#region Snake
public class Snake
{
public Direction direction { get; set; }
public List<Point> body;
public int foodCount { get; set; }
public Snake(Point head, int size, Direction direction)
{
this.direction = direction;
this.body = new List<Point>();
body.Add(head);
for (int i = 0; i < size-1; i++)
{
Point p = new Point(head.x, head.y, head.sym);
body.Add(p);
}
}
public void Draw()
{
foreach(Point p in body)
{
p.Draw();
}
}
public void Move()
{
Point head = body[0];
int x = head.x, y = head.y;
switch(direction)
{
case Direction.RIGHT:
x += 1;
break;
case Direction.LEFT:
x -= 1;
break;
case Direction.UP:
y -= 1;
break;
case Direction.DOWN:
y += 1;
break;
default:
break;
}
body.Insert(0, new Point(x, y, head.sym));
body[^1].Clear();
body.RemoveAt(body.Count - 1);
}
public void EatFood()
{
Point tail = body[^1];
Point realTail = new Point(tail.x, tail.y, tail.sym);
body.Add(realTail);
foodCount++;
}
public bool CrashWall()
{
if (body[0].x <= 0 || body[0].x >= 80 || body[0].y <= 0 || body[0].y >= 20) return true;
else return false;
}
public bool CrashBody()
{
for(int i = 1; i < body.Count; i++)
{
if (body[0].IsHit(body[i])) return true;
}
return false;
}
}
#endregion
#region FoodCretor
public class FoodCreator
{
public int width { get; set; }
public int height { get; set; }
public char sym { get; set; }
Random food = new Random();
public FoodCreator(int width, int height, char sym)
{
this.width = width;
this.height = height;
this.sym = sym;
}
public Point CreateFood()
{
int x = food.Next(1, width - 1);
int y = food.Next(1, height - 1);
return new Point(x,y,sym);
}
}
#endregion
#region Point
public class Point
{
public int x { get; set; }
public int y { get; set; }
public char sym { get; set; }
public Point(int _x, int _y, char _sym)
{
x = _x;
y = _y;
sym = _sym;
}
public void Draw()
{
Console.SetCursorPosition(x, y);
Console.Write(sym);
}
public void Clear()
{
sym = ' ';
Draw();
}
public bool IsHit(Point p)
{
return p.x == x && p.y == y;
}
}
public enum Direction
{
LEFT,
RIGHT,
UP,
DOWN
}
#endregion
}
📌 블랙잭 게임
namespace BlackJack
{
public enum Suit { Hearts, Diamonds, Clubs, Spades }
public enum Rank { Two = 2, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace}
internal class Program
{
static void Main(string[] args)
{
BlackJack blackJack = new BlackJack();
blackJack.StartGame();
}
}
public class BlackJack
{
public Deck deck { get; set; }
public Player player { get; set; }
public Dealer dealer { get; set; }
public Card card { get; set; }
public void StartCard()
{
deck = new Deck();
player = new Player();
dealer = new Dealer();
for (int i = 0; i < 2; i++)
{
player.DrawCardFromDeck(deck);
dealer.DrawCardFromDeck(deck);
Console.Write($"{player.playerCard} ");
}
}
public void StartGame()
{
int choice = 0;
Console.WriteLine("BlackJack게임을 시작합니다.\n");
Console.WriteLine("카드를 섞습니다...... \n");
Console.Write("플레이어의 카드 : ");
StartCard();
Console.WriteLine();
Console.WriteLine($"플레이어의 점수 : {player.Score()}점");
Console.WriteLine($"딜러의 점수 : {dealer.Score()}점\n");
while(true)
{
Console.Write("카드 한장 뽑는다. -> 1 승부를 한다. -> 2 \n");
Console.Write("입렵 해주십시오. : ");
choice = int.Parse(Console.ReadLine());
if (choice == 1)
{
player.DrawCardFromDeck(deck);
dealer.DrawCardFromDeck(deck);
Console.WriteLine();
Console.WriteLine($"플레이어가 뽑은 카드 : {player.playerCard}");
Console.WriteLine($"플레이어의 점수 : {player.Score()}점");
if (dealer.Score() > 17)
{
Console.WriteLine("딜러가 승부를 걸었습니다!\n");
if (Math.Abs(21 - player.Score()) < Math.Abs(21 - dealer.Score()))
{
Console.WriteLine($"플레이어의 점수 : {player.Score()}");
Console.WriteLine($"딜러의 점수 : {dealer.Score()}");
Console.WriteLine("플레이어가 이겼습니다!");
break;
}
else
{
Console.WriteLine($"플레이어의 점수 : {player.Score()}");
Console.WriteLine($"딜러의 점수 : {dealer.Score()}");
Console.WriteLine("딜러가 이겼습니다...");
break;
}
}
}
else if (choice == 2)
{
if (Math.Abs(21 - player.Score()) < Math.Abs(21 - dealer.Score()))
{
Console.WriteLine($"플레이어의 점수 : {player.Score()}");
Console.WriteLine($"딜러의 점수 : {dealer.Score()}");
Console.WriteLine("플레이어가 이겼습니다!");
break;
}
else
{
Console.WriteLine($"플레이어의 점수 : {player.Score()}");
Console.WriteLine($"딜러의 점수 : {dealer.Score()}");
Console.WriteLine("딜러가 이겼습니다...");
break;
}
}
else
{
Console.WriteLine("잘못된 입력.");
Console.ReadLine();
}
}
Console.WriteLine();
}
}
public class Card
{
public Suit suit { get; private set; }
public Rank rank { get; private set; }
public Card (Suit suit, Rank rank)
{
this.suit = suit;
this.rank = rank;
}
public int GetValue()
{
if ((int)rank <= 10)
{
return (int)rank;
}
else if ((int)rank <= 13)
{
return 10;
}
else
{
return 11;
}
}
public override string ToString()
{
return $"{rank} of {suit}";
}
}
public class Deck
{
List<Card> cards;
public Deck()
{
cards = new List<Card>();
foreach (Suit suit in Enum.GetValues(typeof(Suit)))
{
foreach (Rank rank in Enum.GetValues(typeof(Rank)))
{
Card card = new Card(suit, rank);
cards.Add(card);
}
}
Shuffle();
}
public void Shuffle()
{
Random rand = new Random();
for (int i = 0; i < cards.Count; i++)
{
int j = rand.Next(i, cards.Count);
Card temp = cards[i];
cards[i] = cards[j];
cards[j] = temp;
}
}
public Card DrawCard()
{
Card card = cards[0];
cards.RemoveAt(0);
return card;
}
}
public class Hand
{
private List<Card> cards;
public Hand()
{
cards = new List<Card>();
}
public void AddCard(Card card)
{
cards.Add(card);
}
public int GetTotalValue()
{
int total = 0;
int aceCount = 0;
foreach (Card card in cards)
{
if (card.rank == Rank.Ace)
{
aceCount++;
}
total += card.GetValue();
}
while (total > 21 && aceCount > 0)
{
total -= 10;
aceCount--;
}
return total;
}
}
public class Player
{
public Hand hand { get; private set; }
public string playerCard = " ";
public int score = 0;
public Player()
{
hand = new Hand();
}
public Card DrawCardFromDeck(Deck deck)
{
Card drawnCard = deck.DrawCard();
hand.AddCard(drawnCard);
playerCard = drawnCard.ToString();
score += drawnCard.GetValue();
return drawnCard;
}
public int Score()
{
score = hand.GetTotalValue();
return score;
}
}
public class Dealer
{
public Hand hand { get; private set; }
public int score = 0;
public Dealer()
{
hand = new Hand();
}
public Card DrawCardFromDeck(Deck deck)
{
Card drawnCard = deck.DrawCard();
hand.AddCard(drawnCard);
return drawnCard;
}
public int Score()
{
score = hand.GetTotalValue();
return score;
}
}
}