Pages

Monday, May 29, 2017

CoFFeeLands Ep2 - Creating a Random World Generator Using Algo Code Part 2

Welcome back!

This time, I am going to create an algorithm.
//For grass patch
1. If Long Grass is on up, self is Grass Patch.
2. If Flower is on right, self is Grass Patch.
//For long grass
1. If Rock is on up, self is Long Grass.
2. If Grass Patch is right, self is Long Grass.
//For Flower Patch
1. If Long Grass is on right, self is Flower Patch.
2. If Grass Patch is on up, self is Flower Patch.
//For tree
1. If Rock is below, self is Tree.
2. If Flower Patch is on up, self is Tree.
//For rock
1. If Tree is on left, self is Rock.
2. If Long Grass is on left, self is Rock.
//For water
1. If Water is on right, self is also water.
2. If Water is below, self is also water.
//For sand
1. If Water is on left, self is sand.
2. If Water is on up, self is sand.

This seems complicated, so to help the computer to understand, we will assign some number to the blocks.

0 = Grass Patch
1 = Long Grass
2 = Flower Patch
3 = Tree
4 = Rock
5 = Water
6 = Sand

Next, we will design the world, 12 X 12.
_________________________________________________________________________________
var world=[
[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,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]];
_________________________________________________________________________________

You can edit the world numbers into anything you like, such as:
_________________________________________________________________________________
var world=[
[0,0,1,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,3,0,4,4,0],
[0,0,0,0,0,2,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,3,0,0],
[0,0,0,0,0,0,0,4,0,3,0,0],
[0,0,2,0,0,1,0,0,0,5,0,0],
[0,0,0,0,0,1,0,0,3,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,6,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,2,0,0,0,0],
[0,0,0,0,0,0,0,2,0,0,0,0]];
_________________________________________________________________________________

The algorithm will be like this in pseudocode:
//For long grass
1. If world[y-1][x]=4, world[y][x]=1
2. If world[y][x+1]=0, world[y][x]=1
//For Flower Patch
1. If world[y][x+1]=1, world[y][x]=2
2. If world[y-1][x]=0, world[y][x]=2
//For tree
1. If world[y+1][x]=4, world[y][x]=3
2. If world[y-1][x]=2, world[y][x]=3
//For rock
1. If world[y][x-1]=3, world[y][x]=4
2. If world[y][x-1]=1, world[y][x]=4
//For water
1. If world[y][x+1]=6, world[y][x]=5
2. If world[y+1][x]=6, world[y][x]=5
//For sand
1. If world[y][x-1]=6, world[y][x]=6
2. If world[y-1][x]=6, world[y][x]=6

You can turn this into Code form.
We do not need Grass Patch as naturally it will be the most block.
Next,
_________________________________________________________________________________
//world here

var Cx=0;
var Cy=0;

for(Cx=0;Cx<12;Cx++) {
if(Cx==12) {
Cy+=1;
Cx=0;
}

//put the ifs here
//but change the y into Cy and change the x into Cx

var blocktw=60;

if (world[Cy][Cx]==0) {
drawGrassPatch(Cx*blocktw,Cy*blocktw,blocktw,blocktw);
}
if (world[Cy][Cx]==1) {
drawLongGrass(Cx*blocktw,Cy*blocktw,blocktw,blocktw);
}
if (world[Cy][Cx]==2) {
drawFlowerPatch(Cx*blocktw,Cy*blocktw,blocktw,blocktw);
}
if (world[Cy][Cx]==3) {
drawTree(Cx*blocktw,Cy*blocktw,blocktw,blocktw);
}
if (world[Cy][Cx]==4) {
drawRock(Cx*blocktw,Cy*blocktw,blocktw,blocktw);
}
if (world[Cy][Cx]==5) {
drawWater(Cx*blocktw,Cy*blocktw,blocktw,blocktw);
}
if (world[Cy][Cx]==6) {
drawSand(Cx*blocktw,Cy*blocktw,blocktw,blocktw);
}
}
_________________________________________________________________________________

And there.

Go on and try -- it won't do any harm.

Thanks for reading.

29-05-2017

No comments:

Post a Comment