Exemplo n.º 1
0
 public MainWindow()
 {
     mainBox = new MainBox();
     WindowStartupLocation = WindowStartupLocation.CenterScreen;
     if (mainBox.CustomShow() == System.Windows.Forms.DialogResult.Yes)
     {
         Console.WriteLine(mainBox.GetPlayerName(1));
         InitializeComponent();
         InitializeGame();
         InitializeBoard(false);
     }
     else
     {
         Close();
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Function called when a new game begins
        /// A new game can begin when a player click on "New Game" button
        /// or when a player loads a XML file that contains data from another game
        /// </summary>
        /// <param name="g">g = default if "new game" button is clicked</param>
        public void ProperlyNewGame(GameParameter g = default(GameParameter))
        {
            for (int i = 0; i < NB_ROW; i++)
            {
                for (int j = 0; j < NB_COL; j++)
                {
                    // TODO HERE CHANGE LBL BACKGROUND COLOR + RESET a ZERO LES CASES ET REMETTRES LES 4 TOKENS DU DEBUT
                    Label lbl = rules.GetLabel(tokenGrid, i, j);
                    tokenGrid.Children.Remove(lbl);
                }
            }

            btnUndo.IsEnabled = false;
            timerP1.Stop();
            timerP2.Stop();
            board = null;

            //New game = default GameParameter struct (no data loaded)
            if (g.Equals(default(GameParameter)))
            {
                mainBox = new MainBox();
                mainBox.ResetPlayerTokenPath();
                if (mainBox.CustomShow() == System.Windows.Forms.DialogResult.Yes)
                {
                    Console.WriteLine(mainBox.GetPlayerName(1));
                    InitializeComponent();
                    InitializeGame();
                    InitializeBoard(false);
                }
                else
                {
                    Close();
                }
            }
            else
            //data loaded from XML file
            {
                MessageBox.Show("Game loaded : \nPlayer1 : " + g.p1Name + ", Score : " + g.p1Score + "\nPlayer2 : " + g.p2Name + ", Score : " + g.p2Score);
                InitializeGame(g);
                InitializeBoard(true);
            }
        }
Exemplo n.º 3
0
        /// <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);
        }