/* Part of the reset view procedure. Removes the board from the display. */ private void resetView(EGameModels model) { // clear the view tiles and remove it from the view this.genericBoardBase.Controls.Clear(); this.Controls.Remove(genericBoardBase); this.genericBoardBase = null; switch(model) { case EGameModels.Chess: this.blackPiecesCaptured.Controls.Clear(); this.whitePiecesCaptured.Controls.Clear(); this.white_turn_panel.Visible = false; this.black_turn_panel.Visible = false; break; case EGameModels.TicTacToe: break; } // clear message box this.message_box.Items.Clear(); }
/* As part of the abandon procedure of a game, remove the handlers registered to the current game model. */ private void deregisterHandlers(EGameModels model) { switch(model) { case EGameModels.Chess: this.gameController.Model.BoardChanged -= ChessBoardChanged; this.gameController.PropertyChanged -= message_PropertyChanged; this.gameController.Model.CapturedChanged -= ChessBoardCaptureChanged; this.gameController.Model.PlayerChanged -= ChessBoardPlayerChanged; break; case EGameModels.TicTacToe: this.gameController.PropertyChanged -= message_PropertyChanged; this.gameController.Model.BoardChanged -= TTTBoardChanged; break; } }
/* Handler registered to the new TicTacToe game menu item. This code clears any games if they are already in progress, tells the controller to prepare a game of type TicTacToe, creates the View components for the game, registers event handlers to the gameModels displayable events, prepares the game model (model.setup), adjusts final view components, finally tells the controller to begin the TicTacToe game loop. */ private void menuItemTTT_Click(object sender, EventArgs e) { // if a game was already in progress, clear it first if (this.gameController.State == EGameControlState.GameInProgress) abandonGameToolStripMenuItem_Click(null, null); this.selectedGameModel = EGameModels.TicTacToe; // Model this.gameController.InitialiseModel(this.selectedGameModel, this); // View AssembleTTTBoard(); // register event handlers to the ttt model this.gameController.PropertyChanged += message_PropertyChanged; this.gameController.Model.BoardChanged += TTTBoardChanged; // This comes after the handlers are registered so that // the boardchanged handler may update the view. this.gameController.PrepareModel(this.selectedGameModel); this.infoPanel.Visible = false; this.menuItemNewGame.Enabled = true; this.loadGameToolStripMenuItem.Enabled = false; this.abandonGameToolStripMenuItem.Enabled = true; this.message_box.Enabled = true; this.message_box.Visible = true; this.gameController.StartGameLoop(this.selectedGameModel); }
/* This takes a value indicating a gameModel and returns the values of the displayable and game models and utilities to null. */ public void UnInitialiseModel(EGameModels model) { switch(model) { case EGameModels.Chess: chessModel = null; evaluator = null; break; case EGameModels.TicTacToe: tictactoeModel = null; break; } displayableGameModel = null; gameState = EGameControlState.PreInitial; }
/* This takes a value indicating a gameModel and terminates any running gameLoop, sets some values to null and sets the models player value to null. */ public void Terminate(EGameModels model) { //terminate t, if its running StopGameLoop(); gameInput = null; infoMessage = null; switch(model) { case EGameModels.Chess: chessModel.Player = null; break; case EGameModels.TicTacToe: tictactoeModel.Player = null; break; } gameState = EGameControlState.Initial; this.Message = "Game is terminated"; }
/* This takes a value indicating a gameModel and starts a new thread running the appropriate gameLoop for the gameModel. */ public void StartGameLoop(EGameModels model) { switch(model) { case EGameModels.Chess: gLoopThread = new Thread(ChessGameLoop); break; case EGameModels.TicTacToe: gLoopThread = new Thread(TicTacToeGameLoop); break; } if (gameState == EGameControlState.Ready) gLoopThread.Start(); gameState = EGameControlState.GameInProgress; }
/* This takes a value indicating a gameModel and performs the initial setup of the model. Mainly populating with initial starting pieces / setting the starting player. */ public void PrepareModel(EGameModels model) { switch(model) { case EGameModels.Chess: chessModel.Setup(); chessModel.Player = new Player(EGamePlayers.White); break; case EGameModels.TicTacToe: tictactoeModel.Setup(); tictactoeModel.Player = new Player(EGamePlayers.X); break; } gameState = EGameControlState.Ready; }
/* Receives a value indicating a game model and sets up the model as 'displayableModel' (to which the view is bound) and also '*Model' (which is used in the game loop etc). This method also sets up any dependent utilities. */ public void InitialiseModel(EGameModels model, System.Windows.Forms.Form viewRef) { switch(model) { case EGameModels.Chess: displayableGameModel = new ChessPositionModel(); chessModel = (ChessPositionModel)displayableGameModel; evaluator = new Evaluator(); evaluator.ViewRef = viewRef; evaluator.GenerateRays(); evaluator.GeneratePawnRays(); break; case EGameModels.TicTacToe: displayableGameModel = new TicTacToePositionModel(); tictactoeModel = (TicTacToePositionModel)displayableGameModel; break; } gameState = EGameControlState.Initial; }