Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include <pthread.h>
- #include <unistd.h>
- #include "ui.h"
- int soldiers=0;
- int gold=100;
- int minior=0;
- int zombies=1;
- int health=100;
- pthread_mutex_t mutex;
- void *thread_solder(void *arg){
- pthread_mutex_lock(&mutex);
- gold-=10;
- print_gold(gold);
- soldiers++;
- print_soldiers(soldiers);
- pthread_mutex_unlock(&mutex);
- }
- void *thread_solders(void *arg){
- pthread_mutex_lock(&mutex);
- gold-=100;
- print_gold(gold);
- soldiers+=10;
- print_soldiers(soldiers);
- pthread_mutex_unlock(&mutex);
- }
- void *thread_miner(void *arg){
- while(1){
- pthread_mutex_lock(&mutex);
- gold+=10;
- print_gold(gold);
- pthread_mutex_unlock(&mutex);
- sleep(1);
- }
- }
- void *thread_create_miner(void *arg){
- pthread_mutex_lock(&mutex);
- gold-=100;
- print_gold(gold);
- minior++;
- pthread_mutex_unlock(&mutex);
- }
- void *thread_zombies(void *arg){
- //
- while(1){
- //pthread_mutex_lock(&mutex);
- print_health(health);
- for(int i=5;i>=0;i--){
- print_zombies(i,zombies);
- sleep(1);
- }
- if(zombies>soldiers){
- health-=zombies-soldiers;
- print_fail("Zombie attack succeded ;(!\n");
- if(health<=0){
- game_end(zombies);
- }
- }
- else{
- print_succ("Zombie attack deflected! :)\n");
- }
- zombies*=2;
- //pthread_mutex_unlock(&mutex);
- }
- }
- int main() {
- pthread_t ptzombie;
- pthread_t ptsoldier;
- pthread_t ptminer;
- pthread_mutex_init(&mutex, NULL);
- init();
- print_gold(gold);
- print_soldiers(soldiers);
- pthread_create(&ptzombie, NULL, thread_zombies, NULL);
- //5 pthread_create
- //pthread_join(ptzombie,NULL);
- while(1) {
- int ch = get_input();
- switch(ch) {
- case 's':
- if(gold>=10){
- pthread_create(&ptsoldier, NULL, thread_solder, NULL);
- pthread_join(ptsoldier, NULL);
- print_msg("Soldier created!");
- }
- else{
- print_fail("Not enough gold!");
- }
- break;
- case 'm':
- if(gold>=100){
- pthread_create(&ptminer, NULL, thread_create_miner, NULL);
- pthread_join(ptminer, NULL);
- print_msg("Miner created!");
- for(int i=0;i<minior;i++){
- pthread_create(&ptminer, NULL, thread_miner, NULL);
- //pthread_join(ptminer,NULL);
- }
- }
- else{
- print_fail("Not enough gold!");
- }
- break;
- case 'x':
- if(gold>=100){
- pthread_create(&ptsoldier, NULL, thread_solders, NULL);
- pthread_join(ptsoldier, NULL);
- print_msg("10 x soldiers created!");
- }
- else{
- print_fail("Not enough gold!");
- }
- break;
- }
- }
- pthread_mutex_destroy(&mutex);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement