private void PointGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (_currentPoint != null) { Cell tmp = _currentPoint.CurrentCell; _currentPoint.SetNewLocation(tmp.X, tmp.Y); _currentPoint = null; } int side; if (_game.IsLightTurn) side = MyPoint.LIGHT; else side = MyPoint.DARK; int x = (int)e.GetPosition(PointGrid).X; int y = (int)e.GetPosition(PointGrid).Y; _currentPoint = _game.Find(side, x, y); if (_currentPoint != null) { _currentPoint.BringToFront(); } }
private void PointGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (_currentPoint == null) return; int x = (int)e.GetPosition(PointGrid).X; int y = (int)e.GetPosition(PointGrid).Y; if (x < _currentPoint.X || x > _currentPoint.X + MyPoint.WIDTH || y < _currentPoint.Y || y > _currentPoint.Y + MyPoint.HEIGHT || !_game.JumpToCell(x, y, _currentPoint)) { Cell tmp = _currentPoint.CurrentCell; _currentPoint.SetNewLocation(tmp.X, tmp.Y); } ShowState(); if (_game.FreeCellsAmount == 0) MessageBox.Show(String.Format("Game over! The winner is {0}", _game.DarkCount < _game.LightCount ? "Red team" : "Green team")); else if (_game.IsLocked) { int lightCount = _game.LightCount + (_game.IsLightTurn ? 0 : _game.FreeCellsAmount); int darkCount = _game.DarkCount + (_game.IsLightTurn ? _game.FreeCellsAmount : 0); MessageBox.Show(String.Format("Game over! The winner is: {0}", (lightCount > darkCount) ? "Red team" : "Green team")); } _currentPoint.SetToBack(); _currentPoint = null; }
public void Start() { MyPoint t; t = new MyPoint(_cells[0][0], MyPoint.LIGHT, 0, 0); this._grid.Children.Add(t.CurrentImage); _lightList.Add(t); t = new MyPoint(_cells[4][8], MyPoint.LIGHT, 4, 8); this._grid.Children.Add(t.CurrentImage); _lightList.Add(t); t = new MyPoint(_cells[8][4], MyPoint.LIGHT, 8, 4); this._grid.Children.Add(t.CurrentImage); _lightList.Add(t); t = new MyPoint(_cells[0][4], MyPoint.DARK, 0, 4); this._grid.Children.Add(t.CurrentImage); _darkList.Add(t); t = new MyPoint(_cells[4][0], MyPoint.DARK, 4, 0); this._grid.Children.Add(t.CurrentImage); _darkList.Add(t); t = new MyPoint(_cells[8][8], MyPoint.DARK, 8, 8); this._grid.Children.Add(t.CurrentImage); _darkList.Add(t); this.IsLightTurn = true; /*for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) { t = new MyPoint(_cells[i][j], MyPoint.LIGHT, i, j); if (t.CurrentImage != null) this._grid.Children.Add(t.CurrentImage); }*/ }
private void Jump(MyPoint p, Cell c, int i, int j) { p.SetNewLocation(c.X, c.Y); p.I = i; p.J = j; c.Enabled = false; p.CurrentCell.Enabled = true; p.CurrentCell = c; IsLightTurn = !IsLightTurn; Expansion(p); // MessageBox.Show(String.Format("Game over! The winner is: {0}", _lightList.Count == 0 ? "Green team" : "Red team")); }
public void Load() { try { this._grid.Children.Clear(); _lightList.Clear(); _darkList.Clear(); _cells.Clear(); InitializeCells(); FileStream fs = new FileStream("save.bin", FileMode.Open); BinaryReader r = new BinaryReader(fs); int i, j, lcount; MyPoint t; IsLightTurn = r.ReadBoolean(); lcount = r.ReadInt32(); for (int ii = 0; ii < lcount; ii++) { i = r.ReadInt32(); j = r.ReadInt32(); t = new MyPoint(_cells[i][j], MyPoint.LIGHT, i, j); this._grid.Children.Add(t.CurrentImage); _lightList.Add(t); } while (r.BaseStream.Position < r.BaseStream.Length) { i = r.ReadInt32(); j = r.ReadInt32(); t = new MyPoint(_cells[i][j], MyPoint.DARK, i, j); this._grid.Children.Add(t.CurrentImage); _darkList.Add(t); } fs.Close(); } catch (Exception) { throw; } }
public bool JumpToCell(int x, int y, MyPoint p) { for (int i = 0; i < _cells.Count; i++) { for (int j = 0; j < _cells[i].Count; j++) { if (x - _cells[i][j].X >= 0 && x - _cells[i][j].X <= MyPoint.WIDTH && y - _cells[i][j].Y >= 0 && y - _cells[i][j].Y <= MyPoint.HEIGHT) { if (!_cells[i][j].Enabled || (i == p.I && j == p.J)) return false; if ((i == p.I - 2 && (j == p.J || j == p.J - 2)) || (i == p.I && (j == p.J - 2 || j == p.J + 2)) || (i == p.I + 2 && (j == p.J || j == p.J + 2))) { Jump(p, _cells[i][j], i, j); return true; } if ((i == p.I - 1 && (j == p.J || j == p.J - 1)) || (i == p.I && (j == p.J - 1 || j == p.J + 1)) || (i == p.I + 1 && (j == p.J || j == p.J + 1))) Clone(_cells[i][j], p.Side, i, j); return false; } } } return false; }
public int Expansion(MyPoint p) { if (p.Side == MyPoint.LIGHT) { for (int i = 0; i < _darkList.Count; i++) { if ((_darkList[i].I == p.I - 1 && (_darkList[i].J == p.J || _darkList[i].J == p.J - 1)) || (_darkList[i].I == p.I && (_darkList[i].J == p.J - 1 || _darkList[i].J == p.J + 1)) || (_darkList[i].I == p.I + 1 && (_darkList[i].J == p.J || _darkList[i].J == p.J + 1))) { if (ChangeList(_darkList[i], i) < 0) return -1; else i--; } } } else for (int i = 0; i < _lightList.Count; i++) { if ((_lightList[i].I == p.I - 1 && (_lightList[i].J == p.J || _lightList[i].J == p.J - 1)) || (_lightList[i].I == p.I && (_lightList[i].J == p.J - 1 || _lightList[i].J == p.J + 1)) || (_lightList[i].I == p.I + 1 && (_lightList[i].J == p.J || _lightList[i].J == p.J + 1))) { if (ChangeList(_lightList[i], i) < 0) return -1; else i--; } } return 0; }
public void Clone(Cell c, int side, int i, int j) { MyPoint t = new MyPoint(c, side, i, j); this._grid.Children.Add(t.CurrentImage); if (t.Side == MyPoint.LIGHT) _lightList.Add(t); else _darkList.Add(t); IsLightTurn = !IsLightTurn; Expansion(t); // MessageBox.Show(String.Format("Game over! The winner is: {0}", _lightList.Count == 0 ? "Green team" : "Red team")); }
public int ChangeList(MyPoint p, int i) { if (p.Side == MyPoint.DARK) { p.Side = MyPoint.LIGHT; _lightList.Add(_darkList[i]); _darkList.RemoveAt(i); } else { p.Side = MyPoint.DARK; _darkList.Add(_lightList[i]); _lightList.RemoveAt(i); } if (_lightList.Count == 0 || _darkList.Count == 0) return -1; return 0; }