Skip to content

Commit

Permalink
Update main.c
Browse files Browse the repository at this point in the history
  • Loading branch information
Romanov-v authored Jan 30, 2020
1 parent 031005e commit 070440e
Showing 1 changed file with 42 additions and 111 deletions.
153 changes: 42 additions & 111 deletions C/kurseWork/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ int RandomMove(int desk[3][3], int type )
* @param int type in {1, -1} сторона, которая должна сделать ход
* @return тип ошибки или 0, если функция выполнена без ошибок
*/

int inputCoordinates(int desk[3][3], int type)
{
if (desk == NULL )
Expand Down Expand Up @@ -127,7 +128,7 @@ int watchDesk(int desk[3][3])
* @param int desk in/out текущее состояние игровой доски
* @return тип ошибки или 0, если функция выполнена без ошибок
*/
int cheek_1(int desk[3][3], int* out)
int cheek_1(int desk[3][3])
{
if (desk == NULL )
return 1;
Expand All @@ -136,54 +137,47 @@ int cheek_1(int desk[3][3], int* out)
{
if( (desk[0][i] == desk[1][i]) && (desk[0][i] == desk[2][i]))
{
*out = desk[0][i];
return 0;
return desk[0][i];
}
}
*out = 0;
return 0;
}
/*
* Функция проверяет свободные клетки по вертикали
* @param int desk in/out текущее состояние игровой доски
* @return тип ошибки или 0, если функция выполнена без ошибок
*/
int cheek_2(int desk[3][3], int* out)
int cheek_2(int desk[3][3])
{
if (desk == NULL )
return 1;
for (int i=0; i<3; i++)
{
if( (desk[i][0] == desk[i][1]) && (desk[i][0] == desk[i][2]))
{
*out = desk[i][0];
return 0;
return desk[i][0];
}
}
*out = 0;
return 0;
}
/*
* Функция проверяет свободные клетки по диагонали
* @param int desk in/out текущее состояние игровой доски
* @return тип ошибки или 0, если функция выполнена без ошибок
*/
int cheek_3(int desk[3][3], int* out)
int cheek_3(int desk[3][3])
{
if (desk == NULL )
return 1;

if( (desk[0][0] == desk[1][1]) && (desk[0][0] == desk[2][2]))
{
*out = desk[0][0];
return 0;
return desk[0][0];
}
if( (desk[0][2] == desk[1][1]) && (desk[0][2] == desk[2][0]))
{
*out = desk[2][0];
return 0;
return desk[2][0];
}
*out = 0;
return 0;
}

Expand All @@ -192,33 +186,18 @@ int cheek_3(int desk[3][3], int* out)
* @param int desk in/out текущее состояние игровой доски
* @return тип ошибки или 0, если функция выаолнена без ошибок
*/
int super_check(int desk[3][3], int* out)
int super_check(int desk[3][3])
{
if (desk == NULL )
return 1;
int res = -20;
if (cheek_1(desk) != 0)
return cheek_1(desk);

if(cheek_1(desk, &res) != 0)
return 2;
if (res != 0) {
*out = res;
return 0;
}

if(cheek_2(desk, &res) != 0)
return 3;
if (res != 0) {
*out = res;
return 0;
}
if (cheek_2(desk) != 0)
return cheek_2(desk);

if(cheek_3(desk, &res) != 0)
return 4;
if (res != 0) {
*out = res;
return 0;
}
*out = 0;
if (cheek_3(desk) != 0)
return cheek_3(desk);
return 0;
}
/*
Expand Down Expand Up @@ -277,9 +256,8 @@ int smartPlay(int desk[3][3], int type, int difficult )
}
copyDesk[ x[i] ][ y[i] ] = type_copy;
type_copy *= -1;
int res = 0;
res = super_check(copyDesk, &res);
while(res == 0)

while(super_check(copyDesk) == 0)
{
if (moves >= 9)
{
Expand All @@ -291,14 +269,11 @@ int smartPlay(int desk[3][3], int type, int difficult )
} else {
moves ++;
type_copy *= -1;
}
res = super_check(copyDesk, &res);
}
}
res = super_check(copyDesk, &res);
result += 10 * type * res;

res = super_check(copyDesk, &res);
if (res == 0)
result += 10 * type * super_check(copyDesk);
if (super_check(copyDesk) == 0)
result += 7;


Expand All @@ -324,70 +299,33 @@ int smartPlay(int desk[3][3], int type, int difficult )
int test(){

int status = 0;
int desk[3][3] = { {0, 0, 0},
{0, 0, 0},
{0, 0, 0} };
int desk1[3][3] = { {-1,-1,-1},
{0,0,0},
{0, 0, 0} };
int desk2[3][3] = { {-1,1,1},
{1,-1,0},
{0,0,-1} };
watchDesk(desk2);
int desk[3][3] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };
int desk1[3][3] = { {-1,-1,-1}, {0,0,0}, {0, 0, 0} };
int desk2[3][3] = { {1,0,0}, {0,1,0}, {0,0,1} };
//assert(status(RandomMove(desk) != 0);
// check not-winning position
int res = -1000;

assert(cheek_1(desk, &res) == 0);
assert(res == 0);

assert(cheek_2(desk, &res) == 0);
assert(res == 0);

assert(cheek_3(desk, &res) == 0);
assert(res == 0);

assert(super_check(desk, &res) == 0);
assert(res == 0);
assert(cheek_1(desk) == 0);
assert(cheek_2(desk) == 0);
assert(cheek_3(desk) == 0);
assert(super_check(desk) == 0);
//check common wining of O(-1)
assert(cheek_1(desk1, &res) == 0);
assert(res == 0);

assert(cheek_2(desk1, &res) == 0);
assert(res == -1);

assert(cheek_3(desk1, &res) == 0);
assert(res == 0);

assert(super_check(desk1, &res) == 0);
assert(res == -1);

//assert(super_check(desk1) == -1);
assert(cheek_1(desk1) == 0);
assert(cheek_2(desk1) == -1);
assert(cheek_3(desk1) == 0);
assert(super_check(desk1) == -1);
// check diagonal win of X(1)
assert(cheek_1(desk2, &res) == 0);
assert(res == 0);
assert(cheek_2(desk2, &res) == 0);
assert(res == 0);
assert(cheek_3(desk2, &res) == 0);
assert(res == -1);
assert(super_check(desk2, &res) == 0);
assert(res == -1);
//assert(cheek_1(desk2) == 0);
//assert(cheek_2(desk2) == 0);
//assert(cheek_3(desk2) == 1);
//assert(super_check(desk2) == 1);
assert(cheek_1(desk2) == 0);
assert(cheek_2(desk2) == 0);
assert(cheek_3(desk2) == 1);
assert(super_check(desk2) == 1);

return status;

}
/*
* Функция обрабатывает
* @return тип ошибки или 0, если функция выполнена без ошибок
*/

int main()
{
int mode = 1;
int res = 0;
if (mode == 1)
printf("test status: %x \n", test());

Expand Down Expand Up @@ -429,7 +367,7 @@ int main()
err = printf("difficult: %d, %d \n", difficult, h);
if ( err < 0)
return 1;
err = printf("vyberite storonu\n 1 - kerstiku; -1 - noliki\n");
err = printf("vyberite storonu\n 1 - kerstiki; -1 - noliki\n");
if ( err < 0)
return 1;

Expand All @@ -439,8 +377,7 @@ int main()
if( err != 1)
return 1;
watchDesk(desk);

while(res == 0){
while(super_check(desk) == 0){
if(type == user_type)
{
error = inputCoordinates(desk, type);
Expand All @@ -462,14 +399,13 @@ int main()

if (moves >= 9)
{

if (res == 1)
if (super_check(desk) == 1)
{
err = printf("krestiki win\n ");
if ( err < 0)
return 1;
}
else if (res == -1)
else if (super_check(desk) == -1)
{
err = printf("noliki win\n ");
if ( err < 0)
Expand All @@ -481,22 +417,17 @@ int main()
}
return 0;
}
assert(super_check(desk, &res) == 0);

}

assert(super_check(desk, &res) == 0);
if (res == 1){
if (super_check(desk) == 1){
err = printf("krestiki win\n ");
if ( err < 0)
return 1;
}

if (res == -1){
if (super_check(desk) == -1){
err = printf("noliki win\n ");
if ( err < 0)
return 1;
}

return 0;
}

0 comments on commit 070440e

Please sign in to comment.