git repos / blockattack-game

blame: source/code/editor/TheBoard.cpp

normal view · raw

89c4a3a6 sago007 2008-08-29 14:32 1
/*
c53e6443 sago007 2012-04-17 11:04 2
===========================================================================
c53e6443 sago007 2012-04-17 11:04 3
blockattack - Block Attack - Rise of the Blocks
c53e6443 sago007 2012-04-17 11:04 4
Copyright (C) 2005-2012 Poul Sander
c53e6443 sago007 2012-04-17 11:04 5
c53e6443 sago007 2012-04-17 11:04 6
This program is free software: you can redistribute it and/or modify
c53e6443 sago007 2012-04-17 11:04 7
it under the terms of the GNU General Public License as published by
c53e6443 sago007 2012-04-17 11:04 8
the Free Software Foundation, either version 2 of the License, or
c53e6443 sago007 2012-04-17 11:04 9
(at your option) any later version.
c53e6443 sago007 2012-04-17 11:04 10
c53e6443 sago007 2012-04-17 11:04 11
This program is distributed in the hope that it will be useful,
c53e6443 sago007 2012-04-17 11:04 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
c53e6443 sago007 2012-04-17 11:04 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c53e6443 sago007 2012-04-17 11:04 14
GNU General Public License for more details.
c53e6443 sago007 2012-04-17 11:04 15
c53e6443 sago007 2012-04-17 11:04 16
You should have received a copy of the GNU General Public License
c53e6443 sago007 2012-04-17 11:04 17
along with this program.  If not, see http://www.gnu.org/licenses/
c53e6443 sago007 2012-04-17 11:04 18
c53e6443 sago007 2012-04-17 11:04 19
Source information and contacts persons can be found at
c53e6443 sago007 2012-04-17 11:04 20
http://blockattack.sf.net
c53e6443 sago007 2012-04-17 11:04 21
===========================================================================
89c4a3a6 sago007 2008-08-29 14:32 22
*/
89c4a3a6 sago007 2008-08-29 14:32 23
89c4a3a6 sago007 2008-08-29 14:32 24
#include "TheBoard.hpp"
89c4a3a6 sago007 2008-08-29 14:32 25
89c4a3a6 sago007 2008-08-29 14:32 26
TheBoard::TheBoard()
89c4a3a6 sago007 2008-08-29 14:32 27
{
c53e6443 sago007 2012-04-17 11:04 28
	numberOfMoves = 0;
c53e6443 sago007 2012-04-17 11:04 29
	for(int i = 0; i < BOARDWIDTH; i++)
c53e6443 sago007 2012-04-17 11:04 30
		for(int j = 0; j < BOARDHEIGHT; j++)
c53e6443 sago007 2012-04-17 11:04 31
			TheBoard::board[i][j] = -1;
c53e6443 sago007 2012-04-17 11:04 32
	isNull = false;
89c4a3a6 sago007 2008-08-29 14:32 33
}
89c4a3a6 sago007 2008-08-29 14:32 34
89c4a3a6 sago007 2008-08-29 14:32 35
TheBoard::TheBoard(const TheBoard &tb)
89c4a3a6 sago007 2008-08-29 14:32 36
{
c53e6443 sago007 2012-04-17 11:04 37
	TheBoard::numberOfMoves = tb.numberOfMoves;
c53e6443 sago007 2012-04-17 11:04 38
	for(int i = 0; i < BOARDWIDTH; i++)
c53e6443 sago007 2012-04-17 11:04 39
		for(int j = 0; j < BOARDHEIGHT; j++)
c53e6443 sago007 2012-04-17 11:04 40
			TheBoard::board[i][j] = tb.board[i][j];
c53e6443 sago007 2012-04-17 11:04 41
	isNull = false;
89c4a3a6 sago007 2008-08-29 14:32 42
}
89c4a3a6 sago007 2008-08-29 14:32 43
89c4a3a6 sago007 2008-08-29 14:32 44
void TheBoard::copyFrom(TheBoard *tb)
89c4a3a6 sago007 2008-08-29 14:32 45
{
c53e6443 sago007 2012-04-17 11:04 46
	TheBoard::numberOfMoves = tb->numberOfMoves;
c53e6443 sago007 2012-04-17 11:04 47
	for(int i = 0; i < BOARDWIDTH; i++)
c53e6443 sago007 2012-04-17 11:04 48
		for(int j = 0; j < BOARDHEIGHT; j++)
c53e6443 sago007 2012-04-17 11:04 49
			TheBoard::board[i][j] = tb->board[i][j];
89c4a3a6 sago007 2008-08-29 14:32 50
}
89c4a3a6 sago007 2008-08-29 14:32 51
89c4a3a6 sago007 2008-08-29 14:32 52
bool TheBoard::setBrick(int x, int y, int color)
89c4a3a6 sago007 2008-08-29 14:32 53
{
c53e6443 sago007 2012-04-17 11:04 54
	if((x<0)||(x>=BOARDWIDTH)||(y<0)||(y>=12))
c53e6443 sago007 2012-04-17 11:04 55
		return false;
c53e6443 sago007 2012-04-17 11:04 56
	board[x][y]=color;
c53e6443 sago007 2012-04-17 11:04 57
	return true;
89c4a3a6 sago007 2008-08-29 14:32 58
}
89c4a3a6 sago007 2008-08-29 14:32 59
89c4a3a6 sago007 2008-08-29 14:32 60
int TheBoard::getBrick(int x, int y)
89c4a3a6 sago007 2008-08-29 14:32 61
{
c53e6443 sago007 2012-04-17 11:04 62
	if((x<0)||(x>=BOARDWIDTH)||(y<0)||(y>=BOARDHEIGHT))
c53e6443 sago007 2012-04-17 11:04 63
		return -999;
c53e6443 sago007 2012-04-17 11:04 64
	return board[x][y];
89c4a3a6 sago007 2008-08-29 14:32 65
}
89c4a3a6 sago007 2008-08-29 14:32 66
89c4a3a6 sago007 2008-08-29 14:32 67
void TheBoard::setNumberOfMoves(int moves)
89c4a3a6 sago007 2008-08-29 14:32 68
{
c53e6443 sago007 2012-04-17 11:04 69
	TheBoard::numberOfMoves = moves;
89c4a3a6 sago007 2008-08-29 14:32 70
}
89c4a3a6 sago007 2008-08-29 14:32 71
89c4a3a6 sago007 2008-08-29 14:32 72
int TheBoard::getNumberOfMoves()
89c4a3a6 sago007 2008-08-29 14:32 73
{
c53e6443 sago007 2012-04-17 11:04 74
	return TheBoard::numberOfMoves;
89c4a3a6 sago007 2008-08-29 14:32 75
}
89c4a3a6 sago007 2008-08-29 14:32 76
89c4a3a6 sago007 2008-08-29 14:32 77
bool TheBoard::moveUp(int x, int y)
89c4a3a6 sago007 2008-08-29 14:32 78
{
c53e6443 sago007 2012-04-17 11:04 79
	if((x<0)||(x>=BOARDWIDTH)||(y<0)||(y>=BOARDHEIGHT))
c53e6443 sago007 2012-04-17 11:04 80
		return false;
c53e6443 sago007 2012-04-17 11:04 81
	for(int i=1; i<=y; i++)
c53e6443 sago007 2012-04-17 11:04 82
	{
c53e6443 sago007 2012-04-17 11:04 83
		board[x][i-1]=board[x][i];
c53e6443 sago007 2012-04-17 11:04 84
	}
c53e6443 sago007 2012-04-17 11:04 85
	board[x][y]=-1;
c53e6443 sago007 2012-04-17 11:04 86
	return true;
89c4a3a6 sago007 2008-08-29 14:32 87
}  //moveUp
89c4a3a6 sago007 2008-08-29 14:32 88
89c4a3a6 sago007 2008-08-29 14:32 89
bool TheBoard::moveDown(int x, int y)
89c4a3a6 sago007 2008-08-29 14:32 90
{
c53e6443 sago007 2012-04-17 11:04 91
	if((x<0)||(x>=BOARDWIDTH)||(y<0)||(y>=BOARDHEIGHT))
c53e6443 sago007 2012-04-17 11:04 92
		return false;
c53e6443 sago007 2012-04-17 11:04 93
	for(int i=y-1; i>=0; i--)
c53e6443 sago007 2012-04-17 11:04 94
	{
c53e6443 sago007 2012-04-17 11:04 95
		board[x][i+1]=board[x][i];
c53e6443 sago007 2012-04-17 11:04 96
	}
c53e6443 sago007 2012-04-17 11:04 97
	board[x][0]=-1;
c53e6443 sago007 2012-04-17 11:04 98
	return true;
89c4a3a6 sago007 2008-08-29 14:32 99
}
89c4a3a6 sago007 2008-08-29 14:32 100
89c4a3a6 sago007 2008-08-29 14:32 101
void TheBoard::moveRight()
89c4a3a6 sago007 2008-08-29 14:32 102
{
c53e6443 sago007 2012-04-17 11:04 103
	for(int i=0; i<BOARDHEIGHT; i++)
c53e6443 sago007 2012-04-17 11:04 104
	{
c53e6443 sago007 2012-04-17 11:04 105
		for(int j=BOARDWIDTH-2; j>=0; j--)
89c4a3a6 sago007 2008-08-29 14:32 106
		{
c53e6443 sago007 2012-04-17 11:04 107
			board[j+1][i]=board[j][i];
89c4a3a6 sago007 2008-08-29 14:32 108
		}
c53e6443 sago007 2012-04-17 11:04 109
		board[0][i] = -1;
c53e6443 sago007 2012-04-17 11:04 110
	}
89c4a3a6 sago007 2008-08-29 14:32 111
} //moveRight
89c4a3a6 sago007 2008-08-29 14:32 112
89c4a3a6 sago007 2008-08-29 14:32 113
89c4a3a6 sago007 2008-08-29 14:32 114
//moves all pieces one to the right
89c4a3a6 sago007 2008-08-29 14:32 115
void TheBoard::moveLeft()
89c4a3a6 sago007 2008-08-29 14:32 116
{
c53e6443 sago007 2012-04-17 11:04 117
	for(int i=0; i<BOARDHEIGHT; i++)
c53e6443 sago007 2012-04-17 11:04 118
	{
c53e6443 sago007 2012-04-17 11:04 119
		for(int j=1; j<=BOARDWIDTH-1; j++)
89c4a3a6 sago007 2008-08-29 14:32 120
		{
c53e6443 sago007 2012-04-17 11:04 121
			board[j-1][i]=board[j][i];
89c4a3a6 sago007 2008-08-29 14:32 122
		}
c53e6443 sago007 2012-04-17 11:04 123
		board[BOARDWIDTH-1][i] = -1;
c53e6443 sago007 2012-04-17 11:04 124
	}
89c4a3a6 sago007 2008-08-29 14:32 125
} //moveRight
89c4a3a6 sago007 2008-08-29 14:32 126
89c4a3a6 sago007 2008-08-29 14:32 127
bool TheBoard::equals(TheBoard &tb2)
89c4a3a6 sago007 2008-08-29 14:32 128
{
c53e6443 sago007 2012-04-17 11:04 129
	if(tb2.getNumberOfMoves()!=getNumberOfMoves())
c53e6443 sago007 2012-04-17 11:04 130
		return false;
c53e6443 sago007 2012-04-17 11:04 131
	for(int i=0; i<BOARDWIDTH; i++)
c53e6443 sago007 2012-04-17 11:04 132
		for(int j=0; j<BOARDHEIGHT; j++)
89c4a3a6 sago007 2008-08-29 14:32 133
		{
89c4a3a6 sago007 2008-08-29 14:32 134
			if(tb2.board[i][j]!=board[i][j])
89c4a3a6 sago007 2008-08-29 14:32 135
				return false;
89c4a3a6 sago007 2008-08-29 14:32 136
		}
c53e6443 sago007 2012-04-17 11:04 137
	return true;
89c4a3a6 sago007 2008-08-29 14:32 138
}
1970-01-01 00:00 139