Пример #1
0
void InterpretCommand(int clientID, std::string message) {
	bool named = false;
	vector<int> clientIDs = server.getClientIDs();

	ostringstream os;
	ostringstream os2;

	if (message.find("NewPlayer:") == 0) {
		if (message.length() > 10) {
			if (player1 == "")
				player1 = message.substr(10);
			else
				player2 = message.substr(10);
		}
		named = true;
	}

	if (!named) {
		vector<int> clientIDs = server.getClientIDs();
		if (message.length() > 7) {
			if (clientID == clientIDs[0]) {
				snakeState.SetPlayerInput(0, message[8]);
			}
			else { snakeState.SetPlayerInput(1, message[8]); }
		}
	}
}
Пример #2
0
/* called once per select() loop */
void periodicHandler() {
	vector<int> clientIDs = server.getClientIDs();

		static time_t next = time(NULL)+1;
		time_t current = time(NULL);
		messageDelay();
		
		if (gameStarted) {
			if (current   >= next) {
				snakeState.UpdateBoardState();

				ostringstream ss;
				ostringstream score1;
				ostringstream score2;
				ss << "GB:" << snakeState.GetBoardState();
				score1 << "1:" << player1 + " score: " << snakeState.GetPlayerScore(0);
				score2 << "2:" << player2 + " score: " << snakeState.GetPlayerScore(1);

				for (int i = 0; i < clientIDs.size(); i++){
					server.wsSend(clientIDs[i], ss.str());
					server.wsSend(clientIDs[i], score1.str());
					server.wsSend(clientIDs[i], score2.str());
				}

				next = time(NULL) + 1;
			}
		}
}
Пример #3
0
/* called when a client connects */
void openHandler(int clientID) {
	vector<int> clientIDs = server.getClientIDs();

	server.wsSend(clientID, "Welcome!");
	
	ostringstream game_width;
	ostringstream game_height;
	ostringstream game_board;

	game_width << "GW:" << snakeState.GetBoardWidth();
	game_height << "GH:" << snakeState.GetBoardHeight();
	game_board << "GB:" << snakeState.GetBoardState();

	server.wsSend(clientID, game_width.str());
	server.wsSend(clientID, game_height.str());
	server.wsSend(clientID, game_board.str());
	

	if (clientIDs.size() == 2) {
		gameStarted = true;
		snakeState.StartNewGame();
		return;
	}
	else if (clientIDs.size() > 2)
		server.wsClose(clientID);
	else
		gameStarted = false;

}
Пример #4
0
int main()
{
    SnakeGame* game = new SnakeGame();
    game->run();

    free(game);

    return EXIT_SUCCESS;
}
Пример #5
0
int main(int argc, char ** argv){
	vector<pair<int, int> > food;
	food.push_back(make_pair(1,2));
	food.push_back(make_pair(0,1));
	SnakeGame obj = SnakeGame(3, 2, food);
	cout<<"Step 1: "<<obj.move("R")<<endl;
	cout<<"Step 2: "<<obj.move("D")<<endl;
	cout<<"Step 3: "<<obj.move("R")<<endl;
	cout<<"Step 4: "<<obj.move("U")<<endl;
	cout<<"Step 5: "<<obj.move("L")<<endl;
	cout<<"Step 6: "<<obj.move("U")<<endl;
	return 0;
}
Пример #6
0
int main(int argc, char** argv) {

    GameWindow gameWindow;
    Snake* snake = new Snake();            //create player snake
    Fruit* fruit = new Fruit(5, 5);         // create initial fruit
    Dung * dung = new Dung(10, 10);             // create initial dung
    
    gameWindow.setSnake(snake);         //adding snake to the field etc.
    gameWindow.setFruit(fruit);
    gameWindow.setDung(dung);
    SnakeGame* snakeGame = new SnakeGame(&gameWindow);

    snakeGame->startGame();
    
    return 0;
}
Пример #7
0
int main(int argc, char *argv[]) {
	int port;

	cout << "Please set server port: ";
	cin >> port;

	//snakeState.StartNewGame();
	snakeState.UpdateBoardState();

	/* set event handler */
	server.setOpenHandler(openHandler);
	server.setCloseHandler(closeHandler);
	server.setMessageHandler(messageHandler);
	server.setPeriodicHandler(periodicHandler);

	/* start the chatroom server, listen to ip '127.0.0.1' and port '8000' */
	server.startServer(port);
	return 1;
}
Пример #8
0
/* called when a client connects */
void openHandler(int clientID) {
	vector<int> clientIDs = server.getClientIDs();

	server.wsSend(clientID, "Welcome!");
	
	ostringstream game_width;
	ostringstream game_height;
	ostringstream game_board;
	ostringstream ssMove;
	ostringstream playerID;

	game_width << "GW:" << snakeState.GetBoardWidth();
	game_height << "GH:" << snakeState.GetBoardHeight();
	game_board << "GB:" << snakeState.GetBoardState();
	ssMove << "MOVE:" << snakeState.GetPlayerDirection(0) << snakeState.GetPlayerDirection(1);
	playerID << "ID:" << clientID;


	server.wsSend(clientID, playerID.str());
	server.wsSend(clientID, game_width.str());
	server.wsSend(clientID, game_height.str());
	server.wsSend(clientID, game_board.str());
	server.wsSend(clientID, ssMove.str());

	if (clientIDs.size() == 2) {
		gameStarted = true;
		message_to_process[0] = "";
		message_to_process[1] = "";
		emptyQueue();
		last_move[0] = 'D';
		last_move[1] = 'A';
		snakeState.StartNewGame();
		return;
	}
	else if (clientIDs.size() > 2)
		server.wsClose(clientID);
	else
		gameStarted = false;

}
Пример #9
0
int main(int argc,char **argv){
    QApplication  app(argc,argv);
    SnakeGame   sg;//创造一个贪吃蛇
    sg.show();
    return app.exec();
}
Пример #10
0
/* called once per select() loop */
void periodicHandler() {
	vector<int> clientIDs = server.getClientIDs();

		if (last_score1 != snakeState.GetPlayerScore(0) || last_score2 != snakeState.GetPlayerScore(1) ||
			last_move[0] != snakeState.GetPlayerDirection(0) || last_move[1] != snakeState.GetPlayerDirection(1)) {
			
			if (last_score1 != snakeState.GetPlayerScore(0) || last_score2 != snakeState.GetPlayerScore(1)) {
				snakeState.SetPlayerInput(0, 'D');
				snakeState.SetPlayerInput(1, 'A');
			}
			
			emptyQueue();
			message_to_process[0] = "";
			message_to_process[1] = "";
			last_score1 = snakeState.GetPlayerScore(0);
			last_score2 = snakeState.GetPlayerScore(1);
			last_move[0] = snakeState.GetPlayerDirection(0);
			last_move[1] = snakeState.GetPlayerDirection(1);
			snakeState.UpdateBoardState();
		}
		messageDelay();

		LARGE_INTEGER li;
		if (!QueryPerformanceFrequency(&li))
			cout << "QueryPerformanceFrequency failed!\n";
		static double freq = double(li.QuadPart) / 1000.0;
		QueryPerformanceCounter(&li);
		__int64 current = li.QuadPart;
		static __int64 interval = (double)500 * freq; // 500 ms
		static __int64 next = current + interval;
		
		if (gameStarted) {
			if (current   >= next) {
				if (message_to_process[0] != "" && message_to_process[1] != "")
				{
					ProcessMessages();
					message_to_process[0] = "";
					message_to_process[1] = "";
					
					ostringstream ss;
					ostringstream score1;
					ostringstream score2;
					ostringstream ssMove;

					ss << "GB:" << snakeState.GetBoardState();
					score1 << "1:" << player1 + " score: " << snakeState.GetPlayerScore(0);
					score2 << "2:" << player2 + " score: " << snakeState.GetPlayerScore(1);
					ssMove << "MOVE:" << snakeState.GetPlayerDirection(0) << snakeState.GetPlayerDirection(1);

					for (int i = 0; i < clientIDs.size(); i++) {
						server.wsSend(clientIDs[i], ss.str());
						server.wsSend(clientIDs[i], score1.str());
						server.wsSend(clientIDs[i], score2.str());
						server.wsSend(clientIDs[i], ssMove.str());
					}

					snakeState.UpdateBoardState();
					next = current + interval;
				}

	
			}
		}
}
Пример #11
0
int main(int argc, char** argv[])
{
	SnakeGame game;
	game.run();
}