void Game::msgGameWon( Msg* msg ) { MsgGameWon* gameWonMsg = (MsgGameWon*)msg; wonGame(); delete gameWonMsg; }
int main(int args, char *argv[]) { char message[100]; MainPlayerData toPlayerData; CandidateData toCandidateData; StatusType status; fd_set allSocket; fd_set readfds; int fdmax, k, l; int n; int playerIndex; int mainPlayerSelected = 0; int clientId; int playerAnswer; int level; int numberOfClient = 0; int yes = 1; int readResult; int listener; int acceptSocket; struct sockaddr_in server, client; int sin_size; int socket_in_allSocket; int nbytes; int i, j; char clientName[40]; initializeClientList(); createCashPrizeList(); sin_size = sizeof(struct sockaddr_in); FD_ZERO(&allSocket); FD_ZERO(&readfds); if (args < 2) { printf("Usage : %s <PORT>\n", argv[0]); exit(-1); } readResult = readQuestionFile("question.txt"); if (readResult < 0) { printf("Can't load the questions !"); exit(-1); } if ((listener = socket(AF_INET, SOCK_STREAM, 0)) == -1) { printf("socket() error!\n"); exit(-1); } server.sin_family = AF_INET; server.sin_port = htons(atoi(argv[1])); server.sin_addr.s_addr = INADDR_ANY; bzero(&(server.sin_zero), 8); setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); if (bind(listener, (struct sockaddr*) &server, sizeof(struct sockaddr)) == -1) { printf("bind() error!\n"); exit(-1); } //listen if (listen(listener, 2) == -1) { perror("listen"); exit(3); } FD_SET(listener, &allSocket); fdmax = listener; printf("\t<<<Gameshow GHE NONG - AI LA TRIEU PHU>>>\n"); while (1) { readfds = allSocket; /* On error, -1 is returned, select() and pselect() return the number of file descriptors */ if (mainPlayerSelected == 0) { if (select(fdmax + 1, &readfds, NULL, NULL, NULL ) == -1) { perror("select"); printf("errno = %d.\n", errno); exit(4); } } for (i = 0; i <= fdmax; i++) { if (FD_ISSET(i, &readfds)) { //printf("isset i=%d\n", i); if (i == listener) { acceptSocket = accept(listener, (struct sockaddr*) &client, &sin_size); printf("Listener : %d accept %d\n", listener, acceptSocket); //in dia chi ip printf("\n client address %s\n", inet_ntoa(client.sin_addr)); if (acceptSocket == -1) { perror("accept"); } else if (isClientListFull()) { clientId = NULL_ID; send(acceptSocket, &clientId, sizeof clientId, 0); close(acceptSocket); //FD_CLR(acceptSocket, &allSocket); } else { FD_SET(acceptSocket, &allSocket); printf("Server connected with socket %d\n", acceptSocket); addClient(acceptSocket); clientId = acceptSocket; if (acceptSocket > fdmax) { fdmax = acceptSocket; //printf("Now fdmax is : %d\n", fdmax); } send(acceptSocket, &clientId, sizeof clientId, 0); numberOfClient = getNumberOfClient(); printf("We're now having %d clients connected !\n", numberOfClient); } } else { // printf("i before switch = %d Fmax=%d\n", i, fdmax); switch_case: switch (getClientStatus(i)) { case CONNECTED: if ((nbytes = recv(i, &toCandidateData, sizeof toCandidateData, 0)) <= 0) { printf( "Server stopped connecting to client id %d\n", i); close(i); removeClient(i); printf("%d clients left !\n", getNumberOfClient()); FD_CLR(i, &allSocket); break; } else { recv(i, clientName, sizeof clientName, 0); setClientStatus(i, WAITING, clientName); strcpy(message, "Welcome : "); strcat(message, clientName); strcat(message, "\n"); strcpy(toCandidateData.message, message); toCandidateData.status = WAITING; send(i, &toCandidateData, sizeof toCandidateData, 0); printClientList(); /* check if have enough 6 clients, so we can start the game */ if (readyToStart()) { printf("We can start the game !!\n"); k = randomNumber(NUMBERS_PER_QUES); //send question to all the clients // for (j = 0; j < BACKLOG; j++) { setCompetition(j); copyQuestion(&(toCandidateData.question), questLib[0][k]); toCandidateData.ansTime = 0; toCandidateData.status = COMPETING; strcpy(toCandidateData.message, QUICK_QUESTION_MESSAGE); send(clientInfo[j].id, &toCandidateData, sizeof(CandidateData), 0); } //get the answer back from them // for (j = 0; j < BACKLOG; j++) { if (recv(clientInfo[j].id, &toCandidateData, sizeof toCandidateData, 0) <= 0) { printf( "Server stopped connecting to client id %d\n", i); close(i); removeClient(i); printf("%d clients left !\n", getNumberOfClient()); FD_CLR(i, &allSocket); } else { clientInfo[j].quick_answer = toCandidateData.answer; clientInfo[j].ansTime = toCandidateData.ansTime; } } //printf("Sending question done!\n"); printClientsAnswerList(); //select the main player // playerIndex = selectMainPlayer(questLib[0][k], BACKLOG); printf( "Our answer for the quick question : %d\n", questLib[0][k].ans); printf("====MAIN PLAYER SELECTED : %s ====\n", clientInfo[playerIndex].clientName); printClientStatus(clientInfo[playerIndex].id); startGame(playerIndex); for (j = 0; j < BACKLOG; j++) { if (j != playerIndex) { strcpy(toCandidateData.message, JOINER_MESSAGE); } else { strcpy(toCandidateData.message, MAIN_PLAYER_SELECTED); strcpy(toPlayerData.name, clientInfo[j].clientName); } toCandidateData.status = getClientStatus( clientInfo[j].id); send(clientInfo[j].id, &toCandidateData, sizeof(CandidateData), 0); } printClientList(); //printf("Current i = %d\n", i); i = clientInfo[playerIndex].id; mainPlayerSelected = 1; goto switch_case; // break; } else { sprintf(message, "We now have %d players online, please wait until we have more %d client(s) !\n", getNumberOfClient(), BACKLOG - getNumberOfClient()); strcpy(toCandidateData.message, message); send(i, &toCandidateData, sizeof toCandidateData, 0); } } break; case JOINING: //printf("[JOINING] Current i = %d\n", i); // i = 0; // if (mainPlayerSelected == 1) { // if ((nbytes = recv(i, &toCandidateData, // sizeof toCandidateData, 0)) <= 0) { // printf( // "Server stopped connecting to client id %d\n", // i); // close(i); // removeClient(i); // printf("%d clients left !\n", // getNumberOfClient()); // FD_CLR(i, &allSocket); // break; // } // } break; case PLAYING: mainPlayerSelected = 1; initializeMainPlayerData(&toPlayerData); level = 1; do { selectQuestion(level, &toPlayerData); send(clientInfo[playerIndex].id, &toPlayerData, sizeof toPlayerData, 0); for (n = 0; n < BACKLOG; n++) { if (clientInfo[n].status == JOINING) { send(clientInfo[n].id, &toPlayerData, sizeof toPlayerData, 0); } } if (recv(i, &playerAnswer, sizeof playerAnswer, 0) <= 0) { printf( "Server stopped connecting to client id %d\n", i); close(i); removeClient(i); printf("%d clients left !\n", getNumberOfClient()); FD_CLR(i, &allSocket); gameFinish(&toPlayerData,END); for (n = 0; n < BACKLOG; n++) { if (clientInfo[n].status == JOINING) { toPlayerData.status = FINISHED; send(clientInfo[n].id, &toPlayerData, sizeof toPlayerData, 0); close(clientInfo[n].id); removeClient(clientInfo[n].id); FD_CLR(clientInfo[n].id, &allSocket); } } continue; } if (playerAnswer == RIGHT) { printf("Good job! The player gave a right answer!\n"); increaseQuestion(&toPlayerData); //tra loi dung o cau hoi so 15 if (level == MAX_LEVEL) { wonGame(&toPlayerData); send(i, &toPlayerData, sizeof toPlayerData, 0); for (n = 0; n < BACKLOG; n++) { if (clientInfo[n].status == JOINING) { send(clientInfo[n].id, &toPlayerData, sizeof toPlayerData, 0); close(clientInfo[n].id); removeClient(clientInfo[n].id); FD_CLR(clientInfo[n].id, &allSocket); } } close(i); removeClient(i); FD_CLR(i, &allSocket); exit(1); } level = level + 1; } else { endGame(&toPlayerData, playerAnswer); gameFinish(&toPlayerData,playerAnswer); send(i, &toPlayerData, sizeof toPlayerData, 0); for (n = 0; n < BACKLOG; n++) { if (clientInfo[n].status == JOINING) { toPlayerData.status = FINISHED; send(clientInfo[n].id, &toPlayerData, sizeof toPlayerData, 0); close(clientInfo[n].id); removeClient(clientInfo[n].id); FD_CLR(clientInfo[n].id, &allSocket); } } close(i); removeClient(i); printf("%d clients left !\n", getNumberOfClient()); FD_CLR(i, &allSocket); } } while (playerAnswer == RIGHT && level <= MAX_LEVEL); printf("The game of player : %s ended !\n", toPlayerData.name); exit(1); break; default: break; } } } } } }
static void beginNewGame(int difficulty) { int turn_number = 0; char buffer[1]; Board board = {'-', '-', '-', '-', '-', '-', '-', '-', '-'}; bool occupied[9]; for(int index = 0; index < 9; index++) { occupied[index] = false; } while(true) { turn_number++; while(true) { for(unsigned int bufIndex = 0; bufIndex < sizeof(buffer); bufIndex++) { memset(&buffer[bufIndex], 0, sizeof(buffer[bufIndex])); // empty buffer } char * ptr; printf("Please enter a number (1-9): > "); fgets(buffer, 15, stdin); if(buffer[0] == 'q') { printf("Thanks for Playing! :)"); exit(EXIT_SUCCESS); } else if(buffer[0] == '0') { printf("Invalid Choice.\n"); continue; } else if(isdigit(buffer[0])) { int user_entered_num = (int) strtol(buffer, &ptr, 10); if(!occupied[user_entered_num - 1]) { occupied[user_entered_num - 1] = true; break; } } } char * ptr; int user_position = (int) strtol(buffer, &ptr, 10); switch(user_position) { case 1: board.a = 'x'; break; case 2: board.b = 'x'; break; case 3: board.c = 'x'; break; case 4: board.d = 'x'; break; case 5: board.e = 'x'; break; case 6: board.f = 'x'; break; case 7: board.g = 'x'; break; case 8: board.h = 'x'; break; case 9: board.i = 'x'; break; default: break; } printf("Current Board: \n"); printf("%c %c %c\n%c %c %c\n%c %c %c\n", board.a, board.b, board.c, board.d, board.e, board.f, board.g, board.h, board.i); if(wonGame(board, 'x')) { printf("Nice Job - you won the game!\n"); return; } if(turn_number == 5) { printf("This game is officially a draw. Well played, grasshopper.\n"); return; } printf("Computer now choosing Move...\n"); int comp_move = 0; if(difficulty == 1) { // easy comp_move = user_position - 1; while(occupied[comp_move]) { comp_move = (rand() % 9); } } else { // normal if(!occupied[4]) {comp_move = 4;} // start of horizontal 3-in-a-row checks: else if( (board.a != '-') && (board.a == board.b) && (!occupied[2]) ) {comp_move = 2;} // 1. if the top-left and top-center squares are both either 'x' or 'o' // and the top-right square is empty, move there. else if( (board.a != '-') && (board.a == board.c) && (!occupied[1]) ) {comp_move = 1;} // 2. if the top-left and top-right squares are both either 'x' or 'o' // and the top-center square is empty, move there. else if( (board.b != '-') && (board.b == board.c) && (!occupied[0]) ) {comp_move = 0;} // and so on else if( (board.d != '-') && (board.d == board.e) && (!occupied[5]) ) {comp_move = 5;} else if( (board.d != '-') && (board.d == board.f) && (!occupied[4]) ) {comp_move = 4;} else if( (board.e != '-') && (board.e == board.f) && (!occupied[3]) ) {comp_move = 3;} else if( (board.g != '-') && (board.g == board.h) && (!occupied[8]) ) {comp_move = 8;} else if( (board.g != '-') && (board.g == board.i) && (!occupied[7]) ) {comp_move = 7;} else if( (board.h != '-') && (board.h == board.i) && (!occupied[6]) ) {comp_move = 6;} // start of vertical 3-in-a-row checks: else if( (board.a != '-') && (board.a == board.d) && (!occupied[6]) ) {comp_move = 6;} else if( (board.a != '-') && (board.a == board.g) && (!occupied[3]) ) {comp_move = 3;} else if( (board.d != '-') && (board.d == board.g) && (!occupied[0]) ) {comp_move = 0;} else if( (board.b != '-') && (board.b == board.e) && (!occupied[7]) ) {comp_move = 7;} else if( (board.b != '-') && (board.b == board.h) && (!occupied[4]) ) {comp_move = 4;} else if( (board.e != '-') && (board.e == board.h) && (!occupied[1]) ) {comp_move = 1;} else if( (board.c != '-') && (board.c == board.f) && (!occupied[8]) ) {comp_move = 8;} else if( (board.c != '-') && (board.c == board.i) && (!occupied[5]) ) {comp_move = 5;} else if( (board.f != '-') && (board.f == board.i) && (!occupied[2]) ) {comp_move = 2;} // start of diagonal 3-in-a-row checks: else if( (board.a != '-') && (board.a == board.e) && (!occupied[8]) ) {comp_move = 8;} else if( (board.a != '-') && (board.a == board.i) && (!occupied[4]) ) {comp_move = 4;} else if( (board.e != '-') && (board.e == board.i) && (!occupied[0]) ) {comp_move = 0;} else if( (board.b != '-') && (board.b == board.e) && (!occupied[6]) ) {comp_move = 6;} else if( (board.b != '-') && (board.b == board.g) && (!occupied[4]) ) {comp_move = 4;} else if( (board.e != '-') && (board.e == board.g) && (!occupied[2]) ) {comp_move = 2;} else { // if all else fails, choose a randomized move comp_move = user_position - 1; while(occupied[comp_move]) { comp_move = (rand() % 9); } } } occupied[comp_move] = true; usleep(4000000); printf("Computer chooses move #%d\n", comp_move + 1); switch(comp_move + 1) { case 1: board.a = 'o'; break; case 2: board.b = 'o'; break; case 3: board.c = 'o'; break; case 4: board.d = 'o'; break; case 5: board.e = 'o'; break; case 6: board.f = 'o'; break; case 7: board.g = 'o'; break; case 8: board.h = 'o'; break; case 9: board.i = 'o'; break; default: break; } printf("Current Board: \n"); printf("%c %c %c\n%c %c %c\n%c %c %c\n", board.a, board.b, board.c, board.d, board.e, board.f, board.g, board.h, board.i); if(wonGame(board, 'o')) { printf("The computer won this round.\n"); return; } } }
// update the scene based on the time elapsed since last update static void Update(float secondsElapsed) { runTime += secondsElapsed; glm::vec3 lInput; glm::vec2 rInput; Camera* cam = &Camera::getInstance(); glm::vec3 f = cam->forward(); glm::vec3 r = cam->right(); glm::vec3 fmy = cam->forward(); fmy.y = 0; bool shoot = false; ComponentInput* c = static_cast<ComponentInput*>(model->GetComponent(CONTROLLER)); if (c->Refresh()) { lInput = glm::vec3(c->leftStickX, 0, c->leftStickY); rInput = glm::vec2(c->rightStickX / 5, c->rightStickY / 5); if (c->rightTrigger > 0.5 && shotcd > SHOT_CD) shoot = true; } else { const float moveSpeed = 0.2f; //units per second if (glfwGetKey(gWindow, 'S')) lInput.z = -1; else if (glfwGetKey(gWindow, 'W')) lInput.z = 1; if (glfwGetKey(gWindow, 'A')) lInput.x = -1; else if (glfwGetKey(gWindow, 'D')) lInput.x = 1; if (glfwGetKey(gWindow, ' ')) { shoot = true; } //rotate camera based on mouse movement const float mouseSensitivity = 0.005f; double mouseX, mouseY; glfwGetCursorPos(gWindow, &mouseX, &mouseY); //Camera::getInstance().offsetOrientation(mouseSensitivity * (float)mouseY, mouseSensitivity * (float)mouseX); glfwSetCursorPos(gWindow, 0, 0); rInput.x = mouseX * mouseSensitivity; rInput.y = -mouseY * mouseSensitivity; } if (gameState == GAME) { //Begin Camera code model->pos += fmy * (c->getOwner()->vel * lInput.z); model->pos += cam->right() * (c->getOwner()->vel * lInput.x); cam->offsetPosition(model->pos - cam->position()); c->getOwner()->dir = glm::rotateX(c->getOwner()->dir, -rInput.y); c->getOwner()->dir = glm::rotateY(c->getOwner()->dir, rInput.x); cam->offsetOrientation(-rInput.y, rInput.x); //End Camera code for (int i = 0, s = ObjectManager::instance().pMap.size(); i < s; i++) ObjectManager::instance().pMap[i]->update(secondsElapsed); glm::vec3 p = Camera::getInstance().position(); if (shoot && shotcd > SHOT_CD) { Projectile* pr = new Projectile(p, f, 0.5, 100, 10); ObjectManager::instance().pMap.push_back(pr); shotcd = 0; } CollisionManager::instance().checkAll(); ObjectManager::instance().updateProjectile(secondsElapsed); for (int i = 0, s = targets.size(); i < s; i++) { targets[i]->update(secondsElapsed/10); if (targets[i]->hit && targets[i]->alive) { targets[i]->alive = false; skull->play(); targetsKilled++; } } skull->update(secondsElapsed); model->pos.y = 0.5; playTime += secondsElapsed; if (score >= 0) { float gameTime = playTime / 1000; score = ((gameTime * 100) / pow(gameTime, 2)) * 100 - 15; } else score == 0; if (targetsKilled == targets.size() || c->IsPressed(XINPUT_GAMEPAD_BACK)) wonGame(); //cam->lookAt(glm::vec3(7.5, 0, -11)); } else if (gameState == MENU) { //camInterp.speedControlInterp(secondsElapsed/40); //cam->setPosition(camInterp.pos); cam->lookAt(glm::vec3(1000, 0, 0)); //camInterp.speedControlInterp(secondsElapsed/40); //cam->setPosition(camInterp.pos); cam->offsetPosition(cam->right() * 0.1f); if (c->Refresh()) { if (c->IsPressed(XINPUT_GAMEPAD_START)) { startGame(); } } else { if (glfwGetKey(gWindow, ' ')) { startGame(); } } } std::vector<glm::mat4> trans; shotcd += secondsElapsed; tElap += secondsElapsed; //Will need to uncomment the following and have gModel relate to the mech's graphics animatedMechGC->BoneTransform(tElap, trans); }