public void PickTargetTouched(PickTarget target) { if (IsUserInteractionEnabled == false) { Debug.LogWarning("User interaction is disabled"); return; } if (selectedObj != null) { var peg = selectedObj.GetComponent <Peg>(); var position = new Game.Position(peg.size, target.index); if (gameLogic.PlayTurn(peg.gamePosition, position)) { DeselectCurrent(); Hilight(target.gameObject, spotLight.size); if (gameLogic.HasWinner) { Debug.Log("There's a winner!"); } } } }
/*public bool posToMove(Game.Position pos){ * if (direction == Direction.UP && pos.y == position.y-1) * return false; * if (direction == Direction.DOWN && pos.y == position.y+1) * return false; * if (direction == Direction.LEFT && pos.x == position.x -1) * return false; * if (direction == Direction.RIGHT && pos.x == position.x+1) * return false; * return true; * * } */ public bool isTail(Game.Position pos) { foreach (Game.Position tailPos in tail) { if (pos.sameAs(tailPos)) { return(true); } } return(false); }
public Position BestVector( Position pos ) { Position dist = pos.Wrap() - this.Wrap(); if ( dist.X >= GameState.MapWidth >> 1 ) dist.X -= GameState.MapWidth; else if ( dist.X < -( GameState.MapWidth >> 1 ) ) dist.X += GameState.MapWidth; if ( dist.Y >= GameState.MapHeight >> 1 ) dist.Y -= GameState.MapHeight; else if ( dist.Y < -( GameState.MapHeight >> 1 ) ) dist.Y += GameState.MapHeight; return dist; }
public bool canGoTo(Game.Position pos) { if (direction == Direction.UP && pos.y - 1 == position.y) { return(false); } if (direction == Direction.DOWN && pos.y + 1 == position.y) { return(false); } if (direction == Direction.LEFT && pos.x == position.x + 1) { return(false); } if (direction == Direction.RIGHT && pos.x == position.x - 1) { return(false); } return(true); }
static void Main() { byte right = 0; byte left = 1; byte down = 2; byte up = 3; Position[] directions = new Position[] { new Position(0, 1), //right new Position(0, -1), //left new Position(1, 0), //down new Position(-1, 0) //up }; int sleepTime = 100; int direction = right; Random randomNumbersGenerator = new Random(); Console.BufferHeight = Console.WindowHeight; Position food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth)); Queue<Position> snakeElements = new Queue<Position>(); /* Position position1 = new Position(0,0); position1.X = 0; position1.Y = 0; */ for (int i = 0; i < 7; i++) { snakeElements.Enqueue(new Position(0, i)); } foreach (Position position in snakeElements) { Console.SetCursorPosition(position.col, position.row); Console.Write("*"); } while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo userInput = Console.ReadKey(); if (userInput.Key == ConsoleKey.LeftArrow) { direction = left; } if (userInput.Key == ConsoleKey.RightArrow) { direction = right; } if (userInput.Key == ConsoleKey.UpArrow) { direction = up; } if (userInput.Key == ConsoleKey.DownArrow) { direction = down; } } Position snakeHead = snakeElements.Last(); Position nextDirection = directions[direction]; Position snakeNewHead = new Position(snakeHead.row + nextDirection.row, snakeHead.col + nextDirection.col); if (snakeNewHead.row < 0 || snakeNewHead.col < 0 || snakeNewHead.row >= Console.WindowHeight || snakeNewHead.col >= Console.WindowWidth) { Console.SetCursorPosition(0, 0); Console.WriteLine("Game over!"); } snakeElements.Enqueue(snakeNewHead); if ((snakeNewHead.col == food.col) && (snakeNewHead.row == food.row)) { //feeding food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight), randomNumbersGenerator.Next(0, Console.WindowWidth)); } else { //moving... snakeElements.Dequeue(); } Console.Clear(); foreach (Position position in snakeElements) { Console.SetCursorPosition(position.col, position.row); Console.Write("*"); } Console.SetCursorPosition(food.row, food.col); Console.Write("@"); Thread.Sleep(sleepTime); } }
public bool HasWon(Game.Position position) { return(board.HasWon(position)); }
public void Mark(Game.Position position, Game.Symbol symbol) { board.Mark(position, symbol); }
static void Main( string[] args ) { if ( args.Length < 3 ) return; using ( myLogStream = new FileStream( args[ 0 ], FileMode.Create, FileAccess.Write ) ) { myLogWriter = new StreamWriter( myLogStream ); GameState.Timeout = 1000; GameState.TurnLimit = 500; GameState.Seed = (int) DateTime.UtcNow.ToBinary(); GameState.Random = new Random( GameState.Seed ); GameState.FogOfWar = true; GameState.ViewRange = 5.0f; LogComment( "new game staring with properties:" ); Log( "turns", GameState.TurnLimit ); Log( "seed", GameState.Seed ); Log( "fow", GameState.FogOfWar.ToString().ToLower() ); Log( "timeout", GameState.Timeout ); Log( "vrange", GameState.ViewRange ); LogComment( "loading map" ); if ( !GameState.LoadMap( args[ 1 ] ) ) { LogComment( "Error while loading map " + args[ 1 ] ); myLogWriter.Close(); return; } if ( args.Length - 2 < GameState.TeamCount ) { LogComment( "Expected " + GameState.TeamCount + " competing programs" ); myLogWriter.Close(); return; } LogComment( "starting ai programs" ); bool started = true; for ( int i = 0; i < GameState.TeamCount; ++i ) { try { GameState.Teams[ i ].StartProgram( args[ i + 2 ] ); } catch { LogComment( String.Format( "Invalid executable location given for team #{0}, aborting", i ) ); started = false; } } if ( started ) { foreach ( Team team in GameState.Teams ) team.SendSetup(); for ( GameState.Turn = 0; GameState.Turn <= GameState.TurnLimit; ++GameState.Turn ) { Log( "turn", GameState.Turn ); if ( GameState.Turn > 0 ) { foreach ( Team team in GameState.Teams ) { if ( !team.Eliminated ) { team.SendGameState(); team.TakeTurn(); if ( team.Eliminated ) { Log( "# team", team.ID, "eliminated - timeout" ); team.WriteLine( "done" ); } } } } Agent[ , ] agentGrid = new Agent[ GameState.MapWidth, GameState.MapHeight ]; foreach ( Team team in GameState.Teams ) { foreach ( Agent agent in team.Agents ) { if ( !team.Eliminated ) agent.FinishTurn(); int x = agent.Position.X; int y = agent.Position.Y; if ( agentGrid[ x, y ] != null ) { agentGrid[ x, y ].Dead = agent.Dead = true; } else { agentGrid[ x, y ] = agent; } } } foreach ( Team team in GameState.Teams ) { foreach ( Agent agent in team.Agents ) { Position stabPos = agent.Position + agent.Direction; Agent victim = agentGrid[ stabPos.X, stabPos.Y ]; if ( victim != null && victim.Team != agent.Team ) victim.Dead = true; } } int liveCount = 0; int activeTeams = 0; foreach ( Team team in GameState.Teams ) { for ( int i = team.Agents.Count - 1; i >= 0; --i ) { Agent agent = team.Agents[ i ]; if ( agent.Dead ) { Log( "d", agent.ID, agent.Team.ID, agent.Position.X, agent.Position.Y, agent.Direction ); team.Agents.RemoveAt( i ); GameState.Dead.Add( agent ); } else { ++liveCount; Log( "a", agent.ID, agent.Team.ID, agent.Position.X, agent.Position.Y, agent.Direction ); } } if ( !team.Eliminated ) { if ( team.Agents.Count == 0 ) { team.Eliminated = true; Log( "# bot", team.ID, "eliminated - defeated" ); team.WriteLine( "done" ); } else ++ activeTeams; } } bool[ , ] packageGrid = new bool[ GameState.MapWidth, GameState.MapHeight ]; foreach ( Position pos in GameState.Packages ) packageGrid[ pos.X, pos.Y ] = true; for( int i = GameState.Packages.Count - 1; i >= 0; --i ) { Position pos = GameState.Packages[ i ]; Position newPos = new Position(); bool canMove = false; Agent agent = agentGrid[ pos.X, pos.Y ]; if ( agent != null ) { GameState.Packages.RemoveAt( i ); if ( !agent.Dead ) { newPos = agent.SpikePos; canMove = true; } } else { foreach ( Direction dir in Direction.All ) { Position p = pos + dir; Agent ag = agentGrid[ p.X, p.Y ]; if ( ag != null && ag.LastSpikePos.Equals( pos ) && !ag.SpikePos.Equals( ag.LastSpikePos ) ) { if ( agent != null ) { agent = null; break; } agent = ag; } } if ( agent != null ) { newPos = agent.SpikePos; if ( GameState.Map[ newPos.X, newPos.Y ] == Tile.Empty ) { GameState.Packages.RemoveAt( i ); canMove = true; } } } if ( canMove ) { if ( GameState.Map[ newPos.X, newPos.Y ] == Tile.Empty && !packageGrid[ newPos.X, newPos.Y ] && agentGrid[ newPos.X, newPos.Y ] == null ) { bool spawned = false; foreach ( Team team in GameState.Teams ) { if ( team.Eliminated ) continue; foreach ( Position bPos in team.Bases ) { if ( bPos.Equals( newPos ) ) { spawned = true; Agent newAgent = new Agent( team, newPos, agent.Direction ); team.Agents.Add( newAgent ); ++liveCount; Log( "a", newAgent.ID, team.ID, newPos.X, newPos.Y, agent.Direction ); break; } } if ( spawned ) break; } if ( !spawned ) GameState.Packages.Add( newPos ); } } } if ( activeTeams > 1 && liveCount + GameState.Packages.Count + GameState.TeamCount <= Math.Max( GameState.TeamCount * 2, ( GameState.MapWidth * GameState.MapHeight ) / 16 ) ) { int tries = 0; while ( tries++ < 256 ) { Position offset = new Position( GameState.Random.Next( GameState.MapWidth ), GameState.Random.Next( GameState.MapHeight ) ); bool placed = false; foreach ( Team team in GameState.Teams ) { Position pos = ( team.InitialBasePos + offset * team.InitialBaseDir ).Wrap(); bool validPos = true; foreach ( Team t in GameState.Teams ) { foreach ( Position b in t.Bases ) { if ( b.BestVector( pos ).LengthManhattan < 2 ) { validPos = false; break; } } if ( !validPos ) break; } if ( !validPos ) break; if ( GameState.Map[ pos.X, pos.Y ] == Tile.Wall ) break; if ( agentGrid[ pos.X, pos.Y ] != null ) continue; placed = true; GameState.Packages.Add( pos ); } if ( placed ) break; } } foreach ( Position pos in GameState.Packages ) Log( "p", pos.X, pos.Y ); if ( activeTeams <= 1 ) break; } } LogComment( "stopping ai programs" ); for ( int i = 0; i < GameState.TeamCount; ++i ) if ( GameState.Teams[ i ] != null ) GameState.Teams[ i ].StopProgram(); myLogWriter.Close(); } }
public static bool LoadMap( String mapPath ) { if ( !File.Exists( mapPath ) ) return false; using ( FileStream stream = new FileStream( mapPath, FileMode.Open, FileAccess.Read ) ) { StreamReader reader = new StreamReader( stream ); String line; while ( !reader.EndOfStream && ( line = reader.ReadLine().Trim().ToLower() ) != "map" ) { if ( line.Length == 0 ) continue; String[] split = line.Split( new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries ); if ( split.Length < 2 ) return false; switch ( split[ 0 ] ) { case "teams": TeamCount = int.Parse( split[ 1 ] ); break; case "width": MapWidth = int.Parse( split[ 1 ] ); break; case "height": MapHeight = int.Parse( split[ 1 ] ); break; default: return false; } } if( TeamCount == 0 || MapWidth == 0 || MapHeight == 0 ) return false; Program.Log( "teams", TeamCount ); Program.Log( "width", MapWidth ); Program.Log( "height", MapHeight ); Map = new Tile[ MapWidth, MapHeight ]; Teams = new Team[ TeamCount ]; for ( int i = 0; i < GameState.TeamCount; ++i ) Teams[ i ] = new Team( i ); Program.Log( "map" ); if ( !reader.EndOfStream ) { int y = 0; while ( !reader.EndOfStream && ( line = reader.ReadLine().ToLower() ).Trim() != "end" ) { if ( line.Length < MapWidth * 2 ) return false; line = line.Substring( 0, MapWidth * 2 ); Program.Log( line ); for ( int x = 0; x < MapWidth; ++x ) { char c = line[ x << 1 ]; if( c == '#' ) Map[ x, y ] = Tile.Wall; else if( char.IsNumber( c ) ) { int teamNo; if ( !int.TryParse( c.ToString(), out teamNo ) || teamNo >= TeamCount ) return false; Team team = Teams[ teamNo ]; Direction dir; if ( !Direction.TryParse( line[ ( x << 1 ) + 1 ].ToString(), out dir ) ) return false; Position pos = new Position( x, y ); team.Bases.Add( pos ); team.Agents.Add( new Agent( team, pos, dir ) ); team.InitialBasePos = pos; team.InitialBaseDir = dir; } else Map[ x, y ] = Tile.Empty; } ++y; } } Program.Log( "end" ); } Dead = new List<Agent>(); Packages = new List<Position>(); return true; }
public static bool IsWall( Position loc ) { return Map[ loc.X, loc.Y ] == Tile.Wall; }
private void FindVisibility() { for ( int x = 0; x < GameState.MapWidth; ++x ) for ( int y = 0; y < GameState.MapHeight; ++y ) myVisible[ x, y ] = false; foreach ( Agent agent in Agents ) { int xmin = (int) Math.Floor( agent.Position.X - GameState.ViewRange ); int xmax = (int) Math.Ceiling( agent.Position.X + GameState.ViewRange ); int ymin = (int) Math.Floor( agent.Position.Y - GameState.ViewRange ); int ymax = (int) Math.Ceiling( agent.Position.Y + GameState.ViewRange ); float squared = GameState.ViewRange * GameState.ViewRange; for ( int x = xmin; x <= xmax; ++x ) { for ( int y = ymin; y <= ymax; ++y ) { Position pos = new Position( x, y ).Wrap(); if ( agent.Position.BestVector( pos ).LengthSquared <= squared ) myVisible[ pos.X, pos.Y ] = true; } } } }
public void TakeTurn() { Program.Log( "team", ID ); Stopwatch timer = new Stopwatch(); timer.Start(); while ( true ) { if ( timer.ElapsedMilliseconds > GameState.Timeout ) { Program.LogComment( "=== timeout ===" ); Eliminated = true; return; } if ( HasError ) { Program.LogComment( "=== error ===" ); while( HasError ) Program.LogComment( myProcess.StandardError.ReadLine() ); Eliminated = true; return; } if ( myProcess.StandardOutput.EndOfStream ) { Thread.Yield(); continue; } String line = ReadLine().Trim().ToLower(); if ( line == "go" ) { Program.Log( "go" ); break; } String[] split = line.Split( new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries ); if ( split.Length != 3 ) { Eliminated = true; return; } int x, y; if ( !int.TryParse( split[ 0 ], out x ) || !int.TryParse( split[ 1 ], out y ) ) { Eliminated = true; return; } String order = split[ 2 ]; Position pos = new Position( x, y ); Agent agent = null; foreach ( Agent ag in Agents ) if ( ag.Position.Equals( pos ) ) agent = ag; if ( agent == null ) { Eliminated = true; return; } switch ( order ) { case "f": agent.Order = Order.MoveForward; Program.Log( string.Format( "o {0} {1} {2}", agent.ID, agent.Team.ID, order ) ); break; case "l": agent.Order = Order.TurnLeft; Program.Log( string.Format( "o {0} {1} {2}", agent.ID, agent.Team.ID, order ) ); break; case "r": agent.Order = Order.TurnRight; Program.Log( string.Format( "o {0} {1} {2}", agent.ID, agent.Team.ID, order ) ); break; default: Eliminated = true; break; } } }