Board::Board(int id, Resources *rsc): _id(id), _rsc(rsc), _timer(id, InitialTimeout), _player(rsc), _rowLastPiece(16), _score(id, rsc), _sentPieces(0), _gameOver(false), _pendZiiped(0) { Row tmpRow(horiRowsLen, 1, 0, _rsc); _rows.resize(4, tmpRow); tmpRow = Row(vertRowsLen, 0, -1, _rsc); _rows.resize(8, tmpRow); tmpRow = Row(horiRowsLen, -1, 0, _rsc); _rows.resize(12, tmpRow); tmpRow = Row(vertRowsLen, 0, 1, _rsc); _rows.resize(16, tmpRow); SDL_Rect gridRect = _rsc->getGridArea(id); fillRowsRects(gridRect, _rsc->getBlockSize(), _rows); const SDL_Rect playerRect = getPlayerRect(gridRect, _rsc->getBlockSize()); _player.setOriginAndSize(playerRect); }
// this function reads a file for "," separated values and creates a matrix object from it int CFmDataFrame::Load(const char* filename, bool bRowName, bool bColName) { _log_debug(_HI_, "CFmDataFrame::Load:%s, bRowName=%d, bColName=%d", filename, bRowName, bColName); FILE* fp = fopen( filename, "rt"); if (fp==NULL) { _log_error(_HI_, "The file can not be opened(%s)", filename); return(ERR_OPEN_FILE); } char aLine[MAX_LINE_WIDTH+1]={0}; if( fgets( aLine, MAX_LINE_WIDTH, fp ) == NULL) { _log_error( _HI_, "Failed to read a line (%s)", filename); fclose(fp); return(ERR_READ_FILE); } CFmVectorStr vctRowNames(0); CFmVectorStr vctColNames(0); int col_size = 0; // count how many elements in the first lines const char seps1[] = ",\t\n\r"; char* running = Strdup (aLine); char* runnptr = running; char* token = _strsep((char**)&running, seps1 ); while(token) { if(strlen(token)!=0) { vctColNames.Put( token); col_size++; } token = _strsep( (char**)&running, seps1 ); } Free(runnptr); _log_debug(_HI_, "CFmDataFrame::Load: col_size: %d", col_size); CFmVector tmpRow(col_size, 0.0); if (bRowName && !bColName) tmpRow.Resize(col_size-1); int nMaxRows = 20; int nMaxCols = tmpRow.GetLength(); m_nNumCols = tmpRow.GetLength(); m_nNumRows = 0; AllocMemory( nMaxRows, nMaxCols ); if (!bColName) { if (!bRowName) { m_nNumCols = col_size; for (int i=0; i<col_size; i++) Set(0, i, vctColNames[i]); } else { m_nNumCols = col_size-1; for (int i=1; i<col_size+1; i++) Set(0, i-1, vctColNames[i]); vctRowNames.Put( vctColNames[0] ); } m_nNumRows++; } CFmVectorStr vctTmpRow(0); while(!feof(fp)) { if( fgets( aLine, MAX_LINE_WIDTH, fp ) == NULL) break; char* running = Strdup (aLine); vctTmpRow.Reset(0); char* token = _strsep( &running, seps1 ); int nNonEmty = 0; while(token) { if (strlen(token)!=0 && strncmp(token, "\n", 1)!=0 && strncmp(token, "\r", 1)!=0 && strncmp(token, "\t", 1)!=0) nNonEmty ++; vctTmpRow.Put(token); token = _strsep( &running, seps1 ); } Free(running); if (nNonEmty==0) break; m_nNumRows++; if (m_nNumRows >= m_nMaxRows ) AllocMemory( m_nNumRows+20, m_nMaxCols ); if (!bRowName) { for (int i=0; i<col_size && i<vctTmpRow.GetLength(); i++) Set( m_nNumRows-1, i, vctTmpRow[i] ); } else { for (int i=1; i<col_size+1 && i<vctTmpRow.GetLength(); i++) { Set( m_nNumRows-1, i-1, vctTmpRow[i] ); } vctRowNames.Put( vctTmpRow[0]); } } fclose(fp); CFmNewTemp refNew; if (bRowName) m_pRowNames = new (refNew) CFmVectorStr( &vctRowNames ); if (bColName) m_pColNames = new (refNew) CFmVectorStr( &vctColNames ); _log_debug(_HI_, "CFmDataFrame::Load:%d,%d", m_nNumRows, m_nNumCols); return(0); }
vector<T> matrix<T>::solve(vector<T> RHS) { #ifdef _DEBUG if (this->dims[0] != this->dims[1]) FatalErrorST("Can only use Gaussian elimination on a square matrix."); #endif // Gaussian elimination with full pivoting // not to be used where speed is paramount vector<T> vec(this->dims[0]); vector<T> solution(this->dims[0]); vector<int> swap_0(this->dims[0],1); vector<int> swap_1(this->dims[0],1); vector<T> tmpRow(this->dims[0]); matrix<T> LHS(*this); // setup swap arrays for(uint i=0; i<this->dims[0]; i++) { swap_0[i]=i; swap_1[i]=i; } // make triangular for(uint k=0; k<this->dims[0]-1; k++) { T max = 0; T mag; // find pivot int pivot_i = 0; int pivot_j = 0; for(uint i=k; i<this->dims[0]; i++) { for(uint j=k; j<this->dims[0]; j++) { mag = LHS(i,j)*LHS(i,j); if(mag>max) { pivot_i = i; pivot_j = j; max = mag; } } } // swap the swap arrays int itemp_0 = swap_0[k]; swap_0[k] = swap_0[pivot_i]; swap_0[pivot_i] = itemp_0; itemp_0 = swap_1[k]; swap_1[k] = swap_1[pivot_j]; swap_1[pivot_j] = itemp_0; // swap the columns for(uint i=0; i<this->dims[0]; i++) { tmpRow[i] = LHS(i,pivot_j); LHS(i,pivot_j) = LHS(i,k); LHS(i,k) = tmpRow[i]; } // swap the rows for(uint j=0; j<this->dims[0]; j++) { tmpRow[j] = LHS(pivot_i,j); LHS(pivot_i,j) = LHS(k,j); LHS(k,j) = tmpRow[j]; } T tmp = RHS[pivot_i]; RHS[pivot_i] = RHS[k]; RHS[k] = tmp; // subtraction for(uint i=k+1; i<this->dims[0]; i++) { T first = LHS(i,k); RHS[i] = RHS[i] - first/LHS(k,k)*RHS[k]; for(uint j=k; j<this->dims[0]; j++) LHS(i,j) = LHS(i,j) - (first/LHS(k,k))*LHS(k,j); } // exact zero for(uint j=0; j<k+1; j++) { for(uint i=j+1; i<this->dims[0]; i++) { LHS(i,j)=0.0; } } } // back substitute for(int i=(int)this->dims[0]-1; i>=0; i--) { T dtemp_0 = 0.0; for(uint k = i+1; k<this->dims[0]; k++) { dtemp_0 = dtemp_0 + (LHS(i,k)*vec[k]); } vec[i] = (RHS[i]-dtemp_0)/LHS(i,i); } // swap solution rows for(uint i=0; i<this->dims[0]; i++) solution[swap_1[i]] = vec[i]; return solution; }