00001 //--------------------------------------------------------------------------- 00002 // game 00003 //--------------------------------------------------------------------------- 00004 #ifndef __GAME_H 00005 #define __GAME_H 00006 00007 #include "system.h" 00008 #include "board.h" 00009 #include "thread.h" 00010 #include "net_socket.h" 00011 #include "net_host.h" 00012 #include <string> 00013 #include <iostream> 00014 using namespace std; 00015 00016 /*namespace Net{ 00017 00018 class HelloMessage; 00019 class MoveMessage; 00020 class ByeMessage; 00021 00022 class Message{ 00023 protected: 00024 unsigned char type; 00025 unsigned int size; // htonl (out) ntohl (in) 00026 unsigned char *data; 00027 public: 00028 Message(unsigned char type, unsigned int size, unsigned char *data); 00029 static Message* translate(Socket &s); 00030 virtual void send()=0; 00031 unsigned char* getData(){ return data; }; 00032 virtual void setData(unsigned char* data)=0; 00033 }; 00034 00035 class HelloMessage: public Message{ 00036 public: 00037 HelloMessage(){ type = 0; }; 00038 HelloMessage(unsigned char type, unsigned int size, unsigned char *data): Message(type, size, data){}; 00039 virtual void send(); 00040 virtual void setData(unsigned char* data){}; 00041 }; 00042 00043 class MoveMessage: public Message{ 00044 public: 00045 MoveMessage(){ type = 1; }; 00046 MoveMessage(unsigned char type, unsigned int size, unsigned char *data): Message(type, size, data){}; 00047 virtual void send(); 00048 virtual void setData(unsigned char* data); 00049 }; 00050 00051 class ByeMessage: public Message{ 00052 public: 00053 ByeMessage(){ type = 2; }; 00054 ByeMessage(unsigned char type, unsigned int size, unsigned char *data): Message(type, size, data){}; 00055 virtual void send(); 00056 virtual void setData(unsigned char* data){}; 00057 }; 00058 00059 00060 } // end of namespace Net 00061 */ 00062 00063 namespace Games { 00064 00066 class GameState{ 00067 private: 00069 int dataSize; 00070 public: 00072 System::Mutexed<unsigned char*> data; 00074 System::Mutexed<bool> localInterrupt; 00076 System::Mutexed<bool> remoteInterrupt; 00078 System::Mutexed<string> localNick; 00080 System::Mutexed<string> remoteNick; 00082 System::Mutexed<bool> newData; 00084 System::Mutexed<bool> ready; 00085 00087 GameState(int size); 00089 GameState(){ data = NULL; }; 00091 void getData(unsigned char* ptr); 00093 void setData(unsigned char* ptr); 00095 void print(); 00097 void sendMessage(Net::Socket socket, unsigned char type); 00098 }; 00099 00100 00101 } // end of namespace Game 00102 00103 00104 namespace System{ 00105 00107 class Listener: public Thread{ 00108 private: 00110 Net::AcceptedSocket socket; 00112 unsigned int port; 00114 int size; 00115 protected: 00117 void dispatchMessage(); 00119 virtual void* tmain(void *args); 00120 public: 00122 Games::GameState state; 00123 00125 Listener(unsigned int port, int size){ this->port = port; this->size = size; state = Games::GameState(size); }; 00127 Listener(){}; 00129 string peerName(){ return socket.peerName(); }; 00130 00131 00132 }; 00133 00134 00135 00136 /* class ListenThread: public Thread{ 00137 private: 00138 Net::ServerSocket server; 00139 Net::AcceptedSocket listen; 00140 unsigned int port; 00141 unsigned int size; 00142 protected: 00143 virtual void* tmain(void* args); 00144 00145 public: 00146 ListenThread(unsigned int port, unsigned int size); 00147 Mutexed<bool> ready; 00148 Mutexed<bool> interrupt; 00149 Mutexed<int> move; 00150 Mutexed<unsigned char*> data; 00151 Mutexed<string> nick; 00152 Net::Host clientHost; 00153 string toString(){ return listen.peerName(); }; 00154 };*/ 00155 00156 /*class PostThread: public Thread{ 00157 private: 00158 Net::ClientSocket *socket; 00159 protected: 00160 virtual void* main(void* args); 00161 public: 00162 void sendHello(const char* nick); 00163 void sendMove(unsigned int size, unsigned char *ptr); 00164 void sendQuit(); 00165 };*/ 00166 } 00167 00168 namespace Games{ 00169 00170 00172 class Game{ 00173 protected: 00175 bool running; 00177 Board* board; 00179 00182 virtual bool playerOne()=0; 00184 00187 virtual bool playerTwo()=0; 00189 virtual void interrupt(){}; 00191 virtual bool isInterrupted()=0; 00192 public: 00193 //bool isEnd(); 00195 Game(){ board = new Board(16,16,50,50,16); running = false; }; 00197 virtual int run(); 00198 00199 }; 00200 00202 class LocalGame: public Game{ 00203 protected: 00205 bool playerMove(unsigned char pl); 00207 00210 virtual bool playerOne(){ return playerMove(1); }; 00212 00215 virtual bool playerTwo(){ return playerMove(2); }; 00217 00220 virtual bool isInterrupted(){ return false; }; 00221 public: 00223 LocalGame(): Game() { running = true; }; 00224 //virtual int run(); 00225 }; 00226 00227 00229 class NetworkGame: public Game{ 00230 protected: 00232 System::Listener listener; 00234 Net::ClientSocket socket; 00236 00239 bool localPlayer(unsigned char pl); 00241 00244 bool remotePlayer(unsigned char pl); 00246 00249 virtual bool isInterrupted(){ return *listener.state.localInterrupt || *listener.state.remoteInterrupt; }; 00251 virtual void interrupt(){ listener.state.localInterrupt = true; }; 00252 public: 00254 NetworkGame(){}; 00256 virtual ~NetworkGame(){ socket.destroy(); }; 00257 }; 00258 00260 class MasterGame: public NetworkGame{ 00261 protected: 00263 00266 virtual bool playerOne(){ return localPlayer(1); }; 00268 00271 virtual bool playerTwo(){ return remotePlayer(2); }; 00272 public: 00274 MasterGame(string nick, unsigned int size); 00275 }; 00276 00278 class SlaveGame: public NetworkGame{ 00279 protected: 00281 00284 virtual bool playerOne(){ return remotePlayer(1); }; 00286 00289 virtual bool playerTwo(){ return localPlayer(2); }; 00290 public: 00292 SlaveGame(string nick, string host, unsigned int size); 00293 }; 00294 /* class NetworkGame: public Game{ 00295 protected: 00296 System::ListenThread *listener; 00297 Net::ClientSocket socket; 00298 string myNick, otherNick; 00299 void sendMessage(unsigned char type); 00300 int currentMove; 00301 bool isNewMove(){ return currentMove < *(listener->move); }; 00302 bool localPlayerMove(int pl); 00303 bool networkPlayerMove(); 00304 public: 00305 NetworkGame(): currentMove(0){}; 00306 virtual ~NetworkGame(){ listener->interrupt = true; }; 00307 }; 00308 00309 class MasterGame: public NetworkGame{ 00310 protected: 00311 virtual bool playerOne(){ return localPlayerMove(1); }; 00312 virtual bool playerTwo(){ return networkPlayerMove(); }; 00313 virtual void interrupt(){ if (running){ sendMessage(2); listener->interrupt = true; } }; 00314 public: 00315 MasterGame(); 00316 }; 00317 00318 class SlaveGame: public NetworkGame{ 00319 protected: 00320 virtual bool playerOne(){ return networkPlayerMove(); }; 00321 virtual bool playerTwo(){ return localPlayerMove(2); }; 00322 virtual void interrupt(){ if (running){ sendMessage(2); listener->interrupt = true; } }; 00323 public: 00324 SlaveGame(); 00325 }; 00326 */ 00327 } // end of namespace Games 00328 00329 #endif // __GAME_H