Othello move validation

KyleG
KyleG used Ask the Experts™
on
Hey everyone,
I'm trying to make an othello (reversi) game in c++. For the moment I'm making it for two human players, but eventually it will have a computer player. It runs in DOS, and right now I have it being played on a 6 by 6 board, which is represented in a 2d array. I've been tryin to figure out how to validate and complete a player's move. That is, I need to be able to make sure that their move is valid, place their piece, then flip over the other appropriate pieces (you need to know thw rules of othello to understand this part). I came very close to succeding, but there are a few flaws which I cannot fix, so I've decided to start over. Any and all help is appreciated, including links to websites.

Thanks alot,
Kyle
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior .Net Developer
Commented:
well obiously, checking the row or column, if that works then your set.  The only one you need to worry about is the diagnol.



int
array[size][size];


find the nearest spot on the diagnol:

int searchrup(int array[], x,y,this_players_piece)
{
  int count = 0;
while((x<size) && (y<size))
  {
   x++;
   y++;
   if (array[x][y] == this_players_piece)
    return count-1;
   else count++;
  }
 return 0;
}

int searchldown(int array[], x,y,this_players_piece)
{
 int count = 0;
while((x>0) && (y>0))
  {
   x--;
   y--;
   if (array[x][y] == this_players_piece)
    return count-1;
   else
    count++;
  }
  return 0;
}

// in MAIN, left the paramaters out of functions for space
// purposes.

if (searchrup() && searchldown()&&
    searchlup() && searchrdown()&&
     lookdown() && lookup()  &&
     lookleft() && lookright()     )    
    invalid_move();


array is obviously your board
x,y.  Are the position there trying to insert in.

What count does:  
makes sure there's at least one piece between them. if count is 0, it will be returned and checked as false.  If it finds it is a valid move, it returns count - 1 to tell you the number of pieces you have to flip.  (also good if you want to keep score, blackscore -= temp; whitescore += temp)  If it runs off the board, it's obviously not a valid move, and therefore is illegal, hence the return 0 at the end. I gave you the 2 of the 4 diagnals, the 4  looking ones and the other 2 diagnals shouldn't be that hard.



any other questions?






Kyle AbrahamsSenior .Net Developer

Commented:
ah sorry, in the if statement in main, they should all be
!function() &&

(IE you return 0, you want it to evaluate true.  If it is > 1, it will evaluate true while it's a valid move so you want to make sure it evaluates false.)

Author

Commented:
Thanks alot, that really makes alot more sense to me now. Just one more thing, I'm pretty sure that it will work, but I'd like your opinion on it first: I could set up 8 (one for each direction) if statements to handle flipping the other pieces (older ones) on the main function, right? What I mean is, if, for example, searchrup() returns a value greater than 0, I would then call a similar function which flips the pieces along the path that it just searched. This all makes sense in my head, I hope it does in yours :)

Thanks alot,
Kyle
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Author

Commented:
One more thing. I know how to fix it, but I thought I'd point it out for clarity's sake, just incase you hadn't realised it. I hope I don't seem like a jackass pointing out your mistakes! Your code didn't check to see if, while following a certain path, it encountered an empty square. Obviously, if the program just went until it hit a another of the current player's pieces, I could possibly fill in previously blank squares.

Kyle
Kyle AbrahamsSenior .Net Developer

Commented:
LOL, that's why we have the website, to point out each other's mistakes.  You are correct.  At first I was pseudo-coding it, then I just jumped into the code and started writing and did it on the fly.  So in terms of not checking for blanks, yes, you are correct.  (Easily taken care of by adding another conditional to the while loop:
(x > 0; y > 0; !space_is_empty());

and yeah, you could have 8 if statements for flipping purposes; in fact you can have all if statements in side a big if statement if you really wanted to get into it.  (I don't recommend it, it's not pretty, not really good programming, and confusing if you ever went back to it or wanted to show anyone)  

But incase you wanted to make it over difficult

you could call

if (flip(searchrup() && flip(searchldown() . . .)

inside flip your first statement would be

int flip(int numsquares,int direction)
 if (numsquares == 0)
  return 0;
 else
  switch(direction)
   case 0:{do your flipping; break}
   
 return 1;

The direction would be a number by you to know which way to flip, just correspond it to whatever you send, or you can enum it.

Hope that makes sense.  If this works to your satisfaction please accept answer and grade to close the question, or keep firing away at the questions and I'll knock em down as best I can. :-D  Enjoy.


Author

Commented:
Alright, thanks alot, you've answered all of my questions, and without makin them overly complicated answers! There aren't many people that I know that make things simple the way you did, so thanks alot! Here are your points, enjoy them :)

Kyle

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial