public RemotePlayer(ITurnGame game, User userInfo, Tile symbol ) { Game = game; Symbol = symbol; Player = PlayerType.Remote; UserInfo = userInfo; }
public RemotePlayer(ITurnGame game, User userInfo, Tile symbol) { Game = game; Symbol = symbol; Player = PlayerType.Remote; UserInfo = userInfo; }
public LocalPlayer(ITurnGame game, User user, Tile symbol) { Game = game; Symbol = symbol; Player = PlayerType.Local; UserName = user.UserName; ID = user.id; }
public ComputerPlayer(ITurnGame game, Tile symbol) { Game = game; m_symbol = symbol; if (m_symbol == game.Pieces[0]) m_opponentSymbol = game.Pieces[1]; else m_opponentSymbol = game.Pieces[0]; m_emptySymbol = game.Tiles[0]; m_playerType = PlayerType.Computer; m_rng = new Random((int)System.DateTime.Now.Ticks); }
public ComputerPlayer(ITurnGame game, Tile symbol) { Game = game; m_symbol = symbol; if (m_symbol == game.Pieces[0]) { m_opponentSymbol = game.Pieces[1]; } else { m_opponentSymbol = game.Pieces[0]; } m_emptySymbol = game.Tiles[0]; m_playerType = PlayerType.Computer; m_rng = new Random((int)System.DateTime.Now.Ticks); }
/// <summary> /// Create a game in the mobile service. /// </summary> /// <param name="userKey1">The id of the first user.</param> /// <param name="userKey2">The id of the second user.</param> /// <param name="game">The local game object to create in the mobile service.</param> /// <returns>The id of the created game.</returns> public async Task <string> CreateGame(string userKey1, string userKey2, ITurnGame game) { Game gameObj = new Game { User1 = userKey1, User2 = userKey2, Board = game.GameBoard, GameResult = (int)game.Result, CurrentPlayerIndex = game.CurrentPlayerIndex, Winner = "" }; // Insert a new entry in the "games" table await client.GetTable <Game>().InsertAsync(gameObj); // Return the gameID return((string)gameObj.id); }
/// <summary> /// Submit a move to record it in the mobile service. /// </summary> /// <param name="userId">The player making the move.</param> /// <param name="game">The game object which has the move details.</param> public async void MakeMove(string userId, ITurnGame game) { var moveObj = new Move { UserId = game.UserPlayer.ID, GameId = game.ID, NewBoard = game.GameBoard, GameResult = (int)game.Result, CurrentPlayerIndex = game.CurrentPlayerIndex, }; if (game.Winner != null) { moveObj.Winner = game.Winner.UserName; } else { moveObj.Winner = ""; } // Create a new entry in the moves table // (this also sends a push notification to the other user) Dictionary <string, string> parameter = new Dictionary <string, string>(); parameter.Add("OpponentUserId", game.OpponentPlayer.ID.ToString()); await client.GetTable <Move>().InsertAsync(moveObj, parameter); // Update the games table with the latest board. var gameRecord = new JObject(); gameRecord.Add("id", game.ID); gameRecord.Add("Board", game.GameBoard); gameRecord.Add("GameResult", (int)game.Result); gameRecord.Add("CurrentPlayerIndex", game.CurrentPlayerIndex); if (game.Winner != null) { gameRecord.Add("Winner", game.Winner.UserName); } else { gameRecord.Add("Winner", ""); } await client.GetTable <Game>().UpdateAsync(gameRecord); }
/// <summary> /// Submit a move to record it in the mobile service. /// </summary> /// <param name="userId">The player making the move.</param> /// <param name="game">The game object which has the move details.</param> public async void MakeMove(string userId, ITurnGame game) { var moveObj = new Move { UserId = game.UserPlayer.ID, GameId = game.ID, NewBoard = game.GameBoard, GameResult = (int)game.Result, CurrentPlayerIndex = game.CurrentPlayerIndex, }; if (game.Winner != null) moveObj.Winner = game.Winner.UserName; else moveObj.Winner = ""; // Create a new entry in the moves table // (this also sends a push notification to the other user) Dictionary<string, string> parameter = new Dictionary<string, string>(); parameter.Add("OpponentUserId", game.OpponentPlayer.ID.ToString() ); await client.GetTable<Move>().InsertAsync(moveObj, parameter); // Update the games table with the latest board. var gameRecord = new JObject(); gameRecord.Add("id", game.ID); gameRecord.Add("Board", game.GameBoard); gameRecord.Add("GameResult", (int)game.Result); gameRecord.Add("CurrentPlayerIndex", game.CurrentPlayerIndex); if (game.Winner != null) gameRecord.Add("Winner", game.Winner.UserName); else gameRecord.Add("Winner", ""); await client.GetTable<Game>().UpdateAsync(gameRecord); }
/// <summary> /// Create a game in the mobile service. /// </summary> /// <param name="userKey1">The id of the first user.</param> /// <param name="userKey2">The id of the second user.</param> /// <param name="game">The local game object to create in the mobile service.</param> /// <returns>The id of the created game.</returns> public async Task<string> CreateGame(string userKey1, string userKey2, ITurnGame game) { Game gameObj = new Game { User1 = userKey1, User2 = userKey2, Board = game.GameBoard, GameResult = (int)game.Result, CurrentPlayerIndex = game.CurrentPlayerIndex, Winner = "" }; // Insert a new entry in the "games" table await client.GetTable<Game>().InsertAsync(gameObj); // Return the gameID return (string) gameObj.id; }
/// <summary> /// This happens when a push notification is received that indicates that an opponent made a move. /// This event triggers all the games to be reloaded from the server. If the currently viewed game /// is the one that got updated, update the UI. /// </summary> public void ProcessOpponentTurn() { // Before updating the ActiveGames list and the MyTurnGames list, // detect whether the game being updated is the one currently being viewed. // Also update the currentGame to the new currentGame since we // recreated all our games. foreach (ITurnGame game in gameList) { if (String.Equals(game.CurrentPlayer.ID, userKey)) { // Check for this game in the myTurnGamesForUser list. var foundGame = this.myTurnGamesForUser.FirstOrDefault((item) => String.Equals(item.ID, game.ID)); if (foundGame == null) { // The game was not found previously in the my Turns list. // Check to see if it's a new game or one in the (old) activeGames list. if (this.activeGamesForUser.Exists((item) => String.Equals(item.GameId, game.ID))) { // It's in the old list, so it's not a new game. // Now find out if it's the current game and if so, // update the UI. if (String.Equals(game.ID, currentGame.ID)) { currentGame = game; Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { OnPropertyChanged("CurrentBoard"); OnPropertyChanged("Cells"); OnPropertyChanged("IsUsersTurn"); }); } } else { // It's a new game. // It will be added to the activeGames list and the myturns list. } } else { // The game was found in the myTurns List already, // no action necessary. } } } // Now update the active games this.myTurnGamesForUser = new ObservableCollection<ITurnGame>(); foreach (ITurnGame game in gameList) { if (String.Equals(game.CurrentPlayer.ID, userKey)) { this.myTurnGamesForUser.Add(game); } // This seems a bit odd, but replace the current game with the new // game that represents the same game. if (String.Equals(currentGame.ID, game.ID)) { currentGame = game; } } // This method is useful when you need to invoke a method on the UI thread from // a callback or other context. Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { OnPropertyChanged("ActiveGames"); OnPropertyChanged("MyTurnGames"); }); }
/// <summary> /// Sets which game the user wants to play. /// </summary> /// <param name="selectedGame"></param> public void SetCurrentGame(object selectedGame) { currentGame = (ITurnGame)selectedGame; Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { OnPropertyChanged("UserSymbol"); OnPropertyChanged("OpponentName"); OnPropertyChanged("OpponentSymbol"); this.isGameInProgress = true; RedrawCells(); selectedCellIndex = -1; ((DelegateCommand)submitMoveCommand).RaiseCanExecuteChanged(); OnPropertyChanged("IsGameInProgress"); }); }
/// <summary> /// Create a new game with the chosen opponent. /// </summary> /// <param name="parameter"></param> async void CreateNewGame(object parameter) { // Update the friend list PopulateFriendsList(); UserFriend opponent = null; bool isRemoteUserGame = false; currentGame = new TicTacToeGame(); User user = new User(this.userKey, this.userNameText); currentGame.Players.Add(new LocalPlayer(currentGame, user, currentGame.Pieces[0])); isRemoteUserGame = true; opponent = friendList[SelectedPlayerOption]; User opponentUserInfo = new User(opponent.User2Key, opponent.User2Name); currentGame.Players.Add(new RemotePlayer(currentGame, opponentUserInfo, currentGame.Pieces[1])); ResetCells(); currentGame.InitializeBoard(); // If this game involves a remote user, then create the game in the mobile service. if (isRemoteUserGame) { currentGame.ID = await mobileServiceClient.CreateGame(userKey, opponent.User2Key, currentGame); } // Hide the game setup area and previous game's result text. gameResultString = ""; OnPropertyChanged("GameResult"); isGameInProgress = true; OnPropertyChanged("IsGameInProgress"); OnPropertyChanged("CanCreateNewGame"); OnPropertyChanged("UserSymbol"); OnPropertyChanged("OpponentName"); OnPropertyChanged("OpponentSymbol"); // Reset any selection in the list of games (in case some other game was selected) SelectedGameIndex = -1; // Player 1 starts. currentGame.Start(); RefreshGamesLists(); }