0 Replies - 1626 Views - Last Post: 27 October 2009 - 10:25 PM Rate Topic: -----

#1 griffinfujioka   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 26-October 08

C++ Board game Using STL Map

Posted 27 October 2009 - 10:25 PM

I'm in my 3rd semester of computer science, and we're supposed to write a board game program and my instructor wants us to use STL maps. We also have to design a simple player class, which has a private member variable integer ID number. Our program should generate the board, and insert players into it, and then move the players around within some given rules. I've been having trouble even designing the board though.. He recommended using two maps; one which is sorted by ID number, and one which is sorted by coordinate.


// board.h
// Header file for board class

#pragma once

#include<cstdlib> // for NULL 
using namespace std; 

class board
	// Default Constructor 

	// Copy Constructor
	// Takes two ints, and creates a board of that size. row==col for all inputs 
	board(int row, int col); 

	// Index operator 
	const vector<int>& operator[](int row); 

	// Insert
	bool insert(int ID, int row, int column); 

	// Remove
	bool remove(int ID); 

	// Find 
	bool find(int ID); 

	// Find with Pos
	void findWithPos(int& row, int& col); 

	// Move to 
	bool moveTo(int ID, int row, int column); 

	// Print by coordinate
	void printByCoordinate(); 

	// Print by ID
	void printByID(); 

	map<int, player> mapByCoord; // Map where the key is the x value of the coordinate, and the value is a player
	map<int, player> mapByID; // Map where the key is an ID number, and the value is a player
	int numPlayers; // Keep track of the number of players in the game 


// board.cpp
// Implementation file for board class

#include "board.h"


board::board(int row, int col)
	numPlayers = 0; 
	// Don't think I need anything here, 
	// because maps are STL objects dynamic
	// so size can be adjusted on the fly

board::insert(int ID, int row, int column)
	player(ID) p1; 
	mapByID.insert(pair<int, int>(ID, row)); 
	mapByCoord.insert(pair<int, int>(row, ID)); 

and here is my player class.. which I'm not really done with

// player.h
// Header file for player class

class player

	player(int ID); 

	int ID; // Must be unique, positive, and will occupy a unique (x,y) position on the board. 


// player.cpp
// player class implementation


player::player(int ID1)
	ID = ID1; 

Any help is appreciated, even just a shove in the right direction. Thanks in advance

Is This A Good Question/Topic? 0
  • +

Page 1 of 1