What algorithm best describe this?

Started by
18 comments, last by wodinoneeye 12 years, 8 months ago
I just made a board game, and i dont know what Algorithm best describe this referee or arbiter.

The role of arbiter is to check which rank is higher and then remove the lower rank pieces..

I use a lot if IF CONDITIONS to make it work..

Do you know any algorithm that can i put on my papers?

Thanks!
Advertisement
Can you put your pieces into an array and sort them by rank? Perhaps you can make a custom object with piece index and rank only (if the piece class is too wide) and then sort them that way, with refference to the piece index so the lowest sorted can be removed.

If you need help sorting arrays, tell us more about the kind of data that's in them.
I believe this would be a use for the mysterious and often confusing "greater than" sign. In the wild, it is often seen in disguise as ">".

blink.gif

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

Right now your "lots of IF CONDITIONS" is the algorithm. It probably is not expressed as concisely as possible. I imagine it can be reduced to a number of simpler loops.

Can you describe the game, in English, and what the referee or arbiter does?


Do you know any algorithm that can i put on my papers?
[/quote]
Is this for school?
The game is this.. http://en.wikipedia.org/wiki/Game_of_the_Generals

No, not for school, I just want to make a documentation on instruction how I created the game and algorithm how it works.

The arbiter of the game is the referee..

Both players cannot see others pieces.. And if they collide on the same cell the arbiter will look what piece wins..

The arbiter will be the one who will remove, retain or advance the pieces.
Not every piece of code that you might write corresponds to an algorithm with a name, as your question seems to imply. The best way to document code like that is to divide it into functions with good names.

I would probably not use any conditionals to determine who wins, but a lookup table instead:
enum Piece {FiveStarGeneral, FourStarGeneral, ThreeStarGeneral, /*...*/, Flag, NPieces};

enum BattleResult {AttackerWins, NobodyWins, DefenderWins};

BattleResult compute_battle(Piece attacker, Piece deffender) {
static BattleResult const result_table[NPieces][NPieces] = {
{NobodyWins, AttackerWins, AttackerWins, /*...*/},
{DefenderWins, NobodyWins, AttackerWins, /*...*/},
/*...*/
};

return table[attacker][defender];
}

He's already written the logic, he just wants a name.

Not all algorithms have a fancy name. You simply implemented the rules of the game. So just state the rules of the game as they appear on the Wikipedia page.

-me

He's already written the logic, he just wants a name.

If you are responding to my code, I should have made it clear that the real answer to the question was the first paragraph of my post. The code is just an idea I threw in for free. :)


Here is the whole code of my arbiter, its written on Java..Basically it is a two player game by Local Area Network.



{
ARank = Integer.parseInt(Board[intBoardTempOne][intBoardTempTwo].getText());
System.out.println(ARank);
Sender.GrabInfo(intCurrentX, intCurrentY);
try
{
Thread.sleep(50L);//Pause the Program
}
catch(InterruptedException interruptedexception1) { }
YourTurn = false;
Sender.setTurn("true");
//When Pieces are the same on rank
if(ARank == BRank)
{
txtArea.append("\n<System> Both Pieces have died coming out of this battle");
ChatSocket.sendText("z<System> Both Pieces have died coming out of this battle");
Board[intBoardTempOne][intBoardTempTwo].setText("");
Board[intCurrentY][intCurrentX].setText("");
removePic(intCurrentY, intCurrentX);
removePic(intBoardTempOne, intBoardTempTwo);
Sender.EraseEnemy(intCurrentX, intCurrentY);
Sender.EraseEnemy(intBoardTempTwo, intBoardTempOne);
} else
// When Host Private and Client's Spy
if(ARank == 1 && BRank == 14)
{
txtArea.append("\n<System> You have come out victorious, from your battle.");
ChatSocket.sendText("z<System> You have been defeated through a horrendous battle.");
Board[intCurrentY][intCurrentX].setText(Board[intBoardTempOne][intBoardTempTwo].getText());
setPic(intCurrentY, intCurrentX, Board[intBoardTempOne][intBoardTempTwo].getText());
Board[intBoardTempOne][intBoardTempTwo].setText("");
removePic(intBoardTempOne, intBoardTempTwo);
Sender.EraseEnemy(intBoardTempTwo, intBoardTempOne);
Sender.sendPos(intCurrentX, intCurrentY);
} else
// When Host Spy and Client's Private
if(ARank == 14 && BRank == 1)
{
txtArea.append("\n<System> You have been defeated through a horrendous battle");
ChatSocket.sendText("z<System> You have come out victorious, from your battle.");
Board[intBoardTempOne][intBoardTempTwo].setText("");
removePic(intBoardTempOne, intBoardTempTwo);
Board[intCurrentY][intCurrentX].setText("enemy");
setPic(intCurrentY, intCurrentX, "enemy");
Sender.EraseEnemy(intBoardTempTwo, intBoardTempOne);
Sender.EraseEnemy(intCurrentX, intCurrentY);
Sender.SendLabel(BRank, intCurrentX, intCurrentY);
} else
// When Host have higher rank of piece than the Client
if(ARank > BRank)
{
txtArea.append("\n<System> You have come out victorious from your battle.");
ChatSocket.sendText("z<System> You have been defeated through a horrendous battle.");
Board[intCurrentY][intCurrentX].setText(Board[intBoardTempOne][intBoardTempTwo].getText());
setPic(intCurrentY, intCurrentX, Board[intBoardTempOne][intBoardTempTwo].getText());
Board[intBoardTempOne][intBoardTempTwo].setText("");
removePic(intBoardTempOne, intBoardTempTwo);
Sender.EraseEnemy(intBoardTempTwo, intBoardTempOne);
Sender.sendPos(intCurrentX, intCurrentY);
} else
// When Host have lower rank of piece than the Client
if(ARank < BRank)
{
txtArea.append("\n<System> You have been defeated through a horrendous battle");
ChatSocket.sendText("z<System> You have come out victorious, from your battle.");
Board[intBoardTempOne][intBoardTempTwo].setText("");
removePic(intBoardTempOne, intBoardTempTwo);
Board[intCurrentY][intCurrentX].setText("enemy");
setPic(intCurrentY, intCurrentX, "enemy");
Sender.EraseEnemy(intBoardTempTwo, intBoardTempOne);
Sender.EraseEnemy(intCurrentX, intCurrentY);
Sender.SendLabel(BRank, intCurrentX, intCurrentY);
}
// When two flag meets
if(ARank == 0 && BRank == 0)
{
setPic(intCurrentY, intCurrentX, "" + BRank);
setPic(intBoardTempTwo, intBoardTempTwo, "" + ARank);
Sender.SendLabel(ARank, intBoardTempTwo, intBoardTempOne);
Sender.SendLabel(BRank, intCurrentX, intCurrentY);
Sender.sendYouWin();
txtArea.append("\n<System> Tie game!!");
DisableBoard();
Sender.sendTie();
} else
if(ARank == 0)
{
setPic(intCurrentY, intCurrentX, "" + BRank);
setPic(intBoardTempTwo, intBoardTempTwo, "" + ARank);
Sender.SendLabel(ARank, intBoardTempTwo, intBoardTempTwo);
Sender.sendYouWin();
txtArea.append("\n<System> Good game, try again next time.");
txtArea.append("\n<System> Winner will choose to start next game or not.");
DisableBoard();
} else
if(BRank == 0)
{
setPic(intCurrentY, intCurrentX, "" + BRank);
setPic(intBoardTempTwo, intBoardTempTwo, "" + ARank);
Sender.SendLabel(ARank, intBoardTempTwo, intBoardTempTwo);
Sender.SendLabel(BRank, intCurrentX, intCurrentY);
txtArea.append("\n<System> Congratulations, you WIN!");
DisableBoard();
Sender.sendYouLose();
ConfirmNewG.setVisible(true);
}
ConfirmMove.setVisible(false);
Moving = false;
ReturnBoard();


Ugh... separate the decision logic from the result. Bad code style, dude. There is so much duplication of code in there that my head was spinning. That whole thing could be reduced to about 20 lines.

Write a single function that checks two pieces and returns a result of who won (or a tie). Then based on that result, you only have a couple of branches to tell the player and the game what happened.

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

This topic is closed to new replies.

Advertisement