본문 바로가기

코딩 공부/Unity C#

3주차 과제 블랙잭, 카드 나눠준 후 카드 출력

과제설명

이 과제에서는 네 명의 플레이어에게 카드 한 장을 나눠주고, 같은 네 명의 플레이어에게 다른 카드를 나눠준 다음, 두 명의 플레이어에게 카드를 한 장 더 나눠주어야 합니다. 마지막으로 카드를 뒤집어서 각 플레이어의 카드를 인쇄합니다.

 

왜 중요한가요?

이 과제는 클래스와 객체를 사용하는 귀중한 연습을 제공합니다. 유니티 게임(곧 출시 예정!)을 작성할 때 객체 지향 프로그래밍을 사용하므로 클래스와 오브젝트를 사용하는 방법을 이해해야 합니다.

 

답안 작성

- player라는 명의 Deck, 혹은 List<Card> 타입 변수를 생성해야 할까? 라는 고민을 하였으나,

카드를 추가하고 플레이어의 카드를 각각 출력하는 과정이 복잡할 것 같아 포기함.

using System;
using System.Collections.Generic;
using ConsoleCards;

namespace ProgrammingAssignment3
{
    // IMPORTANT: Only add code in the section
    // indicated below. The code I've provided
    // makes your solution work with the 
    // automated grader on Coursera

    /// <summary>
    /// Programming Assignment 3
    /// </summary>
    class Program
    {
        /// <summary>
        /// Programming Assignment 3
        /// </summary>
        /// <param name="args">command-line args</param>
        static void Main(string[] args)
        {
            // loop while there's more input
            string input = Console.ReadLine();
            while (input[0] != 'q')
            {
                // Add your code between this comment
                // and the comment below. You can of
                // course add more space between the
                // comments as needed

                // declare a deck variables and create a deck object
                // DON'T SHUFFLE THE DECK
                Deck deck = new Deck();

                // deal 2 cards each to 4 players (deal properly, dealing
                // the first card to each player before dealing the
                // second card to each player)
                Card card1 = deck.TakeTopCard();
                Card card2 = deck.TakeTopCard();
                Card card3 = deck.TakeTopCard();
                Card card4 = deck.TakeTopCard();
                Card card5 = deck.TakeTopCard();
                Card card6 = deck.TakeTopCard();
                Card card7 = deck.TakeTopCard();
                Card card8 = deck.TakeTopCard();

                // deal 1 more card to players 2 and 3
                Card card9 = deck.TakeTopCard();
                Card card10 = deck.TakeTopCard();

                // flip all the cards over
                card1.FlipOver();
                card2.FlipOver();
                card3.FlipOver();
                card4.FlipOver();
                card5.FlipOver();
                card6.FlipOver();
                card7.FlipOver();
                card8.FlipOver();
                card9.FlipOver();
                card10.FlipOver();

                // print the cards for player 1
                Console.WriteLine(card1.Rank + "," + card1.Suit);
                Console.WriteLine(card5.Rank + "," + card5.Suit);

                // print the cards for player 2
                Console.WriteLine(card2.Rank + "," + card2.Suit);
                Console.WriteLine(card6.Rank + "," + card6.Suit);
                Console.WriteLine(card9.Rank + "," + card9.Suit);

                // print the cards for player 3
                Console.WriteLine(card3.Rank + "," + card3.Suit);
                Console.WriteLine(card7.Rank + "," + card7.Suit);
                Console.WriteLine(card10.Rank + "," + card10.Suit);

                // print the cards for player 4
                Console.WriteLine(card4.Rank + "," + card4.Suit);
                Console.WriteLine(card8.Rank + "," + card8.Suit);

                // Don't add or modify any code below
                // this comment
                input = Console.ReadLine();
            }
        }
    }
}

답안 피드백

- 변수명 card5 >>> player1card2 정도의 차이만 있고 나머지 동일

 

'코딩 공부 > Unity C#' 카테고리의 다른 글

스프라이트 및 게임 오브젝트  (0) 2024.06.05
유니티 기본 구성  (1) 2024.06.04
주사위 굴리기  (0) 2024.06.03
C# 클래스 생성자 사용 및 속성  (0) 2024.06.03
클래스 및 객체  (0) 2024.06.03