Tetris problem

A

Active Volcano

now we have a two-dimension matrix,the dimension is MxN,such as :int
map[M][N],the element of matrix satisfy all the below:
1)initial condition:
1.the element belong to the set:{0,1,2,3}
2.matrix's initial state satisfy:
(*):
for the column , if map[pos] == 0, then map[k][pos] == 0, i<=k<M,
0<=i<M,0<=pos<M

2)requirement:
1. delete element of landscape orientation and lengthwise direction
which sastify:continuous equal and the number >= 3, then need to
sastify (*).

such as:
23000
11122

delete element of landscape orientation:111,turn to:
00000
23022

2.first of all, need to delete element of landscape orientation

May some people give C/C++ code, i had realize this less than 200
line of c++ code 。
 
A

Active Volcano

Active said:
now we have a two-dimension matrix,the dimension is MxN,such as :int
map[M][N],the element of matrix satisfy all the below:
1)initial condition:
1.the element belong to the set:{0,1,2,3}
2.matrix's initial state satisfy:
(*):
for the column , if map[pos] == 0, then map[k][pos]  == 0, i<=k<M,
0<=i<M,0<=pos<M

2)requirement:
1. delete element of landscape orientation and lengthwise direction
which sastify:continuous equal and the number >= 3, then need to
sastify (*).
such as:
23000
11122
delete element of landscape orientation:111,turn to:
00000
23022
2.first of all, need to delete element of landscape orientation
May some people give C/C++ code, i had  realize this less than 200
line of c++ code 。

Can we have your lecturer's email so we can email him direct? :) Cutting
out the middle-man seems like it might save time once we've finished
coding up your homework for you...

Stu


-_-
"i had realize this less than 200 line of c++ code."
You need not guess the other's mind in your mind. I just want to
search the better algorithm.
this is my code:

#include <iostream>

#include <cstdlib>

#include <cstring>

using namespace std;



int data[10][8] =

{

{0, 0, 0, 0, 0, 0, 0, 0 },

{0, 0, 0, 0, 0, 0, 0, 0 },

{0, 2, 2, 1, 0, 0, 3, 0 },

{0, 2, 3, 1, 0, 0, 1, 3 },

{0, 3, 2, 3, 0, 0, 2, 3 },

{0, 3, 2, 2, 0, 0, 1, 1 },

{1, 1, 3, 3, 0, 2, 1, 1 },

{1, 2, 2, 2, 2, 2, 2, 1 },

{2, 3, 2, 3, 1, 2, 1, 3 },

{1, 3, 1, 3, 3, 1, 2, 3 }

};



//result

/*==============

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 2 2 0 0 0 0 0

0 2 3 0 0 0 0 0

0 3 2 0 0 0 0 0

1 3 2 1 0 0 3 0

1 1 3 1 0 2 1 0

2 3 2 3 1 2 2 0

1 3 1 2 3 1 2 0

==============*/



class Tetris

{

public:

Tetris(int arg);

void lineDelete();

void show();



private:

bool SearchMaxMatch(int *array, int len, int bound, int & start,
int & end);

void deleteNull(int *array);

void setData(int *array, int pos);

void getData(int *array, int pos);

void deleteNull(int *array, int len);

void downMap();



private:

static const int max_row = 10;

static const int max_col = 8;

int map[max_row][max_col];

};





Tetris::Tetris(int arg)

{

switch (arg)

{

case 0:

for(int i = 0; i < max_row; i++)

{

for(int j = 0; j < max_col; j++)

{

map[j] = rand()%4;

}

}

break;

case 1:

memcpy(map, data, sizeof(map));

break;

default:

break;

}

}



void Tetris::lineDelete()

{

int start = 0;

int end = 0;

bool nextFlag =true ;

int array[max_row];



memset(array, 0, max_row);

//row delete

while(nextFlag)

{

nextFlag = false;

for(int i = max_row -1; i >= 0; i--)

{

if(SearchMaxMatch(map, max_col, 3, start, end) == true)

{

nextFlag = true;

for(int j = start; j < end; j++)

{

map[j] = 0;

}

}

}

downMap();



//col delete

for(int j = 0 ; j < max_col; j++)

{

getData(array, j);

if(SearchMaxMatch(array, max_row, 3, start, end) == true)

{

nextFlag = true;

for(int i = start; i < end; i++)

{

array = 0;

}

setData(array, j);

}

}

downMap();

}



}



bool Tetris::SearchMaxMatch(int *array, int len, int bound, int &
start, int & end)

{

int t;

for(int i = 0; i < len; i++)

{

t = 1;

for(int j = i+1; (j < len) && (array[j]==array) && (array
!=0); j++)

{

t++;

}

if (t >= bound)

{

start = i;

end = i+t;

return true;

}

}

return false;

}

//1010100 --> 1110000

void Tetris::deleteNull(int *array, int len)

{

int cur = 0;



while (1)

{

while((cur < len) && ((array[cur] != 0)))

{

cur++;

}

if (cur == len)

{

return;

}



int curFront = cur;

while((curFront < len) && (array[curFront] == 0))

{

curFront++;

}

if(curFront == len)

{

return;

}



for(int i = curFront; i < len; i++)

{

array[i-(curFront-cur)] = array;

}

}

}



//set data array to map

void Tetris::setData(int *array, int pos)

{

int j = 0;

for(int i = max_row - 1; i >= 0; i--)

{

map[pos] = array[j];

j++;

}

return;

}



//get data map to array

void Tetris::getData(int *array, int pos)

{

int j = 0;

for(int i = max_row - 1; i >= 0; i--)

{

array[j] = map[pos];

j++;

}

return;

}



void Tetris::downMap()

{

int array[max_row];

memset(array, 0, max_row);

for(int j = 0; j < max_col; j++)

{

getData(array, j);

deleteNull(array, max_row);

setData(array, j);

}

}



void Tetris::show()

{

for(int i = 0; i < max_row; i++)

{

for(int j = 0; j < max_col; j++)

{

cout << map[j] << " ";

}

cout << endl;

}

cout << endl << endl;

}

int main()

{

Tetris tmp(1);

tmp.show();

tmp.lineDelete();

tmp.show();



return 0;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top