/* This may look like c to you, but it's really -*-c++-*- */ /* GMA or "The Go-moku Apprentice" is a go-moku implementation that * learns the game from its own (and its opponents') mistakes. * Copyright (C) 1998 Johan Walles (d92-jwa@nada.kth.se) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "pattern.h" int Pattern::winner() const { // If I can win by making this move, return 0 // If my opponent can win here, return 1 // Otherwise return -1 int direction, position, offset; unsigned char first_sign[2]; int n_signs[2]; bool you_win = false; // bool i_win = false; for (direction = 0; direction < 8; direction += 2) { // Go through all four directions for (offset = 0; offset < 2; offset++) { // Go through each direction both this way and that n_signs[offset] = 0; first_sign[offset] = (chars[direction + offset] >> 6) & 0x03; if ((first_sign[offset] != PATTERN_MINE) && (first_sign[offset] != PATTERN_YOURS)) continue; for (position = 6; position >= 0; position -= 2) { if (((chars[direction + offset] >> position) & 0x03) == first_sign[offset]) n_signs[offset]++; else break; } } // Check both directions by themselves for (offset = 0; offset < 2; offset++) { if (n_signs[offset] == 4) if (first_sign[offset] == PATTERN_MINE) return 0; // i_win = true; else if (first_sign[offset] == PATTERN_YOURS) you_win = true; } // Check both directions together if ((first_sign[0] == first_sign[1]) && ((n_signs[0] + n_signs[1]) >= 4)) { if (first_sign[0] == PATTERN_MINE) return 0; // i_win = true; else if (first_sign[0] == PATTERN_YOURS) you_win = true; } } return you_win ? 1 : -1; }