ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

proj0. 2048 of CS61B of UCB

2022-01-31 02:31:53  阅读:465  来源: 互联网

标签:value proj0 int CS61B 2048 board size col row


Intro


在这个项目中, 我们需要解决四个任务. 我会谈到我是怎么处理这四个问题的.

Task 1. public static boolean emptySpaceExists(Board b)


这个任务要求我们检查 2048 的是否存在空格子. 解决办法很容易想到: 遍历一边所有的格子看看是否有空的即可. 查看其他文件就可以知道要用到什么 API 来完成这个功能. 比如我们可以通过 board.tile(col, row) 来获取对应的格子. 通过返回的状态就可以知道是不是空的.

/** Returns true if at least one space on the Board is empty.
 *  Empty spaces are stored as null.
 * */
public static boolean emptySpaceExists(Board b) {
    int size = b.size();
    for (int col = 0; col < size; col++) {
        for (int row = 0; row < size; row++) {
            if (b.tile(col, row) == null) {
                return true;
            }
        }
    }
    return false;
}

Task 2. public static boolean maxTileExists(Board b)


这个其实跟任务一很类似, 只是此时我们不是要判断格子是否为空, 我们是要判断格子的值是否为一个特定的值, 这其实还是很容易想到的. 因为我们还是通过遍历来解决这个问题, 只是判断的条件变为: t.values() == MAX_PIECE.

/**
 * Returns true if any tile is equal to the maximum valid value.
 * Maximum valid value is given by MAX_PIECE. Note that
 * given a Tile object t, we get its value with t.value().
 */
public static boolean maxTileExists(Board b) {
    int size = b.size();
    for (int col = 0; col < size; col++) {
        for (int row = 0; row < size; row++) {
            Tile t = b.tile(col, row);
            // only when t != null should we check t.value()
            if (t != null && t.value() == MAX_PIECE) {
                return true;
            }
        }
    }
    return false;
}

Task 3. public static boolean atLeastOneMoveExists(Board b)


确实, 这个问题对新手来说会比较有挑战性. 问题的关键在于如何判断出 2048 的方格上是否能继续玩下去, 主要有两种情形

  1. 2048 的所有方格中至少有一个是空的. 这个可以用前面实现的 emptySpaceExists()
  2. 如果移动的方向上有相邻而且值一样的格子, 此时我们可以进行合并. 显然每个格子我们需要检查 4 个方向. 这是通过 dxdy 实现的, 他们表示不同方向上的增量.
/**
 * Returns true if there are any valid moves on the board.
 * There are two ways that there can be valid moves:
 * 1. There is at least one empty space on the board.
 * 2. There are two adjacent tiles with the same value.
 */
public static boolean atLeastOneMoveExists(Board b) {
    if (emptySpaceExists(b)) {
        return true;
    }
    // 4 directions, LEFT/UP/RIGHT/DOWN
    int[] dx = {0, -1, 0, 1};
    int[] dy = {-1, 0, 1, 0};

    int size = b.size();
    for (int col = 0; col < size; col++) {
        for (int row = 0; row < size; row++) {
            // Because we have checked emptySpace, t.values() must exist
            int curTileValue = b.tile(col, row).value();
            for (int move = 0; move < 4; move++) {
                int colNew = col + dx[move];
                int rowNew = row + dy[move];
                // make sure the tile is within the boundary
                if (colNew > 0 && colNew < size && rowNew > 0 && rowNew < size) {
                    Tile newTile = b.tile(colNew, rowNew);
                    if (newTile.value() == curTileValue) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

Task 4. Building the Game Logic


解决这个任务还是花了我不少时间的

标签:value,proj0,int,CS61B,2048,board,size,col,row
来源: https://www.cnblogs.com/MartinLwx/p/15857943.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有