• Assistance needed with programming assignment
    3 replies, posted
Hey all, I am assigned to create a checkers "simulation". That is that instead of actually making a checkers game, I just have to make a program that will calculate how many jumps a checker piece can make from a spot on the board. I have it all programmed, and it works. However, if I make my piece jump over the "opponents" piece more than once, it will remove the opponents piece, but it won't update the position of my piece, making it so I can no longer jump as my piece simply stops "existing" (it just disappears). In the grid (8x8 integer array) a 0 means no piece occupys that position, a 1 is the players piece (me/you), and a 2 is the opponents piece (the opponent is just a set of checker pieces that never move). To tell the program what to do, you input 4 numbers seperated by commas, like: PlayerPositionX, PlayerPositionY, OpponentsPositionX, OpponentsPositionY, it will then tell you if your piece can jump over the opponents, and if it can it will remove the opponents piece and reposition the player to the correct spot (the bottom left or right of the opponents piece, depending on where the players piece originated). This is the game board as it is in the simulation, 2 player pieces and 7 opponent pieces: (garry made my game board skewed, but it is 8x8) 0 0 0 0 1 0 0 0 0 1 0 0 0 2 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 2 0 2 0 0 0 0 0 0 0 0 0 0 0 The board's X/Y coords are a little different: They start at the top left corner, X increases as you go down and Y increases as you go to the right. I have been trying to fix it and I can't seem to figure out what is wrong. It isn't the cleanest code and it probably doesn't conform to the ANSI/ISO C++ Standard :v: , but I commented most of what I have so it shouldn't be to bad to read. [CODE] //-------------------------------------------------------------------------------------- // File: Main.cpp //-------------------------------------------------------------------------------------- #include <iostream> // For Basic I/O #include <string> // For std::string #include <sstream> // For std::stringstream #include <vector> // For std::vector #include <cstdlib> // For atoi() using namespace std; //-------------------------------------------------------------------------------------- // Class: CGame, handles all functions related to the checker grid and pieces. //-------------------------------------------------------------------------------------- class CGame { public: CGame() { // Initalize the checker board to be empty for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { Grid[i][j] = 0; } } } ~CGame() { } // Adds a checker to the game void AddChecker(int Team, int PositionX, int PositionY) { sChecker Checker; Checker.Team = Team; Checker.PositionX = PositionX; Checker.PositionY = PositionY; Checkers.push_back(Checker); } // Updates the grid void PopulateGrid() { for (int i = 0; i < Checkers.size(); i++) { Grid[Checkers[i].PositionX-1][Checkers[i].PositionY-1] = Checkers[i].Team; } } // Moves a checker piece bool move(int PX, int PY, int OX, int OY) { // Subtract 1 from all four variables so they will be correct in the array PX -= 1; PY -= 1; OX -= 1; OY -= 1; // Check if players coords is actually a player if (Grid[PX][PY] != 1) return false; // Check if opponents coords is actually an opponent if (Grid[OX][OY] != 2) return false; // Is piece on the left side of the opponents if (PX < OX && PY < OY) { // Is position ajacent to the players piece the opponent if (PX + 1 == OX && PY + 1 == OY) { // Is position ajacent to opponents piece empty if (Grid[OX+1][OY+1] == 0) { // Loop through the vector's elements to find the correct checker piece for (int i = 0; i < Checkers.size(); i++) { // Find the correct checker piece in the vector if (Checkers[i].PositionX == PX + 1 && Checkers[i].PositionY == PY + 1) { // Reset the players oringal position to 0 Grid[PX][PY] = 0; // Position the players checker after a successful jump Checkers[i].PositionX = OX + 2; Checkers[i].PositionY = OY + 2; // Update the game grid PopulateGrid(); } if (Checkers[i].PositionX == OX + 1 && Checkers[i].PositionY == OY + 1) { // Remove the opponents checker Checkers[i].Team = 0; // Update the game grid PopulateGrid(); } } // Jump was successful return true; } } } // Is piece on the right side of the opponents if (PX < OX && PY > OY) { // Is position ajacent to the players piece the opponent if (PX + 1 == OX && PY - 1 == OY) { // Is position ajacent to opponents piece empty if (Grid[OX+1][OY-1] == 0) { // Loop through the vector's elements to find the correct checker piece for (int i = 0; i < Checkers.size(); i++) { // Find the correct checker piece in the vector if (Checkers[i].PositionX == PX + 1 && Checkers[i].PositionY == PY + 1) { // Reset the players oringal position to 0 Grid[PX][PY] = 0; // Position the players checker after a successful jump Checkers[i].PositionX = OX + 2; Checkers[i].PositionY = OY - 2; // Update the game grid PopulateGrid(); } if (Checkers[i].PositionX == OX + 1 && Checkers[i].PositionY == OY + 1) { // Remove the opponents checker Checkers[i].Team = 0; // Update the game grid PopulateGrid(); } } // Jump was successful return true; } } } } // Displays the checker grid void PrintGrid() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { cout << Grid[i][j] << " "; if (j == 7) cout << "\n"; } } } private: // Create 8x8 integer array to hold values for checker locations. int Grid[8][8]; // Defines a checker piece struct sChecker { int Team, PositionX, PositionY; bool King; }; // Vector to hold the checker pieces vector<sChecker> Checkers; }; //-------------------------------------------------------------------------------------- // Main entry point. //-------------------------------------------------------------------------------------- int main() { // Application state, 1 = running, 0 = exit bool bAppState = 1; // Main input string string Input; // Create game class CGame Game; // Create the checkers needed for the scenario. Game.AddChecker(1, 1, 5); Game.AddChecker(1, 2, 2); Game.AddChecker(2, 2, 6); Game.AddChecker(2, 4, 6); Game.AddChecker(2, 6, 6); Game.AddChecker(2, 3, 3); Game.AddChecker(2, 5, 3); Game.AddChecker(2, 7, 3); Game.AddChecker(2, 7, 5); // Update the grid with the checker pieces Game.PopulateGrid(); // Show the grid in the console Game.PrintGrid(); cout << "\n"; // Main loop while(bAppState) { cout << "Please type sample input: "; cin >> Input; cout << "\n"; stringstream ss(Input); vector<string> result; while(ss.good()) { string substr; getline(ss, substr, ','); result.push_back(substr); } if (Game.move(atoi(result[0].c_str()), atoi(result[1].c_str()), atoi(result[2].c_str()), atoi(result[3].c_str()))) { cout << "SUCCESSFUL JUMP\n\n"; Game.PrintGrid(); cout << "\n"; } } return 0; } [/CODE] If you have any questions about the code feel free to ask.
I think the problem is the way you're handling the case when your piece is on the right of the opponent's. When you update the position of your piece after moving, I think it should be: [code] // Is piece on the right side of the opponents if (PX < OX && PY > OY) { // Is position ajacent to the players piece the opponent if (PX + 1 == OX && PY - 1 == OY) { // Is position ajacent to opponents piece empty if (Grid[OX+1][OY-1] == 0) { // Loop through the vector's elements to find the correct checker piece for (int i = 0; i < Checkers.size(); i++) { // Find the correct checker piece in the vector if (Checkers[i].PositionX == PX + 1 && Checkers[i].PositionY == PY + 1) { // Reset the players oringal position to 0 Grid[PX][PY] = 0; // Position the players checker after a successful jump Checkers[i].PositionX = OX + 2; Checkers[i].PositionY = OY; // Update the game grid PopulateGrid(); } if (Checkers[i].PositionX == OX + 1 && Checkers[i].PositionY == OY + 1) { // Remove the opponents checker Checkers[i].Team = 0; // Update the game grid PopulateGrid(); } } // Jump was successful return true; } } }[/code] Since OY - 2 would actually be three spaces to the left of your opponent's piece (remember you already decremented it at the start of the function. OY now refers to one space to the left of the opponent's piece). Also, I would avoid editing the Grid array directly at any point in your game code. Personally I would prefer clearing the grid to zeros in the PopulateGrid() function before filling it. That way every time you call PopulateGrid() it's guaranteed to reflect the state of your checkers. I think it's better practice to try and keep the display part of your code (and the grid) as independent to the game logic as possible.
[QUOTE]Since OY - 2 would actually be three spaces to the left of your opponent's piece (remember you already decremented it at the start of the function. OY now refers to one space to the left of the opponent's piece).[/QUOTE] :suicide: Thanks for noticing that, I don't know how I decided to subtract 2. [QUOTE]Personally I would prefer clearing the grid to zeros in the PopulateGrid() function before filling it. That way every time you call PopulateGrid() it's guaranteed to reflect the state of your checkers.[/QUOTE] Thats a good idea, It seems to be working, I will be back if I have anymore problems, thanks for your help.
No worries, I had a feeling you wouldn't be too impressed haha tiny logic errors like that are the worst! I think it ended up placing your checker on top of an opponent's or off the board, so you couldn't see it. If you come back and don't get a response, or have any other random questions feel free to pm me.
Sorry, you need to Log In to post a reply to this thread.