Skip to content

Instantly share code, notes, and snippets.

@WindAzure
Created March 23, 2019 09:36
Show Gist options
  • Save WindAzure/b639e271dc92a4edc564ce072bb48e0c to your computer and use it in GitHub Desktop.
Save WindAzure/b639e271dc92a4edc564ce072bb48e0c to your computer and use it in GitHub Desktop.
UVa 220
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
const char BLACK = 'B';
const char WHITE = 'W';
const char SPACE = '-';
const int EIGHT_DIRECTION_LIST[8][2] = {
{0, 1},
{1, 1},
{1, 0},
{1, -1},
{0, -1},
{-1, -1},
{-1, 0},
{-1, 1},
};
bool isBlackPlaying = true;
char enemyType = WHITE;
char companionType = BLACK;
vector<string> board;
bool checkTypeOfPositionEquals(int row, int col, char type)
{
return 0 <= row && row < 8 && 0 <= col && col < 8 && board[row][col] == type;
}
int rowDirectionOffset(int directionIndex)
{
return EIGHT_DIRECTION_LIST[directionIndex][0];
}
int colDirectionOffset(int directionIndex)
{
return EIGHT_DIRECTION_LIST[directionIndex][1];
}
void switchPlayer()
{
isBlackPlaying = !isBlackPlaying;
swap(enemyType, companionType);
}
void moveToCompanionPosition(int &row, int &col, int directionIndex)
{
auto isEnemyExist = false;
while (checkTypeOfPositionEquals(row, col, enemyType))
{
row += rowDirectionOffset(directionIndex);
col += colDirectionOffset(directionIndex);
isEnemyExist = true;
}
if (!isEnemyExist || !checkTypeOfPositionEquals(row, col, companionType))
{
row = -1;
col = -1;
}
}
int getPosibleMoveDirection(int row, int col)
{
if (checkTypeOfPositionEquals(row, col, SPACE))
{
for (auto directionIndex = 0; directionIndex < 8; directionIndex++)
{
auto enemyRow = row + rowDirectionOffset(directionIndex);
auto enemyCol = col + colDirectionOffset(directionIndex);
moveToCompanionPosition(enemyRow, enemyCol, directionIndex);
if (checkTypeOfPositionEquals(enemyRow, enemyCol, companionType))
{
return directionIndex;
}
}
}
return -1;
}
void eatOtherPlayer(int row, int col)
{
auto directionIndex = getPosibleMoveDirection(row, col);
if (directionIndex == -1)
{
switchPlayer();
directionIndex = getPosibleMoveDirection(row, col);
}
while (directionIndex != -1)
{
auto stRow = row + rowDirectionOffset(directionIndex);
auto stCol = col + colDirectionOffset(directionIndex);
while (!checkTypeOfPositionEquals(stRow, stCol, companionType))
{
board[stRow][stCol] = companionType;
stRow += rowDirectionOffset(directionIndex);
stCol += colDirectionOffset(directionIndex);
}
directionIndex = getPosibleMoveDirection(row, col);
}
board[row][col] = companionType;
}
void makeMove(int targetRow, int targetCol)
{
eatOtherPlayer(targetRow - 1, targetCol - 1);
switchPlayer();
auto black = 0, white = 0;
for (auto row = 0; row < 8; row++)
{
for (auto col = 0; col < 8; col++)
{
if (checkTypeOfPositionEquals(row, col, WHITE))
{
white++;
}
else if (checkTypeOfPositionEquals(row, col, BLACK))
{
black++;
}
}
}
printf("Black - %2d White - %2d\n", black, white);
}
void quitGame()
{
auto outputString = string{};
for (const auto& row : board)
{
outputString += row + "\n";
}
cout << outputString;
}
void listCurrentPlayerPossibleMove()
{
auto possibleMoveString = string{};
for (auto row = 0; row < 8; row++)
{
for (auto col = 0; col < 8; col++)
{
if (getPosibleMoveDirection(row, col) != -1)
{
possibleMoveString += possibleMoveString.empty() ? "" : " ";
possibleMoveString += string{ "(" } + to_string(row + 1) + string{ "," } + to_string(col + 1) + string{ ")" };
}
}
}
cout << (possibleMoveString.empty() ? "No legal move." : possibleMoveString) << endl;
}
void initGame()
{
board.clear();
for (auto i = 0; i < 8; i++)
{
auto rowString = string{};
cin >> rowString;
board.emplace_back(rowString);
}
isBlackPlaying = true;
enemyType = WHITE;
companionType = BLACK;
auto player = string{};
cin >> player;
if (player != "B")
{
switchPlayer();
}
}
int main()
{
auto T = 0;
scanf("%d", &T);
for (auto i = 0; i < T; i++)
{
if (i != 0) puts("");
initGame();
auto command = string{};
while (cin >> command)
{
if (command == "L")
{
listCurrentPlayerPossibleMove();
}
else if (command == "Q")
{
quitGame();
break;
}
else
{
makeMove(command[1] - '0', command[2] - '0');
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment