Creative Commons License Foxbond's Repo

/** (c) 2012 Michał (Foxbond) Chraniuk */
#include "stdafx.h"
#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 gameSpeed=300;


int AImove;
int AImoveTry=0;

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

int  AI2x = 12;
int  AI2y = 5;
int  AI2lastx = 12;
int  AI2lasty = 5;
int  AI2moveLastDirection=0;
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";
	 short lol =0;
			  while (lol != 27){
			  		lol = _getch();
		      }
	 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 if (mapMatrix[(y+yC)][(x+xC)] != 0){
	   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 (AI1moveLastDirection == 9 || AI1y == 5){
  	      AImove = rand()%4;
       }else {
   		  AImove = AI1moveLastDirection;
       }
       
       
	   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){
			 	AI1moveLastDirection=AImove;
			 }		 
   		     
		 }
		 			   
	   	 
 	   }else {
  		 AI1moveLastDirection=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;
     	}
  	 }
	 
	 //drugi (ruszy gdy 1 ucieknie)
	 moveAccepted = false;
	 xN = 0;
 	 yN = 0;
 	 AImoveTry=0;
	 
	 while(!moveAccepted && AI1escaped){
  	   if (AI2moveLastDirection == 9 || AI2y == 5){
  	      AImove = rand()%4;
       }else {
   		  AImove = AI2moveLastDirection;
       }
       
       
	   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[(AI2y+yN)][(AI2x+xN)] != 1 && mapMatrix[(AI2y+yN)][(AI2x+xN)] != 40 && mapMatrix[(AI2y+yN)][(AI2x+xN)] != 42){
	   	 moveAccepted=true;
	   	 if ((AI2y+yN) != 5 && (AI2y+yN) != 4 ){
		     if ((rand() % 10) != 1){
			 	AI2moveLastDirection=AImove;
			 }		 
   		     
		 }
		 			   
	   	 
 	   }else {
  		 AI2moveLastDirection=9;
	   }
	    
	    
       if ((AI2y+yN) == AI2lasty && (AI2x+xN) == AI2lastx && AImoveTry < 1000){
	   	  moveAccepted=false;
	   	  AImoveTry++;
	   }
	   
	   if ((AI2y+yN) == 5 && (AI2x+xN) == 10 && AI2escaped){
		  moveAccepted=false;
	   }
 	  
	 }
	 
	 if ((AI2y+yN) == 3 && (AI2x+xN) == 10){ AI2escaped = true; }
	 
	 if (mapMatrix[(AI2y+yN)][(AI2x+xN)] == 3){
  	 	 playerKilled();
	 }else {
         if (mapMatrix[AI2y][AI2x] == 40){
		   mapMatrix[AI2y][AI2x] = 0;
        }else if (mapMatrix[AI2y][AI2x] == 42){
           mapMatrix[AI2y][AI2x] = 2;
        }
        
        AI2y=AI2y+yN;
	 	AI2x=AI2x+xN;
	 	
        if (mapMatrix[AI2y][AI2x] == 2){
	 	    mapMatrix[AI2y][AI2x] = 42;
     	}else if (mapMatrix[AI2y][AI2x] == 0){
	 		mapMatrix[AI2y][AI2x] = 40;
     	}
  	 }
}

void* gameCore(void *just) {
	srand((int)time(NULL));
    while (true) {
		if (destroyGame){ pthread_exit(just); }
        Sleep(gameSpeed);
        cls();
 		  drawField();
 		  cout << "Aktualny wynik: "<<score<<endl;
 		  
 		  
 		  if (ch == 'w' || ch == 72){
		  	 move(-1,0);
 		  }else if (ch == 's' || ch == 80){
		  	 move(1,0);
		  }else if (ch == 'a' || ch == 75){
		  	 move(0,-1);
		  }else if (ch == 'd' || ch == 77){
		  	 move(0,1);
		  }
 		  
 		  gameAI();
 		  
 		  if (score==109){
			  cls();
			  cout << "Zebrales wszystkie kupy!\n";
			  cout << "Nacisnij esc, aby wyjsc";
			  short lol =0;
			  while (lol != 27){
			  		lol = _getch();
		      }
			  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((LPCWSTR)"SHIT-PAC");
	cout << "SHIT-PAC\n(c)2013 http://foxbond.cba.pl\nPoruszasz sie wsad lub strzalkami\nW czasie gry nacisnij esc, aby zamknac\n";
	system("pause");
	cls();
	
	cout << "Podaj szybkosc gry (w milisekundach) (proponowana wartosc 200)\n";
	cin >> gameSpeed;
	cls();
	
}

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

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

> Back