Пример #1
0
int main()
{
	//initialize random seed
	srand((unsigned)time(NULL));

	bool runGame = true;

	while(runGame)
	{
		if(WantsToPlay())
		{
			hPtr deck = (hPtr)malloc(sizeof(_h));
			hPtr player1Hand = (hPtr)malloc(sizeof(_h));
			hPtr player2Hand = (hPtr)malloc(sizeof(_h));
			hPtr player3Hand = (hPtr)malloc(sizeof(_h));
			hPtr player4Hand = (hPtr)malloc(sizeof(_h));
			if(Initialize(deck, player1Hand, player2Hand, player3Hand, player4Hand))
			{
				while(!GameOver(deck, player1Hand, player2Hand, player3Hand, player4Hand))
				{
					PlayRound();
				}
				CalculateWinner();

				if(!FreeMemory())
				{
					printf("ERROR: Memory could not be freed.\n");
					break;
				}
			}
			else
			{
				printf("ERROR: Could not initialize game.\n");
				break;
			}
		}
		else
		{
			runGame = false;
			printf("See you next time!\n");
		}
	}

	return 0;
}
Пример #2
0
void CGame::Run()
{
   int i, j;

   // the game must be played with 3 players
   if (m_iNumPlayer != 3) {
      TerminateOnError("CGame::Run(): m_iNumPlayer != 3!");
   }

   for (i = 0; i < 3; i++) {
      char player_names[3][20];
      strncpy(player_names[RL(0, i)], m_rgszPlayerNames[0], 20);
      strncpy(player_names[RL(1, i)], m_rgszPlayerNames[1], 20);
      strncpy(player_names[RL(2, i)], m_rgszPlayerNames[2], 20);
      player_names[0][19] = '\0';
      player_names[1][19] = '\0';
      player_names[2][19] = '\0';
      m_rgpPlayers[i]->SetPlayerNames(player_names);
   }

   // The main game loop
   while (1) {
      int iWinner = PlayRound(), scores[3];
      bool fLordWon = (iWinner == m_iCurLord);
      CCard rgRemainCards[3][20];
      for (i = 0; i < 3; i++) {
         for (j = 0; j < 20; j++) {
            rgRemainCards[i][j] = m_rgpPlayers[i]->Hand()[j];
         }
      }

      scores[0] = m_iPoints * ((fLordWon ^ (m_iCurLord != 0)) ? 1 : -1);
      scores[1] = m_iPoints * ((fLordWon ^ (m_iCurLord != 1)) ? 1 : -1);
      scores[2] = m_iPoints * ((fLordWon ^ (m_iCurLord != 2)) ? 1 : -1);
      scores[m_iCurLord] *= 2;

      if (atoi(cfg.Get("GAME", "ShowCards", "1"))) {
         m_rgpPlayers[0]->ShowAllCards(RL(iWinner, 0), rgRemainCards);
         m_rgpPlayers[1]->ShowAllCards(RL(iWinner, 1), rgRemainCards);
         m_rgpPlayers[2]->ShowAllCards(RL(iWinner, 2), rgRemainCards);
      }

      m_rgiPlayerScore[0] += scores[0];
      m_rgiPlayerScore[1] += scores[1];
      m_rgiPlayerScore[2] += scores[2];

      if (m_fSingleGame) {
         cfg.Set("GAME", "Score", va("%d", m_rgiPlayerScore[0]));
         cfg.Set("GAME", "BotScore1", va("%d", m_rgiPlayerScore[1]));
         cfg.Set("GAME", "BotScore2", va("%d", m_rgiPlayerScore[2]));
      }

      // send score information to players
      m_rgpPlayers[0]->ScoreBoard(m_rgszPlayerNames, scores, m_rgiPlayerScore, scores[0] > 0);
      m_rgpPlayers[1]->ScoreBoard(m_rgszPlayerNames, scores, m_rgiPlayerScore, scores[1] > 0);
      m_rgpPlayers[2]->ScoreBoard(m_rgszPlayerNames, scores, m_rgiPlayerScore, scores[2] > 0);

      // Wait for all players to get ready for next round
      time_t t;
      time(&t);
      while (1) {
         UTIL_Delay(10);
         if (m_rgpPlayers[0]->IsReady() && m_rgpPlayers[1]->IsReady() && m_rgpPlayers[2]->IsReady()) {
            break;
         }
         if (time(NULL) - t > 30 && !m_fSingleGame) {
            g_UI.PopMessage(msg("someone_quit"));
            return; // someone delayed for too long; close the server
         }
      }
   }
}