src/Move.cpp

Go to the documentation of this file.
00001 
00008 /*
00009 This file is part of Teapot Colony Wars.
00010 
00011 Teapot Colony Wars is free software: you can redistribute it and/or modify
00012 it under the terms of the GNU General Public License as published by
00013 the Free Software Foundation, either version 2 of the License, or
00014 (at your option) any later version.
00015 
00016 Teapot Colony Wars is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 GNU General Public License for more details.
00020 
00021 You should have received a copy of the GNU General Public License
00022 along with Teapot Colony Wars.  If not, see <http://www.gnu.org/licenses/>.
00023 */
00024 
00025 #include "Move.h"
00026 
00027 #include <cstdlib>
00028 
00029 #include "MoveIterator.h"
00030 
00031 Move::Move(Move * next) : _next(next) {
00032 
00033 }
00034 
00035 Move::~Move(){
00036    delete _next; //Delete all the chain recursively
00037 }
00038 
00039 unsigned int Move::size() {
00040   Move * ptr = _next;
00041   unsigned int n = 1;
00042   while(ptr){
00043     ptr = ptr->_next;
00044     n++;
00045   }
00046   return n;
00047 }
00048 
00049 MoveIterator Move::begin() {
00050   MoveIterator m(this);
00051   return m;
00052 }
00053 
00054 MoveIterator Move::end() {
00055   MoveIterator m(NULL);
00056   return m;
00057 }
00058 
00059 std::pair<int,int> Move::getAbsCoords(move_command cmd, int i, int j) const {
00060   std::pair<int,int> result;
00061 
00062   if(cmd == getHandledMovement()){
00063 
00064     std::pair<int,int> relatives;
00065     relatives = getHandledRelCoords();
00066     result.first = i + relatives.first;
00067     result.second = j + relatives.second;
00068 
00069   } else if(_next){
00070     result = _next->getAbsCoords(cmd, i, j);
00071   } else {
00072     throw "Illegal move !";
00073   }
00074   return result;
00075 }
00076 
00077 std::pair<int,int> Move::getAbsCoords(int i, int j) const {
00078   return getAbsCoords(getHandledMovement(), i, j);
00079 }
00080 
00081 move_command Move::opposite(move_command cmd) const {
00082   move_command res;
00083 
00084   //Can we handle it ?
00085   if(cmd == getHandledMovement()){
00086     res = getHandledOpposite();
00087   } else if(_next){ //We can't, so we ask the next... if any
00088     res = _next->opposite(cmd);
00089   } else {
00090     throw "Illegal move !";
00091   }
00092   return res;
00093 }

Generated on Sat Feb 2 22:22:54 2008 for Teapot Colony Wars by  doxygen 1.5.4