#include "PointersLibs.h" #define ROWS 4 void RemoveBits(byte *row, unsigned short count) { *row >>= count; } unsigned short CountBits(byte b) { unsigned short counter = ZERO, loop_length = sizeof(b) * EIGHT; while (loop_length--) { counter += !EvenNumber(b); b >>= ONE; } return (counter); } void PrintGetRow() { printf("Enter row number (1-4): "); } void PrintGetRemoveCount() { printf("Enter many bits: "); } void PrintPlayerWon() { printf("You won!\n"); } void PrintBotWon() { printf("The bot won :(\n"); } unsigned short InputRow() { unsigned short row; scanf("%hu", &row); return (row); } unsigned short InputRemoveCount() { unsigned short remove_count; scanf("%hu", &remove_count); return (remove_count); } BOOLEAN EvenBits(byte b) { return (!(CountBits(b) % TWO)); } unsigned short SumBits(byte *vec, unsigned short length) { unsigned short sum = ZERO; while (length--) { sum += CountBits(*(vec++)); } return (sum); } void PrintBits(byte b) { unsigned short length = sizeof(b) * EIGHT; while (length--) { printf("%hu,", !EvenNumber(b)); b >>= ONE; } } void PrintRows(byte *rows, unsigned short length) { while (length--) { PrintBits(*(rows++)); printf("\n"); } } byte * MaxBitsRow(byte *rows, unsigned short length) { unsigned short max_count = ZERO, temp_count; byte *max_row; while (length--) { temp_count = CountBits(*rows); if (temp_count > max_count) { max_count = temp_count; max_row = rows; } rows++; } return (max_row); } void Bot(byte *rows, unsigned short length) { byte *max_row = MaxBitsRow(rows, length); RemoveBits(max_row, MAX(CountBits(*max_row) / TWO * TWO, ONE)); } void main(void) { byte rows[ROWS] = {0x7f, 0x1f, 0x7, 0x1}; unsigned short row = -ONE, remove_count = ZERO; BOOLEAN player = FALSE; while (SumBits(rows, ROWS)) { PrintRows(rows, ROWS); printf("\n\n"); row = -ONE; remove_count = ZERO; player = !player; if (player) { while (!(row < ROWS)) { PrintGetRow(); row = InputRow(); } while (!(ZERO < remove_count && remove_count <= CountBits(rows[row]))) { PrintGetRemoveCount(); remove_count = InputRemoveCount(); } RemoveBits(&rows[row], remove_count); } else { Bot(rows, ROWS); } } if (player) { PrintPlayerWon(); } else { PrintBotWon(); } }