java File类的基本操作

如果有谁想要这个软件的话,在评论中留一个邮箱吧。)

前几天好几次看到有朋友晒出玩2048刷高分的截图,我就想我能不能也做一个2048呢?仔细想了想2048游戏的规律,发现其实逻辑上很简单,也不用研究什么算法,于是我马上觉得我可以很快写出来。当天下午我就用了四个小时完成了2048的基本功能。几天后觉得不满足,于是给我的2048加了存档、读档和后退一步的功能,这样就更好刷分了呢!

使用语言:C#; 平台:Visual Studio 2012 Win Form。

如何完成2048的基本功能呢?2048在每一次操作后16个方格的数字就有可能发生变化,所以在我的程序里用一个4乘4的二维数组来存这16个值,0表示没有数字。每一次操作直接改变数组的值,然后再把数组的值填到16个方格中。首先来看程序中几个全局的变量:

public int[,] matrixValue = new int[4, 4];//对应16个方格的数字,0表示没有数字
        public bool isThereChanged = false;//表示每一次按键后是否发生改变
        public int score = 0;//此次游戏的分数
        public int maxScore = 0;//记录最高分,用于判断是否破纪录
        public List<storeRecord> dataStoreRecord = new List<storeRecord>();//存储所有存档
        public List<int[,]> history = new List<int[,]>();//存最近10步操作

现在想一下,玩2048的时候往一个方向滑动它是怎么改变16个方格的数字的。我把这个过程分解成三个步骤,对应程序中的三个函数。我觉得在软件开发过程中,这种模块化设计的思想是很重要的。

以下是这三个函数(三个步骤):

public void addSameNumber(string command)//first step往指定方向叠加相同数字的相邻两格
public void moveToEdge(string command)//Second step将所有有数字的格子往指定方向靠边移动
public void addAnNumber()//Third step在没有数字的格子中随机选择一个添加数字

其实注释上已经说明了其目的。第一个函数会在指定方向上叠加相邻两个相同的数字,相邻的意思是中间没有非0的数字,也就是有可能两个相同数字中间两个空格,这种情况下也要相机;第二个函数是在实现了相同数字的叠加之后把所有数字往一个方向挪动;第三个函数实现的是在一个操作过后16个方格状态有改变的时候随机给空格填入一个数字。

第一个函数:(只贴出向上移动的代码)

public void addSameNumber(string command)//first step往指定方向叠加相同数字的相邻两格
        {
            if (command == "up")
            {
                for (int col = 0; col < 4; col++)
                {
                    int firstOfSamePair = -1;
                    int previousIndex = -1;
                    for (int row = 0; row < 4; row++)
                    {
                        if (matrixValue[row, col] != 0)
                        {
                            if (matrixValue[row, col] == firstOfSamePair)
                            {
                                score += matrixValue[row, col] * 2;
                                changeYourScoreHandle();//刷新你的分数
                                matrixValue[previousIndex, col] *= 2;
                                matrixValue[row, col] = 0;
                                firstOfSamePair = -1;
                                previousIndex = -1;
                                isThereChanged = true;
                            }
                            else
                            {
                                firstOfSamePair = matrixValue[row, col];
                                previousIndex = row;
                            }
                        }
                    }
                }
            }//if

第二个函数:

public void moveToEdge(string command)//Second step将所有有数字的格子往指定方向靠边移动
        {
            if (command == "up")
            {
                for (int col = 0; col < 4; col++)
                {
                    int index = 0;
                    for (int row = 0; row < 4; row++)
                    {
                        if (matrixValue[row, col] != 0)
                        {
                            int temp = matrixValue[row, col];
                            matrixValue[row, col] = matrixValue[index, col];
                            matrixValue[index, col] = temp;
                            if (index != row)
                                isThereChanged = true;
                            index++;
                        }
                    }
                }
            }

第三个函数:

public void addAnNumber()//Third step在没有数字的格子中随机选择一个添加数字
        {
            int emptyCount = 0;
            for(int i = 0; i < 4; i++)
            {
                for(int j = 0; j < 4; j++)
                {
                    if(matrixValue[i,j] == 0)
                        emptyCount++;
                }
            }
            Random rand = new Random();
            int temp = rand.Next(emptyCount);
            emptyCount = 0;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (matrixValue[i, j] == 0)
                    {
                        if (emptyCount == temp)
                            randomAddNumber(i, j);//往该位置添加数字
                        emptyCount++;
                    }
                }
            }
        }

其他的请看下篇文章《我的改进版2048(2)》

java File类的基本操作,布布扣,bubuko.com

时间: 2024-08-01 22:48:06

java File类的基本操作的相关文章

File类的基本操作之RandomAccessFile写入操作

今天学习java io中File类下的 RandomAccessfile,欢迎留言讨论,其他知识看api package org.mark.randomaccessfile; import java.io.File; import java.io.RandomAccessFile; /** * 写入操作 */ public class RandomAccessfileDemo1 { /** * @param args */ public static void main(String[] arg

File类的基本操作之RandomAccessFile读取

直接贴代码了,不懂的地方留言讨论 package org.mark.randomaccessfile; import java.io.File; import java.io.FileNotFoundException; import java.io.RandomAccessFile; public class RandomAccessfileDemo2 { /** * @param args * @throws Exception */ public static void main(Stri

File类的基本操作之InputStream字节输入流

话不多少,我直接把代码贴上来了.有什么问题请给我留言 package org.mark.streamRW; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; /** * 字节输出流:OutputStream,整个IO包中字节输出流的最大父类 * 字节输入流:InputStream * * InputStream使用子类FileInputStream.读取 */ public class

File类的基本操作之读出所有目录路径

package org.mark.file; import java.io.File; /** * File类的基本操作之读出所有文件夹路径 * 假设给定一个文件夹,要求将此文件夹中的所有文件都列出来 * 使用递归 */ public class TestChare { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub File mark = new F

File类的基本操作之读出全部文件夹路径

package org.mark.file; import java.io.File; /** * File类的基本操作之读出全部文件夹路径 * 如果给定一个目录,要求将此目录中的全部文件都列出来 * 使用递归 */ public class TestChare { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub File mark = new Fil

java File类

今天我要总结一下java File类.这个是一个很重要的类. 首先是我画的思维导图. 还写了一些自己写的代码. /** * Date : 2017/6/24 * Author : Hsj * Description : */ public class Demo { /** * File(pathname)表示文件或文件夹路径 * File(String parent,child); * File(File parent,child); */ @Test public void fun() { /

java RandomAccessFile类文件基本操作

RandomAccessFile类是java中仿C的文件操作方法,下面通过实例演示RandomAccessFile类对文件的基本操作,深入了解请查看Java API文档.(注:RandomAccessFile类大多不被采用) 上代码 import java.io.*; public class AccessFileDemo { public static void main(String[] args) { Student stu1=new Student("Zhang San",10

JAVA File类 分析(三)

前面两篇与大家一起研究了unix下的文件系统,本篇将和大家一起分析 文件的属性和文件夹. ok,废话不说,先来段代码 #include <stdio.h> #include <sys/types.h> #include <dirent.h> void do_ls(char[]); void main(int ac,char *av[]){ if(ac==1) do_ls("."); else{ while(--ac){ printf("%s

Java File类总结和FileUtils类

Java File类总结和FileUtils类 文件存在和类型判断 创建出File类的对象并不代表该路径下有此文件或目录. 用public boolean exists()可以判断文件是否存在. File类的对象可以是目录或者文件. 如果是目录,public boolean isDirectory()返回true: 如果是文件(非目录则是文件),public boolean isFile()返回true: 但是注意需要先判断文件是否存在,如果文件不存在,上面两个方法都返回false,即不存在的F