internal static bool PlayMove(int id, int player, Tuple<int, int> spot, out int? winner, out string message) { PolarTicTacToeDataContext dataContext = new PolarTicTacToeDataContext(); var game = (from p in dataContext.Games where p.ID == id select p).FirstOrDefault(); var gameRules = new GameRules(); if (gameRules.HasPlayed(game, spot)) { message = "Spot already played"; winner = null; return false; } else { string move = spot.Item1 + ";" + spot.Item2 + ";" + String.Format("{0:g}", DateTime.Now) + ";false,"; game.Moves = game.Moves ?? ""; game.Moves += move; if (gameRules.IsFinished(game, out winner)) { winner = player; game.WinnerID = winner; game.GameState = Utils.GameState.Finished.ToString(); } dataContext.SubmitChanges(); message = ""; return true; } }
public void SetWinningMoves(List<Coordinate> winningCoords) { //PolarTicTacToeDataContext dataContext = new PolarTicTacToeDataContext(); //var curGame = (from p in dataContext.Games where p.ID == this.ID select p).FirstOrDefault(); var curGame = this; var gameRules = new GameRules(); foreach (Coordinate curWinner in winningCoords) { int moveXIndex = curGame.Moves.IndexOf(curWinner.X+";"+curWinner.Y+";"); int moveYIndex = curGame.Moves.IndexOf(";", moveXIndex+1); int moveDateIndex = curGame.Moves.IndexOf(";", moveYIndex+1); int moveWinnerIndex = curGame.Moves.IndexOf(";", moveDateIndex+1); string winnerSub = curGame.Moves.Substring(moveWinnerIndex+1, 5); curGame.Moves = curGame.Moves.Remove(moveWinnerIndex + 1, 5); curGame.Moves = curGame.Moves.Insert(moveWinnerIndex + 1, "true"); } //dataContext.SubmitChanges(); }