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).
Registered users can post here. Sign in or register to post.