/** (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 AI1x = 8;
int AI1y = 5;
int AI1last = 8;
int AI2x = 12;
int AI2y = 5;
int AI2last = 12;
void changeColor (int color){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
void cls () { system("cls"); }
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;
}
}
bool move (int yC, int xC){
if (mapMatrix[(y+yC)][(x+xC)] == 1){
return false;
}else if (mapMatrix[(y+yC)][(x+xC)] == 2){
score++;
}
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;
while(!moveAccepted){
AImove = rand()%4;
cout <<"rand"<<AImove;
if (AImove == 0 ){
xN = -1;
yN = 0;
cout <<"zero";
}else if (AImove == 1) {
xN = 1;
yN = 0;
cout <<"jeden";
}else if (AImove == 2) {
xN = 0;
yN = -1;
cout <<"dwa";
}else if (AImove == 3) {
//}else {
xN = 0;
yN = 1;
cout <<"trzy";
}
cout << AI1y<<"+"<<yN<<"="<<(AI1y+yN)<<endl;
cout << ">>"<<mapMatrix[(AI1y+yN)][(AI1x+xN)]<<"<<";
system("pause");
if (mapMatrix[(AI1y+yN)][(AI1x+xN)] != 1 && mapMatrix[(AI1y+yN)][(AI1x+xN)] != 40 && mapMatrix[(AI1y+yN)][(AI1x+xN)] != 42){
moveAccepted=true;
}
}
cout <<"porandzie";
if (mapMatrix[(AI1y+yN)][(AI1x+xN)] == 3){
}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 gameDestroy(void) {
destroyGame=true;
}
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