so I am a relatively new to coding. So I have to make a Gomoku game for a project. Gomoku is like tic-tac-toe, but have to get five in a row. Have been given certain restrictions such as a board size of 6x6 to 15x15. The use of at least one class. I have chosen to use arrays. I have created PlayerOne as an algorithm which bases its moves on PlayerOne's last move, and moves one random block away from it in next move. The second algorithm is random. For when checking for a win, my horizontal and vertical checkers seem to work but both my diagonal checkers are giving problems, with them saying that 3 consecutive blocks in a row diagonally is 5 in a row. I created my checkers separately, filling in each row manually to test, and the diagonal checkers were fine there, but they're not fine in the whole code.
I'll share both the diagonal checkers up here, and then the whole code a little below so hopefully easier to read. Also quite a bit of it has cout statements as was trying to see where problems where earlier in the process. If anyone can see where the issue is occurring if could point out where, would be so appreciative.
int game::diagonalCheckerNegSlope(int arr[15][15], int size) {
int count1;
int count2;
int whoWon = 0;
for (int i = 0; i < size; i++) {
count1 = 0;
count2 = 0;
for (int j = 0; j < size; j++) {
if (arr[j][i] == 1) {
for (int d = 0; (i + d < size) && (j + d < size); d++)
if (arr[i + d][j + d] == 1) {
count1++;
if (count1 == 5) {
whoWon = 1;
}
} else {
count1 = 0;
}
}
if (arr[j][i] == 2) {
for (int d = 0; (i + d < size) && (j + d < size); d++)
if (arr[i + d][j + d] == 2) {
count2++;
if (count2 == 5) {
whoWon = 2;
}
} else {
count2 = 0;
}
}
}
}
if (whoWon != 1 && whoWon != 2) {
whoWon = 3;
}
return whoWon;
}
int game::diagonalCheckerPosSlope(int arr[15][15], int size) {
int count1;
int count2;
int whoWon = 0;
for (int i = 0; i < size; i++) {
count1 = 0;
count2 = 0;
for (int j = 0; j < size; j++) {
if (arr[i][j] == 1) {
for (int d = 0; (i + d < size) && (j - d < size); d++)
if (arr[i + d][j - d] == 1) {
count1++;
if (count1 == 5) {
whoWon = 1;
}
} else {
count1 = 0;
}
}
if (arr[i][j] == 2) {
for (int d = 0; (i + d < size) && (j - d < size); d++)
if (arr[i + d][j - d] == 2) {
count2++;
if (count2 == 5) {
whoWon = 2;
}
} else {
count2 = 0;
}
}
}
}
if (whoWon != 1 && whoWon != 2) {
whoWon = 3;
}
return whoWon;
}
int main code
#include <iostream>
#include <fstream>
#include "game.h"
#include <iomanip>
#include <ctime>
using namespace std;
int main() {
string inputSize = "input.txt";
int size =0;
srand(time(0));
int arr[15][15];
game Gomuko;
size = Gomuko.getSize(inputSize, arr);
if(size >=6 && size <=15){
Gomuko.initZero(arr, size);
// Gomuko.printArr(arr,size);
Gomuko.Play(arr, size);
Gomuko.printArr(arr,size);
}else{
cout << "Invalid input" << endl;
}
return 0;
}
The .h file of the class
#ifndef GAME_H_
#define GAME_H_
#include <string>
#include <ctime>
using namespace std;
class game {
public:
game();
int getSize(string inputFileName, int dataArray[15][15]);
void initZero(int arr[15][15], int size);
void printArr(int arr[15][15], int size);
void movePlayerOne(int arr[15][15], int size, int counter2);
void movePlayerTwo(int arr[15][15], int size);
void Play(int arr[15][15], int size);
int PlayerOneSurroundRow(int arr[15][15],int size);
int PlayerOneSurroundCol(int arr[15][15],int size);
int findFirstMoveRow(int arr[15][15], int size);
int findFirstMoveCol(int arr[15][15], int size);
bool isValid(int newRow, int newCol, int size);
int horizontalChecker(int arr[15][15], int size);
int verticalChecker(int arr[15][15], int size);
int diagonalCheckerNegSlope(int arr[15][15], int size);
int diagonalCheckerPosSlope(int arr[15][15], int size);
int WholeWinnerChecker(int arr[15][15], int size);
private:
int randRow(int size);
int randCol(int size);
};
#endif /* GAME_H_ */
And finally the .cpp file of the class
#include "game.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
using namespace std;
game::game() {
// TODO Auto-generated constructor stub
}
int game::getSize(string inputFileName, int dataArray[15][15]) {//make between 6 and 15.
ifstream inputData;
int size = 0;
int counter = 0;
inputData.open(inputFileName);
if (!inputData) {
cout << "Cannot open the file \"" << inputFileName << "\"" << endl;
}
else {
while (inputData >> size) {
counter++;
}
cout << "There are " << counter << " board sizes in the inputFile"
<< endl;
cout << "The size of the board is " << size << "x" << size << endl
<< endl;
}
return size;
}
void game::initZero(int arr[15][15], int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
arr[i][j] = 0;
}
}
}
void game::printArr(int arr[15][15], int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
}
int game::randRow(int size) {
int randNoRow = 0;
randNoRow = rand() % size;
return randNoRow;
}
int game::randCol(int size) {
int randNoCol = 0;
randNoCol = rand() % size;
return randNoCol;
}
int game::findFirstMoveRow(int arr[15][15], int size) {
int positionRow;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (arr[i][j] == 1) {
positionRow = i;
}
}
}
return positionRow;
}
int game::findFirstMoveCol(int arr[15][15], int size) {
int positionCol;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (arr[i][j] == 1) {
positionCol = j;
}
}
}
return positionCol;
}
int game::PlayerOneSurroundRow(int arr[15][15], int size) {
int positionRow = game::findFirstMoveRow(arr, size);
// cout << "The old row is " << positionRow << endl;
int oldRow = positionRow;
int randChoice = 0;
int newRow = 0;
randChoice = (rand() % 3);
// cout << "randChoice Row case: " << randChoice << endl;
switch (randChoice) {
case 0:
newRow = oldRow - 1;
break;
case 1:
newRow = oldRow;
break;
case 2:
newRow = oldRow + 1;
break;
}
// cout << "Test2" << endl << endl;
if (newRow > size - 1) {
// cout << "Row too big as Row is " << newRow << endl;
newRow = newRow - 1;
// cout << "Row is now " << newRow << endl;
}
if (newRow < 0) {
// cout << "Row too small as row is " << newRow << endl;
newRow = newRow + 1;
// cout << "Row is now " << newRow << endl;
}
// cout << "The new row is: " << newRow << endl;
return newRow;
}
int game::PlayerOneSurroundCol(int arr[15][15], int size) {
int positionCol = findFirstMoveCol(arr, size);
// cout << "The old col is " << positionCol << endl;
int oldCol = positionCol;
int randChoice = 0;
int newCol = 0;
randChoice = (rand() % 3);
cout << "randChoice Col case: " << randChoice << endl;
switch (randChoice) {
case 0:
newCol = oldCol - 1;
break;
case 1:
newCol = oldCol;
break;
case 2:
newCol = oldCol + 1;
break;
}
// cout << "Test2" << endl << endl;
if (newCol > size - 1) {
// cout << "Col too big as is " << newCol << endl;
newCol = newCol - 1;
// cout << "Col is now " << newCol << endl;
}
if (newCol < 0) {
// cout << "Col too small as is " << newCol << endl;
newCol = newCol + 1;
// cout << "Col is now " << newCol << endl;
}
// cout << "The new col is: " << newCol << endl;
return newCol;
}
bool game::isValid(int newRow, int newCol, int size) {
bool valid = false;
if (((newRow < size) && (newCol < size))
&& ((newRow >= 0) && (newCol >= 0))) {
valid = true;
}
return valid;
}
void game::movePlayerOne(int arr[15][15], int size, int counter2) {
int newRow = 0;
int newCol = 0;
int oldRow = 0;
int oldCol = 0;
int count3 = 0;
if (counter2 == 0) {
oldRow = randRow(size);
oldCol = randCol(size);
arr[oldRow][oldCol] = 1;
// cout << "Test1" << endl << endl << endl;
counter2++;
}
else {
// cout << "Test 3" << endl;
newRow = game::PlayerOneSurroundRow(arr, size);
newCol = game::PlayerOneSurroundCol(arr, size);
if (arr[newRow][newCol] == 0 && isValid(newRow, newCol, size)) {
arr[newRow][newCol] = 1;
// cout << "Test4" << endl;
// cout << "randNoRow = " << newRow << endl;
// cout << "randNoCol = " << newCol << endl;
oldRow = newRow;
oldCol = newCol;
}
else if ((arr[newRow][newCol] == 1) || (arr[newRow][newCol] == 2)
|| (newRow > size) || (newCol > size) || (newRow < 0)
|| (newCol < 0)) {
cout
<< "There has been a match, or even out of bounds, going again. "
<< endl << endl;
while ((arr[newRow][newCol] == 1) || (arr[newRow][newCol] == 2)) {
// cout << "Test5" << endl;
newRow = game::PlayerOneSurroundRow(arr, size);
newCol = game::PlayerOneSurroundCol(arr, size);
// cout << "randNoRow = " << newRow << endl;
// cout << "randNoCol = " << newCol << endl;
count3++;
if ((arr[newRow][newCol] != 1 && arr[newRow][newCol] != 2)
&& isValid(newRow, newCol, size)) {
arr[newRow][newCol] = 1;
// cout << "Test6" << endl;
//
// cout << "randNoRow = " << newRow << endl;
// cout << "randNoCol = " << newCol << endl;
oldRow = newRow;
oldCol = newCol;
break;
}
if (count3++ > 3) {
// cout << "Test 7" << endl;
// newRow = randRow(size);
// newCol = randCol(size);
while (arr[newRow][newCol] == 1 || arr[newRow][newCol] == 2) {
newRow = randRow(size);
newCol = randCol(size);
// cout << "Test 8" << endl;
// cout << "randNoRow = " << newRow << endl;
// cout << "randNoCol = " << newCol << endl;
if (arr[newRow][newCol] == 0) {
arr[newRow][newCol] = 1;
// cout << "Test 9" << endl;
// cout << "randNoRow = " << newRow << endl;
// cout << "randNoCol = " << newCol << endl;
oldRow = newRow;
oldCol = newCol;
break;
}
}
break;
}
}
}
}
}
void game::movePlayerTwo(int arr[15][15], int size) {
// cout << "Test" << endl;
int randNoRow = 0;
int randNoCol = 0;
randNoRow = randRow(size);
cout << "randNoRow = " << randNoRow << endl;
randNoCol = randCol(size);
cout << "randNoCol = " << randNoCol << endl;
cout << endl;
if ((arr[randNoRow][randNoCol] == 1) || (arr[randNoRow][randNoCol] == 2)) {
//cout << "There has been a match, going again. " << endl << endl;
while ((arr[randNoRow][randNoCol] == 1)
|| (arr[randNoRow][randNoCol] == 2)) {
int randNoRow = 0;
int randNoCol = 0;
randNoRow = randRow(size);
//cout << "randNoRow = " << randNoRow << endl;
randNoCol = randCol(size);
//cout << "randNoCol = " << randNoCol << endl;
//cout << endl;
if (arr[randNoRow][randNoCol] == 0) {
arr[randNoRow][randNoCol] = 2;
break;
}
}
} else {
arr[randNoRow][randNoCol] = 2;
}
}
int game::horizontalChecker(int arr[15][15], int size) {
int count1;
int count2;
int whoWon = 0;
for (int i = 0; i < size; i++) {
count1 = 0;
count2 = 0;
for (int j = 0; j < size; j++) {
if (arr[i][j] == 1) {
count1++;
if (count1 == 5) {
whoWon = 1;
}
} else {
count1 = 0;
}
if (arr[j][i] == 2) {
count2++;
if (count2 == 5) {
whoWon = 2;
}
} else {
count2 = 0;
}
}
if (whoWon != 1 && whoWon != 2) {
whoWon = 3;
}
}
return whoWon;
}
int game::verticalChecker(int arr[15][15], int size) {
int count1;
int count2;
int whoWon = 0;
for (int i = 0; i < size; i++) {
count1 = 0;
count2 = 0;
for (int j = 0; j < size; j++) {
if (arr[j][i] == 1) {
count1++;
if (count1 == 5) {
whoWon = 1;
}
} else {
count1 = 0;
}
if (arr[j][i] == 2) {
count2++;
if (count2 == 5) {
whoWon = 2;
}
} else {
count2 = 0;
}
}
if (whoWon != 1 && whoWon != 2) {
whoWon = 3;
}
}
return whoWon;
}
int game::diagonalCheckerNegSlope(int arr[15][15], int size) {
int count1;
int count2;
int whoWon = 0;
for (int i = 0; i < size; i++) {
count1 = 0;
count2 = 0;
for (int j = 0; j < size; j++) {
if (arr[j][i] == 1) {
for (int d = 0; (i + d < size) && (j + d < size); d++)
if (arr[i + d][j + d] == 1) {
count1++;
if (count1 == 5) {
whoWon = 1;
}
} else {
count1 = 0;
}
}
if (arr[j][i] == 2) {
for (int d = 0; (i + d < size) && (j + d < size); d++)
if (arr[i + d][j + d] == 2) {
count2++;
if (count2 == 5) {
whoWon = 2;
}
} else {
count2 = 0;
}
}
}
}
if (whoWon != 1 && whoWon != 2) {
whoWon = 3;
}
return whoWon;
}
int game::diagonalCheckerPosSlope(int arr[15][15], int size) {
int count1;
int count2;
int whoWon = 0;
for (int i = 0; i < size; i++) {
count1 = 0;
count2 = 0;
for (int j = 0; j < size; j++) {
if (arr[i][j] == 1) {
for (int d = 0; (i + d < size) && (j - d < size); d++)
if (arr[i + d][j - d] == 1) {
count1++;
if (count1 == 5) {
whoWon = 1;
}
} else {
count1 = 0;
}
}
if (arr[i][j] == 2) {
for (int d = 0; (i + d < size) && (j - d < size); d++)
if (arr[i + d][j - d] == 2) {
count2++;
if (count2 == 5) {
whoWon = 2;
}
} else {
count2 = 0;
}
}
}
}
if (whoWon != 1 && whoWon != 2) {
whoWon = 3;
}
return whoWon;
}
int game::WholeWinnerChecker(int arr[15][15], int size) {
int finalWinner = 0;
int outcome1 = horizontalChecker(arr, size);
if ((outcome1 == 1) || (outcome1 == 2)) {
finalWinner = outcome1;
cout << "Horizontal Win" << endl;
}
int outcome2 = verticalChecker(arr, size);
if ((outcome2 == 1) || (outcome2 == 2)) {
finalWinner = outcome2;
cout << "Vertical Win" << endl;
}
int outcome3 = diagonalCheckerPosSlope(arr, size);
if ((outcome3 == 1) || (outcome3 == 2)) {
finalWinner = outcome3;
cout << "Diag Pos Win" << endl;
}
int outcome4 = diagonalCheckerNegSlope(arr, size);
if ((outcome4 == 1) || (outcome4 == 2)) {
finalWinner = outcome4;
cout << "Diag Neg Win" << endl;
}
else if ((finalWinner != 1) && (finalWinner != 2)) {
finalWinner = 3;
}
return finalWinner;
}
void game::Play(int arr[15][15], int size) {
int counter = 0;
int Winner=0;
int boardFull= size*size;
while(((Winner != 1)&&(Winner != 2))&& (counter < boardFull)){
cout << "This is turn " << counter + 1 << endl;
if (counter % 2 == 0) {
cout << "PlayerOne: " << endl;
game::movePlayerOne(arr, size, counter);
if (counter >= 4) {
Winner = game::WholeWinnerChecker(arr, size);
if(Winner ==1){
cout << endl <<"======================" << endl;
cout << "Winner = " << Winner << endl;
cout <<"======================" << endl;
}
}
}
if (counter % 2 == 1) {
cout << "PlayerTwo:" << endl;
game::movePlayerTwo(arr, size);
if (counter >= 5) {
Winner = game::WholeWinnerChecker(arr, size);
if (Winner == 2) {
cout << endl << "======================" << endl;
cout << "Winner = " << Winner << endl;
cout << "======================" << endl;
}
}
}
if(counter == boardFull){
cout << "Draw" << endl;
}
counter++;
}
}
I have also been using an input of
6
The problem could be that you don't set count1 and count2 inside the j-loop, but rather count on the variable being reset in the else statement. But if your d-loop ends before the else statement is executed ((i + d < size) && (j - d < size) becomes false "prematurely") then the counter is not reset. Solution: move the zero-initialization to inside the j-loop.
Some improvements:
If lots of methods have the same parameter (such as int arr[15][15] and int size) then this could be the sign that these parameters should become members of the class.
It is possible to have only one checker function. Parameters of this function would be the direction of the search (int deltaX, int deltaY), and also the value that is being checked (1 or 2). Delta-values for all 8 directions are these:
(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, 1), (0, 1), (1, 1)
So, in your d-loop you would not add or subtract d to and from i and j, but you would always add deltaX to i, and deltaY to j.
Also, a bit advanced, but also fun. It is possible to arrange the code in such a way that you can have three types of games: human vs human, human vs computer, computer vs computer. You just for instance assign object of a class HumanPlayer to player 1 and object of a class ComputerPlayer to player 2. This would enable you to pick who plays first, and also have several algorithms battling it out. Both classes would have to be derived from the base class Player with some known methods (like 'move'), and some GameCoordinator would in a loop call this method 'move' on both objects, update the game state, and check if game has ended.
a scheduling problem that wants to minimize a weighted completion time
I would like to pass the image problem to the c++ language and solve it with cplex solver.
Problem:
Min ∑WiCi
xi ∈ 0,1
t= ∆ + ∑_{i} xi*pi1
Ci1 = ∑_{j=1}^{i} xj*pj1
Ci2 = t + ∑_{j=1}^{i} (1−xj)*pj2
Ci ≥ Ci1
Ci ≥ Ci2−Ωxi
Ω = ∆ + ∑_i max(pi1, pi2)
The goal is to minimize the weighted completion time of a problem with one machine with at most one machine reconfiguration. I have a boolean variable Xi that tells me if the job is done before or after the reconfiguration. The variable t is the instant after the reconfiguration(delta is the time to reconfigure the machine). pi1 is the processing time in configuration 1 and pi2 is the processing time in configuration 2. Besides, I have restrictions due to Ci (completion time of a job i).
The example of code I made is not working. I know I have a problem writing the C variable (but I don't know how to solve it).
#include <iostream>
#include <ilcplex/ilocplex.h>
#include <ilcp/cp.h>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int nbJob = 4;
int delta = 2;
int nbConf = 2;
vector<int> w_job;
w_job.push_back(1); w_job.push_back(1); w_job.push_back(1); w_job.push_back(1);
vector<vector<int>>p_job;
p_job.resize(nbJob);
p_job[0].push_back(6); p_job[0].push_back(3);
p_job[1].push_back(1); p_job[1].push_back(2);
p_job[2].push_back(10); p_job[2].push_back(2);
p_job[3].push_back(1); p_job[3].push_back(9);
int max_p = 0;
int aux;
for (size_t i = 0; i < nbJob - 1; i++) {
aux = max(p_job[i][0], p_job[i][1]);
max_p = max(max_p, aux);
}
cout << max_p << endl;
int max_w = 0;
aux = 0;
for (size_t i = 0; i < nbJob - 1; i++) {
aux = max(w_job[i], w_job[i + 1]);
max_w = max(aux, max_w);
}
cout << max_w << endl;
int omega = 0;
for (size_t i = 0; i < nbJob; i++) {
omega += max(p_job[i][0], p_job[i][1]);
}
omega += delta;
cout << omega << endl;
try {
IloEnv env;
IloModel model(env);
IloBoolVarArray x(env, nbJob);
for (size_t i = 0; i < nbJob; i++) {
IloBoolVar xi(env, 0, 1, "xi");
x[i] = xi;
}
IloArray<IloExprArray> C(env, nbJob);
for (size_t i = 0; i < nbJob; i++) {
IloExprArray Ci(env);
for (size_t j = 0; j < nbConf; j++) {
IloExpr Cij(env);
Ci.add(Cij);
}
C[i] = Ci;
}
IloNumVarArray C_final(env, nbJob);
for (size_t i = 0; i < nbJob; i++) {
IloNumVar C_finali(env, 0, IloInfinity, ILOINT); //
C_final[i] = C_finali;
}
IloExpr t(env);
for (int i = 0; i < nbJob; i++) {
t += x[i] * p_job[i][0];
}
t += delta;
for (size_t i = 0; i < nbJob; i++) {
for (size_t j = 0; j < nbJob; j++) {
if (j <= i) {
C[i][0] += x[j] * p_job[j][0];
}
}
}
for (size_t i = 0; i < nbJob; i++) {
for (size_t j = 0; j < nbJob; j++) {
if (j <= i) {
C[i][1] += ((1 - x[j]) * p_job[j][1]);
}
}
C[i][1] += t;
}
for (size_t i = 0; i < nbJob; i++) {
model.add(C_final[i] >= C[i][0]);
}
for (size_t i = 0; i < nbJob; i++) {
model.add(C_final[i] >= C[i][1] - (omega * x[i]));
}
IloExpr wiCi(env);
for (size_t i = 0; i < nbJob; i++) {
wiCi += w_job[i] * C_final[i];
}
model.add(IloMinimize(env, wiCi));
IloCplex solver(model);
solver.solve();
for (int i = 0; i < nbJob; i++) {
cout << "C " << i + 1 << " = " << solver.getValue(C_final[i]) << " x" << i+1 << " = " << solver.getValue(x[i]) << endl;
}
cout << endl;
cout << "t = " << solver.getValue(t) << endl;
cout << "wiCi = " << solver.getObjValue() << endl << endl;
}
catch (IloException e) {
cout << e.getMessage();
}
}
I would like to kill two birds with one stone, as the questions are very similiar:
1:
I followed this code on github Smith Waterman Alignment to create the smith-waterman in C++. After some research I understood that implementing
double H[N_a+1][N_b+1]; is not possible (anymore) for the "newer" C++ versions. So to create a constant variable I changed this line to:
double **H = new double*[nReal + 1];
for (int i = 0; i < nReal + 1; i++)
H[i] = new double[nSynth + 1];
and also the same scheme for int I_i[N_a+1][N_b+1], I_j[N_a+1][N_b+1]; and so one (well, everywhere, where a two dimensional array exists). Now I'm getting the exception:
Unhandled exception at 0x00007FFF7B413C58 in Smith-Waterman.exe: Microsoft C
++ exception: std :: bad_alloc at location 0x0000008FF4F9FA50.
What is wrong here? Already debugged, and the program throws the exceptions above the for (int i = 0; i < nReal + 1; i++).
2: This code uses std::strings as parameters. Would it be also possible to create a smith waterman algortihm for cv::Mat?
For maybe more clarification, my full code looks like this:
#include "BinaryAlignment.h"
#include "WallMapping.h"
//using declarations
using namespace cv;
using namespace std;
//global variables
std::string bin;
cv::Mat temp;
std::stringstream sstrMat;
const int maxMismatch = 2;
const float mu = 0.33f;
const float delta = 1.33;
int ind;
BinaryAlignment::BinaryAlignment() { }
BinaryAlignment::~BinaryAlignment() { }
/**
*** Convert matrix to binary sequence
**/
std::string BinaryAlignment::matToBin(cv::Mat src, std::experimental::filesystem::path path) {
cv::Mat linesMat = WallMapping::wallMapping(src, path);
for (int i = 0; i < linesMat.size().height; i++) {
for (int j = 0; j < linesMat.size().width; j++) {
if (linesMat.at<Vec3b>(i, j)[0] == 0
&& linesMat.at<Vec3b>(i, j)[1] == 0
&& linesMat.at<Vec3b>(i, j)[2] == 255) {
src.at<int>(i, j) = 1;
}
else {
src.at<int>(i, j) = 0;
}
sstrMat << src.at<int>(i, j);
}
}
bin = sstrMat.str();
return bin;
}
double BinaryAlignment::similarityScore(char a, char b) {
double result;
if (a == b)
result = 1;
else
result = -mu;
return result;
}
double BinaryAlignment::findArrayMax(double array[], int length) {
double max = array[0];
ind = 0;
for (int i = 1; i < length; i++) {
if (array[i] > max) {
max = array[i];
ind = i;
}
}
return max;
}
/**
*** Smith-Waterman alignment for given sequences
**/
int BinaryAlignment::watermanAlign(std::string seqSynth, std::string seqReal, bool viableAlignment) {
const int nSynth = seqSynth.length(); //length of sequences
const int nReal = seqReal.length();
//H[nSynth + 1][nReal + 1]
double **H = new double*[nReal + 1];
for (int i = 0; i < nReal + 1; i++)
H[i] = new double[nSynth + 1];
cout << "passt";
for (int m = 0; m <= nSynth; m++)
for (int n = 0; n <= nReal; n++)
H[m][n] = 0;
double temp[4];
int **Ii = new int*[nReal + 1];
for (int i = 0; i < nReal + 1; i++)
Ii[i] = new int[nSynth + 1];
int **Ij = new int*[nReal + 1];
for (int i = 0; i < nReal + 1; i++)
Ij[i] = new int[nSynth + 1];
for (int i = 1; i <= nSynth; i++) {
for (int j = 1; j <= nReal; j++) {
temp[0] = H[i - 1][j - 1] + similarityScore(seqSynth[i - 1], seqReal[j - 1]);
temp[1] = H[i - 1][j] - delta;
temp[2] = H[i][j - 1] - delta;
temp[3] = 0;
H[i][j] = findArrayMax(temp, 4);
switch (ind) {
case 0: // score in (i,j) stems from a match/mismatch
Ii[i][j] = i - 1;
Ij[i][j] = j - 1;
break;
case 1: // score in (i,j) stems from a deletion in sequence A
Ii[i][j] = i - 1;
Ij[i][j] = j;
break;
case 2: // score in (i,j) stems from a deletion in sequence B
Ii[i][j] = i;
Ij[i][j] = j - 1;
break;
case 3: // (i,j) is the beginning of a subsequence
Ii[i][j] = i;
Ij[i][j] = j;
break;
}
}
}
//Print matrix H to console
std::cout << "**********************************************" << std::endl;
std::cout << "The scoring matrix is given by " << std::endl << std::endl;
for (int i = 1; i <= nSynth; i++) {
for (int j = 1; j <= nReal; j++) {
std::cout << H[i][j] << " ";
}
std::cout << std::endl;
}
//search H for the moaximal score
double Hmax = 0;
int imax = 0, jmax = 0;
for (int i = 1; i <= nSynth; i++) {
for (int j = 1; j <= nReal; j++) {
if (H[i][j] > Hmax) {
Hmax = H[i][j];
imax = i;
jmax = j;
}
}
}
std::cout << Hmax << endl;
std::cout << nSynth << ", " << nReal << ", " << imax << ", " << jmax << std::endl;
std::cout << "max score: " << Hmax << std::endl;
std::cout << "alignment index: " << (imax - jmax) << std::endl;
//Backtracing from Hmax
int icurrent = imax, jcurrent = jmax;
int inext = Ii[icurrent][jcurrent];
int jnext = Ij[icurrent][jcurrent];
int tick = 0;
char *consensusSynth = new char[nSynth + nReal + 2];
char *consensusReal = new char[nSynth + nReal + 2];
while (((icurrent != inext) || (jcurrent != jnext)) && (jnext >= 0) && (inext >= 0)) {
if (inext == icurrent)
consensusSynth[tick] = '-'; //deletion in A
else
consensusSynth[tick] = seqSynth[icurrent - 1]; //match / mismatch in A
if (jnext == jcurrent)
consensusReal[tick] = '-'; //deletion in B
else
consensusReal[tick] = seqReal[jcurrent - 1]; //match/mismatch in B
//fix for adding first character of the alignment.
if (inext == 0)
inext = -1;
else if (jnext == 0)
jnext = -1;
else
icurrent = inext;
jcurrent = jnext;
inext = Ii[icurrent][jcurrent];
jnext = Ij[icurrent][jcurrent];
tick++;
}
// Output of the consensus motif to the console
std::cout << std::endl << "***********************************************" << std::endl;
std::cout << "The alignment of the sequences" << std::endl << std::endl;
for (int i = 0; i < nSynth; i++) {
std::cout << seqSynth[i];
};
std::cout << " and" << std::endl;
for (int i = 0; i < nReal; i++) {
std::cout << seqReal[i];
};
std::cout << std::endl << std::endl;
std::cout << "is for the parameters mu = " << mu << " and delta = " << delta << " given by" << std::endl << std::endl;
for (int i = tick - 1; i >= 0; i--)
std::cout << consensusSynth[i];
std::cout << std::endl;
for (int j = tick - 1; j >= 0; j--)
std::cout << consensusReal[j];
std::cout << std::endl;
int numMismatches = 0;
for (int i = tick - 1; i >= 0; i--) {
if (consensusSynth[i] != consensusReal[i]) {
numMismatches++;
}
}
viableAlignment = numMismatches <= maxMismatch;
return imax - jmax;
}
Thanks!
Hi i have a problem with my code, i try sort array by counting sort (it must be stable sort), but my code doesn't work. I try implemented counting-sort from some wbesite, but it was in c# and I'm not sure s it done correctly. Can you tell me what is wrong with it?
#include <stdio.h>
#include <iostream>
using namespace std;
void sort_with_show(int **a, int rozmiar, int polecenie)
{
int y, d;
for (int i = 0; i< rozmiar - 1; i++)
{
for (int j = 0; j < rozmiar - 1 - i; j++)
{
if (a[j + 1][0] < a[j][0])
{
y = a[j][0];
a[j][0] = a[j + 1][0];
a[j + 1][0] = y;
}
}
}
for (int i = 0; i < rozmiar; i++)
{
//cout << tablica[i].x << " " << tablica[i].y << "\n";
if (polecenie == 0)
{
cout << a[i][0] << '\n';
}
else if (polecenie == 1)
{
cout << a[i][0] << "," << a[i][1] << "\n";
}
}
}
int main()
{
int rozmiar = 0;
int polecenie = 0;
char t[20] = { '\0' };
char *p, *q;
int liczba = 0;
int calaLiczba = 0;
bool isY = false;
cin >> rozmiar;
int ** a = new int *[rozmiar];
for (int i = 0; i < rozmiar; i++)
a[i] = new int[2];
cin.ignore();
int i = 0;
while(i < rozmiar)
{
fgets(t, sizeof t, stdin);
for (p = t, q = t + sizeof t; p < q; p++)
{
if (*p >= 48 && *p <= 57)
{
liczba = *p - 48;
calaLiczba = calaLiczba * 10 + liczba;
}
if (*p == ' ')
{
a[i][0] = calaLiczba;
isY = true;
calaLiczba = 0;
liczba = 0;
}
if (*p == '\n')
{
a[i][1] = calaLiczba;
isY = false;
}
}
for (int j = 0; j < 20; j++)
t[j] = '\0';
liczba = 0;
calaLiczba = 0;
isY = false;
i++;
}
cin >> polecenie;
cin.ignore();
sort_with_show(a, rozmiar, polecenie);
return 0;
}
I have been given a "Sand box" of variable length and width. I've been given instructions to find a "shovel" of static size, which may be oriented either horizontally or vertically. I implement the following algorithm in order to search the least amount of times to find one valid location (one which corresponds to a "part of the object") in the grid:
found = false;
nShift = 0;
shovelSize = 4;
for(int i = 0; i < SandBoxRows; i++) {
for(int j = 0; j < SandBoxColumns; j+=shovelSize) {
found = probeSandBoxTwo(('A' + i), (j + 1 + nShift));
}
if(nShift >= shovelSize - 1 || nShift > SandBoxColumns) {
nShift = 0;
} else {
nShift++;
}
}
In this case, the "Sand box" will be tested by the function as described below.
I completely recreate this scenario with a "Sand box" whose size is fixed (though easily manipulated) whose shovel is still randomly placed and oriented within the following code:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
const int ROW = 12;
const int COL = 16;
char sandbox[ROW][COL];
bool probeSandBoxTwo(char c, int i);
void displayResults(int sCount, bool found, int x, int y);
void displaySandbox();
void displaySearchPattern();
void fillSandbox();
void placeShovel();
int main() {
fillSandbox();
placeShovel();
displaySandbox();
//define your variables here
bool found;
int nShift,
sCount,
shovelSize,
x,
y;
found = false;
nShift = 0;
shovelSize = 4;
sCount = 0;
for(int i = 0; i < ROW && !found; i++) {
for(int j = 0; j < COL && !found; j+=shovelSize) {
found = probeSandBoxTwo(('A' + i), (j + 1 + nShift));
x = i;
y = j + nShift;
sCount++;
cout << "Search conducted at (" << i << ", " << (j + nShift) << ")" << endl;
}
if(nShift >= shovelSize - 1 || nShift > ROW) {
nShift = 0;
} else {
nShift++;
}
}
displayResults(sCount, found, x, y);
displaySearchPattern();
}
bool probeSandBoxTwo(char c, int i) {
if(sandbox[c-'A'][i-1] == 'X') {
return true;
} else {
return false;
}
}
void displayResults(int sCount, bool found, int x, int y) {
cout << endl;
cout << "Total searches: " << sCount << endl;
cout << endl;
if(found) {
cout << "Shovel found at coordinates: (" << x << ", " << y << ")" << endl;
}
}
void displaySandbox() {
cout << " ";
for(int i = 0; i < COL; i++) {
cout << (i % 10); //show index numbers [col]
}
cout << endl;
for(int i = 0; i < ROW; i++) {
cout << (i % 10) << " "; //show index numbers [row]
for(int j = 0; j < COL; j++) {
cout << sandbox[i][j];
}
cout << endl;
}
cout << endl;
}
void displaySearchPattern() {
int nShift = 0;
int shovelSize = 4;
cout << endl << " ";
for(int i = 0; i < COL; i++) {
cout << (i % 10); //show index numbers [col]
}
cout << endl;
for(int i = 0; i < ROW; i++) {
cout << (i % 10) << " "; //show index numbers [row]
for(int j = 0; j < COL; j++) {
if(!((j + nShift) % shovelSize)) {
cout << 'o';
} else {
cout << '.';
}
}
if(nShift >= shovelSize - 1 || nShift > COL) {
nShift = 0;
} else {
nShift++;
}
cout << endl;
}
}
void fillSandbox() {
for(int i = 0; i < ROW; i++) {
for(int j = 0; j < COL; j++) {
sandbox[i][j] = '.';
}
}
}
void placeShovel() {
srand(time(NULL));
int shovelRow,
shovelCol,
shovelSize = 4;
if(rand() % 2) {
//horizontal
shovelRow = rand() % ROW + 1;
shovelCol = rand() % (COL - (shovelSize - 1)) + 1;
for(int i = shovelCol - 1; i < shovelSize + (shovelCol - 1); i++) {
sandbox[shovelRow - 1][i] = 'X';
}
} else {
//vertical
shovelRow = rand() % (ROW - (shovelSize - 1)) + 1;
shovelCol = rand() % COL + 1;
for(int i = shovelRow - 1; i < shovelSize + (shovelRow - 1); i++) {
sandbox[i][shovelCol - 1] = 'X';
}
}
}
In this code, I also graphically display the pattern (when run) with which my algorithm searches.
Is this truly the optimal search pattern for such a scenario, is my implementation correct, and if so, why might I be having incorrect results returned?
A given test driver reports the following results:
The source code for this result (and its test driver).
found = false;
nShift = 0;
shovelSize = 4;
for(int i = 0; i < SandBoxRows; i++) {
for(int j = 0; (j + nShift) < SandBoxColumns; j+=shovelSize) {
found = probeSandBoxTwo(('A' + i), (j + 1 + nShift));
}
if(nShift >= shovelSize - 1 || nShift > SandBoxColumns) {
nShift = 0;
} else {
nShift++;
}
}
This corrects an error in the conditional portion of the for loop header which did not account for the index-shifting variable nShift.