Ejemplo n.º 1
0
 public void BoardNoTurnsAtAlreadyOccupiedPositionByEnemy()
 {
     Board b = new Board();
     Position p = new Position(9, 3);
     b[p] = Side.Cross;
     b[p] = Side.Zero;
 }
Ejemplo n.º 2
0
 public void BoardNoTurnsAtAlreadyOccupiedPosition()
 {
     Board b = new Board();
     Position p = new Position(2, 6);
     b[p] = Side.Cross;
     b[p] = Side.Cross;
 }
Ejemplo n.º 3
0
 protected void madeTurn(Position pos)
 {
     if (OnTurnMade != null)
     {
         OnTurnMade(this, new TurnEventArgs(new Turn(pos, Side, 0)));
     }
 }
Ejemplo n.º 4
0
        public Position MakeNextTurn()
        {
            Position pos = new Position(4, 6);

            if (state.Count() > 0)
            {
                pos = ai.FindTurn(state);
            }

            undoList.Push(state.Advance(pos, Side));

            return pos;
        }
        public void HeuristicTurnsGenerator_GenerateFromTrivialState()
        {
            Position pos = new Position(3, 5);
            GameState initialState = new GameState();
            initialState.Advance(pos, Side.Cross);

            HeuristicTrunsGenerator gen = new HeuristicTrunsGenerator(new LineBasedTurnHeuristics());

            int i = 0;
            foreach (Position p in gen.GenerateTurns(initialState))
            {
                ++i;
            }

            Confirm.IsTrue(i > 0);
        }
Ejemplo n.º 6
0
        static void test_1()
        {
            GameState gs = new GameState();

            Position pos0 = new Position(3, 3);
            Position pos1 = new Position(4, 4);

            gs.Advance(pos0, Side.Cross);
            gs.Advance(pos1, Side.Zero);

            Console.WriteLine(gs);

            MinMax m = new MinMax(new SimpleCostFuntcion(Side.Zero), new HeuristicTrunsGenerator(new LineBasedTurnHeuristics()), 2);

            Position p = m.FindTurn(gs);
            Console.WriteLine("Turn found {0},{1}", p.X, p.Y);
        }
Ejemplo n.º 7
0
        public void State_DeriveStateAddsOneTurn()
        {
            //Test case:
            // | |.|.|.|
            // |.|.|o|.|
            // |.|x|.|.| --> initia pos(3,5)
            // |.|.|.| |
            // | | | | |

            GameState gs = new GameState();
            Position pos = new Position(3, 5);
            gs.Advance(pos, Side.Cross);
            Position turnPos = new Position(4, 4);
            gs.Advance(turnPos, Side.Zero);

            //Confirm.Equal(Side.Cross, gs.Player);
            Confirm.Equal(14, gs.Count<Position>());
            Confirm.Equal(Side.Cross, gs[pos].Side);
            Confirm.Equal(Side.Zero, gs[turnPos].Side);
        }
Ejemplo n.º 8
0
        public void BoardLastPosition()
        {
            Board b = new Board();
            Position p = new Position(0, 0);
            b[p] = Side.Cross;
            Confirm.Equal(b.LastPosition, p);

            p = new Position(4, 2);
            b[p] = Side.Zero;
            Confirm.Equal(b.LastPosition, p);

            p = new Position(-4, 3);
            b[p] = Side.Cross;
            Confirm.Equal(b.LastPosition, p);

            p = new Position(5, -21);
            b[p] = Side.Zero;
            Confirm.Equal(b.LastPosition, p);

            p = new Position(0, 2);
            b[p] = Side.Cross;
            Confirm.Equal(b.LastPosition, p);
        }
Ejemplo n.º 9
0
 public TouchEventArgs(Position pos)
 {
     Position = pos;
 }
Ejemplo n.º 10
0
 public Area(Position sw, Position ne)
 {
     this.sw = sw;
     this.ne = ne;
 }
Ejemplo n.º 11
0
 private void writePos(Position p, BinaryWriter bw)
 {
     bw.Write(p.row);
     bw.Write(p.column);
 }
Ejemplo n.º 12
0
        public void BoardUndo()
        {
            Board b = new Board();
            Position p0 = new Position(0, 0);
            b[p0] = Side.Cross;

            Position p1 = new Position(4, 2);
            b[p1] = Side.Zero;

            Position p2 = new Position(-4, 3);
            b[p2] = Side.Cross;

            Position p3 = new Position(5, -21);
            b[p3] = Side.Zero;

            Position p4 = new Position(0, 2);
            b[p4] = Side.Cross;

            Confirm.IsTrue(b.Undo());
            Confirm.Equal(Side.Nobody, b[p4]);

            Confirm.IsTrue(b.Undo());
            Confirm.Equal(Side.Nobody, b[p3]);

            Confirm.IsTrue(b.Undo());
            Confirm.Equal(Side.Nobody, b[p2]);

            Confirm.IsTrue(b.Undo());
            Confirm.Equal(Side.Nobody, b[p1]);

            Confirm.IsTrue(b.Undo());
            Confirm.Equal(Side.Nobody, b[p0]);

            Confirm.IsFalse(b.Undo());
        }
Ejemplo n.º 13
0
        public void State_InitialStateTest()
        {
            GameState gs = new GameState();

            Position pos = new Position(3,5);

            gs.Advance(pos, Side.Cross);

            Confirm.Equal(9, gs.Count<Position>());
            Confirm.Equal(Side.Cross, gs[pos].Side);
        }
Ejemplo n.º 14
0
        public void State_DeriveState_OpenFlag()
        {
            //Test case:
            // | | |o|
            // | |x| | --> initial pos(3,3)
            // | | | |

            GameState gs = new GameState();

            Position pos0 = new Position(3, 3);
            Position pos1 = new Position(4, 4);

            gs.Advance(pos0, Side.Cross);
            gs.Advance(pos1, Side.Zero);

            //Sanity:
            Confirm.Equal(Side.Cross, gs[pos0].Side);
            Confirm.Equal(Side.Zero, gs[pos1].Side);

            // (2, 2)
            {
                PositionInfo info = gs[new Position(2, 2)];
                Confirm.IsNotNull(info);
                PositionInfo.LineInfo l = info[PositionInfo.Orientation.DigonalSW, PositionInfo.Direction.Positive];
                Confirm.Equal(Side.Cross, l.side);
                Confirm.Equal(1, (int)l.amount);
                Confirm.Equal(false, l.open);
            }
            // (5, 5)
            {
                PositionInfo info = gs[new Position(5, 5)];
                Confirm.IsNotNull(info);
                PositionInfo.LineInfo l = info[PositionInfo.Orientation.DigonalSW, PositionInfo.Direction.Negative];
                Confirm.Equal(Side.Zero, l.side);
                Confirm.Equal(1, (int)l.amount);
                Confirm.Equal(false, l.open);
            }
        }
Ejemplo n.º 15
0
 public void EnemyTurn(Position pos)
 {
     undoList.Push(state.Advance(pos, Utils.FlipSide(Side)));
 }
Ejemplo n.º 16
0
 private void thinker()
 {
     readyToAnswer = false;
     Position pos = playerAI.MakeNextTurn();
     Thread.Sleep(300);
     lock (this)
     {
         readyToAnswer = true;
         answer = pos;
     }
 }
Ejemplo n.º 17
0
        public void State_DeriveStateUpdatesPositionInformation()
        {
            //Test case:
            // | | |o|
            // | |x| | --> initia pos(3,3)
            // | | |x|
            GameState gs = new GameState();
            Position pos0 = new Position(3, 3);
            Position pos1 = new Position(4, 4);
            Position pos2 = new Position(4, 2);
            // Create states
            gs.Advance(pos0, Side.Cross);
            gs.Advance(pos1, Side.Zero);
            gs.Advance(pos2, Side.Cross);
            // (4, 3)
            {
                PositionInfo info = gs[new Position(4, 3)];
                Confirm.IsNotNull(info);
                PositionInfo.LineInfo up = info[PositionInfo.Orientation.Vertical, PositionInfo.Direction.Positive];
                Confirm.Equal(Side.Zero, up.side);
                Confirm.Equal(1, (int)up.amount);
                Confirm.Equal(true, up.open);

                PositionInfo.LineInfo left = info[PositionInfo.Orientation.Horizontal, PositionInfo.Direction.Negative];
                Confirm.Equal(Side.Cross, left.side);
                Confirm.Equal(1, (int)left.amount);
                Confirm.Equal(true, left.open);

                PositionInfo.LineInfo down = info[PositionInfo.Orientation.Vertical, PositionInfo.Direction.Negative];
                Confirm.Equal(Side.Cross, down.side);
                Confirm.Equal(1, (int)down.amount);
                Confirm.Equal(true, down.open);
            }

            // (2,2)
            {
                PositionInfo info = gs[new Position(2, 2)];
                Confirm.IsNotNull(info);
                PositionInfo.LineInfo up = info[PositionInfo.Orientation.DigonalSW, PositionInfo.Direction.Positive];
                Confirm.Equal(Side.Cross, up.side);
                Confirm.Equal(1, (int)up.amount);
                Confirm.Equal(false, up.open);
            }
            // (2,4)
            {
                PositionInfo info = gs[new Position(2, 4)];
                Confirm.IsNotNull(info);
                PositionInfo.LineInfo up = info[PositionInfo.Orientation.DiagonalNW, PositionInfo.Direction.Positive];
                Confirm.Equal(Side.Cross, up.side);
                Confirm.Equal(2, (int)up.amount);
                Confirm.Equal(true, up.open);
            }
            // (5,1)
            {
                PositionInfo info = gs[new Position(5, 1)];
                Confirm.IsNotNull(info);
                PositionInfo.LineInfo up = info[PositionInfo.Orientation.DiagonalNW, PositionInfo.Direction.Negative];
                Confirm.Equal(Side.Cross, up.side);
                Confirm.Equal(2, (int)up.amount);
                Confirm.Equal(true, up.open);
            }
        }
Ejemplo n.º 18
0
 public Turn(Position pos, Side s, int i)
 {
     position = pos;
     side = s;
     index = i;
 }
Ejemplo n.º 19
0
        public void State_InitialStateHasCorrectPositionInfo()
        {
            GameState gs = new GameState();
            //empty state
            Position pos0 = new Position(4, 5);
            gs.Advance(pos0, Side.Cross);

            //
            foreach (PositionInfo.Orientation o in PositionInfo.Orientations)
            {
                foreach (PositionInfo.Direction d in PositionInfo.Directions)
                {
                    Position p = pos0 + PositionInfo.GetDirectionVector(o, d);
                    Confirm.IsNotNull(gs[p]);
                    Confirm.Equal(gs[p].Side, Side.Nobody);

                    PositionInfo info = gs[p];
                    //Check that line is dectected in the direction to pos0
                    //and that all others direction are empty
                    foreach (PositionInfo.Orientation o1 in PositionInfo.Orientations)
                    {
                        foreach (PositionInfo.Direction d1 in PositionInfo.Directions)
                        {
                            PositionInfo.LineInfo l = info[o1, d1];

                            if (o1 == o && d1 != d)
                            {
                                Confirm.Equal(Side.Cross, l.side);
                                Confirm.Equal(1, (int)l.amount);
                                Confirm.Equal(true, l.open);
                            }
                            else
                            {
                                Confirm.Equal(0, (int)l.amount);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        public Side this[Position pos]
        {
            set
            {
                try
                {
                    Side s = mField[pos.row, pos.column];

                    if (s != Side.Nobody || value != CurrentPlayer)
                    {
                        throw new InvalidTurnException();
                    }

                    updatePosition(pos, value);
                }
                catch (IndexOutOfRangeException)
                {
                    //Special case, not a game error. reallocation may be needed
                    throw new InvalidTurnException();
                }
            }
            get
            {
                try
                {
                    return mField[pos.row, pos.column];
                }
                catch (IndexOutOfRangeException)
                {
                    return Side.Nobody;
                }
            }
        }
Ejemplo n.º 21
0
        void checkUndoRedo(GameState gs, Position p, Side side)
        {
            string before = gs.ToString();
            GameState.ChangeSet change = gs.Advance(p, side);
            string after = gs.ToString();

            Console.WriteLine("BEFORE:");
            Console.WriteLine(before);

            Console.WriteLine("AFTER:");
            Console.WriteLine(after);

            Confirm.Different(before, after);
            gs.Undo(change);
            Console.WriteLine("UNDO:");
            Console.WriteLine(gs);

            Confirm.Equal(before, gs.ToString());
            gs.Advance(p, side);
            Confirm.Equal(after, gs.ToString());
        }
Ejemplo n.º 22
0
 public Area(Position sw, int w, int h)
 {
     this.sw = sw;
     this.ne = new Position(sw.row + w, sw.column + h);
 }
Ejemplo n.º 23
0
        public void State_DeriveStateUpdatesAlongTheLine()
        {
            //Test case:
            // | | |o| |
            // | |x|o| | --> initia pos(3,3)
            // | | |x| |
            // | | | |x|

            Position pos0 = new Position(3, 3);
            Position pos1 = new Position(4, 4);
            Position pos2 = new Position(4, 2);
            Position pos3 = new Position(3, 2);
            Position pos4 = new Position(5, 1);

            // Create states
            GameState gs = new GameState();
            gs.Advance(pos0, Side.Cross);
            gs.Advance(pos1, Side.Zero);
            gs.Advance(pos2, Side.Cross);
            gs.Advance(pos3, Side.Zero);
            gs.Advance(pos4, Side.Cross);

            //(6,0)
            {
                PositionInfo info = gs[new Position(6, 0)];
                Confirm.IsNotNull(info);
                PositionInfo.LineInfo up = info[PositionInfo.Orientation.DiagonalNW, PositionInfo.Direction.Negative];
                Confirm.Equal(Side.Cross, up.side);
                Confirm.Equal(3, (int)up.amount);
                Confirm.Equal(true, up.open);
            }

            //(2,4)
            {
                PositionInfo info = gs[new Position(2, 4)];
                Confirm.IsNotNull(info);
                PositionInfo.LineInfo up = info[PositionInfo.Orientation.DiagonalNW, PositionInfo.Direction.Positive];
                Confirm.Equal(Side.Cross, up.side);
                Confirm.Equal(3, (int)up.amount);
                Confirm.Equal(true, up.open);
            }
        }
Ejemplo n.º 24
0
        private bool translateTapPosition(Vector2 tap, out Position pos)
        {
            float globalX = lastVisibleRect.X + tap.X;
            float globalY = lastVisibleRect.Y + tap.Y;

            if (!lastVisibleRect.Contains(new Point((int)globalX, (int)globalY)))
            {
                pos = new Position();
                return false;
            }

            pos = new Position((short)(globalX / GameOptions.Instanse.CellSize), (short)(globalY / GameOptions.Instanse.CellSize));
            //problems with divion of negative numbers
            if (globalX < 0) pos.row--;
            if (globalY < 0) pos.column--;
            return true;
        }