예제 #1
0
파일: Game.cs 프로젝트: psmon/chessgo
    public void SwapDolPos(VectorDol leftPos, VectorDol rightPos)
    {
        PlayDol left  = Dols.getPlayDolByIdx(leftPos.x, leftPos.y);
        PlayDol right = Dols.getPlayDolByIdx(rightPos.x, rightPos.y);

        if (left != null && right != null)
        {
            Vector3 oriPos = left.transform.position;
            //left.transform.position = right.transform.position;

            left.move_ani(right.transform.position);

            right.transform.position = oriPos;

            VectorDol temp = new VectorDol();
            temp.setPos(left.GetDolPos());

            left.SetDolPos(right.GetDolPos());
            right.SetDolPos(temp);
        }

        curPlayDol = left;
        WebDataRes chkRes = new WebDataRes();

        chkRes.pid = "CheckGame";
        packetList.Enqueue(chkRes.ToString());
    }
예제 #2
0
 public static void SetOffAllCanMove()
 {
     foreach (GameObject gameObj in allDols)
     {
         PlayDol pdol = gameObj.GetComponent <PlayDol>();
         pdol.SetOffCanMove();
     }
 }
예제 #3
0
    public static PlayDol getPlayDolByIdx(int idx, int idy)
    {
        PlayDol result = null;

        foreach (GameObject gameObj in allDols)
        {
            PlayDol pdol = gameObj.GetComponent <PlayDol>();

            if (pdol.GetDolPos().x == idx && pdol.GetDolPos().y == idy)
            {
                result = pdol;
                break;
            }
        }
        return(result);
    }
예제 #4
0
    public void InitDols()
    {
        //GameObject dol = (GameObject)Instantiate(Resources.Load("Images"));
        for (int idx = 0; idx < 8; idx++)
        {
            for (int idy = 0; idy < 8; idy++)
            {
                GameObject dol = Instantiate(playDol, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
                dol.transform.position = getPosByIdx(idx, idy);
                PlayDol pdol = dol.GetComponent <PlayDol>();
                ;

                if (getDolTypeByIdx(idx, idy) == 1)
                {
                    pdol.SetDolColor(1);
                    pdol.SetDolPos(idx, idy);
                    wplayDols[idx] = dol;
                    dol.name       = "whiteDol-" + idx;
                }
                else if (getDolTypeByIdx(idx, idy) == 2)
                {
                    pdol.SetDolColor(2);
                    pdol.SetDolPos(idx, idy);
                    bplayDols[idx] = dol;
                    dol.name       = "blackDol-" + idx;
                }
                else
                {
                    pdol.SetDolColor(0);
                    pdol.SetDolPos(idx, idy);
                    dol.name = "empty-" + idx + ":" + idy;
                }

                allDols.Add(dol);
            }
        }
    }
예제 #5
0
파일: Game.cs 프로젝트: psmon/chessgo
    void FixedUpdate()
    {
        if (PlayDol.isRunAnimation == true)
        {
            return;
        }

        if (isNetworkPlay && isOffLineMode == false)
        {
            if (globalTimeLeft < 0)
            {
                //GameOver();
                if (isOffLineMode == false)
                {
                }

                GameResultInfo gameResultInfo = new GameResultInfo();

                if (myPlyScore > otherPlyScore)
                {
                    gameResultInfo.winnerColor = isMyDolColorBlack == true ? 2 : 1;
                    gameResultInfo.wiinerScore = myPlyScore;
                    gameResultInfo.loseScore   = otherPlyScore;
                    gameResultInfo.wiinnerIsme = true;
                    showResult("You Win", true);
                    ws.Send(gameResultInfo.ToString());
                }
                else if (myPlyScore < otherPlyScore)
                {
                    gameResultInfo.winnerColor = isMyDolColorBlack == true ? 1 : 2;
                    gameResultInfo.wiinerScore = otherPlyScore;
                    gameResultInfo.loseScore   = myPlyScore;
                    gameResultInfo.wiinnerIsme = false;
                    showResult("You Lost", true);
                    ws.Send(gameResultInfo.ToString());
                }
                else
                {
                    gameResultInfo.winnerColor = isMyDolColorBlack == true ? 2 : 1;
                    gameResultInfo.wiinerScore = myPlyScore;
                    gameResultInfo.loseScore   = otherPlyScore;
                    gameResultInfo.wiinnerIsme = true;
                    showResult("Drawgame", true);
                    ws.Send(gameResultInfo.ToString());
                }

                globalTimeLeft = gameInfo.totalTimeBank + 10;
            }
            else
            {
                globalTimeLeft         -= Time.fixedDeltaTime;
                txtTotalRemainTime.text = string.Format("RemainTime:{0}", (int)globalTimeLeft);
            }

            if (isMyTurn)
            {
                if (0 < privateTimeLeft)
                {
                    privateTimeLeft    -= Time.fixedDeltaTime;
                    txtPrivateTime.text = string.Format("You,Limit:{0}", (int)privateTimeLeft);
                }
                else
                {
                    if (Game.selectedDol != null)
                    {
                        Game.selectedDol.indicatorOff();
                    }

                    Game.selectedDol = null;
                    Game.targetDol   = null;
                    Dols.SetOffAllCanMove();
                    MoveInfoReq moveInfoReq = new MoveInfoReq();
                    VectorDol   nullPos     = new VectorDol();
                    nullPos.x = -1;
                    moveInfoReq.source.setPos(nullPos);
                    moveInfoReq.target.setPos(nullPos);
                    send(moveInfoReq.ToString());
                    privateTimeLeft = gameInfo.privateTimeBank;
                }
            }
            else
            {
                if (0 < privateTimeLeft)
                {
                    privateTimeLeft    -= Time.fixedDeltaTime;
                    txtPrivateTime.text = string.Format("He,Limit:{0}", (int)privateTimeLeft);
                }
                else
                {
                    privateTimeLeft = gameInfo.privateTimeBank;
                }
            }
        }

        ProcessPackets();
    }
예제 #6
0
파일: Game.cs 프로젝트: psmon/chessgo
    private void ProcessPackets()
    {
        string curWsData = "";

        lock (packetList)
        {
            if (packetList.Count > 0)
            {
                curWsData = packetList.Dequeue();
            }
        }

        if (curWsData.Length < 1)
        {
            return;
        }

        var    jsonObject = JsonUtility.FromJson <WebDataRes>(curWsData);
        string pid        = jsonObject.pid;

        switch (pid)
        {
        case "Connected":
            LoginInfo loginInfo = new LoginInfo();
            loginInfo.deviceId = deviceId;
            loginInfo.nickName = PlayerPrefs.GetString("playerName");
            if (Debug.isDebugBuild)
            {
                System.Random rnd    = new System.Random();
                int           random = rnd.Next(1, 1000);
                loginInfo.deviceId = deviceId + random;
            }
            ws.Send(loginInfo.ToString());
            txtServerState.text = "Conneted Server";
            setOnLine(true);
            break;

        case "Disconnected":
            dols.CleanDols();
            isNetworkPlay       = true;
            txtServerState.text = "Disconneted Server";
            isNetworkPlay       = false;
            setOnLine(false);
            //Application.Quit();
            break;

        case "LoginInfoRes":
            isOffLineMode = false;
            LoginInfoRes loginRes = new LoginInfoRes();
            loginRes.FromJsonOverwrite(jsonObject.data);
            Debug.Log("LoginInfoRes: " + loginRes.ToString());
            if (loginRes.loginResult > 0)
            {
                txtServerState.text = "Wait Opponent Player(You Can run PracticeMode during waiting)";
                QuickSeatReq quickSeat = new QuickSeatReq();
                ws.Send(quickSeat.ToString());
            }
            else
            {
                txtServerState.text = "Login Failed...";
            }
            break;

        case "GameInfo":
            GameInfo gameInfoRes = new GameInfo();
            gameInfoRes.FromJsonOverwrite(jsonObject.data);
            gameInfo       = gameInfoRes;
            globalTimeLeft = (float)gameInfoRes.totalTimeBank;
            break;

        case "DolsInfo":
            setOnLine(true);
            globalTimeLeft = (float)gameInfo.totalTimeBank;
            isOffLineMode  = false;
            isNetworkPlay  = true;
            DolsInfo dolsinfo = new DolsInfo();
            dolsinfo.FromJsonOverwrite(jsonObject.data);
            Debug.Log("DolsInfo: " + dolsinfo.ToString());

            if (dolsinfo.isBlack == false)
            {
                dols.firstWplayDols = dolsinfo;
            }
            else
            {
                dols.firstBplayDols = dolsinfo;
                dols.CleanDols();
                dols.InitDols();
            }

            if (dolsinfo.nickName.Length > 0)
            {
                if (dolsinfo.isMe)
                {
                    txtYour_name.text = dolsinfo.nickName;
                }
                else
                {
                    txtOtherName.text = dolsinfo.nickName;
                }
            }
            onStageInit();
            break;

        case "MoveInfoRes":
            MoveInfoRes moveInfoRes = new MoveInfoRes();
            moveInfoRes.FromJsonOverwrite(jsonObject.data);
            SwapDolPos(moveInfoRes.source, moveInfoRes.target);
            break;

        case "TurnInfo":
            TurnInfo turnInfo = new TurnInfo();
            turnInfo.FromJsonOverwrite(jsonObject.data);
            string txtTurnInfo     = "";
            string currentDolColor = turnInfo.isBlack == true ? "black" : "white";
            if (turnInfo.isMe)
            {
                txtTurnInfo       = string.Format("Your({0}) turn", currentDolColor);
                isMyTurn          = true;
                isMyDolColorBlack = turnInfo.isBlack;
                privateTimeLeft   = gameInfo.privateTimeBank;
            }
            else
            {
                txtTurnInfo       = string.Format("Wait other player({0}) Action", currentDolColor);
                isMyTurn          = false;
                isMyDolColorBlack = !turnInfo.isBlack;
                privateTimeLeft   = gameInfo.privateTimeBank;
            }

            if (isOffLineMode)
            {
                isMyDolColorBlack = turnInfo.isBlack;
                txtTurnInfo       = string.Format("{0} Turn", currentDolColor);

                if (isAIMode)
                {
                    if (turnInfo.isBlack == true)
                    {
                        //dols.doaskAIAction
                        PlayDol sourceDol = null;
                        PlayDol targetDol = null;
                        dols.askAIAction(2, ref sourceDol, ref targetDol);

                        MoveInfoRes moveInfoReq = new MoveInfoRes();
                        moveInfoReq.source.setPos(sourceDol.GetDolPos());
                        moveInfoReq.target.setPos(targetDol.GetDolPos());
                        Debug.Log("AI:" + moveInfoReq.ToString());
                        sendLocalData("MoveInfoRes", moveInfoReq.ToString());

                        TurnInfo turnChange = new TurnInfo();
                        turnChange.isMe    = true;
                        turnChange.isBlack = false;

                        sendLocalData("TurnInfo", turnChange.ToString());
                    }
                }
            }

            txtServerState.text = txtTurnInfo;
            break;

        case "CrashGameInfo":
            dols.CleanDols();
            txtServerState.text = "Other User Leaver, Wait Other Player";
            isNetworkPlay       = false;
            break;

        case "CheckGame":
            if (isOffLineMode == false)
            {
                if (isMyDolColorBlack == true && curPlayDol.GetMyDolType() == 2)
                {
                    myPlyScore += Dols.checkGame(curPlayDol);
                }

                if (isMyDolColorBlack == true && curPlayDol.GetMyDolType() == 1)
                {
                    otherPlyScore += Dols.checkGame(curPlayDol);
                }

                if (isMyDolColorBlack == false && curPlayDol.GetMyDolType() == 1)
                {
                    myPlyScore += Dols.checkGame(curPlayDol);
                }

                if (isMyDolColorBlack == false && curPlayDol.GetMyDolType() == 2)
                {
                    otherPlyScore += Dols.checkGame(curPlayDol);
                }
            }
            else
            {
                if (curPlayDol.GetMyDolType() == 1)
                {
                    myPlyScore += Dols.checkGame(curPlayDol);
                }
                else
                {
                    otherPlyScore += Dols.checkGame(curPlayDol);
                }
            }

            if (isOffLineMode == false)
            {
                txtMyPlyScore.text     = myPlyScore.ToString();
                txtOtherPlayScore.text = otherPlyScore.ToString();
            }
            else
            {
                txtMyPlyScore.text     = myPlyScore.ToString();
                txtOtherPlayScore.text = otherPlyScore.ToString();
                txtYour_name.text      = "White";
                txtOtherName.text      = "Black";
            }

            GameResultInfo gameResultInfo = new GameResultInfo();
            if (myPlyScore > gameInfo.winScore - 1)
            {
                gameResultInfo.winnerColor = curPlayDol.GetMyDolType();
                gameResultInfo.wiinerScore = myPlyScore;
                gameResultInfo.loseScore   = otherPlyScore;
                gameResultInfo.wiinnerIsme = true;

                if (isOffLineMode == false)
                {
                    showResult("You Win", true);
                    ws.Send(gameResultInfo.ToString());
                }
                else
                {
                    showResult("White Win", true);
                }
            }

            if (otherPlyScore > gameInfo.winScore - 1)
            {
                gameResultInfo.winnerColor = curPlayDol.GetMyDolType();
                gameResultInfo.wiinerScore = otherPlyScore;
                gameResultInfo.loseScore   = myPlyScore;
                gameResultInfo.wiinnerIsme = false;

                if (isOffLineMode == false)
                {
                    showResult("You Lose", true);
                    ws.Send(gameResultInfo.ToString());
                }
                else
                {
                    showResult("Black Win", true);
                }
            }
            break;
        }
    }
예제 #7
0
    public static int checkGame(PlayDol selectedDol, bool isPreCheck = false, int chkDolType = 1)
    {
        int            score  = 0;
        List <PlayDol> result = new List <PlayDol>();

        VectorDol selectPost = selectedDol.GetDolPos();
        int       selDolType = isPreCheck == false?selectedDol.GetMyDolType() : chkDolType;

        //LeftCechk...
        List <PlayDol> removeDols = new List <PlayDol>();

        for (int idx = selectPost.x - 1; -1 < idx; idx--)
        {
            PlayDol curDol = getPlayDolByIdx(idx, selectPost.y);
            if (curDol == null)
            {
                break;
            }

            if (curDol.GetMyDolType() == 0)
            {
                break;
            }

            if (idx == selectPost.x - 1)
            {
                if (curDol.GetMyDolType() == selDolType)
                {
                    break;
                }
            }

            if (selDolType != curDol.GetMyDolType())
            {
                removeDols.Add(curDol);
            }

            if (selDolType == curDol.GetMyDolType())
            {
                if (removeDols.Count > 0)
                {
                    score += removeDols.Count;
                    foreach (PlayDol removedol in removeDols)
                    {
                        if (isPreCheck == false)
                        {
                            removedol.SetDolColor(0);
                        }
                    }
                }
                break;
            }
        }

        //RightCheck
        removeDols = new List <PlayDol>();
        for (int idx = selectPost.x + 1; idx < 8; idx++)
        {
            PlayDol curDol = getPlayDolByIdx(idx, selectPost.y);
            if (curDol == null)
            {
                break;
            }

            if (curDol.GetMyDolType() == 0)
            {
                break;
            }

            if (idx == selectPost.x + 1)
            {
                if (curDol.GetMyDolType() == selDolType)
                {
                    break;
                }
            }

            if (selDolType != curDol.GetMyDolType())
            {
                removeDols.Add(curDol);
            }

            if (selDolType == curDol.GetMyDolType())
            {
                if (removeDols.Count > 0)
                {
                    score += removeDols.Count;
                    foreach (PlayDol removedol in removeDols)
                    {
                        if (isPreCheck == false)
                        {
                            removedol.SetDolColor(0);
                        }
                    }
                }
                break;
            }
        }

        //Upcheck
        removeDols = new List <PlayDol>();
        for (int idy = selectPost.y + 1; idy < 8; idy++)
        {
            PlayDol curDol = getPlayDolByIdx(selectPost.x, idy);
            if (curDol == null)
            {
                break;
            }

            if (curDol.GetMyDolType() == 0)
            {
                break;
            }

            if (idy == selectPost.y + 1)
            {
                if (curDol.GetMyDolType() == selDolType)
                {
                    break;
                }
            }

            if (selDolType != curDol.GetMyDolType())
            {
                removeDols.Add(curDol);
            }

            if (selDolType == curDol.GetMyDolType())
            {
                if (removeDols.Count > 0)
                {
                    score += removeDols.Count;
                    foreach (PlayDol removedol in removeDols)
                    {
                        if (isPreCheck == false)
                        {
                            removedol.SetDolColor(0);
                        }
                    }
                }
                break;
            }
        }

        //DownCheck
        removeDols = new List <PlayDol>();
        for (int idy = selectPost.y - 1; -1 < idy; idy--)
        {
            PlayDol curDol = getPlayDolByIdx(selectPost.x, idy);
            if (curDol == null)
            {
                break;
            }

            if (curDol.GetMyDolType() == 0)
            {
                break;
            }

            if (idy == selectPost.y - 1)
            {
                if (curDol.GetMyDolType() == selDolType)
                {
                    break;
                }
            }

            if (selDolType != curDol.GetMyDolType())
            {
                removeDols.Add(curDol);
            }

            if (selDolType == curDol.GetMyDolType())
            {
                if (removeDols.Count > 0)
                {
                    score += removeDols.Count;
                    foreach (PlayDol removedol in removeDols)
                    {
                        if (isPreCheck == false)
                        {
                            removedol.SetDolColor(0);
                        }
                    }
                }
                break;
            }
        }
        return(score);
    }
예제 #8
0
    public static List <PlayDol> canMoveDolList(PlayDol selectedDol, bool justGetData = false)
    {
        List <PlayDol> result     = new List <PlayDol>();
        VectorDol      selectPost = selectedDol.GetDolPos();

        //LeftCechk...
        for (int idx = selectPost.x - 1; -1 < idx; idx--)
        {
            bool    canMove = false;
            PlayDol curDol  = getPlayDolByIdx(idx, selectPost.y);
            if (curDol == null)
            {
                break;
            }

            if (curDol.GetMyDolType() == 0)
            {
                canMove = true;
                if (justGetData == false)
                {
                    curDol.SetOnCanMove();
                }

                result.Add(curDol);
            }
            if (canMove == false)
            {
                break;
            }
        }

        //RightCheck
        for (int idx = selectPost.x + 1; idx < 8; idx++)
        {
            bool    canMove = false;
            PlayDol curDol  = getPlayDolByIdx(idx, selectPost.y);
            if (curDol == null)
            {
                break;
            }

            if (curDol.GetMyDolType() == 0)
            {
                canMove = true;
                if (justGetData == false)
                {
                    curDol.SetOnCanMove();
                }
                result.Add(curDol);
            }
            if (canMove == false)
            {
                break;
            }
        }

        //Upcheck
        for (int idy = selectPost.y + 1; idy < 8; idy++)
        {
            bool    canMove = false;
            PlayDol curDol  = getPlayDolByIdx(selectPost.x, idy);
            if (curDol == null)
            {
                break;
            }

            if (curDol.GetMyDolType() == 0)
            {
                canMove = true;
                if (justGetData == false)
                {
                    curDol.SetOnCanMove();
                }
                result.Add(curDol);
            }

            if (canMove == false)
            {
                break;
            }
        }

        //DownCheck
        for (int idy = selectPost.y - 1; -1 < idy; idy--)
        {
            bool    canMove = false;
            PlayDol curDol  = getPlayDolByIdx(selectPost.x, idy);
            if (curDol == null)
            {
                break;
            }

            if (curDol.GetMyDolType() == 0)
            {
                canMove = true;
                if (justGetData == false)
                {
                    curDol.SetOnCanMove();
                }
                result.Add(curDol);
            }

            if (canMove == false)
            {
                break;
            }
        }
        return(result);
    }
예제 #9
0
    public void askAIAction(int dolType, ref PlayDol sourceDol, ref PlayDol targetDol)
    {
        List <PlayDol> canMoveList = new List <PlayDol>();

        foreach (GameObject gameObj in allDols)
        {
            PlayDol pdol = gameObj.GetComponent <PlayDol>();

            if (pdol.GetMyDolType() == dolType)
            {
                List <PlayDol> moveList = canMoveDolList(pdol, true);
                if (moveList.Count > 0)
                {
                    canMoveList.Add(pdol);
                }
            }
        }

        System.Random breakRnd  = new System.Random();
        int           brkrandom = breakRnd.Next(0, canMoveList.Count);
        int           addIdx    = 0;
        int           canObtain = 0;

        foreach (PlayDol aiDol in canMoveList)
        {
            List <PlayDol> moveList = canMoveDolList(aiDol, true);
            System.Random  rnd      = new System.Random();
            int            random   = rnd.Next(0, moveList.Count);
            sourceDol = aiDol;
            targetDol = moveList[random];
            foreach (PlayDol moveDol in moveList)
            {
                if (0 < checkGame(moveDol, true, aiDol.GetMyDolType()))
                {
                    targetDol = moveDol;
                    return;
                }
            }
        }

        foreach (PlayDol aiDol in canMoveList)
        {
            List <PlayDol> moveList = canMoveDolList(aiDol, true);
            System.Random  rnd      = new System.Random();
            int            random   = rnd.Next(0, moveList.Count);
            sourceDol = aiDol;
            targetDol = moveList[random];

            foreach (PlayDol moveDol in moveList)
            {
                if (0 < checkGame(moveDol, true, aiDol.GetMyDolType()))
                {
                    targetDol = moveDol;
                    return;
                }
            }

            if (addIdx == brkrandom)
            {
                break;
            }

            addIdx++;
        }
    }