星 辰 · 第 二 条 约 定

类与类之间的关系图详见http://www.cnblogs.com/ywqu/archive/2009/12/06/1618184.html

代码要实现的功能(闲的蛋疼 就瞎搞了一下)

1、能够上下左右移动,且无法出墙

2、地图中会随机出现五个东西,你要吃掉这五个东西,如果你用的步数是最少,则分数加100,否则生命减1.

打代码前的构思(构思会与实际代码出现偏差)

1、MAP类(地图)

     包含的属性:数组Map[,] ,常量mapsize(地图尺寸)

     包含的方法:draw(绘制6×6地图) print(打印6……6问题)

3、Pos类(位置)

     包含的属性:玩家位置playerpos,五个东西的位置pos[],静态变量isEnd(初始值为5)

     包含的方法:suiji(随机五个东西的位置和玩家位置)

                        

5、Function类(功能)

     包含的属性:来自Pos类的玩家位置和五个东西的位置,玩家步数playermove,最少步数leastmove

     包含的方法:PlayerMove(接收上下左右后移动位置且记录步数),LeastMove类(计算最少步数)

                        IsLeast(判断是否是最少步数,如果是则score+=100,如果不是life-=1)

                        IsEnd(利用isend的值判断五个东西是否被吃完)

                        inicialize(调用Pos类的两个方法,对life time score name isend playermove leastmove重新赋值)

6、ESC类(退出)

     包含的属性:无

     包含的方法:Esc(按下Esc的时候或生命值为0的时候 退出程序)

7、Main方法(实时调用time方法,life方法,Name方法)

     1、实例化Map类,生成对象map,调用draw方法,绘制地图

          实例化GUI类,生成对象gui

     2、实例化Function类。生成对象fun,调用inicialize方法,初始化游戏

     3、循环1

          循环退出条件:调用Esc方法

          循环体:循环2、调用fun中的LeastMove方法和IsLeast、

     4、循环2

          循环退出条件:对象fun中IsEnd 的返回值为真的时候

          循环体:调用fun中的PlayerMove方法

郁闷,先交个能上下左右移动的未成品,继续修改中(PS:坐标问题好烦)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace zuoye
{
    class Map
    {
        private const int mapsize = 20;
        public static int life=3;
        public static int score=0;
        public static string[,] map = new string[mapsize, mapsize];
        private int row;
        private int col;
        public void draw()
        {
            for (row = 0; row < 8; row++)
            {
                for (col = 0; col <8; col++)
                {
                    if (row == 0 || row == 7|| col == 0 || col == 7)
                    {
                        map[row, col] = "";
                    }
                    else
                    {
                        map[row, col] = "  ";
                    }
                }
            }
            map[0, 9] = "life:";
            map[0, 10] = life.ToString();
            map[1, 9] = "score:";
            map[1, 10] = score.ToString();
        }
        public void print()
        {
            for (row = 0; row < mapsize; row++)
            {
                for (col = 0; col < mapsize; col++)
                {
                    if (col != mapsize-1)
                    {
                        Console.Write(map[row, col]);
                    }
                    else 
                    {
                        Console.WriteLine(map[row, col]);
                    }
                }
                
            }
        }
    }
    class Pos
    {
        private int row;
        private int col;
        private int i;
        private int h = 0;
        public static int[] r = new int[5];
        public static int[] c = new int[5];
        public static int playerrow;
        public static int playercol;
        Random rd = new Random();
        public void suiji()
        {
            for (i = 5; i > 0; i--)
            {
                while (true)
                {
                    row = rd.Next(1, 7);
                    col = rd.Next(1, 7);

                    if (Map.map[row, col] == "11")
                    {
                        continue;
                    }
                    else
                    {
                        Map.map[row, col] = "11";
                        r[h] = row;
                        c[h] = col;
                        h++;
                        break;
                    }
                }
            }
            while (true)
            {
                row = rd.Next(1, 7);
                col = rd.Next(1, 7);

                if (Map.map[row, col] == "11")
                {
                    continue;
                }
                else
                {
                    Map.map[row, col] = "";
                    playerrow = row;
                    playercol = col;
                    break;
                }
            }
        }
        class Esc
        {
            public void ESC()
            {
                ConsoleKeyInfo info = Console.ReadKey();
                if (Map.life == 0 || info.Key == ConsoleKey.Escape)
                {
                    Environment.Exit(0);
                }
            }
        }
        class Function : Pos
        {
            public static int playerMove;
            public static int leastMove;
            public static int isend;
            public void inicialize()
            {
                playerMove = 0;
                leastMove = 0;
                isend = 5;
                Map.life = 3;
                Map.score = 0;
                suiji();
            }
            public void PlayerMove()
            {
                ConsoleKeyInfo info = Console.ReadKey();
                    switch (info.Key)
                    {
                        case ConsoleKey.UpArrow:
                            {
                                if (Map.map[playerrow - 1, playercol] != "")
                                {
                                    Map.map[playerrow, playercol] = "  ";
                                    Console.SetCursorPosition(2 * playercol, playerrow);
                                    Console.Write("  ");
                                    Console.SetCursorPosition(0, 11);
                                    if (Map.map[playerrow - 1, playercol] == "11")
                                    {
                                        isend = isend - 1;
                                    }
                                    Map.map[playerrow - 1, playercol] = "";
                                    Console.SetCursorPosition(2 * playercol , playerrow-1);
                                    Console.Write(Map.map[playerrow-1, playercol ]);
                                    Console.SetCursorPosition(0, 11);
                                    playerMove += 1;
                                    playerrow = playerrow - 1;
                                    playercol = playercol;
                                }
                                break;
                            }
                        case ConsoleKey.LeftArrow:
                            {
                                if (Map.map[playerrow, playercol - 1] != "")
                                {
                                    Map.map[playerrow, playercol] = "  ";
                                    Console.SetCursorPosition(2 * playercol, playerrow);
                                    Console.Write("  ");
                                    Console.SetCursorPosition(0, 11);
                                    if (Map.map[playerrow, playercol-1] == "11")
                                    {
                                        isend = isend - 1;
                                    }
                                    Map.map[playerrow, playercol-1] = "";
                                    Console.SetCursorPosition(2 * (playercol-1), playerrow );
                                    Console.Write(Map.map[playerrow, playercol - 1]);
                                    Console.SetCursorPosition(0, 11);
                                    playerMove += 1;
                                    playerrow = playerrow;
                                    playercol = playercol-1;
                                }
                                break;
                            }
                        case ConsoleKey.RightArrow:
                            {
                                if (Map.map[playerrow, playercol + 1] != "")
                                {
                                    Map.map[playerrow, playercol] = "  ";
                                    Console.SetCursorPosition(2 * playercol, playerrow);
                                    Console.Write("  ");
                                    Console.SetCursorPosition(0, 11);
                                    if (Map.map[playerrow + 1, playercol] == "11")
                                    {
                                        isend = isend - 1;
                                    }
                                    Map.map[playerrow, playercol + 1] = "";
                                    Console.SetCursorPosition(2 * (playercol + 1), playerrow);
                                    Console.Write(Map.map[playerrow, playercol+1] );
                                    Console.SetCursorPosition(0, 11);
                                    playerMove += 1;
                                    playerrow = playerrow;
                                    playercol = playercol+1;
                                }
                                break;
                            }
                        case ConsoleKey.DownArrow:
                            {
                                if (Map.map[playerrow + 1, playercol] != "")
                                {
                                    Map.map[playerrow, playercol] = "  ";
                                    Console.SetCursorPosition(2 * playercol, playerrow);
                                    Console.Write("  ");
                                    Console.SetCursorPosition(0, 11);
                                    if (Map.map[playerrow + 1, playercol] == "11")
                                    {
                                        isend = isend - 1;
                                    }
                                    Map.map[playerrow+1, playercol] = "";
                                    Console.SetCursorPosition(2 * playercol, playerrow+1);
                                    Console.Write(Map.map[playerrow+1, playercol]);
                                    Console.SetCursorPosition(0, 11);
                                    playerMove += 1;
                                    playerrow = playerrow + 1;
                                    playercol = playercol;
                                }
                                break;
                            }
                    
                }
            }

            public void LeastMove() //因为不能斜着走 所以可以用贪心算法
            {
                Array.Sort(Pos.r);
                Array.Sort(Pos.c);
                for (int i = 0; i < 5; i++)
                {
                    leastMove += Math.Abs(r[i] - r[5 / 2]) + Math.Abs(c[i] - c[5 / 2]);
                }
            }
            public bool IsLeast()
            {
                if (playerMove == leastMove)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            public bool IsEnd()
            {
                if (isend == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

        }
        class Program
        {
            static void Main(string[] args)
            {
                Map map = new Map();
                map.draw();
                Function fun = new Function();
                fun.inicialize();
                map.print();
                while (true)
                {
                    Esc esc = new Esc();
                    esc.ESC();

                    while (!fun.IsEnd())
                    {
                        fun.PlayerMove();
                    }
                    fun.LeastMove();
                    if (fun.IsLeast())
                    {
                        Map.score += 100;
                    }
                    else
                    {
                       Map.life -= 1;
                    }
                }

            }
        }
    }
}

遇到的问题:

最大的问题就是越到开学越想浪。。。。

坐标问题也很烦啊。。。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
namespace zuoye{    class Map    {        private const int mapsize = 20;        public static int life=3;        public static int score=0;        public static string[,] map = new string[mapsize, mapsize];        private int row;        private int col;        public void draw()        {            for (row = 0; row < 8; row++)            {                for (col = 0; col <8; col++)                {                    if (row == 0 || row == 7|| col == 0 || col == 7)                    {                        map[row, col] = "█";                    }                    else                    {                        map[row, col] = "  ";                    }                }            }            map[0, 9] = "life:";            map[0, 10] = life.ToString();            map[1, 9] = "score:";            map[1, 10] = score.ToString();        }        public void print()        {            for (row = 0; row < mapsize; row++)            {                for (col = 0; col < mapsize; col++)                {                    if (col != mapsize-1)                    {                        Console.Write(map[row, col]);                    }                    else                     {                        Console.WriteLine(map[row, col]);                    }                }                            }        }    }    class Pos    {        private int row;        private int col;        private int i;        private int h = 0;        public static int[] r = new int[5];        public static int[] c = new int[5];        public static int playerrow;        public static int playercol;        Random rd = new Random();        public void suiji()        {            for (i = 5; i > 0; i--)            {                while (true)                {                    row = rd.Next(1, 7);                    col = rd.Next(1, 7);
                    if (Map.map[row, col] == "11")                    {                        continue;                    }                    else                    {                        Map.map[row, col] = "11";                        r[h] = row;                        c[h] = col;                        h++;                        break;                    }                }            }            while (true)            {                row = rd.Next(1, 7);                col = rd.Next(1, 7);
                if (Map.map[row, col] == "11")                {                    continue;                }                else                {                    Map.map[row, col] = "★";                    playerrow = row;                    playercol = col;                    break;                }            }        }        class Esc        {            public void ESC()            {                ConsoleKeyInfo info = Console.ReadKey();                if (Map.life == 0 || info.Key == ConsoleKey.Escape)                {                    Environment.Exit(0);                }            }        }        class Function : Pos        {            public static int playerMove;            public static int leastMove;            public static int isend;            public void inicialize()            {                playerMove = 0;                leastMove = 0;                isend = 5;                Map.life = 3;                Map.score = 0;                suiji();            }            public void PlayerMove()            {                ConsoleKeyInfo info = Console.ReadKey();                    switch (info.Key)                    {                        case ConsoleKey.UpArrow:                            {                                if (Map.map[playerrow - 1, playercol] != "█")                                {                                    Map.map[playerrow, playercol] = "  ";                                    Console.SetCursorPosition(2 * playercol, playerrow);                                    Console.Write("  ");                                    Console.SetCursorPosition(0, 11);                                    if (Map.map[playerrow - 1, playercol] == "11")                                    {                                        isend = isend - 1;                                    }                                    Map.map[playerrow - 1, playercol] = "★";                                    Console.SetCursorPosition(2 * playercol , playerrow-1);                                    Console.Write(Map.map[playerrow-1, playercol ]);                                    Console.SetCursorPosition(0, 11);                                    playerMove += 1;                                    playerrow = playerrow - 1;                                    playercol = playercol;                                }                                break;                            }                        case ConsoleKey.LeftArrow:                            {                                if (Map.map[playerrow, playercol - 1] != "█")                                {                                    Map.map[playerrow, playercol] = "  ";                                    Console.SetCursorPosition(2 * playercol, playerrow);                                    Console.Write("  ");                                    Console.SetCursorPosition(0, 11);                                    if (Map.map[playerrow, playercol-1] == "11")                                    {                                        isend = isend - 1;                                    }                                    Map.map[playerrow, playercol-1] = "★";                                    Console.SetCursorPosition(2 * (playercol-1), playerrow );                                    Console.Write(Map.map[playerrow, playercol - 1]);                                    Console.SetCursorPosition(0, 11);                                    playerMove += 1;                                    playerrow = playerrow;                                    playercol = playercol-1;                                }                                break;                            }                        case ConsoleKey.RightArrow:                            {                                if (Map.map[playerrow, playercol + 1] != "█")                                {                                    Map.map[playerrow, playercol] = "  ";                                    Console.SetCursorPosition(2 * playercol, playerrow);                                    Console.Write("  ");                                    Console.SetCursorPosition(0, 11);                                    if (Map.map[playerrow + 1, playercol] == "11")                                    {                                        isend = isend - 1;                                    }                                    Map.map[playerrow, playercol + 1] = "★";                                    Console.SetCursorPosition(2 * (playercol + 1), playerrow);                                    Console.Write(Map.map[playerrow, playercol+1] );                                    Console.SetCursorPosition(0, 11);                                    playerMove += 1;                                    playerrow = playerrow;                                    playercol = playercol+1;                                }                                break;                            }                        case ConsoleKey.DownArrow:                            {                                if (Map.map[playerrow + 1, playercol] != "█")                                {                                    Map.map[playerrow, playercol] = "  ";                                    Console.SetCursorPosition(2 * playercol, playerrow);                                    Console.Write("  ");                                    Console.SetCursorPosition(0, 11);                                    if (Map.map[playerrow + 1, playercol] == "11")                                    {                                        isend = isend - 1;                                    }                                    Map.map[playerrow+1, playercol] = "★";                                    Console.SetCursorPosition(2 * playercol, playerrow+1);                                    Console.Write(Map.map[playerrow+1, playercol]);                                    Console.SetCursorPosition(0, 11);                                    playerMove += 1;                                    playerrow = playerrow + 1;                                    playercol = playercol;                                }                                break;                            }                                    }            }
            public void LeastMove() //因为不能斜着走 所以可以用贪心算法            {                Array.Sort(Pos.r);                Array.Sort(Pos.c);                for (int i = 0; i < 5; i++)                {                    leastMove += Math.Abs(r[i] - r[5 / 2]) + Math.Abs(c[i] - c[5 / 2]);                }            }            public bool IsLeast()            {                if (playerMove == leastMove)                {                    return true;                }                else                {                    return false;                }            }            public bool IsEnd()            {                if (isend == 0)                {                    return true;                }                else                {                    return false;                }            }
        }        class Program        {            static void Main(string[] args)            {                Map map = new Map();                map.draw();                Function fun = new Function();                fun.inicialize();                map.print();                while (true)                {                    Esc esc = new Esc();                    esc.ESC();
                    while (!fun.IsEnd())                    {                        fun.PlayerMove();                    }                    fun.LeastMove();                    if (fun.IsLeast())                    {                        Map.score += 100;                    }                    else                    {                       Map.life -= 1;                    }                }
            }        }    }}

原文地址:https://www.cnblogs.com/wshyj/p/6384589.html