/// <summary> /// Function used to initialize the board. Create label, define parameters and row/column /// </summary> /// <param name="loadedGame">If loadedGame, create board from data</param> private void InitializeBoard(bool loadedGame, int[,] undoBoard = null) { tokenGrid.Background = board.backgroundBrush; for (int i = 0; i < NB_ROW; i++) { tokenGrid.RowDefinitions.Add(new RowDefinition()); for (int j = 0; j < NB_COL; j++) { Label lbl = new Label { Name = "i" + i + "j" + j }; lbl.ToolTip = (Char)((65) + (j)) + (i + 1).ToString(); lbl.MouseDown += OnClickLabel; lbl.MouseEnter += OnEnterLabel; lbl.MouseLeave += OnLeaveLabel; lbl.BorderThickness = new Thickness(0.1, 0.1, 0.1, 0.1); lbl.BorderBrush = Brushes.White; if (tokenGrid.ColumnDefinitions.Count < NB_COL) { tokenGrid.ColumnDefinitions.Add(new ColumnDefinition()); } Grid.SetColumn(lbl, j); Grid.SetRow(lbl, i); tokenGrid.Children.Add(lbl); //In this case, we put the 4 firsts tokens if (loadedGame == false) { if ((i == 3 && j == 3) || (i == 4 && j == 4)) { lbl.Background = player2.Token.ImgBrush; lbl.MouseDown -= OnClickLabel; board.SetNumberOnBoard(i, j, player2); } if ((i == 3 && j == 4) || (i == 4 && j == 3)) { lbl.Background = player1.Token.ImgBrush; lbl.MouseDown -= OnClickLabel; board.SetNumberOnBoard(i, j, player1); } lblPlayerImgTurn.Background = player1.Token.ImgBrush; timerP1.Start(); } //In this case, board already exists. We need to check each case to put tokens else { //if undoButton clicked, replace current board with undoBoard (copy of board at precedent turn) if (undoBoard != null) { board.SetBoard(undoBoard); } if (board.GetNumberOnBoard(i, j) == 1) { lbl.Background = player1.Token.ImgBrush; lbl.MouseDown -= OnClickLabel; } else if (board.GetNumberOnBoard(i, j) == 2) { lbl.Background = player2.Token.ImgBrush; lbl.MouseDown -= OnClickLabel; } } } } rules.CheckCases(); }
/// <summary> /// Function that will instanciate all the parameters/attributes necessary for a game. /// </summary> /// <param name="g">g = default if "new game" button is clicked</param> private void InitializeGame(GameParameter g = default(GameParameter)) { board = new Board(NB_ROW, NB_COL); if (mainBox.GetPlayerTokenPath(1) != "") { token1 = new Token(mainBox.GetPlayerTokenPath(1)); } else { token1 = new Token(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"..\..\Assets\Tokens\token1.png")); } if (mainBox.GetPlayerTokenPath(2) != "") { token2 = new Token(mainBox.GetPlayerTokenPath(2)); } else { token2 = new Token(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"..\..\Assets\Tokens\token2.png")); } //New game if (g.Equals(default(GameParameter))) { player1 = new Player(token1, mainBox.GetPlayerName(1), 1); player2 = new Player(token2, mainBox.GetPlayerName(2), 2); timerP1 = new MyStopwatch(); timerP2 = new MyStopwatch(); lblPlayer1Score.DataContext = new Player { Score = player1.Score }; lblPlayer2Score.DataContext = new Player { Score = player2.Score }; lblPlayerTurn.Content = player1.Name + "'s turn :"; isPlayer1 = true; nbFreeCells = (NB_ROW * NB_COL) - 4; turn = 1; } //Loaded game. Attributes/Properties will be charged by data in GameParameter struct else { player1 = new Player(token1, g.p1Name, 1); player2 = new Player(token2, g.p2Name, 2); board.SetBoard(g.newBoard); if (g.playerTurn == 2) { isPlayer1 = false; lblPlayerTurn.Content = player2.Name + "'s turn :"; lblPlayerImgTurn.Background = player2.Token.ImgBrush; } else { isPlayer1 = true; lblPlayerTurn.Content = player1.Name + "'s turn :"; lblPlayerImgTurn.Background = player1.Token.ImgBrush; } timerP1 = g.p1Stopwatch; timerP2 = g.p2Stopwatch; if (isPlayer1) { timerP1.Start(); } else { timerP2.Start(); } nbFreeCells = board.GetFreeCells(); turn = g.turn; } lblPlayer1.Content = player1.Name; lblPlayer2.Content = player2.Name; CheckScore(); //Timer used to update lblPlayer1/2Time timerUpdate = new Timer(10); timerUpdate.Elapsed += Timer_tick; timerUpdate.Start(); // Initialize Rules rules = new Rules(board, player1, player2, tokenGrid, NB_ROW, NB_COL); }