I am literally having a hellish time debugging this code.
This code is written to take each player on the board, and move them forward until one of the players reaches the end, but instead it looks like the Player::position is never actually changed. I've tried this with both MinGW C++11 and the g++ installed on my ubuntu remote with flag -std=c++11
.
Please ask questions, or make suggestions, I just don't understand.
#include <iostream>
#include <cstdlib>
#include <vector>
#include <time.h>
using namespace std;
class Player {
string name;
int position;
public:
Player (string p_name):
name(p_name),
position(1)
{
};
int move (int diff) {
position = diff + position;
return position;
};
int get_position () { return position; };
string get_name () { return name; }
};
class GameBoard {
private:
vector<Player> players;
int total_players;
int turn_index;
bool has_winner;
int winning_space;
int dice_sides;
int max_penalty_spaces;
int min_penalty_spaces;
int roll_dice () { return rand() % dice_sides + 1; };
public:
GameBoard (vector<Player> & p_players):
players(p_players),
total_players(p_players.size()),
turn_index(-1),
has_winner(false),
winning_space(20),
dice_sides(6),
max_penalty_spaces(3),
min_penalty_spaces(1)
{
};
bool get_has_winner () { return has_winner; };
Player get_last_player () { return players[turn_index]; };
void move_next_player () {
turn_index = (turn_index + 1) % total_players;
Player current_player = players[turn_index];
cout << ¤t_player << " - test\n";
int spaces_to_move = roll_dice();
int player_space = current_player.move(spaces_to_move);
if (player_space >= winning_space) {
has_winner = true;
} else {
cout << current_player.get_name() << " moved forward " << spaces_to_move << " spaces to space: " << player_space << ".\n";
// Iterate through other players
for (int i = 1; i < total_players - 1; ++i) {
Player other_player = players[(turn_index + i) % total_players];
if (player_space == other_player.get_position()) {
// Player lands on other player's position
int spaces_back = rand() % (max_penalty_spaces - min_penalty_spaces + 1) + min_penalty_spaces;
other_player.move(-1 * spaces_back);
cout << current_player.get_name() << " landed on " << other_player.get_name() << "'s space, causing ";
cout << other_player.get_name() << " to take a tumble and move backwards " << spaces_back << " spaces.\n";
}
}
cout << current_player.get_name() << current_player.get_position() << " " << ¤t_player << " - test\n";
}
}
};
int main () {
srand(time(NULL));
vector<Player> players {
Player("Jack"),
Player("Jill")
};
GameBoard game{players};
while (game.get_has_winner() != true) {
game.move_next_player();
}
Player winner = game.get_last_player();
cout << winner.get_name() << " has won!\n";
//delete[] &players;
//delete &game;
return 0;
}
The out put is also kinda funny:
Jack moved forward 6 spaces to space: 7.
Jack7 0x7fffd1d9a6b0 - test
0x7fffd1d9a6b0 - test
Jill moved forward 4 spaces to space: 5.
Jill5 0x7fffd1d9a6b0 - test
0x7fffd1d9a6b0 - test
Jack moved forward 6 spaces to space: 7.
Jack7 0x7fffd1d9a6b0 - test
0x7fffd1d9a6b0 - test
Showing that both Jack and Jill are sharing the same address.
Aucun commentaire:
Enregistrer un commentaire