Skip to content

Commit cbbc64f

Browse files
committed
2022/Day 02 visu
1 parent c36ea9b commit cbbc64f

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

visualizations/2022/day02.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<html lang="en">
2+
<head>
3+
<title>Day 02: Rock Paper Scissors</title>
4+
<style type="text/css">
5+
canvas {
6+
background-color: #10101a;
7+
border: 1px solid #333340
8+
}
9+
</style>
10+
</head>
11+
<body style="background-color: #0f0f23">
12+
13+
<h1 style='font-family: "Source Code Pro", monospace; color: white; font-size: 18.6667px; font-weight: 400'>
14+
--- Day 02: Rock Paper Scissors ---
15+
</h1>
16+
<script src="../js/p5.min.js"></script>
17+
<script src="../js/ccapture.all.min.js"></script>
18+
<script src="day02.js"></script>
19+
20+
</body>
21+
</html>

visualizations/2022/day02.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
let INPUT = ("A Y\n" +
2+
"B X\n" +
3+
"B X\n" +
4+
"C Y\n" +
5+
"B X\n" +
6+
"A Z\n" +
7+
"B X\n" +
8+
"B X\n" +
9+
"C Z\n" +
10+
"A Z\n" +
11+
"C Z\n" +
12+
"A X\n" +
13+
"A Y\n" +
14+
"A Y\n" +
15+
"B X\n" +
16+
"C Y\n" +
17+
"B Z\n" +
18+
"A X\n" +
19+
"B X\n" +
20+
"C Y");
21+
22+
function setup() {
23+
frameRate(60);
24+
createCanvas(450, 400);
25+
}
26+
27+
let N = 1000;
28+
29+
function textyellow() {
30+
stroke('rgb(204, 204, 204)');
31+
fill('rgb(204, 204, 204)');
32+
}
33+
34+
function textblue() {
35+
stroke('lightblue');
36+
fill('lightblue');
37+
}
38+
39+
let rock, paper, scissors;
40+
41+
function preload() {
42+
rock = loadImage('rock.png');
43+
paper = loadImage('paper.png');
44+
scissors = loadImage('scissors.png');
45+
}
46+
47+
const L = INPUT.split("\n");
48+
49+
const WIDTH = 100;
50+
51+
function drawLeft(y, icon) {
52+
if (icon == 'A') {
53+
image(rock, 50, y, WIDTH, WIDTH);
54+
} else if (icon == 'B') {
55+
image(paper, 50, y, WIDTH, WIDTH);
56+
} else if (icon == 'C') {
57+
image(scissors, 50, y, WIDTH, WIDTH);
58+
}
59+
}
60+
61+
function drawRight(y, icon) {
62+
if (icon == 'X') {
63+
image(rock, 250, y, WIDTH, WIDTH);
64+
} else if (icon == 'Y') {
65+
image(paper, 250, y, WIDTH, WIDTH);
66+
} else if (icon == 'Z') {
67+
image(scissors, 250, y, WIDTH, WIDTH);
68+
}
69+
}
70+
71+
N = 0;
72+
let U = 0;
73+
let score1 = 0;
74+
let score2 = 0;
75+
76+
let shapeScores = {'X': 1, 'Y': 2, 'Z': 3};
77+
78+
let outcomes = {
79+
"A X": 3, "B Y": 3, "C Z": 3,
80+
"A Y": 6, "B Z": 6, "C X": 6
81+
};
82+
83+
let roundEnding = {
84+
"A X": 'Z', "B X": 'X', "C X": 'Y',
85+
"A Y": 'X', "B Y": 'Y', "C Y": 'Z',
86+
"A Z": 'Y', "B Z": 'Z', "C Z": 'Z',
87+
};
88+
89+
function draw() {
90+
background("#10101a");
91+
textyellow();
92+
93+
stroke('rgb(204, 204, 204)');
94+
fill('rgb(255, 255, 102)')
95+
textSize(18);
96+
textFont('Consolas');
97+
text('Strategy #1: ' + score1, 50, 30);
98+
99+
var pair = L[N].split(" ");
100+
drawLeft(50, pair[0]);
101+
drawRight(50, pair[1]);
102+
text('Strategy #2: ' + score2, 50, 220);
103+
104+
var txt2 = pair[0] + " " + roundEnding[L[N]];
105+
var pair2 = txt2.split(" ");
106+
drawLeft(250, pair2[0]);
107+
drawRight(250, pair2[1]);
108+
109+
text('Round ' + (N + 1), 350, 30);
110+
if (N != L.length - 1 && U++ > 20) {
111+
N++;
112+
if (N > L.length - 1) {
113+
N = L.length - 1;
114+
}
115+
U = 0;
116+
score1 += (outcomes[L[N]] || 0) + (shapeScores[pair[1]] || 0);
117+
score2 += (outcomes[txt2] || 0) + (shapeScores[pair2[1]] || 0);
118+
}
119+
120+
}

visualizations/2022/paper.png

10.6 KB
Loading

visualizations/2022/rock.png

11.1 KB
Loading

visualizations/2022/scissors.png

12.5 KB
Loading

0 commit comments

Comments
 (0)