First Upload

This commit is contained in:
2022-02-25 15:33:16 +02:00
commit 0c74d10f0d
295 changed files with 74784 additions and 0 deletions

183
13 - Bits/ex5.c Normal file
View File

@@ -0,0 +1,183 @@
#include "PointersLibs.h"
#define ROWS 3
void RemoveBits(byte *row, unsigned short count)
{
*row >>= count;
}
unsigned short CountBits(byte b)
{
unsigned short counter = ZERO,
loop_lenght = sizeof(b) * EIGHT;
while (loop_lenght--)
{
counter += !EvenNumber(b);
b >>= ONE;
}
return (counter);
}
void PrintGetRow()
{
printf("Enter row number (1-3): ");
}
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()
{
return (InputRow());
}
unsigned short SumBits(byte *vec, unsigned short lenght)
{
unsigned short sum = ZERO;
while (lenght--)
{
sum += CountBits(*(vec++));
}
return (sum);
}
void PrintBits(byte b)
{
unsigned short lenght = sizeof(b) * EIGHT;
while (lenght--)
{
printf("%hu,", !EvenNumber(b));
b >>= ONE;
}
}
void PrintRows(byte *rows, unsigned short lenght)
{
while (lenght--)
{
PrintBits(*(rows++));
printf("\n");
}
}
byte * MaxBitsRow(byte *rows, unsigned short lenght)
{
unsigned short max_count = ZERO,
temp_count;
byte *max_row;
while (lenght--)
{
temp_count = CountBits(*rows);
if (temp_count > max_count)
{
max_count = temp_count;
max_row = rows;
}
rows++;
}
return (max_row);
}
byte * OddMaxBitsRow(byte *rows, unsigned short length)
{
unsigned short max_count = ZERO,
temp_count;
byte *odd_max_row = MaxBitsRow(rows, length);
while (length--)
{
if (!EvenBits(*rows))
{
temp_count = CountBits(*rows);
if (temp_count > max_count)
{
max_count = temp_count;
odd_max_row = rows;
}
}
rows++;
}
return (odd_max_row);
}
void Bot(byte *rows, unsigned short lenght)
{
byte *max_row = OddMaxBitsRow(rows, lenght);
unsigned short sum_bits = SumBits(rows, lenght);
BOOLEAN EvenArea = EvenNumber(sum_bits);
unsigned short count_bits = CountBits(*max_row);
unsigned short remove_count = EvenNumber(sum_bits - (count_bits / TWO)) ? (count_bits / TWO) : (count_bits / TWO) + ONE;
RemoveBits(max_row, remove_count); // EvenArea ? (CountBits(*max_row) / TWO * TWO - ONE) / TWO: (CountBits(*max_row) / TWO * TWO - ONE));
}
void main(void)
{
byte rows[ROWS] = {0x7f, 0x1f, 0x7};
unsigned short row = -ONE, remove_count = ZERO;
BOOLEAN player = FALSE;
while (SumBits(rows, ROWS) != ONE)
{
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
{
printf("Bot:\n");
Bot(rows, ROWS);
}
}
PrintRows(rows, ROWS);
printf("\n\n");
if (player)
{
PrintPlayerWon();
}
else
{
PrintBotWon();
}
}