public MainWindow() { InitializeComponent(); game = new Connect4Game(); spaces = new List <List <Ellipse> >(); for (int rowNumber = 0; rowNumber < 6; rowNumber++) { var row = new List <Ellipse>(); for (int columnNumber = 0; columnNumber < 7; columnNumber++) { var ellipse = new Ellipse(); ellipse.Height = 50; ellipse.Width = 50; ellipse.Stroke = Brushes.Black; ellipse.StrokeThickness = 5; ellipse.HorizontalAlignment = HorizontalAlignment.Left; ellipse.VerticalAlignment = VerticalAlignment.Top; ellipse.Margin = new Thickness(100 + (60 * columnNumber), 50 + (60 * rowNumber), 0, 0); grid.Children.Add(ellipse); row.Add(ellipse); } spaces.Add(row); } updateDisplay(); }
/// <summary> /// Create a new GameWindow. /// </summary> public MainWindow() { InitializeComponent(); // Create a new Connect4Game. m_Connect4Game = new Connect4Game(6, 7, 6); // Initialize playing board. InitializeGameWindow(); // Delegate for varios events. GameGrid.OnColumnFull += new GameGrid.dlgOnColumnFull(OnColumnFull); ColumnButton.OnColumnButtonClicked += new ColumnButton.dlgOnButtonClicked(OnColumnButtonClicked); Connect4Game.OnHumanPlayerPlayed += new Connect4Game.dlgOnHumanPlayerPlayed(UpdateGUI); Connect4Game.OnWin += new Connect4Game.dlgOnWin(OnWin); Connect4Game.OnScoreCalculated += new Connect4Game.dlgOnScoreCalculated(ModifyScoreTextBox); Content = m_WindowGrid; }
/// <summary> /// Process the actual game state /// </summary> /// <param name="gameLogic"></param> /// <param name="models"></param> /// <param name="gameSettings"></param> #region static public void Process(Game gameLogic, Models models, GameSettings gameSettings) static public void Process(Connect4Game gameLogic, GameSettings gameSettings, IBoard board) { // Gameplay for the chosen game mode switch (gameLogic.GameMode) { case GameSettings.GAMEMODE.SinglePlayer: { FSM_SinglePlayer(gameLogic, gameSettings, board); break; } case GameSettings.GAMEMODE.LocalMultiPlayer: { //FSM_MultiPlayer(gameLogic, gameSettings); break; } case GameSettings.GAMEMODE.CPUvsCPU: { //FSM_CPUvsCPU(); break; } } }
/// <summary> /// Constructor. /// </summary> public GameplayScreen() { TransitionOnTime = TimeSpan.FromSeconds (1.5); TransitionOffTime = TimeSpan.FromSeconds (0.5); gameSettings = new GameSettings(); gameLogic = new Connect4Game (GameSettings.GAMEMODE.SinglePlayer, gameSettings.STARTING_PLAYER, gameSettings.AI_STRENGTH, gameSettings); player1 = new Player2 (gameSettings.PLAYER_RED_NAME, true); switch (gameSettings.AI_STRENGTH) { case GameSettings.DIFFICULTY.Easy: { player2 = new Player2 ("RookieBot", false); break; } case GameSettings.DIFFICULTY.Medium: { player2 = new Player2 ("TrainedBot", false); break; } case GameSettings.DIFFICULTY.Hard: { player2 = new Player2 ("Chief O'Bot", false); break; } } gameLogic.player1 = player1; gameLogic.player2 = player2; this.Reset(gameSettings); }
public void SwitchStones(Connect4Game gameLogic) { var yPos = 55; var xPos = 0; // Enable next stone if (gameLogic.activePlayer == gameSettings.RED_PLAYER) { RedStones[gameLogic.turn].SetPosition(xPos, yPos); RedStones[gameLogic.turn].Enable(true); ActiveStone = RedStones[gameLogic.turn]; } else { YellowStones[gameLogic.turn].SetPosition(xPos, yPos); YellowStones[gameLogic.turn].Enable(true); ActiveStone = YellowStones[gameLogic.turn]; } }
public bool DropStone(Connect4Game gameLogic) { ActiveStone.Position.Y -= 5; var yFinal = RowDistance * gameSettings.ROWS - (gameSettings.ROWS - gameLogic.row -1) * RowDistance -3f; if (ActiveStone.Position.Y <= yFinal) { ActiveStone.Position.Y = yFinal; return true; } return false; }
public void SwitchStones(Connect4Game gameLogic) { var yPos = 0;//gameSettings.ROWS * gameSettings.rowDistance; var xPos = 3 * this.ColumnDistance; // Enable next stone if (gameLogic.activePlayer == gameSettings.RED_PLAYER) { RedStones[gameLogic.turn].SetPosition(xPos, yPos); RedStones[gameLogic.turn].Enable(true); ActiveStone = RedStones[gameLogic.turn]; } else { YellowStones[gameLogic.turn].SetPosition(xPos, yPos); YellowStones[gameLogic.turn].Enable(true); ActiveStone = YellowStones[gameLogic.turn]; } ActiveStone.Enable(true); }
public bool DropStone(Connect4Game gameLogic) { return true; }
/// <summary> /// Single player mode /// </summary> /// <param name="gameLogic"></param> /// <param name="models"></param> /// <param name="gameSettings"></param> #region private static void FSM_SinglePlayer(...) private static void FSM_SinglePlayer(Connect4Game gameLogic, GameSettings gameSettings, IBoard board) { switch (gameLogic.GameState) { case GameSettings.GAMESTATE.S_BeforeGame: { if (gameLogic.STARTING_PLAYER == gameSettings.RED_PLAYER) { gameLogic.GameState = GameSettings.GAMESTATE.S_Player1Move; gameLogic.HUMAN_PLAYER = gameSettings.RED_PLAYER; } else { gameLogic.GameState = GameSettings.GAMESTATE.S_WaitForCPU; gameLogic.HUMAN_PLAYER = gameSettings.YELLOW_PLAYER; } break; } case GameSettings.GAMESTATE.S_Player1Move: { //HandleMouseInput(gameSettings, gameLogic); //HandleKeyInput(gameSettings, gameLogic); break; } case GameSettings.GAMESTATE.S_DropPlayerStone: { if (board.DropStone(gameLogic)) { if (board.Bounce()) gameLogic.GameState = GameSettings.GAMESTATE.S_SwitchPlayer; } break; } case GameSettings.GAMESTATE.S_SwitchPlayer: { // Change player gameLogic.SwitchPlayer(); gameLogic.CheckIfFinished(); if (gameLogic.NextGameState == GameSettings.GAMESTATE.S_GameFinished) { gameLogic.GameState = GameSettings.GAMESTATE.S_GameFinished; } else { board.SwitchStones(gameLogic); if (gameLogic.activePlayer == gameSettings.RED_PLAYER) { // Reset stone position for the next new stone gameLogic.SetNextPlayerStone(); gameLogic.GameState = GameSettings.GAMESTATE.S_Player1Move; } else { gameLogic.GameState = GameSettings.GAMESTATE.S_WaitForCPU; } } break; } case GameSettings.GAMESTATE.S_WaitForCPU: { // Start the AI if (!gameLogic.AIStarted) { gameLogic.CPUMove(); } if (gameLogic.NextGameState == GameSettings.GAMESTATE.S_DropCPUStone) { gameLogic.AbortAI(); board.Update(gameLogic.column * board.ColumnDistance); gameLogic.GameState = GameSettings.GAMESTATE.S_DropCPUStone; } break; } case GameSettings.GAMESTATE.S_DropCPUStone: { if (board.DropStone(gameLogic)) { if (board.Bounce()) gameLogic.GameState = GameSettings.GAMESTATE.S_SwitchPlayer; } break; } case GameSettings.GAMESTATE.S_GameFinished: { IsFinished = true; break; } } }
public static void HandleKeyInput(GameSettings gameSettings, Connect4Game gameLogic, KeyboardState keyboardState, IBoard board) { if(gameLogic.GameState != GameSettings.GAMESTATE.S_Player1Move) return; #region Right key if (!flagRight && !flagDown) { if (keyboardState.IsKeyDown(Keys.Right)) { flagRight = true; if (gameLogic.column < gameSettings.COLUMNS - 1) { // Update gamefield information gameLogic.column++; gameLogic.SetRow(gameLogic.column); } else { // Update gamefield information gameLogic.column = 0; gameLogic.SetRow(gameLogic.column); } board.Update(gameLogic.column * board.ColumnDistance); } } #endregion #region Left key if (!flagLeft && !flagDown) { if (keyboardState.IsKeyDown(Keys.Left)) { flagLeft = true; if (gameLogic.column > 0) { // Update gamefield information gameLogic.column--; gameLogic.SetRow(gameLogic.column); } else { // Update gamefield information gameLogic.column = gameSettings.COLUMNS - 1; gameLogic.SetRow(gameLogic.column); } board.Update(gameLogic.column * board.ColumnDistance); } } #endregion #region Down key if (!flagDown) { if (keyboardState.IsKeyDown(Keys.Down) && gameLogic.row != -1) { flagDown = true; gameLogic.SetRow(gameLogic.column); gameLogic.AddRowElement(gameLogic.column, gameLogic.row); // Update game field gameLogic.UpdateGameField(); // Drop player stone gameLogic.GameState = GameSettings.GAMESTATE.S_DropPlayerStone; } } #endregion // Reset flags if (!keyboardState.IsKeyDown(Keys.Right)) flagRight = false; if (!keyboardState.IsKeyDown(Keys.Left)) flagLeft = false; if (!keyboardState.IsKeyDown(Keys.Down)) flagDown = false; }