# Coding reversi in C++



## Swiftle (Oct 29, 2009)

After 1 month of studying if,else and for we must write a reversi game in C++ (no AI, no fancy graphics). My knowledge about C at the moment includes conditions, cycles,vectors and matrices and function calls. I'm not asking for the solution code but I do have some questions about this game.

To begin we have 6 functions which we call from our main. One of them tests if we can flip the oponent's token: asuming we play black and we place our token on (3,4) the function checks if there is another black token on line 3, on row 4 or on the diagonals. Now here is my first question. The way I see things I must go in 8 directions to check if there is a token of my color which for me are 8 tests not to mention that the token can't be on an adjacent case (I know it doesn't make alot of sense but if you play the game you'll see what I'm talking about). Is there a simple way to search for another token?

Next thing is the actual board. It's initialized as a char 


```
char board[8][8];
```

that normally is filled with "v" for blank spaces and "x" and "o" for tokens. I tried to do that on a but when I try to fill a matrix like this with X-es I get and invalid conversion from `const char*' to `char' .

This is for now, sorry for the english


----------



## qamulek (Oct 29, 2009)

> that normally is filled with "v" for blank spaces and "x" and "o" for tokens. I tried to do that on a but when I try to fill a matrix like this with X-es I get and invalid conversion from `const char*' to `char' .



My guess is you're trying to do something like

board[0][0]="x";
rather then
board[0][0]='x';

The "x" is a string with a termination character while the 'x' is just a character.


----------



## Kreij (Oct 29, 2009)

Hi Swiftle.

First, I see you have only 8 posts, so welcome to TPU 

Second, it would help if you post the code you currently have so we can see a little more of what you are trying to do.


----------



## Swiftle (Oct 29, 2009)

Ah yes the 'x' does the job thank you !

Kreij, the code is a mess I haven't finished a single function, at the moment I'm thinking about the actual "algorithms" of the different functions, because I really have no idea where to begin.


----------



## Kreij (Oct 29, 2009)

Well we're all here to help if you want/need it.
Code being a mess is common for all of us at times. Don't be bashful. 

PS... Having no idea what we are doing at the start of a project is not that uncommon either.
PSS.. Having no idea what we are doing in the middle of a project happens too. lol


----------



## Swiftle (Oct 29, 2009)

Ok then  so here is 1 of the 6 functions we must use. This on checks if the selected case is next to a token. 


```
bool peutJouer(int i, int j, char jeu[MAX][MAX])
{
	bool test1 = (jeu[i-1][j-1] != 'v') or (jeu[i-1][j] != 'v') //test si les cases
			  or (jeu[i-1][j+1] != 'v') or (jeu[i][j-1] != 'v') //voisines sont
			  or (jeu[i][j+1] != 'v') or (jeu[i+1][j-1] != 'v') //ocupees
			  or (jeu[i+1][j] != 'v') or (jeu[i+1][j+1] != 'v');
	bool test2 = (jeu[i][j]=='v'); //test si la case est deja occupee
	
	return (test1 && test2);
}
```

Test1 checks all the adjacent cases for values deifferent than v because for this project we use v for empty case. The second test test if the selected case is already occupied. The worry I have is what will happen if I take i or j somewhere on the edges of the board. 

P.S it's in french so if you need anything translated let me know.


----------



## Kreij (Oct 29, 2009)

I would run test 2 first and immediately return if it is an invalid move (space occupied).
Then run test 1 as a "for" loop on all the surrounding spaces , checking first to see if it off the board.


----------



## qamulek (Oct 30, 2009)

Reduce all those or's into several if commands so that you can make special cases if i=0 or j=0 or i=(MAX-1) or j=(MAX-1).

For instance jeu[i-1][j-1] would make no sense if i=0 or j=0, however you can make the board matrix padded by a border of 1 on either side so that you don't have to worry about special cases.  For a padded matrix of board[MAX+2][MAX+2] the valid moves would have to be for i or j between 1 and MAX.


----------



## Swiftle (Oct 30, 2009)

Morning  I gave your ideas some tought and the only thing I see is 8 IF's 4 for the edge cases (i,j = 0; i,j = MAX-1) and then four more for jeu[0][0],[0][MAX-1],[MAX-1][0],[MAX-1][MAX-1].

EDIT. I think I got it. I need to increase the size of my matrix from [8][8] to [10][10] so even if the player's input is on the "edge" the test can still be performed without any extra conditions.


----------

