相关源码:
My2048.h
1 //设计思想,针对每一次的操作用队列进行模拟;如果是向左的话,针对行,对每一行的每一个列元素,如果是非0的话就进队列;全部进队列后,判断如果头两个元素相等的话,就把两者的和放进num[][]中,如果不等就把本身放进去; 2 3 #ifndef MATRIX_H 4 #define MATRIX_H 5 #include<iostream> 6 #include<time.h> 7 #include<stdlib.h> 8 9 #define rows 4 10 #define cols 4 11 #define L 6 12 13 using std::ostream; 14 15 class Matrix{ 16 friend ostream& operator<<(ostream& out, Matrix& matrix); 17 //here is an output example 18 // ----------------------------- 19 // | 16| 8| 4| 2| 20 // | 8| 8| 4| 2| 21 // | | 4| 8| 2| 22 // | | | 2| 2| 23 // ----------------------------- 24 public: 25 Matrix(int p1, int p2); 26 bool moveLeft(); //return true if the matrix change 27 bool moveRight(); 28 bool moveUp(); 29 bool moveDown(); 30 bool add(int p); 31 void update1(); //add a 2 to the possible lowest position 32 void update2(); //add a 2 to a random position 33 private: 34 int num[rows][cols]; 35 }; 36 #endif