I've been developing an AI player class for a battleship game, but I've come across a wall. I think I got a good idea of how I want to go about my methods. It's an AI that searches the grid for random unhit cells, and once targeting something it chooses at random four directions until it uses all its choices and goes back to scouring the grid. Could someone please look over my code to see if it looks about right, because I've looked this over for a while, and there are some things I'm not sure about, like how I can keep the ai from hitting the same place twice. Thank you.
namespace Project6 { public class TemplatePlayer : Player { private enum DirectionEnum {NORTH,SOUTH,EAST,WEST}; private DirectionEnum[] Fails; bool found = true; Random rnd = new Random();
private int guessX, guessY;
public TemplatePlayer(String name) :
base(name)
{
}
/// <summary>
/// Gets the player ready to play a new game. Since the
/// player can be reused for mutliple games this method
/// must initialize all needed data structures.
/// </summary>
/// <param name="game">Game for the player to play.</param>
public override void StartGame( BattleShipGame game )
{
base.StartGame(game);
// Initialize the player data structures.
DirectionEnum Hunt;
}
/// <summary>
/// Returns the next Position to attack from the target list.
/// Check to make sure that the Position hasn't already been
/// played due to logic in Hit() processing.
/// </summary>
/// <returns>Position to attack for the turn.</returns>
public override Position Attack()
{
Position p;
Position lastTarget;
guessX = rnd.Next(0 , Game.GridSize);
guessY = rnd.Next(0 , Game.GridSize);
do
{
p = new Position(guessX , guessY);
lastTarget = p;
// compute position here and assign to p
} while (Game.HitOrMissAt(p) != BattleShipGame.HitOrMissEnum.UNKNOWN);
return p;
}
/// <summary>
/// Notifies the player that the Position was a hit. To
/// optimize the chances of future hits we should have
/// a strategy for trying neighboring Positions when
/// Attack() is called next.
/// </summary>
/// <param name="p">Hit position</param>
public override void Hit(Position p)
{
if( !Game.ShipSunkAt(p) )
{
//Eliinates Target Direction before guessing the next hit
if( p.Column <= Game.GridSize ) {
Fails[0] = DirectionEnum.NORTH;
p = new Position(p.Row , p.Column - 1);
}
if( p.Column >= 0 )
{
Fails[1] = DirectionEnum.SOUTH;
p = new Position(p.Row-1 , p.Column);
}
if( p.Row >= Game.GridSize )
{
Fails[2] = DirectionEnum.EAST;
}
if( p.Row <= 0 )
{
Fails[3] = DirectionEnum.WEST;
}
// Strategy for dealing with Positions near the hit.
else
{
p = new Position(p.Row , p.Column - 1);
}
}
}
public void circleTarget()
{
}
//Searchs for Fails Array for Direction
// if returns true. AI wont search in that direction
private bool Search(DirectionEnum i)
{
switch( i )
{
case DirectionEnum.NORTH:
return true;
case DirectionEnum.SOUTH:
return true;
case DirectionEnum.EAST:
return true;
case DirectionEnum.WEST:
return true;
default:
return false;
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire