private void GetTurn() { if (engine.GetTurn() == Player.WHITE) { pictureBox2.BackColor = Color.White; } else { pictureBox2.BackColor = Color.Red; } }
private void button2_Click(object sender, EventArgs e) { engine = new KaroEngineWrapper(); UpdateGUI(); DateTime startTijd = DateTime.Now; TimeSpan timeDiff = DateTime.Now - DateTime.Now; int white = 0; int red = 0; int draw = 0; bool bdraw = false; int games = int.Parse(textBox2.Text); for (int i = 0; i < games; i++) { newGameToolStripMenuItem_Click(sender, e); int moves = 0; startTijd = DateTime.Now; while (engine.GetGameState() != GameState.GAMEFINISHED) { if (moves >= 180) { bdraw = true; break; } engine.CalculateComputerMove(); moves++; UpdateGUI(); Application.DoEvents(); } textBox2.Text = "" + (int.Parse(textBox2.Text) - 1); if (bdraw) { draw++; } else { if (engine.GetTurn() == Player.WHITE) { red++; } if (engine.GetTurn() == Player.RED) { white++; } } bdraw = false; timeDiff = DateTime.Now - startTijd; ShowWinning(moves, (float)timeDiff.TotalSeconds, bdraw); } this.txtMessageLog.Text = "Draw\t" + draw + "\r\n\r\n" + this.txtMessageLog.Text; this.txtMessageLog.Text = "RED:\t" + red + "\r\n" + this.txtMessageLog.Text; this.txtMessageLog.Text = "WHITE:\t" + white + " \r\n" + this.txtMessageLog.Text; this.txtMessageLog.Text = "Played:\t" + games + "\r\n" + this.txtMessageLog.Text; textBox2.Text = "" + games; }
/// <summary> /// Executes a given move /// </summary> /// <param name="piece">Index of piece</param> /// <param name="tile">Index of tile to</param> /// <param name="tileFrom">Index of tile from</param> private void DoMove(int piece, int tile, int tileFrom) { if (tileFrom >= 0) { if (engine.GetGameState() == KaroEngine.GameState.PLAYING) { Point location = PieceComponents[this.selectedPiece].OnTopofTile.Location; int from = location.X + (location.Y * BOARDWIDTH); Point location2 = TileComponents[this.selectedTile].Location; int fromTile = location2.X + (location2.Y * BOARDWIDTH); Point location3 = this.moveToList[tileFrom].Location; int to = location3.X + (location3.Y * BOARDWIDTH); bool result = engine.DoMove(from, to, fromTile); if (result) { this.ClearSelectedItems(); this.ShowMove(location, location3, location2); //start undo timer startUndoTimer = true; moveUndone = false; } } } if (tile >= 0) //If the move should be on a tile { if (engine.GetGameState() == KaroEngine.GameState.INSERTION) { if (this.selectedStartingPiece >= 0) { Point location2 = TileComponents[tile].Location; if (engine.InsertByXY(location2.X, location2.Y)) { this.ShowMove(location2, location2, location2); startUndoTimer = false; // TODO :: Build in undo move in insertion state (why not just restart the game in this early stage?) moveUndone = false; } } } else if (engine.GetGameState() == KaroEngine.GameState.PLAYING) { Point location2 = TileComponents[tile].Location; int to = location2.X + (location2.Y * BOARDWIDTH); if (engine.GetByXY(location2.X, location2.Y) == KaroEngine.Tile.MOVEABLETILE) { TileComponents[tile].IsSelected = true; this.selectedTile = tile; } if (this.selectedPiece > 0) { Point location = PieceComponents[this.selectedPiece].OnTopofTile.Location; int from = location.X + (location.Y * BOARDWIDTH); this.ClearSelectedItems(); if (engine.DoMove(from, to, -1)) { this.ShowMove(location, location2, new Point()); //start undo timer startUndoTimer = true; moveUndone = false; } } } } if (piece >= 0) { foreach (var entry in TileComponents) { entry.Value.IsPossibleMove = false; } // If the game is still running. if (engine.GetGameState() == KaroEngine.GameState.INSERTION || StartingPieces.Count != 0) { Player player = engine.GetTurn(); Vector3 red = Color.Tomato.ToVector3(); Vector3 white = Color.White.ToVector3(); if (StartingPieces[piece].Color.Equals(white) && player == Player.WHITE || StartingPieces[piece].Color.Equals(red) && player == Player.RED) { if (this.selectedStartingPiece >= 0) { StartingPieces[selectedStartingPiece].IsSelected = false; } StartingPieces[piece].IsSelected = true; this.selectedStartingPiece = piece; } startUndoTimer = false; // TODO :: Build in undo move in insertion state (why not just restart the game in this early stage?) moveUndone = false; } if (engine.GetGameState() == KaroEngine.GameState.PLAYING) { Player player = engine.GetTurn(); Vector3 red = Color.Tomato.ToVector3(); Vector3 white = Color.White.ToVector3(); if (PieceComponents[piece].Color.Equals(white) && player == Player.WHITE || PieceComponents[piece].Color.Equals(red) && player == Player.RED) { this.selectedPiece = piece; PieceComponents[piece].IsSelected = true; Point locTest = PieceComponents[this.selectedPiece].OnTopofTile.Location; if (this.selectedTile > 0) { Point location = PieceComponents[this.selectedPiece].OnTopofTile.Location; Point location2 = TileComponents[this.selectedTile].Location; int[][] possibleMoves = engine.GetPossibleMoves(location.X, location.Y, location2.X, location2.Y); moveToList.Clear(); foreach (int[] item in possibleMoves) { moveToList.Add(new Tile(this, tileModel, true, new Point(item[0], item[1]))); } } else { int[][] possibleMovePiece = engine.GetPossibleMoves(locTest.X, locTest.Y, -1, -1); foreach (int[] item in possibleMovePiece) { foreach (var entry in TileComponents) { if (entry.Value.Location.X == item[0] && entry.Value.Location.Y == item[1]) { entry.Value.IsPossibleMove = true; } } } } } } } }