Advertisement
Guest User

Untitled

a guest
May 5th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.24 KB | None | 0 0
  1. /**
  2. Implementação controlada pelo GIt
  3. Alunos:
  4. Fagner de oliveira Bernardo 14.2.4155
  5. Natália Monferari Sol 14.2.4335
  6. */
  7.  
  8. #ifdef __APPLE__
  9. #include <GLUT/glut.h>
  10. #else
  11. #include <GL/glut.h>
  12. #endif
  13.  
  14. //#include <stdlib.h>
  15. #include <iostream>
  16. #include <cstdlib>
  17. #include <ctime>
  18. #include<unistd.h>
  19. //#include <windows.h>
  20.  
  21. using namespace std;
  22.  
  23. typedef struct ponto{
  24. GLfloat x;
  25. GLfloat y;
  26. }TPonto;
  27.  
  28. #define INDICE 25
  29. #define QUAD2X2 4
  30. #define QUAD3X3 9
  31. #define NIVEL_INICIAL 1
  32. #define NUM_EMB_BOMB 10
  33. #define EMBARCACOES 5
  34. #define TAMANHO 40
  35.  
  36. /*======================DECLARAÇÃO DE VARIÁVEIS GLOBAIS======================*/
  37.  
  38. /*Especifica aposicao da janela*/
  39. GLfloat posX = 700.0, posY = 150.0, rate = 0.05;
  40.  
  41. /*Especifica as dimensoes da janela*/
  42. GLfloat windowWidth = 520, windowHeight = 440;
  43.  
  44. //turn: guarda as posicoes dos quadrados que vao mudar de cor
  45. //rodada guarda a quantidade de quadrado a memorizar
  46. //onGame é deve ser falsa quando o player perder
  47. //guess: guarda os cliques do jogador na posicao dos objetos na tela
  48. //verificacao: usada para chamar a funcao que avalia o resultado do jogo
  49. //jogoAtual: guarda qual jogo está sendo jogado no momento
  50. //indice: guarda a quantidade de cliques de mouse dados nas posicoes dos quadrados
  51. //tempo: determina o tempo base da duração do quadrado preto
  52.  
  53. TPonto a, b, c, d;
  54.  
  55. int turn[INDICE], guess[INDICE], nivel = NIVEL_INICIAL, jogoAtual = 0, indice = 0,
  56. verificacao = 0, tempo = 700, flagBlackColor = 0, posicoesEmb[NUM_EMB_BOMB], lin[NUM_EMB_BOMB], col[NUM_EMB_BOMB],
  57. tabuleiro[TAMANHO][TAMANHO] = {}, linha = 0, coluna = 0;
  58.  
  59. /*===========================INICIO DAS DEFINIÇÕES===========================*/
  60.  
  61. void init(void);
  62.  
  63. /*Gerador dos numeros aleatórios*/
  64. void geraNum2x2();
  65.  
  66. /*Gerador dos numeros aleatórios*/
  67. void geraNum3x3();
  68.  
  69. bool confereBomba(int, int);
  70.  
  71. /*Gerador dos numeros aleatórios*/
  72. void geraNumBatalha();
  73.  
  74. void quad2x2();
  75.  
  76. void jogoMemoriaFacil();
  77.  
  78. void jogoMemoriaDificil();
  79.  
  80. void jogoBatalhaNaval();
  81.  
  82. void quad3x3();
  83.  
  84. void desenhaCubos();
  85.  
  86. void display(void);
  87.  
  88. /*Função calback chamada para gerenciar eventos do mouse*/
  89. void GerenciaMouse(int button, int state, int x, int y);
  90.  
  91. void verificaAcerto();
  92.  
  93. void posicionaEmbarcacoes();
  94.  
  95. void GoMenu(int value);
  96.  
  97. /*===========================FIM DAS DEFINIÇÕES===========================*/
  98.  
  99.  
  100. int main (int argc, char** argv) {
  101.  
  102. glutInit(&argc, argv);
  103. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  104. glutInitWindowSize(windowWidth, windowHeight);
  105. glutInitWindowPosition(posX, posY);
  106. glutCreateWindow("Menu");
  107.  
  108. int sub1 = glutCreateMenu(GoMenu);
  109.  
  110. glutAddMenuEntry("Fácil",1);
  111. glutAddMenuEntry("Difícil",2);
  112.  
  113. glutCreateMenu(GoMenu);
  114. glutAddSubMenu("Memória", sub1);
  115.  
  116. glutAddMenuEntry("Batalha Naval",3);
  117. glutAddMenuEntry("Sair",4);
  118. glutAttachMenu(GLUT_RIGHT_BUTTON);
  119.  
  120. glutMouseFunc(GerenciaMouse);
  121.  
  122. init();
  123. glutDisplayFunc(display);
  124. glutMainLoop();
  125. return 0;
  126. }
  127.  
  128.  
  129.  
  130. /*===========================INICIO DAS ESPECIFICAÇÕES===========================*/
  131.  
  132. void GoMenu(int value) {
  133. switch(value){
  134. case 1:
  135. jogoAtual = 1;
  136. nivel = NIVEL_INICIAL;
  137. indice = 0;
  138. geraNum2x2();
  139.  
  140. //Zera o vetor de chutes
  141. for (int i = 0; i < INDICE; i++){
  142. guess[i] = -1;
  143. }
  144.  
  145. jogoMemoriaFacil();
  146. break;
  147. case 2:
  148. jogoAtual = 2;
  149. nivel = NIVEL_INICIAL;
  150. indice = 0;
  151. geraNum3x3();
  152.  
  153. //Zera o vetor de chutes
  154. for (int i = 0; i < INDICE; i++){
  155. guess[i] = -1;
  156. }
  157.  
  158. jogoMemoriaDificil();
  159. break;
  160. case 3:
  161. jogoAtual = 3;
  162. nivel = NIVEL_INICIAL;
  163. geraNumBatalha();
  164. jogoBatalhaNaval();
  165. break;
  166. case 4:
  167. exit(0);
  168. break;
  169. }
  170. glutPostRedisplay();
  171. }
  172.  
  173. void init(void) {
  174. glClearColor(1.0, 1.0,1.0,0.0);
  175. glMatrixMode(GL_PROJECTION);
  176. glLoadIdentity();
  177.  
  178. a.x = -1.0;
  179. a.y = 0.8;
  180.  
  181. b.x = -0.8;
  182. b.y = 0.8;
  183.  
  184. c.x = -0.8;
  185. c.y = 1.0;
  186.  
  187. d.x = -1.0;
  188. d.y = 1.0;
  189. cout << "init" << endl;
  190. /*Determina as configurações do plano cartesiano de projeção*/
  191. glOrtho(0.0, 1.0, 0.0, 1.0, 0.0 ,0.0);
  192.  
  193. }
  194.  
  195. void geraNum2x2(){
  196. cout << "Entrou geraNum()" << endl;
  197.  
  198. //Gera uma semente para o tempo
  199. srand((unsigned) time (NULL));
  200.  
  201. //Gera um vetor com 25 numeros aleatorios
  202. for(int i = 0 ; i < INDICE ; i++){
  203. turn[i] = rand()%QUAD2X2 +1;
  204. cout << turn[i] << " ";
  205. }
  206. cout << endl;
  207. }
  208.  
  209. void geraNum3x3(){
  210. cout << "Entrou geraNum()" << endl;
  211.  
  212. //Gera uma semente para o tempo
  213. srand((unsigned) time (NULL));
  214.  
  215. //Gera um vetor com 25 numeros aleatorios
  216. for(int i = 0 ; i < INDICE ; i++){
  217. turn[i] = rand()%QUAD3X3 +1;
  218. cout << turn[i] << " ";
  219. }
  220. cout << endl;
  221. }
  222.  
  223. bool confereBomba(int linha, int coluna){
  224. //Confere se não tem nada na posicao
  225. if(tabuleiro[linha][coluna] == 0){
  226. tabuleiro[linha][coluna] = 2;
  227. return true;
  228. }
  229. else{
  230. //Se houver, anda uma linha até achar um lugar vazio
  231. if(linha < TAMANHO-1){
  232. linha++;
  233. confereBomba(linha, coluna);
  234. }
  235. //Anda uma coluna para a direita até achar um lugar vazio
  236. else if(coluna < TAMANHO-1){
  237. linha = 0;
  238. coluna++;
  239. confereBomba(linha, coluna);
  240. }
  241. //Caso não ache lugar vazio, retorna falso. Dentro do while será gerado outra posicao aleatoria
  242. else{
  243. return false;
  244. }
  245. }
  246. }
  247.  
  248. void geraNumBatalha(){
  249. cout << "Entrou geraNum()" << endl;
  250.  
  251. //Gera uma semente para o tempo
  252. srand((unsigned) time (NULL));
  253.  
  254. //Gera um vetor com 10 embarcaccoes aleatorias
  255. for(int i = 0 ; i < NUM_EMB_BOMB ; i++){
  256. posicoesEmb[i] = rand()%EMBARCACOES +1;
  257. cout << posicoesEmb[i] << " ";
  258. }
  259. //Gera 10 posicoes aleatorias para as bombas
  260. for(int j =0; j < NUM_EMB_BOMB; j++){
  261. linha = rand()%TAMANHO;
  262. coluna = rand()%TAMANHO;
  263. //Confere a posicao da bomba está vazia
  264. while(!confereBomba(linha, coluna)){
  265. linha = rand()%TAMANHO;
  266. coluna = rand()%TAMANHO;
  267. }
  268. cout << "Linha: "<<linha<<" Coluna: "<< coluna << endl;
  269. }
  270. cout << endl;
  271. }
  272.  
  273. void quad2x2(){
  274.  
  275. if(flagBlackColor == 1){
  276. glColor3f(0.0, 0.0, 0.0);
  277. cout << "Black color: " << flagBlackColor << " direito superior" << endl;
  278. }
  279. else{
  280. glColor3f(1.0, 1.0, 0.0);
  281. }
  282.  
  283. /*Gera um poligono direita superior*/
  284. glBegin(GL_POLYGON);
  285. glVertex3f(0.0,0.0,0.0);
  286. glVertex3f(0.5,0.0,0.0);
  287. glVertex3f(0.5,0.5,0.0);
  288. glVertex3f(0.0,0.5,0.0);
  289. glEnd();
  290.  
  291. if(flagBlackColor == 2){
  292. glColor3f(0.0, 0.0, 0.0);
  293. cout << "Black color: " << flagBlackColor << " esquerdo superior" << endl;
  294. }
  295. else{
  296. glColor3f(1.0, 0.0, 0.0);
  297. }
  298. /*Gera um poligono esquerda superior*/
  299. glBegin(GL_POLYGON);
  300. glVertex3f(0.0,0.0,0.0);
  301. glVertex3f(0.0,0.5,0.0);
  302. glVertex3f(-0.5,0.5,0.0);
  303. glVertex3f(-0.5,0.0,0.0);
  304. glEnd();
  305.  
  306. if(flagBlackColor == 3){
  307. glColor3f(0.0, 0.0, 0.0);
  308. cout << "Black color: " << flagBlackColor << " esquerdo inferior" << endl;
  309. }
  310. else{
  311. glColor3f(0.0, 0.0, 1.0);
  312.  
  313. }
  314. /*Gera um poligono esquerda inferior*/
  315. glBegin(GL_POLYGON);
  316. glVertex3f(0.0,0.0,0.0);
  317. glVertex3f(-0.5,0.0,0.0);
  318. glVertex3f(-0.5,-0.5,0.0);
  319. glVertex3f(0.0,-0.5,0.0);
  320. glEnd();
  321.  
  322. if(flagBlackColor == 4){
  323. glColor3f(0.0, 0.0, 0.0);
  324. cout << "Black color: " << flagBlackColor << " direito inferior" << endl;
  325. }
  326. else{
  327. glColor3f(0.0, 1.0, 0.0);
  328. }
  329. /*Gera um poligono direita inferior*/
  330. glBegin(GL_POLYGON);
  331. glVertex3f(0.0,0.0,0.0);
  332. glVertex3f(0.0,-0.5,0.0);
  333. glVertex3f(0.5,-0.5,0.0);
  334. glVertex3f(0.5,0.0,0.0);
  335. glEnd();
  336. glFlush();
  337. }
  338.  
  339. void quad3x3(){
  340. if(flagBlackColor == 1){
  341. cout << "Black color: " << flagBlackColor << " direito direita L1" << endl;
  342. glColor3f(0.0, 0.0, 0.0);
  343. }
  344. else{
  345. glColor3f(1.0, 1.0, 0.0);
  346. }
  347.  
  348. /*Gera um poligono direita L1*/
  349. glBegin(GL_POLYGON);
  350. glVertex3f(0.6,0.6,0.0);
  351. glVertex3f(0.2,0.6,0.0);
  352. glVertex3f(0.2,0.2,0.0);
  353. glVertex3f(0.6,0.2,0.0);
  354. glEnd();
  355.  
  356. if(flagBlackColor == 2){
  357. cout << "Black color: " << flagBlackColor << " central L1" << endl;
  358. glColor3f(0.0, 0.0, 0.0);
  359. }
  360. else{
  361. glColor3f(1.0, 0.0, 0.0);
  362. }
  363.  
  364. /*Gera um poligono central L1*/
  365. glBegin(GL_POLYGON);
  366. glVertex3f(0.2,0.6,0.0);
  367. glVertex3f(-0.2,0.6,0.0);
  368. glVertex3f(-0.2,0.2,0.0);
  369. glVertex3f(0.2,0.2,0.0);
  370. glEnd();
  371.  
  372. if(flagBlackColor == 3){
  373. cout << "Black color: " << flagBlackColor << " esquerda L1" << endl;
  374. glColor3f(0.0, 0.0, 0.0);
  375. }
  376. else{
  377. glColor3f(0.0, 0.0, 1.0);
  378. }
  379.  
  380. /*Gera um poligono esquerda L1*/
  381. glBegin(GL_POLYGON);
  382. glVertex3f(-0.2,0.6,0.0);
  383. glVertex3f(-0.6,0.6,0.0);
  384. glVertex3f(-0.6,0.2,0.0);
  385. glVertex3f(-0.2,0.2,0.0);
  386. glEnd();
  387.  
  388. if(flagBlackColor == 4){
  389. cout << "Black color: " << flagBlackColor << " direita L2" << endl;
  390. glColor3f(0.0, 0.0, 0.0);
  391. }
  392. else{
  393. glColor3f(0.5, 0.5, 0.0);
  394. }
  395.  
  396. /*Gera um poligono direita L2*/
  397. glBegin(GL_POLYGON);
  398. glVertex3f(0.6,-0.2,0.0);
  399. glVertex3f(0.2,-0.2,0.0);
  400. glVertex3f(0.2,0.2,0.0);
  401. glVertex3f(0.6,0.2,0.0);
  402. glEnd();
  403.  
  404. if(flagBlackColor == 5){
  405. cout << "Black color: " << flagBlackColor << " central L2" << endl;
  406. glColor3f(0.0, 0.0, 0.0);
  407. }
  408. else{
  409. glColor3f(0.0, 0.5, 0.5);
  410. }
  411.  
  412. /*Gera um poligono central L2*/
  413. glBegin(GL_POLYGON);
  414. glVertex3f(0.2,-0.2,0.0);
  415. glVertex3f(-0.2,-0.2,0.0);
  416. glVertex3f(-0.2,0.2,0.0);
  417. glVertex3f(0.2,0.2,0.0);
  418. glEnd();
  419.  
  420. if(flagBlackColor == 6){
  421. cout << "Black color: " << flagBlackColor << " esquerda L2" << endl;
  422. glColor3f(0.0, 0.0, 0.0);
  423. }
  424. else{
  425. glColor3f(0.5, 0.0, 0.5);
  426. }
  427.  
  428. /*Gera um poligono esquerda L2*/
  429. glBegin(GL_POLYGON);
  430. glVertex3f(-0.2,-0.2,0.0);
  431. glVertex3f(-0.6,-0.2,0.0);
  432. glVertex3f(-0.6,0.2,0.0);
  433. glVertex3f(-0.2,0.2,0.0);
  434. glEnd();
  435.  
  436. if(flagBlackColor == 7){
  437. cout << "Black color: " << flagBlackColor << " direita L3" << endl;
  438. glColor3f(0.0, 0.0, 0.0);
  439. }
  440. else{
  441. glColor3f(1.0, 0.5, 5.0);
  442. }
  443.  
  444. /*Gera um poligono direita L3*/
  445. glBegin(GL_POLYGON);
  446. glVertex3f(0.6,-0.6,0.0);
  447. glVertex3f(0.2,-0.6,0.0);
  448. glVertex3f(0.2,-0.2,0.0);
  449. glVertex3f(0.6,-0.2,0.0);
  450. glEnd();
  451.  
  452. if(flagBlackColor == 8){
  453. cout << "Black color: " << flagBlackColor << " central L3" << endl;
  454. glColor3f(0.0, 0.0, 0.0);
  455. }
  456. else{
  457. glColor3f(0.5, 0.5, 1.0);
  458. }
  459.  
  460. /*Gera um poligono central L3*/
  461. glBegin(GL_POLYGON);
  462. glVertex3f(0.2,-0.6,0.0);
  463. glVertex3f(-0.2,-0.6,0.0);
  464. glVertex3f(-0.2,-0.2,0.0);
  465. glVertex3f(0.2,-0.2,0.0);
  466. glEnd();
  467.  
  468. if(flagBlackColor == 9){
  469. cout << "Black color: " << flagBlackColor << " esquerda L3" << endl;
  470. glColor3f(0.0, 0.0, 0.0);
  471. }
  472. else{
  473. glColor3f(0.5, 1.0, 0.5);
  474. }
  475.  
  476. /*Gera um poligono esquerda L3*/
  477. glBegin(GL_POLYGON);
  478. glVertex3f(-0.2,-0.6,0.0);
  479. glVertex3f(-0.6,-0.6,0.0);
  480. glVertex3f(-0.6,-0.2,0.0);
  481. glVertex3f(-0.2,-0.2,0.0);
  482. glEnd();
  483. glFlush();
  484. }
  485.  
  486. void desenhaCubos(){
  487. float passo = 0.0;
  488. cout << "desenha" << endl;
  489. for(int i = 0; i <= 40; i++){
  490. cout << "Ponto A: ("<< a.x << "," << a.y << ")" << endl;
  491. cout << "Ponto B: ("<< b.x << "," << b.y << ")" << endl;
  492. cout << "Ponto C: ("<< c.x << "," << c.y << ")" << endl;
  493. cout << "Ponto D: ("<< d.x << "," << d.y << ")" << endl;
  494. for(int j = 0; j <= 40; j++){
  495. /*Gera um poligono*/
  496. glBegin(GL_POLYGON);
  497. //Troca a cor dos quadrados
  498. if(j%2 == 0){
  499. if (i%2 == 0){
  500. glColor3f(1.0, 1.0, 0.0);
  501. }
  502. else{
  503. glColor3f(0.0, 1.0, 1.0);
  504. }
  505. }
  506. else{
  507. if (i%2 == 0){
  508. glColor3f(0.0, 1.0, 1.0);
  509.  
  510. }
  511. else{
  512. glColor3f(1.0, 1.0, 0.0);
  513. }
  514.  
  515. }
  516. //Desenha um quadrado
  517. glVertex3f(a.x + passo, a.y, 0.0);
  518. glVertex3f(b.x + passo, b.y, 0.0);
  519. glVertex3f(c.x + passo, c.y, 0.0);
  520. glVertex3f(d.x + passo, d.y, 0.0);
  521.  
  522. //muda as coordenadas base para desenhar outro quadrado na prox iteracao
  523.  
  524. glEnd();
  525. passo += rate;
  526. }
  527. //Retorna ao inicio da linha
  528. passo = 0.0;
  529.  
  530. // Desce p a proxima linha de renderização
  531. a.y -= rate;
  532. b.y -= rate;
  533. c.y -= rate;
  534. d.y -= rate;
  535.  
  536. }
  537. glFlush();
  538. a.x = 0.0;
  539. a.y = 0.0;
  540.  
  541. b.x = 0.2;
  542. b.y = 0.0;
  543.  
  544. c.x = 0.2;
  545. c.y = 0.2;
  546.  
  547. d.x = 0.0;
  548. d.y = 0.2;
  549. }
  550.  
  551. bool confereDestroyer(int lin, int col){
  552. cout << "Confere: lin"<< lin << " col"<< col <<endl;
  553. //Confere se não tem nada na posicao
  554. if(tabuleiro[lin][col] == 0 && tabuleiro[lin][(col+1)] == 0 && tabuleiro[(lin+1)][(col+1)] == 0 && tabuleiro[(lin+1)][(col+2)]== 0 && tabuleiro[(lin+1)][(col+3)] == 0){
  555. tabuleiro[lin][col] = 1;
  556. tabuleiro[lin][(col+1)] = 1;
  557. tabuleiro[(lin+1)][(col+1)] = 1;
  558. tabuleiro[(lin+1)][(col+2)] = 1;
  559. tabuleiro[(lin+1)][(col+3)] = 1;
  560. return true;
  561. }
  562. else{
  563. //Se houver, anda uma linha até achar um lugar vazio
  564. if(lin < TAMANHO-2){
  565. lin++;
  566. confereDestroyer(lin, col);
  567. }
  568. //Anda uma coluna para a direita até achar um lugar vazio
  569. else if (col < TAMANHO-4) {
  570. lin = 0;
  571. col++;
  572. confereDestroyer(lin, col);
  573. }
  574. //Caso não ache lugar vazio, retorna falso. Dentro do while será gerado outra posicao aleatoria
  575. else{
  576. return false;
  577. }
  578. }
  579.  
  580. }
  581.  
  582. bool confereSubmarino(int lin, int col){
  583. cout << "Confere: lin"<< lin << " col"<< col <<endl;
  584. //Confere se não tem nada na posicao
  585. if(tabuleiro[lin][col] == 0 && tabuleiro[lin][(col+1)] == 0 && tabuleiro[lin][(col+2)] == 0 && tabuleiro[lin][(col+3)] == 0 && tabuleiro[lin][(col+4)] == 0 && tabuleiro[(lin-1)][(col+2)] == 0){
  586. tabuleiro[lin][col] = 1;
  587. tabuleiro[lin][(col+1)] = 1;
  588. tabuleiro[lin][(col+2)] = 1;
  589. tabuleiro[lin][(col+3)] = 1;
  590. tabuleiro[lin][(col+4)] = 1;
  591. tabuleiro[(lin-1)][(col+2)] = 1;
  592. return true;
  593. }
  594. else{
  595. //Se houver, anda uma linha até achar um lugar vazio
  596. if(lin < TAMANHO-1){
  597. lin++;
  598. confereSubmarino(lin, col);
  599. }
  600. //Anda uma coluna para a direita até achar um lugar vazio
  601. else if (col < TAMANHO-5) {
  602. lin = 0;
  603. col++;
  604. confereSubmarino(lin, col);
  605. }
  606. //Caso não ache lugar vazio, retorna falso. Dentro do while será gerado outra posicao aleatoria
  607. else{
  608. return false;
  609. }
  610. }
  611.  
  612. }
  613.  
  614. bool conferePortaAvioes(int lin, int col){
  615. cout << "Confere: lin"<< lin << " col"<< col <<endl;
  616. //Confere se não tem nada na posicao
  617. if(tabuleiro[lin][col] == 0 && tabuleiro[lin][(col+1)] == 0 && tabuleiro[lin][(col+2)] == 0 && tabuleiro[lin][(col+3)] == 0 && tabuleiro[(lin+1)][(col+2)] == 0 && tabuleiro[(lin+1)][(col+3)] == 0 && tabuleiro[(lin+1)][(col+4)] == 0 && tabuleiro[(lin+1)][(col+5)] == 0){
  618. tabuleiro[lin][col] = 1;
  619. tabuleiro[lin][(col+1)] = 1;
  620. tabuleiro[lin][(col+2)] = 1;
  621. tabuleiro[lin][(col+3)] = 1;
  622. tabuleiro[(lin+1)][(col+2)] = 1;
  623. tabuleiro[(lin+1)][(col+3)] = 1;
  624. tabuleiro[(lin+1)][(col+4)] =1;
  625. tabuleiro[(lin+1)][(col+5)] = 1;
  626. return true;
  627. }
  628. else{
  629. //Se houver, anda uma linha até achar um lugar vazio
  630. if(lin < TAMANHO-2){
  631. lin++;
  632. conferePortaAvioes(lin, col);
  633. }
  634. //Anda uma coluna para a direita até achar um lugar vazio
  635. else if (col < TAMANHO-6) {
  636. lin = 0;
  637. col++;
  638. conferePortaAvioes(lin, col);
  639. }
  640. //Caso não ache lugar vazio, retorna falso. Dentro do while será gerado outra posicao aleatoria
  641. else{
  642. return false;
  643. }
  644. }
  645.  
  646. }
  647.  
  648. bool confereCorveta(int lin, int col){
  649. cout << "Confere: lin"<< lin << " col"<< col <<endl;
  650. //Confere se não tem nada na posicao
  651. if(tabuleiro[lin][col] == 0 && tabuleiro[lin][(col+1)] == 0 && tabuleiro[(lin+1)][(col+1)] == 0 ){
  652. tabuleiro[lin][col] = 1;
  653. tabuleiro[lin][(col+1)] = 1;
  654. tabuleiro[(lin+1)][(col+1)] = 1;
  655. return true;
  656. }
  657. else{
  658. //Se houver, anda uma linha até achar um lugar vazio
  659. if(lin < TAMANHO-2){
  660. lin++;
  661. confereCorveta(lin, col);
  662. }
  663. //Anda uma coluna para a direita até achar um lugar vazio
  664. else if (col < TAMANHO-2) {
  665. lin = 0;
  666. col++;
  667. confereCorveta(lin, col);
  668. }
  669. //Caso não ache lugar vazio, retorna falso. Dentro do while será gerado outra posicao aleatoria
  670. else{
  671. return false;
  672. }
  673. }
  674.  
  675. }
  676.  
  677. bool confereLancaAtaque(int lin, int col){
  678. cout << "Confere: lin"<< lin << " col"<< col <<endl;
  679. //Confere se não tem nada na posicao
  680. if(tabuleiro[lin][col] == 0 && tabuleiro[lin][(col+1)] == 0 && tabuleiro[(lin+1)][(col+2)] == 0){
  681. tabuleiro[lin][col] = 1;
  682. tabuleiro[lin][(col+1)] = 1;
  683. tabuleiro[(lin+1)][(col+2)] = 1;
  684. return true;
  685. }
  686. else{
  687. //Se houver, anda uma linha até achar um lugar vazio
  688. if(lin < TAMANHO-2){
  689. lin++;
  690. confereLancaAtaque(lin, col);
  691. }
  692. //Anda uma coluna para a direita até achar um lugar vazio
  693. else if (col < TAMANHO-3) {
  694. lin = 0;
  695. col++;
  696. confereLancaAtaque(lin, col);
  697. }
  698. //Caso não ache lugar vazio, retorna falso. Dentro do while será gerado outra posicao aleatoria
  699. else{
  700. return false;
  701. }
  702. }
  703.  
  704. }
  705.  
  706. void posicionaEmbarcacoes(){
  707.  
  708. for(int i = 0 ; i < NUM_EMB_BOMB ; i++){
  709. //Destroyer
  710. if(posicoesEmb[i] == 1){
  711. //gera posicao aleatoria para o destroyer
  712. lin[i] = rand()%(TAMANHO-1) ;
  713. col[i] = rand()%(TAMANHO-3);
  714. cout << "Lin["<<i <<"]: " << lin[i] << "Col: " << col[i] << endl;
  715. //Confere a posicao do destroyer está vazia
  716. while (!confereDestroyer(lin[i],col[i])){
  717. lin[i] = rand()%(TAMANHO-1);
  718. col[i] = rand()%(TAMANHO-3);
  719. }
  720.  
  721. }
  722. //Submarino
  723. else if(posicoesEmb[i] == 2){
  724. //gera posicao aleatoria para o submarino
  725. lin[i] = rand()%(TAMANHO-1)+1;
  726. col[i] = rand()%(TAMANHO-4);
  727. cout << "Lin["<<i <<"]: " << lin[i] << "Col: " << col[i] << endl;
  728. //Confere a posicao do submarino está vazia
  729. while (!confereSubmarino(lin[i],col[i])){
  730. lin[i] = rand()%(TAMANHO-1)+1 ;
  731. col[i] = rand()%(TAMANHO-4);
  732. }
  733. }
  734. else if(posicoesEmb[i] == 3){
  735. //gera posicao aleatoria para o porta avioes
  736. lin[i] = rand()%(TAMANHO-1);
  737. col[i] = rand()%(TAMANHO-5);
  738. cout << "Lin["<<i <<"]: " << lin[i] << "Col: " << col[i] << endl;
  739. //Confere a posicao do porta avioes está vazia
  740. while (!conferePortaAvioes(lin[i],col[i])){
  741. lin[i] = rand()%(TAMANHO-1) ;
  742. col[i] = rand()%(TAMANHO-5);
  743. }
  744. }
  745. else if(posicoesEmb[i] == 4){
  746. //gera posicao aleatoria para a corveta
  747. lin[i] = rand()%(TAMANHO-1);
  748. col[i] = rand()%(TAMANHO-1);
  749. cout << "Lin["<<i <<"]: " << lin[i] << "Col: " << col[i] << endl;
  750. //Confere a posicao da corveta está vazia
  751. while (!confereCorveta(lin[i],col[i])){
  752. lin[i] = rand()%(TAMANHO-1) ;
  753. col[i] = rand()%(TAMANHO-1);
  754. }
  755. }
  756. else{
  757. //gera posicao aleatoria para a lanca de ataque
  758. lin[i] = rand()%(TAMANHO-1);
  759. col[i] = rand()%(TAMANHO-2);
  760. cout << "Lin["<<i <<"]: " << lin[i] << "Col: " << col[i] << endl;
  761. //Confere a posicao da lanca de ataque está vazia
  762. while (!confereLancaAtaque(lin[i],col[i])){
  763. lin[i] = rand()%(TAMANHO-1) ;
  764. col[i] = rand()%(TAMANHO-2);
  765. }
  766. }
  767.  
  768. }
  769.  
  770. }
  771.  
  772. void imprime(){
  773. for(int i = 0; i < TAMANHO; i++){
  774. cout << i << " " ;
  775. for(int j = 0; j < TAMANHO; j++){
  776. cout << tabuleiro[i][j] << " " ;
  777.  
  778. }
  779. cout << endl;
  780. }
  781.  
  782.  
  783. }
  784.  
  785. void display(void){
  786. cout << "Entrou display()" << endl;
  787. glClear(GL_COLOR_BUFFER_BIT);
  788. // Limpa a janela
  789. glMatrixMode(GL_PROJECTION);
  790. //Evita erro de projeção da imagem
  791. glLoadIdentity();
  792.  
  793. //cout << "jogoAtual: " << jogoAtual << endl;
  794.  
  795. if(jogoAtual == 1){
  796. quad2x2();
  797. }
  798. else{
  799. if (jogoAtual == 2){
  800. quad3x3();
  801. }
  802. else{
  803. if (jogoAtual == 3){
  804. cout << "batalha navalis" << endl;
  805. }
  806. }
  807. }
  808. cout << "Saiu display()" << endl;
  809. }
  810.  
  811. void GerenciaMouse(int button, int state, int x, int y){
  812. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  813. cout << "Pixel x: " << x << endl;
  814. cout << "Pixel y: " << y << endl;
  815. }
  816.  
  817. /*if (button == GLUT_RIGHT_BUTTON){
  818. cout << "Botao direito apertado" << endl;
  819. }*/
  820.  
  821. //Usa estas regras para o mouse se o jogo for o nivel fácil
  822. if(jogoAtual == 1){
  823. //Região do quadrado superior direito
  824. if (x > 260 && x < 389 && y > 110 && y < 218 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  825. guess[indice] = 1;
  826. indice++;
  827. cout << "Quadrado superior direito, ";
  828. cout << "indice: " << indice << endl;
  829. if(indice == nivel){
  830. verificacao = 1;
  831. verificaAcerto();
  832. }
  833. }
  834.  
  835. //Região do quadrado superior esquerdo
  836. if (x > 130 && x < 260 && y > 110 && y < 218 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  837. guess[indice] = 2;
  838. indice++;
  839. cout << "Quadrado superior esquerdo, ";
  840. cout << "indice: " << indice << endl;
  841. if(indice == nivel){
  842. verificacao = 1;
  843. verificaAcerto();
  844. }
  845. }
  846.  
  847. //Região do quadrado inferior esquerdo
  848. if (x > 130 && x < 260 && y > 218 && y < 329 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  849. guess[indice] = 3;
  850. indice++;
  851. cout << "Quadrado inferior esquerdo, ";
  852. cout << "indice: " << indice << endl;
  853. if(indice == nivel){
  854. verificacao = 1;
  855. verificaAcerto();
  856. }
  857. }
  858.  
  859. //Região do quadrado inferior direito
  860. if (x > 260 && x < 389 && y > 218 && y < 329 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  861. guess[indice] = 4;
  862. indice++;
  863. cout << "Quadrado inferior direito, " ;
  864. cout << "indice: " << indice << endl;
  865. if(indice == nivel){
  866. verificacao = 1;
  867. verificaAcerto();
  868. }
  869. }
  870. }
  871.  
  872. if(jogoAtual == 2){
  873. //Região do quadrado direito L1
  874. if (x > 313 && x < 415 && y > 88 && y < 174 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  875. guess[indice] = 1;
  876. indice++;
  877. cout << "Quadrado superior direito, ";
  878. cout << "indice: " << indice << endl;
  879. if(indice == nivel){
  880. verificacao = 1;
  881. verificaAcerto();
  882. }
  883. }
  884.  
  885. //Região do meio L1
  886. if (x > 209 && x < 311 && y > 88 && y < 174 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  887. guess[indice] = 2;
  888. indice++;
  889. cout << "Quadrado superior esquerdo, ";
  890. cout << "indice: " << indice << endl;
  891. if(indice == nivel){
  892. verificacao = 1;
  893. verificaAcerto();
  894. }
  895. }
  896.  
  897. //Região do esquerdo L1
  898. if (x > 104 && x < 207 && y > 88 && y < 174 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  899. guess[indice] = 3;
  900. indice++;
  901. cout << "Quadrado inferior esquerdo, ";
  902. cout << "indice: " << indice << endl;
  903. if(indice == nivel){
  904. verificacao = 1;
  905. verificaAcerto();
  906. }
  907. }
  908.  
  909. //Região do quadrado direito L2
  910. if (x > 313 && x < 415 && y > 176 && y < 263 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  911. guess[indice] = 4;
  912. indice++;
  913. cout << "Quadrado inferior direito, " ;
  914. cout << "indice: " << indice << endl;
  915. if(indice == nivel){
  916. verificacao = 1;
  917. verificaAcerto();
  918. }
  919. }
  920.  
  921. //Região do quadrado meio L2
  922. if (x > 209 && x < 311 && y > 176 && y < 263 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  923. guess[indice] = 5;
  924. indice++;
  925. cout << "Quadrado inferior direito, " ;
  926. cout << "indice: " << indice << endl;
  927. if(indice == nivel){
  928. verificacao = 1;
  929. verificaAcerto();
  930. }
  931. }
  932.  
  933. //Região do quadrado esquerdo L2
  934. if (x > 104 && x < 207 && y > 176 && y < 263 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  935. guess[indice] = 6;
  936. indice++;
  937. cout << "Quadrado inferior direito, " ;
  938. cout << "indice: " << indice << endl;
  939. if(indice == nivel){
  940. verificacao = 1;
  941. verificaAcerto();
  942. }
  943. }
  944.  
  945. //Região do quadrado direito L3
  946. if (x > 260 && x < 389 && y > 265 && y < 351 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  947. guess[indice] = 7;
  948. indice++;
  949. cout << "Quadrado inferior direito, " ;
  950. cout << "indice: " << indice << endl;
  951. if(indice == nivel){
  952. verificacao = 1;
  953. verificaAcerto();
  954. }
  955. }
  956.  
  957. //Região do quadrado meio L3
  958. if (x > 209 && x < 311 && y > 265 && y < 351 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  959. guess[indice] = 8;
  960. indice++;
  961. cout << "Quadrado inferior direito, " ;
  962. cout << "indice: " << indice << endl;
  963. if(indice == nivel){
  964. verificacao = 1;
  965. verificaAcerto();
  966. }
  967. }
  968.  
  969. //Região do quadrado esquerdo L3
  970. if (x > 104 && x < 207 && y > 265 && y < 351 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  971. guess[indice] = 9;
  972. indice++;
  973. cout << "Quadrado inferior direito, " ;
  974. cout << "indice: " << indice << endl;
  975. if(indice == nivel){
  976. verificacao = 1;
  977. verificaAcerto();
  978. }
  979. }
  980. }
  981.  
  982. glutPostRedisplay();
  983. }
  984.  
  985. void jogoMemoriaFacil(){
  986. cout << "Entrou jogoMemoriaFacil()" << endl;
  987. int i;
  988.  
  989. // Controla cada jogada até o player errar
  990. for(i = 0; i < nivel ; i++){
  991. cout << "tempo" << tempo << endl;
  992. Sleep(tempo - (10*nivel));
  993. //Pinta um quadrado de preto
  994. flagBlackColor = turn[i];
  995. display();
  996. cout << "Piscou " << i << endl;
  997.  
  998. Sleep(tempo - (10*nivel));
  999.  
  1000. //Pinta o quadrado com a cor original
  1001. flagBlackColor = 0;
  1002. display();
  1003. }
  1004. }
  1005.  
  1006. void jogoMemoriaDificil(){
  1007. cout << "Entrou jogo da memoria Dificil" << endl;
  1008. int i;
  1009.  
  1010. // Controla cada jogada até o player errar
  1011. for(i = 0; i < nivel ; i++){
  1012. cout << "tempo" << tempo << endl;
  1013. Sleep(tempo - (10*nivel));
  1014. //Pinta um quadrado de preto
  1015. flagBlackColor = turn[i];
  1016. display();
  1017.  
  1018. cout << "Piscou " << i << endl;
  1019. Sleep(tempo - (10*nivel));
  1020.  
  1021. //Pinta o quadrado com a cor original
  1022. flagBlackColor = 0;
  1023. display();
  1024. }
  1025. }
  1026.  
  1027. void jogoBatalhaNaval(){
  1028. desenhaCubos();
  1029. posicionaEmbarcacoes();
  1030. imprime();
  1031. }
  1032.  
  1033. void verificaAcerto(){
  1034. int cont = 0;
  1035. cout << "Entrou verifica acerto" << endl;
  1036. cout << "valor do indice: " << indice << endl;
  1037. if(verificacao == 1){
  1038. for(int j = 0 ; j < indice ; j++){
  1039. if (turn[j] == guess[j]){
  1040. cont++;
  1041. }
  1042. }
  1043.  
  1044. cout << "valor do indice: " << indice << endl;
  1045. cout << "valor do cont: " << cont << endl;
  1046. if(cont == indice){
  1047. cout << "Voce acertou!" << endl;
  1048. indice = 0;
  1049. if(nivel <= 25){
  1050. nivel++;
  1051. cout << "Nivel: " << nivel << endl;
  1052. Sleep(500);
  1053.  
  1054. //Inicia o próximo nivel do jogo conforme o jogo que esta sendo jogado
  1055. switch(jogoAtual){
  1056. case 1:{
  1057. jogoMemoriaFacil();
  1058. break;
  1059. }
  1060. case 2:{
  1061. jogoMemoriaDificil();
  1062. break;
  1063. }
  1064. }
  1065.  
  1066. }
  1067. else{
  1068. exit(0);
  1069. }
  1070. }
  1071. else{
  1072. cout << "Voce errou!" << endl;
  1073. exit(0);
  1074. }
  1075. }
  1076.  
  1077.  
  1078. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement