// Copyright 2025 Bill Rossi
//
// This file is part of Hanafuda Hachi-Hachi.
//
// Hanafuda Hachi-Hachi 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 3 of the License, or (at your option) any later version.
//
// Hanafuda Hachi-Hachi 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 Hanafuda Hachi-Hachi. If not, see .
#include
#include
#include
#include "play.h"
bool valid_play(Hand *field, Card *played, Card *target) {
if (target == NULL) {
bool matching_month_in_field = false;
for (int i = 0; i < field->count; i++) {
if (field->cards[i]->month == played->month) {
matching_month_in_field = true;
break;
}
}
return !matching_month_in_field;
} else {
bool target_in_field = false;
for (int i = 0; i < field->count; i++) {
if (field->cards[i]->index == target->index) {
target_in_field = true;
break;
}
}
if (!target_in_field) return false;
return played->month == target->month;
}
}
void valid_targets(Card *active, Hand *field, Card **targets, int *target_count) {
*target_count = 0;
for (int i = 0; i < field->count; i++) {
if (field->cards[i]->month == active->month) {
targets[(*target_count)++] = field->cards[i];
}
}
targets[*target_count] = NULL; // Sentinel
}
Card *valid_target(Card *active, Hand *field) {
for (int i = 0; i < field->count; i++) {
if (field->cards[i]->month == active->month) {
return field->cards[i];
}
}
return NULL;
}
Play ai_play(Hand *hand, Hand *field) {
// very naive play initially
Card *played = hand->cards[0];
for (int i = 0; i < field->count; i++) {
Card *target = field->cards[i];
if (valid_play(field, played, target)) return (Play) { played, target };
}
return (Play) { played, NULL };
}