Files
college-C/8/ex1.c
2022-02-25 15:33:16 +02:00

134 lines
2.9 KiB
C

#include "IdanLib.h"
#include <stdio.h>
#define N 3
unsigned short CountCharInColumn(char mat[][N], unsigned short row_size ,unsigned short column, char c)
{
unsigned short count = ZERO,
counter;
for (counter = ZERO; counter < row_size; counter++)
{
count += (mat[counter][column] == c);
}
return (count);
}
unsigned short CountCharInRow(char mat[][N], unsigned short col_size ,unsigned short row, char c)
{
unsigned short count = ZERO,
counter;
for (counter = ZERO; counter < col_size; counter++)
{
count += (mat[row][counter] == c);
}
return (count);
}
unsigned short CountCharInMainDiagonal(char mat[][N], unsigned short size, char c)
{
unsigned short count = ZERO,
counter;
for (counter = ZERO; counter < size; counter++)
{
count += (mat[size][size] == c);
}
return (count);
}
unsigned short CountCharInSecondaryDiagonal(char mat[][N], unsigned short size, char c)
{
unsigned short count = ZERO,
counter;
for (counter = ZERO; counter < size; counter++)
{
count += (mat[size][N - size] == c);
}
return (count);
}
BOOLEAN CheckWin(char mat[][N], unsigned short size, char c)
{
BOOLEAN win = (CountCharInMainDiagonal(mat, size, c) == THREE) +
(CountCharInSecondaryDiagonal(mat, size, c) == THREE);
unsigned short counter = ZERO;
while (!win * (size - counter))
{
win = (CountCharInColumn(mat, size, counter, c) == THREE) +
(CountCharInRow(mat, size, counter, c) == THREE);
counter++;
}
win = (win > ZERO);
return (win);
}
char InputChar()
{
char c;
scanf(" %c", &c);
return (c);
}
unsigned short InputUnsignedShort()
{
unsigned short number;
scanf("%hu", &number);
return (number);
}
void OutputPrintRequestForChar()
{
printf("Enter 'X' or 'O': ");
}
void OutputPrintRequestForColAndRow()
{
printf("Enter column and row: ");
}
void OutputPrintWhoWin(char c)
{
printf("The winner is: %c\n", c);
}
void OutputPrintMatrix(char mat[][N], unsigned int row_size, unsigned int column_size)
{
unsigned int counter_row,
counter_col;
for (counter_row = ZERO; counter_row < row_size; counter_row++)
{
for (counter_col = ZERO; counter_col < column_size; counter_col++)
{
printf("%c, ", mat[counter_row][counter_col]);
}
printf("\n");
}
}
void main(void)
{
char mat[N][N] = { ZERO };
BOOLEAN win = FALSE;
char input;
unsigned short col,
row;
while (!win)
{
OutputPrintRequestForChar();
input = InputChar();
OutputPrintRequestForColAndRow();
col = InputUnsignedShort();
row = InputUnsignedShort();
mat[row][col] = input;
win = CheckWin(mat, N, input);
OutputPrintMatrix(mat, N, N);
}
OutputPrintWhoWin(input);
}