Example #1
0
		private void SwitchPlayer ()
		{
			if (currentPlayer == redPlayer) {
				currentPlayer = bluePlayer;
			} else {
				currentPlayer = redPlayer;
			}
		}
Example #2
0
		/// <summary>
		/// Allows the game to perform any initialization it needs to before starting to run.
		/// This is where it can query for any required services and load any non-graphic
		/// related content.  Calling base.Initialize will enumerate through any components
		/// and initialize them as well.
		/// </summary>
		protected override void Initialize ()
		{
			gamegrid = new GameGrid ();
			winner = null;

			redPlayer = new HumanPlayer (GameGrid.Token.RED, gamegrid);
			bluePlayer = new ComputerPlayer (GameGrid.Token.BLUE, gamegrid);

			SwitchPlayer (); // defaults to red

			base.Initialize ();
				
		}
Example #3
0
		/// <summary>
		/// Allows the game to run logic such as updating the world,
		/// checking for collisions, gathering input, and playing audio.
		/// </summary>
		/// <param name="gameTime">Provides a snapshot of timing values.</param>
		protected override void Update (GameTime gameTime)
		{
			// For Mobile devices, this logic will close the Game when the Back button is pressed
			// Exit() is obsolete on iOS
			#if !__IOS__
			if (GamePad.GetState (PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
			    Keyboard.GetState ().IsKeyDown (Keys.Escape)) {
				Exit ();
			}
			#endif

			if (winner == null && gamegrid.IsGameDone () != GameGrid.Token.UNDEFINED) {

				int move = currentPlayer.GetMove ();
				if (move != -1 && gamegrid.IsMoveValid (move)) {
					gamegrid.PutToken (move, currentPlayer.Color);

					GameGrid.Token result = gamegrid.IsGameDone ();
					if (GameGrid.IsTokenPlayer (result) && currentPlayer.Color == result) {
						winner = currentPlayer;
					} else {
						SwitchPlayer ();
					}
				}

			} else {
				if (InputManager.IsMouseClicked ()) {
					winner = null;
					currentPlayer = null;
					gamegrid.Reset ();
					SwitchPlayer ();
				}
			}

			base.Update (gameTime);
		}
Example #4
0
		public void DrawCursor (Player currentPlayer, SpriteBatch spriteBatch)
		{
			int col = InputManager.GetMousePotition ().X / 150;
			if (currentPlayer is HumanPlayer && IsMoveValid (col)) {
				spriteBatch.Begin ();
				Vector2 pos = new Vector2 (col * 150, (tops [col] - 1) * 150);
				switch (currentPlayer.Color) {
				case Token.RED:
					spriteBatch.Draw (TOKEN_RED, pos, Color.White);
					break;
				case Token.BLUE:
					spriteBatch.Draw (TOKEN_BLUE, pos, Color.White);
					break;
				default:
					break;
				}
				spriteBatch.End ();
			}
		}