PDA

View Full Version : SoKoBan


Coin
2005.02.04, 09:27 PM
MM i think thats what its called, anyway i have the basc thing build the little guy runs around and pushes boxes correctly (although he jumps from square to square no animation) the process of hardcoding in all the levels would suck, so i want to make it read a text file that has 20 rows and 20 columns

the first level looks like this
####################
#####+++############
#####$++############
#####++$############
###++$+$+###########
###+#+##+#####++&&##
#+$++$++++++++++&&##
#####+###+#@##++&&##
#####+++++##########
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################

# = wall
@ = hero
& = goal pad
+ = ground
$ = movable object


how can i get a loop to read through this and draw the correctly

:edit: i drew the level wrong...... but yea

Josh
2005.02.04, 09:54 PM
How is it being stored? In a string, char array, NSString, random something else?

Coin
2005.02.04, 10:00 PM
if you mean how is the game board being stored, i have 3 20x20 arrays, objects, background, and the accual rects themselves, i just need to know how to extract the character from a rtf file... i can do the rest myself im pretty sure

ravuya
2005.02.04, 10:03 PM
Just load it with fstream and read it with getline statements, then go through the array line and break it up character by character.

http://rav.realitybytes.tk/oldies/Map.cpp
http://rav.realitybytes.tk/oldies/Map.h

Is my current map class (Yeah, it's ugly as hell and doesn't work very nicely) but it will load a map from a text file.

Coin
2005.02.04, 10:18 PM
ive never heard of either of those commands, so
here is my guess

- (void)loadMapFromFile:(NSString *)map
{
ifstream TheFile(map);
//if that is right is TheFile made and defined? or what do i have to pre-define that as?
if (!TheFile) {
NSRunAlertPanel(@"Map Missing!",@"You are missing map %@", @"OK", nil, nil, map);
}
else {
int x, y;
for (x = 0 ; x < 20 ; x++) {
for (y = 0 ; y < 2 ; y++) {
TheFile.getline
//now im confused
}
}
}
}
//and can obj-c/cocoa do this?

belthaczar
2005.02.04, 10:33 PM
//and can obj-c/cocoa do this?

NSString *text = [NSString stringWithContentsOfFile:@"myfile.txt"];
char ch = [text characterAtIndex:index];

NSString (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSString.html)

Coin
2005.02.04, 11:45 PM
NSRunAlertPanel(@"level one", @"%@", @"OK", nil, nil, [NSString stringWithContentsOfFile:@"lvl1.rtf"]);


displays NULL

where does the text file need to be?

lvl1.rtf is in the base level of my files/groups

yes... i spelled it right

:edit: do i have to use a path that points the bundle?
how do you show the bundle in a path

NSMainBundle/Contents/Resources/lvl1.txt

?

seven
2005.02.04, 11:57 PM
I believe the parent directory of the app is the the execution directory, so the path to your resource map is "MyApp.app/Contents/Resources/myResource.txt" You may want to save your files as plaintext because .rtf is going to have a lot of extra stuff in there you don't want to parse through!

BinarySpike
2005.02.05, 12:43 AM
Well,

I have some code for Carbon that loads levels for my game....
And instead of storing them in .rtf store them in .txt.......

http://binary.macintoshdevelopers.net/Shards.html

anyway, If it's a certian size....
Load it into an array (MyArray[20][20])

Take the array and set your level class or struct
against a costant....


enum {
wall = #,
hero = @,
goal pad = &,
ground = +,
movable object = $
};

/// later in your life
Init(x,y)
{
Level[x][y].type = WhatType(MyArray[x][y]);

}

WhatType(x,y)
{
Switch (MyArray[x][y])
case wall:
return kwallFormatedForClass/Struct; // whatever you use for the class/struct storage...
break;

// mostly the same as the first one...
case hero:
case goal pad:
case ground:
case movable object:

}








Then basicly you need a draw function :D

Hope that helps,
BinarySpike :cool:

BinarySpike
2005.02.05, 12:45 AM
Oh,

You might want to add a "nothing" object so that you have better proformance...
:D

Coin
2005.02.05, 02:00 AM
I'm using cocoa, heres what i have

- (IBAction)open:(id)sender
{
NSString *lvl1 = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"lvl1" ofType:@"txt"]];
int a, b;
int index = 0;
for (a = 0 ; a < 20 ; a++) {
for (b = 0 ; b < 20 ; b++) {
switch ([lvl1 characterAtIndex:index]) {
case '#':
enviro[a][b] = WALL;
objects[a][b] = WALL;
break;
case '@':
objects[a][b] = HERO;
xpoh = a;
ypoh = b;
break;
case '+':
enviro[a][b] = GRASS;
objects[a][b] = NONE;
break;
case '$':
objects[a][b] = BLOCK;
break;
default:
objects[a][b] = NONE;
enviro[a][b] = GRASS;
break;
}
index++;
}
index++;
}
[self setNeedsDisplay:YES];
}

that loads the level ALMOST correctly, it loads the top of the map on the left and so on...no idea why

BinarySpike
2005.02.05, 02:08 AM
Well,

That has something to do with a and b....

try switching them...
(instead of [a][b] try [b][a])

This will flip it somehow...
if this doesn't work then it has something to do with the loading...

Oh, along with that "nothing" object you might want to add a
multiple HERO stopper...
(so people can't have more than one Hero)
(unless you want to be able to do that...)

Coin
2005.02.05, 02:14 AM
thats all handled elsewhere

also i already reversed a and b and

blah[b][a] makes it upside down...... really great

im going to be in vegas for a while, stupid other life thing...

anyway here

http://www.nomorepasting.com/paste.php?pasteID=30895

its all there tell me how to be more effient/make it work/anything i should fix

BinarySpike
2005.02.05, 03:02 AM
blah[b][a] makes it upside down...... really great

Ok, then blah[a][b] should be right-side up, instead of...
the top of the map on the left and so on


its all there tell me how to be more effient/make it work/anything i should fix


Well, everything looks good :D

BinarySpike
2005.02.05, 03:03 AM
im going to be in vegas for a while, stupid other life thing...
:lol:

Well, don't lose your shirt ;), :), :D, :lol:.

Coin
2005.02.05, 11:47 AM
Im bringing 2,

Ok, then blah[a][b] should be right-side up,

but it isnt :( :( :( :( :(

its all terrible... and sideways.

Coin
2005.02.05, 01:46 PM
HAHA FIGURED IT OUT,

its reading the file starting at the top left

index = 0;

and it starts drawing to the bottom left
a = 0, b = 0;

now how to fix it?

start a and b at 20 and use a-- and b-- ?

Coin
2005.02.05, 01:55 PM
got it

- (IBAction)open:(id)sender
{
NSString *lvl1 = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"lvl1" ofType:@"txt"]];
int a, b;
int index = 0;
for (a = 20 ; a > 0 ; a--) {
for (b = 0 ; b < 20 ; b++) {
switch ([lvl1 characterAtIndex:index]) {
case '#':
enviro[b][a] = WALL;
objects[b][a] = WALL;
break;
case '@':
objects[b][a] = HERO;
xpoh = b;
ypoh = a;
break;
case '+':
enviro[b][a] = GRASS;
objects[b][a] = NONE;
break;
case '$':
objects[a][b] = BLOCK;
break;
case '&':
enviro[b][a] = GRASS;
objects[b][a] = NONE;
padPos[b][a] = PAD;
break;
}
index++;
}
index++;
}
[self setNeedsDisplay:YES];
}



WOO!