C++ Traffic Jam Issue

Hey guys, could you please help me on this one? I really appreciated the help I received yesterday regarding the homework about the dates.

Here are the instructions:

You will complete the Traffic Jam puzzle so that it runs automatically (no user interaction) showing every step. It will prompt the user for the number of players on the left side and on the right side.

The program starts by showing the initial layout (the '_' is the blank space:)

DCBA_123

and then as the user hits the enter key it shows the layout with the next move (one move at a time).
Sample partial run:.

DCBA_123

DCB_A123

DCB1A_23

DCB1A2_3

DCB1_2A3

etc...

NOTE: I ask you to prompt for number on left and number on right. This means you must dynamically allocate the array.

I wish I can show you my work, as it took me from 6 hours to 2 days to complete it, but unfortunately, the CPPs (that I made) were all corrupted because of a power outage.

Here are the CPPs so far:
CreateO.cpp
CreateX.cpp
ojump.cpp
oMove.cpp
print.cpp
swap.cpp
Last edited on
You will complete the Traffic Jam puzzle
Please describe the puzzle. I'm not familiar with it.
the CPPs (that I made) were all corrupted because of a power outage.

Recreate what you can and then post your work.
Place one square per person on the floor in a straight line with one extra square in the middle.
• Divide the team and have half the members stand on squares to the left and half to the right with one open square in the middle.
• The goal is for members to stay in the same order and end up on the opposite side of the center square.

Legal moves:
• You may only move one person at a time forward to an open square.
You may only pass someone (1 person) from the opposite group onto an open square in any given move.

Illegal moves:
• Cannot pass someone on the same group
• No trading places
• Cannot move backwards
If an individual breaks a rule, the entire team begins again.

Generalized Solution:

X’s start from the right squares wishing to move to the left, and O’s start from the left squares wishing to move to the right.

While all X’s not at the leftmost squares”
For all X’s where the O is in the adjacent square skip left to an empty square.
For a single X where the adjacent square is empty shift left (only one move).

While all O’s not at the rightmost squares”
For all O’s where the XO is in the adjacent square skip right to an empty square.
For a single O where the adjacent square is empty shift right (only one move).
Use the "generalized solution" to sketch out the code. I would keep track of the position of the empty square to help. Change the generalized solution into comments and start adding code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Numbers start from the right squares wishing to move to the left.
// Letters start from the left squares wishing to move to the right.
initBoard();

// While all numbers not at the leftmost squares
while (!allNumbersAtLeft()) {
    // For all numbers where the letter is in the adjacent square skip left to an empty square.
    while (emptyPos < board.size()-2 &&
               isalpha(board[emptyPos+1]) &&
               isdigit(board[emptyPos+2]) {
        // Move number from emptyPos+2 to emptyPos
    }
    // For a single X where the adjacent square is empty shift left (only one move).
    if (emptyPos < board.size()-1 && isdigit(emptyPos+1)) {
        // Move number from emptyPos+1 to emptyPos;
    }

    // While all O’s not at the rightmost squares”
    // For all O’s where the XO is in the adjacent square skip right to an empty square.
    // For a single O where the adjacent square is empty shift right (only one move).
    // This part is similar to above. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
 
int main ( )
{
     std :: cout << "Enter a positive integer: " ;
     int length ;
     std :: cin >> length ;
 
     int * array = new int [ length ] ; // use array new.  Note that length does not need to be constant!
 
// you can then use standard array notation -- remember relationship between pointers and arrays?
 
     array [ 0 ] = 5 ; // set element 0 to value 5
 
     delete [ ] array ; // use array delete to deallocate array
 
     // we don't need to set array to nullptr/0 here because it's going to go out of scope immediately after this anyway
 
     return 0 ;
}
I've already given you some code and comments. See if you can adapt what I wrote to your data structure (or the other way around).
xMove1.cpp
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
#include <algorithm>
#include <iostream>
using namespace std;
void xMove1(char *Array, int Size, int &ExitFlag)
{
	int temp = 0;
		for (int index = 0; index < Size; index++)
		{
			if (*(Array + index) == 'x' && *(Array + index + 1) == '_')
			{
				swap(Array[index], Array[index + 1]);
				for (int index = 0; index < Size; index++)
				{
					cout << *(Array + index) << " ";
				}
				cout << endl;
				cin.get();
				temp++;
			}
		}
		if (temp > 0)
		{
			ExitFlag = 0;

		}
		else
		{
			ExitFlag += 1;
		}
}


xJump2.cpp
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
#include <algorithm>
#include <iostream>
using namespace std;
void xJump2(char *Array, int Size, int &Exitflag)
{
	int temp = 0;

	for (int index = 0; index < Size; index++)
	{
		if (*(Array + index) += 'x' && *(Array + index + 1) == 'o' && *(Array + index + 2) == '_')
		{
			swap(*(Array + index), *(Array + index + 2));
			for (int index = 0; index < Size; index++)
			{
				cout << *(Array + index) << "";
			}
			cout << endl;
			cin.get();
			temp++;
			index = 1;
		}
		if (temp > 0)
		{
			Exitflag = 0;
		}
		else
		Exitflag += 1;
	}
}


Source.cpp
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
#include <iostream>
using namespace std;
void main()
{
	int SetX, Exitflag = 0;
	cout << "Enter a positive integer: ";
	int Size;
	cin >> Size;
	char *Array = new char[Size];

	SetX = CreateX(Array, Size);
	CreateO(Array, Size, SetX);
	CreateEmptySpace(Array, Size);

	for (int index = 0; index < Size; index++)
	{
		cout << Array[index] << " ";
	}
	cin.get();
	cout << endl;
	while (Exitflag < 4)
	{
		xMove1(Array, Size, Exitflag);
		oJump2(Array, Size, Exitflag);
		oMove1(Array, Size, Exitflag);

		xJump2(Array, Size, Exitflag);

	}
	cout << "The End!" << endl;
}


I managed to restore some of the CPPs. That's all I can create.

Still missing:
CreateX.cpp
CreateO.cpp
print.cpp
swap.cpp
xJump1.cpp
xMove2.cpp


Last edited on
That's progress. I'd use Array[index] instead of *(Array+index) because it's easier to read.

Check the limits on your for loops in xJump2() and xMove1(). For example, in xMove1() you access Array[index+1]. That means the end condition of the for loop should be index < Size-1.

Why xMove1() instead of just xMove()?

You should create void printBoard(char *array, int Size) and call it whenever you need to print the board (3 places currently).

I'd create stub implementations for oMove() and oJump(). Then debug to get xMove() and xJump working properly. Then implement oMove() and it get working. And finally implement oJump() and get it working.
Topic archived. No new replies allowed.