Othello is a game played by two people on an 8 x 8 board, using disks that are white on one side and black on the other. One player places disks with the white side up and the other player places disks with the black side up. The players alternate placing one disk on an unoccupied space on the board. In placing a disk, the player must bracket at least one of the other color disks. Disks are bracketed if they are in a straight line horizontally, vertically, or diagonally, with a disk of the current player’s color at each end of the line. When a move is made, all the disks that were bracketed are changed to the color of the player making the move. (It is possible that disks will be bracketed across more than one line in a single move.)
The first line of the input is the number of games to be processed. Each game consists of a boardconfiguration followed by a list of commands. The board configuration consists of 9 lines. The first 8specify the current state of the board. Each of these 8 lines contains 8 characters, and each of thesecharacters will be one of the following:
‘-’ indicating an unoccupied square
‘B’ indicating a square occupied by a black disk
‘W’ indicating a square occupied by a white disk
The ninth line is either a ‘B’ or a ‘W’ to indicate which is the current player. You may assume thatthe data is legally formatted.
Then a set of commands follows. The commands are to list all possible moves for the current player,make a move, or quit the current game. There is one command per line with no blanks in the input.
The commands and the corresponding outputs are formatted as follows:
List all possible moves for the current player. The command is an ‘L’ in the first column of theline. The program should Go through the board and print all legal moves for the current playerin the format (x, y) where x represents the row of the legal move and y represents its column.These moves should be printed in row major order which means:
1) all legal moves in row number i will be printed before any legal move in row number j if jis greater than iand
2) if there is more than one legal move in row number i, the moves will be printed in ascendingorder based on column number.
All legal moves should be put on one line. If there is no legal move because it is impossible for thecurrent player to bracket any pieces, the program should print the message ‘No legal move.’
Make a move. The command is an ‘M’ in the first column of the line, followed by 2 digits in thesecond and third column of the line. The digits are the row and the column of the space to placethe piece of the current player’s color, unless the current player has no legal move. If the currentplayer has no legal move, the current player is first changed to the other player and the movewill be the move of the new current player. You may assume that the move is then legal. Youshould record the changes to the board, including adding the new piece and changing the colorof all bracketed pieces. At the end of the move, print the number of pieces of each color on theboard in the format ‘Black - xx White - yy’ where xx is the number of black pieces on theboard and yy is the number of white pieces on the board. After a move, the current player willbe changed to the player that did not move.
Quit the current game. The command will be a ‘Q’ in the first column of the line. At this point,print the final board configuration using the same format as was used in the input. This terminatesinput for the current game.
You may assume that the commands will be syntactically correct. Put one blank line betweenoutput from separate games and no blank lines anywhere else in the output.
#include
using namespace std;
const int maxn = 15;
char maps[maxn][maxn];
char odr[maxn];
int dir[8][2] = {1,0,1,-1,1,1,-1,1,-1,0,-1,-1,0,1,0,-1};//可以找的八个方向
//初始化地图
void pre_maps()
{
for(int i=1; i<=8; i++)
scanf("%s",maps[i]+1);
}
//检查是否走出地图
bool check2(int x,int y)
{
if(x<1 || y<1 || x>8 || y>8)
return false;
return true;
}
//检查x,y位置是否是合法位置
bool check(int x,int y,char qi)
{
char el_qi;
if(qi == 'B')
el_qi = 'W';
else
el_qi = 'B';
for(int i=0; i<8; i++)
{
if(check2(x+dir[i][0],y+dir[i][1]) && maps[dir[i][0]+x][dir[i][1]+y] == el_qi)
{
int x1 = x,y1 = y;
x1 += dir[i][0];
y1 += dir[i][1];
while(check2(x1,y1))
{
if(maps[x1][y1] == '-')
break;
if(maps[x1][y1] == qi)
return true;
x1 += dir[i][0];
y1 += dir[i][1];
}
}
}
return false;
}
//在找到合法的地点放下棋子之后更新地图
void updata_maps(int x,int y,char qi)
{
char el_qi;
if(qi == 'B')
el_qi = 'W';
else
el_qi = 'B';
for(int i=0; i<8; i++)
{
if(check2(x+dir[i][0],y+dir[i][1]) && maps[dir[i][0]+x][dir[i][1]+y]== el_qi)
{
int x1 = x,y1 = y;
x1 += dir[i][0];
y1 += dir[i][1];
while(check2(x1,y1) && maps[x+dir[i][0]][y+dir[i][1]]==el_qi && maps[x1][y1] != '-')
{
if(maps[x1][y1] == qi)
{
int x2=x,y2=y;
while(x2!=x1 || y2!=y1)
{
if(maps[x2][y2] == el_qi)
maps[x2][y2] = qi;
x2 += dir[i][0];
y2 += dir[i][1];
}
break;
}
x1 += dir[i][0];
y1 += dir[i][1];
}
}
}
}
bool check_M(int x,int y,bool flag)
{
char qi;
if(flag)
qi = 'B';
else
qi = 'W';
if(check(x,y,qi) && maps[x][y] == '-')
{
maps[x][y] = qi;
updata_maps(x,y,qi);
}
else
{
flag = !flag;
if(flag)
qi = 'B';
else
qi = 'W';
maps[x][y] = qi;
updata_maps(x,y,qi);
}
int num_B,num_W;
num_B = num_W = 0;
for(int i=1; i<=8; i++)
for(int j=1; j<=8; j++)
{
if(maps[i][j] == 'B')
num_B++;
else if(maps[i][j] == 'W')
num_W++;
}
printf("Black - %2d White - %2d\n",num_B,num_W);
return flag;
}
void check_L(bool flag)
{
bool prin_flag = false,find_flag = false;
char qi;
if(flag)
qi = 'B';
else
qi = 'W';
for(int i=1; i<=8; i++)
for(int j=1; j<=8; j++)
{
if(maps[i][j] != '-')
continue;
if(check(i,j,qi) && prin_flag)
{
printf(" (%d,%d)",i,j);
find_flag = true;
}
else if(check(i,j,qi) && !prin_flag)
{
printf("(%d,%d)",i,j);
find_flag = true;
prin_flag = true;
}
}
if(!find_flag)//查看是否找到合法的位置
printf("No legal move.\n");
else
printf("\n");
}
void check_Q()
{
for(int i=1; i<=8; i++)
{
for(int j=1; j<=8; j++)
printf("%c",maps[i][j]);
printf("\n");
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(maps,0,sizeof(maps));
pre_maps();
bool flag = false;
while(scanf("%s",odr))
{
//找到第一个走的棋手
if(odr[0] == 'W')
flag = false;
else if(odr[0] == 'B')
flag = true;
if(odr[0] == 'M')
{
flag = check_M(odr[1]-'0',odr[2]-'0',flag);
flag = !flag;
}
else if(odr[0] == 'L')
check_L(flag);
else if(odr[0] == 'Q')
{
check_Q();
break;
}
}
if(t!=0)
printf("\n");
}
return 0;
}