Thread: Java
View Single Post
Old 10-12-2004, 01:34 PM   #26
Kon-Tiki
[BANNED]

 
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
Default

Code:
import bvp.*;
import java.io.*;
public class VoorbeeldPijltjes {
* *public static void main(String[] args) {
This loads the necessary libraries and starts the main class

Code:
String bestandsnaam;
bestandsnaam = "Voorbeeld.txt";

BestandsInvoer bi = new BestandsInvoer (bestandsnaam);

String regel = bi.leesRegel();
This one reads the world-file

Code:
// ********************************
// ** 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);
Code:
// ******************************************************************
// ** 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

Code:
// *************************
// ** 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.

Code:
// ***************************
// ** 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)

Code:
// *********************
// ** 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.

Code:
// *********************************************
// ** 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.
Kon-Tiki is offline                         Send a private message to Kon-Tiki
Reply With Quote