Here are some of my projects I was working on. I hope you enjoy!
Check out the 2 previous threads:
Contents:
- #35 Conway sequence
- #36 Towers Of Hanoi
Here are some of my projects I was working on. I hope you enjoy!
Check out the 2 previous threads:
Contents:
#35 conway sequence
Start with “1”. After that just say, there is one (1) number one(1) = 11.
Now there are two (2) ones(1) = 21
There is one (1) two (2) and one (1) one(1) = 1211
continue a bit till you get to:
[1] 1
[2] 11
[3] 21
[4] 1211
[5] 111221
[6] 312211
[7] 13112221
[8] 1113213211
[9] 31131211131221
[10] 13211311123113112211
[11] 11131221133112132113212221
[12] 3113112221232112111312211312113211
[13] 1321132132111213122112311311222113111221131221
[14] 11131221131211131231121113112221121321132132211331222113112211
[15] 311311222113111231131112132112311321322112111312211312111322212311322113212221
It took me a while to do it, if you find it interesting, check out the short program I made that does this!
just 18 lines of code!
String c = "1";
void draw() {
println("["+frameCount+"] "+(c));
c = iter(c);
}
String iter(String input) {
String str2 = "",str3="";
for(int i = 0; i < input.length(); i++) {
str2+=input.charAt(i);
if(i != input.length()-1) if(input.charAt(i) != input.charAt(i+1)) str2+="_";
}
String parts[] = split(str2,"_");
for(int i = 0; i < parts.length; i++) {
str3+=parts[i].length();
str3+=(parts[i].charAt(0));
}
return str3;
}
nice work keep on update your skills
Thank you for the nice words! I hope more people can do the same thing, so I can s̶t̶e̶a̶l̶ draw inspiration from their ideas. That’s one of the reasons why I do this. Besides, it’s always nice to see new concepts and help people. I learned a lot by understanding other’s code.
Edit: does anyone know the name of the sequence of project #35 (1 11 21 1211 111221 312211…)?
Edit2: @glv thank you for telling me. According to google, it is named Conway’s sequence/Look and say sequence/A005150. Interesting. Of course I choose the Conway name, since it’s the best sounding : D
A Google search can help you with this.
:)
#36 Towers Of Hanoi
int w = 3, h = 5, sclX=200, sclY=50, mx=0, my=0, mmx=0,moves=0,holding = -1;
int grid[][] = new int[w][h];
void setup() {
size(600, 300);
for (int i = 0; i < h; i++) grid[0][i] = h-i;
textAlign(3,3);
textSize(sclY*0.3);
}
void draw() {
background(0);
noStroke();
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
fill( map(grid[i][j], 0, h, 0, 255));
if (grid[i][j] == holding) {
fill(180, 255, 180);
}
rect(i*sclX, j*sclY, sclX, sclY);
}
if (holding == -1) {
mx = floor(map(mouseX, 0, w*sclX, 0, w));
my = getTop(mx);//floor(map(mouseY,0,h*sclY,0,h));
}
mmx = floor(map(mouseX, 0, w*sclX, 0, w));
noFill();
stroke(0, 255, 0);
rect(mx*sclX, my*sclY, sclX, sclY);
if (holding != -1) {
fill(map(holding, 0, h, 0, 255));
rect( mouseX-sclX*0.25, mouseY-sclY*0.25, sclX*0.5, sclY*0.5);
stroke(255,0,0);
if(grid[mmx][h-1] == 0 || mx == mmx) rect(mmx*sclX,(getTop(mmx))*sclY,sclX,sclY);
else rect(mmx*sclX,(getTop(mmx)-1)*sclY,sclX,sclY);
}
noStroke();
fill(100,255,255);
rect(0,h*sclY,width,sclY);
fill(0);
text("moves:"+moves,sclX*0.5,sclY*(h+0.5));
text("TOWERS OF HANOI",sclX*0.5*w,sclY*(h+0.5));
text("timer:"+getTime(millis()),sclX*(w-0.5),sclY*(h+0.5));
}
String getTime(int ms) {
int s = floor(ms*0.001)%60,m = floor(ms*0.001*0.05/3)%60,h = floor(ms*0.001*0.05*0.05/9);
return ((h > 0)? nf(h,2)+"h" : "") + ((m > 0 || h > 0)? nf(m,2)+"m" : "") + nf(s,2)+"s";
}
int getTop(int x) {
for (int i = 0; i < h; i++) {
if (grid[x][i] != 0) return i;
}
return h-1;
}
void mousePressed() {
if (holding == -1) {
if (grid[mx][getTop(mx)] != 0) {
holding = grid[mx][getTop(mx)];
}
} else if (holding != -1) {
if (getTop(mmx) > 0 && grid[mmx][getTop(mmx)] <= holding) {
grid[mx][my] = 0;
if(grid[mmx][h-1] == 0) grid[mmx][getTop(mmx)] = holding;
else grid[mmx][getTop(mmx)-1] = holding;
holding = -1;
moves+=1;
}
}
}
Nice work, @CodeMasterX !
The Conway sequence to which you refer is also known as the look-and-say sequence, because we look at a term in the sequence and describe it in the next term. If you are interested in other integer sequences, see
The On-Line Encyclopedia of Integer Sequences (OEIS). You can perform searches within that site.
Another interesting sequence that describes itself is the Golomb sequence. Here is the beginning of it:
1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, …
Do you see a pattern?
See:
From what I could figure out, the Golomb sequence’s “rule” is to have two counters, 1st to get the curent number displaying, and another to tell how many times you repeat the same number.
Yes, you could consider the positions, or indexes, of the numbers in the sequence to be a sort of counter. It is actually all the integers, beginning with 1
. We could name this index n
. Then, for each n
, g(n)
would be the number of times n
is to appear in the sequence.
We’ll omit index 0
, and begin with a 1
at index 1
. That stipulates that 1
is to occur 1
time in the sequence. So, since 1
has already appeared 1
time, we move on to 2
, which we place at index 2
. Now, since the value at index 2
is 2
, it means that 2
must occur 2
times in the sequence. Thus far it has only occurred 1
time, so we also place a 2
in position 3
.This satisfies the requirement that 2
occur 2
times in the sequence, and also specifies that 3
should occur 2
times. So we place a 3
in position 4
and in position 5
, which specifies that those numbers must each occur 3
times in the sequence, and so on …
#37 Advanced 5 puzzle
MAIN MENU:
p15 p;
boolean created = false;
int pg = 0,ww=7,hh=7;
void setup() {
size(550, 550);
colorMode(HSB);
textAlign(3, 3);
noStroke();
}
void draw() {
page(pg);
}
void page(int pg) {
background(150, 100, 255);
if(pg == 0) {
fill(255);
textSize(50);
text("CREATE",width*0.5,height*0.5);
textSize(20);
text("w:"+ww+" h:" + hh,width*0.5,height*0.6);
fill(85, 255, 255,150);
rect(0,0,width*0.1,height);
fill(42, 255, 255,150);
rect(width*0.9,0,width*0.1,height);
fill(0, 255, 255,150);
rect(0,0,width,height*0.1);
fill(170, 255, 255,150);
rect(0,height*0.9,width,height*0.1);
} else if(pg == 1) {
if(created) p.display();
}
}
void mousePressed() {
if(created) if (p.inside(mouseX, mouseY)) {
PVector pos = p.getSlot(mouseX,mouseY);
p.move((int)pos.x,(int)pos.y);
}
if(pg == 0) {
if(mouseX<width*0.1) if(ww>3) ww--;
if(mouseX>width*0.9) ww++;
if(mouseY<height*0.1) if(hh>3) hh--;
if(mouseY>height*0.9) hh++;
if(mouseX > 0.1*width && mouseX < 0.9*width && mouseY > 0.1*height && mouseY < 0.9*height) if(!created) {
p = new p15(ww,hh,50,50,450,450);
pg=1;
created = true;
p.create();
p.mix();
}
}
}
class p15 {
int w, h, x, y, bw, bh;
float sclX, sclY;
int grid[][];
p15(int w_, int h_, int x_, int y_, int bw_, int bh_) {
w = w_;
h = h_;
x = x_;
y = y_;
bw = bw_;
bh = bh_;
grid = new int[w][h];
sclX=(bw/(float)w);
sclY = (bh/(float)h);
}
void create() {
for (int i = 0; i < w; i++)for (int j = 0; j < h; j++) grid[i][j] = i+j*w;
}
void display() {
textSize(floor( ((ww>hh)? sclX : sclY) *0.5));
noStroke();
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
fill( map(grid[i][j], 0, w*h, 0, 255), 255, ((grid[i][j] == 0)? 0 : 255) );
rect(i*sclX+x, j*sclY+y, sclX, sclY);
fill(255);
if (grid[i][j] != 0) text(grid[i][j]+"", (i+0.5)*sclX+x, (j+0.5)*sclY+y);
}
}
boolean inside(int mx_, int my_) {
return( ((mx_ > x && mx_ < x + bw)&&(my_ > y && my_ < y + bh)) );
}
boolean inBounds(int x, int y) {
return( (x>=0 && x < w && y>=0 && y < h));
}
PVector getSlot(int mx, int my) {
return new PVector(floor(map(mx, x, x+bw, 0, w)),floor(map(my, y, y+bh, 0, h)));
}
void move(int mx, int my) {
for (float i = 0, cx=1, cy=0; i < 4; i++, cx=(int)cos(i*HALF_PI), cy=(int)sin(i*HALF_PI)) {
if(inBounds(mx+(int)cx,my+(int)cy)) if(grid[mx+(int)cx][my+(int)cy] == 0) {
grid[mx+(int)cx][my+(int)cy] = grid[mx][my];
grid[mx][my] = 0;
break;
}
}
}
void mix() {
for(int i = 0; i < w*h*1000; i++) move(floor(random(w)),floor(random(h)));
}
}
#38 Pascals triangle generator
int arr[] = {1};
void setup() {
size(600, 600);
textAlign(3,3);
background(0);
}
void draw() {
//printArray(arr);
arr = tri(arr);
String l = "";
for(int i = 0; i < arr.length-1; i++) l+= (arr[i]+ " ");
if(frameCount>20) noLoop();
println(l);
text(l,width*0.5,height*0.02*(frameCount+1));
}
int[] tri (int in[]) {
int out[] = new int[in.length+1];
out[0] = 1;
for(int i = 1; i < in.length; i++) {
out[i] = in[i-1]+in[i];
}
return out;
}
I haven’t been active in a while. There are 2 reasons: 1) End of the school year 2) I have been playing with c++. It will be a requirement by next year, so it’s better that I start preparing early. If you have any tips for it, you are more than welcome to tell me. I am at the absolute beginning of c++, so any help will be greatly appriciated.
I will still do occasional processing sketches, since I am a long way from using c++ graphics, so yeah. Have a nice day and bye!
Best of luck with school and C++, @CodeMasterX !
You may want to check out Codecademy’s free C++ course. See Codecademy: Learn C++.
Might like to take a look at javidx9 on youtube. He had tutorials in c++ and explores code through game design.!
#39 Push tile puzzle V0 - the game
int w = 20, h = 20, scl = 600/w, px = 10, py = 10;
tile grid[][] = new tile[w][h];
String genCode =
"20,20,16,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1 ";
void setup() {
size(600, 600);
if (genCode.equals("")) {
for (int i = 0; i < w; i++)for (int j = 0; j < h; j++) grid[i][j] = new tile(i, j, 0);
grid[px][py-2] = new tile(px, py-2, 2);
grid[px][py-3] = new tile(px, py-3, 2);
grid[15][15] = new tile(15, 15, 1);
} else load(genCode);
}
void draw() {
background(0);
stroke(255, 40);
noFill();
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
grid[i][j].display();
}
fill(0, 255, 0);
square(px*scl, py*scl, scl);
}
void keyPressed() {
int cy = ((key == 'w' || key == 'W' || keyCode == UP)? -1 : ((key == 's' || key == 'S' || keyCode == DOWN)? 1 : 0));
int cx = ((key == 'a' || key == 'A' || keyCode == LEFT)? -1 : ((key == 'd' || key == 'D' || keyCode == RIGHT)? 1 : 0));
println("CP:",cx,cy);
if (cx!=0||cy!=0) {
if (inBounds(px+cx, py+cy)) {
if (grid[px+cx][py+cy].type == 0) {
px=constrain(px+cx,0,w);
py=constrain(py+cy,0,h);
println(px,py);
} else if (grid[px+cx][py+cy].type == 2) { //type == 1 is ignored, since it is unpushable under any circumstances
grid[px+cx][py+cy].pushTile(cx, cy);
}
}
}
if (key == ' ') {
println("GENERATED STRING: \n" + generate() + "\n end");
}
}
class tile {
int x, y;
int type = 0;
tile(int x_, int y_, int type_) { //0 - empty 1 - unpushable 2 - pushable
x = x_;
y = y_;
type = type_;
}
void pushTile(int cx, int cy) {
if (type == 2) {
if (canMove(cx, cy)) {
px = x;
py = y;
type = 0;
grid[x+cx][y+cy].type = 2;
}
}
}
boolean canMove(int cx, int cy) {
if ( inBounds(x+cx, y+cy)) {
if (grid[x+cx][y+cy].type == 0) {
return true;
}
}
return false;
}
void display() {
if (type == 0) fill(0);
if (type == 1) fill(255);
if (type == 2) fill(0, 0, 255);
square(x*scl, y*scl, scl);
}
}
boolean inBounds(int x, int y) {
return( x>=0&&y>=0&&x<w&&y<h);
}
/**
Loading syntax
w,h,px,py,
typeAt0-0, typeAt1-0, typeAt2-0, typeAt3-0,...
typeAt0-1, typeAt1-1, typeAt2-1, typeAt3-1,...
...
ALWAYS ENDS with ",-1" just for convinience's sake, and a buffer for mistakes
*/
String generate() {
String output = w+","+h+","+px+","+py+",";
String gridinfo = "";
for (int j = 0; j < h; j++)for (int i = 0; i < w; i++) gridinfo+=grid[i][j].type+",";
return output+gridinfo+"-1";
}
void load(String layout) {
String seg[] = split(layout, ",");
int is[] = int(seg); //= int segments
int nw=0, nh=0, npx=0, npy=0;
nw = is[0];
nh = is[1];
npx= is[2];
npy= is[3];
tile ngrid[][] = new tile[nw][nh];
for (int i = 0; i < nw; i++) for (int j = 0; j < nh; j++) {
ngrid[i][j] = new tile(i, j, is[i+j*nw+4]);
}
w = nw;
h = nh;
px = npx;
py = npy;
grid = ngrid;
}
#40 Push the tile puzzle - map editor
//pushBlock editor
int w = 20, h = 20, sel = 1, scl = 600/w, px = -1, py = -1;
int grid[][] = new int[w][h];
void setup() {
size(600,600);
}
void draw() {
background(50);
for(int i = 0; i < w; i++) for(int j = 0; j < h; j++) {
fill(getClr(grid[i][j]));
if(px == i && py == j) fill(0,255,0);
square(i*scl,j*scl,scl);
}
fill(getClr(sel),100);
square(mouseX,mouseY,scl);
if(mousePressed) {
int mx = constrain( (int)map(mouseX,0,width,0,w),0,w-1);
int my = constrain( (int)map(mouseY,0,height,0,h),0,h-1);
grid[mx][my] = sel;
}
}
void keyPressed() {
if(key == 'p') {
px = constrain( (int)map(mouseX,0,width,0,w),0,w-1);
py = constrain( (int)map(mouseY,0,height,0,h),0,h-1);
}
println("HERE IS THE CODE + \n" + generate() + " \n end of code");
}
color getClr(int sel) {
return( (((sel == 0)? color(0) : ((sel == 1)? color(255) : ((sel == 2)? color(0,0,255) : color(255,0,0))))));
}
void mouseWheel(MouseEvent e) {
sel-= e.getCount();
if(sel < 0) sel = 2;
if(sel > 2) sel = 0;
}
String generate() {
String output = w+","+h+","+px+","+py+",";
String gridinfo = "";
for (int j = 0; j < h; j++)for (int i = 0; i < w; i++) gridinfo+=grid[i][j]+",";
return output+gridinfo+"-1";
}
press p to place player
press any key to get blueprints
Not really aesthetic but : P
it outputs a string into the console, which you can use at will. Feel free to make your own levels : D
Fourth library is out! Enjoy!
Project library #4
Fifth library is out!
Project library #5