Reassigning position within an array - c++

I've been working on a dungeon crawling game, and I've hit an issue with moving the player's coordinates throughout my 2-d array. Currently, when choosing a move, the move is passed into the function, but the position is not reassigned. I've tried both pass my reference and by value.
Currently as written, the program gets the players move, but I don't think it's actually being passed into the updateMove function. I tried having the function cout << playerMove, but it doesn't show anything. Am I doing something to cause the move to not be passed into the updateMove function? I'm guessing this is why the move isn't updating.
I've added my main and the function in question:
int main()
{
// initialize random #
srand(static_cast<int>(time(NULL)));
do // play again loop
{
// define variables
bool win, lose;
char playerMove;
char dungeon[MAX_ROW][MAX_COL];
int cash;
displayInstructions(); // display instructions
initializeDungeon(dungeon); // initialize dungeon
do
{
setTraps(dungeon); // set traps
displayDungeon(dungeon); // display dungeon
playerMove = getMove(playerMove);
// check for valid move
// reassign player to new position
updateMove(playerMove, dungeon);
//update map now that move is legit
if (!win && !lose)
{
updateDungeon();
}
} while (!win && !lose);
} while (repeat());
}
void updateMove(char & playerMove, char dungeon[MAX_ROW][MAX_COL])
{
int px, py;
int dx = 0, dy = 0;
if(playerMove == UP && px != 0)
{
dx--;
}
else if (playerMove == DOWN && px != 7)
{
dx++;
}
else if (playerMove == LEFT && py != 0)
{
dy--;
}
else if (playerMove == RIGHT && py != 8)
{
dy++;
}
if(dx != 0 || dy != 0)
{
dungeon[px][py] = SPACE; // I updated this to be an 'empty' space based on my variables
px += dx;
py += dy;
dungeon[px][py] = PLAY;
}
} ````

Needing to iterate over the entire map to find the player is rather wasteful and slow. It also makes coding harder as you're seeing here.
You should store the player's position somewhere so you know it at a glance and can refer back to it for clearing the old position.
Something like this:
int px, py;
void updateMove(char playerMove, char dungeon[MAX_ROW][MAX_COL])
{
int dx = 0, dy = 0;
if(playerMove == 'u' && px != 0)
{
dx--;
}
else if (playerMove == 'd' && px != 8)
{
dx++;
}
else if (playerMove == 'l' && py != 0)
{
dy--;
}
else if (playerMove == 'r' && py != 9)
{
dy++;
}
if(dx != 0 || dy != 0) {
dungeon[px][py] = EMPTY;
px += dx;
py += dy;
dungeon[px][py] = PLAY;
}
}

Related

mesh.delete_face() caused abort()

I have tried to run the code below but got a error when using mesh.delete_face(*it, false);
vector<TriMesh::FaceHandle> terminalFaces;
OpenMesh::FPropHandleT<int> faceType;
OpenMesh::FPropHandleT<bool> faceChecked;
mesh.add_property(faceType, "face-type");
mesh.add_property(faceChecked, "face-checked");
tFillFacesProperty(mesh, faceChecked, false);
tClassifyFaces(mesh, faceType);
terminalFaces = tSplitJointFaces(mesh, faceType);
tClassifyFaces(mesh, faceType);
tAdjustAllVertices(mesh);
for (int i = 0; i < terminalFaces.size(); i++)
{
if (mesh.property(faceType, terminalFaces[i]) != FT_TERMINAL)
continue;
if (mesh.property(faceChecked, terminalFaces[i]))
continue;
vector<TriMesh::VertexHandle> collectedVs;
vector<TriMesh::FaceHandle> collectedFs;
for (TriMesh::FaceVertexIter fvi = mesh.fv_begin(terminalFaces[i]);
fvi != mesh.fv_end(terminalFaces[i]);
++fvi)
{
if (mesh.valence(*fvi) == 2) {
collectedVs.push_back(*fvi);
break;
}
}
assert(collectedVs.size() == 1);
while(1){
bool mustStop = false;
if (collectedFs.empty())
collectedFs.push_back(terminalFaces[i]);
else {
TriMesh::FaceHandle nextFh;
for (TriMesh::FaceFaceIter ffi = mesh.ff_begin(collectedFs.back());
ffi != mesh.ff_end(collectedFs.back());
++ffi)
{
if ((find(collectedFs.begin(), collectedFs.end(), *ffi) == collectedFs.end())
&& mesh.is_valid_handle(*ffi)
&& mesh.property(faceType, *ffi) != FT_NEW)
{
nextFh = *ffi;
break;
}
}
if (mesh.is_valid_handle(nextFh))
collectedFs.push_back(nextFh);
else
mustStop = true;
}
TriMesh::FaceHandle curfh = collectedFs.back();
mesh.property(faceChecked, curfh) = true;
int t = mesh.property(faceType, curfh);
if (t == FT_SLEEVE || t == FT_TERMINAL) {
TriMesh::HalfedgeHandle hh2check;
for (TriMesh::FaceHalfedgeIter fhi = mesh.fh_begin(curfh);
fhi != mesh.fh_end(curfh);
++fhi)
{
TriMesh::FaceHandle oppof = mesh.opposite_face_handle(*fhi);
if (mesh.is_valid_handle(oppof)
&& (find(collectedFs.begin(), collectedFs.end(),oppof) == collectedFs.end()))
{
hh2check = *fhi;
break;
}
}
assert(mesh.is_valid_handle(hh2check) && !mesh.is_boundary(hh2check));
// add new vertices
TriMesh::VertexHandle right, left;
right = mesh.from_vertex_handle(hh2check);
left = mesh.to_vertex_handle(hh2check);
assert(mesh.is_valid_handle(left) && mesh.is_valid_handle(right));
if ((find(collectedVs.begin(), collectedVs.end(), left) == collectedVs.end())
&& mesh.is_valid_handle(left))
collectedVs.insert(collectedVs.begin(), left);
if ((find(collectedVs.begin(), collectedVs.end(), right) == collectedVs.end())
&& mesh.is_valid_handle(right))
collectedVs.push_back(right);
bool inCircle = true;
TriMesh::Point center = (mesh.point(right) + mesh.point(left)) / 2.0;
double diamSq = tSqDist(mesh.point(right), mesh.point(left));
for (int i = 1; i < collectedVs.size() - 1; i++)
{
TriMesh::VertexHandle vh = collectedVs[i];
if (tSqDist(mesh.point(vh), center) * 4 > diamSq) {
inCircle = false;
break;
}
}
if (inCircle && !mustStop) {
continue;
}
else {
OpenMesh::IO::write_mesh(mesh, "outputtr.off");
for (vector<TriMesh::FaceHandle>::iterator it = collectedFs.begin(); it != collectedFs.end(); ++it){
mesh.delete_face(*it, false);
}
I used delete_face() in openmesh to remove one of the faces in mesh.
However, debugging always shows abort() and the error panel just like:
R6010 -abort() has been called
and the breakpoint was triggered by _CrtDbgBreak():
case 1: _CrtDbgBreak(); msgshown = 1;
Maybe the error is caused by a dangling reference/pointer, or an invalidated iterator. How could I fix it?
Just add "mesh.request_face_status();" and solved.

Tests randomly fails

I'm writing board game and I need following functionality: player rolls two dices, if he rolled doubles (same number on both dice), he gets to roll again, if he rolled doubles again, he goes to jail.
In my Game class it looks like that
void logic::Game::rollTheDice() {
m_throwsInCurrentTurn++;
int firstThrow = m_firstDice.roll();
int secondThrow = m_secondDice.roll();
m_totalRollResult += firstThrow + secondThrow;
if (firstThrow == secondThrow) m_doublesInCurrentTurn++;
}
std::string logic::Game::checkForDoubles() {
std::string message;
if (m_doublesInCurrentTurn == 0 && m_throwsInCurrentTurn == 1) {
m_canThrow = false;
m_canMove = true;
}
if (m_doublesInCurrentTurn == 1 && m_throwsInCurrentTurn == 1) {
message = "Doubles! Roll again.";
m_canThrow = true;
m_canMove = false;
}
if (m_doublesInCurrentTurn == 1 && m_throwsInCurrentTurn == 2) {
m_canThrow = false;
m_canMove = true;
}
if (m_doublesInCurrentTurn == 2 && m_throwsInCurrentTurn == 2) {
message = "Doubles again! You are going to jail.";
m_canThrow = false;
m_canMove = false;
getActivePlayer().lockInJail();
}
return message;
}
void logic::Game::setInMotion(unsigned number) {
m_players[m_activePlayer].startMoving();
m_players[m_activePlayer].incrementPosition(number);
}
m_canThrow basicly enables or disables ability to click "Roll the Dice" button, m_canMove decides if player token can start moving, m_players[m_activePlayer] is std::vector<Player>, startMoving() does that,
void logic::Player::startMoving() {
m_isMoving = true;
}
needed for token movement, so baiscly not relevant here.
Last function from Game class I need to show you is reset(), used mainly for testing purposes
void logic::Game::reset() {
m_throwsInCurrentTurn = 0;
m_doublesInCurrentTurn = 0;
m_totalRollResult = 0;
}
Now finnaly Unit Test that sometimes goes wrong. Sometimes, I mean completely random, like 1 out of 10-20 times.
//first throw is double, second throw is not
TEST_F(GameTestSuite, shouldFinishAfterSecondRollAndMove) {
auto game = m_sut.get();
do {
if (game.getThrowsInCurrentTurn() == 2) game.reset();
game.rollTheDice();
game.checkForDoubles();
if (game.getThrowsInCurrentTurn() == 1 && game.getDoublesInCurrentTurn() == 1) {
ASSERT_EQ(game.canThrow(), true);
ASSERT_EQ(game.canMove(), false);
}
} while (game.getThrowsInCurrentTurn() != 2 && game.getDoublesInCurrentTurn() != 1);
ASSERT_EQ(game.canThrow(), false);
ASSERT_EQ(game.canMove(), true);
game.setInMotion(game.getTotalRollResult());
ASSERT_EQ(game.getActivePlayer().isMoving(), true);
ASSERT_EQ(game.getActivePlayer().getPosition(), game.getTotalRollResult());
}
This line exactly, ASSERT_EQ(game.canThrow(), false); sometimes is equal true after do-while loop that should end once m_canThrow is set to false
Shouldn't:
} while (game.getThrowsInCurrentTurn() != 2 && game.getDoublesInCurrentTurn() != 1);
be
} while (game.getThrowsInCurrentTurn() != 2 && game.getDoublesInCurrentTurn() <= 1);
You want to allow up to two turns but 0 or 1 doubles.

Arduino method making

The code below is part of my "safe" program. I have to make 4 "guesses" (the password consists of 4 numbers). For each guess I use a few if statements but i was wondering if there is a way to make a method for each "guess". This way instead of these 4 sections of if-statements i would just have one Method.
if (turn == 1) {
if ((buttonState != lastButtonState) && buttonState == 1) {
guess1 = newValue;
turn = 2;
Printer();
}
lastButtonState = buttonState;
}
if (turn == 2) {
if ((buttonState != lastButtonState) && buttonState == 1) {
guess2 = newValue;
turn = 3;
Printer();
}
lastButtonState = buttonState;
}
if (turn == 3) {
if ((buttonState != lastButtonState) && buttonState == 1) {
guess3 = newValue;
turn = 4;
Printer();
}
lastButtonState = buttonState;
}
if (turn == 4) {
if ((buttonState != lastButtonState) && buttonState == 1) {
gok4 = nieuwewaarde;
beurt = 5;
Printer();
}
lastButtonState = buttonState;
}
Theoretically it should be possible to shrink it to something like this. Note you should use an array instead of a series of guess1, guess2, … variables.
int guess[4];
if ((buttonState != lastButtonState) && buttonState == 1) {
guess[turn-1] = newValue;
if (turn == 4)
{
gok4 = nieuwewaarde;
beurt = 5;
}
else
turn = turn+1;
Printer();
}
lastButtonState = buttonState;
You can adjust the +1/-1 if you count turn from 0. But with a broader view of what you would like to achieve, there might be other ways to write things.
Besides, Arduino code is not in C++, but AVR. The “user” code is Processing.

Can't figure out how to loop playerturns and moves Tic Tac Toe (C++) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
EDIT: Solved now thank you triple_r and AJNeufield for your help on this problem I was having.
I've looked around multiple websites and YouTube about this and I can't seem to find anything on what I am specifically looking for this as my format for the program is a good bit different than others. Therefore, it's hard to decipher where I need to put the things I do need that I know of.
Please note that I'm relatively new to C++ so I'd appreciate all the feedback or criticism you might provide me.
Also, note my code does compile and run it just does not allow me to put in more than one input and more than likely does not allow for a switch of player turns.
Quick Edit: Switched the code with the new setup suggested by triple_r but I seemed to have messed it up somewhere along the line and it does compile(with the exception of x and y not being utilized and one other error) but it always starts off with player 2 going first and as soon as it receives input it ends automatically with a segmentation fault.
#include <iostream>
#include <cstdlib>
using namespace std;
//////////////////////////////////////////////////////////
void initboard(char board[3][3])
{
int x,y;
for (x=0;x<3;x++)
for (y=0;y<3;y++)
board[x][y]=' ';
return;
}
//////////////////////////////////////////////////////////
void printboard(char board[3][3])
{
int x,y;
for (x=0;x<3;x++)
{
cout<<"\n";
for (y=0;y<3;y++)
{
cout<<" "<<board[x][y]<<" ";
if (y<2) cout<<"|";
}
if (x<2) cout<<"\n===========";
}
return;
}
//////////////////////////////////////////////////////////
void getmove(char board[3][3], int player)
{
return;
}
//////////////////////////////////////////////////////////
int main()
{
bool done=false;
char board[3][3];
int x,y,player=1,turn,playerchoice,playermark;
initboard(board);
turn=0;
do
{
if (player==1)
playermark='X';
else
playermark='O';
if (turn%2)
player=1;
else
player=2;
cout<<"Player "<<player<<" where do you want to move?: ";
cin>>playerchoice;
if (playerchoice==1)
{
board[0][0]=playermark;
}
else if (playerchoice==2)
{
board[0][1]=playermark;
}
else if (playerchoice==3)
{
board[0][2]=playermark;
}
else if (playerchoice==4)
{
board[1][0]=playermark;
}
else if (playerchoice==5)
{
board[1][1]=playermark;
}
else if (playerchoice==6)
{
board[1][2]=playermark;
}
else if (playerchoice==7)
{
board[2][0]=playermark;
}
else if (playerchoice==8)
{
board[2][1]=playermark;
}
else if (playerchoice==9)
{
board[2][2]=playermark;
}
else
{
cout<<"Invalid move ";
}
if (board[x][y]!=' ')
cout<<"Move is already taken.";
board[x][y]=playermark;
if(board[x][y]==' ')
turn++;
}while (!done);
void printboard(char board[3][3]);
return 0;
}
EDIT: based on the updated code
So, the first thing I can see is that you are using x and y in your program but you don't initialize them or assign any values to them. Also, try to use functions/classes/... yo make your code more readable. You already have a function for player move but you are not using it. You can move the large if statement inside that function and that will make your main code shorter and more readable.
Here are my comments on the main part of your program:
int main()
{
// add a new variable to see if the move was valid or not:
bool done=false, validmove = true;
char board[3][3];
int x, y, player = 1, turn = 0, playerchoice, playermark;
initboard(board);
do
{
// swap the two `if`s so you decide who`s turn it is then assign the player mark,
// also, reverse the condition to make sure turn '0' is player 1's turn.
if (!(turn % 2))
player = 1;
else
player = 2;
if (player == 1)
playermark = 'X';
else
playermark = 'O';
cout << "Player " << player << " where do you want to move?: ";
cin >> playerchoice;
// Assign `x` and `y` here instead of updating the board, because you want to make
// sure that the move is valid before putting the mark:
validmove = true;
if (playerchoice == 1)
{
x = 0; y = 0;
}
else if (playerchoice == 2)
{
x = 0; y = 1;
}
else if (playerchoice == 3)
{
x = 0; y = 2;
}
else if (playerchoice == 4)
{
x = 1; y = 0;
}
else if (playerchoice == 5)
{
x = 1; y = 1;
}
else if (playerchoice == 6)
{
x = 1; y = 2;
}
else if (playerchoice == 7)
{
x = 2; y = 0;
}
else if (playerchoice == 8)
{
x = 2; y = 1;
}
else if (playerchoice == 9)
{
x = 2; y = 2;
}
else
{
cout << "Invalid move, try again!";
// Make sure to mark the move as invalid so they get a chance to
// change their move:
validmove = false;
}
// check to see if the turn was valid:
if(validmove)
{
if (board[x][y] != ' ')
{
cout << "Move is already taken, try again";
}
else
{
board[x][y] = playermark;
turn++;
}
}
// have to make sure you have a condition for end of game. A simple
// one is to check if turn is less than `9`, otherwise the board is
// full:
if(turn == 9)
done = true;
// you probably want to add a few more checks to see who won the game.
}while (!done);
// when calling a function, no need to put the return type or parameter type:
printboard(board);
return 0;
}
========================================================================
There are two do-while loops in your program and both seem to be meant as a game loop. What I would do is:
initboard(...);
turn = 0;
do{
//this is the game loop
...;
if( validturn )
turn++;
}while(!done);
release_resources(...);
return 0;
so, you fold everything into one loop. In that loop, you want to:
find who's turn it is:
if (turn % 2)
player = 1;
else
player = 2;
get users input:
std::cin >> playerchoice;
...
convert player choice to grid location:
switch ( move )
{
case 0:
x = 0;
y = 0;
break;
case 1:
...;
...
default:
//invalid move
}
see if the move is valid:
if( board[x][y] != ' ' )
//already taken, invalid move
then apply the move:
board[x][y] = playermark;
I hope this helps.
Your cin >> playerchoice is outside your do { ... } while ( moves != 9); loop. Move it inside.

How to change a value in an array C++

This function is for code to play a game of tic tac toe:
//--------------------FUNCTION--------------------
bool playCell(int player, int position, int gameBoard[]) {
if (gameBoard[position] == 10 || gameBoard[position] == 11) {
return false;
} else {
return true;
if (player == 0){
gameBoard[position] = 10;
} else {
gameBoard[position] = 11;
} // end if
}
} // end function
playCell takes a player (0 for "O", 1 for "X"), a position (1 to 9), and the nine element gameBoard, and returns true if the move is legal (i.e. that spot is not yet taken), and false otherwise. If the move is legal it changes the position to that players number (10 for "O", 11 for "X"). If the player or position input is invalid, it returns false.
I'm trying to figure out how to get the array to change its value to either a 10 or 11 depending on the player, and saving to the position they entered to play in.
The return keyword redirect the program flow back to the main program. So the code after return will not be executed. Change the position of return:
//--------------------FUNCTION--------------------
bool playCell(int player, int position, int gameBoard[])
{
if (gameBoard[position] == 10 || gameBoard[position] == 11)
{
return false;
}
else
{
if (player == 0)
{
gameBoard[position] = 10;
}
else
{
gameBoard[position] = 11;
} // end if
return true;
}
} // end function
You have a return statement prior to your array assignment here:
return true; // HERE
if (player == 0){
gameBoard[position] = 10;
} else {
gameBoard[position] = 11;
} // end if
this causes your code not to be executed. Remove this line from there and put in the correct place.