Created
April 27, 2012 05:39
-
-
Save marcosbrasil/2506218 to your computer and use it in GitHub Desktop.
Jogo campo minado - Trabalho UNA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <ctime> | |
#include <cstdlib> | |
#include <string> | |
#include <windows.h> | |
#define ARRAY_SIZE 6 | |
using namespace std; | |
//Declarando global screen vars | |
char borda[11]; | |
int screenData[25][6];//id,posicao_linha,posicao_coluna,tipo,status,num_minas | |
void resetColor(){ | |
system("color 70"); | |
} | |
void changeColor(int color){ | |
HANDLE hConsole; | |
hConsole = GetStdHandle(STD_OUTPUT_HANDLE); | |
SetConsoleTextAttribute(hConsole, color); | |
} | |
void cls(){ | |
system("cls"); | |
} | |
void debugData(){ | |
for(int i=0;i<25;i++){ | |
for(int j=0;j<6;j++){ | |
cout << screenData[i][j]<<"-"; | |
} | |
cout<<endl; | |
} | |
} | |
void countMines(){ | |
int array[7][7], count=0, total; | |
//cria o array | |
for(int i=0;i<7;i++){ | |
for(int j=0;j<7;j++){ | |
array[i][j] = 0; | |
} | |
} | |
//preenche o array com as bombas | |
for(int i=1;i<6;i++){ | |
for(int j=1;j<6;j++){ | |
array[i][j] = screenData[count++][3]; | |
} | |
} | |
//contando bombas ao redor | |
count = 0; | |
for(int i=1;i<6;i++){ | |
for(int j=1;j<6;j++){ | |
total = 0; | |
for(int lin=i-1;lin<i+2;lin++){ | |
for(int col=j-1;col<j+2;col++){ | |
total += array[lin][col]; | |
} | |
} | |
screenData[count++][5] = total; | |
} | |
} | |
} | |
void inicializeData(){ | |
int id=65,k=0,random_integer; | |
srand((unsigned)time(0)); | |
for(int i=0;i<25;i++){ | |
screenData[i][0] =id+i; | |
screenData[i][3] =(rand()%2); | |
screenData[i][4] =0; | |
} | |
for(int i=0;i<5;i++){ | |
for(int j=0;j<5;j++){ | |
screenData[k][1] =i; | |
screenData[k++][2] =j; | |
} | |
} | |
//conta e armazena o numero de minas | |
countMines(); | |
} | |
int updateData(int id){ | |
screenData[id-65][4] = 1; | |
} | |
void makeTop(){ | |
cout<<borda[5]<<borda[9]<<borda[9]<<borda[9]; | |
for(int i = 0;i<ARRAY_SIZE-2;i++){ | |
cout<<borda[7]<<borda[9]<<borda[9]<<borda[9]; | |
} | |
cout<<borda[2]<<endl; | |
} | |
void makeInner(){ | |
cout<<borda[8]<<borda[9]<<borda[9]<<borda[9]; | |
for(int i = 0;i<ARRAY_SIZE-2;i++){ | |
cout<<borda[10]<<borda[9]<<borda[9]<<borda[9]; | |
} | |
cout<<borda[0]<<endl; | |
} | |
void makeBottom(){ | |
cout<<borda[4]<<borda[9]<<borda[9]<<borda[9]; | |
for(int i = 0;i<ARRAY_SIZE-2;i++){ | |
cout<<borda[6]<<borda[9]<<borda[9]<<borda[9]; | |
} | |
cout<<borda[3]<<endl; | |
} | |
void loadScreen(){ | |
cls();//limpa a tela | |
borda[0] = (char)185; | |
borda[1] = (char)186; | |
borda[2] = (char)187; | |
borda[3] = (char)188; | |
borda[4] = (char)200; | |
borda[5] = (char)201; | |
borda[6] = (char)202; | |
borda[7] = (char)203; | |
borda[8] = (char)204; | |
borda[9] = (char)205;//horizontal reto | |
borda[10] = (char)206; | |
makeTop();//constroi o tempo do tabuleiro | |
int ch=0,status,tipo,mines; | |
for(int i=0;i<ARRAY_SIZE-1;i++){ | |
cout<<borda[1]; | |
for(int j = 0;j<ARRAY_SIZE-1;j++){ | |
tipo = screenData[ch][3]; | |
status = screenData[ch][4]; | |
mines = screenData[ch][5]; | |
if(status == 0){ | |
cout<<" "<<(char)(65+ch)<<" "<<borda[1]; | |
}else{ | |
if(tipo == 0){ | |
changeColor(114); | |
//_textcolor(2); | |
cout<<" "<<mines<<" "; | |
changeColor(112); | |
cout<<borda[1]; | |
}else{ | |
if(tipo == 1){ | |
changeColor(124); | |
cout<<" "<<"#"<<" "; | |
changeColor(112); | |
cout<<borda[1]; | |
} | |
} | |
} | |
ch++; | |
} | |
cout<<endl; | |
if(i!=4) | |
makeInner();//prepara para a proxima linha | |
} | |
makeBottom(); //constroi o rodape do tabuleiro | |
} | |
int main() | |
{ | |
char letra; | |
int code = 1; | |
inicializeData();//Popula a tabela de dados do jogo | |
resetColor();//seta cores no padrao do jogo | |
while(code != 0){ | |
loadScreen(); | |
cout<<"Escolha uma letra: "; | |
cin>>letra; | |
if((int)letra<123 && (int)letra>=97){ | |
letra=(char)letra-32; | |
} | |
cout<<endl; | |
code = (int)letra; | |
updateData(code); | |
} | |
system("PAUSE"); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment