You are here:

C++/Dots and boxes game

Advertisement


Question
I have written most of the program to play against the computer and understand what is supposed to do. I am just have trouble printing out the move. I am supposed to make an extra column of letters and an extra row of numbers so that it represents a point and connects it with another point.

Example:

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

a + + +
      
b + + +

c + + +
 1 2 3

*Also I have made it that if there is an open space to apply a ' '

Now, I am trying to do something like this and apply the vertical move:

if(letter appears first)
 board[row][col] = '-';
else
 board[row][col] = '|';
------------------------------------------------------------------
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>

using namespace std;

void gameBoard();
int checkWinner();
void computerPlayer();

int size, test, row, col, turn, option2 = 0;

char board[15][15];
char alpha[] = {"abcdefghijklmnopqrstuvwxyz"};

int main()
{
     int size, num, test, row, col, turn, option2 = 0;
     string move;
     move = num;
     move = alpha;
     
     
     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

       while(option2 = -1)
       {      
         if(turn%2 == 0)//if it's even then it's Human's turn
         {
         bool condition = true;
         while(condition == true)
         {      
         
         for(int rows = 0; rows < size; rows++)
         {
         for(int cols = 0; cols < size; cols++)
         {
         board[rows][cols] = '+';
         cout << board[rows][cols];
         }        
         cout << endl;
         }  
         //Program crashses here because I do not know how to input the move correctly
         cout<<"Enter dots to connect (a1-b1 or 1a-b1): ";
         cin>>move;
         
         if(board[row][col] == ' ')
         {   
         board[row][col] = '-';
         turn++;
         condition = false;
         }    
         else
         cout<<"Invalid move!\n";
         }    
         }    
         else //AI must do something
         {
         computerPlayer();          
         turn++;
         }
         
         if(checkWinner() == 1) //X wins
         {
         cout<<"Congratulations! You won!\n\n";
         option2 = -1;
         }          
         else if(checkWinner() == 2) //X wins
         {
         cout<<"You lose. Better luck next time!\n\n";
         option2 = -1;
         }          
         gameBoard();
       }  
         
   system("PAUSE");
   return 0;
}
//****************************************************Functions
//print the board to the screen
void gameBoard()
{
    cout<<"\n";
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {        
         cout << "+" << board[i][j] << "+";
        }        
      cout << endl;
    }        
}     
//**************************************************************
int checkWinner()
{
//check to see if '-'
if(board[0][0] == '-' && board[0][1] == '-' && board[0][2] == '-')
         return 1;
if(board[1][0] == '-' && board[1][1] == '-' && board[1][2] == '-')
         return 1;     
if(board[2][0] == '-' && board[2][1] == '-' && board[2][2] == '-')
         return 1;  
         
if(board[0][0] == '-' && board[1][0] == '-' && board[2][0] == '-')
         return 1;  
if(board[0][1] == '-' && board[1][1] == '-' && board[2][1] == '-')
         return 1;
if(board[0][2] == '-' && board[1][2] == '-' && board[2][2] == '-')
         return 1;          

if(board[0][0] == '-' && board[1][1] == '-' && board[2][2] == '-')
         return 1;   
if(board[2][0] == '-' && board[1][1] == '-' && board[0][2] == '-')
         return 1;  
//check to see '|'
if(board[0][0] == '|' && board[0][1] == '|' && board[0][2] == '|')
         return 0;
if(board[1][0] == '|' && board[1][1] == '|' && board[1][2] == '|')
         return 0;     
if(board[2][0] == '|' && board[2][1] == '|' && board[2][2] == '|')
         return 0;  
         
if(board[0][0] == '|' && board[1][0] == '|' && board[2][0] == '|')
         return 0;  
if(board[0][1] == '|' && board[1][1] == '|' && board[2][1] == '|')
         return 0;
if(board[0][2] == '|' && board[1][2] == '|' && board[2][2] == '|')
         return 0;          

if(board[0][0] == '|' && board[1][1] == '|' && board[2][2] == '|')
         return 0;   
if(board[2][0] == '|' && board[1][1] == '|' && board[0][2] == '|')
         return 0;          
}        
//*************************************************************
//This function generates a random move and see if it's available. if it is, then make it
//if it isn't then generate another random move
void computerPlayer()
{
    col = -1;
    row = -1;
    while(col == -1 || row == -1)
    {
       time_t seconds;
       time(&seconds);
       srand((unsigned int) seconds);  
       col = rand()%3;
       row = rand()%3;
       if(board[row][col] != ' ')
       {
         col = -1;
         row = -1;
       }
       board[row][col] = '-';          
    }          
}

Answer
Use a std::vector for the grid:

#include <vector>
#include <iostream>

typedef std::vector<char> row_t ;
typedef std::vector< row_t > grid_t ;


Define some constants:

const char DOT = '.' ;
const char HLINE = '-' ;
const char VLINE = '|' ;
const char SPACE = ' ' ;
const char A = 'A' ;
const char B = 'B' ;


Create a grid which has n x n dots:

grid_t init_grid( int n )
{
       int nchars = n + n-1 ; // number of characters to print in a grid with n dots
       row_t row_with_dots( nchars, SPACE ) ;
       for( int i = 0 ; i < nchars ; i += 2 ) row_with_dots[i] = DOT ;
       row_t row_with_spaces( nchars, SPACE ) ;

       grid_t grid ;
       for( int i = 0 ; i < nchars ; ++i )
       {
         if( i%2 == 0 ) grid.push_back(row_with_dots) ;
         else grid.push_back(row_with_spaces) ;
       }
       return grid ;
}


And print it:

void print_grid( const grid_t& grid, std::ostream& stm )
{
       int nchars = grid.size() ;
       for( int i = 0 ; i < nchars ; ++i )
       {
         for( int j = 0 ; j < nchars ; ++j ) stm << grid[i][j] ;
         stm << '\n' ;
       }
}

int main()
{
       grid_t grid =  init_grid(7) ;
       print_grid( grid, std::cout ) ;
}


This snippet connects dots to form a box and prints 'A' in the box.

       grid[2][3] = HLINE ;
       grid[4][3] = HLINE ;
       grid[3][2] = VLINE ;
       grid[3][4] = VLINE ;
       grid[3][3] = A ;
       print_grid( grid, std::cout ) ;

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.