PDA

View Full Version : Java


Kon-Tiki
07-12-2004, 01:43 PM
w00t! Almost done with the tutorial. Then on to getting some experience and doing the assignment fast enough for it to be done in time.

Rogue
07-12-2004, 02:06 PM
Nice... :)

The Niles
07-12-2004, 02:10 PM
I still want to learn Java but I don't have the time. Writing programs used to be fun back when I was still on an MSX.

Kon-Tiki
07-12-2004, 02:22 PM
Tutorial (http://java.sun.com/docs/books/tutorial/java/index.html) that only takes a few hours, and the more you know 'bout C++, the less time the tutorial'll take. What you need to do afterwards, is find a way to get experience in the coding. That's what I need to get now too. So... any suggestions to get my thinking right for this one? :D

Rogue
07-12-2004, 02:58 PM
Any programming (VB, C++, Java...) requires good planning. So if you prepare plan based on what you need to accomplish (this one is some sort of global plan), then create pseudo code (in your own language, what you like to do), your programming will then take minimum time, your code will look better, and it will be easier to fix problems.

I have not done much Java in last 2-3 years, but still remember of having a hard time with syntax. I had impression that visual part was also kind of unusual, but have to remember that programs were able to work on any kind of OS, which supports Java.

To make sure that you will not end up debugging, start this process with this:

Answer these questions:

What program supposed to do?

What is my input?

What are my processes?

What output should be?

After that just write down (still in your language) all functions, modules (or whatever you call them) that you will need in this program.

Then take a look if you can find Java commands that will do what you need.

And final step will be writing the code, which at this point should be strait forward.

And end will include testing and debugging.

This is about what we had to learn in data structures and procedures class, which basically includes preparing plan and documentation for problem, and then apply plan to any programming language. (I liked mostly to use C++)

When you done with documentation, post it here, so that we can see it.

Kon-Tiki
07-12-2004, 03:40 PM
Assignment doc:

Task
In this practicum, you'll have to program a game, using a simple graphicacl library.

The Game
The game situates in a 2-dimensional world, represented by a board, which is divided in
an amount of cells. A cell can be empty or contain a box. Dozo is the actor in this game.
He is controled by the player by using the cursors and can navigate through this 2D world
this way. The goal of the game is to get as many boxes to disappear. Boxes disappear when
they are grouped per 3 or per 4 (depending on the type of box).

The game stops if all boxes are disappeared.

Boxes
Boxes can be moved by Dozo when he pushes them. Dozo can move 1 or more boxes at once in one
direction this way, on condition that all boxes are of the same type and not placed against
a wall. Dozo can, in other words, not move both a red and a blue box at once. When he does
try this, he's meant to remain standing on the spot.

Two boxes are connected if and only if they have one side in common. Two boxes x and y belong
to the same group if x and y are connected or if a box z exists in such a way that x and z
are connected and z and y belong to the same group.

Red boxes have as characteristic that they disappear as soon as at least 3 such boxes are
grouped. Blue boxes disappear as soon as they're grouped with at least 4.

Walls
A wall (black square on the figure) is immovable. You can assume in this practicum that Dozo's
world is closed by walls.

The boxes below are grouped correctly and will thus have to disappear
* *<figure 2>
Figure 3 shows configurations of boxes that aren't complete and thus don't disappear.
* *<figure 3>

Concrete
A description of the world is saved in a file with extension .txt. The file exists out of a
few rows that represent the world. The example below makes this clear.

Dim 12 5 *The first rule defines a board of 12 cells wide and 5 cells high.
..########..
..#.B.#R..#. . Empty cell
..#BB.B.R.@# # wall (immovable)
..#....R..#. B a blue box
...#######.. R a red box
*@ Dozo

You can assume that:
* There's only one Dozo defined.
* The board is closed. Dozo can't walk reach a cell that's at the edge of the board in any
way.
* Except for the first row, all rows in the file are equally long.

To load a world from a file, you may use the class FileInput which you used in the previous
practicum.

Tips
Although the previous is strictly seen enough to get you to work, we're giving some classes
which can appear in your design. A possible implementation could use the following classes.
Note, you're not obliged to follow these directions.

Keep in mind that with the quoting, a fancy graphical userinterface won't be taken into
concideration, but the quality of your design and implementation of it will.

On Toledo, you'll find under "assignments", aside of a digital version of the task of this
practicum, a library with which you can draw graphical components on the screen. This way,
you can, for example, draw Dozo's world with the aid of the classe Bord. A couple of examples
make the use of these classes clear.

DozoWorld
This class enables you to make and control an object which keeps a model of the 2D world in
which Dozo navigates.

Define a constructor that creates and initiates a DozoWorld using the rows that can be read
from the file and give as paramenter the Board on which the world will be drawn.
* public DozoWorld(String[] rows, Board bord)

Next you can offer the next methods (this list isn't complete)
* public Item getCel (int x, int y)
Gives the item that is on that position (x,y). If there's no item on that position,
you can, for example, have the method return the value null.
* public void placeCel (int x, int y, Item item)
Places the item on that position (x,y).
* public void drawWorld()
Draws the complete world, including Dozo, on the board.

Item
An object of the class Item can be attached to each cell. An item can, for example, be a box
or a wall.
Examples of interesting methods here are:
* public boolean moveItem(int direction)
This method tries to move the item in the given direction and returns whether or not
this operation has succeeded.

* public Item give Item(int direction)
Gives the item that borders to the item on which the method has ran.

* public int getX()
Gives the x-coordinate back of the cell on which the item is.

Wall
You can define a Wall as subclass of Item. The method moveItem can be given a new implementation
here.

Dozo
Dozo too can be defined as subclass of Item.

Practical details
Design your solution on an objectorientated manner. Keep in mind as well that your code has
to be read and understood by other people:
* Choose clear variable- and methodnames and keep yourself to the conventions concearning
namegiving: names of variables and methods start with small letters, names of constants are
fully in capitals, ...
* Put structure in your code (indenting of blocks, ...) and add comments where needed.
* Complicated methods can be best tested by using it in a different main of a different class
and see if it has the right result.

Input:
The world to be loaded and cursors, to move Dozo
Processes: Loading a world
Moving Dozo
Pushing boxes -> Checking if a box is moved against a wall
Checking for how many boxes to be pushed
Checking for amount of grouped boxes to make them disappear
Output:
Object properties change and objects on screen change accordingly
Functions:
See Processes

Rogue
07-12-2004, 04:09 PM
OK, this would be a global plan. Now what's the plan for each of steps in process? Go to lovest level of code, but still in plain english.

Kon-Tiki
07-12-2004, 04:40 PM
1) Main program:
Set the board up and allow loading a world

2) Loading a world: Choosing the .txt-file
Reading it and displaying the world with graphics from the graphical library on the screen
Starting the program

3) Moving Dozo: Check for current position
Check what object is in the direction of where he moves
Check if there's another object in front of him
-> Do Checking for how many boxes to be pushed

Method: Moving Dozo Change the current coordinates to the ones of one tile in given direction
Clear Dozo-property from current position and add it to the new one
Draw Dozo on the new position

4) Checking for how many boxes to be pushed: Get the object type Dozo moved into
-> If it's a wall: stop moving
-> If it's an empty spot -> Move Dozo
Check if the object one space further in the direction Dozo moved is the same
-> Loop until it encounters something else than the current object type
Act according to the result:
-> If the object the loop stopped at is empty, then
1) set the property of the object that's moved into to empty
2) the empty object at the end of the loop's property to box
3) draw the box at that object
4) remove the box-pic from the object that's moved into
5) Call the method Moving Dozo in class Moving Dozo
-> Else
Do nothing and await new input

We'll keep it at that for now. Still can't really see how to do the grouping yet. I'd have it count the same way as I did with the last assignment, but they can be attached to each other in a corner too.

Rogue
07-12-2004, 04:45 PM
How do you like to set up coordinates?

Kon-Tiki
07-12-2004, 04:49 PM
Where the @ is in the world-file, it'll store the coordinates of that into an array which contains two ints. When doing the checking for the boxes, it'll copy this array and change the values. When Dozo moves, the array's values change too, to the new position.

Rogue
07-12-2004, 04:54 PM
into an array which contains two ints = multidimensional array?

Kon-Tiki
07-12-2004, 04:58 PM
First int is for x-coordinate, second for y-coordinate, so that it'll read like (x,y)

The Niles
07-12-2004, 05:18 PM
This topic seems more appropriate here.

Rogue
07-12-2004, 06:14 PM
Originally posted by Kon-Tiki@Dec 7 2004, 12:58 PM
First int is for x-coordinate, second for y-coordinate, so that it'll read like (x,y)
Now,
Create pseudo code of move.

For example:

move_up:
-- do this
-- check this
-- do this


From this you will get all needed functions to make a simple move

Note: you can move, without pushing, if I'm not mistaken.

Note 2. there was a game on PC Engine that is very similar to this. Very interesting and challenging game.

Kon-Tiki
07-12-2004, 07:01 PM
Move_Dozo Copy the array Dozo position to the array Temp position
Get Direction
-> If direction = up, call move_up
-> If direction = down, call move_down
-> If direction = left, call move_left
-> If direction = right, call move_right
Move_up Substract 1 from the second value in Temp position
Call Moving
Move_down Add 1 from the second value in Temp position
Call Moving
Move_left Substract 1 from the first value in Temp position
Call Moving
Move_right Add 1 from the first value in Temp position
Call Moving
Moving Check the property of the object on position Temp position
-> If object property = empty, call End_Moving
-> Else, call Objectmove, passing on Temp position

End_Moving Set property of object at Temp position to Dozo
Set property of object at Dozo position to empty
Set graphics of Temp position to Dozo
Set graphics of Dozo position to empty
Copy Temp position to Dozo position

Rogue
08-12-2004, 01:06 AM
Something is not right. :blink:

Try to complete couple things at once.

Kon-Tiki
08-12-2004, 12:38 PM
Huh? :blink:

Kon-Tiki
08-12-2004, 02:21 PM
Woo! Was nozing through the example files given with this thing, and got the moving to work, along with loading the board. Nothing 'bout loading the world itself yet though :whistle:

Rogue
09-12-2004, 02:36 AM
steps you wrote are not exactly in the best order.

you should use part of logic for that other game you had (at least to calculate win position)

post here your prograss

ps. as you can see, i'm kind of busy myself, but will try to help ya if I can. :bye:

Kon-Tiki
09-12-2004, 09:32 AM
's Alright :)
Right now, I've got this: The walking works, it can read a world-file and interprete the first line. I'm working on at least getting the loop for drawing the world on the playfield done today, so the big chunks're done and I can focus on the game itself.

Kon-Tiki
09-12-2004, 10:10 AM
Tadaa! Loop works fine, except that it draw black spots instead of the boxes, but it knows where they have to come. Probably just named them wrong. Borders of any level are set too. Now the next part: Giving properties to each part (so that he won't walk through walls anymore)

Kon-Tiki
10-12-2004, 12:30 PM
Eep, third post after each other.

Assignment so far (http://www.abandonia.com/~kon-tiki/Friendsstuff/Assignment.zip)

Rogue
10-12-2004, 12:44 PM
you have to post the code trough forum. I have only VJ.net on this computer.

Also, can you explain in plain english what you like to accomplishe.

While you do this, here is my recomendation for code (In example I'll write the pseudo code):

[CODE]
move up:
check if cell is empty
if yes, just move up
if no, check if is the end of the world (:D)
if yes, don't move, put something , or play some bad sound (perhaps fart :D)
if no, check what colort is the first box you hitting
check the next box
if is empty, move only that one box
if is not empty, check if next box is same color
if it is, do the same check with next cell
if not, play that bad sound again.

in this checks, you should also make sure that you don't move world's blocks.

also, can you tell how do you check win situation

Kon-Tiki
10-12-2004, 12:56 PM
Well, that already works fine (but I like the fart-sound thing :D ) Main problem now is the winning condition. This's the code:

import bvp.*;
import java.io.*;
public class VoorbeeldPijltjes {
* *public static void main(String[] args) {


String bestandsnaam;
bestandsnaam = "Voorbeeld.txt";

BestandsInvoer bi = new BestandsInvoer (bestandsnaam);

String regel = bi.leesRegel();


// ********************************
// ** De begin-coordinaten lezen **
// ********************************

int Checker = 0;
Checker = Counting(regel, Checker)+1;
int Temp = Counting(regel, Checker);

String tempX = regel.substring(4,Temp);
int dimX = Integer.parseInt(tempX);

Checker = Counting(regel, Checker)+1;
int TempStart = Temp + 1;
Temp = Counting(regel, Checker);
String tempY = regel.substring(TempStart);
int dimY = Integer.parseInt(tempY);


// *************************
// ** Nieuw bord plaatsen **
// *************************

Bus bus=new Bus();
Figuren verz = new Figuren("dozo2.fig");
int e=verz.getEenheid("grid");
Bord bord=new Bord("Dozo World", dimX*e, dimY*e, bus);

int plaatsX;
int plaatsY = 0;
int x = 0;
int y = 0;
int[] spelbord;
spelbord = new int[dimX*dimY];


// ***************************
// ** Plaatsen van de items **
// ***************************

for (plaatsY = 0; plaatsY < dimY; plaatsY++) {
regel = bi.leesRegel();
for (plaatsX = 0; plaatsX < dimX; plaatsX++) {
*switch(regel.charAt(plaatsX)) {
* case '.': spelbord[(plaatsY*dimX) + plaatsX] = 0;
* * * bord.plaatsFiguur("vloer", verz, plaatsX*e, plaatsY*e);
* * * break;
* case 'B': spelbord[(plaatsY*dimX) + plaatsX] = 1;
* * * bord.plaatsFiguur("doos1", verz, plaatsX*e, plaatsY*e);
* * * break;
* case 'R': spelbord[(plaatsY*dimX) + plaatsX] = 2;
* * * bord.plaatsFiguur("doos2", verz, plaatsX*e, plaatsY*e);
* * * break;
* case '#': spelbord[(plaatsY*dimX) + plaatsX] = 3;
* * * bord.plaatsFiguur("muur", verz, plaatsX*e, plaatsY*e);
* * * break;
* case '@': spelbord[(plaatsY*dimX) + plaatsX] = 4;
* * * bord.plaatsFiguur("mannetje", verz, plaatsX*e, plaatsY*e);
* * * x=plaatsX; *y=plaatsY;
* * * break;
*}
}
}
* * * *bord.toonVeranderingen();


// *********************
// ** Bewegen + duwen **
// *********************

* * * *while (true) {
* * * * * *Boodschap boodschap=bus.getBoodschap();
* * * * * *if (boodschap.getType().equals("toetsenbord")) {
* * * * * * * *String data=boodschap.getData();
* * * * * * * *if (data.startsWith("!-")) {

* *int oldx=x; int oldy=y;
* *int object;
* *int d;


// ** Naar links

* *if (data.equals("!-Left") && x>1) {
*d=x;
*d--;
*if (spelbord[(y*dimX)+d] == 0) {
* *spelbord[(y*dimX)+x] = 0;
* * * * * * x--;
*}
*if (spelbord[(y*dimX)+d] == 1 || spelbord[(y*dimX)+d] == 2) {
* object = spelbord[(y*dimX)+d];
* System.out.println("Object = "+ object);
* do {
* *d--;
* } while (spelbord[(y*dimX)+d] == object);

* *if (spelbord[(y*dimX)+d] == 0) {
* * spelbord[(y*dimX)+d] = object;
* * bord.plaatsFiguur("doos"+ object, verz, d*e, y*e);
* * bord.plaatsFiguur("vloer", verz, x*e, y*e);
* * spelbord[(y*dimX)+x] = 0;
* * x--;
* * bord.plaatsFiguur("mannetje", verz, x*e, y*e);
* * spelbord[(y*dimX)+x] = 4;
* }
*
*} *

* *}


// ** Naar rechts

* *if (data.equals("!-Right") && x<(dimX-1)) {
*d=x;
*d++;
*if (spelbord[(y*dimX)+d] == 0) {
* *spelbord[(y*dimX)+x] = 0;
* * * * * * x++;
*}
*if (spelbord[(y*dimX)+d] == 1 || spelbord[(y*dimX)+d] == 2) {
* object = spelbord[(y*dimX)+d];
* System.out.println("Object = "+ object);
* do {
* *d++;
* } while (spelbord[(y*dimX)+d] == object);
* if (spelbord[(y*dimX)+d] == 0) {
* *spelbord[(y*dimX)+d] = object;
* *bord.plaatsFiguur("doos"+ object, verz, d*e, y*e);
* *bord.plaatsFiguur("vloer", verz, x*e, y*e);
* *spelbord[(y*dimX)+x] = 0;
* *x++;
* *bord.plaatsFiguur("mannetje", verz, x*e, y*e);
* *spelbord[(y*dimX)+x] = 4;
* }
*} *

* *}
* *
* *
// ** Naar boven

* *if (data.equals("!-Up") && y>1) {
*d=y;
*d--;
*if (spelbord[(d*dimX)+x] == 0) {
* *spelbord[(y*dimX)+x] = 0;
* * * * * * y--;
*}
*if (spelbord[(d*dimX)+x] == 1 || spelbord[(d*dimX)+x] == 2) {
* object = spelbord[(d*dimX)+x];
* System.out.println("Object = "+ object);
* do {
* *d--;
* } while (spelbord[(d*dimX)+x] == object);
* if (spelbord[(d*dimX)+x] == 0) {
* *spelbord[(d*dimX)+x] = object;
* *bord.plaatsFiguur("doos"+ object, verz, x*e, d*e);
* *bord.plaatsFiguur("vloer", verz, x*e, y*e);
* *spelbord[(y*dimX)+x] = 0;
* *y--;
* *bord.plaatsFiguur("mannetje", verz, x*e, y*e);
* *spelbord[(y*dimX)+x] = 4;
* }
*
*} *

* *}


// ** Naar beneden

* *if (data.equals("!-Down") && y<(dimY-1)) {
*d=y;
*d++;
*if (spelbord[(d*dimX)+x] == 0) {
* *spelbord[(y*dimX)+x] = 0;
* * * * * * y++;
*}
*if (spelbord[(d*dimX)+x] == 1 || spelbord[(d*dimX)+x] == 2) {
* object = spelbord[(d*dimX)+x];
* System.out.println("Object = "+ object);
* do {
* *d++;
* } while (spelbord[(d*dimX)+x] == object);
* if (spelbord[(d*dimX)+x] == 0) {
* *spelbord[(d*dimX)+x] = object;
* *bord.plaatsFiguur("doos"+ object, verz, x*e, d*e);
* *bord.plaatsFiguur("vloer", verz, x*e, y*e);
* *spelbord[(y*dimX)+x] = 0;
* *y++;
* *bord.plaatsFiguur("mannetje", verz, x*e, y*e);
* *spelbord[(y*dimX)+x] = 4;
* }
*
*} *

* *}
* *
* *bord.plaatsFiguur("vloer", verz, oldx*e, oldy*e);
* *bord.plaatsFiguur("mannetje", verz, x*e, y*e);
* *bord.toonVeranderingen();


// *********************************************
// ** Checken voor dozen die mogen verdwijnen ** *-> Nog aan aan't werken
// *********************************************

int g;
int h;
int doos;
int teller = 0;
int[] groep;
groep = new int[dimX*dimY];
for (doos = 1; doos <=2; doos++) {
*for (g=0; g < dimX; g++) {
* for (h=0; h < dimY; h++) {
* *if (spelbord[(h*dimX)+g] == doos) {
* * teller ++;
* * groep[teller] = (h*dimX)+g;
* *}
* *else {
* * teller = 0;
* *}
* *if (teller == 4 && doos == 1) {
* * System.out.println("Voldoende dozen 1");
* *}
* *if (teller == 3 && doos == 2) {
* * System.out.println("Voldoende dozen 2");
* *}
* *
* } *
*}
}








* * * * * * * *}
* * * * * *}
* * * *}





* *}


// ************************************************** ****************
// ** Tellen tot'n spatie komt (voor de eerste regel van'n wereld) **
// ************************************************** ****************

public static int Counting(String regel, int Checker) {
int counter = regel.length()-1;
int StartCount = Checker;
for (int c=StartCount; c<=counter; c++) {
*char Temp = regel.charAt(c);
* if (Temp == ' ') {
* * *break;
* }
* else {
* * *Checker ++;
* }
}
return Checker;
}
}

Rogue
10-12-2004, 01:08 PM
Can you explain what are you trying to accoplish with each code?

Kon-Tiki
10-12-2004, 01:34 PM
import bvp.*;
import java.io.*;
public class VoorbeeldPijltjes {
* *public static void main(String[] args) {

This loads the necessary libraries and starts the main class


String bestandsnaam;
bestandsnaam = "Voorbeeld.txt";

BestandsInvoer bi = new BestandsInvoer (bestandsnaam);

String regel = bi.leesRegel();

This one reads the world-file


// ********************************
// ** De begin-coordinaten lezen **
// ********************************

int Checker = 0;
Checker = Counting(regel, Checker)+1;
int Temp = Counting(regel, Checker);

String tempX = regel.substring(4,Temp);
int dimX = Integer.parseInt(tempX);

Checker = Counting(regel, Checker)+1;
int TempStart = Temp + 1;
Temp = Counting(regel, Checker);
String tempY = regel.substring(TempStart);
int dimY = Integer.parseInt(tempY);


// ************************************************** ****************
// ** Tellen tot'n spatie komt (voor de eerste regel van'n wereld) **
// ************************************************** ****************

public static int Counting(String regel, int Checker) {
int counter = regel.length()-1;
int StartCount = Checker;
for (int c=StartCount; c<=counter; c++) {
*char Temp = regel.charAt(c);
* if (Temp == ' ') {
* * *break;
* }
* else {
* * *Checker ++;
* }
}
return Checker;
}

The first one reads the dimensions of the world as described in the first line of the world-file (being DIM x y, DIM 12 5 for example) It does this by calling the second code, which runs through the line until it finds a space, then returns how many characters it's got to run along. It takes that part (from the beginning of the checker until the first space) and tosses it away at first (as it's DIM, which we don't need), then turns the string it receives for the x-dimension into the int dimX and the string for the y-dimension into the int dimY


// *************************
// ** Nieuw bord plaatsen **
// *************************

Bus bus=new Bus();
Figuren verz = new Figuren("dozo2.fig");
int e=verz.getEenheid("grid");
Bord bord=new Bord("Dozo World", dimX*e, dimY*e, bus);

int plaatsX;
int plaatsY = 0;
int x = 0;
int y = 0;
int[] spelbord;
spelbord = new int[dimX*dimY];

This one initializes the board and input (both done through the bvp.jar library), as well as it loads the graphical tiles and readies some variables for the next part. The world is stored as an array, called spelbord.


// ***************************
// ** Plaatsen van de items **
// ***************************

for (plaatsY = 0; plaatsY < dimY; plaatsY++) {
regel = bi.leesRegel();
for (plaatsX = 0; plaatsX < dimX; plaatsX++) {
*switch(regel.charAt(plaatsX)) {
* case '.': spelbord[(plaatsY*dimX) + plaatsX] = 0;
* * * bord.plaatsFiguur("vloer", verz, plaatsX*e, plaatsY*e);
* * * break;
* case 'B': spelbord[(plaatsY*dimX) + plaatsX] = 1;
* * * bord.plaatsFiguur("doos1", verz, plaatsX*e, plaatsY*e);
* * * break;
* case 'R': spelbord[(plaatsY*dimX) + plaatsX] = 2;
* * * bord.plaatsFiguur("doos2", verz, plaatsX*e, plaatsY*e);
* * * break;
* case '#': spelbord[(plaatsY*dimX) + plaatsX] = 3;
* * * bord.plaatsFiguur("muur", verz, plaatsX*e, plaatsY*e);
* * * break;
* case '@': spelbord[(plaatsY*dimX) + plaatsX] = 4;
* * * bord.plaatsFiguur("mannetje", verz, plaatsX*e, plaatsY*e);
* * * x=plaatsX; *y=plaatsY;
* * * break;
*}
}
}
* * * *bord.toonVeranderingen();

This part of the code reads the game's world line per line and puts the right tiles on the right spot, and gives the corresponding number in the array the right value. Here's a list of which means which: 0 = Floor
1 = Box type 1
2 = Box type 2
3 = Wall
4 = player
After that, it shows the changes to the world by calling the method (still from the bvp-library) bord.toonVeranderingen.

You might notice some confusing thing in the array index... this formula: (y*dimX)+x. It's because before, when I just used x*y for the array index, x = 2, y = 5 had the same index as x = 5, y = 2, x = 1, y = 10 and x = 10, y = 1, as example. Too buggy. This formula's kinda the same as Row*maxColumns + Column from the Connect Four assignment (with difference that this matrix's point 0 is at the top left, not the bottom left)


// *********************
// ** Bewegen + duwen **
// *********************

* * * *while (true) {
* * * * * *Boodschap boodschap=bus.getBoodschap();
* * * * * *if (boodschap.getType().equals("toetsenbord")) {
* * * * * * * *String data=boodschap.getData();
* * * * * * * *if (data.startsWith("!-")) {

* *int oldx=x; int oldy=y;
* *int object;
* *int d;


// ** Naar links

* *if (data.equals("!-Left") && x>1) {
*d=x;
*d--;
*if (spelbord[(y*dimX)+d] == 0) {
* *spelbord[(y*dimX)+x] = 0;
* * * * * * x--;
*}
*if (spelbord[(y*dimX)+d] == 1 || spelbord[(y*dimX)+d] == 2) {
* object = spelbord[(y*dimX)+d];
* System.out.println("Object = "+ object);
* do {
* *d--;
* } while (spelbord[(y*dimX)+d] == object);

* *if (spelbord[(y*dimX)+d] == 0) {
* * spelbord[(y*dimX)+d] = object;
* * bord.plaatsFiguur("doos"+ object, verz, d*e, y*e);
* * bord.plaatsFiguur("vloer", verz, x*e, y*e);
* * spelbord[(y*dimX)+x] = 0;
* * x--;
* * bord.plaatsFiguur("mannetje", verz, x*e, y*e);
* * spelbord[(y*dimX)+x] = 4;
* }
*
*} *

* *}


// ** Naar rechts

* *if (data.equals("!-Right") && x<(dimX-1)) {
*d=x;
*d++;
*if (spelbord[(y*dimX)+d] == 0) {
* *spelbord[(y*dimX)+x] = 0;
* * * * * * x++;
*}
*if (spelbord[(y*dimX)+d] == 1 || spelbord[(y*dimX)+d] == 2) {
* object = spelbord[(y*dimX)+d];
* System.out.println("Object = "+ object);
* do {
* *d++;
* } while (spelbord[(y*dimX)+d] == object);
* if (spelbord[(y*dimX)+d] == 0) {
* *spelbord[(y*dimX)+d] = object;
* *bord.plaatsFiguur("doos"+ object, verz, d*e, y*e);
* *bord.plaatsFiguur("vloer", verz, x*e, y*e);
* *spelbord[(y*dimX)+x] = 0;
* *x++;
* *bord.plaatsFiguur("mannetje", verz, x*e, y*e);
* *spelbord[(y*dimX)+x] = 4;
* }
*} *

* *}
* *
* *
// ** Naar boven

* *if (data.equals("!-Up") && y>1) {
*d=y;
*d--;
*if (spelbord[(d*dimX)+x] == 0) {
* *spelbord[(y*dimX)+x] = 0;
* * * * * * y--;
*}
*if (spelbord[(d*dimX)+x] == 1 || spelbord[(d*dimX)+x] == 2) {
* object = spelbord[(d*dimX)+x];
* System.out.println("Object = "+ object);
* do {
* *d--;
* } while (spelbord[(d*dimX)+x] == object);
* if (spelbord[(d*dimX)+x] == 0) {
* *spelbord[(d*dimX)+x] = object;
* *bord.plaatsFiguur("doos"+ object, verz, x*e, d*e);
* *bord.plaatsFiguur("vloer", verz, x*e, y*e);
* *spelbord[(y*dimX)+x] = 0;
* *y--;
* *bord.plaatsFiguur("mannetje", verz, x*e, y*e);
* *spelbord[(y*dimX)+x] = 4;
* }
*
*} *

* *}


// ** Naar beneden

* *if (data.equals("!-Down") && y<(dimY-1)) {
*d=y;
*d++;
*if (spelbord[(d*dimX)+x] == 0) {
* *spelbord[(y*dimX)+x] = 0;
* * * * * * y++;
*}
*if (spelbord[(d*dimX)+x] == 1 || spelbord[(d*dimX)+x] == 2) {
* object = spelbord[(d*dimX)+x];
* System.out.println("Object = "+ object);
* do {
* *d++;
* } while (spelbord[(d*dimX)+x] == object);
* if (spelbord[(d*dimX)+x] == 0) {
* *spelbord[(d*dimX)+x] = object;
* *bord.plaatsFiguur("doos"+ object, verz, x*e, d*e);
* *bord.plaatsFiguur("vloer", verz, x*e, y*e);
* *spelbord[(y*dimX)+x] = 0;
* *y++;
* *bord.plaatsFiguur("mannetje", verz, x*e, y*e);
* *spelbord[(y*dimX)+x] = 4;
* }
*
*} *

* *}
* *
* *bord.plaatsFiguur("vloer", verz, oldx*e, oldy*e);
* *bord.plaatsFiguur("mannetje", verz, x*e, y*e);
* *bord.toonVeranderingen();

This one's for moving. I'll discuss one direction, as the rest's the same principle. Each direction exists out of two main parts: one for if the spot the player moves to is a floortile, one to see if it's either type of box. It stores that value in the variable object, which is then used to check for boxes of the same type. The loop loops through the direction the player moved to and either increases or decreases the d-variable, according to the direction it goes to, until the value of that array index is not that type of box anymore. Then it checks to see if, at the spot in front of all the boxes, is an empty floortile. If it is, it changes that spot's property to that box type, changes the property of the spot you moved from to floor and the property of the spot you moved into to player, then changes the graphics for it.


// *********************************************
// ** Checken voor dozen die mogen verdwijnen ** *-> Nog aan aan't werken
// *********************************************

int g;
int h;
int doos;
int teller = 0;
int[] groep;
groep = new int[dimX*dimY];
for (doos = 1; doos <=2; doos++) {
*for (g=0; g < dimX; g++) {
* for (h=0; h < dimY; h++) {
* *if (spelbord[(h*dimX)+g] == doos) {
* * teller ++;
* * groep[teller] = (h*dimX)+g;
* *}
* *else {
* * teller = 0;
* *}
* *if (teller == 4 && doos == 1) {
* * System.out.println("Voldoende dozen 1");
* *}
* *if (teller == 3 && doos == 2) {
* * System.out.println("Voldoende dozen 2");
* *}
* *
* } *
*}
}

This's the part where it should be checking to win, but it's not working yet. This was just an attempt to see how well it'd work. Works fine, but only for the exact amount of boxes, not that amount of boxes or more, and only if they're all hooked up vertically.

Rogue
10-12-2004, 01:46 PM
The move part has to be done in different way.

You have to have a lot of if statements.


You start to move (no mather of direction)

1 - if is floor, move

2. if is not floor - check if is the wall

3. if is wall, don't move

4. if is box, check next space

now same procedure (same stuff) with next box

1. if is floor, move

2. if is not floor, check if is the wall

3. if is wall, don't move

4. if is box, check the color

5. if is same color go to next box

6 if is different color color, don't move

You can move only in 4 directions, this make it easy!

Kon-Tiki
10-12-2004, 01:48 PM
Uhhhmmm... the moving works smoothly... everything's just fine, except for the winning condition.

Rogue
10-12-2004, 01:49 PM
how big is the world?

Kon-Tiki
10-12-2004, 02:00 PM
It's dimX, dimY big. It should be able to read any world-file. A world-file looks like this:

DIM 10 6 -> Defines the dimensions, in this case x = 10, y = 6
...######. -> . = floor
..#.....#. -> # = wall
.#@.B.R..# -> @ = player
#BB..R..R# -> B = Box 1 (or box 2, I don't remember off the top of my head)
#R.....B.# -> R = Other type of box
##########

Of course, the comments to the right aren't included. It should be able to read worlds of 2x3 dimensions as well as worlds of 1234567890x1234567890 dimensions (which it can... tested that :D If it's 12345678901, it gives an error)

Edit: just tested that world. It's impossible to win. That's what you get for throwing those characters around randomly :D

Rogue
10-12-2004, 02:43 PM
do this....


for (numC = 0; numC <+ 3; numC ++)
{
*for( numX = 0; numX < 'width of wold' - 3 - numC; numX++)
*{
* * for( numY = 0; numY < 'height of world'; numY ++)
* * {
* * * *if( numC = 0 )
* * * *{
* * * * * *if( box1 = W and box2= W and box 3 = W)
* * * * * *{
* * * * * * * * BoXeS desapear function
* * * * * * }
* * * *}
* * * *if( numC = 1 )
* * * *{
* * * * * *if( box1 = W and box2= W and box3 = W and box4 = W)
* * * * * *{
* * * * * * * * BoXeS desapear function
* * * * * * }
* * * * }
* * * *if( numC = 2 )
* * * *{
* * * * * *if( box1 = W and box2= W and box3 = W and box4 = W and box5 = W)
* * * * * *{
* * * * * * * * BoXeS desapear function
* * * * * * }
* * * * }
* * * * if( numC = 3 )
* * * *{
* * * * * *if( box1 = W and box2= W and box3 = W and box4 = W and box5 = W and box6 = W)
* * * * * *{
* * * * * * * * BoXeS desapear function
* * * * * * }
* * * * }
* *}

*}
}

This is function that calculates horisontally if boxes should desapear. You can use select case statement in inner for loop, which will make code work better.

Some explanation:

box1 = W --- box1 is the cooridinate of the first box, and if is equal to the color you are checking

first loop ( 0 to 3) makes this code check if you have 3, 4, 5 or 6 blocks in the row ( make changes as you need, you need two separate functions to check win situation, one is from 4 - 6, second is from 3 - 4).

NOTE: I just noticed that I mixed 3 and 4 boxes win situation. I don't have a time to fix it now.

Gicve me an houd and I'll make this code look a bit better, I have to run to do something.

Kon-Tiki
10-12-2004, 02:54 PM
's Alright. Thanks for the help :)

Rogue
10-12-2004, 03:57 PM
This is code for color that needs at least 4 boxes to desapear

for (numC = 0; numC <= 2; numC ++)
{
*for( numX = 0; numX < 'width of wold' - 3 - numC; numX++)
*{
* *for( numY = 0; numY < 'height of world'; numY ++)
* *{ * * * * *
* * *if( numC = 0 )
* * *{
* * * *if( box1 = W and box2= W and box3 = W and box4 = W)
* * * *{
* * * * *BoXeS desapear function
* * * *}
* * *}
* * *if( numC = 1 )
* * *{
* * * *if( box1 = W and box2= W and box3 = W and box4 = W and box5 = W)
* * * * *{
* * * * * *BoXeS desapear function
* * * * *}
* * * *}
* * *if( numC = 2 )
* * *{
* * * *if( box1 = W and box2= W and box3 = W and box4 = W and box5 = W and box6 = W)
* * * *{
* * * * *BoXeS desapear function
* * * *}
* * *}
* *} *// for numY loop ends
*} *// for numX loop ends
} *// for numC loop ends

This code checks boxes in if loop. Box1 is the first one (start point), Box2 = cooridinate of box next to the box1. (Increase x value in cooridinate) etc.

simillar loop have to exist for 3 boxes:

for (numC = 0; numC <= 1; numC ++)
{
*for( numX = 0; numX < 'width of wold' - 2 - numC; numX++)
*{
* *for( numY = 0; numY < 'height of world'; numY ++)
* *{ * * * * *
* * *if( numC = 0 )
* * *{
* * * *if( box1 = W and box2= W and box3 = W)
* * * *{
* * * * *BoXeS desapear function
* * * *}
* * *}
* * *if( numC = 1 )
* * *{
* * * *if( box1 = W and box2= W and box3 = W and box4 = W)
* * * *{
* * * * *BoXeS desapear function
* * * *}
* * *}
* *} *// for numY loop ends
*} *// for numX loop ends
} *// for numC loop ends

do the same for vertical

note, to win with 4 colored color, you can have 3+1, 3 + 2 or 3+ 3 boxes together. (you move 3 boxes, and there are already 3 staying, so when you connect them, you can have 6 boxes)

Rogue
11-12-2004, 06:54 PM
Have you finish it? :blink:

Kon-Tiki
11-12-2004, 07:25 PM
Not yet. This thing makes me want to smash my head into the monitor. Your code'd work fine if it weren't for the groups having to exist out of at least 3 boxes for one type and at least 4 for the other. That code works for exactly 3 boxes and exactly 4, it seems. Wouldn't work for take 8 boxes linked together.

Right now, I changed some things to my code. That big-behind formula's changed and position's stored with coordinates. Spelbord's a double array now, like this: spelbord[x][y]. I also made it so that I know exactly how many boxes of each type are in the game, and got an algorythm to store their coordinates. x is stored in groupX[counter], y in groupY[counter], where counter is the amount found. Max. of those two arrays, is the amount of boxes of that type in total.

Up to there, it works fine. The checking itself's still a giant pain in the behind and it's starting to frustrate me to no end that nothing I try even works the slightest.

Rogue
11-12-2004, 07:37 PM
Originally posted by Kon-Tiki@Dec 11 2004, 03:25 PM
Not yet. This thing makes me want to smash my head into the monitor. Your code'd work fine if it weren't for the groups having to exist out of at least 3 boxes for one type and at least 4 for the other. That code works for exactly 3 boxes and exactly 4, it seems. Wouldn't work for take 8 boxes linked together.

Right now, I changed some things to my code. That big-behind formula's changed and position's stored with coordinates. Spelbord's a double array now, like this: spelbord[x][y]. I also made it so that I know exactly how many boxes of each type are in the game, and got an algorythm to store their coordinates. x is stored in groupX[counter], y in groupY[counter], where counter is the amount found. Max. of those two arrays, is the amount of boxes of that type in total.

Up to there, it works fine. The checking itself's still a giant pain in the behind and it's starting to frustrate me to no end that nothing I try even works the slightest.
You should not be able to link 8 boxes together!

For one of the boxes max you can have is 6 (one that needs 4 to disappear) and that is accomplished by moving 3 boxes and adding them on the group of 3.

Other one (3 needed to disappear) can be 4 max. (2 moved to 2 standing unit).

Reason for this is that you should not be able to have 4 or 3 boxes standing on the board, as they should already disappear.


Do you understand this?

Another suggestion, keep a world, boxes and player in array. (if you are not)

Kon-Tiki
11-12-2004, 07:43 PM
Well, with 4 boxes to form a group, the max is 9. If the boxes are set up like this:

____B
____B
BBB_B
___B_
___B_
___B_

If they push the lowermost row of three up one spot, all boxes'll have to disappear. Same when they push the horizontal row one more to the right.

In the same way, the max for a group formed with at least 3 is 6.

Rogue
11-12-2004, 08:06 PM
You mean in this case


___B_
___B_
___B_
BBB_BBB< move this box to left
___B_
___B_
___B_


Total 12 boxes that should disappear! :blink: :wall:


Do this:

Create additional array in which you will store coordinates of boxes that should disappear.

Create 4 functions (2 for each case) that will go and check if there are boxes set that have met the criteria.

In my code, change this:

for (numC = 0; numC <= [B]2[/B]; numC ++)

to 3, and add additional line to check 7 boxes in the line:

* * * if( box1 = W and box2= W and box3 = W and box4 = W and box5 = W and box6 = W and box7 = W)
* * * {
* * * * BoXeS disappear function
* * * }



Instead of boxes disappear function, add boxes coordinates to array.

Run all 4 search functions, and after you done, create function that will go trough array with boxes that should disappear and delete them.

Try this and let me know if it worked!

Kon-Tiki
11-12-2004, 08:13 PM
Alright, let's see if it works.

Kon-Tiki
11-12-2004, 08:42 PM
Ok, added, but when compiling, it tells me box1, box2, etc aren't declared, and probably'll do the same for W. If I know what they're supposed to be, I can quickly change it... I hope.

Really don't understand much of the code yet. Looks to me like it won't be able to get something like

BB
BB
_B

or

B__B
BBBB

Edit: reread the first post with the code and will get the Box1, etc right. Hold on...
Edit edit: It works fine with a horizontal row, it seems, but vertical, or in a tetris-block... doesn't work yet.


if (object == 1) {

*CheckedX = new int[doos1];
*CheckedY = new int[doos1]; *
*
*for (g = 0; g <= 3; g++) {
* for (int numX = 0; numX < ((dimX - 3) - g); numX++) {
* *for(int numY = 0; numY < dimY; numY ++) {
* * if(g == 0) {
* * *if (spelbord[numX][numY] == object && spelbord[numX+1][numY] == object && spelbord[numX+2][numY] == object && spelbord[numX+3][numY] == object) {
* * * System.out.println("Boxes disappear.");
* * *}
* * }
* * if(g == 1) {
* * *if(spelbord[numX][numY] == object && spelbord[numX+1][numY] == object && spelbord[numX+2][numY] == object && spelbord[numX+3][numY] == object && spelbord[numX+4][numY] == object) {
* * * System.out.println("Boxes disappear."); * *
* * *}
* * }
* * if(g == 2) {
* * *if(spelbord[numX][numY] == object && spelbord[numX+1][numY] == object && spelbord[numX+2][numY] == object && spelbord[numX+3][numY] == object && spelbord[numX+4][numY] == object && spelbord[numX+5][numY] == object) {
* * * System.out.println("Boxes disappear.");
* * *}
* * }
* * if(g == 3) {
* * *if(spelbord[numX][numY] == object && spelbord[numX+1][numY] == object && spelbord[numX+2][numY] == object && spelbord[numX+3][numY] == object && spelbord[numX+4][numY] == object && spelbord[numX+5][numY] == object && spelbord[numX+6][numY] == object) {
* * * System.out.println("Boxes disappear.");
* * *}
* * }

* *}
* }
*} *
}

Rogue
11-12-2004, 09:02 PM
You should reuse same logic, just instead of *world width* use *world height*, and when you calculate wins, you use Y instead of X.

Same rule apply here. *you can have max 7 or 5 boxes*

Kon-Tiki
11-12-2004, 09:13 PM
Now vertically works fine too, but a combination doesn't... it can't check this:

__B
BBB

Yet that's a good group too, just like:

_B__
BBBB

or

BB
BB

Rogue
11-12-2004, 09:21 PM
what do you mean it can't check?

Kon-Tiki
11-12-2004, 09:24 PM
It won't recognize such groups as ready for removal.

Rogue
11-12-2004, 09:28 PM
Originally posted by Kon-Tiki@Dec 11 2004, 05:24 PM
It won't recognize such groups as ready for removal.
this should remove too?

Kon-Tiki
11-12-2004, 09:40 PM
Yep.
Taken from the assignment doc...
Following blocks are grouped correctly:
http://www.filespace.org/Kon-Tiki/Assignment1.png

These blocks aren't:
http://www.filespace.org/Kon-Tiki/Assignment2.png

Kon-Tiki
12-12-2004, 03:05 AM
Ok, made a recursive class... it works fine, but has one little quirk. It seems to take the first If-block that turns out to be true, does what's stated in there, then ignores the If-blocks after that one, hence it'll ignore certain boxes.

Code:

public static void Checker(int doos1, int object, int[][] spelbord, int[] groepX, int[] groepY, int g, int teller, int[] CheckedX,int[] CheckedY) {

int q;
q = 0;
if(spelbord[groepX[g]][groepY[g]] == object) {
System.out.println("Current Position: ("+ groepX[g] +","+ groepY[g] +")");
*CheckedX[teller] = groepX[g];
*CheckedY[teller] = groepY[g];
*teller++;
*System.out.println("Teller++");
*System.out.println("Teller = "+ teller);
}


// ** Right

if(spelbord[groepX[g]+1][groepY[g]] == object) {
*for (q = 0; q < doos1; q++) {
* *if(CheckedX[q] == groepX[g]+1 && CheckedY[q] == groepY[g]) {
* * break;
* *}
* *else {
* * if(q == doos1-1) {
* *groepX[g]++;
* *Checker(doos1, object, spelbord, groepX, groepY, g, teller, CheckedX, CheckedY);
* * }
* *}
*}
}


// ** Down

if(spelbord[groepX[g]][groepY[g]+1] == object) {
*for (q = 0; q < doos1; q++) {
* *if(CheckedX[q] == groepX[g] && CheckedY[q] == groepY[g]+1) {
* * break;
* *}
* *else {
* * if(q == doos1-1) {
* *groepY[g]++;
* *Checker(doos1, object, spelbord, groepX, groepY, g, teller, CheckedX, CheckedY);
* * }
* *}
*}
}


// ** Left

if(spelbord[groepX[g]-1][groepY[g]] == object) {
*for (q = 0; q < doos1; q++) {
* *if(CheckedX[q] == groepX[g]-1 && CheckedY[q] == groepY[g]) {
* * break;
* *}
* *else {
* * if(q == doos1-1) {
* *groepX[g]--;
* *Checker(doos1, object, spelbord, groepX, groepY, g, teller, CheckedX, CheckedY);
* * }
* *}
*}
}


// ** Up

if(spelbord[groepX[g]][groepY[g]-1] == object) {
*for (q = 0; q < doos1; q++) {
* *if(CheckedX[q] == groepX[g] && CheckedY[q] == groepY[g]-1) {
* * break;
* *}
* *else {
* * if(q == doos1-1) {
* *groepY[g]--;
* *Checker(doos1, object, spelbord, groepX, groepY, g, teller, CheckedX, CheckedY);
* * }
* *}
*}
}
}

For now, I'll let it be like that, as I sure as hell have no idea how to solve it. Will be working on the actual box-destroying code for now... and wonder how to put that one in so it'll start at the right moment.

Rogue
12-12-2004, 03:43 AM
what this code suposed to do? :blink:

Kon-Tiki
12-12-2004, 01:23 PM
It's supposed to check in each direction of the current object if there's an object of the same type, then do the same for that one, if it's not checked yet, while checking further.

Rogue
12-12-2004, 03:31 PM
THat would be to big code. What do you need is code simillar to ona you already have.

Just scatch all options, and always start from the bottom left, make range you can make with that code (for example 4 boxes made of 2 by 2 you can check from bottom left by going 1 to right, and then top line also first and one to right box, and you don't need code for the last column or row, as from there you can't make 4 boxes that way.

So all you neeed is just same code, made for all possible solutions.

Kon-Tiki
13-12-2004, 12:19 AM
Ok, got the recursive thing done, and the removal of the boxes. Sent it in, too, so no use in changing it now to make it better. It's got a couple of bugs left in though.

1) It still refuses to check the other directions after finding a direction that works fine.
2) The boxes'll only be removed when you walk past one box in the group.
3) Pushing a box into a wall'll get an index out of bound in an array somewhere.
4) I got weird results when trying to see if you won or not, so I left that out (as it wasn't obligatory anyways :whistle: )