tetris_modal.c 15.4 KB
Newer Older
qiuyiuestc's avatar
qiuyiuestc 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * File      : tetris_modal.c
 * This file is part of RTGUI in RT-Thread RTOS
 * COPYRIGHT (C) 2010, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2010-08-14     Yi.Qiu       first version
 */
 
#include <rtthread.h>
#include <stdlib.h> 
#include "tetris.h"

struct rt_tetris
{
	rt_uint32_t	width;			/* the width of the tetris */
	rt_uint32_t	height;			/* the height of the tetris */
qiuyiuestc's avatar
qiuyiuestc 已提交
23 24 25 26 27 28 29 30
	rt_uint16_t* 	panel;			/* the panel of the tetris */
	rt_uint32_t* 	brick;			/* the current brick of the tetris */
	rt_uint32_t* 	next_brick;		/* the next brick  of the tetris */
	rt_tetris_view_t* view;			/* the view on which the tetris show */
	rt_uint32_t 	level;			/* game level */
	rt_uint32_t 	lines;			/* released lines count */
	rt_uint32_t 	score;			/* total scores statistic */
	rt_bool_t 		status;			/* game status, pause or runing */
qiuyiuestc's avatar
qiuyiuestc 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
};

static const rt_uint32_t g_brick[][4] = 
{
	{23,7,8,22},
	{23,7,8,24},
	{24,7,8,25},
	{8,7,9,23},	
	{8,7,9,24},
	{8,7,9,25},
	{7,6,8,9},
};

static rt_err_t rt_tetris_append_brick(rt_tetris_t* thiz, rt_uint32_t brick[]);
static rt_err_t rt_tetris_delete_brick(rt_tetris_t* thiz, rt_uint32_t brick[]);
static rt_err_t rt_tetris_release_lines(rt_tetris_t* thiz, rt_uint32_t brick[]);
static rt_err_t rt_tetris_is_reach_top(rt_tetris_t* thiz, rt_uint32_t brick[]);
static rt_err_t rt_tetris_update_brick(rt_tetris_t* thiz);

/**
 * this function create a tetris instance
 *
 * @param width the width of tetris.
 * @param height the height of tetris.
 *
 * @return the tetris instance
 */
rt_tetris_t* rt_tetris_create(rt_uint32_t width, rt_uint32_t height)
{
	int index;

	rt_tetris_t* thiz = (rt_tetris_t*)rt_malloc(sizeof(rt_tetris_t));
	RT_ASSERT(thiz != RT_NULL);
qiuyiuestc's avatar
qiuyiuestc 已提交
64

qiuyiuestc's avatar
qiuyiuestc 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
	thiz->height 	= height;
	thiz->width		= width;
	thiz->panel 	= rt_malloc(thiz->height * sizeof(rt_uint32_t));
	rt_memset(thiz->panel, 0, thiz->height * sizeof(rt_uint32_t));	

	thiz->brick		= (rt_uint32_t*)rt_malloc(4 * sizeof(rt_uint32_t));
	index 			= (int)(7.0 * rand()/(RAND_MAX + 1.0)); 
	rt_memcpy(thiz->brick, g_brick[index], 4 * sizeof(rt_uint32_t));

	thiz->next_brick= (rt_uint32_t*)rt_malloc(4 * sizeof(rt_uint32_t));
	index 			= (int)(7.0 * rand()/(RAND_MAX + 1.0)); 
	rt_memcpy(thiz->next_brick, g_brick[index], 4 * sizeof(rt_uint32_t));

	thiz->view		= RT_NULL;
	thiz->level		= 0;
	thiz->lines 	= 0;
	thiz->score		= 0;
	thiz->status	= RT_FALSE;

	return thiz;
}

/**
 * this function destory a tetris instance
 *
 * @param thiz the tetris instance.
 *
 * @return RT_EOK
 */
rt_err_t rt_tetris_destory(rt_tetris_t* thiz)
{
	RT_ASSERT(thiz->panel && thiz->brick && thiz->next_brick);

	rt_free(thiz->panel);
	rt_free(thiz->brick);
	rt_free(thiz->next_brick);
	rt_free(thiz);

	return RT_EOK;
}

/**
 * this function start tetris game
 *
 * @param thiz the tetris instance.
 *
 * @return RT_EOK
 */
rt_err_t rt_tetris_start(rt_tetris_t* thiz)
{
	RT_ASSERT(thiz != RT_NULL);

	/* update next brick on view */
	thiz->view->update_next_brick(thiz->view, thiz);

	/* update level */
	thiz->view->update_level(thiz->view, thiz);

	/* update lines and score */
	thiz->view->update_score_and_lines(thiz->view, thiz);

	thiz->status = RT_TRUE;

	return RT_EOK;
}

/**
 * this function pause tetris game
 *
 * @param thiz the tetris instance.
 *
 * @return RT_EOK
 */
rt_err_t rt_tetris_pause(rt_tetris_t* thiz)
{
	RT_ASSERT(thiz != RT_NULL);

	thiz->status = RT_FALSE;

	return RT_EOK;
}

/**
 * this function get width of a tetris instance
 *
 * @param thiz the tetris instance.
 *
 * @return the width of the tetris instance
 */
rt_uint32_t rt_tetris_width(rt_tetris_t* thiz)
{
	RT_ASSERT(thiz != RT_NULL);

	return thiz->width;
}

/**
 * this function get next brick of a tetris instance
 *
 * @param thiz the tetris instance.
 *
 * @return the next brick of the tetris instance
 */
rt_uint32_t* rt_tetris_next_brick(rt_tetris_t* thiz)
{
	RT_ASSERT(thiz != RT_NULL);

	return thiz->next_brick;
}

/**
 * this function get level of the tetris instance
 *
 * @param thiz the tetris instance.
 *
 * @return the level of the tetris instance
 */
rt_uint32_t rt_tetris_level(rt_tetris_t* thiz)
{
	RT_ASSERT(thiz != RT_NULL);

	return thiz->level;
}

/**
 * this function get released lines of the tetris instance
 *
 * @param thiz the tetris instance.
 *
 * @return the released lines of the tetris instance
 */
rt_uint32_t rt_tetris_lines(rt_tetris_t* thiz)
{
	RT_ASSERT(thiz != RT_NULL);

	return thiz->lines;
}

/**
 * this function get score of the tetris instance
 *
 * @param thiz the tetris instance.
 *
 * @return the score of the tetris instance
 */
rt_uint32_t rt_tetris_score(rt_tetris_t* thiz)
{
	RT_ASSERT(thiz != RT_NULL);

	return thiz->score;
}

/**
 * this function get height of a tetris instance
 *
 * @param thiz the tetris instance.
 *
 * @return the height of the tetris instance
 */
rt_uint32_t rt_tetris_height(rt_tetris_t* thiz)
{
	RT_ASSERT(thiz != RT_NULL);	

	return thiz->height;
}

/**
 * this function get status of a tetris instance
 *
 * @param thiz the tetris instance.
 *
 * @return the status of the tetris instance
 */
rt_bool_t rt_tetris_status(rt_tetris_t* thiz)
{
	RT_ASSERT(thiz != RT_NULL);	

	return thiz->status;
}

/**
 * this function makes current brick move down
 *
 * @param thiz the tetris instance.
 *
 * @return RT_EOK on success, -RT_ERROR on fail
 */
rt_err_t rt_tetris_down(rt_tetris_t* thiz)
{
	int i;

	RT_ASSERT(thiz != RT_NULL);	

	if(thiz->status == RT_FALSE) return -RT_ERROR;

	/* delete the brick from tetris panel */
	rt_tetris_delete_brick(thiz, thiz->brick);

	for(i=0; i<4; i++)
	{
		/* check collision and bottom*/
		if((thiz->brick[i] >= thiz->width * (thiz->height - 1))
			|| rt_tetris_check_collision(thiz, thiz->brick[i] + thiz->width) == RT_EOK)
		{
			/* restore the deleted brick */
			rt_tetris_append_brick(thiz, thiz->brick);

			if(rt_tetris_is_reach_top(thiz, thiz->brick) == RT_EOK)
			{
				rt_memset(thiz->panel, 0xff, thiz->height * sizeof(rt_uint32_t));				

				/* update view */
				thiz->view->update(thiz->view, thiz);

				/* game over */
				return -RT_ETIMEOUT;
			}

			if(rt_tetris_release_lines(thiz, thiz->brick) == RT_EOK) 
			{
				/* update view */
				thiz->view->update(thiz->view, thiz);
			}

			rt_tetris_update_brick(thiz);
			return -RT_ERROR;			
		}
	}	

	for(i=0; i<4; i++)
	{	
		/* increase one line */
		thiz->brick[i] += thiz->width;
	}		

	/* append the brick to tetris panel */
	rt_tetris_append_brick(thiz, thiz->brick);

	/* update view */
	thiz->view->update(thiz->view, thiz);

	return RT_EOK;
}

/**
 * this function makes current brick move left
 *
 * @param thiz the tetris instance.
 *
 * @return RT_EOK on success, -RT_ERROR on fail
 */
rt_err_t rt_tetris_left(rt_tetris_t* thiz)
{
	int i;
	RT_ASSERT(thiz != RT_NULL);	

	if(thiz->status == RT_FALSE) return -RT_ERROR;

	/* delete the brick from tetris panel */
	rt_tetris_delete_brick(thiz, thiz->brick);

	for(i=0; i<4; i++)
	{
		/* check left board */
		if((thiz->brick[i] % thiz->width) == 0)
		{	
			/* restore the deleted brick */
			rt_tetris_append_brick(thiz, thiz->brick);
			return -RT_ERROR;
		}

		if(rt_tetris_check_collision(thiz, thiz->brick[i] - 1) == RT_EOK)
		{	
			/* restore the deleted brick */
			rt_tetris_append_brick(thiz, thiz->brick);
			return -RT_ERROR;
		}
	}	
	
	for(i=0; i<4; i++)
	{	
		/* move one step to left */
		thiz->brick[i] --;;
	}		

	/* append the brick to tetris panel */
	rt_tetris_append_brick(thiz, thiz->brick);

	/* update view */
	thiz->view->update(thiz->view, thiz);

	return RT_EOK;
}

/**
 * this function makes current brick move right
 *
 * @param thiz the tetris instance.
 *
 * @return RT_EOK on success, -RT_ERROR on fail
 */
rt_err_t rt_tetris_right(rt_tetris_t* thiz)
{
	int i;
	RT_ASSERT(thiz != RT_NULL);	

	if(thiz->status == RT_FALSE) return -RT_ERROR;

	/* delete the brick from tetris panel */
	rt_tetris_delete_brick(thiz, thiz->brick);

	for(i=0; i<4; i++)
	{
		/* check left board */
		if(((thiz->brick[i] + 1) % thiz->width) == 0)
		{	
			/* restore the deleted brick */
			rt_tetris_append_brick(thiz, thiz->brick);
			return -RT_ERROR;
		}

		/* check collision */
		if(rt_tetris_check_collision(thiz, thiz->brick[i] + 1) == RT_EOK)
		{	
			/* restore the deleted brick */
			rt_tetris_append_brick(thiz, thiz->brick);
			return -RT_ERROR;
		}
	}	
	
	for(i=0; i<4; i++)
	{	
		/* move one step to right */
		thiz->brick[i] ++;;
	}		

	/* append the brick to tetris panel */
	rt_tetris_append_brick(thiz, thiz->brick);

	/* update view */
	thiz->view->update(thiz->view, thiz);

	return RT_EOK;
}

/**
 * this function makes current brick drop quickly
 *
 * @param thiz the tetris instance.
 *
 * @return RT_EOK on success, -RT_ERROR on fail
 */
rt_err_t rt_tetris_drop(rt_tetris_t* thiz)
{
	rt_err_t ret;
	RT_ASSERT(thiz != RT_NULL);

	if(thiz->status == RT_FALSE) return -RT_ETIMEOUT;

	/* move down until blocked */
	while((ret = rt_tetris_down(thiz)) == RT_EOK);

	return ret;
}

/**
 * this function makes current brick do rotation
 *
 * @param thiz the tetris instance.
 *
 * @return RT_EOK on success, -RT_ERROR on fail
 */
rt_err_t rt_tetris_rotate(rt_tetris_t* thiz, rt_bool_t direction)
{
	int i;
	rt_uint32_t tmp[4];
	RT_ASSERT(thiz != RT_NULL);	

	if(thiz->status == RT_FALSE) return -RT_ERROR;

	rt_tetris_delete_brick(thiz, thiz->brick);

	tmp[0] = thiz->brick[0];
	for(i=1; i<4; i++)
	{
		int diff = thiz->brick[0] - thiz->brick[i];
		if(diff == 1)
		{
			tmp[i] = thiz->brick[0] - thiz->width;
		}
		else if(diff == -1)
		{
			tmp[i] = thiz->brick[0] + thiz->width;
		}
		else if(diff == 2)
		{
			tmp[i] = thiz->brick[0] - 2 * thiz->width;
		}
		else if(diff == -2)
		{
			tmp[i] = thiz->brick[0] + 2 * thiz->width;
		}
		else if(diff == thiz->width - 1)
		{
			tmp[i] = thiz->brick[0] + thiz->width + 1;
		}
		else if(diff == 1 - thiz->width)
		{
			tmp[i] = thiz->brick[0] - thiz->width - 1;
		}
		else if(diff == thiz->width)
		{
			if((thiz->brick[0] + 1) % thiz->width == 0) 
			{
				/* restore the deleted brick */
				rt_tetris_append_brick(thiz, thiz->brick);				
				return -RT_ERROR;
			}
			else tmp[i] = thiz->brick[0] + 1;
		}
		else if(diff == -1 * (thiz->width))
		{
			if(thiz->brick[0] % thiz->width == 0)
			{	
				/* restore the deleted brick */
				rt_tetris_append_brick(thiz, thiz->brick);			
				return -RT_ERROR;
			}
			else tmp[i] = thiz->brick[0] - 1;
		}
		else if(diff == thiz->width + 1)
		{
			tmp[i] = thiz->brick[0] - thiz->width + 1;
		}
		else if(diff == -1 - thiz->width)
		{
			tmp[i] = thiz->brick[0] + thiz->width - 1;
		}
		else if(diff == 2 * thiz->width)
		{
			if((thiz->brick[0] % thiz->width) >= (thiz->width - 2)) 
			{
				/* restore the deleted brick */
				rt_tetris_append_brick(thiz, thiz->brick);			
				return -RT_ERROR;
			}
			else tmp[i] = thiz->brick[0] + 2;
		}
		else if(diff == -2 * thiz->width)
		{
			if((thiz->brick[0] % thiz->width) < 2) 
			{
				/* restore the deleted brick */
				rt_tetris_append_brick(thiz, thiz->brick);			
				return -RT_ERROR;
			}
			else tmp[i] = thiz->brick[0] - 2;
		}
		
		if(tmp[i] > (thiz->height) * thiz->width) 
		{
			/* restore the deleted brick */
			rt_tetris_append_brick(thiz, thiz->brick);
			return -RT_ERROR;		
		}

		if(rt_tetris_check_collision(thiz, tmp[i]) == RT_EOK)
		{
			/* restore the deleted brick */
			rt_tetris_append_brick(thiz, thiz->brick);
			return -RT_ERROR;		
		}
	}

	/* do roration */
	for(i=0; i<4; i++)
	{
		thiz->brick[i] = tmp[i];
	}

	/* append the brick to tetris panel */
	rt_tetris_append_brick(thiz, thiz->brick);

	/* update view */
	thiz->view->update(thiz->view, thiz);

	return RT_EOK;
}

/**
 * this function add a view to the tetris
 *
 * @param thiz the tetris instance.
 * @param view the view instance.
 *
 * @return RT_EOK on success, -RT_ERROR on fail
 */
rt_err_t rt_tetris_add_view(rt_tetris_t* thiz, rt_tetris_view_t* view)
{
	RT_ASSERT(thiz != RT_NULL);

	/* Only suppurt single view now */
	thiz->view = view;

	return RT_EOK;
}

/**
 * this function delete a view from the tetris
 *
 * @param thiz the tetris instance.
 * @param view the view instance.
 *
 * @return RT_EOK on success, -RT_ERROR on fail
 */

rt_err_t rt_tetris_delete_view(rt_tetris_t* thiz, rt_tetris_view_t* view)
{
	RT_ASSERT(thiz != RT_NULL);

	thiz->view = RT_NULL;

	return RT_EOK;
}

/**
 * this function used to check collision
 *
 * @param thiz the tetris instance.
 * @param block the block to be checked.
 *
 * @return RT_EOK on collision, -RT_ERROR on not collision
 */
rt_err_t rt_tetris_check_collision(rt_tetris_t* thiz, rt_uint32_t block)
{
	RT_ASSERT(thiz != RT_NULL);
	RT_ASSERT(block < thiz->height * thiz->width);

	if((thiz->panel[block/thiz->width] & (1 << (block % thiz->width)))
		== (1 << (block % thiz->width)))
	{
		return RT_EOK;
	}
	else
	{
		return -RT_ERROR;
	}
}

static rt_err_t rt_tetris_update_brick(rt_tetris_t* thiz)
{
	int index;

	RT_ASSERT(thiz != RT_NULL);

	index = (int)(7.0 * rand()/(RAND_MAX + 1.0)); 

	rt_memcpy(thiz->brick, thiz->next_brick, 4 * sizeof(rt_uint32_t));
	rt_memcpy(thiz->next_brick, g_brick[index], 4 * sizeof(rt_uint32_t));

	/* update next brick on view */
	thiz->view->update_next_brick(thiz->view, thiz);
		
	return RT_EOK;
}

static rt_err_t rt_tetris_append_brick(rt_tetris_t* thiz, rt_uint32_t brick[])
{
	int i;

	RT_ASSERT(thiz != RT_NULL);
	RT_ASSERT(brick != RT_NULL);

	for(i=0; i<4; i++)
	{
		int y = brick[i]/thiz->width;
		int x = brick[i]%thiz->width;

		thiz->panel[y] |= (1<<x);
	}

	return RT_EOK;
}

static rt_err_t rt_tetris_delete_brick(rt_tetris_t* thiz, rt_uint32_t brick[])
{
	int i;

	RT_ASSERT(thiz != RT_NULL);
	RT_ASSERT(brick != RT_NULL);

	for(i=0; i<4; i++)
	{
		int y = brick[i]/thiz->width;
		int x = brick[i]%thiz->width;

		thiz->panel[y] &= ~(1<<x);
	}

	return RT_EOK;
}

static rt_err_t rt_tetris_is_reach_top(rt_tetris_t* thiz, rt_uint32_t brick[])
{
	int i;

	RT_ASSERT(thiz != RT_NULL);
	RT_ASSERT(brick != RT_NULL);

	for(i=0; i<4; i++)	
	{
		if(brick[i] / thiz->width == 0)
			return RT_EOK;
	}

	return -RT_ERROR;
}

static rt_err_t rt_tetris_release_lines(rt_tetris_t* thiz, rt_uint32_t brick[])
{
	int i, j, check_line = 0;
	rt_bool_t line_released = -RT_ERROR;

	RT_ASSERT(thiz != RT_NULL);
	RT_ASSERT(brick != RT_NULL);

	for(i=0; i<4; i++)
	{
		/* choose a line */
		check_line = brick[i]/thiz->width;	
		if((thiz->panel[check_line]) == ((1 << thiz->width) - 1))
		{
			for(j=check_line; j>0; j--)
			{
				thiz->panel[j] = thiz->panel[j-1];
			}
			
			/* clear the first line */
			thiz->panel[0] = 0;
			
			for(j=i+1; j<4; j++)
			{
				if(brick[j] < brick[i])
				{
					brick[j] += thiz->width;
				}
			}
			
			thiz->lines++;
			thiz->score += 100;
			line_released = RT_EOK;				
		}
	}

	if(line_released == RT_EOK)
	{
		/* update view */
		thiz->view->update_score_and_lines(thiz->view, thiz);
		return RT_EOK;
	}
	else
	{
		return -RT_ERROR;
	}
}
qiuyiuestc's avatar
qiuyiuestc 已提交
730