int main () { srand (time (NULL)); int numGames, numPlayers; Card deck[NUM_CARDS]; Card hand[MAX_PLAYER][HAND_SIZE]; getGameInfo (&numGames); initFile (numGames); for (int i = 0; i < numGames; i++) { getNumPlayers (&numPlayers); int handScores[numPlayers][1 + HAND_SIZE]; memset(handScores, 0, numPlayers * (1 + HAND_SIZE) * sizeof(int)); int bestHandIndices[numPlayers]; memset (bestHandIndices, 0, numPlayers*sizeof(int)); int *numTied = (int *)malloc(sizeof(int)); *numTied = 0; printfName (i + 1, numPlayers); initDeck (deck); shuffleDeck (deck); dealHands (deck, hand, numPlayers); evaluateWinner (hand, numPlayers, handScores, bestHandIndices, numTied); for (int j = 0; j < numPlayers; j++) { printResults (&hand[j][0], HAND_SIZE, j, handScores); } printWinner (i + 1, hand, numPlayers, handScores, bestHandIndices, *numTied); } }
void GameManager::play() { std::ifstream input; while (!gameHasBeenWon()) { updateCurrentPlayer(); playOneFullTurn(input); } printWinner(); std::cout << board; }
void GameManager::testDriver(std::ifstream &input) { std::string nextLine; char layout[7][7]; for (int i = 0; i < 7; i++) { std::getline(input, nextLine); for (int j = 0; j < 7; j++) { layout[i][j] = nextLine[j]; } } board = Board(layout); if (!gameHasBeenWon()) { updateCurrentPlayer(); playOneFullTurn(input); if (!gameHasBeenWon()) { updateCurrentPlayer(); playOneFullTurn(input); } else { printWinner(); } } else { printWinner(); } std::cout << board; }
void GameManager::start() { while (!isExit() && m_alivePlayersCount > 1) { m_gameCounter++; // assume the game counter is lower than int max value. Sleep(SLEEP_TIME); if (isExit()) { continue; } m_gameBoard.moveArrows(); // move the game players if (m_gameCounter % 2 == 0) { for (int iPlayer = 0; iPlayer < m_alivePlayersCount; iPlayer++) { m_players[iPlayer]->move(); if (m_gameCounter % 15 == 0) { //TODO - implement a shooting algorithm - probably should be in the player class on the shoot method. m_players[iPlayer]->shoot(); } } // TODO - handle more then 2 players if (m_alivePlayersCount > 1) { handleCollision(*(m_players[0]), *(m_players[1])); } } if (m_gameCounter % 4 == 0) { m_gameBoard.addSurprise(); } } if (m_alivePlayersCount == 1) { printWinner(); } }
void interfaceLoop() { Puzzle *puzzle = NULL; char command = '\0'; showSolution = false; createDirectionKeys(); createOptionKeys(); do { puzzle = createPuzzle(getPuzzleSize()); shuffleCount = getShuffleCount(); shufflePuzzle(puzzle, shuffleCount); showSolution = false; while( !isSolved(puzzle) ){ printHeader(); printPuzzle(puzzle); command = getNextCommand(puzzle); if(command == INVERT_KEY) { puzzle->inverted = (puzzle->inverted) ? false : true; invertSolution(puzzle); setDirectionVectors(puzzle); } else if(command == SOLVE_KEY) showSolution = (showSolution) ? false : true; else if(command == QUIT_KEY || command == RESTART_KEY) break; else applyDirection(puzzle, getData(directionKeys, command)); } if( isSolved(puzzle) ) { printPuzzle(puzzle); printWinner(); } destroyPuzzle(&puzzle); } while(command == RESTART_KEY); destroyOptionKeys(); destroyDirectionKeys(); }
void pong(bool** ledArray) { //Playfield constants: const float TOP_MARGIN = 0.0; //The margins bound the area controlled by the game. const float BOT_MARGIN = 0.0; const float LEFT_MARGIN = 0.0; const float RIGHT_MARGIN = 0.0; const float BOT_END = ARRAY_HEIGHT - BOT_MARGIN - 1.0; const float RIGHT_END = ARRAY_WIDTH - RIGHT_MARGIN - 1.0; //Object constants: const int BALL_DIAMETER = 2; const float INIT_BALL_X_SPEED = 0.5; const float MAX_BALL_Y_SPEED = 0.5; const int PAD_HEIGHT = 8; const int PAD_WIDTH = 2; const int PAD_DISTANCE = 3; //The distance from each paddle to its side. const float LEFT_PAD_X = LEFT_MARGIN + PAD_DISTANCE; //Refers to the innermost pixel on the top of the paddle. const float RIGHT_PAD_X = RIGHT_END - (PAD_WIDTH - 1.0) - PAD_DISTANCE; const float LEFT_IMPACT_X = LEFT_PAD_X + PAD_WIDTH; const float RIGHT_IMPACT_X = RIGHT_PAD_X - BALL_DIAMETER; const float PAD_MOVE_DISTANCE = 1.0; //Object initialization: float ballDeltaX = INIT_BALL_X_SPEED; float ballDeltaY = 0; float ballX = LEFT_MARGIN + (RIGHT_END - BALL_DIAMETER - LEFT_MARGIN) / 2; float ballY = TOP_MARGIN + (BOT_END - BALL_DIAMETER - TOP_MARGIN) / 2; /*the last number is to test stationary impacts*/ float prevBallX = ballX; float prevBallY = ballY; float leftPadY = TOP_MARGIN + (BOT_END + 1 - TOP_MARGIN) / 2 - PAD_HEIGHT / 2; float rightPadY = TOP_MARGIN + (BOT_END + 1 - TOP_MARGIN) / 2 - PAD_HEIGHT / 2; int leftPadDir = 0; int rightPadDir = 0; drawRectangle(ledArray, true, rightPadY + 0.5, RIGHT_PAD_X, PAD_HEIGHT, PAD_WIDTH); drawRectangle(ledArray, true, leftPadY + 0.5, LEFT_PAD_X, PAD_HEIGHT, PAD_WIDTH); //Score initialization int leftScore = 0; int rightScore = 0; int scoreDelay = 10; //Deactivates ball movement for a number of cycles after someone scores while(1) { frameTest(ledArray, TOP_MARGIN, LEFT_MARGIN, BOT_END, RIGHT_END); //Paddle movement: leftPadDir = getLeftInput(); leftPadDir = movePaddle(leftPadDir, &leftPadY, PAD_MOVE_DISTANCE, PAD_HEIGHT, ledArray, LEFT_PAD_X, PAD_WIDTH, TOP_MARGIN, BOT_END); rightPadDir = getRightInput(); rightPadDir = movePaddle(rightPadDir, &rightPadY, PAD_MOVE_DISTANCE, PAD_HEIGHT, ledArray, RIGHT_PAD_X, PAD_WIDTH, TOP_MARGIN, BOT_END); //Ball movement: if (scoreDelay > 0) { scoreDelay--; } else { drawBall(ledArray, false, prevBallY + 0.5, prevBallX + 0.5, BALL_DIAMETER, LEFT_MARGIN, RIGHT_END); drawBall(ledArray, true, ballY + 0.5, ballX + 0.5, BALL_DIAMETER, LEFT_MARGIN, RIGHT_END); prevBallX = ballX; prevBallY = ballY; ballY += ballDeltaY; ballDeltaY *= checkWallImpact(&ballY, BALL_DIAMETER, TOP_MARGIN, BOT_END); ballX += ballDeltaX; if(ballX <= LEFT_IMPACT_X) { //A paddle collision or score is possible drawRectangle(ledArray, true, leftPadY + 0.5, LEFT_PAD_X, PAD_HEIGHT, PAD_WIDTH); checkLeftImpact(ballY, &ballX, LEFT_IMPACT_X, &ballDeltaY, &ballDeltaX, BALL_DIAMETER, leftPadY, PAD_HEIGHT, MAX_BALL_Y_SPEED); //Check if the ball hit the edge of the paddle (not yet done) (non-critical functionality) //Checking if the ball hit the left edge of the screen: if(ballX + BALL_DIAMETER < LEFT_MARGIN) { drawBall(ledArray, false, prevBallY + 0.5, prevBallX + 0.5, BALL_DIAMETER, LEFT_MARGIN, RIGHT_END); rightScore++; //Also change displayed score, if it exists. if(rightScore == 3) { drawRectangle(ledArray, false, rightPadY + 0.5, RIGHT_PAD_X, PAD_HEIGHT, PAD_WIDTH); drawRectangle(ledArray, false, leftPadY + 0.5, LEFT_PAD_X, PAD_HEIGHT, PAD_WIDTH); printWinner(1); break; } resetBall(&ballX, &ballY, &ballDeltaX, BALL_DIAMETER, TOP_MARGIN, BOT_END, LEFT_MARGIN, RIGHT_END, INIT_BALL_X_SPEED); scoreDelay = 10; } } else if(ballX > RIGHT_IMPACT_X) { //A paddle collision or score is possible drawRectangle(ledArray, true, rightPadY + 0.5, RIGHT_PAD_X, PAD_HEIGHT, PAD_WIDTH); checkRightImpact(ballY, &ballX, RIGHT_IMPACT_X, &ballDeltaY, &ballDeltaX, BALL_DIAMETER, rightPadY, PAD_HEIGHT, MAX_BALL_Y_SPEED); //Checking if the ball hit the edge of the paddle: (not yet done) (non-critical functionality) //Checking if the ball hit the right edge of the screen: if(ballX > RIGHT_END) { drawBall(ledArray, false, prevBallY + 0.5, prevBallX + 0.5, BALL_DIAMETER, LEFT_MARGIN, RIGHT_END); leftScore++; //Also change displayed score, if it exists. if(leftScore == 3) { drawRectangle(ledArray, false, rightPadY + 0.5, RIGHT_PAD_X, PAD_HEIGHT, PAD_WIDTH); drawRectangle(ledArray, false, leftPadY + 0.5, LEFT_PAD_X, PAD_HEIGHT, PAD_WIDTH); printWinner(2); break; } resetBall(&ballX, &ballY, &ballDeltaX, BALL_DIAMETER, TOP_MARGIN, BOT_END, LEFT_MARGIN, RIGHT_END, INIT_BALL_X_SPEED); scoreDelay = 10; } } } } return; }
int main(int argc, char const *argv[]){ char port[20]; char address[50]; int i, j; if (argc<1 || argc>3){ printf(ILLEGAL_ARGS); exit(1); } if (argc == 1 || argc == 2){ strcpy(port, DEFAULT_PORT); if (argc == 1) strcpy(address, DEFAULT_HOST); } if (argc == 2 || argc == 3){ strcpy(address, argv[1]); if (argc == 3){ strcpy(port, argv[2]); } } int sock = socket(AF_INET, SOCK_STREAM, 0); // Get socket if (sock == -1){ printf(SOCKET_ERROR, strerror(errno)); return errno; } // Connect to server sock = connectToServer(sock, address, port); // Get initial data char readBuf[BUF_SIZE]; int bufSize = BUF_SIZE; receive_all(sock, readBuf, &bufSize, 1); // Get the initial data from the server if (game.valid == 0){ printf(CONNECTION_REJECTION); return 0; } playerId = game.myPlayerId; myTurn = game.isMyTurn; printf("You are client %d\n", playerId + 1); if (playerId == 0){ printf("Waiting to client 2 to connect.\n"); receive_all(sock, readBuf, &bufSize, 1); //wait until second player connects } printGameState(game); int addReadyForSend = 0; fd_set fdSetRead, fdSetWrite; struct clientMsg cm; if (myTurn == 1){ printf(YOUR_TURN); } while (game.win == -1){ int maxClientFd = sock; FD_ZERO(&fdSetRead); FD_ZERO(&fdSetWrite); FD_SET(STDIN, &fdSetRead); FD_SET(sock, &fdSetRead); if (addReadyForSend == 1){ FD_SET(sock, &fdSetWrite); } int fdReady = select(maxClientFd + 1, &fdSetRead, &fdSetWrite, NULL, NULL); if (fdReady == 0){ //chicken check. continue; } if (FD_ISSET(sock, &fdSetWrite) && addReadyForSend == 1){//packets are ready for send int buf = BUF_SIZE; char cmBuffer[BUF_SIZE]; i = 0; while (i<cmQueueLength){ //send as fifo createClientMsgBuff(cmQueue[i], cmBuffer); if (send_all(sock, cmBuffer, &buf) == -1){ break; } i++; } j = -1; while (i<cmQueueLength){ //reorganize cmQueue j++; cmQueue[j] = cmQueue[i]; i++; } cmQueueLength = j + 1; if (cmQueueLength == 0){ addReadyForSend = 0; }; } if (FD_ISSET(STDIN, &fdSetRead)){// there is input from cmd int rSize = BUF_SIZE; fgets(readBuf, rSize, stdin); cm = getMoveFromInput(sock, readBuf); if (cm.msg == 1){ //it's a message! send it right away! cmQueue[cmQueueLength] = cm; cmQueueLength++; addReadyForSend = 1; } else{//it's a turn. if (myTurn != 1){// not my turn! printf(MOVE_REJECTED); } else{ cmQueue[cmQueueLength] = cm; cmQueueLength++; addReadyForSend = 1; } } } if (FD_ISSET(sock, &fdSetRead)){ char rBuf[BUF_SIZE]; int rSize = BUF_SIZE; receive_all(sock, rBuf, &rSize, 0); } } printWinner(game); return 0; }
int main(int argc, char const *argv[]) { /*printf("Client Started\n");*/ char port[20]; char address[50]; assert(argc >= 1 && argc <= 3); if (argc < 2) { strcpy(address,"localhost"); }else{ strcpy(address,argv[1]); } if (argc < 3) { strcpy(port,"6325"); } else { strcpy(port,argv[2]); } /*printf("trying to get socket\n");*/ // Get socket int sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) { printf( "Error opening the socket: %s\n", strerror( errno ) ); return errno; } /*printf("Succesfully got a socket number: %d\n", sock);*/ // Connect to server sock = connectToServer(sock, address, port); char buf[msgSize]; struct move m; // Get initial data struct gameData game = receiveDataFromServer(sock); if (game.isMisere == 1) { printf("This is a Misere game\n"); }else{ printf("This is a Regular game\n"); } printGameState(game); while(game.win == -1){ m = getMoveFromInput(sock); sprintf(buf, "%d$%d", m.heap,m.amount); if(sendAll(sock, buf, &msgSize) == -1){ close(sock); exit(0); } game = receiveDataFromServer(sock); // Check if move was valid printValid(game); // keep on playing printGameState(game); } printWinner(game); return 0; }