Code:
#include <vector>
#include <iostream>
#include <algorithm>
#include <utility>
#include <stack>
using namespace std;
#define SIZE 4
vector<pair<int,int> > get_all_flips(vector<vector<int> >& board, int r, int c, int color){
vector<pair<int,int> > rv;
int rr,cc;
int opposite = color^3;
//N,NE,E,SE,S,SW,W,NW
int drs[]={-1,-1, 0, 1, 1, 1, 0,-1};
int dcs[]={ 0, 1, 1, 1, 0,-1,-1,-1};
for(int qq=0;qq<8;++qq){
int dr = drs[qq];
int dc = dcs[qq];
rr=r+dr;
cc=c+dc;
if(board[rr][cc] == opposite ){
stack<pair<int,int> >stk;
while( rr >= 0 && cc >= 0 && rr < SIZE && cc < SIZE &&
board[rr][cc] == opposite ){
stk.push(make_pair(rr,cc));
rr+=dr;
cc+=dc;
}
//fallen off board, hit blank, hit my own color
if( rr >= 0 && cc >= 0 && rr < SIZE && cc < SIZE &&
board[rr][cc] == color ){
while(!stk.empty()){
rv.push_back( stk.top() );
stk.pop();
}
}
}
}
return rv;
}
bool is_valid_move(vector<vector<int> >& board, int r, int c, int colour){
if( get_all_flips(board,r,c,colour).empty())
return true;
else
return false;
}
void print_board(vector<vector<int> >& board){
cout << " ";
for(int i=0;i<SIZE;i++)
cout << " " << i ;
cout << "\n";
for(int i =0; i<SIZE; ++i){
cout << i << " ";
for(int j=0;j<SIZE;j++){
if( board[i][j] == 0 )
cout << "| ";
else if(board[i][j] == 1 )
cout << "|O" ;
else
cout << "|#";
}
cout << "|\n";
}
}
bool can_someone_move(vector<vector<int> >&board){
for(int i=0;i<SIZE;++i){
for(int j=0;j<SIZE;++j){
if( board[i][j] == 0 )
return true;
}
}
return false;
}
int blkCount(vector<vector<int> >&board){
int blackcount = 0;
for(int i = 0; i<SIZE; ++i)
for(int j = 0; j<SIZE; ++j){
if(board[i][j] == 2)
blackcount++;
}
return blackcount;
}
int whtCount(vector<vector<int> >&board){
int whitecount = 0;
for(int i = 0; i<SIZE; ++i)
for(int j = 0; j<SIZE; ++j){
if(board[i][j] == 1)
whitecount++;
}
return whitecount;
}
int main(int argc, char* argv[])
{
do{
vector<vector<int> > board;
vector<pair<int,int> > rv;
int temp;
board.resize(SIZE);
for(int i=0;i<SIZE;++i)
board[i].resize(SIZE);
board[SIZE/2][SIZE/2] = board[SIZE/2-1][SIZE/2-1] = 1;
board[SIZE/2-1][SIZE/2] = board[SIZE/2][SIZE/2-1] = 2;
int turn=1;
while( can_someone_move(board) ){
print_board(board);
//Shows score of the game.
//int blkCount = 2;
//int whtCount = 2;
int blackcount = blkCount(board);
int whitecount = whtCount(board);
cout << "Black Count is: " << blackcount << endl;
cout << "White Count is: " << whitecount << endl;
if(turn == 1){
cout << "Player 1 Move (row column)? ";
//cout << "" << endl;
}
else{
cout << "Player 2 Move (row column)? ";
//cout << "" << endl;
}
int r,c;
cin >> r >> c;
if( cin.fail() ){
cout << "BAD!\n";
continue;
}
//off the board or space is used
if( r < 0 || c < 0 || r >= SIZE || c >= SIZE || //!!!!!!!!!!!!
board[r][c] != 0 || !is_valid_move(board,r,c,turn) ){ //This code is causing problem
cout << "You're stupid! Try Again!\n";
continue;
}
//determine which pieces to flip
temp = board[r][c];
board[r][c] = turn ^ 3;
rv = get_all_flips(board, r, c, turn ^ 3);
//flip them
if(!rv.empty()){ //if rv returns empty there is nothing to flip so move is invalid.
for(int i = 0; i<rv.size(); i++ ){
board[rv[i].first][rv[i].second] = turn^3;
}
}
else{
cout << "Invalid Move, Choose Again." << endl;
board[r][c] = temp;
continue;
}
turn = turn ^ 3;
cout << "" << endl;
}
}while(1);
};