/* Copyright (C) 2014 by Alexandru Cojocaru */ /* 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 3 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, see . */ #include #include typedef unsigned int uint; static uint h[3] = {5,4,3}; static bool win() { return (! (h[0] | h[1] | h[2])); } static void ai() { uint nimsum = h[0]^h[1]^h[2]; if (nimsum == 0) { for (int i = 0; i < 3; ++i) { if (h[i]) { printf ("%d %d\n", i, h[i]); h[i] = 0; break; } } } else { for (int i = 0; i < 3; ++i) { if ((h[i]^nimsum) < h[i]) { printf ("%d %d\n", i, h[i] - (h[i]^nimsum)); h[i] = h[i]^nimsum; break; } } } } static void user() { int hi; int n; scanf ("%d %d", &hi, &n); h[hi] -= n; } static void printheaps () { printf ("h0 %d\th1 %d\th2 %d\n", h[0], h[1], h[2]); } static int next () { static int i = 0; char *msg[] = {"Ai wins", "User wins"}; printheaps(); if (i == 0) ai (); else user (); if (win ()) { printheaps (); printf ("%s\n", msg[i]); return 0; } i = (i+1) % 2; return 1; } int main (void) { while (next ()) {;} return 0; }