자 이제 가위 바위 보를 해볼까요?

자 다시 문제를 봅시다

플레이어는 (0)가위 (1)바위 (2)보 중에 하나를 내고 컴퓨터도 임의로 하나를 내어서 승패를 결정하는 게임입니다. 입력시 -1 을 입력하면 게임을 종료합니다.

게임이 종료되면 총 게임횟수와 이긴횟수 진횟수 비긴횟수 및 승률을 출력해 줍니다.

저는 이렇게 생각 했습니다.

1. 저는 플레이어에 대해서 클래스를 만들것 입니다. 리턴형으로 제가 낸 -1 ,0,1,2 를 입력받아 리턴하는 메서드를 만들것입니다.

2. 두번째는 컴퓨터 클래스를 만들것 입니다. 랜덤으로 0~2까지 숫자중 하나를 생성해서 리턴하는 매서드를 만들것 입니다.

3.세번째로는 이제 메인 클래스에서 컴퓨터객체 플레이어 객체를 생성 이긴 횟수 진 횟수를 선언 하겠습니다

4.값을 가져오는 메서드, 이겼는지 졌는지 플레이를 보여주는 메서드,게임이 이겼는지 졌는지를 계산하는 메서드,다 끝나고 게임을 보여주는 메서드를 선언하겠습니다.

5.마지막 run 메서드에 -1이 될때까지 하며 -1이 되멸 끝내게끔 하겠습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace RSPcs
{
    class Program
    {
        Computer m_com;
        Player m_player;
        int m_total;
        int m_win;
        int m_lost;
        public Program()
        {
            m_com = new Computer();
            m_player = new Player();
            m_total = 0;
            m_win = 0;
            m_lost = 0;
        }
        String getValue(int a)
        {
            String msg;
            if(0==a)
            {
                msg = "가위";
            }
            else if(1==a)
            {
                msg = "바위";
            }
            else
            {
                msg = "보";
            }
            return msg;
        }
        void showPlay(int com,int p,int result)
        {
            String msg;
 
            switch(result)
            {
                case 0:
                    msg = "무승부";
                    break;
                case 1:
                    msg = "이겼습니다";
                    break;
                default:
                    msg = "졌습니다";
                    break;
            }
            Console.WriteLine("컴퓨터 " + getValue(com) + "플레이어 " + getValue(p));
            Console.WriteLine("결과 " + msg);
        }
        void showEndGame()
        {
            int equal;
            float rate;
 
            equal = m_total - (m_win + m_lost);
            rate = (float)m_win / m_total;
 
            Console.WriteLine("-------------");
            Console.WriteLine(" 총 게임수"+m_total);
            Console.WriteLine(" 이긴수"+m_win);
            Console.WriteLine(" 비긴수"+equal);
            Console.WriteLine(" 진수"+m_lost);
            Console.WriteLine(" 승률"+rate);
            Console.WriteLine("-------------");
 
        }
        int checkGame(int com,int my)
        {
            int a;
 
            a = 0;
            m_total++;
 
            if(com == my)
            {
                a = 0;
            }
            else if(0 == com && 1 == my)
            {
                a = 1;
                m_win++;
            }
            else if (0 == com && 2 == my)
            {
                a = 2;
                m_lost++;
            }
            else if (1 == com && 0 == my)
            {
                a = 2;
                m_win++;
            }
            else if (1 == com && 2 == my)
            {
                a = 1;
                m_win++;
            }
            else if (2 == com && 0 == my)
            {
                a = 1;
                m_win++;
            }
            else if (2 == com && 1 == my)
            {
                a = 2;
                m_lost++;
            }
            return a;
        }
        void run()
        {
            int com;
            int p;
            int result;
 
            while(true)
            {
                p = m_player.input();
                com = m_com.go();
 
                if(-1==p)
                {
                    break;
                }
                result = checkGame(com, p);
                showPlay(com, p, result);
            }
            showEndGame();
        }
        static void Main(string[] args)
        {
            Program m = new Program();
            //m.Program();
            m.run();
        }
    }
}
 
cs


결과 화면





+ Recent posts