Skip to content

Instantly share code, notes, and snippets.

@Alynva
Created February 11, 2017 15:09
Show Gist options
  • Save Alynva/4baf2c21bcafd8627602a4cf01be5f35 to your computer and use it in GitHub Desktop.
Save Alynva/4baf2c21bcafd8627602a4cf01be5f35 to your computer and use it in GitHub Desktop.
Jogo da Velha em CGI
#include <stdio.h>
#include <stdlib.h>
int main() {
// 0 = não jogado
// 1 = jogado o "O"
// 2 = jogado o "X"
char* dados = NULL;
int i;
int bloco = 0, proxJogada = 0;
int jogador = 0;
int placar[2] = {0,0};
int blocos[9] = {0,0,0,0,0,0,0,0,0};
int vencedor = -1;
int completo = 0;
char msg[50] = {'\n'};
FILE *lf;
lf = fopen("jogo-da-velha.bin", "r+b");
if (lf == NULL) {
snprintf(msg, sizeof msg, "O arquivo não pôde ser criado/lido.");
} else {
// Lendo o placar
fseek(lf, 0, SEEK_SET);
for (i = 0; i < 2; i++) fread(&placar[i], 1, sizeof(int), lf);
// Lendo o jogador
fseek(lf, 2*sizeof(int), SEEK_SET);
fread(&jogador, 1, sizeof(int), lf);
// Lendo as jogadas
fseek(lf, 3*sizeof(int), SEEK_SET);
for (i = 0; i < 9; i++) {
fread(&blocos[i], 1, sizeof(int), lf);
}
dados = getenv("QUERY_STRING");
if (dados == NULL) {
snprintf(msg, sizeof msg, "Erro durante a extração de dados do formulário.");
} else if (sscanf(dados, "bloco=%d&proxJogada=%d", &bloco, &proxJogada) != 2) {
snprintf(msg, sizeof msg, "Placar zerado.");
// Zerar o placar
placar[0] = 0;
placar[1] = 0;
fseek(lf, 0, SEEK_SET);
for (i = 0; i < 2; i++) fwrite(&completo, 1, sizeof(int), lf);
fflush(lf);
} else {
// Alterando o jogador
if (jogador == 0 || jogador == 2){jogador = 1;}
else {jogador = 2;}
fseek(lf, 2*sizeof(int), SEEK_SET);
fwrite(&jogador, 1, sizeof(int), lf);
fflush(lf);
// Salvando a nova jogada
if (bloco < 1) bloco = 1;
if (bloco > 9) bloco = 9;
fseek(lf, (2+bloco)*sizeof(int), SEEK_SET);
fwrite(&proxJogada, 1, sizeof(int), lf);
fflush(lf);
// Atualizando o vetor para a nova jogada
fseek(lf, (2+bloco)*sizeof(int), SEEK_SET);
fread(&blocos[bloco - 1], 1, sizeof(int), lf);
}
}
// Verificar ganhador
if (blocos[0] != 0) {
if ((blocos[0] == blocos[1] && blocos[1] == blocos[2]) || (blocos[0] == blocos[3] && blocos[3] == blocos[6]))
vencedor = blocos[0];
}
if (blocos[8] != 0) {
if ((blocos[8] == blocos[7] && blocos[7] == blocos[6]) || (blocos[8] == blocos[5] && blocos[5] == blocos[2]))
vencedor = blocos[8];
}
if (blocos[4] != 0) {
if ((blocos[4] == blocos[0] && blocos[0] == blocos[8]) || (blocos[4] == blocos[1] && blocos[1] == blocos[7]) || (blocos[4] == blocos[2] && blocos[2] == blocos[6]) || (blocos[4] == blocos[5] && blocos[5] == blocos[3]))
vencedor = blocos[4];
}
// Verificar se o jogo está completo
for (i = 0; i < 9; i++) if (blocos[i] != 0) completo++;
if (completo == 9 || vencedor != -1) {
snprintf(msg, sizeof msg, "Deu velha.");
if (vencedor != -1) {
if (vencedor == 1) {
snprintf(msg, sizeof msg, "<span style=\"color:red\">O</span> venceu!");
placar[0]++;
}
if (vencedor == 2) {
snprintf(msg, sizeof msg, "<span style=\"color:blue\">X</span> venceu!");
placar[1]++;
}
// Salvando o placar
fseek(lf, 0, SEEK_SET);
for (i = 0; i < 2; i++) fwrite(&placar[i], 1, sizeof(int), lf);
fflush(lf);
}
// Zerando as jogadas
for (i = 0; i < 9; i++) blocos[i] = 0;
fseek(lf, 3*sizeof(int), SEEK_SET);
for (i = 0; i < 9; i++) fwrite(&blocos[i], 1, sizeof(int), lf);
fflush(lf);
}
printf("%s%c%c\n","Content-Type:text/html;charset=UTF-8",13,10);
printf("<html>");
printf("<head>");
printf("<meta charset=\"utf-8\">");
printf("<meta name=\"author\" content=\"Alisson Nunes\">");
printf("<meta name=\"reply-to\" content=\"alynva@gmail.com\">");
printf("<meta name=\"generator\" content=\"Dev-C++ 5.11\">");
printf("<meta http-equiv=\"content-language\" content=\"pt-br\">");
printf("<meta name=\"description\" content=\"Um jogo simples, com a mecânica programada em Lingaugem C.\">");
printf("<meta name=\"viewport\" content=\"initial-scale = 1.0, maximum-scale = 1.0, user-scalable = no, width = device-width\">");
printf("<title>Jogo da velha</title>");
printf("<link rel=\"stylesheet\" href=\"../jogo-da-velha.css\">");
printf("</head>");
printf("<body class=\"prox%d\">", jogador);
printf("<a href=\"?\"><div id=\"placar\">");
printf("<div id=\"o\">");
printf("<span>O</span>");
printf("<br>");
printf("<span>%d</span>", placar[0]);
printf("</div>");
printf("<div id=\"x\">");
printf("<span>X</span>");
printf("<br>");
printf("<span>%d</span>", placar[1]);
printf("</div>");
printf("</div></a>");
printf("<div id=\"jogo\">");
printf("<a href=\"?bloco=1&proxJogada=%d\"><div class=\"cell%d\"></div></a>", jogador, blocos[0]);
printf("<a href=\"?bloco=2&proxJogada=%d\"><div class=\"cell%d\"></div></a>", jogador, blocos[1]);
printf("<a href=\"?bloco=3&proxJogada=%d\"><div class=\"cell%d\"></div></a>", jogador, blocos[2]);
printf("<a href=\"?bloco=4&proxJogada=%d\"><div class=\"cell%d\"></div></a>", jogador, blocos[3]);
printf("<a href=\"?bloco=5&proxJogada=%d\"><div class=\"cell%d\"></div></a>", jogador, blocos[4]);
printf("<a href=\"?bloco=6&proxJogada=%d\"><div class=\"cell%d\"></div></a>", jogador, blocos[5]);
printf("<a href=\"?bloco=7&proxJogada=%d\"><div class=\"cell%d\"></div></a>", jogador, blocos[6]);
printf("<a href=\"?bloco=8&proxJogada=%d\"><div class=\"cell%d\"></div></a>", jogador, blocos[7]);
printf("<a href=\"?bloco=9&proxJogada=%d\"><div class=\"cell%d\"></div></a>", jogador, blocos[8]);
printf("</div>");
printf("<div id=\"log\">%s</div>", msg);
printf("</body>");
printf("</html>");
fclose(lf);
return 0;
}
* {
font-family: monospace;
font-size: 1.1em;
text-align: center;
outline:none;
transition: opacity .4s;
}
body {
background-color: rgb(40,80,150);
color: white;
margin: 0;
}
#placar {
position: absolute;
top: 10px;
left: 0px; right: 0;
margin: auto;
text-align: center;
font-family: monospace;
cursor: pointer;
}
#placar > div {
display: inline-flex;
padding: 10px;
background-color: transparent;
color: white;
}
.prox1 #o, .prox2 #x {
background-color: white;
color: black;
border-bottom: 3px solid;
}
.prox1 #o {
border-bottom-color: red;
}
.prox2 #x {
border-bottom-color: blue;
}
#placar > div > span:nth-of-type(1) {
font-size: 2em;
}
#placar > div > span:nth-of-type(2) {
font-size: 1.2em;
}
#jogo {
position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
margin: auto;
height: calc(80vmin - 30px);
width: calc(80vmin - 30px);
}
.cell, .cell0, .cell1, .cell2 {
width: calc((80vmin - 30px) / 3);
height: calc((80vmin - 30px) / 3);
border: 5px white solid;
margin: -4px;
display: inline-block;
}
a:nth-of-type(1) .cell, a:nth-of-type(1) .cell0, a:nth-of-type(1) .cell1, a:nth-of-type(1) .cell2,
a:nth-of-type(2) .cell, a:nth-of-type(2) .cell0, a:nth-of-type(2) .cell1, a:nth-of-type(2) .cell2,
a:nth-of-type(3) .cell, a:nth-of-type(3) .cell0, a:nth-of-type(3) .cell1, a:nth-of-type(3) .cell2 {
border-top: 0;
}
a:nth-of-type(3) .cell, a:nth-of-type(3) .cell0, a:nth-of-type(3) .cell1, a:nth-of-type(3) .cell2,
a:nth-of-type(6) .cell, a:nth-of-type(6) .cell0, a:nth-of-type(6) .cell1, a:nth-of-type(6) .cell2,
a:nth-of-type(9) .cell, a:nth-of-type(9) .cell0, a:nth-of-type(9) .cell1, a:nth-of-type(9) .cell2 {
border-right: 0;
}
a:nth-of-type(9) .cell, a:nth-of-type(9) .cell0, a:nth-of-type(9) .cell1, a:nth-of-type(9) .cell2,
a:nth-of-type(8) .cell, a:nth-of-type(8) .cell0, a:nth-of-type(8) .cell1, a:nth-of-type(8) .cell2,
a:nth-of-type(7) .cell, a:nth-of-type(7) .cell0, a:nth-of-type(7) .cell1, a:nth-of-type(7) .cell2 {
border-bottom: 0;
}
a:nth-of-type(1) .cell, a:nth-of-type(1) .cell0, a:nth-of-type(1) .cell1, a:nth-of-type(1) .cell2,
a:nth-of-type(4) .cell, a:nth-of-type(4) .cell0, a:nth-of-type(4) .cell1, a:nth-of-type(4) .cell2,
a:nth-of-type(7) .cell, a:nth-of-type(7) .cell0, a:nth-of-type(7) .cell1, a:nth-of-type(7) .cell2 {
border-left: 0;
}
.prox1 .cell0:hover {
background-image: radial-gradient(transparent 38%, rgba(255,0,0,.4) 38%, rgba(255,0,0,.4) 44%, transparent 44%);
background-size: calc((80vmin - 30px) / 3);
background-position: 0 0;
cursor: pointer;
}
.prox2 .cell0:hover {
background-image: linear-gradient(45deg, transparent 47%, rgba(0,0,255,.4) 47%, rgba(0,0,255,.4) 53%, transparent 53%), linear-gradient(-45deg, transparent 47%, rgba(0,0,255,.4) 47%, rgba(0,0,255,.4) 53%, transparent 53%);
background-size: calc((80vmin - 30px) / 6);
background-position: calc((80vmin - 30px) / 12);
background-repeat: no-repeat;
cursor: pointer;
}
.cell1 {
background-image: radial-gradient(transparent 38%, red 38%, red 44%, transparent 44%);
background-size: calc((80vmin - 30px) / 3);
background-position: 0 0;
}
.cell2 {
background-image: linear-gradient(45deg, transparent 47%, blue 47%, blue 53%, transparent 53%), linear-gradient(-45deg, transparent 47%, blue 47%, blue 53%, transparent 53%);
background-size: calc((80vmin - 30px) / 6);
background-position: calc((80vmin - 30px) / 12);
background-repeat: no-repeat;
}
#log {
position: absolute;
width: 100%;
font-size: calc((80vmin - 30px) / 6);
bottom: 10px;
left: 0;
right: 0;
margin: auto;
font-weight: bolder;
font-family: sans-serif;
cursor: pointer;
pointer-events: none;
}
<html>
<head>
<meta http-equiv="refresh" content=" 0 ;url=cgi-bin/jogo-da-velha.cgi">
<meta charset="utf-8">
<meta name="author" content="Alisson Nunes">
<meta name="reply-to" content="alynva@gmail.com">
<meta name="generator" content="Sublime Text 2">
<meta http-equiv="content-language" content="pt-br">
<meta name="description" content="Um jogo simples, com a mecânica programada em Lingaugem C.">
<title>Jogo da velha</title>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment