public bool DrawLine(string tblId, string cellId, string oponentId, string myId) { var gt = context.GameTables.FirstOrDefault(x => x.GameId == tblId); var lastBoardInfoList = context.MoveTables.Where(x => x.TableId == gt.GameId).ToList(); var lastBoardInfo = lastBoardInfoList[lastBoardInfoList.Count - 1]; var tableCellStrings = lastBoardInfo.Board.Split(','); //Clients.All.allertAMessage(gt.UserInTurn, oponentId, tableCellStrings.Length); List<CellObj> tableCellObj = new List<CellObj>(); foreach (var item in tableCellStrings) { CellObj obj = new CellObj(item); tableCellObj.Add(obj); } CellObj currentObj = new CellObj(cellId); currentObj.Taken = true; StringBuilder newBoardState = new StringBuilder(); foreach (var item in tableCellObj) { if (item.Row == currentObj.Row && item.Col == currentObj.Col) { newBoardState.Append(currentObj.ToString()); } else { newBoardState.Append(item.ToString()); } newBoardState.Append(','); } newBoardState.Length--; NewState(cellId, gt.GameId, gt.UserInTurn, newBoardState); var hasCloseBox = CloseBox(currentObj, tableCellObj, newBoardState, gt.GameId, gt.UserInTurn); if (hasCloseBox.IsClose == false) { gt.UserInTurn = oponentId; } else { var meUserPf = context.Users.FirstOrDefault(x => x.DeviceId == myId); var oponentUserPf = context.Users.FirstOrDefault(x => x.DeviceId == oponentId); if (hasCloseBox.RowUp != 0 && hasCloseBox.ColUp != 0) { string boxId = hasCloseBox.RowUp + "-" + hasCloseBox.ColUp; Clients.Client(meUserPf.UserWebClientId).colorBox(myId, oponentId, tblId, boxId); Clients.Client(oponentUserPf.UserWebClientId).colorBox(myId, oponentId, tblId, boxId); } if (hasCloseBox.RowDown != 0 && hasCloseBox.ColDown != 0) { string boxId = hasCloseBox.RowDown + "-" + hasCloseBox.ColDown; Clients.Client(meUserPf.UserWebClientId).colorBox(myId, oponentId, tblId, boxId); Clients.Client(oponentUserPf.UserWebClientId).colorBox(myId, oponentId, tblId, boxId); } } context.SaveChanges(); return true; }
public CloseBox CloseBox(CellObj cellObj, List<CellObj> tableComponentsObj, StringBuilder boardState, string gameId, string userId) { string lastCellId = tableComponentsObj[tableComponentsObj.Count - 1].Id; CellObj lastCell = new CellObj(lastCellId); lastCell.Row--; lastCell.Col--; CloseBox closed = new CloseBox(false); if (cellObj.IsHorizontal) { if (cellObj.Row == 0) { var isTakenUp = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row + 2 && x.Col == cellObj.Col && x.Taken); var isTakenLeft = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row + 1 && x.Col == cellObj.Col - 1 && x.Taken);// (cellObj.Row + 1) + "_" + (cellObj.Col - 1); var isTakenRight = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row + 1 && x.Col == cellObj.Col + 1 && x.Taken);// (cellObj.Row + 1) + "_" + (col + 1); if (isTakenUp != null && isTakenLeft != null && isTakenRight != null) { closed.IsClose = true; closed.RowUp = cellObj.Row + 1; closed.ColUp = cellObj.Col; return closed; } return closed; } else if (cellObj.Row == lastCell.Row) { var isTakenUp = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row - 2 && x.Col == cellObj.Col && x.Taken); var isTakenLeft = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row - 1 && x.Col == cellObj.Col - 1 && x.Taken); var isTakenRight = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row - 1 && x.Col == cellObj.Col + 1 && x.Taken); if (isTakenUp != null && isTakenLeft != null && isTakenRight != null) { closed.IsClose = true; closed.RowUp = cellObj.Row - 1; closed.ColUp = cellObj.Col; return closed; } return closed; } else { var isTakenUp = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row - 2 && x.Col == cellObj.Col && x.Taken); var isTakenLeft = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row - 1 && x.Col == cellObj.Col - 1 && x.Taken); var isTakenRight = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row - 1 && x.Col == cellObj.Col + 1 && x.Taken); if (isTakenUp != null && isTakenLeft != null && isTakenRight != null) { closed.IsClose = true; closed.RowUp = cellObj.Row - 1; closed.ColUp = cellObj.Col; } isTakenUp = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row + 2 && x.Col == cellObj.Col && x.Taken); isTakenLeft = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row + 1 && x.Col == cellObj.Col - 1 && x.Taken); isTakenRight = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row + 1 && x.Col == cellObj.Col + 1 && x.Taken); if (isTakenUp != null && isTakenLeft != null && isTakenRight != null) { closed.IsClose = true; closed.RowUp = cellObj.Row + 1; closed.ColUp = cellObj.Col; return closed; } return closed; } } else { if (cellObj.Col == 0) { var isTakenUp = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row && x.Col == cellObj.Col + 2 && x.Taken); var isTakenLeft = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row - 1 && x.Col == cellObj.Col + 1 && x.Taken); var isTakenRight = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row + 1 && x.Col == cellObj.Col + 1 && x.Taken); if (isTakenUp != null && isTakenLeft != null && isTakenRight != null) { closed.IsClose = true; closed.RowUp = cellObj.Row; closed.ColUp = cellObj.Col + 1; return closed; } return closed; } else if (cellObj.Col == lastCell.Col) { var isTakenUp = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row && x.Col == cellObj.Col - 2 && x.Taken); var isTakenLeft = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row - 1 && x.Col == cellObj.Col - 1 && x.Taken); var isTakenRight = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row + 1 && x.Col == cellObj.Col - 1 && x.Taken); if (isTakenUp != null && isTakenLeft != null && isTakenRight != null) { closed.IsClose = true; closed.RowUp = cellObj.Row; closed.ColUp = cellObj.Col - 1; return closed; } return closed; } else { var result = false; var isTakenUp = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row && x.Col == cellObj.Col - 2 && x.Taken); var isTakenLeft = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row - 1 && x.Col == cellObj.Col - 1 && x.Taken); var isTakenRight = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row + 1 && x.Col == cellObj.Col - 1 && x.Taken); if (isTakenUp != null && isTakenLeft != null && isTakenRight != null) { closed.IsClose = true; closed.RowUp = cellObj.Row; closed.ColUp = cellObj.Col - 1; } isTakenUp = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row && x.Col == cellObj.Col + 2 && x.Taken); isTakenLeft = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row - 1 && x.Col == cellObj.Col + 1 && x.Taken); isTakenRight = tableComponentsObj.FirstOrDefault(x => x.Row == cellObj.Row + 1 && x.Col == cellObj.Col + 1 && x.Taken); if (isTakenUp != null && isTakenLeft != null && isTakenRight != null) { closed.IsClose = true; closed.RowUp = cellObj.Row; closed.ColUp = cellObj.Col + 1; } return closed; } } }