Commit 17a20de4 authored by Sam Moore's avatar Sam Moore

Modified manager program, updated website

Major changes: Added simple GUI for human players
Setup phase doesn't use GUI yet (uses a default if -g enabled).
Click in the general region of where you want to select/move pieces.

Modified Board::Draw to allow for showing of already revealed pieces,
but not all pieces. Used to make human player GUI nicer (hides AI pieces).

Modified -t switch to allow for "infinite" stall_time
(wait for user to press enter)

Changed website. Removed protocol description and linked to manual.txt.
Added screenshot to make things slightly more exciting.
Updated manual.txt

Minor changes to simulate.py (output)

TODO:
Fix segmentation fault found in manager program
It occurs just before exit. Memory error in the cleanup process?
ANNOYING!

Still have to bring myself to take the time to finish setting up that vm...
Need to talk to a non-hostile wheel member about security. I don't want my vm to take out our network or something.

Preferably I don't even want the vm to be compromised, regardless of the rest of ucc.
So much I don't know about servers/linux...

Need to stop staying up past midnight...
parent 2120cc40
...@@ -158,9 +158,20 @@ MovementResult Controller::MakeMove(string & buffer) ...@@ -158,9 +158,20 @@ MovementResult Controller::MakeMove(string & buffer)
} }
if (Game::theGame->allowIllegalMoves && !Board::LegalResult(moveResult)) if (!Board::LegalResult(moveResult))
return MovementResult::OK; //HACK - Illegal results returned as legal! {
else if (Game::theGame->allowIllegalMoves)
return moveResult; return MovementResult::OK; //HACK - Illegal results returned as legal! (Move not made)
else if (this->HumanController()) //Cut human controllers some slack and let them try again...
{
//Yes, checking type of object is "not the C++ way"
// But sometimes its bloody useful to know!!!
Message("Bad move: \'" + buffer + "\' <- Please try again!");
buffer = "";
return this->MakeMove(buffer);
}
}
return moveResult;
} }
...@@ -19,7 +19,7 @@ class Controller ...@@ -19,7 +19,7 @@ class Controller
MovementResult MakeMove(std::string & buffer); MovementResult MakeMove(std::string & buffer);
virtual bool HumanController() const {return false;} //Hacky... overrides in human_controller... avoids having to use run time type info
void Message(const std::string & buffer) {Message(buffer.c_str());} void Message(const std::string & buffer) {Message(buffer.c_str());}
virtual void Message(const char * string) = 0; virtual void Message(const char * string) = 0;
......
...@@ -404,14 +404,34 @@ MovementResult Game::Play() ...@@ -404,14 +404,34 @@ MovementResult Game::Play()
MovementResult result = MovementResult::OK; MovementResult result = MovementResult::OK;
turnCount = 1; turnCount = 1;
string buffer; string buffer;
Piece::Colour toReveal = reveal;
red->Message("START"); red->Message("START");
//logMessage("START\n");
while (!Board::HaltResult(result) && (turnCount < maxTurns || maxTurns < 0)) while (!Board::HaltResult(result) && (turnCount < maxTurns || maxTurns < 0))
{ {
if (red->HumanController())
toReveal = Piece::RED;
if (printBoard)
{
system("clear");
if (turnCount == 0)
fprintf(stdout, "START:\n");
else
fprintf(stdout, "%d BLUE:\n", turnCount);
theBoard.PrintPretty(stdout, toReveal);
fprintf(stdout, "\n\n");
}
if (graphicsEnabled)
theBoard.Draw(toReveal);
turn = Piece::RED; turn = Piece::RED;
logMessage( "%d RED: ", turnCount); logMessage( "%d RED: ", turnCount);
...@@ -421,16 +441,25 @@ MovementResult Game::Play() ...@@ -421,16 +441,25 @@ MovementResult Game::Play()
logMessage( "%s\n", buffer.c_str()); logMessage( "%s\n", buffer.c_str());
if (Board::HaltResult(result)) if (Board::HaltResult(result))
break; break;
if (graphicsEnabled)
theBoard.Draw(reveal); if (stallTime > 0)
Wait(stallTime);
else
ReadUserCommand();
if (blue->HumanController())
toReveal = Piece::BLUE;
if (printBoard) if (printBoard)
{ {
system("clear"); system("clear");
fprintf(stdout, "%d RED:\n", turnCount); fprintf(stdout, "%d RED:\n", turnCount);
theBoard.PrintPretty(stdout, reveal); theBoard.PrintPretty(stdout, toReveal);
fprintf(stdout, "\n\n"); fprintf(stdout, "\n\n");
} }
Wait(stallTime); if (graphicsEnabled)
theBoard.Draw(toReveal);
turn = Piece::BLUE; turn = Piece::BLUE;
logMessage( "%d BLU: ", turnCount); logMessage( "%d BLU: ", turnCount);
...@@ -444,18 +473,13 @@ MovementResult Game::Play() ...@@ -444,18 +473,13 @@ MovementResult Game::Play()
if (graphicsEnabled)
theBoard.Draw(reveal);
if (printBoard)
{
system("clear");
fprintf(stdout, "%d BLUE:\n", turnCount);
theBoard.PrintPretty(stdout, reveal);
fprintf(stdout, "\n\n");
}
Wait(stallTime);
if (stallTime > 0)
Wait(stallTime);
else
ReadUserCommand();
if (theBoard.MobilePieces(Piece::BOTH) == 0) if (theBoard.MobilePieces(Piece::BOTH) == 0)
result = MovementResult::DRAW; result = MovementResult::DRAW;
...@@ -493,6 +517,20 @@ int Game::logMessage(const char * format, ...) ...@@ -493,6 +517,20 @@ int Game::logMessage(const char * format, ...)
return result; return result;
} }
/**
* Waits for a user command
* Currently ignores the command.
*/
void Game::ReadUserCommand()
{
fprintf(stdout, "Waiting for user to press enter...\n");
string command("");
for (char c = fgetc(stdin); c != '\n' && (int)(c) != EOF; c = fgetc(stdin))
{
command += c;
}
}
MovementResult FileController::QuerySetup(const char * opponentName, std::string setup[]) MovementResult FileController::QuerySetup(const char * opponentName, std::string setup[])
{ {
...@@ -538,3 +576,5 @@ MovementResult FileController::QueryMove(std::string & buffer) ...@@ -538,3 +576,5 @@ MovementResult FileController::QueryMove(std::string & buffer)
buffer = string(s); buffer = string(s);
return MovementResult::OK; return MovementResult::OK;
} }
...@@ -27,7 +27,7 @@ class Game ...@@ -27,7 +27,7 @@ class Game
static void HandleBrokenPipe(int signal); static void HandleBrokenPipe(int signal);
void ReadUserCommand();
const Piece::Colour Turn() const {return turn;} const Piece::Colour Turn() const {return turn;}
void ForceTurn(const Piece::Colour & newTurn) {turn = newTurn;} void ForceTurn(const Piece::Colour & newTurn) {turn = newTurn;}
...@@ -53,7 +53,8 @@ class Game ...@@ -53,7 +53,8 @@ class Game
private: private:
FILE * log; FILE * log;
Piece::Colour reveal; public:
const Piece::Colour reveal;
int turnCount; int turnCount;
static bool gameCreated; static bool gameCreated;
......
...@@ -13,23 +13,83 @@ MovementResult Human_Controller::QuerySetup(const char * opponentName, string se ...@@ -13,23 +13,83 @@ MovementResult Human_Controller::QuerySetup(const char * opponentName, string se
if (!shownMessage) if (!shownMessage)
{ {
if (graphicsEnabled) if (graphicsEnabled)
fprintf(stderr, "GUI not yet supported! Please use CLI\n"); fprintf(stdout, "WARNING: GUI not fully supported. You will be forced to use the default setup.\n");
fprintf(stdout,"Enter %d x %d Setup grid\n", Game::theGame->theBoard.Width(), 4); else
fprintf(stdout,"Please enter one line at a time, using the following allowed characters:\n");
for (Piece::Type rank = Piece::FLAG; rank <= Piece::BOMB; rank = Piece::Type((int)(rank) + 1))
{ {
fprintf(stdout,"%c x %d\n", Piece::tokens[(int)rank], Piece::maxUnits[(int)rank]); fprintf(stdout,"Enter %d x %d Setup grid\n", Game::theGame->theBoard.Width(), 4);
} fprintf(stdout,"Please enter one line at a time.\n");
fprintf(stdout, "You must place at least the Flag (%c). Use '%c' for empty squares.\n", Piece::tokens[(int)Piece::FLAG], Piece::tokens[(int)Piece::NOTHING]); fprintf(stdout, "You must place at least the Flag (%c). Use '%c' for empty squares.\n", Piece::tokens[(int)Piece::FLAG], Piece::tokens[(int)Piece::NOTHING]);
fprintf(stdout, "NOTE: Player RED occupies the top four rows, and BLUE occupies the bottom four rows.\n");
switch (colour)
{
case Piece::RED:
fprintf(stdout, "You are RED and occupy the top 4 rows of the board.\n");
fprintf(stdout, "NOTE: Enter \"DEFAULT\" to use the setup:\n");
fprintf(stdout, "FB8sB479B8\nBB31555583\n6724898974\n967B669999\n");
break;
case Piece::BLUE:
fprintf(stdout, "You are BLUE and occupy the bottom 4 rows of the board.\n");
fprintf(stdout, "NOTE: Enter \"DEFAULT\" to use the setup:\n");
fprintf(stdout, "967B669999\n6724898974\nBB31555583\nFB8sB479B8\n");
break;
default:
fprintf(stdout, "WARNING: Unknown colour error! Please exit the game.\n");
break;
}
}
shownMessage = true; shownMessage = true;
} }
if (graphicsEnabled)
{
switch(colour)
{
case Piece::RED:
setup[0] = "FB8sB479B8";
setup[1] = "BB31555583";
setup[2] = "6724898974";
setup[3] = "967B669999";
break;
case Piece::BLUE:
setup[0] = "967B669999";
setup[1] = "6724898974";
setup[2] = "BB31555583";
setup[3] = "FB8sB479B8";
break;
default:
assert(false);
break;
}
return MovementResult::OK;
}
for (int y = 0; y < 4; ++y) for (int y = 0; y < 4; ++y)
{
cin >> setup[y]; cin >> setup[y];
if (y == 0 && setup[0] == "DEFAULT")
{
switch(colour)
{
case Piece::RED:
setup[0] = "FB8sB479B8";
setup[1] = "BB31555583";
setup[2] = "6724898974";
setup[3] = "967B669999";
break;
case Piece::BLUE:
setup[0] = "967B669999";
setup[1] = "6724898974";
setup[2] = "BB31555583";
setup[3] = "FB8sB479B8";
break;
default:
assert(false);
break;
}
break;
}
}
assert(cin.get() == '\n'); assert(cin.get() == '\n');
return MovementResult::OK; return MovementResult::OK;
...@@ -40,20 +100,78 @@ MovementResult Human_Controller::QueryMove(string & buffer) ...@@ -40,20 +100,78 @@ MovementResult Human_Controller::QueryMove(string & buffer)
static bool shownMessage = false; static bool shownMessage = false;
if (!shownMessage) if (!shownMessage)
{ {
if (graphicsEnabled) if (!graphicsEnabled)
fprintf(stderr, "GUI not yet supported! Please use the CLI\n"); {
fprintf(stdout, "Please enter your move in the format:\n X Y DIRECTION [MULTIPLIER=1]\n"); fprintf(stdout, "Please enter your move in the format:\n X Y DIRECTION [MULTIPLIER=1]\n");
fprintf(stdout, "Where X and Y indicate the coordinates of the piece to move;\n DIRECTION is one of UP, DOWN, LEFT or RIGHT\n and MULTIPLIER is optional (and only valid for scouts (%c))\n", Piece::tokens[(int)(Piece::SCOUT)]); fprintf(stdout, "Where X and Y indicate the coordinates of the piece to move;\n DIRECTION is one of UP, DOWN, LEFT or RIGHT\n and MULTIPLIER is optional (and only valid for scouts (%c))\n", Piece::tokens[(int)(Piece::SCOUT)]);
}
shownMessage = true; shownMessage = true;
} }
if (graphicsEnabled)
{
fprintf(stdout, "Click to move!\n");
SDL_Event event; int mouseClick = 0;
buffer.clear(); int x[] = {-1, -1}; int y[] = {-1, -1};
for (char in = fgetc(stdin); in != '\n'; in = fgetc(stdin)) while (mouseClick < 2)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
Game::theGame->logMessage("Exit called by human player!\n");
exit(EXIT_SUCCESS);
break;
case SDL_MOUSEBUTTONDOWN:
switch (event.button.button)
{
case SDL_BUTTON_LEFT:
SDL_GetMouseState(&x[mouseClick], &y[mouseClick]);
x[mouseClick] /= 32; y[mouseClick] /= 32; //Adjust based on graphics grid size
if (mouseClick == 0)
{
stringstream s("");
s << x[0] << " " << y[0] << " ";
buffer += s.str();
}
else if (mouseClick == 1)
{
int xDist = x[1] - x[0];
int yDist = y[1] - y[0];
if (abs(xDist) > abs(yDist))
{
if (xDist < 0)
buffer += "LEFT";
else
buffer += "RIGHT";
}
else if (yDist < 0)
buffer += "UP";
else
buffer += "DOWN";
}
mouseClick++;
break;
}
break;
}
}
}
fprintf(stdout, "Move complete!\n");
}
else
{ {
buffer += in; buffer.clear();
for (char in = fgetc(stdin); in != '\n'; in = fgetc(stdin))
{
buffer += in;
}
} }
......
...@@ -12,9 +12,10 @@ class Human_Controller : public Controller ...@@ -12,9 +12,10 @@ class Human_Controller : public Controller
Human_Controller(const Piece::Colour & newColour, const bool enableGraphics) : Controller(newColour, "human"), graphicsEnabled(enableGraphics) {} Human_Controller(const Piece::Colour & newColour, const bool enableGraphics) : Controller(newColour, "human"), graphicsEnabled(enableGraphics) {}
virtual ~Human_Controller() {} virtual ~Human_Controller() {}
virtual bool HumanController() const {return true;}
virtual MovementResult QuerySetup(const char * opponentName, std::string setup[]); virtual MovementResult QuerySetup(const char * opponentName, std::string setup[]);
virtual MovementResult QueryMove(std::string & buffer); virtual MovementResult QueryMove(std::string & buffer);
virtual void Message(const char * message) {fprintf(stderr, "Recieved message \"%s\" from manager.\n", message);} virtual void Message(const char * message) {fprintf(stderr, "%s\n", message);}
private: private:
const bool graphicsEnabled; const bool graphicsEnabled;
......
...@@ -75,7 +75,10 @@ Piece::Colour SetupGame(int argc, char ** argv) ...@@ -75,7 +75,10 @@ Piece::Colour SetupGame(int argc, char ** argv)
fprintf(stderr, "ARGUMENT_ERROR - Expected timeout value after -t switch!\n"); fprintf(stderr, "ARGUMENT_ERROR - Expected timeout value after -t switch!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
timeout = atof(argv[ii+1]); if (strcmp(argv[ii+1], "inf") == 0)
timeout = -1;
else
timeout = atof(argv[ii+1]);
++ii; ++ii;
break; break;
case 'g': case 'g':
......
...@@ -19,38 +19,48 @@ DESCRIPTION ...@@ -19,38 +19,48 @@ DESCRIPTION
If set to "human", stratego will request the user to make moves for the Red player using stdin. If set to "human", stratego will request the user to make moves for the Red player using stdin.
NOTES NOTES
1. There is no plan to support AI programs named "human". Deal with it. 1. There is no plan to support AI programs named "human". Deal with it.
2. As of writing, human players must use stdin. A graphical interface may be added later. 2. The graphical interface for human players is... basic. Deal with it.
blue_player blue_player
As red_player, except for controlling the Blue player. As red_player, except for controlling the Blue player.
OPTIONS OPTIONS
-g -g
By default, graphics are disabled. If the -g switch is present, stratego will draw the game as it is played using OpenGL By default, graphics are disabled. If the -g switch is present, stratego will draw the game as it is played using SDL/OpenGL
-p -p
By default, even if graphics are disabled, the board state is not printed. If -p is present, the board will be printed to stdout. By default, even if graphics are disabled, the board state is not printed. If -p is present, the board will be printed to stdout.
If the system supports colour, the characters will be in colour. If the system supports colour, the characters will be in colour.
If -p and -g are both present you will see both behaviours (overkill)! Yes, If -p and -g are both present you will see both behaviours (overkill)!
-i -i
By default, stratego will exit if a move which is deemed "illegal" is made. If the -i switch is present, illegal moves will be ignored. By default, stratego will exit if a move which is deemed "illegal" is made. If the -i switch is present, illegal moves will be ignored.
That is, the move will not be made (effectively the player making the illegal move loses a turn). That is, the move will not be made (effectively the player making the illegal move loses a turn).
NOTE: If -i is not given and a human player accidentally(?) makes an illegal move, they will be asked to make a different move. The game will continue.
This is intended to prevent fits of rage due to the horrible graphical interface causing humans to make illegal moves.
-r -r
By default, the identities of all pieces are shown. If the -r switch is present, and graphics are enabled, red pieces will be disguised. By default, the identities of all pieces are shown. If the -r switch is present, and graphics are enabled, red pieces will be disguised.
If graphics are disabled, the -r switch has no effect. If graphics are disabled, the -r switch has no effect.
Pieces which have previously taken part in combat (and survived) will be revealed.
-b -b
As -r, except blue pieces will be disguised. As -r, except blue pieces will be disguised.
NOTE: Both -r and -b may be used. NOTE: Both -r and -b may be used together.
-o -o
By default, stratego is silent. If the -o switch is present, the result of each move is printed to a file. If output_file is "stdout" By default, stratego does not log moves. If the -o switch is present, the result of each move is printed to a file.
then stdout will be used instead of a text file. If output_file is "stdout" then stdout will be used instead of a text file.
-t -t
By default, stratego executes moves as fast as they are recieved. If the -t switch is present, a delay of stall_time will be introduced By default, stratego executes moves as fast as they are recieved. If the -t switch is present, a delay of stall_time will be introduced
between each move. between each move.
If stall_time is negative or "inf", stratego will wait for the user to press enter before moving to the next move.
It is tentatively planned to allow the user to enter various commands to alter the game or proceed to specified turns.
However this is slightly complicated. So it might never be done.
-m -m
By default, the game is declared a Draw after 5000 turns have ellapsed. By default, the game is declared a Draw after 5000 turns have ellapsed.
Use this option to change the maximum number of turns. Use this option to change the maximum number of turns.
To play for an infinite number of turns, supply "inf" as max_number. To play for an infinite number of turns, supply "inf" as max_number. This is not recommended for obvious reasons.
-f -f
By default, stratego requires red_player and blue_player to enact a game. By default, stratego requires red_player and blue_player to enact a game.
...@@ -107,6 +117,10 @@ PROTOCOL ...@@ -107,6 +117,10 @@ PROTOCOL
RESPONSE: 4 lines, each of length BOARD_WIDTH, of characters. Each character represents a piece. The characters are shown above. RESPONSE: 4 lines, each of length BOARD_WIDTH, of characters. Each character represents a piece. The characters are shown above.
RED's pieces are placed at the top of the board, and BLUE's pieces are placed at the bottom.
An AI program does not have to place all 40 pieces, but must at least place the flag ('F').
2. TURN 2. TURN
QUERY: START | CONFIRMATION QUERY: START | CONFIRMATION
BOARD_STATE BOARD_STATE
...@@ -127,23 +141,25 @@ PROTOCOL ...@@ -127,23 +141,25 @@ PROTOCOL
The AI program should print "NO_MOVE" if it is unable to determine a move. The AI program should print "NO_MOVE" if it is unable to determine a move.
This will typically occur when the only pieces belonging to the AI program are Bombs and the Flag. This will typically occur when the only pieces belonging to the AI program are Bombs and the Flag.
CONFIRMATION: X Y DIRECTION [MULTIPLIER=1] OUTCOME | NO_MOVE | QUIT RESULT CONFIRMATION: X Y DIRECTION [MULTIPLIER=1] OUTCOME | NO_MOVE {OK | ILLEGAL} | QUIT [RESULT]
OUTCOME may be either OK, ILLEGAL, KILLS or DIES OUTCOME may be either OK, ILLEGAL, KILLS or DIES
OK - Move was successful OK - Move was successful
ILLEGAL - Move was not allowed. If stratego was not started with the -i switch, the game will end. ILLEGAL - Move was not allowed. If stratego was not started with the -i switch, the game will end.
KILLS ATTACKER_RANK DEFENDER_RANK - The piece moved into an occupied square and killed the defender. KILLS ATTACKER_RANK DEFENDER_RANK - The piece moved into an occupied square and killed the defender.
DIES ATTACKER_RANK DEFENDER_RANK - The piece moved into an occupied square and was killed by the defender. DIES ATTACKER_RANK DEFENDER_RANK - The piece moved into an occupied square and was killed by the defender.
A confirmation of "NO_MOVE" occurs when the AI program made no move for a legitimate reason. Most turns will be confirmed with: "X Y DIRECTION [MULTIPLIER=1] OUTCOME"
A confirmation of "NO_MOVE OK" occurs when the AI program made no move for a legitimate reason.
"NO_MOVE ILLEGAL" is printed if the AI program made no move for an illegitimate reason. "NO_MOVE ILLEGAL" is printed if the AI program made no move for an illegitimate reason.
If both AI programs successively make a "NO_MOVE" response, then the game will end. If both AI programs successively make a "NO_MOVE" response, then the game will end.
The player with the highest piece value will win, or a draw will be declared if the values are equal. The player with the highest piece value will win, or a draw will be declared if the values are equal.
3. END GAME 3. END GAME
If the CONFIRMATION line is of the form: If the CONFIRMATION line is of the form:
QUIT RESULT QUIT [RESULT]
Then the game is about to end. Then the game is about to end.
If present, RESULT will be a direct copy of the message to stdout described in the EXIT/OUTPUT section below. If present, RESULT will be a direct copy of the message to stdout described in the EXIT/OUTPUT section below.
...@@ -190,7 +206,11 @@ EXIT/OUTPUT ...@@ -190,7 +206,11 @@ EXIT/OUTPUT
BUGS BUGS
There are no known bugs at this time. However, stratego is still a work in progress. Report a bug to the AUTHOR (see below). WARNING:
stratego has been observed to segfault occassionally after the end of a game. It is not yet known what is causing these errors.
They appear to occur most often when the result is a draw, however they have been observed to occur under all exit conditions except the Illegal case. The result is still printed to stdout. However this bug _must_ be fixed before stratego can be used by simulation scripts.
stratego is still a work in progress. Report another bug to the AUTHOR (see below).
AUTHORS AUTHORS
Sam Moore (for the UCC Programming Competition 2012) <[email protected]> Sam Moore (for the UCC Programming Competition 2012) <[email protected]>
......
...@@ -202,7 +202,7 @@ void Board::PrintPretty(FILE * stream, const Piece::Colour & reveal) ...@@ -202,7 +202,7 @@ void Board::PrintPretty(FILE * stream, const Piece::Colour & reveal)
* Draw the board state to graphics * Draw the board state to graphics
* @param reveal - Pieces matching this colour will be revealed. All others will be shown as blank coloured squares. * @param reveal - Pieces matching this colour will be revealed. All others will be shown as blank coloured squares.
*/ */
void Board::Draw(const Piece::Colour & reveal) void Board::Draw(const Piece::Colour & reveal, bool showRevealed)
{ {
if (!Graphics::Initialised()) if (!Graphics::Initialised())