You are here:

C++/Help turning psuedo code into c++ code

Advertisement


Question
QUESTION: Alright I am trying to write a program that will play Dots and Boxes, the common paper and pencil game. I have wrote a psuedo code algorithm for this program. My problem is that I do not know where to begin with the the "Game" function and was wondering if you could help me out? I know that to print grid, you have to create a set of 2-d arrays, one called rows and the other called columns but could you guide me on how to write to function into c++ code. Thanks very much.

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int preGame();
void Game();
void postGame();

int main(int argc, char *argv[])
{
preGame();
//This function will ask the user to input a number and returns a grid size for that input if the number is legal.
Game();
//This function will print the current grid and score after each turn. It will also obtain and apply the move.
postGame();
//This function will print the finish grid score, final score, and the farewell message.
system("PAUSE");
return EXIT_SUCCESS;
}

int preGame ()
{
  int input;

  while(input < 2 || input > 9)
  {
    cout << "What size grid would you like? (2..9) ";
    cin >> input;
    
    if( input < 2 || input > 9)
    cout << "\nTry again" << endl << endl;
    else
    break;
     
  }//end of while loop
    return input;
}

void Game()
{
int input;

while (game is not finished)
{
Printing the grid
Priniting the score
//Obtaining the move
cout << "\nEnter your next move";
cin >> input;
remove non-alphanumeric characters from move
//Applying the move
Check if the move is llegal
 if(Both dots are one edge apart with that edge is open && is within legal range of grid)
  if ( move =='Q')
    break to postgame phase
  else if(letter is first)
   apply vertical move
  else
   apply horizontal move
}
}

void postGame()
{
Print finished grid
Print final score
//Print farewell message
 if(Human score > Computer score)
 {
  cout << "\nCongratulations!" << endl;
  return EXIT_SUCCESS;
 }
 else if(Human score < Computer score)
 {
  cout << "\nBetter luck next time." << endl;
  return EXIT_SUCCESS;
 }
 else
 {
  cout << "\nI think we need a rematch." << endl;
  return EXIT_SUCCESS;
 }
}

ANSWER: Hi Peter

Itís an interesting problem, and I think you made a good start, but you have to continue refining your pseudo code until it is closer to what can be implemented in a machine. Many of your pseudo code lines can be turned into functions which will hide the lower level details.

For example, you might have a function called isMoveLegal, taking the starting and ending point of a move. Immediately we are faced with the question of  how to represent a point. Lets use the traditional co-ordinate representation of x1,y1 for the starting point and x2, y2 for the ending point. Deciding on the data representation lets us pin down the meaning of a legal move. A move is legal if:
(x1, y1, x2, y2) are all  >= 0 and all < grid size
and ( |x1-x2| = 1 and y1 = y2 or |y1-y2| = 1 and x1 = x2 )
and moveIsAvailable(x1,y1, x2, y2)

Clearly the first part is easily translated into code, but to determine if a move is available, you will need to develop a data representation which will allow you to answer the question. How about having a two dimensional array where each element is a structure with Boolean elements north, south, east, and west. A connection between elements in a particular direction is indicated by a setting the direction flag to true. So, if you are trying to connect from x, y, to x+1, y, and all the above tests pass, then finally check if the east flag is false, and if it is false, the move has not been made yet. Notice that if you are setting the east flag of one point, you will have to set the west flag of the opposite point because if A is connected to B, then B is connected to A.

Once you make a move from point A to point B, you will need to decide if a box is made. The connection flags help you here too. If going from A to B is going east, then you have 2 possible boxes. You have a box if from B you can go south, then west, then north. You have a box if from B you can go north, then west, then south.

Writing your program comes down to deciding which questions need to be answered, and organizing your data so that the questions can be answered easily.

I hope that gets you started. I assume for now you are going to use the computer as a referee for two human players. It would be really interesting to write a program with the computer as a player.

Best regards
Zlatko

---------- FOLLOW-UP ----------

QUESTION: How would you go about coding an extra row index containing numbers and an extra column index containing letters? I also swithched the numbers with the letters and want it to print out like the example below. Could you help me out?

EX:

a + + + +
      
b + + + +
        
c + + + +
        
d + + + +
 1 2 3 4


#include <iostream>
#include <string>

using namespace std;

int main()
{
   char grid[15][15];
   int row, col;
   int size;
   int count =0;
   char a = {'a'};
   
   while(size < 2 || size > 9)
 {
   cout << "What size grid would you like? (2..9) ";
   cin >> size;
      if( size < 2 || size > 9)
   cout << "\nTry again" << endl << endl;
   else
   break;
    
 }//end of while loop
 cout << endl;
   
  
   for(int rows=0; rows<size; rows++)
   {
     count++;
     cout << count;
     a<='z'; a++;
     cout << a;
    for(int cols=0; cols<size; cols++)
    {     
         grid[rows][cols]= '+';
         cout << grid[rows][cols];
         
     }
     
     cout << endl;
    }

    cout << endl << endl;
     
 
system("PAUSE");
return EXIT_SUCCESS;
}

---------------------------------------
Output:

What size grid would you like? (2..9) 9

1b+++++++++
2c+++++++++
3d+++++++++
4e+++++++++
5f+++++++++
6g+++++++++
7h+++++++++
8i+++++++++
9j+++++++++


Press any key to continue . . .

Answer
Hi Peter

I would not store the grid co-ordinates (letters/numbers) in the grid, nor would I store the '+' character. One reason is that we need to display many characters for every grid point. We need to display what is between the grid points - either space or connecting lines. I would use the grid to encode the state of the game. The grid is the model. I would separate the display of the grid (the view) from the model. As I said in my last answer, to encode the state of the game, you need a grid of elements where each element contains information about what other element it is connected to. Once you have that, it is a simple matter to go through the grid model, and print a display.

The process is something like this:

for each row
{
  print row identifier
  for each element in a row
  {
     print '+'
     if connected to east neighbour, print dash, else print space
  }
  print newline
  for each element in a row (same row again)
  {
     if connected to south neighbour, print vertical line, else print space
     print space
  }
  print newline
}
for i in 0 to row length
{
   print column id
}

You can use the iomanip setw operator to print pad your column id with spaces. For example
include <iomanip>
cout << setw(3) << columnId;  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.