- // 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
- #include<iostream>
- #include<queue>
- #include"Matrix.h"
- using std::endl;
- using std::queue;
- Matrix::Matrix(int p1,int p2){
- for(int r =0; r < rows; r++)
- for(int c =0; c < cols; c++)
- num[r][c]=0;
- num[p1/cols][p1%cols]=2;
- num[p2/cols][p2%cols]=2;
- }
- boolMatrix::moveLeft(){
- int tmp[rows][cols];
- for(int r =0; r < rows; r++)
- for(int c =0; c < cols; c++)
- tmp[r][c]= num[r][c];
- for(int r =0; r < rows; r++){
- queue<int> q;
- for(int c =0; c < cols; c++){
- if(num[r][c])
- q.push(num[r][c]);
- num[r][c]=0;
- }
- int pos =0;
- while(!q.empty()){
- int k = q.front();
- q.pop();
- if(q.empty()|| k != q.front()){
- num[r][pos++]= k;
- }else{
- num[r][pos++]=2*k;
- q.pop();
- }
- }
- }
- for(int r =0; r < rows; r++)
- for(int c =0; c < cols; c++)
- if(tmp[r][c]!= num[r][c])
- returntrue;
- returnfalse;
- }
- boolMatrix::moveRight(){
- int tmp[rows][cols];
- for(int r =0; r < rows; r++)
- for(int c =0; c < cols; c++)
- tmp[r][c]= num[r][c];
- for(int r =0; r < rows; r++){
- queue<int> q;
- for(int c = cols-1; c >=0; c--){
- if(num[r][c])
- q.push(num[r][c]);
- num[r][c]=0;
- }
- int pos = cols-1;
- while(!q.empty()){
- int k = q.front();
- q.pop();
- if(q.empty()|| k != q.front()){
- num[r][pos--]= k;
- }else{
- num[r][pos--]=2*k;
- q.pop();
- }
- }
- }
- for(int r =0; r < rows; r++)
- for(int c =0; c < cols; c++)
- if(tmp[r][c]!= num[r][c])
- returntrue;
- returnfalse;
- }
- boolMatrix::moveUp(){
- int tmp[rows][cols];
- for(int r =0; r < rows; r++)
- for(int c =0; c < cols; c++)
- tmp[r][c]= num[r][c];
- for(int c =0; c < cols; c++){
- queue<int> q;
- for(int r =0; r < rows; r++){
- if(num[r][c])
- q.push(num[r][c]);
- num[r][c]=0;
- }
- int pos =0;
- while(!q.empty()){
- int k = q.front();
- q.pop();
- if(q.empty()|| k != q.front()){
- num[pos++][c]= k;
- }else{
- num[pos++][c]=2*k;
- q.pop();
- }
- }
- }
- for(int r =0; r < rows; r++)
- for(int c =0; c < cols; c++)
- if(tmp[r][c]!= num[r][c])
- returntrue;
- returnfalse;
- }
- boolMatrix::moveDown(){
- int tmp[rows][cols];
- for(int r =0; r < rows; r++)
- for(int c =0; c < cols; c++)
- tmp[r][c]= num[r][c];
- for(int c =0; c < cols; c++){
- queue<int> q;
- for(int r = rows-1; r >=0; r--){
- if(num[r][c])
- q.push(num[r][c]);
- num[r][c]=0;
- }
- int pos = rows-1;
- while(!q.empty()){
- int k = q.front();
- q.pop();
- if(q.empty()|| k != q.front()){
- num[pos--][c]= k;
- }else{
- num[pos--][c]=2*k;
- q.pop();
- }
- }
- }
- for(int r =0; r < rows; r++)
- for(int c =0; c < cols; c++)
- if(tmp[r][c]!= num[r][c])
- returntrue;
- returnfalse;
- }
- boolMatrix::add(int p){
- int r = p/cols;
- int c = p%cols;
- if(num[r][c])
- returnfalse;
- num[r][c]=2;
- returntrue;
- }
- ostream&operator<<(ostream& out,constMatrix& matrix){
- for(int i =0; i <=(L+1)*cols; i++)
- out <<"-";
- out << endl;
- for(int r =0; r < rows; r++){
- out <<"|";
- for(int c =0; c < cols; c++){
- int k = matrix.num[r][c];
- out.width(L);
- if(k)
- out << matrix.num[r][c];
- else
- out <<"";
- out <<"|";
- }
- out << endl;
- }
- for(int i =0; i <=(L+1)*cols; i++)
- out <<"-";
- out << endl;
- return out;
- }
// 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;
- }