UVa 220 Othello [模拟]

UVa 220 Othello
#Description
黑白棋
T测试数据组数
然后给出8*8的地图
在给出谁先下
W或者B
接下来是一系列指令
L表示列出一系列可行的走法的点
如果没有可行的,就交互选手
MXY表示在(X,Y)处落子 保证可行 输出落子之后的黑白子个数
然后Q退出,输出最终图形
#Algorithm
对应L操作
枚举每一个点,写OK函数
当周围有大于一个的连续的异色棋子,并且最终由同色棋子,则返回TRUE
对应MXY操作
于OK函数类似,不过在找到同色棋子后,沿着方向向量返回将颜色改写
就是模拟,不过异常复杂
输出很坑,具体看代码,我在王老师指导下避开了全部PE陷阱,一次AC
#Sample Input

2
--------
--------
--------
---WB---
---BW---
--------
--------
--------
W
L
M35
L
Q
WWWWB---
WWWB----
WWB-----
WB------
--------
--------
--------
--------
B
L
M25
L
Q

#Sample Output

(3,5) (4,6) (5,3) (6,4)
Black - 1 White - 4
(3,4) (3,6) (5,6)
--------
--------
----W---
---WW---
---BW---
--------
--------
--------
No legal move.
Black - 3 White - 12
(3,5)
WWWWB---
WWWWW---
WWB-----
WB------
--------
--------
--------
--------

#Code

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char g[8][8];
void print()
{
  for (int i = 0; i < 8; i++)
  {
    for (int j = 0; j < 8; j++)
      cout << g[i][j];
    cout << endl;
  }
}
bool player; //w true b false
bool same(char ch)
{
  if (ch == 'W' && player == true) return true;
  if (ch == 'B' && player == false) return true;
  return false;
}
void count()
{
  int totalW = 0;
  int totalB = 0;
  for (int i = 0; i < 8; i++)
    for (int j = 0; j < 8; j++)
    {
      if (g[i][j] == 'W') totalW++;
      if (g[i][j] == 'B') totalB++;
    }
  printf("Black - %2d White - %2d\n", totalB, totalW);
}
void change(int x, int y)
{
  if (player) g[x][y] = 'W'; else g[x][y] = 'B';
  for (int dx = -1; dx < 2; dx++)
    for (int dy = - 1; dy < 2; dy++)
    {
      if (dx == 0 && dy == 0) continue;
      int xx = x + dx;
      int yy = y + dy;
      if (xx < 0 || xx == 8 || yy < 0 || yy == 8) continue;
      if (g[xx][yy] != '-' && !same(g[xx][yy])) {
        for (;;) {
          xx += dx;
          yy += dy;
          if (xx < 0 || xx == 8 || yy < 0 || yy == 8) break;
          if (g[xx][yy] != '-' && same(g[xx][yy])) {
            while (xx != x || yy != y) {
              if (player) g[xx][yy] = 'W'; else g[xx][yy] = 'B';
              xx -= dx;
              yy -= dy;
            }
            break;
          }
          if (g[xx][yy] == '-') break;
        }
      }
    }
}
bool ok(int x, int y)
{
  if (g[x][y] != '-') return false;
  bool flag = false;
  for (int dx = -1; dx < 2; dx++)
    for (int dy = -1; dy < 2; dy++) {
      if (dx == 0 && dy == 0) continue;
      int xx = x + dx;
      int yy = y + dy;
      if (xx < 0 || xx == 8 || yy < 0 || yy == 8) continue;
      if (g[xx][yy] != '-' && !same(g[xx][yy])) {
        for (;;) {
          xx += dx;
          yy += dy;
          if (xx < 0 || xx == 8 || yy < 0 || yy == 8) break;
          if (g[xx][yy] != '-' && same(g[xx][yy])) return true;
          if (g[xx][yy] == '-') break;
        }
      }
    }
  if (flag) return true; else return false;
}
void solve()
{
  for (int i = 0; i < 8; i++) gets(g[i]);
  char s[10];
  gets(s);
   //W true B false
  if (s[0] == 'W') player = true; else player = false;
  for (;;) {
    gets(s);
    if (s[0] == 'Q') {
      print();
      break;
    }
    bool flag = false;
    bool flag2 = true;
    if (s[0] == 'L') {
      for (int i = 0; i < 8; i++)
        for (int j = 0; j < 8; j++) {
          if (ok(i, j)) {
            if (flag2) {
              printf("(%d,%d)", i + 1, j + 1);
              flag2 = false;
            }else printf(" (%d,%d)", i + 1, j + 1);
            flag = true;
          }
        }
      if (!flag)
      {
        puts("No legal move.");
        player = !player;
      }else
      printf("\n");
      continue;
    }
    if (s[0] == 'M') {
      int x = s[1] - '0' - 1;
      int y = s[2] - '0' - 1;
      change(x, y);
      count();
      player = !player;
    }
  }
}
int main()
{
//  freopen("input.txt", "r", stdin);
  int t;
  scanf("%d\n", &t);
  solve();
  for (int i = 1; i < t; i++)
  {
    printf("\n");
    solve();
  }
}

猜你喜欢

转载自blog.csdn.net/YYecust/article/details/51890787