public async void SelectSquare(SquareImage squareImg) { if (!squareImg.Square.MyRPS) { return; } var(row, col) = squareImg.Square.Position; var json = await MyHttpClient.Game.SendRequestAsync(MyHttpClient.Endpoints.FLAG, new JObject { ["token"] = Prefs.Instance.Token, ["gameId"] = _context.GameId, ["row"] = row, ["col"] = col }); if ((bool)json["success"]) { squareImg.Square.Type = SquareType.Flag; _context.State = new SelectTrapState(_context, isOpponentReady); } else { throw new Exception("flag"); } }
public async void SelectSquare(SquareImage squareImg) { if (squareImg.Square.Type != SquareType.Empty || !squareImg.Square.MyRPS) { return; //flag check & is valid square } var(row, col) = squareImg.Square.Position; var json = await MyHttpClient.Game.SendRequestAsync(MyHttpClient.Endpoints.TRAP, new JObject { ["token"] = Prefs.Instance.Token, ["gameId"] = _context.GameId, ["row"] = row, ["col"] = col }); if ((bool)json["success"]) { squareImg.Square.Type = SquareType.Trap; _context.State = new RPSState(_context, isOpponentReady); } else { throw new Exception("trap"); } }
public async void SelectSquare(SquareImage squareImg) { //clear moves highlights //if chosen move // do move // go to waiting state //else // go to myTurn state //clear highlighting foreach (var move in _possibleMoves) { move.Button.ClearValue(Control.BackgroundProperty); } if (_possibleMoves.Contains(squareImg)) { await SendMove(squareImg); } else { (_context.State = new MyTurnState(_context)).SelectSquare(squareImg); //change state & execute clicking (selecting) on new square (this square is now selected) } }
public async void OnSelecting(SquareImage selected) { selected.Button.Background = new SolidColorBrush(Colors.Green); //send selection to server context.loadBar.Visibility = Visibility.Visible; var response = await MyHttpClient.Game.SendRequestAsync(MyHttpClient.Endpoints.DRAW, new JObject { ["token"] = Prefs.Instance.Token, ["decision"] = selected.Square.Type.ToString(), ["gameId"] = context.gameID, }); string opponentSelection = (string)response["opponent"]; if (opponentSelection != null) //opponent has selected { context.loadBar.Visibility = Visibility.Collapsed; int result = (int)response["result"]; context.Resolve(selected, opponentSelection, result); } else { context.State = new WaitingState(context, selected); //wait for opponent } }
private void RevealOpponent(string squareType, SquareImage pawn) { string path; SquareType type = (SquareType)Enum.Parse(typeof(SquareType), squareType, true); switch (type) { case SquareType.Paper: path = ImageFactory.BLUE_PAPER; break; case SquareType.Rock: path = ImageFactory.BLUE_ROCK; break; case SquareType.Scissors: path = ImageFactory.BLUE_SCISSORS; break; default: throw new Exception("invalid type"); } _context.Dispatcher.Invoke(() => pawn.Image.Source = ImageFactory.LoadImage(path)); pawn.Square.Type = type; }
private void InitBoard() { string[] paths = { ImageFactory.RED_ROCK, ImageFactory.RED_PAPER, ImageFactory.RED_SCISSORS }; Opponent = new SquareImage[3]; SquareImage[] squares = new SquareImage[3]; for (int i = 0; i < 3; i++) { //set opponent squares { var(button, border, img) = CreateView(ImageFactory.BLUE); Opponent[i] = new SquareImage { Button = button, Border = border, Image = img, Square = new Square(0, i) { Type = SquareType.Rock + i } }; Grid.SetColumn(button, i); Grid.SetRow(button, 0); rootView.Children.Add(button); } //set my square { const int row = 2; var(button, border, img) = CreateView(paths[i]); squares[i] = new SquareImage() { Button = button, Image = img, Border = border, Square = new Square(row, i) { Type = SquareType.Rock + i } }; int index = i; button.Click += (s, e) => { State.OnSelecting(squares[index]); }; Grid.SetColumn(button, i); Grid.SetRow(button, row); rootView.Children.Add(button); } } }
public void OnHoveringEnd(SquareImage squareImage) { if (_oldImageSource != null) { squareImage.Image.Source = _oldImageSource; _oldImageSource = null; } squareImage.Border.BorderBrush = new SolidColorBrush(Colors.Black); }
public void OnHoveringStart(SquareImage squareImage) { var border = squareImage.Border; if (squareImage.Square.MyRPS) { border.BorderBrush = new SolidColorBrush(Colors.Yellow); } }
private void AnimateSquareOpen(bool Open = true) { if (Open) { CloseOpenInfoWindows(); } squareopened = Open; if (Open) { if (IsLargeField(PlayerPieces[CurrentPlayer].CurrentField)) { SquareImage.Width = 520; Canvas.SetLeft(SquareImage, 245); } else if (System.Convert.ToInt32(SquareImage.GetValue(Canvas.LeftProperty)) != 405) { SquareImage.Width = 200; Canvas.SetLeft(SquareImage, 405); } } DoubleAnimation HorizontalAnimation = new DoubleAnimation(); DoubleAnimation VerticalAnimation = new DoubleAnimation(); HorizontalAnimation.From = 1; HorizontalAnimation.To = 520; VerticalAnimation.From = 330; VerticalAnimation.To = 70; if (!Open) { HorizontalAnimation.To = 0; HorizontalAnimation.From = 520; VerticalAnimation.From = 70; VerticalAnimation.To = 330; } HorizontalAnimation.Duration = VerticalAnimation.Duration = TimeSpan.FromSeconds(0.5); Storyboard SquareOpenStory = new Storyboard(); Storyboard SquareMoveStory = new Storyboard(); SquareOpenStory.Children.Add(HorizontalAnimation); SquareMoveStory.Children.Add(VerticalAnimation); Storyboard.SetTarget(SquareOpenStory, SquareImage); Storyboard.SetTargetProperty(SquareOpenStory, new PropertyPath(Image.HeightProperty)); Storyboard.SetTarget(SquareMoveStory, SquareImage); Storyboard.SetTargetProperty(SquareMoveStory, new PropertyPath(Canvas.TopProperty)); SquareOpenStory.Begin(); SquareMoveStory.Begin(); }
internal static void ResolveDraw(SquareImage selected, SquareImage target, int gameID) { var draw = new DrawRPS(selected, target, gameID, async result => { await Task.Delay(1000); Battle(result, selected, target, gameID); }); draw.ShowDialog(); }
public void OnHoveringEnd(SquareImage squareImage) { var color = Colors.Black; if (_possibleMoves.Contains(squareImage)) { color = Colors.Green; } squareImage.Border.BorderBrush = new SolidColorBrush(color); }
internal static void MoveTo(SquareImage selected, SquareImage target) { target.Image.Source = selected.Image.Source; //move image to target selected.Image.Source = ImageFactory.LoadImage(ImageFactory.NONE); //remove image from old position target.Square.MyRPS = selected.Square.MyRPS; //make square mine target.Square.Type = selected.Square.Type; selected.Square.MyRPS = false; //old square not mine selected.Square.Type = SquareType.Empty; }
public void OnHoveringStart(SquareImage squareImage) { var img = squareImage.Image; var border = squareImage.Border; if (_context.HoveredPosition.row >= 5) //if hovering my rows { _oldImageSource = img.Source; img.Source = ImageFactory.LoadImage(ImageFactory.RED_FLAG); border.BorderBrush = new SolidColorBrush(Colors.Yellow); } }
private void Resolve(SquareImage mySelection, string oppSelection, int result) { SquareType type; int index; switch (oppSelection.ToLower()) { case "rock": type = SquareType.Rock; index = 0; break; case "paper": type = SquareType.Paper; index = 1; break; default: type = SquareType.Scissors; index = 2; break; } //hide other squares Dispatcher.Invoke(async() => { Opponent[(index + 1) % 3].Button.Visibility = Visibility.Collapsed; Opponent[(index + 2) % 3].Button.Visibility = Visibility.Collapsed; Opponent[index].Image.Source = ImageFactory.LoadBlueImage(type); var opp_tuple = (Opponent[index].Square.Type, Opponent[index].Image.Source); var my_tuple = (mySelection.Square.Type, mySelection.Image.Source); await Task.Delay(1000); //for UX if (attacker.Square.MyRPS) { UpdateSquares(my_tuple, opp_tuple); //im attacking } else { UpdateSquares(opp_tuple, my_tuple); } onComplete(result); //callback SocketClient.Game.OnBroadcast -= OnReceive; //register to event Close(); }); }
public DrawRPS(SquareImage attacker, SquareImage target, int gameID, Action <int> onComplete) { InitializeComponent(); State = new ChoosingState(this); this.attacker = attacker; //dependency injection, in order to change those squares later this.target = target; this.gameID = gameID; this.onComplete = onComplete; userTxt.Text = (string)Prefs.Instance["name"]; SocketClient.Game.OnBroadcast += OnReceive; //register to event InitBoard(); }
internal static void Battle(int result, SquareImage attacker, SquareImage target, int gameID) { if (result == -1) { Kill(attacker); //attack lost } else if (result == 1) { MoveTo(attacker, target); //attack won } else if (result == 0) { ResolveDraw(attacker, target, gameID); // draw } else { throw new Exception("unkown result"); //other } }
} //do nothing public void SelectSquare(SquareImage squareImg) { var selected = squareImg.Square; if (selected.MyRPS && selected.Type != SquareType.Trap && selected.Type != SquareType.Flag) //valid selection - find possible moves { List <SquareImage> possibleMoves = new List <SquareImage>(); var(row, col) = selected.Position; var squares = _context.squares; //if move is valid & can go there (not my soldier) //check up if (row - 1 >= 0 && !squares[row - 1, col].Square.MyRPS) { possibleMoves.Add(squares[row - 1, col]); } //check left if (col - 1 >= 0 && !squares[row, col - 1].Square.MyRPS) { possibleMoves.Add(squares[row, col - 1]); } //check down if (row + 1 < MainWindow.BOARD_SIZE && !squares[row + 1, col].Square.MyRPS) { possibleMoves.Add(squares[row + 1, col]); } //check right if (col + 1 < MainWindow.BOARD_SIZE && !squares[row, col + 1].Square.MyRPS) { possibleMoves.Add(squares[row, col + 1]); } //highlight moves foreach (var move in possibleMoves) { move.Button.Background = new SolidColorBrush(Colors.Green); } //go to selected state _context.State = new SelectedSquareState(_context, squareImg, possibleMoves); } }
private static Dictionary<string, SquareImage> DrawBoardImages() { var playerColors = new[] { PlayerColor.Black, PlayerColor.White }; var ret = new Dictionary<string, SquareImage>(); var emptySquare = new SquareImage(); emptySquare.BlackSquare = DrawSquare(SquareColor.Black); emptySquare.WhiteSquare = DrawSquare(SquareColor.White); ret.Add("Empty", emptySquare); foreach (var type in typeof(Piece).Assembly.GetTypes().Where(i => i.IsSubclassOf(typeof(Piece)))) { foreach (var playerColor in playerColors) { var img = new SquareImage(); img.BlackSquare = DrawSquare(SquareColor.Black, playerColor, type); img.WhiteSquare = DrawSquare(SquareColor.White, playerColor, type); ret.Add(playerColor + type.Name, img); } } return ret; }
public SelectedSquareState(MainWindow context, SquareImage selected, List <SquareImage> possibleMoves) { _context = context; _possibleMoves = possibleMoves; _selected = selected; }
public WaitingState(DrawRPS context, SquareImage mySelection) { this.context = context; this.mySelection = mySelection; }
internal static void Kill(SquareImage square) { square.Image.Source = ImageFactory.LoadImage(ImageFactory.NONE); square.Square.MyRPS = false; square.Square.Type = SquareType.Empty; }
public void OnHoveringEnd(SquareImage squareImage) { }
public void OnSelecting(SquareImage squareImage) { } //Do Nothing
public void OnHoveringStart(SquareImage squareImage) { //do nothing }
public void SelectSquare(SquareImage squareImg) { }
public void OnHoveringEnd(SquareImage squareImage) { //do nothing }
public void OnHoveringStart(SquareImage squareImage) { }
private async Task SendMove(SquareImage target) { var(to_row, to_col) = target.Square.Position; var json = new JObject { ["token"] = Prefs.Instance.Token, ["gameId"] = _context.GameId, ["type"] = "move", ["from"] = new JObject { ["row"] = _selected.Square.Position.row, ["col"] = _selected.Square.Position.col, }, ["to"] = new JObject { ["row"] = to_row, ["col"] = to_col, } }; if (target.Square.Type != SquareType.Empty && !target.Square.MyRPS) { json["square_type"] = _selected.Square.Type.ToString(); } var response = await MyHttpClient.Game.SendRequestAsync(MyHttpClient.Endpoints.MOVE, json); if (!(bool)response["success"]) { throw new Exception($"error here - {response}"); } if (response.ContainsKey("s_type")) //reveal unkown opponent { string t = (string)response["s_type"]; var square = _context.squares[to_row, to_col]; square.Square.Type = (SquareType)Enum.Parse(typeof(SquareType), t, true); _context.Dispatcher.Invoke(() => square.Image.Source = ImageFactory.LoadBlueImage(square.Square.Type)); await Task.Delay(1000); //wait for UX - let user see opponent rps } if (response.ContainsKey("battle")) //do battle { int result = (int)response["battle"]; //get battle results _context.Dispatcher.Invoke(() => Moves.Battle(result, _selected, target, _context.GameId)); } else { _context.Dispatcher.Invoke(() => Moves.MoveTo(_selected, target)); } if (response.ContainsKey("winner")) { _context.GameOver(true); } else { _context.State = new WaitingState(_context); } }
public void OnHoveringEnd(SquareImage squareImage) { squareImage.Border.BorderBrush = new SolidColorBrush(Colors.Black); }
public void OnHoveringStart(SquareImage squareImage) { squareImage.Border.BorderBrush = new SolidColorBrush(Colors.Yellow); }