Writing a basic game in C++ - Need advice!

Hello guys, I am writing a "reversi" game, and I want to know what ways do you guys recommend to check for the legal and illegal moves for black dots ?


So far, this is my logic.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void set(string input){
  // ... Change the string array and find the right indexs ...
  // Save the indexes as row and column.
  // test the locations around the that spot at row and column around the board.
  
  if (test_moves(row, column) == 1){    // test_moves is a bool function.
     // valid move 
     // print a black dot on the board
  }
  else{
   // invalid move
   // ask again for legal moves.
  }
  print_board();
}


However, my boolean function include very long if statements that keep chechking directions at left, right, top, bottom, and diagonal, with lots of && and || symbols. I feel like I am missing easier approaches to checking for legal moves. Is there any way I can improve my boolean function or is there an easier approach to check for legal moves ?
Last edited on
As I recall, diagonals don't matter.

Put a token down in an empty spot, then its legal if you can trace a line left, right, up, or down from there finding 1 or more opponent tokens followed by 1 of your own tokens. so 4 loops, checking left right up and down.

once you find that it is legal, you can stop checking...

you can probably figure out a way to 'remember' what you did when checking legal when you do the flips... its the same work. For example if "check_north()" returned "not legal" then you don't need to flip the north side tokens if the move was legal after all due to "check_east()" ...

@jonnin

Diagonals DO matter.

@Kourosh23
You have to check if the dot next to your dot, is of the opposite color. Then check if the dot after that is your dot. If it is, switch the opponents dots, to yours. Of course, the can be many of the opponents between the one you just placed, and the end. I have made a 'Reversi' game, but it's almost 19,000 bytes, plus I use color, and cursor control to place the 'stones'.
ok, well same logic, just 4 more checks, 8 instead of 4, each a loop of "1-n of his followed by 1 of mine"

Last edited on
Topic archived. No new replies allowed.