public void MakeMove(Move move) { GamePiece start = move.MoveFrom; GamePiece end = move.MoveTo; int i = 40 + (48 * start.Row); int j = start.Col + 1; j = j * 48; int k = 40 + (48 * end.Row); int l = end.Col + 1; l = l * 48; Point check = new Point(j, i); if (Green1.Location == check) { Green1.Location = new Point(l, k); } else if (Green2.Location == check) { Green2.Location = new Point(l, k); } else if (Green3.Location == check) { Green3.Location = new Point(l, k); } else if (Green4.Location == check) { Green4.Location = new Point(l, k); } else if (Green5.Location == check) { Green5.Location = new Point(l, k); } else if (Green6.Location == check) { Green6.Location = new Point(l, k); } else if (Green7.Location == check) { Green7.Location = new Point(l, k); } else if (Red1.Location == check) { Red1.Location = new Point(l, k); } else if (Red2.Location == check) { Red2.Location = new Point(l, k); } else if (Red3.Location == check) { Red3.Location = new Point(l, k); } else if (Red4.Location == check) { Red4.Location = new Point(l, k); } else if (Red5.Location == check) { Red5.Location = new Point(l, k); } else if (Red6.Location == check) { Red6.Location = new Point(l, k); } else if (Red7.Location == check) { Red7.Location = new Point(l, k); } }
public void AddMove(Move Move) { this.Moves.AddLast(Move); }
private void button2_Click(object sender, EventArgs e) { //48, 40 are the starting coordinates of a1. //add 48 to each in order to move to the next square. //MessageBox.Show(Move_enter.Text); string move = Move_enter.Text; if (move.Length >= 4 && move.Length <= 5) { GamePiece start = new GamePiece(); start.Row = (move[0] - 'a'); start.Col = (int)Char.GetNumericValue(move[1]) - 1; GamePiece end = new GamePiece(); end.Row = move[2] - 'a'; if (move.Length == 5) { if (move[3] == '1' && move[4] == '0') { end.Col = 9; } } else { end.Col = (int)Char.GetNumericValue(move[3]) - 1; } Move playerMove = new Move(start, end); if (logic.MakePlayerMove(playerMove)) { MakeMove(playerMove); Move logicMove = logic.GetNextMove(); logic.MakeComputerMove(logicMove); MakeMove(logicMove); } else { MessageBox.Show("Invalid Move"); } } else if (move.Length == 6) { GamePiece start = new GamePiece(); start.Row = (move[0] - 'a'); start.Col = 9; GamePiece end = new GamePiece(); end.Row = move[3] - 'a'; end.Col = 9; Move playerMove = new Move(start, end); if (logic.MakePlayerMove(playerMove)) { MakeMove(playerMove); Move logicMove = logic.GetNextMove(); logic.MakeComputerMove(logicMove); MakeMove(logicMove); } else { MessageBox.Show("Invalid Move"); } } Move_enter.Text = ""; }
private bool IsValidPlayerMove(Move Move) { GamePiece MoveFrom = Move.MoveFrom; GamePiece MoveTo = Move.MoveTo; if (MoveFrom.Row < GameBoard.GetLength(0) && MoveTo.Row < GameBoard.GetLength(0) && MoveFrom.Col < GameBoard.GetLength(1) && MoveTo.Col < GameBoard.GetLength(1)) { BoardPiece MoveFromBoardPiece = GameBoard[MoveFrom.Row, MoveFrom.Col]; BoardPiece MoveToBoardPiece = GameBoard[MoveTo.Row, MoveTo.Col]; if (MoveFromBoardPiece == this.Player.BoardPiece && MoveToBoardPiece == BoardPiece.Open) { if (this.Player.BoardPiece == BoardPiece.Red) { if (MoveTo.Col >= MoveFrom.Col && (MoveTo.Row >= 1 && MoveTo.Row <= 8)) { if (MoveTo.Col == MoveFrom.Col + 1 && MoveTo.Row == MoveFrom.Row) { return true; } else if (MoveTo.Col == MoveFrom.Col) { if (MoveTo.Row == MoveFrom.Row - 1 || MoveTo.Row == MoveFrom.Row + 1) { return true; } } } } else { if (MoveTo.Row >= MoveFrom.Row && (MoveTo.Col >= 1 && MoveTo.Col <= 8)) { if (MoveTo.Row == MoveFrom.Row + 1 && MoveTo.Col == MoveFrom.Col) { return true; } else if (MoveTo.Row == MoveFrom.Row) { if (MoveTo.Col == MoveFrom.Col - 1 || MoveTo.Col == MoveFrom.Col + 1) { return true; } } } } } } return false; }
private int Eval(BoardPiece[,] gameBoard, Move move) { int totalPoints = 0; GamePiece from = move.MoveFrom; GamePiece to = move.MoveTo; BoardPiece MoveFromBoardPiece = GameBoard[from.Row, from.Col]; if (MoveFromBoardPiece == BoardPiece.Green) { // Forward totalPoints += 2 * (to.Row - from.Row) + to.Row % GameBoard.GetLength(0); // Sideways if (from.Col < to.Col && from.Col != to.Col) { if (to.Row < 9 && GameBoard[to.Row + 1, to.Col] == BoardPiece.Open) { totalPoints += 1 * (to.Col - from.Col); } } else if (from.Col != to.Col) { if (to.Row < 9 && GameBoard[to.Row + 1, to.Col] == BoardPiece.Open) { totalPoints += 1 * (from.Col - to.Col); } } // Moving out of a blocking position if (GameBoard[from.Row, from.Col - 1] == BoardPiece.Red) { totalPoints--; } // Moving into a blocking position if (GameBoard[to.Row, to.Col - 1] == BoardPiece.Red) { totalPoints++; } } else { // Forward totalPoints += 2 * (to.Col - from.Col) + to.Col % GameBoard.GetLength(1); // Sideways if (from.Row < to.Row && from.Row != to.Row) { if (to.Col < 9 && GameBoard[to.Row, to.Col + 1] == BoardPiece.Open) { totalPoints += 1 * (to.Row - from.Row); } } else if (from.Row != to.Row) { if (to.Col < 9 && GameBoard[to.Row, to.Col + 1] == BoardPiece.Open) { totalPoints += 1 * (from.Row - to.Row); } } if (GameBoard[from.Row - 1, from.Col] == BoardPiece.Green) { totalPoints--; } if (GameBoard[to.Row - 1, to.Col] == BoardPiece.Green) { totalPoints++; } } return totalPoints; }
public bool MakePlayerMove(Move Move) { if (this.Player.IsTurn && this.IsValidPlayerMove(Move)) { GamePiece MoveFrom = Move.MoveFrom; GamePiece MoveTo = Move.MoveTo; GameBoard[MoveFrom.Row, MoveFrom.Col] = BoardPiece.Open; GameBoard[MoveTo.Row, MoveTo.Col] = this.Player.BoardPiece; this.Player.AddMove(Move); this.Player.IsTurn = false; this.Computer.IsTurn = true; return true; } return false; }
public Move GetNextMove(bool asComputer) { Move bestMove = null; int evaluation = 0; int pos = 0; for (int i = 0; i < computerGamePieces.Length; i++) { GamePiece computerGamePiece = computerGamePieces[i]; if (Computer.BoardPiece == BoardPiece.Green) { if (computerGamePiece.Row < 9) { Move moveDown = new Move(computerGamePiece, new GamePiece(computerGamePiece.Row + 1, computerGamePiece.Col)); if (IsValidComputerMove(moveDown)) { int tempEval = Eval(GameBoard, moveDown); if (tempEval >= evaluation) { bestMove = moveDown; evaluation = tempEval; pos = i; } } Move moveLeft = new Move(computerGamePiece, new GamePiece(computerGamePiece.Row, computerGamePiece.Col - 1)); if (IsValidComputerMove(moveLeft)) { int tempEval = Eval(GameBoard, moveLeft); if (tempEval >= evaluation) { bestMove = moveLeft; evaluation = tempEval; pos = i; } } Move moveRight = new Move(computerGamePiece, new GamePiece(computerGamePiece.Row, computerGamePiece.Col + 1)); if (IsValidComputerMove(moveRight)) { int tempEval = Eval(GameBoard, moveRight); if (tempEval >= evaluation) { bestMove = moveRight; evaluation = tempEval; pos = i; } } } } else { if (computerGamePiece.Col < 9) { Move moveRight = new Move(computerGamePiece, new GamePiece(computerGamePiece.Row, computerGamePiece.Col + 1)); if (IsValidComputerMove(moveRight)) { int tempEval = Eval(GameBoard, moveRight); if (tempEval >= evaluation) { bestMove = moveRight; evaluation = tempEval; pos = i; } } Move moveUp = new Move(computerGamePiece, new GamePiece(computerGamePiece.Row - 1, computerGamePiece.Col)); if (IsValidComputerMove(moveUp)) { int tempEval = Eval(GameBoard, moveUp); if (tempEval >= evaluation) { bestMove = moveUp; evaluation = tempEval; pos = i; } } Move moveDown = new Move(computerGamePiece, new GamePiece(computerGamePiece.Row + 1, computerGamePiece.Col)); if (IsValidComputerMove(moveDown)) { int tempEval = Eval(GameBoard, moveDown); if (tempEval >= evaluation) { bestMove = moveDown; evaluation = tempEval; pos = i; } } } } } if (bestMove == null) { for (int i = 0; i < computerGamePieces.Length; i++) { GamePiece computerGamePiece = computerGamePieces[i]; if (Computer.BoardPiece == BoardPiece.Green) { if (computerGamePiece.Row == 9) { Move moveLeft = new Move(computerGamePiece, new GamePiece(computerGamePiece.Row, computerGamePiece.Col - 1)); if (IsValidComputerMove(moveLeft)) { int tempEval = Eval(GameBoard, moveLeft); if (tempEval >= evaluation) { bestMove = moveLeft; evaluation = tempEval; pos = i; } } Move moveRight = new Move(computerGamePiece, new GamePiece(computerGamePiece.Row, computerGamePiece.Col + 1)); if (IsValidComputerMove(moveRight)) { int tempEval = Eval(GameBoard, moveRight); if (tempEval >= evaluation) { bestMove = moveRight; evaluation = tempEval; pos = i; } } } } else { if (computerGamePiece.Col == 9) { Move moveUp = new Move(computerGamePiece, new GamePiece(computerGamePiece.Row - 1, computerGamePiece.Col)); if (IsValidComputerMove(moveUp)) { int tempEval = Eval(GameBoard, moveUp); if (tempEval >= evaluation) { bestMove = moveUp; evaluation = tempEval; pos = i; } } Move moveDown = new Move(computerGamePiece, new GamePiece(computerGamePiece.Row + 1, computerGamePiece.Col)); if (IsValidComputerMove(moveDown)) { int tempEval = Eval(GameBoard, moveDown); if (tempEval >= evaluation) { bestMove = moveDown; evaluation = tempEval; pos = i; } } } } } } if (Computer.BoardPiece == BoardPiece.Green) { computerGamePieces[pos] = bestMove.MoveTo; } else { computerGamePieces[pos] = bestMove.MoveTo; } return bestMove; }