![]() |
|
|
#1 |
![]() 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]; This is for now, sorry for the english
|
|
|
|
|
|
#2 | |
![]() Join Date: Feb 2008
Posts: 185 (0.10/day)
Thanks: 51
Thanked 26 Times in 26 Posts
|
Quote:
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.
__________________
|
|
|
|
|
|
|
#3 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
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
|
|
|
|
|
|
#4 |
![]() 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. |
|
|
|
|
|
#5 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
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
|
|
|
|
|
|
#6 |
![]() 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);
}
P.S it's in french so if you need anything translated let me know. |
|
|
|
|
|
#7 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
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
|
|
|
|
|
|
#8 |
![]() 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.
__________________
|
|
|
|
|
|
#9 |
![]() 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. |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
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 |