int main(int argc, char* argv[]) { if(argc != 3 && argc != 2){ std::cerr << "<usage>: client ip [port]" << std::endl; return 1; } std::string message; std::string port = argc == 3 ? argv[2] : "12345"; IO client(argv[1], port); client.receive(message); srand(static_cast<unsigned int>(time(0)+message[0])); AI* ai = new AI(message[0]-'0'); client.send(ai->name() + '\n'); while (true) { client.receive(message); if (message == "game end") { break; } else if (message == "action") { const Operation& op = ai->makeDecision(); auto decision = boost::format("%d %d %d\n") % op.source.x % op.source.y % DIR_CHAR[op.direction]; client.send(decision.str()); } else { const int id = message[0]-'0'; const Point src(message[2]-'0', message[4]-'0'); const Point tar(message[6]-'0', message[8]-'0'); ai->update(id, src, tar); } } }
int main() { sf::RenderWindow window(sf::VideoMode(1260, 860), "Tetris AI"); #pragma region set cmd size COORD c; c.X = 200; c.Y = 1000; SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), c); #pragma endregion sf::Clock clock; // starts the clock sf::Clock frameRateClock; bool pause = false; GameLogic* gameLogic = new GameLogic(); AI* ai = new AI(gameLogic); gameLogic->setAI(ai); View* view = new View(&window); view->setGameLogic(gameLogic); view->setAI(ai); #pragma region audio sf::Music music; if (!music.openFromFile("Sound/tetris_theme.wav")) printf("ERROR while loading tetris_theme"); music.play(); music.setVolume(10); music.setLoop(true); #pragma endregion while (window.isOpen()) { #pragma region SFML event stuff + keyboard / mouse events sf::Event event; while (window.pollEvent(event) ) { if (event.type == sf::Event::Closed) window.close(); if (event.key.code == sf::Keyboard::Escape) window.close(); if (event.type == sf::Event::MouseButtonPressed && !view->isGamePlayChosen()) if (event.mouseButton.button == sf::Mouse::Left) view->choseGamePlay(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y); if (event.type == sf::Event::MouseMoved && !view->isGamePlayChosen()) view->highlightButtons(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y); if (event.type == sf::Event::KeyPressed && !gameLogic->getGameOver() && gameLogic->getSinglePlayer()) { if (event.key.code == sf::Keyboard::Right) gameLogic->moveRight(); //drawCurrentTetro(); if (event.key.code == sf::Keyboard::Left) gameLogic->moveLeft(); //drawCurrentTetro(); // hard drop if (event.key.code == sf::Keyboard::Down) gameLogic->automaticDrop(); // firm drop if (event.key.code == sf::Keyboard::Space || event.key.code == sf::Keyboard::Return) gameLogic->firmDrop(); if (event.key.code == sf::Keyboard::F) gameLogic->rotate(1); // // 1 = 90°, 3 = 270° / -90° if (event.key.code == sf::Keyboard::D) gameLogic->rotate(3); // // 1 = 90°, 3 = 270° / -90° } if (event.type == sf::Event::KeyReleased && !gameLogic->getGameOver() && !pause) if (event.key.code == sf::Keyboard::P) pause = true; } #pragma endregion // currently disabled #pragma region framerate //sf::Time frameRateTime = frameRateClock.getElapsedTime(); //float framerate = 1.0f / frameRateTime.asSeconds(); //printf("frameRate: %f \n", framerate ); //frameRateClock.restart().asSeconds(); #pragma endregion window.clear(); if (view->isGamePlayChosen()) { if (!(gameLogic->getGameOver())) { view->drawBackground(); #pragma region trigger automaticDrop sf::Time elapsed = clock.getElapsedTime(); if (!pause) { if (elapsed.asSeconds() >= 0.1f) { if (gameLogic->getGeneticAlgorithmComputing()) { clock.restart(); // returns true, if tetromino has landed. this also triggers spawning new tetrominos within gamelogic.cpp if (gameLogic->dropAI()) { // once the tetromino has landed, starting computation for next tetromino ai->makeDecision(false); } } else if (gameLogic->getFinishedAIPlays()) { clock.restart(); // returns true, if tetromino has landed. this also triggers spawning new tetrominos within gamelogic.cpp if (gameLogic->automaticDrop()) { // once the tetromino has landed, starting computation for next tetromino ai->makeDecision(false); } } } if (elapsed.asSeconds() >= 1.f - 0.05f * gameLogic->getLevel()) { if (gameLogic->getSinglePlayer()) { clock.restart(); gameLogic->automaticDrop(); } } } else if (pause) { //drawPauseScreen(); } #pragma endregion view->drawCurrentTetromino(); view->drawNextTetro(); view->drawLandedArray(); view->drawInterface(); } else view->drawGameOver(); } else view->drawGamePlaySelection(); window.display(); } /*delete ai; delete gameLogic; delete view;*/ return 0; }