-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
181 lines (151 loc) · 4.96 KB
/
main.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* To Do:
*
* Main Game Structure [ ]
* --Base [x]
* --Snakes [x]
* --Ladders [x]
* --Make it such so that no two ladders have feet at the same location and no two snakes have their heads at the same location [x]
* --Players [ ]
*
* Game Functioning [ ]
* --Player Movement algo thinking + imlementation [ ]
* --Snake and ladder detection + suitable movement implementation [ ]
* --Movement logs (like player one got bit by a snake, etc.) in println) [ ]
* --Dice Roll [ ]
* --Two Player Implementation (New player, player change, etc.) [ ]
* --Appropriate calculation of possible moves in the last [ ]
* --Winning instance
*
* UI [ ]
* --Start Menu [ ]
* (Not thinking of allowing a lot of customisation through UI. Users might just do it with the code)
* --Dice roll on click [ ]
*
* UX [ ]
* --Changing the size of the program. 400x400 is fine for the gameboard but for dice, other things, 600x600 would be fine [ ]
* --Nice cover image on start menu [ ]
* --Cool Snake design(s) (This might be curvy) [ ]
* --Cool Ladder Design [ ]
* --Improving Board and background colour scheme [ ]
* --Improving Dice [ ]
* --Maybe bringing movement logs on the canvas without println [ ]
*
* Any other things I think of in the future [ ]
*
* **/
/**Developer Comments
* Intentionally not removing snake start point on ladder's end point to increase fun!
*
* Had this comment earlier: 'Leaving for a few months (13th April, 2022)'
* **/
//Red ones are snakes and orange ones are ladders
random();
//println("Board Loading...");
var startMillis = millis();
/*Game Board*/
//@Creation
var n = 10;
var oneMoveWidth = 35;
var oneMoveHeight = 35;
var widthIterate = 1;
var heightIterate = 0;
var loopDirection = 1;
var baseMatrix = [];
var x = 36;
var y = 356;
//Base Matrix Generation that has all the player holders
for(var i = 1; i <= n*n; i++){
baseMatrix.push([x, y]);
if(i % n !== 0){
x = x + oneMoveWidth*loopDirection;
}
else if(i % n === 0){
y = y - oneMoveHeight*1;
loopDirection = - loopDirection;
}
}
//Snake Layer Matrix Generation
var snakeMatrix = [];
var totalSnakeNumber = 5;
var headRow, headIndex;
function findUniqueheadIndex(){
headIndex = floor(random((headRow - 1)*n, (headRow - 1)*n + n));
for(var i = 0; i < snakeMatrix.length; i++){
if(headIndex === snakeMatrix[i][4]){
findUniqueheadIndex();
}
}
}
for(var i = 1; i <= totalSnakeNumber; i++){
var headRow = floor(random(2, n + 1));
var tailRow = floor(random(1, headRow));
findUniqueheadIndex();
var headX = baseMatrix[headIndex][0];
var headY = baseMatrix[headIndex][1];
var tailIndex = floor(random((tailRow - 1)*n, (tailRow - 1)*n + n));
var tailX = baseMatrix[tailIndex][0];
var tailY = baseMatrix[tailIndex][1];
snakeMatrix.push([headX, headY, tailX, tailY, headIndex]);
}
//Ladder Layer Matrix Generation
var ladderMatrix = [];
var totalLadderNumber = 5;
var footRow, footIndex;
function findUniquefootIndex(){
footIndex = floor(random((footRow - 1)*n, (footRow - 1)*n + n));
for(var i = 0; i < ladderMatrix.length; i++){
if(footIndex === ladderMatrix[i][4]){
findUniquefootIndex();
}
}
}
for(var i = 1; i <= totalLadderNumber; i++){
var topRow = floor(random(2, n + 1));
var footRow = floor(random(1, headRow));
var topIndex = floor(random((topRow - 1)*n, (topRow - 1)*n + n)); //I think I might forget what I did here
var topX = baseMatrix[topIndex][0];
var topY = baseMatrix[topIndex][1];
//var footIndex = floor(random((footRow - 1)*n, (footRow - 1)*n + n));
findUniquefootIndex();
var footX = baseMatrix[footIndex][0];
var footY = baseMatrix[footIndex][1];
ladderMatrix.push([topX, topY, footX, footY, footIndex]);
}
//@Display
var playerHolderRadius = 22;
//Base
function baseLayer(){
for(var i = 0; i < baseMatrix.length; i++){
strokeWeight(2);
ellipse(baseMatrix[i][0], baseMatrix[i][1], playerHolderRadius, playerHolderRadius);
}
}
//Snake Layer
function snakeLayer(){
for(var i = 0; i < snakeMatrix.length; i++){
stroke(217, 20, 20, 170);
strokeWeight(5);
line(snakeMatrix[i][0], snakeMatrix[i][1], snakeMatrix[i][2], snakeMatrix[i][3]);
stroke(0, 0, 0);
strokeWeight(9);
point(snakeMatrix[i][0], snakeMatrix[i][1]); //head of snakes
}
}
//Ladder Layer
function ladderLayer(){
for(var i = 0; i < ladderMatrix.length; i++){
stroke(217, 126, 21, 200);
strokeWeight(5);
line(ladderMatrix[i][0], ladderMatrix[i][1], ladderMatrix[i][2], ladderMatrix[i][3]);
stroke(0, 0, 0);
strokeWeight(9);
point(ladderMatrix[i][2], ladderMatrix[i][3]); //head of snakes
}
}
//println(snakeMatrix);
// println(millis() - startMillis);
//println("Loaded! (~ " + (millis() - startMillis).toString() + " milliseconds)");
baseLayer();
snakeLayer();
ladderLayer();