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: Ok, let us take this up one (small) step at a time.

The grid is a two dimensional NxN structure as shown here http://en.wikipedia.org/wiki/Dots_and_Boxes
We need to be able to connect adjacent dots horizontally (with a '-') and vertically (with a '|').

If N is say 4, one row would be like . . . . or .-. .-. or .-.-.-. So we need seven characters to represent a single row, 4 for the dots and three for the spaces/lines between the dots. In general, we need N + N-1 characters.

Likewise for columns; to play a game of 4x4, our grid needs to be able to show 7x7 characters. In general, for NxN, we need a (2*N+1)x(2*N+1) grid.

Note: If you also need to mark the boxes as 'A' or 'B', the grid needs to be bigger Q: By how much?

First settle on the grid representation and write the PrintGrid function, get it working, and then we'll look at
a. how to connect two dots within the grid
b. how to keep track of boxes being formed.

Hint: use a std::vector< std::vector<char> > to represent the grid.
http://www.mochima.com/tutorials/vectors.html

One you have done that much, come back with a second question.



 

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

QUESTION: So far I got it asking the user for an input and then it prints out the grid underneath. I am trying to figure out of how to connect the dots within the grid. In my print function how do I ad in an extra column that will spit out numbers or an extra row that spits out characters?

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


using namespace std;

int preGame();

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.
//This function will print the current grid and score after each turn. It will also obtain and apply the move.
//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
 
//Print the grid
char grid[input][input];
cout << endl;
for (int row=0; row<input; row++)
{
for (int col=0; col<input; col++)
{
grid[row][col]='+';
cout << grid[row][col];
}
cout << endl;
}
}

Answer
> char grid[input][input];

This is not valid C++. The number of elements in a C-style array must be a constant known at compile time.

> how to connect the dots within the grid.

Repeat:
We need to be able to connect adjacent dots horizontally (with a '-') and vertically (with a '|').

If N is say 4, one row would be like . . . . or .-. .-. or .-.-.-. So we need seven characters to represent a single row, 4 for the dots and three for the spaces/lines between the dots. In general, we need N + N-1 characters.

Likewise for columns; to play a game of 4x4, our grid needs to be able to show 7x7 characters. In general, for NxN, we need a (2*N+1)x(2*N+1) grid.

> In my print function how do I ad in an extra column that will spit out numbers or an extra row that spits out characters?

We'll come to that once we know how to connect the dots within the grid.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.