Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ui.h"
- #include <stdio.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <stdlib.h>
- pthread_mutex_t mutex;
- pthread_cond_t cond;
- int gold = 100;
- int zombies = 1;
- int soldiers = 0;
- int health = 100;
- void *gold_miner()
- {
- while(1)
- {
- pthread_mutex_lock(&mutex);
- gold = gold + 10;
- pthread_cond_signal(&cond);
- print_gold(gold);
- pthread_mutex_unlock(&mutex);
- sleep(1);
- }
- return NULL;
- }
- void *zombies_attack()
- {
- while(1)
- {
- pthread_mutex_lock(&mutex);
- while(gold < 100)
- {
- pthread_cond_wait(&cond, &mutex);
- }
- pthread_mutex_unlock(&mutex);
- for(int i = 5; i > 0; i--)
- {
- print_zombies(i, zombies);
- sleep(1);
- }
- print_zombies(5, zombies);
- pthread_mutex_lock(&mutex);
- if(zombies > soldiers)
- {
- health = health - (zombies - soldiers);
- print_health(health);
- print_fail("Zombie attack succeded ;(!");
- if(health <= 0)
- {
- game_end(zombies);
- }
- }else
- {
- print_health(health);
- print_succ("Zombie attack deflected! :)");
- }
- zombies = zombies * 2;
- pthread_mutex_unlock(&mutex);
- }
- return NULL;
- }
- int main()
- {
- init();
- print_gold(100);
- print_soldiers(0);
- print_zombies(5, 13);
- print_health(100);
- pthread_mutex_init(&mutex, NULL);
- pthread_cond_init(&cond,NULL);
- pthread_t pthzombie;
- pthread_create(&pthzombie, NULL, zombies_attack, NULL);
- while(1)
- {
- int ch = get_input();
- switch(ch)
- {
- case 'q':
- game_end(zombies);
- break;
- case 'm':
- pthread_mutex_lock(&mutex);
- if(gold >= 100)
- {
- gold = gold - 100;
- print_gold(gold);
- pthread_t thread;
- pthread_create(&thread, NULL, gold_miner, NULL);
- print_msg("Miner created!");
- }else{
- print_fail("Not enough gold!");
- }
- pthread_mutex_unlock(&mutex);
- break;
- case 's':
- pthread_mutex_lock(&mutex);
- if(gold >= 10)
- {
- gold = gold - 10;
- print_gold(gold);
- soldiers++;
- print_soldiers(soldiers);
- print_msg("Soldier created!");
- }else{
- print_fail("Not enough gold!");
- }
- pthread_mutex_unlock(&mutex);
- break;
- case 'x':
- pthread_mutex_lock(&mutex);
- if(gold >= 100)
- {
- gold = gold - 100;
- print_gold(gold);
- soldiers = soldiers + 10;
- print_soldiers(soldiers);
- print_msg("10 x soldiers created!");
- }else{
- print_fail("Not enough gold!");
- }
- pthread_mutex_unlock(&mutex);
- break;
- }
- }
- }
Add Comment
Please, Sign In to add comment