public void Print(Gameboard gameboard) { Console.WriteLine("############## CURRENT GAMEBOARD ##############"); for (int row = 0; row < 8; ++row) { for (int column = 0; column < 8; ++column) { Console.Write("[{0},{1}] ", gameboard.getPiece(row, column).team, gameboard.getPiece(row, column).type); } Console.WriteLine(""); } Console.WriteLine("###############################################"); }
private void cleanUp() { Gameboard copyOfGameboard = new Gameboard(gameboard); Rulebook rulebook = new Rulebook(); List<Tuple<int, int>> copyOfValids = new List<Tuple<int, int>>(); bool foundIllegalMove = false; Piece originalCurrentPiece; int currentTeam = copyOfCurrentPiece.team; int opponentTeam = info.getOpponent(currentTeam); foreach (Tuple<int, int> validMove in validDestinations) // För varje möjligt drag för markerad Piece { copyOfGameboard = new Gameboard(gameboard); originalCurrentPiece = copyOfGameboard.getPiece(copyOfCurrentPiece.row, copyOfCurrentPiece.column); Piece destination = copyOfGameboard.getPiece(validMove.Item1, validMove.Item2); copyOfGameboard.Move(originalCurrentPiece, destination); // Gör temporärt move foundIllegalMove = false; foreach (Piece opponent in copyOfGameboard.getTeam(opponentTeam)) // För varje Piece i team OPPONENT { List<Tuple<int,int>> validDest = rulebook.getValidMoves(opponent, copyOfGameboard, false); // Hämta valid moves för Piece i OPPONENT (Utan hänsyn till möjliga drag som sätter kungen i schack..) foreach (Tuple<int,int> coordinate in validDest) // För varje möjligt drag för Piece i OPPONENT { if (copyOfGameboard.getPiece(coordinate.Item1, coordinate.Item2).type == (int)type.king) // Om Piece i OPPONENT kan ta kungen i CURRENT { foundIllegalMove = true; break; } if (foundIllegalMove) break; } if (foundIllegalMove) break; } if (!foundIllegalMove) copyOfValids.Add(new Tuple<int, int>(validMove.Item1, validMove.Item2)); copyOfGameboard.setPiece(originalCurrentPiece); copyOfGameboard.setPiece(destination); } validDestinations = copyOfValids; }
public List<Tuple<int, int>> Rules(Piece current_piece, Gameboard gameboard) { this.gameboard = gameboard; this.current_piece = current_piece; if (current_piece.column > min && current_piece.row > min + 1) { next_piece = gameboard.getPiece(current_piece.row - 2, current_piece.column - 1); if (next_piece.empty == true || next_piece.team != current_piece.team) valid_destinations.Add(new Tuple<int, int>(current_piece.row - 2, current_piece.column - 1)); // up left } if (current_piece.column < max && current_piece.row > min + 1) { Piece next_piece = gameboard.getPiece(current_piece.row - 2, current_piece.column + 1); if (next_piece.empty == true || next_piece.team != current_piece.team) valid_destinations.Add(new Tuple<int, int>(current_piece.row -2, current_piece.column + 1)); // up right } if (current_piece.column < max - 1 && current_piece.row > min) { Piece next_piece = gameboard.getPiece(current_piece.row - 1, current_piece.column + 2); if (next_piece.empty == true || next_piece.team != current_piece.team) valid_destinations.Add(new Tuple<int, int>(current_piece.row - 1, current_piece.column + 2)); // right up } if (current_piece.column < max - 1 && current_piece.row < max) { Piece next_piece = gameboard.getPiece(current_piece.row + 1, current_piece.column + 2); if (next_piece.empty == true || next_piece.team != current_piece.team) valid_destinations.Add(new Tuple<int, int>(current_piece.row + 1, current_piece.column + 2)); // right down } if (current_piece.column < max && current_piece.row < max - 1) { Piece next_piece = gameboard.getPiece(current_piece.row + 2, current_piece.column + 1); if (next_piece.empty == true || next_piece.team != current_piece.team) valid_destinations.Add(new Tuple<int, int>(current_piece.row + 2, current_piece.column + 1)); // down right } if (current_piece.column > min && current_piece.row < max - 1) { Piece next_piece = gameboard.getPiece(current_piece.row + 2, current_piece.column - 1); if (next_piece.empty == true || next_piece.team != current_piece.team) valid_destinations.Add(new Tuple<int, int>(current_piece.row + 2, current_piece.column - 1)); // down left } if (current_piece.column > min + 1 && current_piece.row < max) { Piece next_piece = gameboard.getPiece(current_piece.row + 1, current_piece.column - 2); if (next_piece.empty == true || next_piece.team != current_piece.team) valid_destinations.Add(new Tuple<int, int>(current_piece.row + 1, current_piece.column - 2)); // left down } if (current_piece.column > min + 1 && current_piece.row > min) { Piece next_piece = gameboard.getPiece(current_piece.row - 1, current_piece.column - 2); if (next_piece.empty == true || next_piece.team != current_piece.team) valid_destinations.Add(new Tuple<int, int>(current_piece.row - 1, current_piece.column - 2)); // left up } return valid_destinations; }
public List<Tuple<int, int>> Rules(Piece piece, Gameboard gameboard) { if (piece.row == 0 || piece.row == 7) return new List<Tuple<int, int>>(); int max_row; int moving_factor; int step = 1; List<Tuple<int, int>> valid_destinations = new List<Tuple<int, int>>(); if (piece.team == (int)team.black) { max_row = 7; moving_factor = 1; if (piece.row == 1) ++step; } else { max_row = 0; moving_factor = -1; if (piece.row == 6) ++step; } for (int i = 1; i < step + 1; i++) { if (gameboard.getPiece(piece.row + (i * moving_factor), piece.column).empty && piece.row != max_row) { valid_destinations.Add(new Tuple<int, int>(piece.row + (i * moving_factor), piece.column)); } if (i == 1 && piece.column != 0 && gameboard.getPiece(piece.row + (i * moving_factor), piece.column - 1).team != piece.team && !gameboard.getPiece(piece.row + (i * moving_factor), piece.column - 1).empty) { valid_destinations.Add(new Tuple<int, int>(piece.row + (i * moving_factor), piece.column-1)); } if (i == 1 && piece.column != 7 && gameboard.getPiece(piece.row + (i * moving_factor), piece.column + 1).team != piece.team && !gameboard.getPiece(piece.row + (i * moving_factor), piece.column + 1).empty) { valid_destinations.Add(new Tuple<int, int>(piece.row + (i * moving_factor), piece.column+1)); } } return valid_destinations; }
public void Move(Gameboard gameboard, State currentState) { Rulebook rulebook = new Rulebook(); List<Tuple<Piece, Piece>> topValidMoves = new List<Tuple<Piece, Piece>>(); foreach(Piece piece in gameboard.getTeam(thisTeam)) { List<Tuple<int, int>> validDestinations = rulebook.getValidMoves(piece, gameboard); foreach (Tuple<int,int> coordinate in validDestinations) { if (validDestinations.Count() == 0) break; else if (topValidMoves.Count() == 0 || rewards[topValidMoves[0].Item2.type] == rewards[gameboard.getPiece(coordinate.Item1, coordinate.Item2).type]) topValidMoves.Add(new Tuple<Piece, Piece>(piece, gameboard.getPiece(coordinate.Item1, coordinate.Item2))); else if (rewards[topValidMoves[0].Item2.type] < rewards[gameboard.getPiece(coordinate.Item1, coordinate.Item2).type]) { topValidMoves.Clear(); topValidMoves.Add(new Tuple<Piece, Piece>(piece, gameboard.getPiece(coordinate.Item1, coordinate.Item2))); } } } if (topValidMoves.Count() != 0) { Random rand = new Random(); int index = rand.Next(topValidMoves.Count()); gameboard.Move(topValidMoves[index].Item1, topValidMoves[index].Item2); if (topValidMoves[index].Item1.type == (int)type.pawn && (topValidMoves[index].Item1.row == 0 || topValidMoves[index].Item1.row == 7)) { Piece bestPieceFromDead = new Piece(); foreach (Piece deadPiece in gameboard.getDead(thisTeam)) { if (rewards[deadPiece.type] > rewards[bestPieceFromDead.type]) bestPieceFromDead = deadPiece; } gameboard.tradePawn(topValidMoves[index].Item1, bestPieceFromDead); } //gameboard.checkChessMate(currentState.getWhosTurn()); } //currentState.swapTurn(); }