Creative Commons License Foxbond's Repo

/** (c) 2012 Michał (Foxbond) Chraniuk */
#include <pthread.h>
#include <iostream>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define BLUE 1
#define GREEN 2
#define RED 4
#define PURPLE 5
#define GOLD 6
#define LIGHTBLUE 9
#define WHITE 15

using namespace std;


bool destroyGame = false;
int ch=0;
int mapMatrix[11][21] = {
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,2,2,2,1},
		{1,2,1,1,2,1,2,1,1,1,1,1,1,1,2,1,2,1,1,2,1},
		{1,2,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,2,1},
		{1,2,1,2,1,1,2,1,1,1,2,1,1,1,2,1,1,2,1,2,1},
		{1,2,2,2,2,2,2,1,42,2,2,2,42,1,2,2,2,2,2,2,1},
		{1,2,1,2,1,1,2,1,1,1,1,1,1,1,2,1,1,2,1,2,1},
		{1,2,1,2,2,2,2,2,2,2,3,2,2,2,2,2,2,2,1,2,1},
		{1,2,1,1,2,1,2,1,1,1,1,1,1,1,2,1,2,1,1,2,1},
		{1,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,2,2,2,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
		};
	
int i,ii;
int score=0;
int x=10;
int y=7;


int AImove;
int AImoveTry=0;
int AImoveLastDirection=0;

int  AI1x = 8;
int  AI1y = 5;
int  AI1lastx = 8;
int  AI1lasty = 5;
bool AI1escaped=false;

int  AI2x = 12;
int  AI2y = 5;
int  AI2lastx = 12;
int  AI2lasty = 5;
bool AI2escaped=false;


void changeColor (int color){
	 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}

void cls () { system("cls"); }

void gameDestroy(void) {
	 destroyGame=true;
	 
}

void drawField (void){
	for (i=0;i<11;i++){
		for (ii=0;ii<21;ii++){
			if (mapMatrix[i][ii] == 0){
				cout << " "; 
			} else if (mapMatrix[i][ii] == 1){
   			    changeColor(PURPLE);
			  	cout << "#";
			  	changeColor(WHITE);
   			} else if (mapMatrix[i][ii] == 2){
			  	changeColor(GOLD);
			  	cout << ".";
			  	changeColor(WHITE);
			  	//•
   			} else if (mapMatrix[i][ii] == 3){
			  	changeColor(GREEN);
			  	cout << "e";
			  	changeColor(WHITE);
			  	//
   			} else if (mapMatrix[i][ii] == 40 || mapMatrix[i][ii] == 42){
			  	changeColor(RED);
			  	cout << "O";
			  	changeColor(WHITE);
			  	//
   			}  
		}
		cout << endl;	  
   }
}

void playerKilled(void){
	 cls();
	 cout << "Zostales zzarty przez krwiozercze oko\nUdalo ci sie jednak zebrac "<<score<<" kup!\nNacisnij esc, aby wyjsc\n";
	 gameDestroy();
}

bool move (int yC, int xC){
	 
	 if (mapMatrix[(y+yC)][(x+xC)] == 1){
  	 	return false;
	 }else if (mapMatrix[(y+yC)][(x+xC)] == 2){
	 	score++;
     }else {
	   playerKilled();	   
     }
	 
	 mapMatrix[y][x] = 0;
	 y=y+yC;
	 x=x+xC;
	 mapMatrix[y][x] = 3;
	 return true;
	 
}

void gameAI (void) {
	 bool moveAccepted = false;
	 int xN = 0;
 	 int yN = 0;
 	 AImoveTry=0;
	 
	 while(!moveAccepted){
  	   if (AImoveLastDirection == 9 || AI1y == 5){
  	      AImove = rand()%4;
       }else {
   		  AImove = AImoveLastDirection;
       }
       
       
	   if (AImove == 0 ){
  	 	xN = -1;
  	 	yN =  0;
	   }else if (AImove == 1) {
	 	xN =  1;
  	 	yN =  0;
       }else if (AImove == 2) {
	 	xN =  0;
  	 	yN = -1;
       }else if (AImove == 3) {
	   //}else {
	 	xN =  0;
  	 	yN =  1;
       }
       
       
       if (mapMatrix[(AI1y+yN)][(AI1x+xN)] != 1 && mapMatrix[(AI1y+yN)][(AI1x+xN)] != 40 && mapMatrix[(AI1y+yN)][(AI1x+xN)] != 42){
	   	 moveAccepted=true;
	   	 if ((AI1y+yN) != 5 && (AI1y+yN) != 4 ){
		     if ((rand() % 10) != 1){
			 	AImoveLastDirection=AImove;
			 }		 
   		     
		 }
		 			   
	   	 
 	   }else {
  		 AImoveLastDirection=9;
	   }
	    
	    
       if ((AI1y+yN) == AI1lasty && (AI1x+xN) == AI1lastx && AImoveTry < 10000000){
	   	  moveAccepted=false;
	   	  AImoveTry++;
	   }
	   
	   if ((AI1y+yN) == 5 && (AI1x+xN) == 10 && AI1escaped){
		  moveAccepted=false;
	   }
 	  
	 }
	 
	 if ((AI1y+yN) == 3 && (AI1x+xN) == 10){ AI1escaped = true; }
	 
	 if (mapMatrix[(AI1y+yN)][(AI1x+xN)] == 3){
  	 	 playerKilled();
	 }else {
         if (mapMatrix[AI1y][AI1x] == 40){
		   mapMatrix[AI1y][AI1x] = 0;
        }else if (mapMatrix[AI1y][AI1x] == 42){
           mapMatrix[AI1y][AI1x] = 2;
        }
        
        AI1y=AI1y+yN;
	 	AI1x=AI1x+xN;
	 	
        if (mapMatrix[AI1y][AI1x] == 2){
	 	    mapMatrix[AI1y][AI1x] = 42;
     	}else if (mapMatrix[AI1y][AI1x] == 0){
	 		mapMatrix[AI1y][AI1x] = 40;
     	}
  	 }
	 
	 
}

void* gameCore(void *just) {
	srand(time(NULL));
    while (true) {
		if (destroyGame){ pthread_exit(just); }
        _sleep(300);
        cls();
 		  drawField();
 		  cout << "Aktualny wynik: "<<score<<endl;
 		  
 		  
 		  if (ch == 'w'){
		  	 move(-1,0);
 		  }else if (ch == 's'){
		  	 move(1,0);
		  }else if (ch == 'a'){
		  	 move(0,-1);
		  }else if (ch == 'd'){
		  	 move(0,1);
		  }
 		  
 		  gameAI();
 		  
 		  if (score==109){
			  cls();
			  cout << "Zebrales wszystkie kupy!\n";
			  cout << "Nacisnij esc, aby wyjsc";
			  
			  gameDestroy();
		  }
        
        
        
    }
    return NULL;
}

void* gameKeyboard(void* just) {
    while (true) {
        ch = getch();
        if (ch == 27) { gameDestroy(); }
        if (destroyGame){ pthread_exit(just); }
    }
    return NULL;
}

void gameInit (void){
	 
	SetConsoleTitle("SHIT-PAC");
	cout << "SHIT-PAC\nPoruszasz sie wsad\nW czasie gry nacisnij esc, aby zamknac\n\n";
	system("pause");
	cls();
	 
}

int main(int argc, char* argv[]) {
    pthread_t thread1, thread2;
    int tret1, tret2, just;
    tret2 = pthread_create(&thread2, NULL, gameCore,
            (void*) &just);
    tret1 = pthread_create(&thread1, NULL, gameKeyboard,
            (void*) &just);

	gameInit();
	
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    
    
    return 0;
}

> Back