public Piece[,] createBoardFromFile() { var info = new FileInfo("game.xml"); var tempTime = info.LastWriteTime; if (DateTime.Compare(tempTime, lastWriteTime) > 0) { Piece[,] board = new Piece[8, 8]; XDocument prevGame = XDocument.Load("game.xml"); var lv0s = from lv0 in prevGame.Descendants("settings") select new { Turn = lv0.Attribute("turn").Value, White = lv0.Attribute("white").Value, Black = lv0.Attribute("black").Value, Whiteischeck = lv0.Attribute("whiteischeck").Value, Blackischeck = lv0.Attribute("blackischeck").Value }; foreach (var lv0 in lv0s) { this.whiteIsCheck = Convert.ToBoolean(lv0.Whiteischeck); this.blackIsCheck = Convert.ToBoolean(lv0.Blackischeck); this.white = lv0.White; this.black = lv0.Black; this.turn = (Piece.PieceColor)Enum.Parse(typeof(Piece.PieceColor), lv0.Turn); } var lv1s = from lv1 in prevGame.Descendants("square") select new { Type = lv1.Attribute("type").Value, Color = lv1.Attribute("color").Value, Coordx = lv1.Attribute("coordx").Value, Coordy = lv1.Attribute("coordy").Value, Texture = lv1.Attribute("texture").Value }; int count = 0; foreach (var lv1 in lv1s) { count += 1; Piece.PieceType type = (Piece.PieceType)Enum.Parse(typeof(Piece.PieceType), lv1.Type); Piece.PieceColor color = (Piece.PieceColor)Enum.Parse(typeof(Piece.PieceColor), lv1.Color); int x = Int32.Parse(lv1.Coordx); int y = Int32.Parse(lv1.Coordy); int txt = Int32.Parse(lv1.Texture); Piece temp = new Piece(type, x, y, color, txt);; board [x, y] = temp; } lastWriteTime = info.LastWriteTime; return(board); } else { return(this.board); } }
public static bool isCheckMate(Board board, Piece.PieceColor color) { bool stillCheck = true; List <Coord> currPieceLegalMoves = new List <Coord> (); Coord kingCoord = new Coord(); for (int y = 0; y < board.board.GetLength(0); y++) { for (int x = 0; x < board.board.GetLength(0); x++) { if (board.board [x, y].color == color && stillCheck) { currPieceLegalMoves = getLegalMoves(board.board, board.board [x, y].coord); foreach (Coord c in currPieceLegalMoves) { Board tempBoard = new Board(board); Piece temp = tempBoard.board [x, y]; temp.coord = c; if (tempBoard.board [c.xpos, c.ypos].type != Piece.PieceType.NONE) { tempBoard.board [c.xpos, c.ypos] = new Piece(Piece.PieceType.NONE, c.xpos, c.ypos, Piece.PieceColor.NONE, 99); } tempBoard.board [x, y] = tempBoard.board [c.xpos, c.ypos]; tempBoard.board [c.xpos, c.ypos] = temp; foreach (Piece p in tempBoard.board) { if (p.type == Piece.PieceType.KING && p.color == color) { kingCoord = new Coord(p.coord.xpos, p.coord.ypos); } } if (color == Piece.PieceColor.WHITE) { stillCheck = isCheck(tempBoard.board, Piece.PieceColor.BLACK, kingCoord); if (stillCheck == false) { ; return(false); } } else { stillCheck = isCheck(tempBoard.board, Piece.PieceColor.WHITE, kingCoord); if (stillCheck == false) { return(false); } } } } } } return(true); }
public Player(PlayerType type, Board board, Piece.PieceColor color) { this.type = type; this.board = board; if (this.type == PlayerType.AI) { ai = new AI(color); } }
// Copy constructor used to avoid reference problems in simulations public Board(Board b) { board = Board.newBoard(); for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { board [x, y] = new Piece(b.board [x, y]); } } turn = b.turn; whiteIsCheck = b.whiteIsCheck; blackIsCheck = b.blackIsCheck; }
public bool CheckWinCondition(Piece[,] board, Piece.PieceColor color) { // Nested for loop to itterate over every possible peice position for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { // Check to see if there are no more pieces of specific color if (board[x, y].color == color) { // If no more pieces of one team are present return false return(false); } } } // Otherwise there are still pieces of one team remaining return(true); }
public static bool isCheck(Piece[,] board, Piece.PieceColor color, Coord kingCoord) { List <Coord> possibleMoves = new List <Coord> (); foreach (Piece p in board) { if (p.color == color) { possibleMoves = getLegalMoves(board, p.coord); foreach (Coord c in possibleMoves) { if (c.xpos == kingCoord.xpos && c.ypos == kingCoord.ypos) { return(true); } } } } return(false); }
bool TrySpawnPiece(int x, int y, Piece.PieceColor color) { if (isGameOver) { return(false); } //Debug.Log("tryspawn " + x + " " + y); if (board[x][y] != null) { return(false); } GameObject newGamePiece = Instantiate(piecePrefab); GameObject gridButton = grid[x][y].gameObject; newGamePiece.transform.SetParent(gridButton.transform); RectTransform rt = newGamePiece.GetComponent <RectTransform>(); rt.offsetMax = Vector2.zero; rt.offsetMin = Vector2.zero; Piece piece = newGamePiece.GetComponent <Piece>(); piece.Init(color); board[x][y] = piece; //check if the new spawn filled in a connection CheckBoard(x, y, color); //check if board is full if (IsBoardFull()) { GameOver(); return(false); } return(true); }
public AI(Piece.PieceColor c) { color = c; }
public void startNewGame() { turn = Piece.PieceColor.WHITE; board = newBoard(); saveBoardToFile(); }
// Ask the rules if a move between two positions is legal, calls swap() if ok public void tryMove(Coord fromPos, Coord toPos) { Coord kingCoord = new Coord(); if (Rules.isLegalMove(board, fromPos, toPos)) { board[fromPos.xpos, fromPos.ypos].moved = true; Board tempBoard = new Board(this); Piece temp = tempBoard.board [fromPos.xpos, fromPos.ypos]; temp.coord = toPos; if (tempBoard.board [toPos.xpos, toPos.ypos].type != Piece.PieceType.NONE) { tempBoard.board [toPos.xpos, toPos.ypos] = new Piece(Piece.PieceType.NONE, toPos.xpos, toPos.ypos, Piece.PieceColor.NONE, 99); } tempBoard.board [fromPos.xpos, fromPos.ypos] = tempBoard.board [toPos.xpos, toPos.ypos]; tempBoard.board [toPos.xpos, toPos.ypos] = temp; foreach (Piece p in tempBoard.board) { if (p.type == Piece.PieceType.KING && p.color == turn) { kingCoord = new Coord(p.coord.xpos, p.coord.ypos); } } Piece.PieceColor colorToCheck; if (turn == Piece.PieceColor.WHITE) { colorToCheck = Piece.PieceColor.BLACK; } else { colorToCheck = Piece.PieceColor.WHITE; } if (!(Rules.isCheck(tempBoard.board, colorToCheck, kingCoord))) { swap(fromPos, toPos); //Investigate if move caused a Check. foreach (Piece p in board) { if (p.type == Piece.PieceType.KING && p.color != turn) { kingCoord = new Coord(p.coord.xpos, p.coord.ypos); } } if (Rules.isCheck(board, turn, kingCoord)) { if (turn == Piece.PieceColor.BLACK) { Console.WriteLine("White is in check"); whiteIsCheck = true; } else { Console.WriteLine("Black is in check"); blackIsCheck = true; } } else { whiteIsCheck = false; blackIsCheck = false; } if (turn == Piece.PieceColor.WHITE) { turn = Piece.PieceColor.BLACK; player2.nextMove(); } else { turn = Piece.PieceColor.WHITE; player1.nextMove(); } } else { Console.WriteLine("Cant move because king will be checked"); } if (whiteIsCheck) { Console.WriteLine("check if white is checkmate"); if (Rules.isCheckMate(this, Piece.PieceColor.WHITE)) { Console.WriteLine("GAME IS OVER, BLACK WON"); } } if (blackIsCheck) { Console.WriteLine("check if black is checkmate"); if (Rules.isCheckMate(this, Piece.PieceColor.BLACK)) { Console.WriteLine("GAME IS OVER, WHITE WON"); } } saveBoardToFile(); } }
//check to the right, down right, and down 3 directions for matches of the same color //remove the pieces while checking here, and count the score based on the pieces removed times a incremental multiplier //do right, down right, then down, if a match occurs during any of the 3, //remove all the pieces in that match except the starting position and add to count //finally at the end, if necessary, remove the origin piece and add to count //at very end, based on count, add to score, and return true/false based on the count. bool CheckBoard(int x, int y, Piece.PieceColor color) { bool connectionMade = false; int connectedPieces = 0; List <Pair> connectionPieces = new List <Pair>(); ////////////////////////////////////////////////////////// /// Check Vertical connection ////////////////////////////////////////////////////////// //temp variables int tracker = 0; List <Pair> currentConnectedPieces = new List <Pair>(); int negIndex = -1; //up and left int posIndex = 1; //down and right while (x + negIndex >= 0 && board[x + negIndex][y] != null && board[x + negIndex][y].color == color) { currentConnectedPieces.Add(new Pair(x + negIndex, y)); tracker++; negIndex--; } while (x + posIndex < board.Count && board[x + posIndex][y] != null && board[x + posIndex][y].color == color) { currentConnectedPieces.Add(new Pair(x + posIndex, y)); tracker++; posIndex++; } //connection made (including origin piece) all pieces accounted for to both sides if (tracker >= 4) { connectionMade = true; connectedPieces += tracker; connectionPieces.AddRange(currentConnectedPieces); } ////////////////////////////////////////////////////////// /// Check Horizontal connection ////////////////////////////////////////////////////////// //reset temp variables tracker = 0; currentConnectedPieces = new List <Pair>(); negIndex = -1; //up and left posIndex = 1; //down and right while (y + negIndex >= 0 && board[x][y + negIndex] != null && board[x][y + negIndex].color == color) { currentConnectedPieces.Add(new Pair(x, y + negIndex)); tracker++; negIndex--; } while (y + posIndex < board[x].Count && board[x][y + posIndex] != null && board[x][y + posIndex].color == color) { currentConnectedPieces.Add(new Pair(x, y + posIndex)); tracker++; posIndex++; } //connection made (including origin piece) all pieces accounted for to both sides if (tracker >= 4) { connectionMade = true; connectedPieces += tracker; connectionPieces.AddRange(currentConnectedPieces); } ////////////////////////////////////////////////////////// /// Check topleft-botright diagonal connection ////////////////////////////////////////////////////////// //reset temp variables tracker = 0; currentConnectedPieces = new List <Pair>(); negIndex = -1; //up and left posIndex = 1; //down and right while (x + negIndex >= 0 && y + negIndex >= 0 && board[x + negIndex][y + negIndex] != null && board[x + negIndex][y + negIndex].color == color) { currentConnectedPieces.Add(new Pair(x + negIndex, y + negIndex)); tracker++; negIndex--; } while (x + posIndex < board.Count && y + posIndex < board[x].Count && board[x + posIndex][y + posIndex] != null && board[x + posIndex][y + posIndex].color == color) { currentConnectedPieces.Add(new Pair(x + posIndex, y + posIndex)); tracker++; posIndex++; } //connection made (including origin piece) all pieces accounted for to both sides if (tracker >= 4) { connectionMade = true; connectedPieces += tracker; connectionPieces.AddRange(currentConnectedPieces); } ////////////////////////////////////////////////////////// /// Check botleft-topright diagonal connection ////////////////////////////////////////////////////////// //reset temp variables tracker = 0; currentConnectedPieces = new List <Pair>(); negIndex = -1; //up and left posIndex = 1; //down and right while (x - negIndex < board.Count && y + negIndex >= 0 && board[x - negIndex][y + negIndex] != null && board[x - negIndex][y + negIndex].color == color) { currentConnectedPieces.Add(new Pair(x - negIndex, y + negIndex)); tracker++; negIndex--; } while (x - posIndex >= 0 && y + posIndex < board[x].Count && board[x - posIndex][y + posIndex] != null && board[x - posIndex][y + posIndex].color == color) { currentConnectedPieces.Add(new Pair(x - posIndex, y + posIndex)); tracker++; posIndex++; } //connection made (including origin piece) all pieces accounted for to both sides if (tracker >= 4) { connectionMade = true; connectedPieces += tracker; connectionPieces.AddRange(currentConnectedPieces); } if (connectionMade) { //include origin piece connectedPieces++; connectionPieces.Add(new Pair(x, y)); CalculateScore(connectedPieces); CleanUpPieces(connectionPieces); } return(connectionMade); }
// TODO Display Victory and Reset the Game public void Victory(Piece.PieceColor color) { Debug.Log(color); }