revert to original files

This commit is contained in:
Sandipsinh Rathod 2024-12-06 17:31:01 -05:00
parent c1c4586abd
commit b32b61fad8
No known key found for this signature in database
3 changed files with 39 additions and 29 deletions

@ -151,7 +151,6 @@ string GameMaster(const vector<string>& libpaths, vector<int>& scores, vector<do
message = LoadPlayer(player, libpath, board_rows, board_cols, box_type, line_type); message = LoadPlayer(player, libpath, board_rows, board_cols, box_type, line_type);
if(message != "") if(message != "")
{ {
cout << message << endl;
cout << "Library error while loading a player (" << libpath << ")." << endl; cout << "Library error while loading a player (" << libpath << ")." << endl;
if(player != nullptr) if(player != nullptr)
player->Close(); player->Close();

@ -1,28 +1,33 @@
#include <cassert> #include <iostream>
#include <fstream>
#include <assert.h>
#include <vector> #include <vector>
#include "common.h"
#include "board.h"
#include "player.h" #include "player.h"
using namespace std; using namespace std;
#include <unistd.h> // usleep() #include <unistd.h> // usleep()
#include <dlfcn.h> #include <dlfcn.h>
string GameMaster(const vector<string> &libpaths, vector<int> &scores, vector<double> &timespents, double sleep_sec); string GameMaster(const vector<string>& libpaths, vector<int>& scores, vector<double>& timespents, double sleep_sec);
void CloseLibs(); void CloseLibs();
int main(int argc, char *argv[]) { int main(int argc, char* argv[])
{
// argv[0] : program name // argv[0] : program name
// argv[1] : 1st player library path // argv[1] : 1st player library path
// argv[2] : 2nd player library path // argv[2] : 2nd player library path
// argv[3] : 3rd player library path // argv[3] : 3rd player library path
if (argc <= 2) if(argc <= 2)
return 0; return 0;
vector<string> libpaths; vector<string> libpaths;
vector<int> scores; vector<int> scores;
vector<double> timespents; vector<double> timespents;
for (int i = 1; i < argc; i++) { for(int i=1; i<argc; i++)
{
libpaths.push_back(argv[i]); libpaths.push_back(argv[i]);
scores.push_back(0); scores.push_back(0);
timespents.push_back(0); timespents.push_back(0);
@ -33,45 +38,51 @@ int main(int argc, char *argv[]) {
CloseLibs(); CloseLibs();
} }
void ClearConsole() { void ClearConsole()
{
system("clear"); system("clear");
} }
void SleepInSec(double sleep_sec) { void SleepInSec(double sleep_sec)
{
int sleep_microsecond = sleep_sec * 1000000; int sleep_microsecond = sleep_sec * 1000000;
usleep(sleep_microsecond); usleep(sleep_microsecond);
} }
vector<void *> lst_player_libhandle; vector<void*> lst_player_libhandle;
typedef IPlayer* (*CreatePlayerType)();
typedef IPlayer *(*CreatePlayerType)();
string LoadPlayer string LoadPlayer
(IPlayer *&iplayer, string libpath, int board_rows // the size of board (including dots, lines, and boxes) ( IPlayer*& iplayer
, int board_cols // the size of board (including dots, lines, and boxes) , string libpath
, char box_type // the character for the player's boxes , int board_rows // the size of board (including dots, lines, and boxes)
, char line_type // the character for the player's lines , int board_cols // the size of board (including dots, lines, and boxes)
) { , char box_type // the character for the player's boxes
void *player_libhandle = dlopen(libpath.c_str(), RTLD_LAZY); , char line_type // the character for the player's lines
if (player_libhandle == nullptr) { )
{
void* player_libhandle = dlopen(libpath.c_str(), RTLD_LAZY);
if(player_libhandle == nullptr)
{
return "cannot load " + libpath; return "cannot load " + libpath;
} }
lst_player_libhandle.push_back(player_libhandle); lst_player_libhandle.push_back(player_libhandle);
void *pfunc = dlsym(player_libhandle, "PlayerFactory"); void* pfunc = dlsym(player_libhandle, "PlayerFactory");
if (pfunc == nullptr) { if(pfunc == nullptr)
{
return "cannot find PlayerFactory() function defined as extern C"; return "cannot find PlayerFactory() function defined as extern C";
} }
CreatePlayerType CreatePlayer = (CreatePlayerType) pfunc; CreatePlayerType CreatePlayer = (CreatePlayerType)pfunc;
iplayer = CreatePlayer(); iplayer = CreatePlayer();
iplayer->Init(board_rows, board_cols, box_type, line_type); iplayer->Init(board_rows, board_cols, box_type, line_type);
if (iplayer == nullptr) { if(iplayer == nullptr)
{
return "cannot create player using PlayerFactory() function"; return "cannot create player using PlayerFactory() function";
} }
return ""; return "";
} }
void CloseLibs()
void CloseLibs() { {
for (void *player_libhandle: lst_player_libhandle) for(void* player_libhandle : lst_player_libhandle)
dlclose(player_libhandle); dlclose(player_libhandle);
lst_player_libhandle.clear(); lst_player_libhandle.clear();
} }

@ -8,7 +8,7 @@ extern "C" IPlayer* PlayerFactory()
string StrategicPlayer::PlayerInfo() { string StrategicPlayer::PlayerInfo() {
return "Sapan Shah (scs6041@psu.edu), Sandipsinh Rathod (sdr5549@psu.edu)"; return "Sandipsinh Rathod (sdr5549@psu.edu), Sapan Shah (scs6041@psu.edu)";
} }
void StrategicPlayer::Init(int board_rows, int board_cols, char box_type, char line_type) { void StrategicPlayer::Init(int board_rows, int board_cols, char box_type, char line_type) {