Prog #2:Trinity Solution

$30.00

Description

Create an interface so two players can take turns choosing from among 9 playing pieces ( (A,B,C,)   (1,2,3)   (a,b,c) ) to try and get 3 pieces of a particular kind in a row in a 3×3 board. Play alternates between the two players.  Pieces can only be used once, and cannot be moved once played. Three in a row of a particular kind can be achieved by any of the following three possibilities:

  1. Three of the same set, namely all three upper-case, all three lower-case, or all three numbers, in any order.
    Examples are:  A B C,  1 3 2,    c b a

  2. Three of the same set position (first, second or third).  In other words A is in the first position of the (A,B,C) set, and 2 is in the second position of the (1,2,3) set.
    Examples are:  A 1 a,    2 b B,    3 C c

  3. One of each set position (first, second or third).  One value must be from the first position of one of the sets, a second value must be from the second position of one of the other sets, and the third value must be from the third position of the remaining set.
    Examples are:  A 2 c,    B 1 c,    a, 3, B

[This game was created on 1/19/16 by Dale Reed and Pat Troy.]

Running your program will look something like what is shown below, where user input is shown in bold:

Program #2: Trinity
Author: Dale Reed
Lab: Tues 5am
Date: January 22, 2016
System:  DevC++ on Windows 10

Welcome to Trinity, the game where there's three ways
to get three in a row, in one of three rows or columns,
or diagonals.  Play alternates between the two players.
On each move select any available piece and an open
board destination location.  A winning configuration
is three of the same set in a row (e.g. b a c, A B C 
or 3 2 1),  all 3 from the same set position (e.g. A 1 a, 
B 2 b or c 3 C), or three in a row where each is a 
different set member (1st, 2nd, 3rd) such as 2 A c,
a B 3 or C 1 b.   
------------------------------------
Pieces available to play:
   A B C
   1 2 3
   a b c 
             
 -----------   Positions:
|   |   |   |    1  2  3
|-----------|
|   |   |   |    4  5  6
|-----------|
|   |   |   |    7  8  9
 -----------
1. Player 1 enter piece and position: A1

------------------------------------
Pieces available to play:
     B C
   1 2 3
   a b c 
             
 -----------   Positions:
| A |   |   |    1  2  3
|-----------|
|   |   |   |    4  5  6
|-----------|
|   |   |   |    7  8  9
 -----------
2. Player 2 enter piece and position: 26

------------------------------------
Pieces available to play:
     B C
   1   3
   a b c 
             
 -----------   Positions:
| A |   |   |    1  2  3
|-----------|
|   |   | 2 |    4  5  6
|-----------|
|   |   |   |    7  8  9
 -----------
3. Player 1 enter piece and position: A2
*** Piece A is not available. Please retry: B6
*** Location 6 is already taken. Please retry: C8

------------------------------------
Pieces available to play:
     B  
   1   3
   a b c 
             
 -----------   Positions:
| A |   |   |    1  2  3
|-----------|
|   |   | 2 |    4  5  6
|-----------|
|   | C |   |    7  8  9
 -----------
4. Player 2 enter piece and position: 
. . .

You need to know the following concepts in order to write this program:

Handling user input and output; Using functions with parameters; How to break down a bigger problem into smaller problems that are more manageable.

Notes / Grading:

  1. Consider writing your program using the following steps, where the number of points for each element is given out of the total 55 points for program execution.

    1. Print out identifying information and instructions

    2. Declare 9 variables for the pieces available to play, initializing them respectively to the characters: A,B,C,1,2,3,a,b,c
      I recommend you call these something simple like p1,p2,p3, …p9
      Modify your program so that after the instructions it also prints out those characters.

    3. Declare 9 more variables to be used to represent the board playing pieces.  I recommend you call them something simple like b1,b2,b3, …b9
      Modify your program so that after printing the pieces, you now also print the 9 board pieces by themselves, in 3 rows of 3.

    4. Modify your program so you also display the border characters around the 9 board pieces

    5. Now also add the position reference numbers to the right of the board

    6. Now read in the user input.  You will need to explicitly read in the return character, otherwise your program will skip input on following turns.  For instance if you declare the pieceToPlay as a char, and the position as a char, you should also declare a variable returnChar as a char.  Then for every line of input you could use something like:
      scanf(“%c%c%c”, &pieceToPlay, &position, &returnChar);     // read user input
      After reading them in, echo them, to make sure that everything is working right

    7. (5 points) Now put a loop around the above code, so inside the loop you display the board and read user input.
      Add a variable for moveNumber, and increment it after each move.  Add a variable for playerToMove.  Use this in the prompt.  This value should alternate between 1 and 2.  
      You must get at least this part working to get any points at all on this assignment.

    8. (5 points) Add error checking code to verify the desired piece is available.  This should be in a loop so that if input is invalid you can use continue to try again.

    9. (5 points) Add error checking code to verify the destination position is not already occupied.  This should be in a loop so that if input is invalid you can use continue to try again.

    10. Now you are ready to start checking for 3-in-a-row winning conditions.  To start with add a single condition for a single location, such as A B C across the top row.
      When this condition is found, your program should exit the loop that otherwise displays the board and prompts for input.  Once this winning condition is found after a user’s move, you should display a message “Congratulations, you win with A B C”.  

    11. Now modify your code so that it accepts not just A B C, but any order of those three letters.

    12. (5 points) Now modify your code so that you test not just the top row, but all three rows, all three columns, and both diagonals.  At this point you should be thinking about how you can write a function (or multiple functions) to make your code more general, so you don’t have to explicitly list all the possible conditions.

    13. (5 points) Now extend your code so that it does the same kind of check for the other two sets (1 2 3 and a b c) as well.

    14. (15 points) Now add the check for winning combinations where all 3 are from the same set position.  In other words, all 3 are the first in their set (A 1 a), the second in their set (B 2 b) or the third in their set (C 3 c).  Note that these can be in any order, as long as all three are there.

    15. (15 points) Add the check for winning combinations where you have one of each set, where each one is from a different position in its set.  One must be the first in its set, one must be the second, and the remaining element must be the third in its set.

Turning In Your Program

The name of the program you will turn in should be prog2 followed by your netid and the .cpp file extension.  In other words, if your netid is reed then your program would be called prog2reed.cpp     You must also zip up the file you turn in.  After zipping your program (e.g. prog2reed.cpp), you will likely end up with a file called something like prog2reed.zip.   Only turn in this single file, turning it in on Blackboard into the assignment Program2: Trinity.  Failing to follow these naming conventions and failure to turn in a zip file will result in a 5 point deduction, even if everything else is perfect in your program. 

Extra Credit

(up to 10 points) Play the game against a smart computer opponent.  Submit this into the “Program 2: Extra Credit” Assignment in Blackboard.  Extra credit program versions may not be submitted late.