-
Notifications
You must be signed in to change notification settings - Fork 0
/
cards.js
158 lines (118 loc) · 4.36 KB
/
cards.js
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
154
155
156
157
158
// Jeremy Hu
// 02/2023
// A little personal visualization of the space of poker starting combos
// Future ideas: Snowglobe effect for individual cubes
// react-icons is installed already
'use strict'
// https://stackoverflow.com/questions/60155446/cube-of-cubes-in-three-js
// Create data representing hands with no suits (only ranks)
// e.g. data[0][0] = ["AA", PAIR_STRING]
let SUITED_STRING = "suited";
let OFFSUIT_STRING = "offsuit";
let PAIR_STRING = "pair";
function createHandMatrix() {
// Create 2-D grid with offsuit, paired, and suited hands
let ranks = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"];
const data = Array();
// Really dumb way of pre-allocating - fix this later
for(let i = 0; i < ranks.length; i++){
data[i]=[];
for(let j = 0; j < ranks.length; j++) {
data[i][j] = [];
}
}
for(let i = 0; i < ranks.length; i++) {
for(let j = i; j < ranks.length; j++) {
if (i != j) {
data[i][j] = [ranks[i]+ranks[j], SUITED_STRING]; // suited hands go along row i
data[j][i] = [ranks[i]+ranks[j], OFFSUIT_STRING]; // off suit hands go along col. i
} else {
data[i][j] = [ranks[i]+ranks[j], PAIR_STRING];
}
}
}
return data;
}
// Create cube visuals
function createCubeGrid(hands, gridGap) {
// Input nxnx2 list of hands constructed with createHandMatrix()
// [["AA", PAIR_STRING], ...]
let suits = ["s", "c", "h", "d"];
let cubes = [];
let offsuit = [];
let suited = [];
let pairs = [];
// Create combinations of suits
for (let i = 0; i < suits.length; i++) {
for (let j = 0; j < suits.length; j++) {
let s1 = suits[i];
let s2 = suits[j];
if (i == j) {
suited.push(s1+s2);
} else {
offsuit.push(s1+s2);
if (!pairs.includes(s2+s1) && !pairs.includes(s1+s2)) {
pairs.push(s1+s2);
}
}
}
}
let offSuitMaterial = new THREE.MeshBasicMaterial({ color: "skyblue", opacity: 0.2, transparent: true });
let suitedMaterial = new THREE.MeshBasicMaterial({ color: "pink", opacity: 0.2, transparent: true });
let pairMaterial = new THREE.MeshBasicMaterial({ color: "darkorchid", opacity: 0.2, transparent: true });
for(let i = 0; i < hands.length; i++) {
for(let j = 0; j < hands[0].length; j++) {
let handType = hands[i][j][1];
let suit_array = handType == OFFSUIT_STRING ? offsuit : handType == SUITED_STRING ? suited : pairs;
let material = handType == OFFSUIT_STRING ? offSuitMaterial : handType == SUITED_STRING ? suitedMaterial : pairMaterial;
for (let k = 0; k < suit_array.length; k++) {
let cube = new THREE.Mesh(geometry, material);
// Construct cube location
cube.position.x += i * gridGap;
cube.position.z += j * gridGap;
cube.position.y += k * gridGap;
cube.name = hands[i][j][0] + suit_array[k]; // UID: card,card,suit,suit e.g. AAsc
scene.add(cube);
}
}
}
return cubes;
}
// Show selected hand by maxing out opacity
function selectHand(hand) {
}
let scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff);
let camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
let renderer = new THREE.WebGLRenderer();
let cardGridElement = document.getElementById("cardGrid");
let s = getComputedStyle(cardGridElement);
renderer.setSize(parseInt(s.width), parseInt(s.height));
cardGridElement.appendChild(renderer.domElement);
let controls = new THREE.OrbitControls(camera, renderer.domElement);
// controls.autoRotate = true;
let geometry = new THREE.BoxGeometry(); //object that contains all the points and faces of the cube
///////////////////////////////////////////////////////
let gridWidth = 5;
let gridGap = 3;
let hands = createHandMatrix();
createCubeGrid(hands, gridGap);
camera.position.x = -40;
camera.position.y = 35;
camera.position.z = -20;
let axesHelper = new THREE.AxesHelper(150);
scene.add(axesHelper);
///////////////////////////////////////////////////////
//render loop
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
///////////////////////////////////////////////////////