techPowerUp! Forums

Go Back   techPowerUp! Forums > Software > Programming & Webmastering

Reply
 
Thread Tools
Old Oct 29, 2009, 09:43 PM   #1
Swiftle
25 Posts
 
Swiftle's Avatar
 
Join Date: Jul 2009
Posts: 27 (0.02/day)
Thanks: 1
Thanked 1 Time in 1 Post

Coding reversi in C++

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

Code:
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
Swiftle is offline  
Reply With Quote
Old Oct 29, 2009, 10:03 PM   #2
qamulek
75 Posts
 
qamulek's Avatar
 
Join Date: Feb 2008
Posts: 185 (0.10/day)
Thanks: 51
Thanked 26 Times in 26 Posts

Quote:
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.
__________________

avatarsuperduper[youtube]: ..."stop lieing feel ur self owned!"
qamulek is offline  
Reply With Quote
Old Oct 29, 2009, 10:04 PM   #3
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

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.
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
Old Oct 29, 2009, 10:07 PM   #4
Swiftle
25 Posts
 
Swiftle's Avatar
 
Join Date: Jul 2009
Posts: 27 (0.02/day)
Thanks: 1
Thanked 1 Time in 1 Post

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.
Swiftle is offline  
Reply With Quote
Old Oct 29, 2009, 10:14 PM   #5
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

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
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
Old Oct 29, 2009, 10:25 PM   #6
Swiftle
25 Posts
 
Swiftle's Avatar
 
Join Date: Jul 2009
Posts: 27 (0.02/day)
Thanks: 1
Thanked 1 Time in 1 Post

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

Code:
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.
Swiftle is offline  
Reply With Quote
Old Oct 29, 2009, 10:48 PM   #7
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

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.
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
Old Oct 30, 2009, 02:53 AM   #8
qamulek
75 Posts
 
qamulek's Avatar
 
Join Date: Feb 2008
Posts: 185 (0.10/day)
Thanks: 51
Thanked 26 Times in 26 Posts

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.
__________________

avatarsuperduper[youtube]: ..."stop lieing feel ur self owned!"
qamulek is offline  
Reply With Quote
Old Oct 30, 2009, 09:30 AM   #9
Swiftle
25 Posts
 
Swiftle's Avatar
 
Join Date: Jul 2009
Posts: 27 (0.02/day)
Thanks: 1
Thanked 1 Time in 1 Post

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.

Last edited by Swiftle; Oct 30, 2009 at 09:43 PM.
Swiftle is offline  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to deal with coding dillemnas Kreij Programming & Webmastering 0 Oct 14, 2009 03:16 AM
Need coding help, willing to pay SkyKast Programming & Webmastering 16 Jun 22, 2009 12:29 PM
ICMP coding question for Utility Kreij Programming & Webmastering 11 Jul 25, 2008 10:22 PM
Music while coding Jizzler Programming & Webmastering 2 Oct 30, 2007 12:43 PM


All times are GMT. The time now is 08:47 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
no new posts