~/$ ./p4
Welcome to SWARE!
board> N = 6 : 8 1 6 6 2 4
 #          
 #          
 #   # #    
 #   # #    
 #   # #   #
 #   # #   #
 #   # # # #
 # # # # # #
-------------
 A B C D E F
> swap A B

   #        
   #        
   # # #    
   # # #    
   # # #   #
   # # #   #
   # # # # #
 # # # # # #
-------------
 A B C D E F
> swap E F

   #        
   #        
   # # #    
   # # #    
   # # # #  
   # # # #  
   # # # # #
 # # # # # #
-------------
 A B C D E F
> reverse B F

           #
           #
       # # #
       # # #
     # # # #
     # # # #
   # # # # #
 # # # # # #
-------------
 A B C D E F
8 points! (3 moves)

Swap-and-Reverse Game

This lab will give you practice writing functions dealing with arrays — in this case arrays of ints. The end result, if you finish it all, will be a simple game Swap and Reverse.

As shown on the right, the game starts with a board and by using swap and reverse. The game ends when the player sorts out the bars on the board in increasing order (check the sample run on the right).

Part 1 : Array functions

Write a program (call it p1.cpp) with main() as shown below.

int main()
{
  char c;
  int N;
  cin >> c >> c >> N;
  
  int* A = read(N);
  print(A,N);
  if (isInOrder(A,N))
    cout << "Is in order!" << endl;
  else
    cout << "Is not in order!" << endl;
  delete [] A;
  return 0;
}

Requirement: Implement the following function that you see being used in the code.

~/$ ./p1
N = 4 : 1 4 5 9  
 1 4 5 9
---------
 A B C D
Is in order!
~/$ ./p1 
N = 6 : 1 3 4 4 6 8   
 1 3 4 4 6 8 
-------------
 A B C D E F 
Is in order!
~/$ ./p1 
N = 6 : 1 3 4 4 6 5 
 1 3 4 4 6 5
-------------
 A B C D E F
Is not in order!
The output needs to match the examples shown above.

Submit as:

~/bin/submit -c=IC210 -p=lab07 p1.cpp

Part 2: Swap It!

The examples below ought to show you the basics of how the game functions including a command: swap.
~/$ ./p2
Welcome to SWARE!
board> N = 6 : 8 7 2 4 2 8

 8 7 2 4 2 8
-------------
 A B C D E F
> swap A E

 2 7 2 4 8 8
-------------
 A B C D E F
> swap B C

 2 2 7 4 8 8
-------------
 A B C D E F
> swap C D

 2 2 4 7 8 8
-------------
 A B C D E F
9 points! (3 moves)
~/$ ./p2
Welcome to SWARE!
board> N = 5 : 2 5 3 5 2

 2 5 3 5 2
-----------
 A B C D E
> swip
Unknown move 'swip'

 2 5 3 5 2
-----------
 A B C D E
> swap B E

 2 2 3 5 5
-----------
 A B C D E
3 points! (1 moves)
~/$ ./p2
Welcome to SWARE!
board> N = 6 : 1 3 3 1 4 4

 1 3 3 1 4 4
-------------
 A B C D E F
> swap B D

 1 1 3 3 4 4
-------------
 A B C D E F
3 points! (1 moves)

How the program works

Submit as:

~/bin/submit -c=IC210 -p=lab07 p1.cpp p2.cpp

Part 3: Reverse It!

Build on your Part 2 solution by providing the player with a new move, reverse x y , where x and y are column positions. Column x should come before column y, and the command should reverse the values in cloumns x through y. Like this:
 9 1 2 5 9 8 7 6 4    reverse E H    9 1 2 5 6 7 8 9 4
-------------------  ------------>  -------------------
 A B C D E F G H I                   A B C D E F G H I

Examples are given below:

~/$ ./p3
Welcome to SWARE!
board> N = 8 : 9 2 8 7 6 5 8 1

 9 2 8 7 6 5 8 1
-----------------
 A B C D E F G H
> reverse C F

 9 2 5 6 7 8 8 1
-----------------
 A B C D E F G H
> swap A H

 1 2 5 6 7 8 8 9
-----------------
 A B C D E F G H
5 points! (2 moves)

Submit as:

~/bin/submit -c=IC210 -p=lab07 p1.cpp p2.cpp p3.cpp

Part 4 : Going Further, Let's get graphical

The game is more compelling if the board is printed out "graphically", which for us will mean a sort of "ASCII Art" thing. Specifically, you must produce game boards in the style depicted below.
~/$ ./p4
Welcome to SWARE!
board> N = 5 : 4 3 2 4 3

 #     #  
 # #   # #
 # # # # #
 # # # # #
-----------
 A B C D E
> swap A E

       # #
 # #   # #
 # # # # #
 # # # # #
-----------
 A B C D E
> reverse A C

       # #
   # # # #
 # # # # #
 # # # # #
-----------
 A B C D E
5 points! (2 moves)
Hint: This is made vastly easier by defining a function to find the max element in an array.

Submit as:

~/bin/submit -c=IC210 -p=lab07 p1.cpp p2.cpp p3.cpp p4.cpp