C++ 2048简化版

相关源码:

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
View Code

My2048.cpp

  1 #include<iostream>
  2 #include<queue>
  3 #include"My2048.h"
  4 
  5 using std::queue;
  6 
  7 Matrix::Matrix(int p1, int p2) {
  8     int i, j;
  9     for(i = 0; i < rows; ++i)
 10         for(j = 0; j < cols; ++j)
 11             num[i][j] = 0;
 12 
 13     num[p1/cols][p1%cols] = 2;
 14     num[p2/cols][p2%cols] = 2;
 15 }
 16 
 17 bool Matrix::moveLeft() {
 18     int tmp[rows][cols], i, j, k, pos;
 19     queue<int> q;
 20     
 21     for(i = 0; i < rows; ++i)
 22         for(j = 0; j < cols; ++j)
 23             tmp[i][j] = num[i][j];
 24     
 25     for(i = 0; i < rows; ++i) {
 26         for(j = 0; j < cols; ++j) { //delete the 0
 27             if(num[i][j])
 28                 q.push(num[i][j]);
 29             num[i][j] = 0;
 30         }
 31         pos = 0;
 32         while(!q.empty()) {
 33             k = q.front();
 34             q.pop();
 35             if(q.empty() || k != q.front()) {
 36                 num[i][pos++] = k;
 37             } else {
 38                 num[i][pos++] = 2*k;
 39                 q.pop();
 40             }
 41         }
 42     }
 43     for(i = 0; i < rows; ++i){
 44         for(j = 0; j < cols; ++j) {
 45             if(num[i][j] != tmp[i][j])
 46                 return true;
 47         }
 48     }
 49     return false;
 50 }
 51 
 52 bool Matrix::moveRight() {
 53     int i, j, pos, tmp[rows][cols], k;
 54     queue<int> q;
 55     for(i = 0; i < rows; ++i)
 56         for(j = 0; j < cols; ++j)
 57             tmp[i][j] = num[i][j];
 58 
 59     for(i = 0; i < rows;++i) {
 60         for(j = cols - 1; j >= 0; --j) {
 61             if(num[i][j])
 62                 q.push(num[i][j]);
 63             num[i][j] = 0;
 64         }
 65         pos = cols - 1;
 66         while(!q.empty()) {
 67             k = q.front();
 68             q.pop();
 69             if(q.empty() || k != q.front()) {
 70                 num[i][pos--] = k;
 71             } else {
 72                 num[i][pos--] = 2 * k;
 73                 q.pop();
 74             }
 75         }
 76     }
 77     
 78     for(i = 0; i < rows; ++i) {
 79         for(j = 0; j < cols; ++j) {
 80             if(num[i][j] != tmp[i][j])
 81                 return true;
 82         }
 83     }
 84     return false;
 85 }
 86 
 87 bool Matrix::moveUp() {
 88     int i, j, pos, tmp[rows][cols], k;
 89     queue<int> q;
 90     for(i = 0; i < rows; ++i)
 91         for(j = 0; j < cols; ++j)
 92             tmp[i][j] = num[i][j];
 93 
 94     for(i = 0; i < cols;++i) {
 95         for(j = 0; j < rows; ++j) {
 96             if(num[j][i])
 97                 q.push(num[j][i]);
 98             num[j][i] = 0;
 99         }
100         pos = 0;
101         while(!q.empty()) {
102             k = q.front();
103             q.pop();
104             if(q.empty() || k != q.front()) {
105                 num[pos++][i] = k;
106             } else {
107                 num[pos++][i] = 2 * k;
108                 q.pop();
109             }
110         }
111     }
112     
113     for(i = 0; i < rows; ++i) {
114         for(j = 0; j < cols; ++j) {
115             if(num[i][j] != tmp[i][j])
116                 return true;
117         }
118     }
119     return false;
120 }
121 
122 bool Matrix::moveDown() {
123     int i, j, pos, tmp[rows][cols], k;
124     queue<int> q;
125     for(i = 0; i < rows; ++i)
126         for(j = 0; j < cols; ++j)
127             tmp[i][j] = num[i][j];
128 
129     for(i = 0; i < cols;++i) {
130         for(j = rows - 1; j >= 0; --j) {
131             if(num[j][i])
132                 q.push(num[j][i]);
133             num[j][i] = 0;
134         }
135         pos = rows - 1;
136         while(!q.empty()) {
137             k = q.front();
138             q.pop();
139             if(q.empty() || k != q.front()) {
140                 num[pos--][i] = k;
141             } else {
142                 num[pos--][i] = 2 * k;
143                 q.pop();
144             }
145         }
146     }
147     
148     for(i = 0; i < rows; ++i) {
149         for(j = 0; j < cols; ++j) {
150             if(num[i][j] != tmp[i][j])
151                 return true;
152         }
153     }
154     return false;
155 }
156 
157 bool Matrix::add(int p) {
158     if(num[p/cols][p%cols] != 0)
159         return false;
160     else {
161         num[p/cols][p%cols] = 2;
162     }
163 }
164 
165 
166 void Matrix::update1() {
167     int i;
168     for(i = 0; i < rows * cols; ++i) {
169         if(add(i))
170             return;
171     }
172 }
173 
174 void Matrix::update2() {
175     srand((unsigned)time(NULL));
176     while(true) {
177         int n = rand() % (rows*cols);
178         if(add(n))
179             return;
180     }
181 }
182 
183 ostream& operator<<(ostream& out, Matrix& matrix) {
184     int i, k, j;
185     for(i = 0; i < (L + 1) * cols; ++i)
186         out << "-";
187     out << std::endl;
188 
189     for(i = 0; i < rows; ++i) {
190         out << "|";
191         for(j = 0; j < cols; ++j) {
192             k = matrix.num[i][j];
193             out.width(L);
194             if(k)
195                 out << k;
196             else
197                 out << "";
198             out << "|";
199         }
200         out << std::endl;
201     }
202     for(i = 0; i <= (L+1) * cols; ++i)
203         out << "-";
204     out << std::endl;
205 
206     return out;
207 }
View Code

 Main.cpp

 1 #include<iostream>
 2 #include<time.h>
 3 #include<stdlib.h>
 4 #include "My2048.h"
 5 
 6 using std::cout;
 7 using std::cin;
 8 int main() {
 9     Matrix m(0, 13);
10     cout << m;
11 
12     char c;
13     while(cin >> c) {
14         bool flag = false;
15         if(c == 'q')
16             break;
17         switch (c) {
18             case 'a' :
19                 flag = m.moveLeft();
20                 break;
21             case 'd':
22                 flag = m.moveRight();
23                 break;
24             case 'w':
25                 flag = m.moveUp();
26                 break;
27             case 's':
28                 flag = m.moveDown();
29                 break;
30             default:
31                 break;
32         }
33         //system("cls");
34         if(flag)
35             m.update1();
36         cout << m;
37     }
38 }
View Code

makefile

Task:main.o My2048.o
    g++ -g main.o My2048.o -o Task
main.o:main.cpp
    g++ -g -c main.cpp
My2048.o:My2048.h My2048.cpp
    g++ -g -c My2048.cpp -o My2048.o

 

posted @ 2015-08-15 21:11  Kinthon  阅读(782)  评论(0编辑  收藏  举报