From 9d4c87881cdb79eb75048d91075b01bdaf1bdb86 Mon Sep 17 00:00:00 2001 From: Xtha-Sunil Date: Wed, 30 Oct 2024 23:36:08 +0545 Subject: [PATCH] Added solution for check if move is legal. --- C++/check-if-move-is-legal.cpp | 51 ++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 C++/check-if-move-is-legal.cpp diff --git a/C++/check-if-move-is-legal.cpp b/C++/check-if-move-is-legal.cpp new file mode 100644 index 0000000..92f4b10 --- /dev/null +++ b/C++/check-if-move-is-legal.cpp @@ -0,0 +1,51 @@ +class Solution { +public: + bool search(vector>& board, int rMove, int cMove, char g_color, int i, int j) + { + rMove += i; + cMove += j; + int turn = 1; + + + // Loop over the path until either condition is certain + while(rMove < 8 && cMove < 8 && rMove >= 0 && cMove >= 0 + && board[rMove][cMove] != '.') + { + if(turn > 1 && board[rMove][cMove] == g_color) {return true;} + else if(board[rMove][cMove] == g_color) return false; + turn++; + rMove += i; + cMove += j; + } + + return false; + } + bool checkMove(vector>& board, int rMove, int cMove, char color) { + // A problem about searching the cells around and checking conditions + // Eight different moving patterns that needs to be checked + // For each pattern we need to ensure that the first next two celles are the + // opposite color. After this, we need to find a same color. + // If no such exist (either boundary conditions or ".") then return false + // OR the results together and return + + // Early termination: + if(board[rMove][cMove] != '.') + { + return false; + } + + + // Loop over all possible combinations of row and column traversal + for(int i = -1; i <= 1; i++) + { + for(int j = -1; j <= 1; j++) + { + if(search(board, rMove, cMove, color, i, j)) + { + return true; + } + } + } + return false; + } +}; \ No newline at end of file