#include <stdio.h>
#include "startgame.h"
#include "draw_chessboard.h"
void startgame(unsigned char array[chessboard_lenth][chessboard_width])
{
unsigned char insert_num = 0;
char choice = 0;
char chessboard_row = 0;
char chessboard_colum = 0;
char is_win = false;
int i = 0;
for(i = 0; i < step_max; ++i)
{
label: draw_chessboard(array);
#if 0
if(is_win)
{
//do something
break;
}
#endif
if(i%2 == 0)
{
printf("It's your turn :first player\n");
}
else
{
printf("It's your turn :second player\n");
}
scanf("%d", &insert_num);
fflush(stdin);
chessboard_row = (insert_num / 15);
chessboard_colum = (insert_num % 15);
if(array[chessboard_row][chessboard_colum] == 225 || array[chessboard_row][chessboard_colum] == 226)
{
printf("wrong insert\n");
goto label;
}
array[chessboard_row][chessboard_colum] = ((i%2)?225: 226);
is_win = judge_is_game_pass(array, chessboard_row, chessboard_colum);
if(is_win)
{
draw_chessboard(array);
break;
}
else
{
//
}
}
if(is_win)
{
if(i % 2 == 0)
{
printf("first player win\n");
}
else
{
printf("second player win");
}
}
else
{
printf("equal\n");
}
}
bool judge_is_game_pass(unsigned char array[chessboard_lenth][chessboard_width], char chessboard_row, char chessboard_colum)
{
unsigned char center = array[chessboard_row][chessboard_colum];
char i = 0;
char vertical_count = 0;
char horizontal_count = 0;
char left_circumgyrate_count = 0;
char right_circumgyrate_count = 0;
unsigned char Temp_row = chessboard_row;
unsigned char Temp_colum = chessboard_colum;
for(i = 0; i < 4; ++i) //1
{
Temp_row--;
if(Temp_row < 0)
{
break;
}
if(array[Temp_row][Temp_colum] == center)
{
vertical_count++;
}
else
{
break;
}
}
Temp_row = chessboard_row;
for(i = 0; i < 4; ++i) //2
{
Temp_row++;
if(Temp_row >= chessboard_lenth)
{
break;
}
if(array[Temp_row][Temp_colum] == center)
{
vertical_count++;
}
else
{
break;
}
}
Temp_row = chessboard_row;
for(i = 0; i < 4; ++i) //3
{
Temp_colum -= chessboard_width;
if(Temp_row < 0 )
{
break;
}
if(array[Temp_row][Temp_colum] == center)
{
horizontal_count++;
}
else
{
break;
}
}
Temp_colum = chessboard_colum;
for(i = 0; i < 4; ++i) //4
{
Temp_colum += chessboard_width;
if(Temp_row >= chessboard_width)
{
break;
}
if(array[Temp_row][Temp_colum] == center)
{
horizontal_count++;
}
else
{
break;
}
}
Temp_colum = chessboard_colum;
for(i = 0; i < 4; ++i) //5
{
Temp_row--;
Temp_colum--;
if(Temp_row < 0 || Temp_colum < 0)
{
break;
}
if(array[Temp_row][Temp_colum] == center)
{
left_circumgyrate_count++;
}
else
{
break;
}
}
Temp_row = chessboard_row;
Temp_colum = chessboard_colum;
for(i = 0; i < 4; ++i) //6
{
Temp_row++;
Temp_colum++;
if(Temp_row >= chessboard_width || Temp_colum >= chessboard_lenth)
{
break;
}
if(array[Temp_row][Temp_colum] == center)
{
left_circumgyrate_count++;
}
else
{
break;
}
}
Temp_row = chessboard_row;
Temp_colum = chessboard_colum;
for(i = 0; i < 4; ++i) //7
{
Temp_row++;
Temp_colum--;
if(Temp_row >= chessboard_width || Temp_colum < 0)
{
break;
}
if(array[Temp_row][Temp_colum] == center)
{
right_circumgyrate_count++;
}
else
{
break;
}
}
Temp_row = chessboard_row;
Temp_colum = chessboard_colum;
for(i = 0; i < 4; ++i) //8
{
Temp_row--;
Temp_colum++;
if(Temp_row < 0 || Temp_colum >= chessboard_lenth)
{
break;
}
if(array[Temp_row][Temp_colum] == center)
{
right_circumgyrate_count++;
}
else
{
break;
}
}
if(vertical_count == 4 || horizontal_count == 4 || left_circumgyrate_count ==4 || right_circumgyrate_count == 4)
{
return true;
}
else
{
return false;
}
}
评论0