help with snakes and ladders game (again)

6 views
Skip to first unread message

indianapple89

unread,
Dec 26, 2005, 6:02:10 AM12/26/05
to C++ Game Programming
hey everyone
to all those that read my last post, thanks for the help and i've
finished the main game. i've just got one problem now. i'm making a
high score list inside the game which says which player has won the
game how many times. now i'm having a problem with this. everytime its
giving the same output on the highscore page: @ 0. please help. im
giving the relevant coding below. i'm using turbo c++ by thw way. i
can't use any other because its for a school project. thanks in advance

for helping.


void snl::winscr()
{
textbackground(RED);
clrscr();


highscore h[2],temp;
int i=0,j=0,k=0;


fstream file("hs",ios::app|ios::binary);
file.seekg(0,ios::beg);
while (file)
{
file.read((char*)&temp,sizeof(temp));
if (file.eof())
break;
else
{ if (name1==temp.name)
{
j=i;
h[0]=temp;
}
else if (name2==temp.name)
{
k=i;
h[1]=temp;
}
i++;
}

}


if (j==0 && k==0)
{
strcpy(h[0].name,name1);
strcpy(h[1].name,name2);
h[0].score=h[1].score=0;

}


else if (j==0)
{
strcpy(h[0].name,name1);
h[0].score=0;

}


else if (k==0)
{
strcpy(h[1].name,name2);
h[1].score=0;


}


if (win==1)
{
gotoxy(23,16);
textcolor(CYAN+BLINK);
cprintf("CONGRATULATIONS ");
cout<<h[0].name<<"!!!!!!!!!!!!!!!";
h[0].score++;
if (j!=0)
file.seekg(j*sizeof(temp),ios::beg);

file.write((char*)&h[0],sizeof(temp));
while (file)
if (file.eof())
break;
file.write((char*)&h[1],sizeof(temp));

}


else if (win==2)
{
gotoxy(23,12);
textcolor(CYAN+BLINK);
cprintf("CONGRATULATIONS ");
cout<<h[1].name<<"!!!!!!!!!!!!!!!";
h[1].score++;
if (k!=0)
file.seekg(k*sizeof(temp),ios::beg);

file.write((char*)&h[1],sizeof(temp));
while (file)
if (file.eof())
break;
file.write((char*)&h[0],sizeof(temp));

}


file.close();

getch();


}


//---------------------------------HIGH-----------------------------------

void snl::high()
{
textbackground(0);
clrscr();

int i=4;
highscore h;
fstream file("hs",ios::binary|ios::in);
while (file)
{


file.read((char*)&h,sizeof(highscore));
gotoxy(17,i);
cout<<h.name<<" "<<h.score<<endl;
i++;
if (file.eof())
break;


}


file.close();
getch();
menu();


}


PS: name1 and name2 are part of the class snl and have already been
defined.

Spy187

unread,
Dec 28, 2005, 4:35:34 PM12/28/05
to C++ Game Programming
Not very helpful information your giving us. Firstly we don't have the
data of the file thats being read.

And secondly, im not sure i understand.

Isnt a high score suppose to just save a NAME & SCORE and then it sorts
the scores so that the highest score is at the top?

Spy187

unread,
Dec 28, 2005, 4:37:56 PM12/28/05
to C++ Game Programming
Okay well i grabed some code form an old project. it was written in
Turbo Pascal
-----------------------------------------
{**************************************************************************}
{ Highscore table which will show the 10 best scores. }
PROCEDURE HScore;
VAR
HScoreFile : THSFile;
HScoreArr : THSArr;
Last : Integer;
TempScore : String;
InputPos : Integer;
Position : Integer;
Character : Char;


{************************************************************************}
{ Reading Highscore file }
PROCEDURE ReadFile( VAR PFile : THSFile;
VAR PArr : THSArr;
VAR Last : Integer );
BEGIN
Reset(PFile);
i := 1;
WHILE NOT(EOF(PFile)) DO
WITH PArr[i] DO
BEGIN
Readln(PFile,Name);
Readln(PFile,Score);
Inc(i);
END;
Dec(i);
Last := i;
END; { ReadFile }

{************************************************************************}
{ Writing Highscore file }
PROCEDURE WriteFile( VAR PFile : THSFile;
PArr : THSArr;
Last : Integer );
BEGIN
ReWrite(PFile);
FOR i := 1 TO Last DO
WITH PArr[i] DO
BEGIN
Writeln(PFile,Name);
Writeln(PFile,Score);
END;
END; { WriteFile }

{************************************************************************}
{ Sorting Highscore Table }
PROCEDURE Sort( VAR PHScores : THSArr;
Last : Integer );
var
j : integer;

{**********************************************************************}
{ Swop two numbers }
PROCEDURE Swop( VAR Entry1,Entry2 : THScore );
VAR
Temp : THScore;
BEGIN
Temp := Entry1;
Entry1 := Entry2;
Entry2 := Temp;
END; { Swop }


BEGIN
FOR i := (last-1) DOWNTO 1 DO
FOR j := 1 TO i DO
BEGIN
IF (PHScores[j].Score < PHScores[j+1].Score)
THEN Swop(PHScores[j],PHScores[j+1]);
END;
END; { Sort }

BEGIN

while keypressed do readkey;
{$I-} { IOResult checking off }
Assign(HScoreFile,'DATA\HScore.TXT');
ReadFile(HScoreFile,HScoreArr,Last);
{$I+}
Sort(HScoreArr,Last);
InputPos := 0;
{ Placing the player's score in the table. }
IF (Last >= 10) AND (Score > HScoreArr[Last].Score) THEN
BEGIN
HScoreArr[Last].Score := Score;
HScoreArr[Last].Name := '';
Sort(HScoreArr,Last);
FOR i := 1 TO Last DO
IF HScoreArr[i].Score = Score
THEN InputPos := i;
END;

IF (Last < 10) AND (Score > 0) THEN
BEGIN
Inc(Last);
HScoreArr[Last].Score := Score;
HScoreArr[Last].Name := '';
Sort(HScoreArr,Last);
FOR i := 1 TO Last DO
IF HScoreArr[i].Score = Score
THEN InputPos := i;
END;
clear(vaddr);
hud;
Print(30,50,hs_text[1],115,vaddr);
Print(130,50,hs_text[2],115,vaddr);

FOR i := 1 TO Last DO
WITH HScoreArr[i] DO
BEGIN
Str(Score,TempScore);
Print(30,70+((i-1)*10),Name,115,vaddr);
Print(130,70+((i-1)*10),TempScore,115,vaddr);
END;
Flip(vaddr,sega000);

IF ((Last<10) AND (Score>0)) OR ((Last>=10) AND
(Score>HScoreArr[Last].Score)) THEN
BEGIN
i := 1;
Position := 0;
HScoreArr[InputPos].Name := '';
REPEAT
Character := UpCase(Readkey);
IF (NOT(Key = Enter) AND NOT(Key = BackSpace) AND NOT(Key = Left)
AND NOT(Key = Right) AND NOT(Key = Up) AND NOT(Key = Down))
THEN
BEGIN
HScoreArr[InputPos].Name := HScoreArr[InputPos].Name +
Character;
Position := Position + CharWidth(Character);

Print(30,70+((InputPos-1)*10),HScoreArr[InputPos].Name,150,vaddr);
Inc(i);
END;
Flip(vaddr,sega000);
UNTIL (Key = Enter) OR (i = 10);
Score := 0;
readkey;
END
ELSE
BEGIN
readkey;
END;
WriteFile(HScoreFile,HScoreArr,Last);
Close(HScoreFile);
while keypressed do readkey;
key:=0;
END; { HScore }

indianapple89

unread,
Dec 29, 2005, 5:41:26 AM12/29/05
to C++ Game Programming
yes the high score does exactly that except that i havent inserted the
code for sorting the list yet. the problem i am getting is that i'm not
able to modify the existing data even though im getting my file pointer
to the exact location and then overwriting. the program is just
updating the score and then adding it to the end of the file instead of
adding the data at the place where the file pointer is.

Spy187

unread,
Dec 31, 2005, 11:43:37 AM12/31/05
to C++ Game Programming
Cool
-------
Firstly have you opened the file with writing permision (i think you
have cause as you say it appends to the file)

Secondly: Reading from file works writing to a dos console, the cursor
moves so you need to reset the pointer.

Try freset();

Or easier to reset the pointer is to close the file and reopen it with
write permission.

indianapple89

unread,
Jan 11, 2006, 3:27:48 AM1/11/06
to C++ Game Programming
thanks for the help...i've completed my project...yay!!!!.
thanks for all the help guys!!

Reply all
Reply to author
Forward
0 new messages