public bool DoMove(Move m) { Square from = m.From(); Square to = m.To(); bool promote = m.Promote(); int fKind = IsDrop(from) ? sideToMove == Color.BLACK ? (int)from : (int)from + Piece.WhiteBit : square[(int)from]; int tKind = fKind + (promote ? Piece.PromoteBit : 0); int capture = square[(int)to]; square[(int)to] = tKind; square[(int)from] = Piece.Empty; // トライ勝ち if (sideToMove == Color.BLACK) { if (Square.SQ_04 < to && to < Square.SQ_08) { return(true); } } else { if (Square.SQ_16 < to && to < Square.SQ_20) { return(true); } } return((capture == Piece.BK || capture == Piece.WK) ? true : false); }
public bool DoMove(Move m) { Square from = m.From(); Square to = m.To(); bool promote = m.Promote(); int fKind = IsDrop(from) ? sideToMove == Color.BLACK ? (int)from : (int)from + Piece.WhiteBit : square[(int)from]; int tKind = fKind + (promote ? Piece.PromoteBit : 0); int capture = square[(int)to]; square[(int)to] = tKind; if (IsDrop(from)) { stand[(int)sideToMove][(int)from]--; } else { square[(int)from] = Piece.Empty; if (capture != Piece.Empty) { if (Piece.Abs(capture) != Piece.BK) { if (Piece.Abs(capture) != Piece.BPP) { stand[(int)sideToMove][Piece.Abs(capture)]++; } else { stand[(int)sideToMove][Piece.BP]++; } } } } // トライ勝ち if (Piece.Abs(fKind) == Piece.BK) { if (sideToMove == Color.BLACK && Square.SQ_08 < to && to < Square.SQ_12) { return(true); } if (sideToMove == Color.WHITE && Square.SQ_20 < to && to < Square.SQ_24) { return(true); } } // 手番変更 sideToMove = (sideToMove == Color.BLACK) ? Color.WHITE : Color.BLACK; return((capture == Piece.BK || capture == Piece.WK) ? true : false); }
public bool IsLegalMove(Move m) { Square from = m.From(); Square to = m.To(); bool promote = m.Promote(); // out of range if (from < Square.SQ_01 || from > Square.SQ_19 || to < Square.SQ_01 || to > Square.SQ_19) { return(false); } if (IsDrop(from)) { if (promote) { return(false); } if (square[(int)to] != Piece.Empty) { return(false); } } int piece = square[(int)from]; // 同じ場所には移動できない if (from == to) { return(false); } if (promote) { if (Piece.Abs(square[(int)from]) != Piece.BP) { return(false); } return(sideToMove == Color.BLACK ? (Square.SQ_04 < to && to < Square.SQ_08) : (Square.SQ_16 < to && to < Square.SQ_20)); } if (!Piece.CanPromote(piece) && promote) { return(false); } return(true); }
public bool IsLegalMove(Move m) { Square from = m.From(); Square to = m.To(); bool promote = m.Promote(); // out of range if (from < Square.SQ_01 || from > Square.SQ_23 || to < Square.SQ_01 || to > Square.SQ_23) { return(false); } if (IsDrop(from)) { if (promote) { return(false); } if (square[(int)to] != Piece.Empty) { return(false); } // 駒を持ってない if (Stand(sideToMove, (int)from) == 0) { return(false); } } else { int piece = square[(int)from]; // 同じ場所には移動できない if (from == to) { return(false); } // 駒が存在しない、または壁 if (piece == Piece.Empty || piece == Piece.Wall) { return(false); } // 動かすのが自分の駒でない if ((sideToMove == Color.BLACK && Piece.IsWhite(piece)) || (sideToMove == Color.WHITE && Piece.IsBlack(piece))) { return(false); } int cap = square[(int)to]; // 取るのが相手の駒でない if (cap != Piece.Empty && ((sideToMove == Color.WHITE && Piece.IsWhite(cap)) || (sideToMove == Color.BLACK && Piece.IsBlack(cap)))) { return(false); } int inc = to - from; // 動けない方向に動いている if (!Piece.Inc[piece].Contains(inc)) { return(false); } if (promote && !Piece.CanPromote(piece, to)) { return(false); } } return(true); }
private void clientComm(object p) { bool isready = false; Player threadPlayer = (Player)p; TcpClient threadClient = threadPlayer.Tcp(); NetworkStream threadStream = threadPlayer.Stream(); while (true) { // wait for data to come in try { byte[] buffer = new byte[255]; int bytesRead = threadStream.Read(buffer, 0, 255); string bufferStr = Encoding.UTF8.GetString(buffer); Console.WriteLine(threadPlayer.Name() + ">" + bufferStr); // if client clicks cancel if (bytesRead == 0) { break; } if (bufferStr.StartsWith("LOGIN")) { string s = bufferStr.Substring(0, bytesRead); string[] temp = s.Split(':'); if (temp.Length > 1) { string name = temp[1].Trim(); Console.WriteLine("LOGIN:"******",ok\n"); WriteStream(threadPlayer.Stream(), "LOGIN:"******",ok\n"); threadPlayer.SetName(name); } else { WriteStream(threadPlayer.Stream(), "Please LOGIN like \"LOGIN UserName\"\n"); } continue; } if (!isready && bufferStr.StartsWith("AGREE")) { WriteStream(threadPlayer.Stream(), "START\n"); isready = true; continue; } if (threadPlayer.Opponent() == null) { continue; } // resign if (bufferStr.StartsWith("resign")) { WriteStream(threadPlayer.Opponent().Stream(), "#GAME_OVER\n"); WriteStream(threadPlayer.Opponent().Stream(), "#WIN\n"); break; } else if (bufferStr.StartsWith("+") || (bufferStr.StartsWith("-"))) { // 処理中に対局が無くなると死ぬ int gameIdx = -1; for (int i = 0; i < games.Count; i++) { if (games[i].Id() == threadPlayer.GameId()) { gameIdx = i; } } if (gameIdx == -1) { Console.WriteLine("ERROR"); break; } Move move = new Move(bufferStr.Substring(1, 6)); // illegal move if ((bufferStr.StartsWith("+") && threadPlayer.MyColor() != Color.BLACK) || (bufferStr.StartsWith("-") && threadPlayer.MyColor() != Color.WHITE) || threadPlayer.MyColor() != games[gameIdx].pos.SideToMove() || !games[gameIdx].pos.IsLegalMove(move)) { // DEBUG Console.WriteLine(games[gameIdx].pos.PrintPosition()); WriteStream(threadPlayer.Stream(), "#ILLEGAL\n"); WriteStream(threadPlayer.Stream(), "#LOSE\n"); //tell opponent game ended WriteStream(threadPlayer.Opponent().Stream(), "#GAME_OVER\n"); WriteStream(threadPlayer.Opponent().Stream(), "#WIN\n"); break; } // OKを送る string mStr = (move.Promote() ? bufferStr.Substring(0, 6) : bufferStr.Substring(0, 5)) + ",OK\n"; WriteStream(threadPlayer.Stream(), mStr); WriteStream(threadPlayer.Opponent().Stream(), mStr); // do move if (games[gameIdx].pos.DoMove(move)) { WriteStream(threadPlayer.Stream(), "#GAME_OVER\n"); WriteStream(threadPlayer.Stream(), "#WIN\n"); //tell opponent game ended WriteStream(threadPlayer.Opponent().Stream(), "#GAME_OVER\n"); WriteStream(threadPlayer.Opponent().Stream(), "#LOSE\n"); break; } } // chat else { threadPlayer.Opponent().Stream().Write(buffer, 0, bytesRead); } } // Client closed catch (System.IO.IOException) { break; } } Console.WriteLine("Player #" + threadPlayer.PlayerId() + " left"); if (threadPlayer.Opponent() != null && threadPlayer.Opponent().Tcp().Connected) { threadPlayer.Opponent().Stream().Close(); threadPlayer.Opponent().Tcp().Close(); } threadStream.Close(); threadClient.Close(); lock (waitingPlayerLock) { if (waitingPlayer != null) { if (waitingPlayer.PlayerId() == threadPlayer.PlayerId()) { waitingPlayer = null; } } } lock (playerLock) { players.RemoveAll(x => x.PlayerId() == threadPlayer.PlayerId()); } // find game from gameID and see if can remove from list lock (gamesLock) { for (int i = 0; i < games.Count; i++) { if (games[i].Id() == threadPlayer.GameId()) { if (threadPlayer.Opponent() != null) { if (games[i].Player1().Tcp().Connected == false && games[i].Player2().Tcp().Connected == false) { games.RemoveAt(i); break; } } } } } }
private void clientComm(object p) { bool isready = false; int bytesRead; Player threadPlayer = (Player)p; TcpClient threadClient = threadPlayer.Tcp(); NetworkStream threadStream = threadPlayer.Stream(); byte[] buffer = new byte[255]; byte[] start = Encoding.GetEncoding("UTF-8").GetBytes("START\n"); byte[] abnormal = Encoding.GetEncoding("UTF-8").GetBytes("#ABNORMAL\n"); byte[] gameover = Encoding.GetEncoding("UTF-8").GetBytes("#GAME_OVER\n"); byte[] illegal = Encoding.GetEncoding("UTF-8").GetBytes("#ILLEGAL\n"); byte[] win = Encoding.GetEncoding("UTF-8").GetBytes("#WIN\n"); byte[] lose = Encoding.GetEncoding("UTF-8").GetBytes("#LOSE\n"); byte[] draw = Encoding.GetEncoding("UTF-8").GetBytes("#DRAW\n"); while (true) { // wait for data to come in try { bytesRead = threadStream.Read(buffer, 0, 255); string bufferStr = Encoding.UTF8.GetString(buffer); Console.WriteLine(bufferStr); if (!isready && bufferStr.StartsWith("AGREE")) { threadPlayer.Stream().Write(start, 0, start.Length); isready = true; continue; } if (threadClient.Client.Connected && threadClient.Client.Poll(1000, SelectMode.SelectRead) && (threadClient.Client.Available == 0)) { break; } if (threadPlayer.Opponent() == null) { continue; } // if client clicks cancel if (bytesRead == 0) { if (threadPlayer.Opponent() != null) { if (threadPlayer.Opponent().Tcp().Connected == true) { // tell opponent game ended threadPlayer.Opponent().Stream().Write(abnormal, 0, abnormal.Length); } } break; } // resign else if (bufferStr.StartsWith("resign")) { //tell opponent game ended threadPlayer.Opponent().Stream().Write(gameover, 0, gameover.Length); threadPlayer.Opponent().Stream().Write(win, 0, win.Length); break; } else if (bufferStr.StartsWith("+") || (bufferStr.StartsWith("-"))) { // 処理中に対局が無くなると死ぬ int gameIdx = -1; for (int i = 0; i < games.Count; i++) { if (games[i].Id() == threadPlayer.GameId()) { gameIdx = i; } } if (gameIdx == -1) { Console.WriteLine("ERROR"); break; } Move move = new Move(bufferStr.Substring(1, 6)); // illegal move if ((bufferStr.StartsWith("+") && threadPlayer.MyColor() != Color.BLACK) || (bufferStr.StartsWith("-") && threadPlayer.MyColor() != Color.WHITE) || !games[gameIdx].pos.IsLegalMove(move)) { threadPlayer.Stream().Write(illegal, 0, illegal.Length); threadPlayer.Stream().Write(lose, 0, lose.Length); //tell opponent game ended threadPlayer.Opponent().Stream().Write(gameover, 0, gameover.Length); threadPlayer.Opponent().Stream().Write(win, 0, win.Length); break; } // OKを送る string mStr = (move.Promote() ? bufferStr.Substring(1, 5) : bufferStr.Substring(1, 4)) + ",OK\n"; byte[] ok = Encoding.GetEncoding("UTF-8").GetBytes(mStr); threadPlayer.Stream().Write(ok, 0, ok.Length); threadPlayer.Opponent().Stream().Write(ok, 0, ok.Length); // do move if (games[gameIdx].pos.DoMove(move)) { threadPlayer.Stream().Write(gameover, 0, gameover.Length); threadPlayer.Stream().Write(win, 0, win.Length); //tell opponent game ended threadPlayer.Opponent().Stream().Write(gameover, 0, gameover.Length); threadPlayer.Opponent().Stream().Write(lose, 0, win.Length); break; } } // chat else { threadPlayer.Opponent().Stream().Write(buffer, 0, bytesRead); } } // Client closed catch (System.IO.IOException) { // tell opponent game ended if (threadPlayer.Opponent() != null) { threadPlayer.Opponent().Stream().Write(abnormal, 0, abnormal.Length); } break; } } Console.WriteLine("Player #" + threadPlayer.PlayerId() + " left"); if (threadPlayer.Opponent() != null) { threadPlayer.Opponent().Stream().Close(); threadPlayer.Opponent().Tcp().Close(); } threadStream.Close(); threadClient.Close(); lock (waitingPlayerLock) { if (waitingPlayer != null) { if (waitingPlayer.PlayerId() == threadPlayer.PlayerId()) { waitingPlayer = null; } } } // find game from gameID and see if can remove from list lock (gamesLock) { for (int i = 0; i < games.Count; i++) { if (games[i].Id() == threadPlayer.GameId()) { if (threadPlayer.Opponent() != null) { if (games[i].Player1().Tcp().Connected == false && games[i].Player2().Tcp().Connected == false) { games.RemoveAt(i); break; } } } } } }