next up previous
Next: C++ Implementation 10/11 Up: Von C++ zu Java Previous: C++ Implementation 8/11

C++ Implementation 9/11

Board.cc

#include "Board.h"

Board::Board(const int rows, const int columns)
{
  reset(rows, columns);
}

void Board::reset(const int rows, const int columns)
{
  the_row_size = rows; the_column_size = columns;
  for (int i=0; i<the_column_size; i++)
  {
    for (int j=0; j<the_row_size; j++)
    {
      the_grid[j][i].drop(Counter(INVISIBLE));
    }
    the_height[i] = 0;
  }
  the_last_row = the_last_col = 0;
  the_no_empty_cells = rows * columns;
}

void Board::drop_in_column(const int column, const Counter c)
{
  the_last_col = column; the_last_row = the_height[column];
  add_counter_to_board(column, c);
}

Boolean Board::move_ok_for_column(const int column) const
{
  if (column >= 0 && column < the_column_size)
  {
    if (the_height[column] < the_row_size)
    {
      return TRUE;
    }
  }
  return FALSE;
}

void Board::add_counter_to_board(const int column, 
     const Counter players_counter)
{
  the_grid[the_height[column] ][column].drop(players_counter);
  the_height[column]++;
  the_no_empty_cells--;
}

BoardState Board::situation(void) const
{
  if (is_there_a_win() == TRUE)
  {
    return WIN;
  }
  if (the_no_empty_cells == 0)
  {
    return DRAW;
  }
  return PLAY;
}

Boolean Board::is_there_a_win(void) const
{
  char stone = the_grid[the_last_row][the_last_col].holds();
  for (int dir=1; dir <= 4; dir++)
  {
    int stones_in_a_line = 
      check_for_a_win(dir, the_last_row, the_last_col, stone) + 
      check_for_a_win(dir+4, the_last_row, the_last_col, stone) - 1;
    if ( stones_in_a_line >= STONES_IN_A_WIN_LINE )
    {
      return TRUE;
    }
  }
  return FALSE;
}

int Board::check_for_a_win(const int dir, const int x_coord, 
        const int y_coord, const char counter) const
{
  int x = x_coord; int y = y_coord;
  if ( (x >= 0) && (x < the_row_size) &&
       (y >= 0) && (y < the_column_size) &&
       (the_grid[x][y].holds() == counter) )
  {
    switch(dir)
    {
    case 1:      y++; break;
    case 2: x++; y++; break;
    case 3: x++;      break;
    case 4: x++; y--; break;

    case 5:      y--; break;
    case 6: x--; y--; break;
    case 7: x--;      break;
    case 8: x--; y++; break;

    default: cerr << "internal error 1\n";
    }
    return 1 + check_for_a_win(dir, x, y, counter);
  }
  else 
  {
    return 0;
  }
}


void Board::display(void) const
{
  cout << " ";
  for (int j=1; j<=the_column_size; j++)
    cout << j << "   ";
  cout << '\n';

  for (int i=the_row_size - 1; i>=0; i--)
  {
    cout << "| ";
    for (int j=0; j<the_column_size; j++)
    {
      the_grid[i][j].display();
      cout << " | ";
    }
    cout << '\n';
  }

  for (int k=the_row_size; k>=0; k--) cout << "----";
  cout << "-\n\n";
}


next up previous
Next: C++ Implementation 10/11 Up: Von C++ zu Java Previous: C++ Implementation 8/11

© 1997 Gottfried Rudorfer, C++-AG, Lehrveranstaltungen, Abteilung für Angewandte Informatik, Wirtschaftsuniversität Wien, 3/19/1998