-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.ts
132 lines (114 loc) · 5.19 KB
/
Player.ts
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
import { Card } from './Card.js';
import {
letPlayerChooseCards, revealPlayerCardLayout, hidePlayerCardLayout,
disableAllButtons
} from './ChangeLayout.js';
import { Hand } from './Hand.js';
/////////////////////////////////
//
//
/* SECTION BELOW: this is the player class */
//
//
/////////////////////////////////
class Player {
// COMMENTS: these are the member variables of Player
private cardObjectsArray: Array<HTMLElement>;
public allCards: Array<Card>;
// REQUIRES: _cardObjectsArray is an array of HTML elements representing the player's card slots
constructor(_cardObjectsArray: Array<HTMLElement>) {
this.allCards = [];
this.cardObjectsArray = _cardObjectsArray;
}
// REQUIRES: addedCard is a Card object
// EFFECTS: adds addedCard to allCards
addCardToPlayer(addedCard: Card): void {
this.allCards.push(addedCard);
// FUTURE: addCardToPlayer currently sorts this.allCards, but this may change in future
this.allCards.sort(Card.compareCards);
hidePlayerCardLayout(this.allCards, this.cardObjectsArray);
}
// REQUIRES: allCards are sorted and player has cards (should be 13)
// EFFECTS: returns true if player has three of diamonds, false otherwise
hasThreeOfDiamonds(): boolean {
this.allCards.sort(Card.compareCards);
return this.allCards[0].isThreeOfDiamonds();
}
//EFFECTS: converts card indices into actual hand, empty if not valid
turnTrackToArrayThreeOfDiamondsStart(trackSelection: Array<boolean>): Array<number> {
let selectedCards: Array<number> = new Array<number>();
for (let i: number = 0, j: number = 0; i < 13 && j < 5; ++i) {
if (trackSelection[i]) {
++j;
selectedCards.push(i);
}
}
let createdHand: Hand = new Hand();
for (let i: number = 0; i < selectedCards.length; ++i) {
createdHand.addCardToHand(this.allCards[selectedCards[i]]);
}
// COMMENTS: if created hand is valid, then return hand. otherwise, play lowest card.
if (createdHand.isValidStart() && createdHand.hasThreeOfDiamonds()) return selectedCards;
return [0];
}
//EFFECTS: converts card indices into actual hand, empty if not valid
turnTrackToArrayRegularStart(trackSelection: Array<boolean>): Array<number> {
let selectedCards: Array<number> = new Array<number>();
for (let i: number = 0, j: number = 0; i < 13 && j < 5; ++i) {
if (trackSelection[i]) {
++j;
selectedCards.push(i);
}
}
let createdHand: Hand = new Hand();
for (let i: number = 0; i < selectedCards.length; ++i) {
createdHand.addCardToHand(this.allCards[selectedCards[i]]);
}
// COMMENTS: if created hand is valid, then return hand. otherwise, play lowest card.
if (createdHand.isValidStart()) return selectedCards;
return [0];
}
//EFFECTS: converts card indices into actual hand, empty if not valid
turnTrackToArrayFollowUp(bestHandPlayedSoFar: Hand, trackSelection: Array<boolean>): Array<number> {
let selectedCards: Array<number> = new Array<number>();
for (let i: number = 0, j: number = 0; i < 13 && j < 5; ++i) {
if (trackSelection[i]) {
++j;
selectedCards.push(i);
}
}
let createdHand: Hand = new Hand();
for (let i: number = 0; i < selectedCards.length; ++i) {
createdHand.addCardToHand(this.allCards[selectedCards[i]]);
}
// COMMENTS: if created hand is valid, then return hand. otherwise, play lowest card.
if (!bestHandPlayedSoFar.isBeatenBy(createdHand)) {
selectedCards = new Array<number>();
}
return selectedCards;
}
// REQUIRES: player is first person to make move
// EFFECTS: allows player to select cards
allowSelectCardIndices(trackSelection: Array<boolean>): void {
revealPlayerCardLayout(this.allCards, this.cardObjectsArray);
letPlayerChooseCards(this.allCards.length, this.cardObjectsArray, trackSelection);
}
// EFFECTS: stop player from choosing cards
stopPlayerFromChoosingCards(): void {
hidePlayerCardLayout(this.allCards, this.cardObjectsArray);
disableAllButtons(this.cardObjectsArray);
}
// REQUIRES: selectedCardIndices is a sorted array where 0 <= selectedCardIndices.length <= 5;
// EFFECTS: returns a Hand object showing what player wants to play
playCards(selectedCardIndices: Array<number>): Hand {
let playedHand: Hand = new Hand();
for (let i: number = selectedCardIndices.length - 1; i >= 0; --i) {
// COMMENTS: selectedCardIndices[i] is indexToRemove, this.allCards.splice(...) is playedCard
let playedCard: Card = this.allCards.splice(selectedCardIndices[i], 1)[0];
playedHand.addCardToHand(playedCard);
}
hidePlayerCardLayout(this.allCards, this.cardObjectsArray);
return playedHand;
}
}
export { Player }