Head First - 경마를 변형한 프로그램



Bet.cs


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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace SnailRaceSimulator
{
    public class Bet
    {
        public int m_iAmount, m_iSnail;
        public Guy Bettor;
 
        public Bet(int _amount, int _Snail, Guy _bettor)
        {
            m_iAmount = _amount;
            m_iSnail = _Snail;
            Bettor = _bettor;
        }
 
        public string GetDescription()
        {
            string description = "";
 
            if (m_iAmount > 0)
            {
                description = String.Format(Bettor.m_strName + " bets " + m_iAmount + " on Snail #" + m_iSnail);
            }
            else
            {
                description = String.Format(Bettor.m_strName + " hasn't placed any bets");
            }
            return description;
        }
 
        public int PayOut(int Winner)
        {
            if (m_iSnail == Winner)
            {
                return m_iAmount;
            }
            return -m_iAmount;
        }
    }
}
 
 
cs


Guy.cs

 

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
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace SnailRaceSimulator
{
    public class Guy
    {
        public string m_strName;
        public Bet bet;
        public int m_iCash;
 
        public RadioButton MyRadioButton;
        public Label MyLabel;
 
        public Guy(string _Name, Bet _Bet, int _Cash, RadioButton _RadioButton, Label _Label)
        {
            m_strName = _Name;
            bet = _Bet;
            m_iCash = _Cash;
            MyRadioButton = _RadioButton;
            MyLabel = _Label;
        }
 
        public void UpdateLabels()
        {
            if (bet == null)
            {
                MyLabel.Text = String.Format(m_strName + " hasn't placed any bets");
            }
            else
            {
                MyLabel.Text = bet.GetDescription();
            }
            MyRadioButton.Text = m_strName + " has " + m_iCash + " bucks";
        }
 
        public void ClearBet()
        {
            bet.m_iAmount = 0;
        }
 
        public bool PlaceBet(int Amount, int Snail)
        {
            if (Amount <= m_iCash)
            {
                bet = new Bet(Amount, Snail, this);
                return true;
            }
            else
            {
                MessageBox.Show(m_strName + "은 돈이 모자릅니다.");
            }
            return false;
        }
 
        public void Collect(int iWinner)
        {
            m_iCash += bet.PayOut(iWinner);
        }
    }
}
 
cs


Snail.cs

 

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
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
 
namespace SnailRaceSimulator
{
    public class Snail
    {
        public int m_iStartingPosition, m_iRacetrackLength, m_iLocation = 0;
        public PictureBox MyPictureBox = null;
 
        public bool Run(int _iDistance)
        {
            MoveMyPictureBox(_iDistance);
            m_iLocation += _iDistance;
            if (m_iLocation >= (m_iRacetrackLength - m_iStartingPosition))
            {
                return true;
            }
            return false;
        }
 
        public void TakeStartingPosition()
        {
            MoveMyPictureBox(-m_iLocation);
            m_iLocation = 0;
        }
 
        public void MoveMyPictureBox(int distance)
        {
            Point p = MyPictureBox.Location;
            p.X += distance;
            MyPictureBox.Location = p;
        }
    }
}
 
cs


Form1.cs

 

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
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace SnailRaceSimulator
{
    public partial class Form1 : Form
    {
        Snail[] dogs = new Snail[4];
        Guy[] guys = new Guy[3];
        int m_iWinningSnail;
        Random random = new Random();
 
        public Form1()
        {
            InitializeComponent();
            SetupRaceTrack();
        }
 
        private void SetupRaceTrack()
        {
            MinimumBet.Text = string.Format("Minimum bet " + BetAmount.Minimum);
 
            int startingPosition = Snail1.Right - racetrack.Left;
            int raceTrackLength = racetrack.Size.Width;
 
            dogs[0= new Snail() { MyPictureBox = Snail1, m_iRacetrackLength = raceTrackLength, m_iStartingPosition = startingPosition };
            dogs[1= new Snail() { MyPictureBox = Snail2, m_iRacetrackLength = raceTrackLength, m_iStartingPosition = startingPosition };
            dogs[2= new Snail() { MyPictureBox = Snail3, m_iRacetrackLength = raceTrackLength, m_iStartingPosition = startingPosition };
            dogs[3= new Snail() { MyPictureBox = Snail4, m_iRacetrackLength = raceTrackLength, m_iStartingPosition = startingPosition };
 
            guys[0= new Guy("Joe"null50, joeButton, joeBet);
            guys[1= new Guy("Bob"null75, bobButton, bobBet);
            guys[2= new Guy("Al"null45, alButton, alBet);
 
            for (int i = 0; i < guys.Length; i++)
                guys[i].UpdateLabels();
        }
 
        private void joeButton_CheckedChanged(object sender, EventArgs e)
        {
            SetBettorNameTextLabel("Joe");
        }
 
        private void bobButton_CheckedChanged(object sender, EventArgs e)
        {
            SetBettorNameTextLabel("Bob");
        }
 
        private void alButton_CheckedChanged(object sender, EventArgs e)
        {
            SetBettorNameTextLabel("Al");
        }
 
        private void SetBettorNameTextLabel(string strName)
        {
            BettorName.Text = strName;
        }
 
        private void Bets_Click(object sender, EventArgs e)
        {
            int GuyNumber = 0;
 
            if (joeButton.Checked)
            {
                GuyNumber = 0;
            }
            else if (bobButton.Checked)
            {
                GuyNumber = 1;
            }
            else if (alButton.Checked)
            {
                GuyNumber = 2;
            }
 
            guys[GuyNumber].PlaceBet((int)BetAmount.Value, (int)SnailNumber.Value);
            guys[GuyNumber].UpdateLabels();
        }
 
        private void race_Click(object sender, EventArgs e)
        {
            m_iWinningSnail = 0;
            race.Enabled = false;
            Bets.Enabled = false;
            timer1.Start();
 
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            SetBettorNameTextLabel("Joe");
        }
 
        private void timer1_Tick_1(object sender, EventArgs e)
        {
            for (int i = 0; i < dogs.Length; i++)
            {
                int iTemp = random.Next(-13);
                if (iTemp + dogs[i].m_iLocation < 0)
                    iTemp = random.Next(02);
                if (dogs[i].Run(iTemp))
                {
                    timer1.Stop();
                    m_iWinningSnail = i + 1;
                    MessageBox.Show("Snail " + m_iWinningSnail + "# won the race!");
                    for (int j = 0; j < guys.Length; j++)
                    {
                        if (guys[j].bet != null)
                        {
                            guys[j].Collect(m_iWinningSnail);
                            guys[j].bet = null;
                            guys[j].UpdateLabels();
                        }
                    }
                    for (int j = 0; j < dogs.Length; j++)
                        dogs[j].TakeStartingPosition();
 
                    race.Enabled = true;
                    Bets.Enabled = true;
                    break;
                }
 
            }
        }
 
        private void Form1_Load_1(object sender, EventArgs e)
        {
            SetBettorNameTextLabel("Joe");
        }
    }
}
 
cs





'프로그래밍 > C#' 카테고리의 다른 글

(C#) Head First - 캡슐화 PartyPlaner  (0) 2016.07.09
(C#) Head First - 캡슐화  (0) 2016.07.09
(C#) head first - MessageBox 사용  (0) 2016.07.09
(C#) WPF로 만든 심플 게임  (1) 2016.07.09
(C#) 엑세스 수식자, 예외처리  (0) 2016.07.09

+ Recent posts