vbuser1338

Members
  • Content count

    298
  • Joined

  • Last visited

Community Reputation

175 Neutral

About vbuser1338

  • Rank
    Member
  1. Retrieving name of c++ type

    This is a possibility and probably is safer then typing the class name every time I call a function with that type and possibly mistyping it. Thanks for the suggestion.
  2. Retrieving name of c++ type

    Thanks, thats what I tried but as you said it varies from compilers so I cannot just remove the "4" from "4Test" to get the class name. I guess I will have to continue explicitly passing the class name.
  3. I'm working on my scripting system and I want to know if there is a way to get the class name of a template parameter. Right now you explicitly pass the name of the class and I am wondering if there is a portable way of getting the class name. I tried typeid but it includes other stuff with the class name (for example I get "4Test" instead of Test). I looked through boost but didn't find anything. Here is how I have it now: // Push an object to the function template <typename T> template <typename ObjectType> void Function<T>::pushArgument(const std::string &objectTypename, ObjectType argument) { arguments.push_back(ArgumentPtr(new ObjectArgument<ObjectType>(objectTypename, argument))); } // Class wrapping the passing of an object to lua template <typename T> class ObjectArgument : public FunctionArgument { private: const std::string objectTypename; // The class name of this type T argumentValue; public: ObjectArgument(const std::string &objectTypename_, T &argumentValue_) : objectTypename(objectTypename_), argumentValue(argumentValue_) {} virtual void push(lua_State *luaState); }; template <typename T> inline void ObjectArgument<T>::push(lua_State *luaState) { assert(luaState && "No created lua state to push argument"); tolua_pushusertype(luaState, &argumentValue, objectTypename.c_str()); } // Example usage class Test { ... }; Utils::Scripting::Function<Test> objectFunction(lua, "test.lua", "objectFunction"); objectFunction.pushArgument("Test", Test(20)); Test test = objectFunction(); test.print(); Is there a easy way to just go objectFunction.pushArgument(Test(20)) and able to get the class name? Thanks for any help
  4. Thanks, the main problem was the __getitem__ method. It was declared to take a tuple but for every access I had two __getitem__ lookups plus one on the regular list because the board contained a custom class MultiArray that contained the list of tiles. I changed this to a single list and it took just over 1 second to do the first lookup. I'm sure there are many other optimisations I can make to get better performance. I didn't think the lookups would be that expensive. Thanks again for the help, vbuser
  5. I tried to prototype the AI for my game in python so I programmed a game of Tic Tac Toe as a refresher of MiniMax but the performance is very bad. It takes about 4.8 seconds to generate the first move. I coded up a c++ version quickly and it takes very little time to generate the move. I can't believe that the python version is that much slower. I compared the two codes and I don't see anything different. It could be I'm calling an expensive function in the python version that I am unaware of but I can't figure it out. I tried not using a list to generate the moves thinking that could be a bottle neck but that didn't shave much off the time. Should the python version be this much slower or is it something I've done wrong? Here is the code: C++ Game::MiniMaxNode Game::miniMax() { return max(); } Game::MiniMaxNode Game::max() { if (gameOver()) { // Get the score and return return Game::MiniMaxNode(evaluate()); } else { Game::MiniMaxNode bestMove(-1000); for (int y = 0; y < Board::BOARD_SIZE; ++y) { for (int x = 0; x < Board::BOARD_SIZE; ++x) { if (board.board[x][y] == EMPTY) { board.board[x][y] = X; Game::MiniMaxNode score = min(); if (score.score > bestMove.score) { bestMove.score = score.score; bestMove.x = x; bestMove.y = y; } board.board[x][y] = EMPTY; } } } return bestMove; } } Game::MiniMaxNode Game::min() { if (gameOver()) { // Get the score and return return Game::MiniMaxNode(evaluate()); } else { Game::MiniMaxNode bestMove(1000); for (int y = 0; y < Board::BOARD_SIZE; ++y) { for (int x = 0; x < Board::BOARD_SIZE; ++x) { if (board.board[x][y] == EMPTY) { board.board[x][y] = O; Game::MiniMaxNode score = max(); if (score.score < bestMove.score) { bestMove.score = score.score; bestMove.x = x; bestMove.y = y; } board.board[x][y] = EMPTY; } } } return bestMove; } } int Game::evaluate() const { // Return positive if x wins, negative if o wins and 0 if its a tie ++eval; Winner winner = getWinner(); if (winner == X_WIN) { return 1; } else if (winner == O_WIN) { return -1; } else { return 0; } } bool Game::gameOver() const { if (board.isFull()) { return true; } else { // Check for winner Winner winner = getWinner(); if (winner == X_WIN || winner == O_WIN) { return true; } } // Game not over return false; } Game::Winner Game::getWinner() const { // Check horizontally for (int y = 0; y < Board::BOARD_SIZE; ++y) { if (board.board[0][y] != EMPTY && board.board[0][y] == board.board[1][y] && board.board[1][y] == board.board[2][y]) { return convertWinner(board.board[0][y]); } } // Check veritcally for (int x = 0; x < Board::BOARD_SIZE; ++x) { if (board.board[x][0] != EMPTY && board.board[x][0] == board.board[x][1] && board.board[x][1] == board.board[x][2]) { return convertWinner(board.board[x][0]); } } // Check diagonals if (board.board[0][0] != EMPTY && board.board[0][0] == board.board[1][1] && board.board[1][1] == board.board[2][2]) { return convertWinner(board.board[0][0]); } else if (board.board[2][0] != EMPTY && board.board[2][0] == board.board[1][1] && board.board[1][1] == board.board[0][2]) { return convertWinner(board.board[2][0]); } // No winner so if game is over then its a tie or else there is no winner if (board.isFull()) { return TIE; } else { return NONE; } } Game::Winner Game::convertWinner(TileType type) const { // Convert the tile type to a winner if (type == X) { return X_WIN; } else if (type == O) { return O_WIN; } else { throw std::logic_error("Invalid tile type to convert to winner"); } } Python def _getInput(self, events): """ Handles the input for the game """ for event in events: if event.type == QUIT: self.running = False elif event.type == MOUSEBUTTONDOWN: # Move the player pos = pygame.mouse.get_pos() xTile = (pos[0] - self.boardOffset[0]) / 30 yTile = (pos[1] - self.boardOffset[1]) / 30 if (0 <= xTile < 3) and (0 <= yTile < 3): self.board[xTile, yTile] = Board.O # If game is not over move computer if not self._gameOver(): t = time.time() move = self._miniMax() self.board[move[0], move[1]] = Board.X print "Took ", (time.time() - t) * 1000, "ms" def _evaluate(self): """ Evalutes the score for the game and returns a large positive if X wins, a large negative if Y wins and 0 if there is no winner or it is a tie """ winner = self._getWinner() if winner == Board.X: return 1 elif winner == Board.O: return -1 else: return 0 def _getWinner(self): """ Returns the winner of the game """ # Check horizontally for y in range(3): if (self.board[0, y] != Board.EMPTY) and (self.board[0, y] == self.board[1, y] == self.board[2, y]): return self.board[0, y] # Check vertically for x in range(3): if (self.board[x, 0] != Board.EMPTY) and (self.board[x, 0] == self.board[x, 1] == self.board[x, 2]): return self.board[x, 0] # Check diagonally if (self.board[0, 0] != Board.EMPTY) and (self.board[0, 0] == self.board[1, 1] == self.board[2, 2]): return self.board[0, 0] elif (self.board[2, 0] != Board.EMPTY) and (self.board[2, 0] == self.board[1, 1] == self.board[0, 2]): return self.board[2, 0] # Tie or no one won return None def _miniMax(self): _, move = self._max() return move def _max(self): if self._gameOver(): return self._evaluate(), [0, 0] else: bestScore = -10000 bestMove = [0, 0] validMoves = self._generateMoves() for validMove in validMoves: self.board[validMove[0], validMove[1]] = Board.X score, _ = self._min() if score > bestScore: bestScore = score bestMove = validMove self.board[validMove[0], validMove[1]] = Board.EMPTY return bestScore, bestMove def _min(self): if self._gameOver(): return self._evaluate(), [0, 0] else: bestScore = 10000 bestMove = [0, 0] validMoves = self._generateMoves() for validMove in validMoves: self.board[validMove[0], validMove[1]] = Board.O score, _ = self._max() if score < bestScore: bestScore = score bestMove = validMove self.board[validMove[0], validMove[1]] = Board.EMPTY return bestScore, bestMove def _gameOver(self): """ Returns true if the game is over and false otherwise """ winner = self._getWinner() if winner == Board.X or winner == Board.O or self.board.isFull(): # Game over return True # Game not over return False def _generateMoves(self): """ Returns a list of valid moves on a board """ moves = [] for y in range(3): for x in range(3): if self.board[x, y] == Board.EMPTY: moves.append((x, y)) return moves Can anyone explain this? Thanks, vbuser
  6. www.libsdl.org Down??

    Do you happen to have the mac framework or sources for SDL_ttf that they could host for me to get? Thanks, vbuser
  7. Blocky Man OSX port

    I just tried it on my PowerPC and it crashes when it tries to load a level. I looked in the crash log and it crashed with the exception EXC_BAD_ACCESS.
  8. I think my best option is to use java web start. I will just have to create a few work arounds to get it to work right. Thanks for the help, vbuser
  9. I am just finishing my game but I am having quite a bit of trouble deploying my application. I am trying to bundle my application as a jar because some of the libraries I am using don't work well with java web start because of all the resources having to be in a jar file. The only problem is on some platforms I need to set an environment variable before the jar is executed without the user having to use the command line. I have been searching and have not found a way to set the environment variables without running the jar from the command line. Is there a way to do this that will work cross platform? Or do I have to export an executable for different platforms from eclipse? Thanks, vbuser
  10. The delta should be the time in between frames, here is the modified version: ticks = SDL_GetTicks() delta = (ticks - lastUpdate) / 1000 // 1000ms = 1 second ... // Later in your game code myFlyingRobot.x += myFlyingRobot.speed * delta // speed is in pixels per second // Keep last update lastUpdate = ticks where ticks is the variable keeping track of the time of the last update.
  11. You could limit the time rate, but you could also used time based calculations. Have a certain number of degrees that should be rotated a second, say 60, and multiply that by the elapsed time in seconds since the last update. Something like this: - Get time in seconds since last update - current rotation = current rotation + (rotation speed * elapsed time) where the rotation speed is the number of degrees to rotate in one second and the elapsed time is the time in seconds since the last update. If you need a little more clarification here is a link to an article on time based movement. Hope this helps.
  12. This is a little off topic but you said about how you don't want to spend too much money on books. You can read most of those books online at Safari Books for $10 a month and they also have a free trial for so many days. Its good if you can stand reading books on the computer. I don't really like to, but for the good price I do. I did a quick look and found every book except More Effective C++, but it might be on there. Hopefully this is helpful.
  13. Thanks again for the help. I don't know how many times I will have to get a clone of an element, but now I know how to when I do[smile]. I though everyone said c++ was harder to learn than java but some things in java seem more confusing, but I guess thats just because I'm just learning. Thanks, vbuser
  14. Instead of making a new reference to copy everything from the originalList to and then the originalList reference is not needed I just assigned it back to itself so I get a copy back. And thanks Michael Nischt that seems much better that copying the whole original list. But I have one more question is there a way to copy a list or even a subList without sharing the contents? I thought this might work but it didn't: List<Exercise7> x = new ArrayList<Exercise7>(originalList); x.get(0).id = 20; But when I modify x it still modifies the original list. Is there a way without iterating through the original list and making a clone of each object and putting the clone in the other? Thanks, vbuser
  15. I have decided to learn a bit of Java and am working on some of the exercises out of my book but have come to a problem. I get a ConcurrentModificationException when trying to remove a sublist from an original list. I did a quick search and I found: "... For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.". I think this is what is happening but I am not sure. I think it doesn't completly finish creating the sub list when I try to remove it. This is the code that is throwing the exception: public static void main(String[] args) { List<Exercise7> originalList = new ArrayList<Exercise7>(); // Create original list for (int i = 0;i < 10;++i) { originalList.add(new Exercise7(i)); } println("Original List: " + originalList); // Create sublist List<Exercise7> subList = originalList.subList(0, 5); println("Sub List: " + subList); // Remove sublist //originalList = new ArrayList<Exercise7>(originalList); originalList.removeAll(subList); println("New Original List: " + originalList); } It will not throw the exception when I uncomment the line where I make a copy of the whole original list but I don't think that is the best solution, especially if I was working with larger lists. Thanks for any help, vbuser