简易游戏 2048 制作

  • // Matrix.h
  • #ifndef MATRIX_H
  • #define MATRIX_H
  •  
  • #include<iostream>
  • #include<time.h>
  • #include<stdlib.h>
  •  
  • #define rows 4
  • #define cols 4
  • #define L 6
  •  
  • using std::ostream;
  •  
  • classMatrix{
  • friend ostream&operator<<(ostream& out,constMatrix& matirx);
  • // here is an output example
  • // -----------------------------
  • // | 16| 8| 4| 2|
  • // | 8  | 8| 4| 2|
  • // |     | 4| 8| 2|
  • // |     |   | 2| 2|
  • // -----------------------------
  • public:
  •   Matrix(int p1,int p2);
  •   bool moveLeft();// return true if the matrix changes
  •   bool moveRight();// return true if the matrix changes
  •   bool moveUp();// return true if the matrix changes
  •   bool moveDown();// return true if the matrix changes
  •   bool add(int p);// return true if the matrix changes
  •   void update2(){// add a 2 to a random position
  •    srand((unsigned)time(NULL));
  •   unsignedint seed;
  •   while(true){
  •     int n = rand_r(&seed)%(rows*cols);
  •     if(add(n))
  •       return;
  •     }
  •   }
  •  
  • private:
  •   int num[rows][cols];
  • };
  •  
  • #endif



// Matrix.cpp
                   
  1. #include<iostream>
  2. #include<queue>
  3. #include"Matrix.h"
  4.  
  5. using std::endl;
  6. using std::queue;
  7.  
  8. Matrix::Matrix(int p1,int p2){
  9. for(int r =0; r < rows; r++)
  10. for(int c =0; c < cols; c++)
  11. num[r][c]=0;
  12. num[p1/cols][p1%cols]=2;
  13. num[p2/cols][p2%cols]=2;
  14. }
  15.  
  16. boolMatrix::moveLeft(){
  17. int tmp[rows][cols];
  18. for(int r =0; r < rows; r++)
  19. for(int c =0; c < cols; c++)
  20. tmp[r][c]= num[r][c];
  21. for(int r =0; r < rows; r++){
  22. queue<int> q;
  23. for(int c =0; c < cols; c++){
  24. if(num[r][c])
  25. q.push(num[r][c]);
  26. num[r][c]=0;
  27. }
  28. int pos =0;
  29. while(!q.empty()){
  30. int k = q.front();
  31. q.pop();
  32. if(q.empty()|| k != q.front()){
  33. num[r][pos++]= k;
  34. }else{
  35. num[r][pos++]=2*k;
  36. q.pop();
  37. }
  38. }
  39. }
  40. for(int r =0; r < rows; r++)
  41. for(int c =0; c < cols; c++)
  42. if(tmp[r][c]!= num[r][c])
  43. returntrue;
  44. returnfalse;
  45. }
  46.  
  47. boolMatrix::moveRight(){
  48. int tmp[rows][cols];
  49. for(int r =0; r < rows; r++)
  50. for(int c =0; c < cols; c++)
  51. tmp[r][c]= num[r][c];
  52. for(int r =0; r < rows; r++){
  53. queue<int> q;
  54. for(int c = cols-1; c >=0; c--){
  55. if(num[r][c])
  56. q.push(num[r][c]);
  57. num[r][c]=0;
  58. }
  59. int pos = cols-1;
  60. while(!q.empty()){
  61. int k = q.front();
  62. q.pop();
  63. if(q.empty()|| k != q.front()){
  64. num[r][pos--]= k;
  65. }else{
  66. num[r][pos--]=2*k;
  67. q.pop();
  68. }
  69. }
  70. }
  71. for(int r =0; r < rows; r++)
  72. for(int c =0; c < cols; c++)
  73. if(tmp[r][c]!= num[r][c])
  74. returntrue;
  75. returnfalse;
  76. }
  77.  
  78. boolMatrix::moveUp(){
  79. int tmp[rows][cols];
  80. for(int r =0; r < rows; r++)
  81. for(int c =0; c < cols; c++)
  82. tmp[r][c]= num[r][c];
  83. for(int c =0; c < cols; c++){
  84. queue<int> q;
  85. for(int r =0; r < rows; r++){
  86. if(num[r][c])
  87. q.push(num[r][c]);
  88. num[r][c]=0;
  89. }
  90. int pos =0;
  91. while(!q.empty()){
  92. int k = q.front();
  93. q.pop();
  94. if(q.empty()|| k != q.front()){
  95. num[pos++][c]= k;
  96. }else{
  97. num[pos++][c]=2*k;
  98. q.pop();
  99. }
  100. }
  101. }
  102. for(int r =0; r < rows; r++)
  103. for(int c =0; c < cols; c++)
  104. if(tmp[r][c]!= num[r][c])
  105. returntrue;
  106. returnfalse;
  107. }
  108.  
  109. boolMatrix::moveDown(){
  110. int tmp[rows][cols];
  111. for(int r =0; r < rows; r++)
  112. for(int c =0; c < cols; c++)
  113. tmp[r][c]= num[r][c];
  114. for(int c =0; c < cols; c++){
  115. queue<int> q;
  116. for(int r = rows-1; r >=0; r--){
  117. if(num[r][c])
  118. q.push(num[r][c]);
  119. num[r][c]=0;
  120. }
  121. int pos = rows-1;
  122. while(!q.empty()){
  123. int k = q.front();
  124. q.pop();
  125. if(q.empty()|| k != q.front()){
  126. num[pos--][c]= k;
  127. }else{
  128. num[pos--][c]=2*k;
  129. q.pop();
  130. }
  131. }
  132. }
  133. for(int r =0; r < rows; r++)
  134. for(int c =0; c < cols; c++)
  135. if(tmp[r][c]!= num[r][c])
  136. returntrue;
  137. returnfalse;
  138. }
  139.  
  140. boolMatrix::add(int p){
  141. int r = p/cols;
  142. int c = p%cols;
  143. if(num[r][c])
  144. returnfalse;
  145. num[r][c]=2;
  146. returntrue;
  147. }
  148.  
  149. ostream&operator<<(ostream& out,constMatrix& matrix){
  150. for(int i =0; i <=(L+1)*cols; i++)
  151. out <<"-";
  152. out << endl;
  153. for(int r =0; r < rows; r++){
  154. out <<"|";
  155. for(int c =0; c < cols; c++){
  156. int k = matrix.num[r][c];
  157. out.width(L);
  158. if(k)
  159. out << matrix.num[r][c];
  160. else
  161. out <<"";
  162. out <<"|";
  163. }
  164. out << endl;
  165. }
  166. for(int i =0; i <=(L+1)*cols; i++)
  167. out <<"-";
  168. out << endl;
  169.  
  170. return out;
  171. }

 

 

 

 // client.cpp

  • #include<iostream>
  • #include<time.h>
  • #include"Matrix.h"
  • #include<curses.h>
  •  
  • using std::cout;
  • using std::cin;
  • int main(){
  • Matrix m(0,13);
  • cout << m;
  •  
  • char c;
  • while(cin >> c){
  • bool flag =false;
  • switch(c){// a, d, w, s stand for left, right, up and down
  • case'a':
  • flag = m.moveLeft();
  • break;
  • case'd':
  • flag = m.moveRight();
  • break;
  • case'w':
  • flag = m.moveUp();
  • break;
  • case's':
  • flag = m.moveDown();
  • break;
  • default:
  • break;
  • }
  • if(flag)// if the matrix changes, add a 2 to the matrix
  • m.update2();
  • // system("Cls");
  • cout << m;
  • }
  • return0;
  • }

           
posted on 2014-05-08 23:09  左手代码右手诗  阅读(1401)  评论(4编辑  收藏  举报