Help with game

Hi my friend steve and I are making a game and the part i'm working on now is to get the client to be able to move the character around on the server. I have tried different prototypes and none seem to be successful. I am using winsock to do this. Using telnet for client for now.

The process of execution for Client:
1. connect to ip

2. enter player

3. This part is supposed to be moving the character around the screen by pressing w,a,s,d and that doesnt move it. I am only able to move it from the server side. When I tried to change the server to wait for a connection on every key and when it got a key it moved the character on the screen then went back to waiting mode for the other movements.

Server process of execution:

1. wait on port 23

2. handle player and display to map

3. controls the character from server not from client. I'm using telnet for client.



Here is the code which is 580 lines long so far:

[code]
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#include

using namespace std;

/*
Game created by Steve Barbaraso and winsock, logger added by George Mouyios.
George had idea for adding network featurs and file logging.

Changes needed to be made: need bonus food for character. Steve chose ?.
George thought of adding color to objects.
*/

//int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
//{
int main()
{
ofstream log;
log.open("GameLog.txt", ios::out | ios::app);

log<<"

*******************Starting Game Session*************************

";
log.close();


srand( time(0) );

//create the game world of x and y coords. Everything in this program interacts with it.
char world[60][20];
int bonus=0,score=0,level=1,step=0,rndwall=5,rndy,rndx,dum=0,user=0,ux=1,uy=0,y=0,x=0;
//char score=0,level=1,step=0;//a little bug : )

//if this method doesnt work restore the types to
//normal and then change the type conversion

//reseting values for the game world.
while (x<20)
{ //Setting level empty
while (y <60)
{
world[y][x]=' ';
++y;
}
y=0;
++x;
}

y=0;

//Setting unpassable walls with while loop.
while (dum<60)
{
world[y][0]='#';//sets x axis.top row wall.
dum++;
y++;
}//end while()

dum=0;
y=0;
x=1;
while (x<19)
{

world[0][x]='#';//sets x and moves it up to the 59th place from 0.
world[59][x]='#';
x+=1;
}

while (dum<60)
{
world[y][19]='#';
dum++;
y++;
world[59][18]=' ';//character is placed here at these coords.
}
y=0;
dum=0;
x=0;

//I(George) insert this winsock snippet.
//wait for a connection and handle incoming data as a character(Server Side).

WSADATA wsaData;

/*create winsock api version 1.1*/
WORD sockVersion = MAKEWORD(1,1);

WSAStartup(sockVersion, &wsaData);

SOCKET incomeSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

while(1)
{

/*make listening port and obtain ip address*/
struct sockaddr_in server;
struct sockaddr_in client;

server.sin_family = AF_INET;
server.sin_port = htons(23);
server.sin_addr.S_un.S_addr = INADDR_ANY;
//use inet_addr("127.0.0.1") instead of obtaining ip automaticly.

/*
//log ip obtained
if(incomeSock == -1)
{
cerr<<"IP
";
WSACleanup();
exit(1);
}
else
{
log.open("GameLog.txt", ios::out | ios::app);
log<<incomeSock;
log.close();
}
*/

//we have a connection now bind our socket to the ip address and port.
if(bind(incomeSock, (struct sockaddr*)(&server), sizeof(server)) == -1)
{
cerr<<"binding";
WSACleanup();
exit(1);
}
else
{
cout<<"success binding socket";
}

// Make the socket listen
// Up to 10 connections at once
cout<<"
Listening mode.
";
int listensocket;
if(listensocket = listen(incomeSock, 10) == -1)
{
cerr<<"listen
";
WSACleanup();
exit(1);
}
else
{
cout<<"Success
";
//listensocket = getchar();
}

cout<<"Get our Player:
";
int sin_size = sizeof(struct sockaddr_in);
if ((incomeSock = accept(incomeSock, (struct sockaddr *)&client,&sin_size)) == -1)
{
cerr<<"accept";
WSACleanup();
exit(1);
}
else
{
//log it
log.open("GameLog.txt", ios::out | ios::app);
log<<"character alive!";
log.close();
//print to screen
cout<<"character alive!
";
//send to client
//char sendOut[] = "character alive!";
//send(incomeSock, sendOut, sizeof(sendOut), 0);
}

cout<<"
handle connection";
char buffer[] = "";
//char *buffer = new char[4];
if(incomeSock = recv(incomeSock, buffer, sizeof(buffer), 0) == -1)
{
cerr<<"
recieveBytes";
WSACleanup();
exit(1);
}
else
{
cout<<"
recieved bytes";
}

/*//we have now bound the socket please connect
cout<<"
Connecting";
int connectsocket;
if(connectsocket = connect(incomeSock, (sockaddr*)(&sockAddr), sizeof(sockAddr)) == -1)
{
cerr<<"connection";
WSACleanup();
exit(1);
}
else
{
cout<<"success";
}
*/

if(incomeSock == INVALID_SOCKET)
{
cerr<<"
invalid socket!";
WSACleanup();
exit(1);
}

//if there was a character sent over connection perform the following...
if((char)buffer != '`')
{
//End of My insertion. Start of Steves.

world[uy][ux] = (char)buffer;
x=0;
y=0;
dum=0;

//Displaying the screen
while (x<20)
{
while (y <60)
{
cout<<world[y][x];
++y;
}
y=0;
++x;
cout<<"
";
}


//when user presses q reset the x,y coords?
while (user!=113)
{
x=0;
y=0;

clrscr();

//after clearing the screen we move onto the meat of the program.
cout<<"press q to quit | w: up | s: down | a: left | d: right |
Steps: "<<step
<<" Score: "<<score<<" Level: "<<level<<"

";


while (x<20)
{ //Displaying the screen
while (y <60)
{
cout<<world[y][x];
++y;
}
y=0;
++x;
cout<<"
";
}


dum=0;

//Setting of random walls every 5 steps.
if (rndwall==5)
{
clrscr();
cout<<"press q to quit | w: up | s: down | a: left | d: right |
Steps: "<<step
<<" Score: "<<score<<" Level: "<<level<<"

";

x=0;
y=0;

//Setting level empty
while (x<20)
{
while (y <60)
{
world[y][x]=' ';
++y;
}
y=0;
++x;
}
y=0;

//Setting walls
while (dum<60)
{
world[y][0]='#';
dum++;
y++;
}
dum=0;
y=0;
x=1;

while (x<19)
{

world[0][x]='#';
world[59][x]='#';
x+=1;
}

while (dum<60)
{
world[y][19]='#';
dum++;
y++;
world[59][18]=' ';
}

y=0;
dum=0;
x=0;

world[uy][ux]= (char)buffer;
x=0;
y=0;
dum=0;

//setting random walls
while (dum<(level*50))
{
rndy=rand() % 59;
rndx=rand() % 19;
world[rndy][rndx]='#';
dum++;
}

rndwall=0;//reset random wall to 0 and begin incrementing.

world[1][1]=' ';
world[0][1]=' ';
world[uy][ux]= (char)buffer;

//Displaying the screen
while (x<20)
{
while (y <60)
{
cout<<world[y][x];
++y;
}
y=0;
++x;
cout<<"
";
}

}//ends if()
//finished redrawing level

while(1)
{
/*if(user = accept(incomeSock, (sockaddr*)(INADDR_ANY), 0) == -1)
{
cerr<<"
accept
";
WSACleanup();
exit(1);
}
else
{
cout<<"
Success!
";
}
*/

//user = getch();
user = (char)buffer;
if(world[uy-1][ux]=='#' && world[uy+1][ux]=='#' && world[uy][ux+1]=='#' && world[uy][ux-1]=='#') //
user=113;

if (user!=113)
user=getch();
char user2[] = "";
user = (int)buffer;
if(user = recv(incomeSock, user2, sizeof(user2),0)==-1)
{
cerr<<"
RecieveBytes";
WSACleanup();
exit(1);
}

else if (user = recv(incomeSock, user2,sizeof(user2),0)||
user!=113 && user==119 && world[uy][ux-1]!='#'){ //up


if (bonus>0 && bonus < 11)
bonus++;

if(world[uy][ux-1]=='?' && bonus==0)
bonus=1;

world[uy][ux]=' ';
ux-=1;

if (bonus==0)
step+=1;

rndwall+=1;

if (bonus==10)
bonus=0;
}
else

if (user = recv(incomeSock, user2,sizeof(user2),0)||
user!=113 && user==115 && world[uy][ux+1]!='#'){ //down


if (bonus>0 && bonus < 11)
bonus++;

if(world[uy][ux+1]=='?' && bonus==0)
bonus=1;

world[uy][ux]=' ';
ux+=1;

if (bonus==0)
step+=1;

rndwall+=1;

if (bonus==10)
bonus=0;

}
else
if (user = recv(incomeSock, user2,sizeof(user2),0)||
user!=113 && user==97 && world[uy-1][ux]!='#'){ //left


if (bonus>0 && bonus < 11)
bonus++;

if(world[uy-1][ux]=='?' && bonus==0)
bonus=1;

world[uy][ux]=' ';
uy-=1;

if (bonus==0)
step+=1;

rndwall+=1;

if (bonus==10)
bonus=0;

}
else
if (user = recv(incomeSock, user2,sizeof(user2),0)||
user!=113 && user==100 && world[uy+1][ux]!='#'){ //right


if (bonus>0 && bonus < 11)
bonus++;

if(world[uy+1][ux]=='?' && bonus==0)
bonus=1;

world[uy][ux]=' ';
uy+=1;

if (bonus==0)
step+=1;


rndwall+=1;

if (bonus==10)
bonus=0;
}
}//end while(1)
world[uy][ux]= (char)buffer;

if (world[59][18]== (char)buffer) { //printing out level win screen and reseting charecter
clrscr();

if (step<20)
score+=200;
else
if (step<50)
score+=160;
else
if (step<60)
score+=150;
else
if (step<70)
score+=140;
else
if (step<80)
score+=100;
else
if (step<100)
score+=95;
else
if (step<120)
score+=80;
else
if (step<140)
score+=70;
else
if (step<160)
score+=65;
else
if (step<180)
score+=60;
else
if (step<190)
score+=50;
else
if (step<210)
score+=45;
else
if (step<230)
score+=40;
else
if (step<240)
score+=10;
else
if (step>240)
score+=5;

//Setting a score log ***************************

log.open("GameLog.txt", ios::out | ios::app);

log ";
while(user!=113)
user=getch();
user=0;
level+=1;
x=0;
y=0;
dum=0;

step=0;
rndwall=0;
world[uy][ux]=' ';
uy=0;
ux=1;
world[uy][ux]= (char)buffer;




}









}

log.open("GameLog.txt", ios::out | ios::app);

log<<"

*******************End of Game Session*************************

";
log.close();
cout<<"
Thank you for playing
";

return 0;

}
}
}
[/code]

Thanks George

<------------------------------------------>

Reach me at:

AIM IM: kc2keo
EMAIL: kc2keo@yahoo.com

Comments

  • Hi again. We have started to make a couple of changes to the game. First of all Steve has started fooling around with BGI Graphics from an old Pong game. I have been trying to set up multiple operating systems. I am trying to move over to the Linux development envirnment and its kind of difficult given that we are using DirectMusic, Winsock and some functions from windows.h I'm trying to find substitutes in place of those.

    Thats about it right now,
    George

    <------------------------------------------>

    Reach me at:

    AIM IM: kc2keo
    EMAIL: kc2keo@yahoo.com

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion