/* Checks Rule 7: Any piece without liberties should be removed from the board * Does this for board history by check that every point has a liberty for each board */ public static bool CheckPiecesWithoutLiberties(string[][][] boards) { List <List <List <string> > > pointsLists = GetPointsLists(boards); for (int i = 0; i < boards.Length; i++) { BoardWrapper boardObject = new BoardWrapper(boards[i], boards[i].Length); foreach (string b in pointsLists[i][0]) { if (!boardObject.Reachable(b, " ")) { throw new RuleCheckerException("Rule 7 violated in RuleChecker: stones with no liberties must be removed at point " + b); } } foreach (string w in pointsLists[i][1]) { if (!boardObject.Reachable(w, " ")) { throw new RuleCheckerException("Rule 7 violated in RuleChecker: stones with no liberties must be removed at point" + w); } } } return(true); }
/* * Simulates a move being made and updates _board_history, _pass_count, and current_player * If the move is illegal, throws an exception adn updates _victors */ public void Play(string point) { try { RuleCheckerWrapper.Play(_current_player.GetStone(), point, GetBoardHistory()); } catch (RuleCheckerException) { if (_current_player == _player1) { _victors.Add(_player2); } else { _victors.Add(_player1); } throw new RefereeException("Invalid Play in Referee: an illegal move has been made"); } //update _board_history BoardWrapper b = new BoardWrapper(_board_history[0].GetBoard(), _board_history[0].GetSize()); b.PlaceStone(_current_player.GetStone(), point); b.RemoveDeadStones(); _board_history.Insert(0, b); if (_board_history.Count == 4) { _board_history.RemoveAt(3); } _pass_count = 0; _current_player = _current_player == _player1 ? _player2 : _player1; }
void Awake() { boardContainer = GameObject.FindGameObjectWithTag("Board"); boardWrapper = boardContainer.GetComponent <BoardWrapper>(); mainAudio = GameObject.FindGameObjectWithTag("Audio"); mainAudioSource = mainAudio.GetComponent <AudioSource>(); errorObject = GameObject.FindGameObjectWithTag("ErrorText"); //errorText = errorObject.GetComponent<Text>(); }
public BoardWrapper GetBoard() { var board = new BoardWrapper { DisplayBoard = GameBoard.DisplayBoard }; return(board); }
public void Awake() { boardContainer = GameObject.FindGameObjectWithTag("Board"); boardWrapper = boardContainer.GetComponent <BoardWrapper>(); mainAudio = GameObject.FindGameObjectWithTag("Audio"); mainAudioSource = mainAudio.GetComponent <AudioSource>(); rotIcon = Resources.Load("XIcon") as GameObject; rotIconBottom = Resources.Load("ZIcon") as GameObject; }
public void onButtonPressed() { BoardWrapper boardWrapper = GameObject.FindGameObjectWithTag("Board").GetComponent <BoardWrapper>(); Board board = boardWrapper.getBoard(); if (!board.isComplete()) { GameOver(); } else { WinnerIsYou(); } }
/* Helper function that, given an array of boards * Returns a list of points of each stone color for each board * In the form pointsLists[board_num][stone][points] * stone==0 => Black stone, stone==1 => White stone * e.g. pointsLists[0][1][2] => Third white stone in current board */ public static List <List <List <string> > > GetPointsLists(string[][][] boards) { List <List <List <string> > > pointsLists = new List <List <List <string> > >(); //[0][1].Count == current board, white points foreach (string[][] board in boards) { BoardWrapper boardObject = new BoardWrapper(board, board.Length); List <List <string> > points = new List <List <string> >(); points.Add(boardObject.GetPoints("B")); points.Add(boardObject.GetPoints("W")); pointsLists.Add(points); } return(pointsLists); }
/* Checks Rule 5: Game starts with a blank board * Does this by checking that the last board is blank if boards.Length == 1 or 2 */ public static bool CheckStartWithEmptyBoard(string[][][] boards) { if (boards.Length == 1 || boards.Length == 2) { string[][] board = boards[boards.Length - 1]; BoardWrapper boardObject = new BoardWrapper(board, board.Length); if (boardObject.GetPoints("B").Count > 0 || boardObject.GetPoints("W").Count > 0) { throw new RuleCheckerException("Rule 5 violated in RuleChecker: game must start with an empty board"); } } return(true); }
/* Checks Rule 8: Ko - cannot place a stone such that you recreate the board position following one's previous move * Does this by simulating the move, * resolving the board after the move has been made (remove dead stones) * and comparing the new board with board past1 */ public static bool CheckKOForPlay(string stone, string point, string[][][] boards) { List <List <List <string> > > pointsLists = GetPointsLists(boards); BoardWrapper boardObject = new BoardWrapper(boards[0], boards[0].Length); try { boardObject.PlaceStone(stone, point); } catch (BoardException) { throw new RuleCheckerException("Illegal Move in RuleChecker: cannot place stones on occupied points"); } if (boards.Length == 3) { //Remove dead stones if (stone == "B") { foreach (string w in pointsLists[0][1]) { if (!boardObject.Reachable(w, " ")) { boardObject.RemoveStone("W", w); } } } if (stone == "W") { foreach (string b in pointsLists[0][0]) { if (!boardObject.Reachable(b, " ")) { boardObject.RemoveStone("B", b); } } } List <string> bPoints = boardObject.GetPoints("B"); List <string> wPoints = boardObject.GetPoints("W"); if (bPoints.SequenceEqual(pointsLists[1][0]) && wPoints.SequenceEqual(pointsLists[1][1])) { throw new RuleCheckerException("Rule 8 violated in RuleChecker: cannot place a stone such that you recreate the board position following one's previous move"); } } return(true); }
public static JObject Score(string[][] board) { int blackScore = 0; int whiteScore = 0; BoardWrapper boardObject = new BoardWrapper(board, board.Length); for (int i = 0; i < board.Length; i++) { for (int j = 0; j < board.Length; j++) { if (board[i][j] == "B") { blackScore++; } else if (board[i][j] == "W") { whiteScore++; } else { bool bTerritory = boardObject.Reachable((j + 1).ToString() + "-" + (i + 1).ToString(), "B"); bool wTerritory = boardObject.Reachable((j + 1).ToString() + "-" + (i + 1).ToString(), "W"); if (bTerritory && wTerritory) { continue; } if (bTerritory) { blackScore++; } if (wTerritory) { whiteScore++; } } } } JObject jObject = new JObject( new JProperty("B", blackScore), new JProperty("W", whiteScore)); return(jObject); }
/* * Returns a point if there is a valid move given the board history * point is determined by _AIType * Returns "pass" if there are no valid moves * Throws an exception if board history is illegal */ public string MakeAMove(string[][][] boards) { try { RuleChecker.CheckHistory(_stone, boards); } catch (RuleCheckerException) { throw new PlayerException("This history makes no sense!"); } Random rng; switch (_AIType) { case "smart": rng = new Random(); if (rng.NextDouble() > 0.2) { goto case "less dumb"; } else { return("pass"); } case "illegal": rng = new Random(); double n = rng.NextDouble(); if (n > 0.8) { return("pass"); } else if (n > 0.4) { return("[50]"); } else { return("1234"); } case "less dumb": string oppositeStone; if (_stone == "B") { oppositeStone = "W"; } else { oppositeStone = "B"; } BoardWrapper boardObject = new BoardWrapper(boards[0], boards[0].Length); List <string> pointsList = boardObject.GetPoints(oppositeStone); List <string> iterate; List <List <string> > possibleMoves = new List <List <string> >(); //Find all oppositeStones with only one adjacent liberty //Add {point, adjacentLiberty} to a possibleMoves foreach (string point in pointsList) { iterate = boardObject.GetAdjacentLiberties(point); iterate.Insert(0, point); if (iterate.Count == 2) { possibleMoves.Add(iterate); } } List <List <string> > temp = new List <List <string> >(); //Check the validity of each possibleMove //Also check that making that possibleMove will result in a capture //Failing these conditions, remove move from possibleMoves foreach (List <string> move in possibleMoves) { try { RuleChecker.CheckMove(_stone, move[1], boards); } catch (Exception e) { if (!(e is RuleCheckerException) && !(e is BoardException)) { throw; } continue; } boardObject = new BoardWrapper(boards[0], boards[0].Length); boardObject.PlaceStone(_stone, move[1]); if (!boardObject.Reachable(move[0], " ")) { temp.Add(move); } } possibleMoves = temp; //sort in lowest column lowest row order (numeric) possibleMoves.Sort(ComparePossibleMoves); //for n == 1, return the first possible Move (if it exists) if (_n == 1) { if (possibleMoves.Count > 0) { return(possibleMoves[0][1]); } } //Otherwise, n > 1, and if there is only one possibleMove, return it if (possibleMoves.Count == 1) { return(possibleMoves[0][1]); } //Otherwise, play dumb goto case "dumb"; case "dumb": for (int i = 0; i < boards[0].Length; i++) { for (int j = 0; j < boards[0].Length; j++) { try { string point = (i + 1).ToString() + "-" + (j + 1).ToString(); RuleChecker.CheckMove(_stone, point, boards); return(point); } catch (Exception e) { if (!(e is RuleCheckerException) && !(e is BoardException)) { throw; } } } } break; } return("pass"); }
/* Checks Rule 7a: A play is illegal if the placed stone must be immediately removed * Does this by simulating the move and checking if the placed stone has any liberties * and if the placed stone will capture any pieces (giving it liberties) */ public static bool CheckSuicide(string stone, string point, string[][][] boards) { string oppositeStone; if (stone == "B") { oppositeStone = "W"; } else { oppositeStone = "B"; } BoardWrapper boardObject = new BoardWrapper(boards[0], boards[0].Length); try { boardObject.PlaceStone(stone, point); } catch (BoardException) { throw new RuleCheckerException("Illegal Move in RuleChecker: cannot place stones on occupied points"); } if (!boardObject.Reachable(point, " ")) { bool willCapture = false; int[] p = ParsingHelper.ParsePoint(point); /* * If the point has no liberties, check if there are any adjacent oppositeStones * if there are, check if they have any liberties * if they all do, move is illegal */ string eastPoint = (p[0] + 1).ToString() + "-" + (p[1] + 2).ToString(); if (p[1] != boardObject.GetSize() - 1 && boardObject.Occupies(oppositeStone, eastPoint) && boardObject.Reachable(eastPoint, " ") != true) { willCapture = true; } string southPoint = (p[0] + 2).ToString() + "-" + (p[1] + 1).ToString(); if (p[0] != boardObject.GetSize() - 1 && boardObject.Occupies(oppositeStone, southPoint) && boardObject.Reachable(southPoint, " ") != true) { willCapture = true; } string westPoint = (p[0] + 1).ToString() + "-" + (p[1]).ToString(); if (p[1] != 0 && boardObject.Occupies(oppositeStone, westPoint) && boardObject.Reachable(westPoint, " ") != true) { willCapture = true; } string northPoint = (p[0]).ToString() + "-" + (p[1] + 1).ToString(); if (p[0] != 0 && boardObject.Occupies(oppositeStone, northPoint) && boardObject.Reachable(northPoint, " ") != true) { willCapture = true; } if (!willCapture) { throw new RuleCheckerException("Rule 7a violated in RuleChecker: self sacrifice is not allowed"); } } return(true); }
/* Goes through the board history and checks * Rule 6b: Players alternate playing (checks this by counting stones) * Rule 6.7: Each move is either placing a single stone or passing * Rule 6x: ONLY stones without liberties are removed * Rule 7: Any stone without liberties should be removed from the board * (basically, checks that points on the board are resolved correctly) */ public static bool CheckStoneChanges(string stone, string[][][] boards) { List <List <List <string> > > pointsLists = GetPointsLists(boards); for (int i = 1; i < boards.Length; i++) { int currentPlayer, nextPlayer; //think in terms of board[i] if (stone == "B" && i == 1 || stone == "W" && i == 2) { currentPlayer = 1; nextPlayer = 0; } else { currentPlayer = 0; nextPlayer = 1; } //Check that 0 new stones are added by nextPlayer List <string> newStones = new List <string>(pointsLists[i - 1][nextPlayer]); foreach (string s in pointsLists[i][nextPlayer]) { newStones.Remove(s); } if (newStones.Count != 0) { throw new RuleCheckerException("Rule 6b violated in RuleChecker: new stones were added, players must play in succesion"); } //Check that only 0 or 1 new stones are added by currentPlayer newStones = new List <string>(pointsLists[i - 1][currentPlayer]); foreach (string s in pointsLists[i][currentPlayer]) { newStones.Remove(s); } if (newStones.Count != 0 && newStones.Count != 1) { throw new RuleCheckerException("Rule 6.7 violated in RuleChecker: only one stone can be placed per turn (or 0 for pass)"); } //Check that 0 stones are removed by currentPlayer List <string> removedStones = new List <string>(pointsLists[i][currentPlayer]); foreach (string s in pointsLists[i - 1][currentPlayer]) { removedStones.Remove(s); } if (removedStones.Count != 0) { throw new RuleCheckerException("Rule 6x violated in RuleChecker: stones of currentPlayer should not be removed"); } //Check that removed stones of nextPlayer have no liberties (after placing newStones) //Or check that no stones were removed if currentPlayer passed if (newStones.Count == 1) { removedStones = new List <string>(pointsLists[i][nextPlayer]); foreach (string s in pointsLists[i - 1][nextPlayer]) { removedStones.Remove(s); } BoardWrapper hBoard = new BoardWrapper(boards[i], boards[i].Length); try { if (currentPlayer == 1) { hBoard.PlaceStone("W", newStones[0]); } else { hBoard.PlaceStone("B", newStones[0]); } } catch { throw new RuleCheckerException("Illegal board history in RuleChecker: cannot place stones on occupied points"); } foreach (string s in removedStones) { if (hBoard.Reachable(s, " ")) { throw new RuleCheckerException("Rule 6x violated in RuleChecker: stones of nextPlayer with liberties should not be removed"); } } //Additionally, check that all stones of nextPlayer that should've been removed are removed foreach (string s in pointsLists[i][nextPlayer]) { if (!hBoard.Reachable(s, " ")) { if (!removedStones.Contains(s)) { throw new RuleCheckerException("Rule 6x violated in RuleChecker: stones of nextPlayer without liberties should be removed"); } } } } else { if (!pointsLists[i][nextPlayer].SequenceEqual(pointsLists[i - 1][nextPlayer])) { throw new RuleCheckerException("Rule 7 violated in RuleChecker: stones of nextPlayer with liberties should not be removed"); } } } return(true); }