Funções da TAD snake.h
Autor: Fabiola Garcia | Publicado: 2012-06-15T05:29:00-07:00


/*
&nbsp; Função responsável por atribuir os valores iniciais
&nbsp; de direção e da matriz (cobra).
*/
void Reseta(int cobra[linhas][colunas], int &amp;direcao)
{
&nbsp; &nbsp; &nbsp;direcao = 2;
&nbsp; &nbsp; &nbsp;int i,j;
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;for (i=0;i&lt;linhas;i++)
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (j=0;j&lt;colunas;j++)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cobra[i][j] = vazio;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;cobra[0][0] = 190;
&nbsp; &nbsp; &nbsp;cobra[0][1] = 250;
&nbsp; &nbsp; &nbsp;cobra[0][2] = 190+TAM;
&nbsp; &nbsp; &nbsp;cobra[0][3] = 250+TAM;
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;cobra[1][0] = 190-TAM;
&nbsp; &nbsp; &nbsp;cobra[1][1] = 250;
&nbsp; &nbsp; &nbsp;cobra[1][2] = 190;
&nbsp; &nbsp; &nbsp;cobra[1][3] = 250+TAM;
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;cobra[2][0] = 190-TAM-TAM;
&nbsp; &nbsp; &nbsp;cobra[2][1] = 250;
&nbsp; &nbsp; &nbsp;cobra[2][2] = 190;
&nbsp; &nbsp; &nbsp;cobra[2][3] = 250+TAM;
}

/*
&nbsp; &nbsp;Função responsável por movimentar a matriz(cobra).
&nbsp; &nbsp; Faz com que a mesma se movimente sem parar de
&nbsp; &nbsp; acordo com o valor da direção atual.
*/
void Movimenta(int cobra[linhas][colunas], int direcao)
{ &nbsp; &nbsp;
&nbsp; &nbsp; if (direcao == UP &amp;&amp; cobra[0][1] &gt;= TelaInicio)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp;cobra[0][1] -= TAM;
&nbsp; &nbsp; &nbsp; &nbsp;cobra[0][3] -= TAM;
&nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; if (direcao == RIGHT &amp;&amp; cobra[0][2] &lt;= TelaX)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp;cobra[0][0] += TAM;
&nbsp; &nbsp; &nbsp; &nbsp;cobra[0][2] += TAM;
&nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; if (direcao == DOWN &amp;&amp; cobra[0][3] &lt;= TelaY)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp;cobra[0][1] += TAM;
&nbsp; &nbsp; &nbsp; &nbsp;cobra[0][3] += TAM;
&nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; if (direcao == LEFT &amp;&amp; cobra[0][0] &gt;= TelaInicio)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp;cobra[0][0] -= TAM;
&nbsp; &nbsp; &nbsp; &nbsp;cobra[0][2] -= TAM;
&nbsp; &nbsp; }
}

/*
&nbsp; &nbsp;Função com uma única finalidade:
&nbsp; &nbsp;criar dois números randômicos
&nbsp; &nbsp;para x e y do bitmap comidinha,
&nbsp; &nbsp;colocando a comidinha sempre em
&nbsp; &nbsp;lugares aleatórios da tela.
*/
void Comidinha(int &amp;x, int &amp;y)
{
&nbsp; &nbsp; &nbsp;if (x == vazio &amp;&amp; y == vazio)
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; srand(time(NULL));
&nbsp; &nbsp; &nbsp; &nbsp; x = rand() % 35;
&nbsp; &nbsp; &nbsp; &nbsp; x *= TAM;
&nbsp; &nbsp; &nbsp; &nbsp; x += TelaInicio;
&nbsp; &nbsp; &nbsp; &nbsp; y = rand() % 25;
&nbsp; &nbsp; &nbsp; &nbsp; y *= TAM;
&nbsp; &nbsp; &nbsp; &nbsp; y += TelaInicio;
&nbsp; &nbsp; &nbsp;}
}

/*
&nbsp; Esta função tem a finalidade de limpar a última posição
&nbsp; da matriz conforme a cobra anda pra frente.
&nbsp; [FUNÇÃO EXPLICADA EM DETALHES NO VÍDEO]
*/
void Crescer(int cobra[linhas][colunas], int x)
{ &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;int contador = 0;
&nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;while (cobra[contador][0] != vazio)
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp;contador++;
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;int aux = contador;
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;while (contador &gt; 0)
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp;cobra[contador][0] = cobra[contador - 1][0];
&nbsp; &nbsp; &nbsp; &nbsp;cobra[contador][1] = cobra[contador - 1][1];
&nbsp; &nbsp; &nbsp; &nbsp;cobra[contador][2] = cobra[contador - 1][2];
&nbsp; &nbsp; &nbsp; &nbsp;cobra[contador][3] = cobra[contador - 1][3];

&nbsp; &nbsp; &nbsp; &nbsp;contador--;
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;if (x != vazio)
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cobra[aux][0] = cobra[aux + 1][0];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cobra[aux][1] = cobra[aux + 1][1];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cobra[aux][2] = cobra[aux + 1][2];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cobra[aux][3] = cobra[aux + 1][3];
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;else if (x == vazio)
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cobra[aux + 1][0] = cobra[aux + 2][0];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cobra[aux + 1][1] = cobra[aux + 2][1];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cobra[aux + 1][2] = cobra[aux + 2][2];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cobra[aux + 1][3] = cobra[aux + 2][3];
&nbsp; &nbsp; &nbsp;}
}

/* Função responsável por verificar o momento em que a cobra encosta na
comidinha. Neste momento o level diminui (aumenta a velocidade) e o
score ganha 10 pontos. */

void Comer(int cobra[linhas][colunas], int &amp;x, int &amp;y, int &amp;score, int &amp;level)
{
&nbsp; &nbsp; &nbsp;if (cobra[0][0] == x &amp;&amp; cobra[0][1] == y &amp;&amp; cobra[0][2] == x+TAM &amp;&amp; cobra[0][3] == y+TAM)
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; level -= 5;
&nbsp; &nbsp; &nbsp; &nbsp; x = vazio;
&nbsp; &nbsp; &nbsp; &nbsp; y = vazio;
&nbsp; &nbsp; &nbsp; &nbsp; score += 10;
&nbsp; &nbsp; &nbsp;}
}

/*
&nbsp; Colisao(); Essa função tem duas verificações, ela verifica se
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;houve colisão com as paredes, e também verifica
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;se houve colisão com a própria matriz(cobra). Caso
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eventualmente ocorrer colisão, a mensagem |Game Over|
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;aparecerá na tela, e o jogo termina.
*/
void Colisao(int cobra[linhas][colunas], int &amp;exit)
{
&nbsp; &nbsp; &nbsp;int contador = 0;
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;while (cobra[contador][0] != vazio)
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;contador++;
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;int i = 1;
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;if (cobra[0][0] &lt; TelaInicio || cobra[0][1] &lt; TelaInicio|| cobra[0][2] &gt; TelaX || cobra[0][3] &gt; TelaY)
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; exit = 0;
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;while (i &lt; contador)
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (cobra[0][0] != cobra[i][0] || cobra[0][1] != cobra[i][1] || cobra[0][2] != cobra[i][2] || cobra[0][3] != cobra[i][3])
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit = 0;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp;}
}

/* Função responsável por incluir um arquivo */

void Incluir(int score)
{
&nbsp; &nbsp; &nbsp;FILE *Arq;
&nbsp; &nbsp; &nbsp;Jogador jog;
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;jog.score = score;
&nbsp; &nbsp; &nbsp;printf("Escreva o seu nome: ");
&nbsp; &nbsp; &nbsp;gets(jog.nome);
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;Arq = fopen("AngrySnake.dat","a");
&nbsp; &nbsp; &nbsp;fwrite(&amp;jog, sizeof(jog),1,Arq);
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;fclose(Arq);
}

/* Função responsável por fazer uma consulta no arquivo,
salvar o melhor score em uma variável auxiliar e printar esse
resultado na tela do jogo. */

void ConsultarBS(int &amp;bestScore)
{
&nbsp; &nbsp; &nbsp;int i = 0;
&nbsp; &nbsp; &nbsp;int j = 0;
&nbsp; &nbsp; &nbsp;FILE *Arq;
&nbsp; &nbsp; &nbsp;Jogador jog[100];
&nbsp; &nbsp; &nbsp;Jogador aux;
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;Arq = fopen("AngrySnake.dat", "r");
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;while (fread(&amp;jog[i],sizeof(jog[i]),1,Arq))
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i++;
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp;fclose(Arq);
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;for (j=1;j&lt;i;j++)
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (jog[j].score &gt; jog[j-1].score)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aux.score = jog[j].score;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bestScore = aux.score; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp;}
}