So I'm trying to create a program that mimics the Minesweeper game. I have double-checked the header files, the class names, and made sure the headers are #included in the other cpp files, but when I try to build the program, I get a LNK2019 error in the "Main" class I have.
Error in full:
Error 1 error LNK2019: unresolved external symbol "public: __thiscall
Board::Board(int,int,int)" (??0Board##QAE#HHH#Z) referenced in
function _main \fpsb\g\gathmr26\visual studio
2013\Projects\Minesweeper\Minesweeper\Main.obj Minesweeper
I've spent probably around 2 hours looking at answers here on StackOverflow and elsewhere and got nowhere. I've run down through every bullet point in this MSDN page, and every "common cause" in this popular answer, and none of them seemed to apply to my situation. I've also tried all the "Diagnosis tools" options on the MSDN page and all they've done is just confuse me more.
The closest I have to my situation (as far as I can tell) is this question except that all of my code is just in one project, not multiple. One of the people who answered that question has said "I typed this code into my Visual Studio and it worked fine", having assumed that the files were in one project. I don't understand why that answer got it working there when I have pretty much the same situation here.
So, anyway, here is the code:
Main.cpp
#include <iostream>
#include <string>
#include "Cell.h"
#include "Board.h"
int main() {
Board *bee;
bee = new Board(50, 50, 50);
std::cout << "board created";
return 0;
}
Board.cpp
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
#include "Cell.h"
#include "Board.h"
#ifndef BOARD_H
#define BOARD_H
// Board class. Used to create an array of cell objects to function as data model for Minsweeper game.
class Board
{
private:
int width; // number of columns in board
int height; // number of rows in board
int mines; // number of mines stored in board
Cell*** cells; // array storing cell objects
public:
// Constructor for board. Takes number of columns, rows, and mines as parameters
Board::Board(int cols, int rows, int numMines) {
width = cols;
height = rows;
mines = numMines;
cells = new Cell**[height];
for (int i = 0; i < height; i++) {
cells[i] = new Cell*[width];
}
int c = 0;
int r = 0;
while (r < height)
{
while (c < width)
{
setCell(c, r, CellValue::COVERED_CELL);
c++;
}
c = 0;
r++;
}
int m = 0;
while (m < numMines)
{
std::srand(std::time(nullptr));
int x = generateRandomNumberInRange(0, width - 1);
int y = generateRandomNumberInRange(0, height - 1);
if (!(getCellVal(x, y) == MINE))
{
setCell(x, y, CellValue::MINE);
m++;
}
}
}
// Accessor for width field
int Board::getWidth()
{
return width;
}
// Accessor for height field
int Board::getHeight()
{
return height;
}
// Accessor for mines field
int Board::getMines()
{
return mines;
}
// Function to access value of cell located in array where x is column parameter and y is row parameter
CellValue Board::getCellVal(int x, int y)
{
CellValue value = CellValue::INVALID_CELL;
if (!(x < 0 || x >(width - 1) || y < 0 || y >(height - 1)))
{
Cell temp = *cells[x][y];
value = temp.getValue();
}
return value;
}
// Function to set value of cell located in array where x is column parameter and y is row parameter
void Board::setCell(int x, int y, CellValue value)
{
if (!(x < 0 || x >(width - 1) || y < 0 || y >(height - 1)))
{
Cell temp = *cells[x][y];
temp.setValue(value);
}
}
// Function to determine if game is lost
// Loops through array to see if there are any UNCOVERED_MINES
// If so, returns true, game ends, as you've lost :(
// If not, returns false and game can continue
// Should run after every click action in game
bool Board::isGameLost()
{
bool isLost = false;
int c = 0;
int r = 0;
while (r < height)
{
while (c < width)
{
if (getCellVal(c, r) == UNCOVERED_MINE)
{
isLost = true;
}
c++;
}
c = 0;
r++;
}
return isLost;
}
// Function to determine if game is won
// Loops through array to determine if there are any falsely flagged mines, unflagged mines, covered cells, or uncovered mines
// If there are, returns false and game continues
// If not, returns true, games ends, you've won :)
bool Board::isGameWon()
{
bool isWon = true;
int c = 0;
int r = 0;
while (r < height)
{
while (c < width)
{
CellValue value = getCellVal(c, r);
if ((value == FLAG) ||
(value == MINE) ||
(value == COVERED_CELL) ||
(value == UNCOVERED_MINE))
{
isWon = false;
}
c++;
}
c = 0;
r++;
}
return isWon;
}
};
#endif
Board.h
#include <iostream>
#include <string>
#include "Cell.h"
#ifndef BOARD_H
#define BOARD_H
class Cell;
enum CellValue;
class Board
{
private:
int width;
int height;
int mines;
Cell*** cells;
public:
Board(int cols, int rows, int numMines);
int getWidth();
int getHeight();
int getMines();
CellValue* getCellVal(int x, int y);
void setCell(int x, int y, CellValue value);
void uncoverCell(int x, int y);
void flagCell(int x, int y);
bool isGameLost();
bool isGameWon();
};
#endif
I know this is a common error that people have and that there's more than a handful of questions about this on StackOverflow, but at this point, I've not found any that seem to match what I have here. What is the issue here?
Seems like you're mixing header and source files. Your cpp file contains a class declaration with all the functions defined inside. This is not what a cpp file looks like. It should only contain function declarations:
Board::Board(...)
{
...
}
bool Board::IsGameWon...
etc...
Related
I am currently trying to make a 2d vector representing pixels.
The problem is that when I try to set a value at a specific index the program crash.
The compilation is good.
Here is the .h :
#include "../include/graphics.h"
#include <stdlib.h>
#include <stdio.h>
#include <functional>
#include <vector>
#include <string>
using namespace rgb_matrix;
class Viewport
{
public:
Viewport(std::string id,int xMn,int yMn,int xMx,int yMx,Canvas *c)
{
canvas_=c;
_id = id;
_x = xMn;
_y = yMn;
width=xMx-xMn;
height=yMx-yMn;
}
void SetPixel(int x_vport,int y_vport,rgb_matrix::Color c);
int getXMax(){
return _x + height;
}
int getHeight(){
return height;
}
int getWidth(){
return width;
}
int getX(){
return _x;
}
void clear();
std::string getId(){return _id;}
void show();
void fill(int r,int g,int b);
private :
Canvas *canvas_;
int width,height;
int _x,_y;
std::string _id;
std::vector<std::vector<rgb_matrix::Color> > colors;
};
The weird point is that I can access to the values into the .h but not into the .cpp :
void Viewport::SetPixel(int x, int y, rgb_matrix::Color c) {
if(x>=0 && y>= 0 && x< width && y < height) {//if in rectangle
colors.at(x).at(y) = c;
}
}
This is not how you fill values in the vector:
void Viewport::SetPixel(int x, int y, rgb_matrix::Color c) {
if(x>=0 && y>= 0 && x< width && y < height) {//if in rectangle
colors.at(x).at(y) = c;
}
}
Before you visit an element of the vector colors.at(x) you must assure that the size of the vector colors.size() is bigger than x. So, maybe you call colors.resize() somewhere in other place, before filling vector with values.
The crash is intended, that's an exception thrown due to the out-of-bounds access.
I would question the choice of the data structure:
std::vector<rgb_matrix::Color> can be chosen if you need the continuous block of memory (just remember to either resize it or construct it with width*height parameter =)
vector of vectors implies a dense nonrectangular structure - normally it's used for something like triangle matrices to save the memory.
using x = unsigned; using y = unsigned;vector::<std::tuple<x, y, rgb::matrix::Color> can be used for a sparse structure with a few pixels in random places.
Also x>=0 && y>= 0 can be avoided if you switch to unsigned instead of int=)
The title pretty much says it all. I'm required to write a program for a Software course I'm taking that puts two basic algorithms against each other in a game of checkers. For this, I have a Square class that stores whether the square is occupied (bool), which player by (enum Pl, either NA, P1, or P2), and the label of that square (Index, int). I then created a Board class, which is effectively a 2D array of Squares that will have dimensions specified by the user. However, in the implementation file of my Board class, I'm having an error using the board[][] array in one of my functions. I get the error "board was not declared in this scope", which shouldn't be too tricky to figure out, except that the exact same syntax (at least from what I can see, and I've been working on fixing this for a while now) that's causing the error works perfectly in the constructor for the Board class in the same file. Below is the code for my Board.h file, and the relevant parts of my Board.cpp file:
Board.h:
#ifndef BOARD_H
#define BOARD_H
#include "Square.h"
#include <string>
class Board
{
public:
Board(int);
int getBsize();
int getWinner();
std::string getCmove();
Square getSquare(int, int);
void setWinner(int);
void setCmove(std::string);
bool canMoveL(int, int);
bool canMoveR(int, int);
bool canTakeL(int, int);
bool canTakeR(int, int);
void P1Move(int, int);
void P2Move(int, int);
void printCmove(std::string);
void printWin(std::string);
private:
Square board[12][12];
int bSize;
int winner;
std::string cMove;
};
#endif // BOARD_H
Board.cpp constructor as well as top of the file (in case the error is with the libraries and files I've included in Board.cpp):
#include "Square.h"
#include "Board.h"
#include <string>
Board::Board(int bSize)
{
Board::bSize = bSize;
Board::winner = 0;
Board::cMove = "";
Board::board[12][12];
int x = bSize-1;
int index = 1;
for(int j = 0; j < bSize; j++)
{
for(int i = 0; i < bSize; i++)
{
if(((i%2!=0)&&(j%2==0))||((i%2==0)&&(j%2!=0)))
{
if((j==x/2)||(j==x/2+1))
{
board[i][j] = Square(false, NA, index);
index++;
}
else if(j < x/2)
{
board[i][j] = Square(true, P1, index);
index++;
}
else if(j > x/2+1)
{
board[i][j] = Square(true, P2, index);
index++;
}
}
else
{
board[i][j] = Square(false, NA, 0);
}
}
}
}
Function in Board.cpp causing the error:
bool canMoveL(int i, int j)
{
bool temp = false;
Square sq = board[i][j];
if((i-1 < 0)||((sq.getPl() == P1)&&(j-1 >= bSize))||((sq.getPl() == P2)&&(j-1 < 0)))
{
return temp;
}
if(sq.getOcc() == 0)
{
return temp;
}
else
{
if(sq.getPl() == P1)
{
temp = true;
}
else if(sq.getPl() == P2)
{
temp = true;
}
}
return temp;
}
Note: Since I haven't been able to test the canMovL function, I realise there may be errors in that as well, but I'm specifically just looking for help fixing the error I get with the board array in canMovL(). Thanks.
bool canMoveL(int i, int j)
should be
bool Board::canMoveL(int i, int j).
else you define free function unrelated to Board.
i am having problem getting the correct object out from the lines
Cell cF = fList[rand() % fList.size()];//choose random frontier cell
Cell cl = inList[rand() % inList.size()];//choose random in cell
whenever i debug using visual studio, i can see a Cell will have it's members added due to the method pushToPrimsFrontierList(Cell& c), however when i try to get the object from the inList or fList, it seems like im not getting that same object reference, because its neighbour list is 0 again.what is happening here?
You can see from the image that during the first iteration a startCell is added to the inList, so when im accessing it it will return me only that one object, however that is not the case, it seems like my object is not even push_backed to the inList vector.
#ifndef __CELL_H_
#define __CELL_H_
#include <vector>
using namespace std;
class Cell{
private:
int x, y, val; // cell co-ordinate
bool visited;
void setX(int);
void setY(int);
vector<Cell> neighbourList;
public:
Cell();
Cell(int,int);
int getX();
int getY();
int getVal();
vector<Cell>& getNeighbourList();
void pushToNeighbourList(Cell&);
void setVal(int);
void setVisited(bool);
bool IsVisited();
bool equals(Cell&);
};
#endif
#ifndef GENMAZE_H
#define GENMAZE_H
#include <vector>
#include <iostream>
#include "cell.h"
using namespace std;
class GenMaze{
private:
int rows;
int cols;
int gridSize;
vector<vector<Cell>> mazeGrid;
vector<Cell> inList;
vector<Cell> fList;
public:
GenMaze(int,int);
void setRows(int);
void setCols(int);
void setGridSize(int);
int getGridSize();
int getRows();
int getCols();
vector<vector<Cell>>& getMazeGrid();
void setMazeGrid(vector<vector<Cell>>);
void setValAt(int,int, Cell);
void printMazeCoords();
void printMazeValue();
bool isOddBlock(int,int);
void Prims();
void pushToPrimsFrontierList(Cell&);
void printCell(Cell&);
void makePath(Cell&, Cell&);
void removeFromfList(Cell&);
};
#endif
void GenMaze::Prims(){
Cell startCell = mazeGrid[1][1];//startCell
inList.push_back(startCell);
pushToPrimsFrontierList(startCell);
int randomFrontier= 0;
int randomIn = 0;
while (!fList.empty()){
cout<< "e";
Cell cF = fList[rand() % fList.size()];//choose random frontier cell
Cell cl = inList[rand() % inList.size()];//choose random in cell
for (vector<Cell>::size_type i = 0; i != cl.getNeighbourList().size(); i++) {
if (cl.getNeighbourList()[i].equals(cF)){
inList.push_back(cF);
pushToPrimsFrontierList(cF);
makePath(cl, cF);
removeFromfList(cF);
}
}
}
}
void GenMaze::removeFromfList(Cell& c){
for (vector<Cell>::size_type i = 0; i != fList.size(); i++) {
if (fList[i].equals(c)){
fList.erase(fList.begin() + i);
}
}
}
void GenMaze::makePath(Cell& from, Cell& to){
cout << "making path";
//on top
if ((from.getX() - 2 == to.getX()) & (from.getY() == to.getY())){
mazeGrid[from.getX() - 1][from.getY()].setVal(0);
}
//on right
if ((from.getX() == to.getX()) & (from.getY() + 2 == to.getY())){
mazeGrid[to.getX()][from.getY() - 1].setVal(0);
}
//on bottom
if ((from.getX() + 2 == to.getX()) & (from.getY() == to.getY())){
mazeGrid[from.getX() + 1][from.getY()].setVal(0);
}
//on left
if ((from.getX() == to.getX()) & (from.getY() - 2 == to.getY())){
mazeGrid[from.getX()][from.getY() - 1].setVal(0);
}
}
void GenMaze::printCell(Cell& c){
cout << "(" << c.getX() << "," << c.getY() << ")";
}
void GenMaze::pushToPrimsFrontierList(Cell& c){
//push all Cells around the given Cell c, into the frontier list.
if (!(c.getX() - 2 < 0)){
Cell topCell = mazeGrid[c.getX() - 2][c.getY()];
fList.push_back(topCell);
c.pushToNeighbourList(topCell);
}
if (!(c.getY() - 2 < 0)){
Cell leftCell = mazeGrid[c.getX()][c.getY() - 2];
fList.push_back(leftCell);
c.pushToNeighbourList(leftCell);
}
if (!(c.getY() + 2 > getCols() - 1)){
Cell rightCell = mazeGrid[c.getX()][c.getY() + 2];
fList.push_back(rightCell);
c.pushToNeighbourList(rightCell);
}
if (!(c.getX() + 2 > getRows() - 1)){
Cell bottomCell = mazeGrid[c.getX() + 2][c.getY()];
fList.push_back(bottomCell);
c.pushToNeighbourList(bottomCell);
}
}
To the std::vector as pretty much to every STL collection you cannot put the reference to the object. If you do:
Cell c;
std::vector<Cell> myvector1;
std::vector<Cell> myvector2;
myvector1.push_back(c);
myvector2.push_back(c);
When you try to modify c in myvector1 the value won't be propagated to myvector2. This is because push_back adds the element by value not by reference. If you need the real reference to some object you should create collection of pointers to elements and the code should rather look like this:
Cell *c = new Cell;
std::vector<Cell*> myvector1;
std::vector<Cell*> myvector2;
myvector1.push_back(c);
myvector2.push_back(c);
Now when you want to modify element beneith c you just do:
myvector1[indexofc]->somecellfield = othervalue
This is a function in a program replicating Sierpinski's gasket. This function is supposed to attach the points in the triangle for the fractal.
After much deliberation I've figured out where the issue lies:
void add_pts(int &x, int &y)
{
Vector_ref<Rectangle> pt;
for (int i = 0; i < POINTS; ++i)
{
pt_test; //generates changing x, y vals within the limits of the triangle
cout << "pass" << i <<endl;
pt.push_back(new Rectangle(Point(x,y),5,5));
pt[i].set_fill_color(Color::yellow);
win.attach(pt[i]);
}
}
The output is "pass1...pass[POINTS-1]", but for whatever reason it runs when i = POINTS and runs into the segmentation error. I have no clue as to why. Can anyone assist, please?
Here is my code. The pt_test and coord are a bit sloppy but seeing as it can't run properly it's very hard to ascertain what I can streamline.
#include <stdlib.h>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <time.h>
#include "Simple_window.h"
#include "Graph.h"
#include "Point.h"
#include "GUI.h"
#include "Window.h"
using namespace std;
using namespace Graph_lib;
// globals
const int POINTS = 5000;
unsigned int seed = (unsigned int)time(0);
Simple_window win(Point(100,100),1100,700,"Homework 9");
// function declarations
double random(unsigned int &seed);
bool coords(int &x, int &y);
void pt_test(int x, int y);
void add_pts(int &x, int &y);
int main()
{
int x, y;
// title
Text title(Point(400,50), "The Sierpinski Gasket");
title.set_font(Graph_lib::Font::helvetica_bold);
title.set_font_size(25);
title.set_color(Color::cyan);
win.attach(title);
// triangle
Closed_polyline tri;
tri.add(Point(250,75)); // A
tri.add(Point(850,75)); // B
tri.add(Point(550,675)); // C
tri.set_fill_color(Color::white);
tri.set_color(Color::dark_red);
tri.set_style(Line_style(Line_style::solid,3));
win.attach(tri);
// vertices
Text vert_a(Point(225,70), "A (250, 75)");
vert_a.set_font(Graph_lib::Font::helvetica_bold);
vert_a.set_font_size(15);
vert_a.set_color(Color::cyan);
Text vert_b(Point(855,70), "B (850, 75)");
vert_b.set_font(Graph_lib::Font::helvetica_bold);
vert_b.set_font_size(15);
vert_b.set_color(Color::cyan);
Text vert_c(Point(575,670), "C (550, 675)");
vert_c.set_font(Graph_lib::Font::helvetica_bold);
vert_c.set_font_size(15);
vert_c.set_color(Color::cyan);
win.attach(vert_a);
win.attach(vert_b);
win.attach(vert_c);
// point selection
add_pts(x, y);
// window title and display
win.wait_for_button();
}
double random(unsigned int &seed)
{
const int MODULUS = 15749;
const int MULTIPLIER = 69069;
const int INCREMENT = 1;
seed = ((MULTIPLIER*seed)+INCREMENT)%MODULUS;
return double(seed)/double(MODULUS);
}
bool coords(int &x, int &y) // generates the points
{
x = int(251 + 600*random(seed));
y = int(76 + 600*random(seed));
if( y > (2*x-425) && x<= 550 || x>=550 && y < (-2*x + 1775))
return true;
}
void pt_test(int x, int y) // tests the points until they are within the range
{
coords;
while(coords == 0)
coords;
}
void add_pts(int &x, int &y) // attaches the points as shapes
{
Vector_ref<Rectangle> pt;
for (int i = 0; i < POINTS; ++i)
{
pt_test;
cout << "i == " << i << " points == " << POINTS << endl;
pt.push_back(new Rectangle(Point(x,y),5,5));
pt[i].set_fill_color(Color::yellow);
win.attach(pt[i]);
}
}
I've also noticed that the function add_pts doesn't work when the body is in the loop, but if you put the body in int_main(), it runs indefinitely but doesn't reach the segmentation fault as quickly, if at all.
This is for a poker game and I have class PokerTable defined in PokerTable.h
#include <iostream>
using namespace std;
class PokerTable
{
private:
int numPlayers;
int numPlaying;
int dealerPos;
int bigBlind;
int potSize;
int betSize;
bool flop;
bool turn;
bool river;
public:
//constructors
PokerTable();
PokerTable(int,int,int,int,int,bool,bool,bool);
//getters
int getNumPlayers(){return numPlayers;};
int getDealerPos(){return dealerPos;};
int getBigBlind(){return bigBlind;};
int getNumPlaying(){return numPlaying;};
int getPotSize(){return potSize;};
int getBetSize(){return betSize;};
bool getFlop(){return flop;};
bool getTurn(){return turn;};
bool getRiver(){return river;};
//void buttonShow(int);
//setters
void setBetSize(int inBetSize){betSize = inBetSize;};
void setBigBlind(int inBigBlind){bigBlind = inBigBlind;};
void setNumPlaying(int inNumPlaying){numPlaying = inNumPlaying;};
void setPotSize(int inPotSize){potSize = inPotSize;};
void setFlop(bool inFlop){flop = inFlop;};
void setTurn(bool inTurn){turn = inTurn;};
void setRiver(bool inRiver){river = inRiver;};
void setNumPlayers(int inPlayers){numPlayers = inPlayers;};
void setDealerPos(int inDealerPos){dealerPos = inDealerPos;};
};
PokerTable::PokerTable()
{
numPlayers = 9;
numPlaying = 9;
dealerPos = 1;
bigBlind = 20;
flop = false;
turn = false;
river = false;
}
PokerTable::PokerTable(int playerNum, int playingCount, int posDealer, int blindBig,int inPotSize, bool inFlop,bool inTurn,bool inRiver)
{
numPlayers = playerNum;
numPlaying = playingCount;
dealerPos = posDealer;
potSize = inPotSize;
bigBlind = blindBig;
flop = inFlop;
turn = inTurn;
river = inRiver;
}
In my watch list pokerTable.numPlayers has a random value up to 4 million before I even execute this next line of code.
PokerTable aPokerTable(9,9,1,20,30,false,false,false);
and afterwards here is pokerTable in my watch list:
- aPokerTable { numPlayers=2990892 numPlaying=9 dealerPos=9 ...} PokerTable
betSize 30 int
bigBlind 1 int
dealerPos 9 int
flop false bool
numPlayers 2990892 int
numPlaying 9 int
potSize 20 int
river false bool
turn false bool
Can anyone tell me why all the values are not what I declared them to be??!?!!
And how I can fix this?
This is Form1.h
#pragma once
#include "PokerTable.h"
#include "Card.h"
#include <time.h>
#include "PokerPlayer.h"
#include <fstream>
#include <string>
#include <sstream>
//global variables
//TODO make players start from 0
int firstPlayer;
int deck[52];
int nextCard=0;
PokerTable aPokerTable(9,9,1,20,30,false,false,false);
PokerPlayer players[9]; //however many players
ofstream gameLog;
/*
void setTable()
{
aPokerTable.setNumPlayers(9);
aPokerTable.setNumPlaying(9);
aPokerTable.setDealerPos(1);
aPokerTable.setBigBlind(20);
aPokerTable.setPotSize(30);
aPokerTable.setBetSize(20);
aPokerTable.setFlop(false);
aPokerTable.setTurn(false);
aPokerTable.setRiver(false);
}
*/
string convertInt(int number) //convert to string
{
stringstream ss;//create a stringstream
ss << number;//add number to the stream
return ss.str();//return a string with the contents of the stream
}
void createPlayers()
{
// aPokerTable.setNumPlayers(9);
for(int x=0;x<=(aPokerTable.getNumPlayers()-1);x++)
{
players[x] = *(new PokerPlayer(1000,(aPokerTable.getDealerPos())+1,false,0,1));//1000 chips, position i+1, not folded
}
}
void playRound()
{
int action;
for(int playerTurn = firstPlayer; playerTurn <= aPokerTable.getNumPlayers()+firstPlayer; playerTurn++)
{
if(players[playerTurn].getFold() == false)
{
if(aPokerTable.getNumPlaying() == 1)
{
players[playerTurn].setChipStack(players[playerTurn].getChipStack() + aPokerTable.getPotSize()); //player wins pot
}
else //there is more than one person playing
{
action = players[playerTurn].action(); //0 is check/fold, value is call/bet/raise,
if(action > aPokerTable.getBetSize())
{
aPokerTable.setBetSize(action);
aPokerTable.setPotSize(aPokerTable.getPotSize() + action);
playerTurn = playerTurn - aPokerTable.getNumPlayers();
}
else if (action == aPokerTable.getBetSize()) //call
{
aPokerTable.setPotSize(aPokerTable.getPotSize() + action);
}
else //action < aPokerTable.betSize
{
players[playerTurn].setFold(true);
aPokerTable.setNumPlaying(aPokerTable.getNumPlaying()-1); //removes player from playing tally
}
}
}
}
}
void randomDeck()
{
int random_integer;
int tempCard;
//srand((unsigned)time(0));
for(int j=0;j<=51;j++)
{
deck[j] = j;
}
for(int i=51; i>=1; i--)
{
random_integer = rand()%(i); //a random number between 0 and i
tempCard = deck[i];
deck[i] = deck[random_integer]; //put the random card from unshuffled deck into slot i of the deck
deck[random_integer] = tempCard; //put whatever was at slot i into the random slot
}
}
void dealCards()
{
for(int j=1;j<=aPokerTable.getNumPlayers();j++)
{
players[j].setCard1(deck[nextCard]);
nextCard++;
players[j].setCard2(deck[nextCard]);
nextCard++;
}
}
void playPreFlop()
{
aPokerTable.setBetSize(aPokerTable.getBigBlind());
aPokerTable.setFlop(false); //it is before the flop
aPokerTable.setTurn(false);
aPokerTable.setRiver(false);
randomDeck(); //shuffle cards
dealCards();
firstPlayer = (aPokerTable.getDealerPos() + 3)%(aPokerTable.getNumPlayers()); // first player is left of blinds between 0 and numplayers
playRound();
}
void playFlop()
{
aPokerTable.setFlop(true);
firstPlayer = (aPokerTable.getDealerPos())%aPokerTable.getNumPlayers(); // first player is left of dealer between 0 and numplayers
aPokerTable.setBetSize(0);
playRound();
}
void playTurn()
{
aPokerTable.setTurn(true);
firstPlayer = (aPokerTable.getDealerPos())%aPokerTable.getNumPlayers(); // first player is left of dealer between 0 and numplayers
aPokerTable.setBetSize(0);
playRound();
}
void playRiver()
{
aPokerTable.setRiver(true);
firstPlayer = (aPokerTable.getDealerPos())%(aPokerTable.getNumPlayers()); // first player is left of dealer between 0 and numplayers
aPokerTable.setBetSize(0);
playRound();
if(aPokerTable.getNumPlaying() >=2)
{
//showDown();
}
}
/*
void showDown()
{
}
*/
This is pokerPlayer.h
using namespace std;
class PokerPlayer
{
private:
int chipStack,position;
bool fold;
int card1,card2;
public:
//constructors
PokerPlayer();
PokerPlayer(int,int,bool,int,int);
//getters
int getChipStack() {return chipStack;}
int getPosition() {return position;}
int getCard1(){return card1;}
int getCard2(){return card2;}
bool getFold(){return fold;}
//setters
void setChipStack(int inChips){chipStack = inChips;}
void setPosition(int inPos){position = inPos;}
void setCard1(int inCard1){card1 = inCard1;}
void setCard2(int inCard2){card2 = inCard2;}
void setFold(bool inFold){fold = inFold;}
int action();
};
PokerPlayer::PokerPlayer()
{
chipStack = 1000;
position = 0;
fold=false;
card1 = 0;
card2 = 1;
}
PokerPlayer::PokerPlayer(int inChipStack,int inPos, bool inFold, int inCard1, int inCard2)
{
chipStack = inChipStack;
position = inPos;
fold = inFold;
card1 = inCard1;
card2 = inCard2;
}
int PokerPlayer::action()
{
return 0;
}
aPokerTable { numPlayers=2990892 numPlaying=9 dealerPos=9 ...}
Note that dealerPos got assigned the value 9, that's wrong as well. If you look closely, you'll see that everything is shifted by 4 bytes.
Two possible reasons. The debugger could have picked the wrong address for aPokerTable, the actual address minus 4. That's unlikely. Or there's a mismatch between the definition of the PokerTable class as seen by pokertable.cpp and the other .cpp files that #include the pokertable.h include file. Where pokertable.cpp saw an extra member before the numPlayers member. Maybe you edited the header and deleted that member but ended up not recompiling pokertable.cpp for some mysterious reason. Build + Rebuild to fix. Do panic a bit if this actually works.
It's because in C++ before the constructor is called, variable uses the value that it already contains in its memory location that is a "random" value
I cannot reconstruct it because i dont have the full code. However, a random value near 4 million sounds like a pointer. When you store or retrieve a member variable maybe you did not de-reference the pointer. Please post the rest of the code so we can check if that's the case.
players[x] = *(new PokerPlayer(...));
That is a memory leak. What you probably want is:
players[x] = PokerPlayer(1000,(aPokerTable.getDealerPos())+1,false,0,1);