コード例 #1
0
ファイル: board_game.cs プロジェクト: konnya/board-game
    private void TryMovePlayerPiece(PlayerType ptype, Pos from, Pos to)
    {
        var idx = GetPlayerIndex(ptype);

        // var gs = GetPiece(_pmap, from);
        var gs = GetPiece(idx, from);

        if (gs == null)
        {
            var cx = CoordinateTrans.XtoA(from.x);
            var cy = CoordinateTrans.YtoA(from.y);
            throw new PieceDoesNotExistException(
                      $"Threre is NOT a PIECE in ({cx},{cy}) or ({from.x},{from.y}).");
        }

        // var friend = GetPiece(_pmap, to);
        var friend = GetPiece(idx, to);

        if (friend != null)
        {
            var cx = CoordinateTrans.XtoA(from.x);
            var cy = CoordinateTrans.YtoA(from.y);
            throw new PieceDoesNotExistException(
                      $"Threre is a FRIEND PIECE in ({cx},{cy}) or ({from.x},{from.y}).");
        }

        // requires System.Linq.Enumerable;
        // var e_indices =
        //     from i in Enumerable.Range(0, players.Length) where i != idx select i;
        // foreach (int i in e_indices) {
        for (int i = 0; i < players.Length; i++)
        {
            if (i == idx)
            {
                continue;
            }
            var enemy = GetPiece(i, to);
            if (enemy != null)
            {
                MarkAsTaken(i, to);
                break;
            }
        }
        Move(idx, from, to);
        // var enemy  = GetPiece(1, to);
        // if (enemy != null) {
        //   MarkAsTaken(1, to);
        // }
        // Move(0, from ,to);

        return;
    }
コード例 #2
0
ファイル: board_game.cs プロジェクト: konnya/board-game
 public void DispPieceInfo()
 {
     Console.WriteLine("     1   2   3   4   5   6");
     Console.WriteLine("   +---+---+---+---+---+---+");
     for (int x = 0; x < 6; x++)
     {
         String str = " " + CoordinateTrans.XtoA(x) + " |";
         for (int y = 0; y < 6; y++)
         {
             if (players[0].map[x, y] != null)
             {
                 var sym = GetPieceTypeSymbol(players[0].map[x, y]);
                 if ((x == 0 || x == 5) && (y == 0 || y == 5))
                 {
                     str += "(" + sym + ")|";
                 }
                 else
                 {
                     str += " " + sym + " |";
                 }
             }
             else if (players[1].map[x, y] != null)
             {
                 var sym = GetPieceTypeSymbol(players[1].map[x, y]);
                 if ((x == 0 || x == 5) && (y == 0 || y == 5))
                 {
                     str += "(" + sym + ")|";
                 }
                 else
                 {
                     str += " " + sym + " |";
                 }
             }
             else
             {
                 if ((x == 0 || x == 5) && (y == 0 || y == 5))
                 {
                     str += "( )|";
                 }
                 else
                 {
                     str += "   |";
                 }
             }
         }
         Console.WriteLine(str);
         Console.WriteLine("   +---+---+---+---+---+---+");
     }
 }
コード例 #3
0
ファイル: board_game.cs プロジェクト: konnya/board-game
    public void TryMovePiece(PlayerType ptype, Pos from, Direction dir)
    {
        var to = from + CalcDiff(ptype, dir);

        Console.WriteLine($"------> {from.x} {from.y}");
        Console.WriteLine($"------> {CalcDiff(ptype, dir).x} {CalcDiff(ptype, dir).y}");
        if (WithIn(to) == false)
        {
            var cx = CoordinateTrans.XtoA(to.x);
            var cy = CoordinateTrans.YtoA(to.y);
            throw new OutOfBoardAreaPositionException(
                      $"({cx},{cy}) or ({to.x},{to.y}) is out of board area ({BoardHeight},{BoardWidth}).");
        }

        TryMovePlayerPiece(ptype, from, to);
    }
コード例 #4
0
ファイル: board_game.cs プロジェクト: konnya/board-game
    public static void Main()
    {
        Console.WriteLine("--- Piece Test");
        Piece gst = new Piece(Piece.Type.Good);

        Console.WriteLine("--- Pos : x:{0} y:{1}", gst.X(), gst.Y());
        Console.WriteLine("--- Move Front");
        gst.Move(Direction.Front);
        Console.WriteLine("--- Pos : x:{0} y:{1}", gst.X(), gst.Y());
        Console.WriteLine("--- Move Right");
        gst.Move(Direction.Right);
        Console.WriteLine("--- Pos : x:{0} y:{1}", gst.X(), gst.Y());

        Board brd = new Board();

        for (int i = 0; i < 4; i++)
        {
            Piece gst1 = new Piece(Piece.Type.Good);
            brd.LocatePiece(PlayerType.Player1, 4, i + 1, gst1);
            Piece gst2 = new Piece(Piece.Type.Bad);
            brd.LocatePiece(PlayerType.Player1, 5, i + 1, gst2);
        }
        for (int i = 0; i < 4; i++)
        {
            Piece gst1 = new Piece(Piece.Type.Good);
            brd.LocatePiece(PlayerType.Player2, 0, i + 1, gst1);
            Piece gst2 = new Piece(Piece.Type.Bad);
            brd.LocatePiece(PlayerType.Player2, 1, i + 1, gst2);
        }
        brd.test();
        brd.DispCurrentMap();

        var commands = new Dictionary <string, ConsoleCommand>();

        commands.Add("args", delegate(string[] args, Board board) {
            Console.WriteLine(" Command : {0}", args[0]);
            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine(" {0} : {1}", i, args[i]);
            }
            return(0);
        });
        commands.Add("info", delegate(string[] args, Board board) {
            board.DispPieceInfo();
            return(0);
        });
        commands.Add("start", delegate(string[] args, Board board) {
            var mgr = new GameManager();
            mgr.AssignBoard(ref board);
            mgr.StartGame();

            return(0);
        });
        commands.Add("movep", delegate(string[] args, Board board) {
            if (args.Length < 4)
            {
                Console.WriteLine(" {0} fromA-F from1-6 <Front,Back,Left,Right> ", args[0]);
                return(22);
            }

            var x    = CoordinateTrans.AtoX(args[1][0]);
            var y    = CoordinateTrans.AtoY(args[2][0]);
            var from = new Pos(x, y);
            var dir  = CoordinateTrans.AtoDir(args[3][0]);

            board.TryMovePiece(PlayerType.Player1, from, dir);

            return(0);
        });
        commands.Add("movee", delegate(string[] args, Board board) {
            if (args.Length < 4)
            {
                Console.WriteLine(" {0} fromA-F from1-6 <Front,Back,Left,Right> ", args[0]);
                return(22);
            }

            var x    = CoordinateTrans.AtoX(args[1][0]);
            var y    = CoordinateTrans.AtoY(args[2][0]);
            var from = new Pos(x, y);
            var dir  = CoordinateTrans.AtoDir(args[3][0]);

            board.TryMovePiece(PlayerType.Player2, from, dir);

            return(0);
        });

        do
        {
            Console.Write("$ ");
            var args = GetUserInput();
            Console.WriteLine(" --> <{0}> ", args[0]);

            if (args[0] == "exit")
            {
                Console.WriteLine("good bye!");
                break;
            }
            if (args[0] == "help")
            {
                Console.WriteLine(" command name : discription");
                foreach (var name in commands.Keys)
                {
                    Console.WriteLine(" {0} : {1}", name, "n/a");
                }
                continue;
            }

            try {
                var cmd = commands[args[0]];
                try {
                    var eno = cmd(args, brd);
                    Console.WriteLine(" Command {0} --> {1}", args, eno);
                } catch (System.Exception e) {
                    Console.WriteLine(" Command {0} throw exception", args);
                    Console.WriteLine(e);
                }
            } catch (System.Collections.Generic.KeyNotFoundException) {
                Console.WriteLine(" unknown command : {0}", args[0]);
            }

            brd.DispCurrentMap();
        }while(true);
    }
コード例 #5
0
ファイル: board_game.cs プロジェクト: konnya/board-game
    public void DispCurrentMap()
    {
        Console.WriteLine("     1   2   3   4   5   6");
        Console.WriteLine("   +---+---+---+---+---+---+");
        for (int x = 0; x < 6; x++)
        {
            String str = " " + CoordinateTrans.XtoA(x) + " |";
            for (int y = 0; y < 6; y++)
            {
                if (players[0].map[x, y] != null)
                {
                    // if(_pmap[x, y] != null) {
                    if ((x == 0 || x == 5) && (y == 0 || y == 5))
                    {
                        str += "(^)|";
                    }
                    else
                    {
                        str += " ^ |";
                    }
                    // } else if(_emap[x, y] != null) {
                }
                else if (players[1].map[x, y] != null)
                {
                    if ((x == 0 || x == 5) && (y == 0 || y == 5))
                    {
                        str += "(v)|";
                    }
                    else
                    {
                        str += " v |";
                    }
                }
                else
                {
                    if ((x == 0 || x == 5) && (y == 0 || y == 5))
                    {
                        str += "( )|";
                    }
                    else
                    {
                        str += "   |";
                    }
                }
            }
            Console.WriteLine(str);
            Console.WriteLine("   +---+---+---+---+---+---+");
        }

        Console.WriteLine("");

        Console.WriteLine("Taken Pieces");
        Console.WriteLine(" Player1");
        Console.Write("    Good :");
        foreach (Piece gs in players[0].taken_goods)
        {
            Console.Write(" v");
        }
        Console.Write(Environment.NewLine);
        Console.Write("    Bad  :");
        foreach (Piece gs in players[0].taken_bads)
        {
            Console.Write(" v");
        }
        Console.Write(Environment.NewLine);

        Console.WriteLine(" Player2 :");
        Console.Write("    Good :");
        foreach (Piece gs in players[1].taken_goods)
        {
            Console.Write(" ^");
        }
        Console.Write(Environment.NewLine);
        Console.Write("    Bad  :");
        foreach (Piece gs in players[1].taken_bads)
        {
            Console.Write(" ^");
        }
        Console.Write(Environment.NewLine);
    }