private void changeCardButtonColorByPlayer(CardButton i_CardButtonToChange, Player i_PlayerTurn) { i_CardButtonToChange.BackColor = i_PlayerTurn == m_GameToPlay.Player1 ? Color.Aquamarine : Color.LightPink; i_CardButtonToChange.Update(); }
private void secondMoveOnFail(CardButton i_CardButton, bool i_IsComputer) { Thread.Sleep(1000); m_GameToPlay.GameBoard.EraseLastMoveFromBoard(i_CardButton.ButtonCard); Thread.Sleep(1000); if (m_GameToPlay.AiMode == eAiModes.Random && !i_IsComputer) { doComputerTurn(); } }
private void onTurnEnd(CardButton i_CardButton, bool i_IsComputer, bool i_GuessedRight) { if (!i_GuessedRight) { secondMoveOnFail(i_CardButton, i_IsComputer); } else { secondMoveOnSuccess(i_IsComputer); } }
private void InitializeTableLayoutPanel() { WebClient wc = new WebClient(); Dictionary <char, Image> imagesByLetters = new Dictionary <char, Image>(); int heightOfBoard = m_GameBoard.GetHeightOfBoard(); int lengthOfBoard = m_GameBoard.GetLengthOfBoard(); m_TableLayoutPanel.Dock = DockStyle.Fill; m_TableLayoutPanel.ColumnCount = lengthOfBoard; m_TableLayoutPanel.RowCount = heightOfBoard; for (int i = 0; i < lengthOfBoard; i++) { m_TableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100 / lengthOfBoard)); } for (int i = 0; i < heightOfBoard; i++) { m_TableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100 / heightOfBoard)); } for (int i = 0; i < heightOfBoard; i++) { for (int j = 0; j < lengthOfBoard; j++) { Card card = m_GameBoard.GetCardByCoordinates(new BoardCoordinates(i, j)); CardButton button = null; if (imagesByLetters.ContainsKey(card.Letter)) { button = new CardButton(card, imagesByLetters[card.Letter]); } else { byte[] bytes = wc.DownloadData("https://picsum.photos/80"); MemoryStream ms = new MemoryStream(bytes); Image imgFromWeb = Image.FromStream(ms); imagesByLetters.Add(card.Letter, imgFromWeb); button = new CardButton(card, imgFromWeb); } button.Name = $"card_{i}_{j}"; button.Dock = DockStyle.Fill; button.Click += m_CardButton_Click; m_TableLayoutPanel.Controls.Add(button, j, i); } } addLabelsToTableLayoutPanel(); }
private void doComputerTurn() { m_Timer.Enabled = true; disableClickEvents(); Player computerPlayerForColorChange = m_GameToPlay.WhosTurnIsIt(); BoardCoordinates[] nextMovesCoordinates = m_GameToPlay.AiForComputerPlay.GetCardsForNextMove(m_GameToPlay.GameBoard); m_GameToPlay.GameBoard.ExposeCard(m_GameToPlay.GameBoard.GetCardByCoordinates(nextMovesCoordinates[0])); CardButton firstCardButtonToChange = tableLayoutPanel.Controls.Find($"Card{nextMovesCoordinates[0].Row}{nextMovesCoordinates[0].Column}", true).FirstOrDefault() as CardButton; changeCardButtonColorByPlayer(firstCardButtonToChange, computerPlayerForColorChange); Thread.Sleep(1000); CardButton secondCardButtonToChange = tableLayoutPanel.Controls.Find($"Card{nextMovesCoordinates[1].Row}{nextMovesCoordinates[1].Column}", true).FirstOrDefault() as CardButton; changeCardButtonColorByPlayer(secondCardButtonToChange, computerPlayerForColorChange); bool wasSuccess = m_GameToPlay.GuessCardAndUpdateScores(m_GameToPlay.GameBoard.GetCardByCoordinates(nextMovesCoordinates[1])); onTurnEnd(secondCardButtonToChange, true, wasSuccess); }
private void InitializeComponent() { tableLayoutPanel = new TableLayoutPanel(); currentPlayerLabel = new Label(); player1ScoreLabel = new Label(); player2ScoreLabel = new Label(); m_GameToPlay.Player1.ScoreChange += Player_ScoreChange; m_GameToPlay.Player2.ScoreChange += Player_ScoreChange; m_GameToPlay.Player1.MyTurn += Player_MyTurn; m_GameToPlay.Player2.MyTurn += Player_MyTurn; m_GameToPlay.GameOver += m_GameToPlay_GameOver; WebClient wc = new WebClient(); Dictionary <char, Image> imagesByLetters = new Dictionary <char, Image>(); m_Timer = new Timer(); m_Timer.Tick += m_Timer_Tick; // currentPlayerLabel currentPlayerLabel.AutoSize = true; currentPlayerLabel.Font = new Font(Label.DefaultFont, FontStyle.Bold); Player_MyTurn(m_GameToPlay.WhosTurnIsIt()); // player1ScoreLabel player1ScoreLabel.AutoSize = true; player1ScoreLabel.BackColor = Color.Aquamarine; this.player1ScoreLabel.Text = $"{m_GameToPlay.Player1.Name}: 0 Pairs"; // player2ScoreLabel player2ScoreLabel.AutoSize = true; player2ScoreLabel.BackColor = Color.LightPink; this.player2ScoreLabel.Text = $"{m_GameToPlay.Player2.Name}: 0 Pairs"; // tableLayoutPanel int heightOfBoard = m_GameToPlay.GameBoard.GetHeightOfBoard(); int lengthOfBoard = m_GameToPlay.GameBoard.GetLengthOfBoard(); tableLayoutPanel.ColumnCount = lengthOfBoard; tableLayoutPanel.RowCount = heightOfBoard; for (int i = 0; i < lengthOfBoard; i++) { tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f / lengthOfBoard)); } for (int i = 0; i < heightOfBoard; i++) { tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100f / heightOfBoard)); } for (int i = 0; i < heightOfBoard; i++) { for (int j = 0; j < lengthOfBoard; j++) { Card card = m_GameToPlay.GameBoard.GetCardByCoordinates(new BoardCoordinates(i, j)); CardButton button = null; if (imagesByLetters.ContainsKey(card.Letter)) { button = new CardButton(card, imagesByLetters[card.Letter]); } else { byte[] bytes = wc.DownloadData("https://picsum.photos/80"); MemoryStream ms = new MemoryStream(bytes); Image imgFromWeb = Image.FromStream(ms); imagesByLetters.Add(card.Letter, imgFromWeb); button = new CardButton(card, imgFromWeb); } button.BackColor = Color.Transparent; button.Image = null; button.Name = $"Card{i}{j}"; button.Dock = DockStyle.Fill; button.Click += CardButton_Click; tableLayoutPanel.Controls.Add(button, j, i); } } tableLayoutPanel.Controls.Add(currentPlayerLabel); tableLayoutPanel.SetColumnSpan(currentPlayerLabel, lengthOfBoard); tableLayoutPanel.Controls.Add(player1ScoreLabel); tableLayoutPanel.SetColumnSpan(player1ScoreLabel, lengthOfBoard); tableLayoutPanel.Controls.Add(player2ScoreLabel); tableLayoutPanel.SetColumnSpan(player2ScoreLabel, lengthOfBoard); tableLayoutPanel.Dock = DockStyle.Fill; this.Controls.Add(tableLayoutPanel); this.Width = 700; this.Height = 700; this.Name = "GameForm"; this.Text = "Memory Game"; }