帮助C#上的主游戏循环 [英] Help with main game loop on C#

查看:41
本文介绍了帮助C#上的主游戏循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧所以这是我新的和改进的问题,我需要让这件作品从9,9向后移动到9,8等等,使用随机数发生器(1,6)模拟骰子将棋子向上移动



这是我的代码到目前为止



ok so here's my new and improved question i need to make the piece move from 9,9 backwards to 9,8 and so on using a random number generator (1,6) to simulate a dice moving the piece up the board

here is my code so far

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

namespace SnakesNLadders_MS4403
{
    class Program
    {
        static char[,] CH_AR_Board = new char[10, 10];
        static string player1;
        static string player2;

        static void Main(string[] args)
        {

            Introduction();
            DisplayBoard();
            Console.ReadKey();
        }

        static void DisplayBoard()
        {
            //display column numbers for board
            for (int i = 0; i < CH_AR_Board.GetLength(1); i++)
            {
                //CH_AR_Board.GetLength(0) gives the size of the second dimension of the array
                if(i == 0)
                {
                    //at the beginning i need to add a bit more space than usual
                    Console.Write("   " + i + " ");
                }
                else
                {
                    //display value of i with spaces
                    Console.Write(" " + i + " ");
                }
            }
            //skip a line
            Console.WriteLine();

            //nested for loop to iterate around 2D array
            // outer for loop deals with rows (so i)
            // inner for loop deals with columns (so j)

            for (int i = 0; i < CH_AR_Board.GetLength(0); i++)
            {
                //CH_AR_Board.GetLength(0) gives the size of the first dimension of the Array
                //Display row numbers for board
                Console.Write(i + " ");
                for(int j = 0; j < CH_AR_Board.GetLength(1); j++)
                {
                    //display value in board space using counters as indexes
                    Console.Write("[" + CH_AR_Board[i, j] + "]");
                }
                Console.WriteLine();
            }
        }

        static void Introduction()
        {
            Console.Title = "Snakes And Ladders";
            Console.WriteLine("Welcome to Snakes And Ladders, A Simple Game\n");
            Console.WriteLine("Press Any Key To Continue");
            Console.ReadLine();
            Console.Clear();

            Console.WriteLine("Player 1 Enter Your Name ");
            player1 = Console.ReadLine();
            Console.WriteLine("Payer 2 Enter Your Name ");
            player2 = Console.ReadLine();
            Console.Clear();
        }
    }
}







谢谢



我尝试了什么:



i已经尝试了




thank you

What I have tried:

i have tried

static void PlacePiece(int row, int col, string player)
        {
            if (player == "Player1")
            {
                CH_AR_Board[row, col] = Convert.ToChar(1);
            }
            else
            {
                CH_AR_Board[row, col] = Convert.ToChar(5);
            }

        }





它放置了一块但我不知道你是怎么回事将使用随机生成器移动它



it places the piece but i have no idea how u=i would use the random generator to move it

推荐答案

请参阅Matt T Heffron和我对问题评论的评论。基本上,这就是答案。



代码的骨架应该类似于

Please see the comments to the comments to the question, by Matt T Heffron and myself. Essentially, this is the answer.

The skeleton of your code should be something like
//...

static void Main() {
   Game game = new Game();
   Game.Introduction();
   Game.DisplayBoard();
   while (!Game.Finished) {
      Game.Step();
      // ...
   }    
}

在这里,你可以看到一个建议:使用OOP,定义一些非静态类游戏;不要试图在一个班级中发展。这不是必需的,但会让您的工作更舒适。主循环通常也应该在同一个游戏类中;我已单独展示它以显示邮件循环的外观。



更多时刻:



请勿使用 Console.ReadKey(); 使用 Console.ReadKey(true)。这是您需要的所有控制台输入。你根本不需要 Console.WriteLine Console.Read * (除了介绍)。你很快就会看到它,所以最好从一开始就理解它。您需要输出到屏幕位置,因此您需要控制台成员 Cursor * Window * 等等。请参阅:控制台类(系统) [< a href =https://msdn.microsoft.com/en-us/library/system.console%28v=vs.110%29.aspx\"target =_ blanktitle =New Window> ^ ]。



您将不得不尝试使用这些成员来实现伪图形输出。但话说:即使是控制台上的简单棋盘游戏看起来并不严肃。 GUI应用程序很可能更容易(当然看起来和表现更好),特别是对于WPF及其 Canvas 类。但是,如果你想练习控制台,这可能是一个有趣的练习。



-SA

Here, you can see one suggestion: use OOP, define some non-static class Game; don't try to develop all in one class. This is not required, but would make your work more comfortable for you. The main loop is usually also should be in the same game class; I've shown it separately just to show the look of the mail loop.

Some more moments:

Don't use Console.ReadKey(); use Console.ReadKey(true) instead. This is all the console input you would need. You won't really need Console.WriteLine or Console.Read* at all (except, perhaps, introduction). You will soon see it yourself, so it's better to understand it from the very beginning. You will need output to a location of screen, so you will need Console members Cursor* and Window*, and some more. Please see: Console Class (System)[^].

You will have to experiment with those members to achieve pseudo-graphical output. But that said: even a simple board game on a console doesn't look serious. It's very likely that the GUI application would be easier (and of course look and behave much better), especially with WPF and its Canvas class. However, if you want to practice with console, it could be an interesting exercise.

—SA


这篇关于帮助C#上的主游戏循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆