public async Task DoMoveAsyncTest(HttpStatusCode status, bool success = true, char expected = 't') { MockHttpHandler handler = new MockHttpHandler(); handler.SetHttpStatus(status); var move = new Move { Type = MoveType.Set, X = 0, Y = 0 }; char[,] map = new char[10, 10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { map[i, j] = 'u'; } } map[0, 0] = 't'; var result = new MineResult { map = map, status = GameStatus.Ongoing, success = true, turn = true }; handler.SetReturnObj(result); ApiHandler.Instance.SetHandler(handler); result = await ApiHandler.Instance.DoMoveAsync(move); Assert.AreEqual(success, result.success); Assert.AreEqual(expected, result.map[0, 0]); }
public async Task SurrenderAsyncTest(HttpStatusCode status, bool success = true, char expected = 'e', GameStatus gameStatus = GameStatus.Lost) { MockHttpHandler handler = new MockHttpHandler(); handler.SetHttpStatus(status); char[,] map = new char[10, 10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { map[i, j] = 'u'; } } map[0, 0] = 'e'; var result = new MineResult { map = map, status = GameStatus.Lost, success = true, turn = true }; handler.SetReturnObj(result); ApiHandler.Instance.SetHandler(handler); result = await ApiHandler.Instance.SurrenderAsync(); Assert.AreEqual(success, result.success); Assert.AreEqual(expected, result.map[0, 0]); Assert.AreEqual(gameStatus, result.status); }
public async Task <MineResult> DoMoveAsync(Move move) { MineResult result = new MineResult(); var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(move)); // Wrap our JSON inside a StringContent which then can be used by the HttpClient class var httpContent = new StringContent(stringPayload, Encoding.UTF8, mediaType); HttpResponseMessage response = await client.PostAsync( requestUri + "DoMove/" + _gameId, httpContent); try { response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); result = JsonConvert.DeserializeObject <MineResult>(responseBody); } catch { //Catching result.success = false; } return(result); }
public MockAPI(MineResult result, Move move, PlayerData player, MoveSet role) { this.result = result; this.move = move; this.player = player; this.role = role; }
protected bool UpdateMap(MineResult result) { if (result.success) { yourTurn = result.turn; lock (obj) { dispatcher.Invoke(() => { label_turn.Content = yourTurn ? "Tavo �jimas" : "Prie�ininko �jimas"; RemakeGrid(result); }); if (result.status != GameStatus.Ongoing) { if (result.status == GameStatus.Won) { dispatcher.Invoke(() => { MessageBox.Show("J�s laim�jote!"); }); } else { dispatcher.Invoke(() => { MessageBox.Show("J�s pralaim�jote..."); }); } dispatcher.Invoke(() => { left_menu_not_in_game.Visibility = Visibility.Visible; left_menu_game_started.Visibility = Visibility.Hidden; left_menu_game_ended.Visibility = Visibility.Visible; }); started = false; return(true); } if (yourTurn) { return(true); } } } else { //TODO Error handling } return(false); }
public override void Surrender() { if (started) { var f = Task.Run(async() => await api.SurrenderAsync()); MineResult result = f.Result; RemakeGrid(result); started = false; MessageBox.Show("Jūs pralaimėjote..."); left_menu_not_in_game.Visibility = Visibility.Visible; left_menu_game_ended.Visibility = Visibility.Visible; left_menu_game_started.Visibility = Visibility.Hidden; //mineGrid.Visibility = Visibility.Hidden; } }
public async Task <string> GetHighScores() { MineResult result = new MineResult(); // Wrap our JSON inside a StringContent which then can be used by the HttpClient class HttpResponseMessage response = await client.GetAsync( requestUri + "GetPlayers/"); try { response.EnsureSuccessStatusCode(); return(await response.Content.ReadAsStringAsync()); } catch { //Catching return("Something went wrong"); } }
public IHttpActionResult Get() { var lastBlock = BlockChain.LastBlock(); var lastProof = lastBlock.Proof; var proof = BlockChain.ProofOfWorkAlgorithm.ProofOfWork(lastProof); // Reward ourselves for mining. 0 means that this node did it, and id is this nodes identifier. BlockChain.NewTransaction("0", _id, 1); // Forge the new block by adding it to the chain var block = BlockChain.NewBlock(proof); var response = new MineResult() { Message = "New Block Forged", Index = block.Index, Transactions = block.Transactions, Proof = block.Proof, PreviousHash = block.PreviousHash }; return(Json(response)); }
/// <summary> /// Remakes the grid /// </summary> /// <param name="Result">Status of the game and map</param> protected void RemakeGrid(MineResult result) { int ii = 10, jj = 10; mineGrid.ColumnDefinitions.Clear(); mineGrid.RowDefinitions.Clear(); mineGrid.Children.Clear(); for (int i = 0; i < ii; i++)//Create Rows and Columns { RowDefinition row = new RowDefinition() { SharedSizeGroup = "FirstRow"//equal size rows }; ColumnDefinition col = new ColumnDefinition { SharedSizeGroup = "FirstColumn" }; mineGrid.ColumnDefinitions.Add(col); mineGrid.RowDefinitions.Add(row); } for (int i = 0; i < ii; i++)//set buttons in cells { for (int j = 0; j < jj; j++) { //Button b = new Button(); //b.Content = string.Format("Row: {0}, Column: {1}", i, j); Object content = ""; bool enableButton = (result.turn && (result.status == GameStatus.Ongoing));//disables button if not player's turn, or the game is over char c = result.map[i, j]; string tag = string.Format("{0};{1}", i, j); switch (c) { case 'u': //Unknown break; case 't': //Bomb content = new Image { Source = new BitmapImage(new Uri(TntUri)),//image source path VerticalAlignment = VerticalAlignment.Center }; if (role == MoveSet.MineSweeper) { enableButton = false; } else { tag = string.Format("{0};{1};{2}", i, j, c); } break; case 'w': //Wrong content = new Image { Source = new BitmapImage(new Uri(WrongTntUri)),//image source path VerticalAlignment = VerticalAlignment.Center }; enableButton = false; break; case 'e': //Exploded content = new Image { Source = new BitmapImage(new Uri(ExplodedUri)),//image source path VerticalAlignment = VerticalAlignment.Center }; break; case 'm': //Marked content = content = new Image { Source = new BitmapImage(new Uri(MarkedUri)),//image source path VerticalAlignment = VerticalAlignment.Center }; break; default: content = c; enableButton = false; break; } Button b = new Button//button with image { //Width = 24, //Height = 24, Tag = tag, //for testing mostly IsEnabled = enableButton, //change to false to disable Content = content, }; Grid.SetRow(b, i); //set row Grid.SetColumn(b, j); //set column mineGrid.Children.Add(b); //add to grid } } }