Click here to Skip to main content
15,892,161 members
Articles / Desktop Programming / MFC

WinBattle

Rate me:
Please Sign up or sign in to vote.
4.46/5 (14 votes)
22 Dec 200320 min read 63.6K   2.8K   43  
A multi-player game tutorial and reusable framework
// ============================================================================
// Board IO functions (transfer between client and server)
//
// (c) 2003 Ken Reed
//
// This is free software. You can redistribute it and/or modify it under the
// terms of the GNU General Public License version 2 as published by the Free
// Software Foundation.
// ============================================================================

#include "stdafx.h"
#include "cell.h"
#include "board.h"
#include "universal.h"


// ============================================================================
// Update the board with a new cell and explore around it (if that option is
// enabled).
// ============================================================================

void Board::new_data(Socket * socket)
{
   static Cell new_cell;

   read_cell(*socket, new_cell);

   int i(new_cell.i);
   int j(new_cell.j);

   Cell * cell = &(cells[j][i]);
   cell->merge(new_cell);

   cell->draw(window, hidden, player);

   // If we are exploring, and the cell belongs to us, and we haven't explored
   // from here before then explore.

   if (!exploring || (cell->player != player) || cell->scanned)return;

   cell->explored = true;
   cell->scanned  = true;

   int start_i (i - 3);
   int start_j (j - 2);
   int end_i   (i + 4);
   int end_j   (j + 3);

   if (start_i < 0) start_i = 0;
   if (start_j < 0) start_j = 0;

   if (end_i > board_x_size) end_i = board_x_size;
   if (end_j > board_y_size) end_j = board_y_size;

   for (j = start_j; j < end_j; j++) {
      for (i = start_i; i < end_i; i++) {
         cell = &(cells[j][i]);
         if (! cell->explored) {
            cell->explored = true;
            cell->draw(window, hidden, player);
         }
      }
   }
}


// ============================================================================
// Transmit the board to a client
// ============================================================================

void Board::transmit(Socket * socket) 
{
   (*socket) <<  exploring << " " << hidden << "\n";

   for (int j = 0; j < board_y_size; j++) {
      for (int i = 0; i < board_x_size; i++) {
         send_cell(*socket, cells[j][i]);
      }
   }
}


// ============================================================================
// Receive a new board
// ============================================================================

void Board::receive(Socket * socket) 
{
   (*socket) >> exploring >> hidden;

   for (int j = 0; j < board_y_size; j++) {
      for (int i = 0; i < board_x_size; i++) {
         cells[j][i].reset(exploring);
         read_cell(*socket, cells[j][i]);
      }
   }
}


// ============================================================================
// Send any changed cells
// ============================================================================

void Board::send_updates(Socket * socket)
{
   int i, j;
   int count(0);
  

   for (j = 0; j < board_y_size; j++) {
      for (i = 0; i < board_x_size; i++) {
         if (cells[j][i].changed) count++;
      }
   }

   (*socket) << count << "\n";

   for (j = 0; j < board_y_size; j++) {
      for (i = 0; i < board_x_size; i++) {
          if (cells[j][i].changed) {
            send_cell(*socket, cells[j][i]);
         }
      }
   }
}


// ============================================================================
// Read any changed cells
// ============================================================================

void Board::get_updates(Socket * socket)
{
   int count(0);
  
   (*socket) >> count;

   for (int k = 0; k < count; k++) {
      new_data(socket);
   }
}


// ============================================================================
// Reset all cell changed flags
// ============================================================================

void Board::end_of_updates()
{
   for (int j = 0; j < board_y_size; j++) {
      for (int i = 0; i < board_x_size; i++) {
         cells[j][i].changed = false;
      }
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Systems Engineer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions