7

我有一些现有的 C# 代码,用于非常非常简单的 RogueLike 引擎。这是故意天真,因为我试图尽可能简单地做最少的数量。它所做的只是使用箭头键和 System.Console 在硬编码地图周围移动 @ 符号:

//define the map
var map = new List<string>{
  "                                        ",
  "                                        ",
  "                                        ",
  "                                        ",
  "    ###############################     ",
  "    #                             #     ",
  "    #         ######              #     ",
  "    #         #    #              #     ",
  "    #### #### #    #              #     ",
  "       # #  # #    #              #     ",
  "       # #  # #    #              #     ",
  "    #### #### ######              #     ",
  "    #              =              #     ",
  "    #              =              #     ",
  "    ###############################     ",
  "                                        ",
  "                                        ",
  "                                        ",
  "                                        ",
  "                                        "
};

//set initial player position on the map
var playerX = 8;
var playerY = 6;

//clear the console
Console.Clear();

//send each row of the map to the Console
map.ForEach( Console.WriteLine );

//create an empty ConsoleKeyInfo for storing the last key pressed
var keyInfo = new ConsoleKeyInfo( );

//keep processing key presses until the player wants to quit
while ( keyInfo.Key != ConsoleKey.Q ) {
  //store the player's current location
  var oldX = playerX;
  var oldY = playerY;

  //change the player's location if they pressed an arrow key
  switch ( keyInfo.Key ) {
    case ConsoleKey.UpArrow:
      playerY--;
      break;
    case ConsoleKey.DownArrow:
      playerY++;
      break;
    case ConsoleKey.LeftArrow:
      playerX--;
      break;
    case ConsoleKey.RightArrow:
      playerX++;
      break;
  }

  //check if the square that the player is trying to move to is empty
  if( map[ playerY ][ playerX ] == ' ' ) {
    //ok it was empty, clear the square they were standing on before
    Console.SetCursorPosition( oldX, oldY );
    Console.Write( ' ' );
    //now draw them at the new square
    Console.SetCursorPosition( playerX, playerY );
    Console.Write( '@' );
  } else {
    //they can't move there, change their location back to the old location
    playerX = oldX;
    playerY = oldY;
  }

  //wait for them to press a key and store it in keyInfo
  keyInfo = Console.ReadKey( true );
}

我在 F# 中玩弄它,最初我试图使用函数概念来编写它,但结果我有点过头了,所以我做了一个直接的移植——它不是真正的 F# 程序(尽管它编译并运行)它是一个用 F# 语法编写的过程程序:

open System

//define the map
let map = [ "                                        ";
            "                                        ";
            "                                        ";
            "                                        ";
            "    ###############################     ";
            "    #                             #     ";
            "    #         ######              #     ";
            "    #         #    #              #     ";
            "    #### #### #    #              #     ";
            "       # #  # #    #              #     ";
            "       # #  # #    #              #     ";
            "    #### #### ######              #     ";
            "    #              =              #     ";
            "    #              =              #     ";
            "    ###############################     ";
            "                                        ";
            "                                        ";
            "                                        ";
            "                                        ";
            "                                        " ]

//set initial player position on the map
let mutable playerX = 8
let mutable playerY = 6

//clear the console
Console.Clear()

//send each row of the map to the Console
map |> Seq.iter (printfn "%s")

//create an empty ConsoleKeyInfo for storing the last key pressed
let mutable keyInfo = ConsoleKeyInfo()

//keep processing key presses until the player wants to quit
while not ( keyInfo.Key = ConsoleKey.Q ) do
    //store the player's current location
    let mutable oldX = playerX
    let mutable oldY = playerY

    //change the player's location if they pressed an arrow key
    if keyInfo.Key = ConsoleKey.UpArrow then
        playerY <- playerY - 1
    else if keyInfo.Key = ConsoleKey.DownArrow then
        playerY <- playerY + 1
    else if keyInfo.Key = ConsoleKey.LeftArrow then
        playerX <- playerX - 1
    else if keyInfo.Key = ConsoleKey.RightArrow then
        playerX <- playerX + 1

    //check if the square that the player is trying to move to is empty
    if map.Item( playerY ).Chars( playerX ) = ' ' then
        //ok it was empty, clear the square they were standing on
        Console.SetCursorPosition( oldX, oldY )
        Console.Write( ' ' )
        //now draw them at the new square 
        Console.SetCursorPosition( playerX, playerY )
        Console.Write( '@' )
    else
        //they can't move there, change their location back to the old location
        playerX <- oldX
        playerY <- oldY

    //wait for them to press a key and store it in keyInfo
    keyInfo <- Console.ReadKey( true )

所以我的问题是,为了更实用地重写它,我需要学习什么,你能给我一些提示,一个模糊的概述之类的东西。

我宁愿朝正确的方向推进,而不是仅仅看一些代码,但如果这是你向我解释它的最简单方法,那么很好,但在这种情况下,你能否也解释一下“为什么”而不是“如何”的吗?

4

4 回答 4

9

一般来说,游戏编程将测试您管理复杂性的能力。我发现函数式编程鼓励你将解决的问题分解成更小的部分。

您要做的第一件事是通过分离所有不同的关注点将您的脚本变成一堆函数。我知道这听起来很傻,但是这样做会使代码更具功能性(双关语)。您主要关心的是状态管理。我使用记录来管理位置状态和元组来管理运行状态。随着您的代码变得更高级,您将需要对象来干净地管理状态。

尝试在这个游戏中添加更多内容,并随着功能的发展不断分解功能。最终,您将需要对象来管理所有功能。

在游戏编程笔记上,不要将状态更改为其他状态,如果它未通过某些测试,则将其更改回来。你想要最小的状态变化。因此,例如,我在下面计算newPosition,然后仅在playerPosition未来位置通过时才更改。

open System

// use a third party vector class for 2D and 3D positions
// or write your own for pratice
type Pos = {x: int; y: int} 
    with
    static member (+) (a, b) =
        {x = a.x + b.x; y = a.y + b.y}

let drawBoard map =
    //clear the console
    Console.Clear()
    //send each row of the map to the Console
    map |> List.iter (printfn "%s")

let movePlayer (keyInfo : ConsoleKeyInfo) =
    match keyInfo.Key with
    | ConsoleKey.UpArrow -> {x = 0; y = -1}
    | ConsoleKey.DownArrow -> {x = 0; y = 1}
    | ConsoleKey.LeftArrow -> {x = -1; y = 0}
    | ConsoleKey.RightArrow  -> {x = 1; y = 0}
    | _ -> {x = 0; y = 0}

let validPosition (map:string list) position =
    map.Item(position.y).Chars(position.x) = ' '

//clear the square player was standing on
let clearPlayer position =
    Console.SetCursorPosition(position.x, position.y)
    Console.Write( ' ' )

//draw the square player is standing on
let drawPlayer position =
    Console.SetCursorPosition(position.x, position.y)
    Console.Write( '@' )

let takeTurn map playerPosition =
    let keyInfo = Console.ReadKey true
    // check to see if player wants to keep playing
    let keepPlaying = keyInfo.Key <> ConsoleKey.Q
    // get player movement from user input
    let movement = movePlayer keyInfo
    // calculate the players new position
    let newPosition = playerPosition + movement
    // check for valid move
    let validMove = newPosition |> validPosition map
    // update drawing if move was valid
    if validMove then
        clearPlayer playerPosition
        drawPlayer newPosition
    // return state
    if validMove then
        keepPlaying, newPosition
    else
        keepPlaying, playerPosition

// main game loop
let rec gameRun map playerPosition =
    let keepPlaying, newPosition = playerPosition |> takeTurn map 
    if keepPlaying then
        gameRun map newPosition

// setup game
let startGame map playerPosition =
    drawBoard map
    drawPlayer playerPosition
    gameRun map playerPosition


//define the map
let map = [ "                                        ";
            "                                        ";
            "                                        ";
            "                                        ";
            "    ###############################     ";
            "    #                             #     ";
            "    #         ######              #     ";
            "    #         #    #              #     ";
            "    #### #### #    #              #     ";
            "       # #  # #    #              #     ";
            "       # #  # #    #              #     ";
            "    #### #### ######              #     ";
            "    #              =              #     ";
            "    #              =              #     ";
            "    ###############################     ";
            "                                        ";
            "                                        ";
            "                                        ";
            "                                        ";
            "                                        " ]

//initial player position on the map
let playerPosition = {x = 8; y = 6}

startGame map playerPosition
于 2010-12-21T17:38:41.250 回答
4

这是一个不错的小游戏:-)。在函数式编程中,您希望避免使用可变状态(正如其他人指出的那样),并且您还希望将游戏的核心编写为没有任何副作用的函数(例如从控制台读取和写作)。

游戏的关键部分是控制位置的功能。您可以重构您的代码以具有具有类型签名的函数:

val getNextPosition : (int * int) -> ConsoleKey -> option<int * int>

None如果游戏应该退出,该函数返回。否则,它将返回符号的新位置Some(posX, posY)posX位置。通过进行更改,您将获得一个不错的功能核心,并且该功能也易于测试(因为它总是为相同的输入返回相同的结果)。posY@getNextPosition

要使用该函数,最好的选择是使用递归编写循环。main 函数的结构如下所示:

let rec playing pos =
  match getNextPosition pos (Console.ReadKey()) with
  | None -> () // Quit the game
  | Some(newPos) ->
     // This function redraws the screen (this is a side-effect,
     // but it is localized to a single function)
     redrawScreen pos newPos
     playing newPos
于 2010-12-21T12:55:37.453 回答
2

作为一款游戏,并且使用控制台,这里存在固有的状态和副作用。但是你要做的关键是消除那些可变的。使用递归循环而不是 while 循环将帮助您做到这一点,因为您可以将您的状态作为参数传递给每个递归调用。除此之外,我可以看到在这里利用 F# 功能的主要内容是使用模式匹配而不是 if/then 语句和开关,尽管这主要是美学上的改进。

于 2010-12-21T03:32:22.503 回答
1

我会尽量避免过于具体——如果我最终在另一个方向上走得太远而且这太模糊了,请告诉我,我会试着改进一下。

在制作具有某种状态的函数式程序时,您要实现的基本机制类似于:

(currentState, input) => newState

然后您可以围绕它编写一个小包装器来处理获取输入和绘图输出。

于 2010-12-21T03:32:07.493 回答