-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerView.cs
More file actions
153 lines (125 loc) · 4.33 KB
/
PlayerView.cs
File metadata and controls
153 lines (125 loc) · 4.33 KB
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
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Label = System.Windows.Forms.Label;
namespace idk
{
internal class PlayerView
{
protected List<Card> cards;
protected Label nrCarti;
protected Button changeLayout;
protected PictureBox[] cardBoxes;
int selectedLayout = 0;
public PictureBox[] CardBoxes => cardBoxes;
public Button ChangeLayoutButton => changeLayout;
public string CountLabelFormat { get; set; } = "{0} cards";
public Label CountLabel => nrCarti;
public int CardCount => cards.Count;
public PlayerView()
{
cards = new List<Card>();
cardBoxes = new PictureBox[5];
for (int i = 0; i < cardBoxes.Length; i++)
{
cardBoxes[i] = new PictureBox
{
Visible = true,
SizeMode = PictureBoxSizeMode.StretchImage
};
}
this.nrCarti = new Label
{
Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))),
AutoSize = true,
Visible = true
};
this.changeLayout = new Button
{
Text = "< Layout >",
Font = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))),
AutoSize = true,
Visible = true
};
changeLayout.Click += ChangeLayout_Click;
}
private void ChangeLayout_Click(object sender, EventArgs e)
{
if (cards.Count == 0)
return;
int nrLayout = (int)Math.Ceiling(cards.Count / 5.0);
if (nrLayout <= 0)
return;
selectedLayout = (selectedLayout + 1) % nrLayout;
selectedLayout = Math.Max(0, Math.Min(selectedLayout, nrLayout - 1));
UpdateCardBoxes();
}
private void UpdateCardBoxes()
{
int cardsPerLayout = 5;
int startIndex = selectedLayout * cardsPerLayout;
int endIndex = Math.Min(startIndex + cardsPerLayout, cards.Count);
int cardsToShow = Math.Max(0, endIndex - startIndex);
for (int i = 0; i < cardBoxes.Length; i++)
{
int cardIndex = startIndex + i;
if (i < cardsToShow && cardIndex >= 0 && cardIndex < cards.Count)
{
cardBoxes[i].Visible = true;
cardBoxes[i].Image = cards[cardIndex].Image;
cardBoxes[i].Tag = cards[cardIndex];
}
else
{
cardBoxes[i].Visible = false;
cardBoxes[i].Image = null;
cardBoxes[i].Tag = null;
}
}
try
{
nrCarti.Text = string.Format(CountLabelFormat, cards.Count);
}
catch
{
nrCarti.Text = $"{cards.Count} cards";
}
}
public void AddCard(Card card)
{
this.cards.Add(card);
UpdateCardBoxes();
}
public void RemoveCard(Card card)
{
if (cards.Remove(card))
{
UpdateCardBoxes();
}
}
public bool HasCard(Card card)
{
return cards.Contains(card);
}
public bool HasBlockingCard()
{
if (cards == null)
return false;
for (int i = 0; i < cards.Count; i++)
{
var c = cards[i];
if (c.rank == Card.Rank.King || c.rank == Card.Rank.Four || c.rank == Card.Rank.Two)
return true;
}
return false;
}
public virtual void HandleOpponentSpecialCard(Card playedCard, Game game, Action onDrawCards)
{
// override in Player1 & Player2
}
public virtual void HandleSuitSelection(Game game, Action<Card.Suit> onSuitSelected)
{
// override in Player1 & Player2
}
}
}