Changing values of an array

.
Last edited on
I must be missing something.

 
pegs[y][x] = '.';
. Please delete this thread, thanks : )
Last edited on
Ok, never delete your post!!

Anyway, I saw your post and decided to code it, based on what I thought you meant with your explanation.

From what you said, I got this:
You wanted to know how to change the array when a user makes a jump on a peg game. You would be jumping 2 spaces, i.e.
Your player is at x2, y2.
You jump right and land at x4, y2


Your question was how do you change the array, so that if there were a peg in between the the start of the jump position and the land position.

Here's what I coded:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>

//constant for array
const int MAX = 5;

enum direction
{
	UP,
	RIGHT,
	DOWN,
	LEFT
};

void pegCheck( direction dir, char grid[][ MAX ], int &x, int &y );
void viewGrid( char grid[][ MAX ] );

int main()
{
	char grid[ MAX ][ MAX ];

	//init to 'o' for each element
	for( int i = 0; i < MAX; ++i )
	{
		for( int j = 0; j < MAX; ++j )
			grid[ i ][ j ] = 'o';
	}

	//create an enum of direction
	direction dir = RIGHT;

	int curPosX = 2;
	int curPosY = 2;

	//set the players char
	grid[ curPosX ][ curPosY ] = '*';

	//set x3, y2 to be a peg, so that
	//a right jump will jump over a peg
	grid[ curPosX + 1 ][ curPosY ] = '.';

	//view before jump
	viewGrid( grid );

	//get input for the direction...
	/* code */
	//Get the direction from the user

	pegCheck( dir, grid, curPosX, curPosY );

	//view after jump
	viewGrid( grid );

	return 0;
}


void pegCheck( direction dir, char grid[][ MAX ], int &x, int &y )
{
	switch( dir )
	{
		case UP:
				/* code */
				break;

		case RIGHT:
				//erase players current position
				grid[ x ][ y ] = 'o';

				//jump over one and land on second
				x += 2;

				//draw players current position
				grid[ x ][ y ] = '*';
				
				//if the between location is a peg
				if( grid[ x - 1 ][ y ] == '.' )
				{
					/* maybe add points, or what ever you have planned */

					/* reset the peg to a space... */
					grid[ x - 1 ][ y ] = 'o';
				}

				break;

		case DOWN:
				/* code */
				break;

		case LEFT:
				/* code */
				break;
	}
}

void viewGrid( char grid[][ MAX ] )
{
	//view grid
	for( int i = 0; i < MAX; ++i )
	{
		for( int j = 0; j < MAX; ++j )
		{
			std::cout << grid[ j ][ i ];
		}
		std::cout << '\n';
	}

	std::cout << '\n';
}
Why do you need to delete your post for?

It' really awful attitude. After all if you are only interested in solving your problem find another place and don't post here. The proposed solution might help others also you know. You just ask for help but you don't seem to care to provide some. That's selfish you know. If I were you you I wouldn't help you to be honest
Last edited on
OP:
rej3kt wrote:
I'm basically creating a game, peg solitaire, where the board is a sort of cross and you jump over pegs to eliminate them.

I've got my grid sorted with a 2d array (o's for pegs and . for blank space).

I've got a function which draws the grid, and I ask the user to input two numbers to represent co-ordinates of the array, saved as "selection1 and selection2", then I ask the user to input the direction they want to move their peg, N,E,S,W.

My problem is, I can't quite figure out how to tell the program to eliminate the peg that is jumped over, for example peg 3,4 moving west would jump over another peg and into the centre of the board. What would be the easiest way of doing this, I was thinking a switch statement for N,E,S,W and some statement saying something like for north: row - 2 or column -2 for the array, not entirely sure how to change the value (row + column are the names of the two parts to the array). Thanks for any help, I know it sounds confusing.
Topic archived. No new replies allowed.