Hi,

I have been working on a Reversi/Othello game using QT and have got stuck when trying to implement a new game function which will return the game state back to its original state.

reversigame.cpp

/** 
@file reversigame.cpp
@author Rob Charrett

@brief ReversiGame implementation.
*/

#include "reversigame.h"

ReversiGame::ReversiGame(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags), gameBoard(GameGrid(10,10,this))
{
	// initialise UI based on reversigame.ui form file
	ui.setupUi(this);

	// display the board in the central widget location, has been created with default
	// size of 10x10
	setCentralWidget(&gameBoard);

	// default to new game
	state = GAME_RUNNING;

	// set up status bar
	setupStatusBar();

	// set up menu bar
	setupMenu();
}

ReversiGame::~ReversiGame()
{

}

void ReversiGame::setupStatusBar()
{
	// set starting text of each label
	if(playerTurn == PLAYER_ONE)
	{
		feedback.setText(tr("PLAYER ONE"));
	}
	else if(playerTurn == PLAYER_TWO)
	{
		feedback.setText(tr("PLAYER TWO"));
	}


	player1Score.setText(tr("Player 1: "));
	player2Score.setText(tr("Player 2: "));


	// add widgets to status bar
	statusBar()->addWidget(&feedback,1);
	statusBar()->addWidget(&player1Score);
	statusBar()->addWidget(&player2Score);
}

void ReversiGame::setupMenu()
{
	// add a new menu ("File")
	QMenu* fileMenu = menuBar()->addMenu(tr("&File"));

	createNewAction = fileMenu->addAction(tr("&Create New Game"), this, SLOT(newg()));
	createNewAction->setShortcut(tr("Ctrl+N"));

	loadSavedAction = fileMenu->addAction(tr("&Load A Saved Game"), this, SLOT(load()));
	loadSavedAction->setShortcut(tr("Ctrl+L"));

	quitAction = fileMenu->addAction(tr("&Quit"), this, SLOT(quit()));
	quitAction->setShortcut(tr("Ctrl+Q"));		

	/*saveCurrentAction = fileMenu->addAction(tr("&Save Current Game"), this, SLOT(Save Current()));
	saveCurrentAction->setShortcut(tr("Ctrl+S"));

	ReplayAction = fileMenu->addAction(tr("&Play Again?"), this, SLOT(Again?()));
	ReplayAction->setShortcut(tr("Ctrl+P"));*/
}

void ReversiGame::boardClicked()
{
	// Get the GridNode on the board that received the click (the origin of the signal
	// that kicked off this function)
	GridNode* slot = (GridNode*) sender();



	// Get the position of the slot in the grid.
	NodePosition pos = slot->getPosition();	
	GridNode* gridNode = gameBoard.getNode(pos);


	if(playerTurn == PLAYER_ONE)
	{
		if(slot -> getOwner() == OWNER_NONE) 
		{
			slot -> setOwner (OWNER_PLAYER1);
			playerTurn = PLAYER_TWO;
		}

	}

	else if(playerTurn == PLAYER_TWO)
	{
		if(slot -> getOwner() == OWNER_NONE) 
		{
			slot -> setOwner (OWNER_PLAYER2);
			playerTurn = PLAYER_ONE;
		}
	}
	else
	{
		playerTurn = PLAYER_ONE;
	}
	setupStatusBar();
}

//
//void ReversiGame::quit
//
//void ReversiGame::load()
//{
//}
//
//void ReversiGame::save()
//{
//}

void ReversiGame::newg()
{	
	NodePosition pos(0,0);	
	GridNode* gridNode = gameBoard.getNode(pos);

	for(pos.col = 0; pos.col <10; pos.col++)
	{
		for(pos.row = 0; pos.row <10; pos.row++)
		{

			if(gridNode->getOwner() == OWNER_NONE)
			{
				if (pos.col == 5 && pos.row == 5)
				{
					getOwner = OWNER_PLAYER1;
				}
				if (pos.col == 4 && pos.row == 4)
				{
					getOwner = OWNER_PLAYER1;
				}
				if (pos.col == 4 && pos.row == 5)
				{
					getOwner = OWNER_PLAYER2;
				}
				if (pos.col == 5 && pos.row == 4)
				{
					getOwner = OWNER_PLAYER2;
				}
			}
		}
	}

}

void ReversiGame::quit()
{
	// currently, simply exit the application
	qApp->quit();
}

reversigame.h

/** 
	@file reversigame.h
	@author Rob Charrett
	
	@brief ReversiGame and GameState definitions.

	@description This file contains the definition of the core ReversiGame class and the 
	related GameState enum.
*/
#ifndef REVERSIGAME_H
#define REVERSIGAME_H

#include <QtGui/QMainWindow>
#include <QtGui/QLabel>
#include <QtGui/QAction>
#include "ui_reversigame.h"
#include "gameboard.h"

/** Enumeration used for handling the current game states in ReversiGame */
enum GameState {GAME_NONE, GAME_RUNNING, GAME_FINISHED};

enum Turn {PLAYER_ONE, PLAYER_TWO};

/**
	@brief The ReversiGame class forms the core of the ReversiGame application.

	@description This class forms the core of the ReversiGame application, handling the
	GUI, user interaction and game logic. The GUI is initialised through 
*/
class ReversiGame : public QMainWindow
{
	Q_OBJECT

public:
	/** ReversiGame constructor */
	ReversiGame(QWidget *parent = 0, Qt::WFlags flags = 0);
	~ReversiGame();

protected:
	/** 
		Menu setup function. Change this to change the items appearing in the menu at 
		start up, as well as any actions that should be associated with said items. 
	*/
	void setupMenu();

	/**
		Status bar setup function. Change this to change the items and text appearing
		in the status bar at start up.
	*/
	void setupStatusBar();

private:
	/** Class generated by reversigame.ui form file */
	Ui::ReversiGameClass ui;

	/** Game board, fwds mouse clicks to boardClicked() (see ReversiGame constructor) */
	GameGrid gameBoard;

	/** Current game state */
	GameState state;
	
	Turn playerTurn;
	
	/** Label (text field) used for displaying player1's score in the status bar */
	QLabel player1Score;

	/** Label (text field) used for displaying player2's score in the status bar */
	QLabel player2Score;

	/** Label (text field) used for displaying feedback in the status bar */
	QLabel feedback;

	/** Action used in menu bar, connected to the quit() slot */
	QAction* quitAction;
	QAction* createNewAction;
	QAction* loadSavedAction;
	/*QAction* ReplayAction;
	QAction* saveCurrentAction;*/

	public slots:
	/** 
		Public slot, receiving signals from the GameGrid (see ReversiGame constructor). 
		This function is responsible for catching user interaction, and handling the game
		logic that follows said interaction.
	*/
	void boardClicked();
	

protected slots:


private slots:
	/** Private slot, quits the application upon receiving a signal */
	void quit();
	
	void newg();
};

#endif // REVERSIGAME_H

I am getting the error C2065: undeclared identifier for 'getOwner'.
I know this is probably quite obvious but have spent far too long staring at a pc screen! so any help would be greatly appreciated.

Recommended Answers

All 3 Replies

On line 142 you are trying to assign something to getOwner. I don't see a variable called getOwner declared in function newg() or as a member of class ReversiGame, so that is likely where your problem lies.

Dave

On line 142 you are trying to assign something to getOwner. I don't see a variable called getOwner declared in function newg() or as a member of class ReversiGame, so that is likely where your problem lies.

Dave

thanks dave...would this be something as simple as just declaring it as an int, or I have recently changed it from owner, which is referenced by NodeOwner I have another gridnode.h which has the reference there! But it still threw up an error so i therefore changed it too what you see now

gridnode.h(this is where its from)

/** 
	@file gridnode.h
	@author Rob Charrett
	
	@brief GridNode class definition, as well as supporting NodePosition struct

	@description This file contains the definition of the GridNode class, one of each
	resides in each slot of the GameGrid used as a board for the ReversiGame application,
	as well as the NodePosition struct used as a compound type for easier returning of 
	positions in the grid.
*/
#ifndef REVERSISLOT_H
#define REVERSISLOT_H

#include <QtGui/QPushButton>

/** @brief NodePosition struct - used simply as a means for returning both row and col in 
	the GridNode::getPosition() function */
struct NodePosition
{
	NodePosition(int r = 0, int c = 0) : row(r), col(c) {};
	int row;
	int col;
};

/** Enum used to handle current owner of each grid node */
enum NodeOwner {OWNER_NONE, OWNER_PLAYER1, OWNER_PLAYER2};


/**
	@brief The GridNode class is used for each node in the GameGrid that forms the game 
	board for the ReversiGame application.

	@description Technically each GridNode inherit from QPushButton - so has all the 
	functionality of push buttons in Qt, plus extra information such as its position in 
	the grid (so it can be accessed in ReversiGame::boardClicked(), the current owner of 
	the node, as well as an overloaded function for the painting of the node.
*/
class GridNode : public QPushButton
{
Q_OBJECT
public:
	/** Constructor - defaults the current owner to none */
	GridNode(QWidget* parent = NULL) : QPushButton(parent), owner(OWNER_NONE) {};

	// Getters & Setters
	NodeOwner getOwner() {return owner;}
	void setOwner(NodeOwner s);
	NodePosition getPosition() {return pos;}
	void setPosition(NodePosition p);

	/** Function used to control drawing of a game "piece" */
	void drawPiece();

public slots:
	
protected:
	/** Overloaded virtual function that stops this looking like any other Qt button */
	void paintEvent(QPaintEvent* event);

private:
	/** Current owner of the node */
	enum NodeOwner owner;

	/** Position of this node in the grid */
	NodePosition pos;
};

#endif

hi guys, fixed the problem, with the new game, changed to a series of for loops which compared the rows and columns and then set the new owners of each grid node accordingly. Thanks for the responses though.

I'm interested in trying to implement save/load functionality using QT, but have never done any I/O in C++ before, any pointers, how would I go about it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.