Files
connect4/tests/probe_draw.cpp
T
Andre Neto f5d7468710 connect4: README uniforme, build para Windows verificado e testes da logica
- README no formato uniforme (What it is / Requirements / Install-Build / Usage /
  Tests / Structure / License). Diz que e uma aplicacao de consola Windows e
  documenta o kit real do build versionado (Qt 5.15.2 + MinGW GCC 8.1 64-bit,
  lido do Makefile do Qt Creator, nao inferido do nome da directoria).
- Install/Build: build cruzado para Windows verificado com mingw-w64 (0 erros,
  4 avisos -Wall) e tambem via qmake; o binario e PE32+ de consola x86-64 e nao
  importa DLLs do Qt (o .pro declara CONFIG -= qt).
- Tests: tests/test_cboard.cpp (59 verificacoes da logica do tabuleiro, inclui
  prova diferencial contra uma referencia independente), tests/probe_oob.cpp
  (AddressSanitizer: playPiece/checkWin nao validam a coluna) e
  tests/probe_draw.cpp (tabuleiro cheio sem vitoria). tests/run-tests.sh corre
  os tres passos. O codigo do jogo NAO foi alterado.
- License: por definir (co-autoria, Ines Jorge da Silva e Ferreira).
- .gitignore: builds do qmake, objectos, .exe e a saida de execucao do jogo,
  para o binario de teste nao ficar versionado.

Porque: faltava o README uniforme e nao havia prova de que o codigo compila nem
de que a logica do tabuleiro funciona.
2026-10-08 15:52:41 +01:00

114 lines
4.1 KiB
C++

// ---------------------------------------------------------------------------
// Sonda B: existe tabuleiro cheio (42 pecas) sem 4 em linha?
//
// O ramo do empate nos jogos (current_player = 'T') so dispara com o tabuleiro
// cheio, e nenhuma funcao de CBoard o responde. Aqui procura-se, com procura
// aleatoria repetida (semente fixa), uma sequencia de 42 jogadas legais em que
// NENHUMA feche 4 em linha; se aparecer, verifica-se:
// * com uma referencia independente (refWin/anyWin, escrita para o teste);
// * com o proprio CBoard::checkWin a cada jogada;
// * com getRow() == -1 nas 7 colunas no fim.
//
// Nao altera nada do jogo.
// ---------------------------------------------------------------------------
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "cboard.h"
class CPvM { // acesso de leitura via a amizade ja declarada no jogo
public:
static int turnOf(const CBoard &b) { return b.turn; }
};
static bool refWin(const char g[6][7], int row, int col, char p) {
static const int dr[4] = {0, 1, 1, 1};
static const int dc[4] = {1, 0, 1, -1};
for (int d = 0; d < 4; d++) {
int n = 1;
for (int s = 1; s < 4; s++) {
int r = row + s * dr[d], c = col + s * dc[d];
if (r >= 0 && r < 6 && c >= 0 && c < 7 && g[r][c] == p) n++; else break;
}
for (int s = 1; s < 4; s++) {
int r = row - s * dr[d], c = col - s * dc[d];
if (r >= 0 && r < 6 && c >= 0 && c < 7 && g[r][c] == p) n++; else break;
}
if (n >= 4) return true;
}
return false;
}
// varredura completa: ha 4 em linha em algum sitio do tabuleiro?
static bool anyWin(const char g[6][7]) {
for (int r = 0; r < 6; r++)
for (int c = 0; c < 7; c++)
if (g[r][c] != ' ' && refWin(g, r, c, g[r][c])) return true;
return false;
}
static void dump(const char g[6][7]) {
for (int r = 0; r < 6; r++) {
printf(" |");
for (int c = 0; c < 7; c++) printf("%c|", g[r][c]);
printf("\n");
}
}
int main(int argc, char **argv) {
int trials = argc > 1 ? atoi(argv[1]) : 200000;
unsigned long long seed = 987654321ULL;
for (int t = 0; t < trials; t++) {
char g[6][7];
memset(g, ' ', sizeof g);
int h[7] = {0, 0, 0, 0, 0, 0, 0};
int seq[42][2];
bool ok = true;
for (int turn = 0; turn < 42; turn++) {
char p = (turn % 2 == 0) ? 'O' : '0';
int cand[7], nc = 0;
for (int c = 0; c < 7; c++) {
if (h[c] >= 6) continue;
int r = 5 - h[c];
g[r][c] = p;
bool w = refWin(g, r, c, p);
g[r][c] = ' ';
if (!w) cand[nc++] = c; // jogada que nao fecha 4 em linha
}
if (nc == 0) { ok = false; break; } // so restam jogadas vencedoras
seed = seed * 6364136223846793005ULL + 1442695040888963407ULL;
int c = cand[(seed >> 33) % (unsigned)nc];
int r = 5 - h[c];
g[r][c] = p;
h[c]++;
seq[turn][0] = c;
seq[turn][1] = r;
}
if (!ok) continue;
printf("achado: tabuleiro cheio sem vitoria na tentativa %d\n", t);
if (anyWin(g)) { printf("ERRO: a varredura independente encontrou 4 em linha\n"); return 1; }
printf("referencia independente: nenhum 4 em linha (42 celulas varridas)\n");
dump(g);
CBoard b;
int wid = 0;
for (int turn = 0; turn < 42; turn++) {
char p = (turn % 2 == 0) ? 'O' : '0';
int c = seq[turn][0];
b.playPiece(c, p);
if (b.checkWin(c, 4)) wid++; // o jogo nao veria vitoria nenhuma
}
bool full = true;
for (int c = 0; c < 7; c++) if (b.getRow(c) != -1) full = false;
printf("replay no CBoard: turn == %d, checkWin verdadeiros == %d (esperado 0), tabuleiro cheio == %s\n",
CPvM::turnOf(b), wid, full ? "sim" : "nao");
return (wid == 0 && full && CPvM::turnOf(b) == 42) ? 0 : 1;
}
printf("nao encontrei tabuleiro cheio sem vitoria em %d tentativas (inconclusivo)\n", trials);
return 3;
}