• We are currently performing site maintenance, parts of civfanatics are currently offline, but will come back online in the coming days. For more updates please see here.

How do I do this in C++

Narnia

Prince
Joined
Nov 19, 2009
Messages
513
I'm taking C++ for a class and as an exercise for that class I'm making a version of the game Pente. Everything is going fine except that I am drawing a blank on how to program a move in the game. The move is called capturing. If player 1 is X, player 2 is O, and + is nutural then a capture occurs player 1 moves thus: +OOX to XOOX. When this happens, the two Os in the center revert to +s and player 1 gets a point like thus:X++X. However a player cannot capture their own pieces meaning that in the situation that player 2 moves like so: XO+X to XOOX, nothing happens. This move can be done across either a diagnonul, vertically, or horizontally. I REALLY DON'T want to write out every possible occurrence that this could occur at so I need to figure out a mathematical formula or rule as a shortcut. Any ideas? Thanks :) Also, if anyone notices a bug in this, PLEASE tell me.
Included is the C++ files.
PS: This is an exercise so please do not do this for me; I only need someone to point me in the right direction so that I can do this for myself.
PS: This is NOT a graded assignment.

If you have any questions, please ask
 

Attachments

I set up a C++ group here:

http://forums.civfanatics.com/group.php?groupid=276

Is the game reversi?

It sounds like it. You need to set up a loop which you do in all 8 directions looking for opponent pieces until you find one of your own pieces, then capture all of those. Break out of the loop if you find a blank. Keep count of how many pieces you capture.

Watch out for the edges.

Write a function that takes the x, y directions to check and your play position and call it 8 times with the following (direction) parameters:

x: 1 y: 0
x: 1 y: 1
x: 0 y: 1
x: -1 y: 1
x: -1 y: 0
x: -1 y: -1
x: -1 y: 0
x: -1 y: 1

Can give more tips but not until tomorrow when I am less drunk ;)
 
I set up a C++ group here:

http://forums.civfanatics.com/group.php?groupid=276

Is the game reversi?

It sounds like it. You need to set up a loop which you do in all 8 directions looking for opponent pieces until you find one of your own pieces, then capture all of those. Break out of the loop if you find a blank. Keep count of how many pieces you capture.

Watch out for the edges.

Write a function that takes the x, y directions to check and your play position and call it 8 times with the following (direction) parameters:

x: 1 y: 0
x: 1 y: 1
x: 0 y: 1
x: -1 y: 1
x: -1 y: 0
x: -1 y: -1
x: -1 y: 0
x: -1 y: 1

Can give more tips but not until tomorrow when I am less drunk ;)




Thanks. Ok, I think I know how to do this now. I uploaded what I have so far. I meant for this to say that when board[1] is X, and board[2] and board[3] are Os and player x places an X at board[4] (this will show up as XOO+ to XOOX on the gameboard), the values of board[2] and board[3] should change from 'O' back to their original '+'. The code I inputed is in spoilers below. Everything works except that the Os don't turn into +s. Can anyone see where my mistake is? Thanks
Thanks
Spoiler :
if (board[move] == board[move+3] &&board[move+2]==board[move+1]&&board[move+1]=='O')//capture function
{
board[move+1]='+';
board[move+2]='+';
points1++;
}
 

Attachments

I took a glimpse of the source code, and noticed these problems:

1) Array index in C/C++ is zero based. For your board[400] the first element is board[0], and the last is board[400 - 1].

2) void main() is not the correct way to define the main function. It works (only because the compiler lets you), but it's not proper. The correct way is:

int main(int argc, char** argv) where argc is the array size of argv, and argv is the array commandline parameters.

Or if you don't need commandline parameters,

int main()

3) You spent 361 lines to initialize the array, one element at a time. Same for showboard() function. You'll have carpal tunnel syndrome before you finish coding the game.

It's time for you to learn about iterations. Try CPlusPlus.com:
http://www.cplusplus.com/doc/tutorial/. Particularly, the "Control Structures" section, but I'd recommend reading from the start.

4) The game is not reversi... reversi board is 8x8. Go uses 19x19 board, but Go doesn't capture opponent tokens like that. Or is it a new game you designed? If so you should tell us about all the rules - Are there disallowed moves? Must a new token be placed next to existing ones? If you can capture anything can you still make that move? How to determine if game has ended? How to determine who won? etc.

5) For status flag with only two possible values (e.g. turn), you can use a bool instead.

6) Line 411 to 417. It seems your code currently only deals with a board with a single line (from the comment //v3.0 cordinate 20x20 grid with center point as 0,0), but even so those lines only check 3 moves in a single direction. You'll be able to write a new algorithm, after learning iteration.

7) For the token-capturing algorithm, first explain to us the rules of the game. Then write pseudo code - think about how you can tell someone with an IQ of 1 do it (i.e. you have to tell them literally everything, no skipping steps).
 
1 and 2) I'm about halfway through my first semester of C. I don't know very much about programming. Most of what I know I learned fiddling with excel and writing programs on a ti84 calculator. Every project I've done so far used void main () as the main function. I was not aware that this was not necessary.

3) I realized this problem before I wrote the program and figured out a shortcut. I used word and excel to write this for me. I just copy pasted it.

4) Rules explaned in 7p1.

5) I didn't think of that. Thanks

6p1) Right now I'm figuring out how to have it check for a capture in one direction only. After I figure that out I can program in the other 7 directions. This is why it only has provisions for one direction. Also, the 4th location is the move it's self. Because a capture can only occur at the end of your turn and you can only capture enemy pieces, I figured
6p2) About Comment v3.0 Eventually, I plan to change the display of the program so that it shows up showing a coordinate plane so that you enter x,y coordinates instead of assigning numbers to each square like it currently does. the 20x20 grid refers to the fact that I can't remember if pente is played on a 19x19 or 20x20 board and I'll have to fix this.
7p1)Sorry, I think that the game is called Pente. I designed it with a 400 because I could not for the life of me remember if it was played on a 20x20 game board or 19x19. The physical version of the game is played on a graph, 19 lines wide by 19 lines tall (it could be 20 but I can't remember and I've never played a game where this mattered). Each turn you place one token on the board at the intersection of any two lines. You cannot place a token on an already occupied intersection. You then wait for your opponent to take his turn. There are two ways to win. 1 is to get five tokens in an uninterrupted vertical, horizontal, or diagonal line. The other is to capture five pairs of the enemy's tokens. You capture a token by "surrounding" an enemy pair. This means that your opponent has two pieces next to each other and you then place one of your pieces on either side (i.e. xoox). You cannot capture yourself. If by placing a token you complete several captures, all pairs are taken. This means that if player 1 is x and player 2 is o, the board is like so xo+x (+ is neutral) and it is player 2's turn, if player 2 moves like so xoox, then he is safe and nothing happens. You can only capture an enemy's piece. You can place a token in any unoccupied intersection. You can only capture two tokens that are side by side. You can't capture a single piece and you can't capture 3+ pieces that are in a row, only 2. You can however capture in multiple directions (see below)
7p2) My original plan was that after each player moved, the program would check in all eight directions around the move to check to see if a capture was made. It would do this by checking if the value of the the intersections 1 and 2 places to the right of it equal to O's and if the intersection 3 spaces to the right was an X. If both were accurate, this would mean that one of the 8 different ways a capture could be made. If this condition is found to be true, the two intersections to the right of the location would revert back to their original +s. It would also add +1 to a counter for each player that represented the number of captures each player had made. If this is not true then the move would continue as normal and nothing special would happen.

I am doing this for three reasons. 1, I like pente but I don't have a pente set and I'm broke so I can't just buy one. 2. I wanted to experiment with c++ to reinforce what I'd learned in the class and to learn something new. 3. I enjoy C++. I find it relaxing and I'm treating it like a hobby so I don't care if I have to completely re-write sections of code to make changes.



An example of capturing in multiple directions
x++x
o+o+
oo++
+oox

to:
x++x
o+o+
oo++
xoox
Player x captures three sets (vertical, diagonal, and horisontal)
Thus at the end of player x's turn:
x++x
++++
++++
x++x
 
Oops, sorry, I just assumed "Pente" is a word from foreign language. I've played the game before but I didn't know its name.

The capturing algorithm is identical to that of reversi though - the tokens must match this pattern O(Xn)O (for n X in between, bolded O is current move).

So you start from the current move, which is color1.
Then you move in the direction one step at a time, looking for color2.
If you reach the edge, meet a blank space or color1, capture is impossible.
Once you have found the first color2, you switch to look for the next color1.
If you reach the edge, or meet a blank space, capture is impossible.
If next color1 is found, all pieces in between are captured.

Put that in a function, and apply to all eight directions... and for both players.
 
Oops, sorry, I just assumed "Pente" is a word from foreign language. I've played the game before but I didn't know its name.

The capturing algorithm is identical to that of reversi though - the tokens must match this pattern O(Xn)O (for n X in between, bolded O is current move).

So you start from the current move, which is color1.
Then you move in the direction one step at a time, looking for color2.
If you reach the edge, meet a blank space or color1, capture is impossible.
Once you have found the first color2, you switch to look for the next color1.
If you reach the edge, or meet a blank space, capture is impossible.
If next color1 is found, all pieces in between are captured.

Put that in a function, and apply to all eight directions... and for both players.
I'm currently using the below lines of code. I've only programed one direction so far, have not entered the exception of if the piece is at an edge, and have only entered it for player 1. The program runs but when I test it, player 1 captures a pair but the capture doesn't happen, the O's remain when they should have turned back into +. At the beginning of each turn the program asks the player to move and this move is recorded to int move. Any ideas as to where my mistake is?
Spoiler :
if (board[move] == board[move+3] &&board[move+2]==board[move+1]&&board[move+1]=='O')//capture function
{
board[move+1]='+';
board[move+2]='+';
points1++;
}
turn = 2;
break;



Oops, sorry, I just assumed "Pente" is a word from foreign language. I've played the game before but I didn't know its name.
There are several variation of the game and they have different names.
 
Any ideas as to where my mistake is?
Spoiler :
if (board[move] == board[move+3] &&board[move+2]==board[move+1]&&board[move+1]=='O')//capture function
{
board[move+1]='+';
board[move+2]='+';
points1++;
}
turn = 2;
break;

Maybe the error is that your test case has a different expected result than what your code does...

Here's the output I get (Pente 2.0).
Spoiler :
P1 name
a
P2 name
b
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
a it's your turn
type in cordinet of move or enter 0 to acnoledge loss
4
+++X+++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
b it's your turn
type in cordinet of move or enter 0 to acnoledge loss
2
+O+X+++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
a it's your turn
type in cordinet of move or enter 0 to acnoledge loss
9 (ED: Making an out-of-the-way move that doesn't affect the test)
+O+X++++X++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
b it's your turn
type in cordinet of move or enter 0 to acnoledge loss
3
+OOX++++X++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
a it's your turn
type in cordinet of move or enter 0 to acnoledge loss
1
X++X++++X++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++


The code does what it is programed to do: If (Piece in 3 positions to the right and current move equals 'X') and (Pieces in 1 and 2 positions to the right equals 'O'), change pieces in 1 and 2 positions to the right to '+'.

You've successfully made changes to the array. To proceed, you *must* learn to use iterations first... in this particular case, for loop.

Otherwise, how many if conditions are you going to write? You have at most 19 positions in a single direction and that's a lot of combination to hard code.
 
Maybe the error is that your test case has a different expected result than what your code does...

Here's the output I get (Pente 2.0).
Spoiler :
P1 name
a
P2 name
b
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
a it's your turn
type in cordinet of move or enter 0 to acnoledge loss
4
+++X+++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
b it's your turn
type in cordinet of move or enter 0 to acnoledge loss
2
+O+X+++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
a it's your turn
type in cordinet of move or enter 0 to acnoledge loss
9 (ED: Making an out-of-the-way move that doesn't affect the test)
+O+X++++X++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
b it's your turn
type in cordinet of move or enter 0 to acnoledge loss
3
+OOX++++X++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
a it's your turn
type in cordinet of move or enter 0 to acnoledge loss
1
X++X++++X++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++
+++++++++++++++++++


The code does what it is programed to do: If (Piece in 3 positions to the right and current move equals 'X') and (Pieces in 1 and 2 positions to the right equals 'O'), change pieces in 1 and 2 positions to the right to '+'.

You've successfully made changes to the array. To proceed, you *must* learn to use iterations first... in this particular case, for loop.

Otherwise, how many if conditions are you going to write? You have at most 19 positions in a single direction and that's a lot of combination to hard code.

:wallbash: I AM SUCH AN IDIOT!!! Thank you so very much! I've been looking at that code for so long trying to figure out where my mistake is. My mistake was that I was giving the imput in the exact opposite way, imputing the left x first instead of the right x which I had not yet entered into my code. Thank you so very much for your help :) I can't believe that I didn't think of that.
 
Back
Top Bottom