C_chess/logic.h

155 lines
5.8 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#define uchar unsigned char
static void rewriting_an_array(uchar move_options[8], int n, ...){
va_list factor;
va_start(factor, n);
for(int i=0;i<n; i++){
move_options[i] = va_arg(factor, int);
}
va_arg(factor, int);
va_end(factor);
}
static uchar bit_4_conversion(uchar x, uchar y){
return (x << 4) + y;
}
static uchar enemy_detection(int x, int y,uchar figure[8][8], struct target_struct target, uchar* T){
return (figure[target.x + x][target.y + y] >= 7 && *T) || (figure[target.x + x][target.y + y] < 7 && !*T);
}
static uchar procedural_positions(int* x, int* y, uchar* n, uchar figure[8][8], struct motion_struct motion[36], struct target_struct target, uchar* T){
if (figure[target.x + *x][target.y + *y] != 0){
if (enemy_detection(*x, *y, figure, target, T)){
motion[*n].xy = bit_4_conversion(target.x + *x, target.y + *y);
motion[*n].t = 2;
*n+=1;
}
return 1;
}
else{
motion[*n].xy = bit_4_conversion(target.x + *x, target.y + *y);
motion[*n].t = 1;
*n+=1;
}
return 0;
}
static void movement_request(uchar move_options, int* x, int* y){
switch(move_options){
case 0: *x+=1; break;
case 1: *x-=1; break;
case 2: *y+=1; break;
case 3: *y-=1; break;
case 4: *x+=1; *y+=1; break;
case 5: *x+=1; *y-=1; break;
case 6: *x-=1; *y-=1; break;
case 7: *y+=1; *x-=1; break;
}
}
static void pawn(uchar figure[8][8], struct motion_struct motion[36], struct target_struct target, uchar* i, uchar* T){
if (figure[target.x][target.y + (-1 + *T * 2)] == 0){// Генерация "стандартного" хода вверх
motion[*i].xy = bit_4_conversion(target.x, target.y+(-1 + *T * 2));
motion[*i].t = 1;
*i+=1;
if ((target.y == (6 - *T * 5)) &&(figure[target.x][target.y + (-1 + *T * 2) * 2] == 0)){ // Обработка первого хода
motion[*i].xy = bit_4_conversion(target.x, target.y+(-1 + *T * 2) * 2);
motion[*i].t = 1;
*i+=1;
}
}
int x = 1,y = (-1 + *T * 2);
if (figure[target.x + x][target.y + y] != 0 && (enemy_detection(x, y, figure, target, T))){ // Съедаем фигуру справа сверху
motion[*i].xy = bit_4_conversion(target.x+1, target.y + (-1 + *T * 2));
motion[*i].t = 2;
*i+=1;
}
x = -1;
if (figure[target.x + x][target.y + y] != 0 && enemy_detection(x, y, figure, target, T)){ // Съедаем фигуру слева сверху
motion[*i].xy = bit_4_conversion(target.x-1, target.y+ (-1 + *T * 2));
motion[*i].t = 2;
*i+=1;
}
}
static void reshuffle(int* x, int* y, int* n){
*n = *x;
*x = *y;
*y = *n;
}
static void horse(uchar figure[8][8], struct motion_struct motion[36], struct target_struct target, uchar* i, uchar* T){
int n, x_shift=0, y_shift=0;
for(int G = 0, x = 0, y = -2;G!= 2;reshuffle(&x, &y, &n), G++){
for(int bf=0; bf != 2;y=-y, x=-x, bf++){
if(x == 2 || x == -2){
x_shift = 0; y_shift = 1;
}
else{
x_shift = 1; y_shift = 0;
}
for (int v=0; v!=2; v++, x_shift = -x_shift, y_shift = -y_shift){
if (target.x+x+x_shift >= 0 && target.x+x+x_shift <= 7 && target.y+y+y_shift >= 0 && target.y+y+y_shift <= 7){// Поле
if (figure[target.x+x+x_shift][target.y+y+y_shift] != 0){// Препятствие
if (enemy_detection(x+x_shift, y+y_shift, figure, target, T)){ // Враг
motion[*i].xy = bit_4_conversion(target.x+x+x_shift, target.y+y+y_shift);
motion[*i].t = 2;
*i+=1;
}
}
else{ // Перемещение
motion[*i].xy = bit_4_conversion(target.x+x+x_shift, target.y+y+y_shift);
motion[*i].t = 1;
*i+=1;
}
}
}
}
}
}
void selection_moves(uchar figure[8][8], struct motion_struct motion[36], struct target_struct target){
uchar n = 0;
if (figure[target.x][target.y] == 0){
motion[n].t = 0;
return;
}
uchar T;
if (figure[target.x][target.y] < 7) T = 1; // Wite
else T = 0;
uchar move_options[8], N;
if (figure[target.x][target.y] == 1 || figure[target.x][target.y] == 7){ // Пешка
pawn(figure, motion, target, &n, &T);
motion[n].t = 0;
return;
}
else if (figure[target.x][target.y] == 3 || figure[target.x][target.y] == 9){// Ферзь
horse(figure, motion, target, &n, &T);
motion[n].t = 0;
return;
}
else if (figure[target.x][target.y] == 2 || figure[target.x][target.y] == 8){// Ладья
N = 4;
rewriting_an_array(move_options, N, 0,1,2,3);
}
else if (figure[target.x][target.y] == 4 || figure[target.x][target.y] == 10){// Слон
N = 4;
rewriting_an_array(move_options, N, 4,5,6,7);
}
else if (figure[target.x][target.y] == 5 || figure[target.x][target.y] == 11){// Ферзь
N = 8;
rewriting_an_array(move_options, N, 0,1,2,3,4,5,6,7);
}
else{
motion[n].t = 0;
return;
}
int x, y;
for (int i = 0; i != N; i++){
for (x = 0, y = 0, movement_request(move_options[i], &x, &y);
(target.x + x != -1) && (target.y + y != -1) && (target.x + x != 8) && (target.y + y != 8);
movement_request(move_options[i], &x, &y) ){
if(procedural_positions(&x, &y, &n, figure, motion, target, &T)) break;
}
}
motion[n].t = 0;
}