Skip to content

Instantly share code, notes, and snippets.

@byron-perez
Last active February 11, 2018 20:20
Show Gist options
  • Save byron-perez/fd39809e2399beeccfc6d36b6c57b99d to your computer and use it in GitHub Desktop.
Save byron-perez/fd39809e2399beeccfc6d36b6c57b99d to your computer and use it in GitHub Desktop.
ways to give a check
#include <bits/stdc++.h>
using namespace std;
vector<int> findBlackKing(vector < vector<char> > board)
{
int board_len = board.size();
vector<int> king_pos;
for(int x = 0; x < board_len; x++)
{
for(int y = 0; y < board_len; y++)
{
if(board[x][y] == 'k')
{
king_pos.push_back(x);
king_pos.push_back(y);
}
}
}
return king_pos;
}
vector<vector <int> > getRectMoves(vector<int> piece_pos)
{
//a list of positions (moves) along of the straight lines, that a piece can afford
vector<vector <int>> rect_moves;
int board_sz = 8;
for(int i = 0; i < board_sz; i++)
{
vector<int> tmp_pos(2);
tmp_pos = piece_pos;
tmp_pos[1] = i;
if(piece_pos[1] != tmp_pos[1])
{
rect_moves.push_back(tmp_pos);
}
}
return rect_moves;
}
vector<vector <int> > getDiagMoves(vector<int> piece_pos)
{
//a list of positions (moves) that a piece can afford
vector<vector <int>> moves;
}
//return a vector with positions that a given piece can get
vector<vector <int> > getPosibleMoves(char piece, vector<int> piece_pos)
{
//a list of positions (moves) that a piece can afford
vector<vector <int>> moves;
//temporary variable for storing a position
vector<int> position(2);
//proces a piece
switch(piece)
{
case 'K':
//king to left
position[0] = piece_pos[0];
position[1] = piece_pos[1] - 1;
moves.push_back(position);
//king to the right
position[0] = piece_pos[0];
position[1] = piece_pos[1] + 1;
moves.push_back(position);
//king towards bottom
position[0] = piece_pos[0] + 1;
position[1] = piece_pos[1];
moves.push_back(position);
return moves;
case 'Q':
vector<vector <int>> rect_moves;
vector<vector <int>> diag_moves;
//get rect moves
rect_moves = getRectMoves(piece_pos);
int n = rect_moves.size()
//put rect moves on total moves
for(int i = 0; i < n; i++)
{
moves.push_back(rect_moves[i]);
}
//get diagonal moves
diag_moves = getDiagMoves(piece_pos);
n = diag_moves.size();
//put diagonal moves on total moves
for(int i = 0; i < n; i++)
{
moves.push_back(diag_moves[i]);
}
return moves;
cout << piece << endl;
case 'N':
cout << piece << endl;
case 'B':
cout << piece << endl;
case 'R':
cout << piece << endl;
}
return moves;
}
int waysToGiveACheck(vector < vector<char> > board) {
//getting the size of the board (dimensions)
int n = board.size();
//position of a valid pawn to be promoted
vector<int> valid_pawn_pos;
//position of the promoted pawn
vector<int> promoted_pawn_pos;
//position of the king
vector<int> black_king_pos;
//number of ways to give a check
int ways_to_give_a_check = 0;
//find position of valid pawn
for(int i = 0; i < n; i++)
{
if(board[1][i] == 'P')
{
if(board[0][i] == '#')
{
valid_pawn_pos.push_back(1);
valid_pawn_pos.push_back(i);
}
}
}
//fing position of promoted pawn
promoted_pawn_pos = valid_pawn_pos;
promoted_pawn_pos[0] = valid_pawn_pos[0] - 1;
//find position of black king
black_king_pos = findBlackKing(board);
/*find all ways to give a check situation*/
vector <vector <int>> moves_set;
moves_set = getPosibleMoves('K', promoted_pawn_pos);
int moves_set_len = moves_set.size();
for(int i = 0; i < moves_set_len; i++)
{
cout << "(" << moves_set[i][0];
cout << ", ";
cout << moves_set[i][1] << ")";
cout << endl;
}
//cout << "(" << valid_pawn_pos[0] << " " << valid_pawn_pos[1] << ")" << endl;
//cout << "(" << black_king_pos[0] << " " << black_king_pos[1] << ")" << endl;
cout << endl << endl;
return 0;
}
int main() {
int t;
cin >> t;
for(int a0 = 0; a0 < t; a0++){
vector< vector<char> > board(8,vector<char>(8));
for(int board_i = 0;board_i < 8;board_i++){
for(int board_j = 0;board_j < 8;board_j++){
cin >> board[board_i][board_j];
}
}
int result = waysToGiveACheck(board);
cout << result << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment