Processing: passing in objects - object

I have a Bot class, Gem class, and a main program.
In the Gem class:
Gem(float x, float y)
{
xLoc = x;
yLoc = y;
}
In the main program:
void mousePressed()
{
gem1 = new Gem(mouseX, mouseY);
seekGem = true;
}
void draw()
{
if (seekGem)
bot1.seek(gem1.xLoc, gem1.yLoc);
}
and then in the Bot class I was given:
void seek(float xTarg, float yTarg)
{
if (abs(xTarg - xLoc) < bodyW/4)
xDir = 0;
else if (xTarg > xLoc)
xDir = 1;
else if (xTarg < xLoc)
xDir = -1;
xLoc = xLoc + xDir * speed;
if (abs(yTarg - yLoc) < bodyH/4)
yDir = 0;
else if (yTarg > yLoc)
yDir = 1;
else if (yTarg < yLoc)
yDir = -1;
yLoc = yLoc + yDir * speed;
}
Basically the bot moves to the gem when a gem appears on the screen.
I was told to pass gem1 into the bot's seek method instead of having bot1.seek(gem1.xLoc, gem1.yLoc) but I don't know how to do that.

like this:
void draw()
{
if (seekGem)
bot1.seek(gem1);
}
and the seek method in the bot:
void seek(Gem gemToSeek)
{
float xTarg = gemToSeek.xLoc;
float yTarg = gemToSeek.yLoc;
if (abs(xTarg - xLoc) < bodyW/4)
xDir = 0;
else if (xTarg > xLoc)
xDir = 1;
else if (xTarg < xLoc)
xDir = -1;
xLoc = xLoc + xDir * speed;
if (abs(yTarg - yLoc) < bodyH/4)
yDir = 0;
else if (yTarg > yLoc)
yDir = 1;
else if (yTarg < yLoc)
yDir = -1;
yLoc = yLoc + yDir * speed;
}

Related

Exception thrown: read access violation. this->pt_mat was 0x1110112

I got this error while using a double pointer in a class function to set coefficient and using it in the main.
The class function is used in a stand-alone function to set the coefficient using a class type pointer.
The class code is:
class Matrix
{
private:
int size_m, size_n;
double **pt_mat;
public:
Matrix() {};
Matrix(int m, int n)
{
pt_mat = new double *[m];
for (int i = 0; i < m; i++)
{
pt_mat[i] = new double[n];
}
}
void set_size(int m, int n)
{
size_m = m;
size_n = n;
}
void set_coef(int i, int j, double x)
{
pt_mat[i][j] = x;
}
void get_size(int *pt_m, int *pt_n)
{
*pt_m = size_m;
*pt_n = size_n;
}
double get_coef(int i, int j)
{
return pt_mat[i][j];
}
void print(ofstream &fout)
{
fout << size_m << " " << size_n << endl;
int i, j;
for (i = 0; i < size_m; i++)
{
for (j = 0; j < size_n; j++)
{
fout << pt_mat[i][j] << " ";
}
fout << endl;
}
}
void max_min(int *pt_imax, int *pt_jmax, double *pt_max, int *pt_imin, int *pt_jmin, double *pt_min)
{
double max, min;
int i, j, imax = 0, imin = 0, jmax = 0, jmin = 0;
max = pt_mat[0][0];
for (i = 0; i < size_m; i++)
{
for (j = 0; j < size_n; j++)
{
if (pt_mat[i][j] > max)
{
max = pt_mat[i][j];
imax = i;
jmax = j;
}
}
}
*pt_max = max;
*pt_imax = imax;
*pt_jmax = jmax;
min = pt_mat[0][0];
for (i = 0; i < size_m; i++)
{
for (j = 0; j < size_n; j++)
{
if (pt_mat[i][j] < min)
{
min = pt_mat[i][j];
imin = i;
jmin = j;
}
}
}
*pt_min = min;
*pt_imin = imin;
*pt_jmin = jmin;
}
};
The stand-alone function:
void mat_add(Matrix a, Matrix b, Matrix *pt_c)
{
Matrix c;
pt_c = &c;
int a_m, a_n, *p_am = &a_m, *p_an = &a_n;
a.get_size(p_am, p_an);
c.set_size(a_m, a_n);
int m, n, *pt_m = &m, *pt_n = &n;
double coef;
a.get_size(pt_m, pt_n);
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
{
coef = a.get_coef(i, j) + b.get_coef(i, j);
c.set_coef(i, j, coef);
}
}
please help me in this regard if you can. I have no syntax error showing.enter code here

how to store pieces in gomoku

am currently learning MFC and decided to make the game Gomoku. this is the code I have so far.
**mainframe.h**
class CMainFrame : public CFrameWnd
{
public:
CMainFrame();
protected:
DECLARE_DYNAMIC(CMainFrame)
public:
public:
public:
public:
virtual ~CMainFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
DECLARE_MESSAGE_MAP()
void DrawBoard(CDC* pDC);
int m_nNextChar;
int board[15][15];
static const int EMPTY = 0, WHITE = 1, BLACK = 2;
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnPaint();
};
**mainframe.cpp**
#include "stdafx.h"
#include "01.win32tomfc.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMainFrame
IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_PAINT()
END_MESSAGE_MAP()
int diameter = 23;
int size = 40;
int xCod;
int yCod;
int xCodx;
int yCody;
// CMainFrame ¹¹Ôì/Îö¹¹
CMainFrame::CMainFrame()
{
m_nNextChar = BLACK;
Create(NULL, _T("Generic Sample Application"));
CRect rect(0, 0, 700, 700);
CalcWindowRect(&rect);
SetWindowPos(NULL, 0, 0, rect.Width(), rect.Height(),
SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW);
}
CMainFrame::~CMainFrame()
{
}
// CMainFrame Õï¶Ï
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
// CMainFrame ÏûÏ¢´¦Àí³ÌÐò
void CMainFrame::DrawBoard(CDC * pDC)
{
CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
CPen* pOldPen = pDC->SelectObject(&pen);
for (int i = 1; i <= 16; i++) {
pDC->MoveTo(40 * i, 40);
pDC->LineTo(40 * i, 640);
pDC->MoveTo(40, 40 * i);
pDC->LineTo(640, 40 * i);
}
pDC->SelectObject(pOldPen);
}
void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
CPen* pOldPen = dc.SelectObject(&pen);
dc.SelectStockObject(BLACK_BRUSH);
xCod = (point.x + (size / 2)) / size;
xCod = (xCod * size) - diameter / 2;
yCod = (point.y + (size / 2)) / size;
yCod = (yCod * size) - diameter / 2;
xCodx = xCod + diameter;
yCody = yCod + diameter;
if (m_nNextChar != BLACK )
return;
else {
if (xCod > 20 && yCod <= 640 && xCodx < 655 && yCody > 40) {
dc.Ellipse(xCod, yCod, xCodx, yCody);
}
}
m_nNextChar = WHITE;
CFrameWnd::OnLButtonDown(nFlags, point);
}
void CMainFrame::OnRButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
CPen* pOldPen = dc.SelectObject(&pen);
dc.SelectStockObject(WHITE_BRUSH);
xCod = (point.x + (size / 2)) / size;
xCod = (xCod * size) - diameter / 2;
yCod = (point.y + (size / 2)) / size;
yCod = (yCod * size) - diameter / 2;
xCodx = xCod + diameter;
yCody = yCod + diameter;
if (m_nNextChar != WHITE)
return;
else {
if (xCod > 20 && yCod <= 640 && xCodx < 655 && yCody > 40) {
dc.Ellipse(xCod, yCod, xCodx, yCody);
}
}
m_nNextChar = BLACK;
CFrameWnd::OnRButtonDown(nFlags, point);
}
void CMainFrame::OnPaint()
{
CPaintDC dc(this);
DrawBoard(&dc);
}
the code I have draws a 15 X 15 grid in the function DrawBoard() and draws the black and white pieces in the OnLButtonDown and OnRButtonDown respectively. thing is when I run the program and click to draw the black piece and then the white piece, the white piece can be drawn over the black piece and vice versa. so I thought to create a two-dimensional array board[15][15] to store a piece when its drawn so that a different piece cannot be drawn over a current piece would be best(am I on the right track here). I tried but I can't seem to figure out how to do it. am not very good at programming and realize this might be easy but some help would really be appreciated. please explain how I would go about it the right way.
this is what I tried.
void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
CPen* pOldPen = dc.SelectObject(&pen);
dc.SelectStockObject(BLACK_BRUSH);
xCod = (point.x + (size / 2)) / size;
xCod = (xCod * size) - diameter / 2;
yCod = (point.y + (size / 2)) / size;
yCod = (yCod * size) - diameter / 2;
xCodx = xCod + diameter;
yCody = yCod + diameter;
if ((m_nNextChar != BLACK) && (board[xCod][yCod] = WHITE) )
return;
else {
if (xCod > 20 && yCod <= 640 && xCodx < 655 && yCody > 40) {
dc.Ellipse(xCod, yCod, xCodx, yCody);
board[xCod][yCod] = BLACK;
}
}
void CMainFrame::OnRButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
CPen* pOldPen = dc.SelectObject(&pen);
dc.SelectStockObject(WHITE_BRUSH);
xCod = (point.x + (size / 2)) / size;
xCod = (xCod * size) - diameter / 2;
yCod = (point.y + (size / 2)) / size;
yCod = (yCod * size) - diameter / 2;
xCodx = xCod + diameter;
yCody = yCod + diameter;
if (m_nNextChar != WHITE && (board[xCod][yCod] = BLACK))
return;
else {
if (xCod > 20 && yCod <= 640 && xCodx < 655 && yCody > 40) {
dc.Ellipse(xCod, yCod, xCodx, yCody);
board[xCod][yCod] = WHITE;
}
}
m_nNextChar = BLACK;
CFrameWnd::OnRButtonDown(nFlags, point);
}
You should do all the drawings in OnPaint. Don't draw in other functions such as OnLButtonDown. Instead, get the necessary information from OnLButtonDown and call Invalidate, this will repaint the window.
Here is an example. For simplicity, I created a structure info and a 2-dimensional array data. data stores all the information for each cell, that is the rectangle and color. You have to initialize data once, and paint based on the information in data
#include <vector>
class CMainFrame : public CFrameWnd
{
...
struct info
{
CRect rect;
int color;
};
std::vector<std::vector<info>> data;
};
CMainFrame::CMainFrame()
{
...
data.resize(15);
for(int i = 0; i < data.size(); i++)
data[i].resize(15);
int xoffset = 20;
int yoffset = 20;
for(int row = 0; row < 15; row++)
{
for(int col = 0; col < 15; col++)
{
data[row][col].rect.SetRect(0, 0, size + 1, size + 1);
data[row][col].rect.MoveToXY(xoffset + row * size, yoffset + col * size);
}
}
}
void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point)
{
CFrameWnd::OnLButtonDown(nFlags, point);
for(int row = 0; row < 15; row++)
{
for(int col = 0; col < 15; col++)
{
if(data[row][col].color)
break;
if(data[row][col].rect.PtInRect(point))
{
data[row][col].color = WHITE;
break;
}
}
}
Invalidate(FALSE);
}
void CMainFrame::OnRButtonDown(UINT nFlags, CPoint point)
{
CFrameWnd::OnRButtonDown(nFlags, point);
for(int row = 0; row < 15; row++)
{
for(int col = 0; col < 15; col++)
{
if(data[row][col].color)
break;
if(data[row][col].rect.PtInRect(point))
{
data[row][col].color = BLACK;
break;
}
}
}
Invalidate(FALSE);
}
void CMainFrame::OnPaint()
{
CPaintDC dc(this);
CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
dc.SelectObject(&pen);
CBrush white, black;
white.CreateSolidBrush(RGB(255, 255, 255));
black.CreateSolidBrush(RGB(0, 0, 0));
for(int row = 0; row < 15; row++)
{
for(int col = 0; col < 15; col++)
{
dc.Rectangle(data[row][col].rect);
if(data[row][col].color)
{
CBrush *oldbrush;
if(data[row][col].color == WHITE)
oldbrush = dc.SelectObject(&white);
else
oldbrush = dc.SelectObject(&black);
dc.Ellipse(data[row][col].rect);
dc.SelectObject(oldbrush);
}
}
}
}

compiler is showing ArrayIndexOutOfBoundException...i guess my logic is wrong somewhere

static int binarySearch(int start, int end, int x, int[] arr) {
while (start <= end) {
int mid = (start + end) / 2;
if (x > arr[mid]) {
start = mid + 1;
} else if (x < arr[mid]) {
end = mid - 1;
} else {
return mid;
}
}
return -1;
}

missing template arguments before '[' token

I am very new in R and cpp. These are my cpp codes with Rcpp.
#include <cmath>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector getPerformance(NumericVector accProfitSeries, NumericVector dateSeries, double capital, double riskFreeRate)
{
NumericVector retult(8);
int Length = accProfitSeries.length();
double strategyProfit = accProfitSeries[Length - 1];
double returnRate = log(strategyProfit / capital);
double tradingPeriod = (dateSeries[dateSeries.length() - 1] - dateSeries[0]) / 365;
double returnRateYear = returnRate / tradingPeriod;
double sharpeRatio;
for (int i = 0; i < 8; i++) {
result[i] = 0;
}
NumericVector capitalLevel(Length), capitalReturn(Length);
double meanReturn, sumReturn, stdReturn;
double tmp;
for (int i = 0; i < Length; i++)
{
capitalLevel[i] = accProfitSeries[i] + capital;
capitalReturn[i] = log(capitalReturn[i] / capitalReturn[0]);
sumReturn = sumReturn + capitalReturn[i];
}
meanReturn = sumReturn / Length;
for (int i = 0; i < Length; i++)
tmp += pow(capitalReturn[i] - meanReturn, 2);
stdReturn = sqrt(tmp / Length);
sharpeRatio = (meanReturn - riskFreeRate) / stdReturn;
double maxCapital = 0, drawback = 0, maxDrawback = 0, maxDrawbackPercent = 0;
int drawbackPeriod = 0, maxDrawbackPeriod = 0;
for (int i = 0; i < Length; i++)
{
maxCapital = max(NumericVector::create(maxCapital, capitalLevel[i]));
drawback = capitalLevel[i] - maxCapital;
maxDrawback = min(NumericVector::create(maxDrawback, drawback));
maxDrawbackPercent = maxDrawback / (maxCapital - capital);
if (drawback >= 0)
{
drawbackPeriod = 0;
}
else
{
drawbackPeriod++;
maxDrawbackPeriod = max(NumericVector::create(maxDrawbackPeriod, drawbackPeriod));
}
}
result[0] = returnRate;
result[1] = tradingPeriod;
result[2] = returnRateYear;
result[3] = sharpeRatio;
result[4] = maxDrawback;
result[5] = maxDrawbackPeriod;
result[6] = strategyProfit;
result[7] = maxDrawbackPercent;
return(result);
}
I got one of the error messages, others are similar:
getPerformance.cpp:17:9: error: missing template arguments before '[' token
result[i] = 0;
How do I solve it?
Mainly, you have two issues:
As pointed out by #DirkEddelbuettel, you need to change retult(8) to result(8) to proper setup a variable referenced elsewhere in the script.
You failed to initialize several double variables.
The following should work...
#include <cmath>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector getPerformance(NumericVector accProfitSeries,
NumericVector dateSeries,
double capital, double riskFreeRate)
{
NumericVector result(8); // retult -> result
int Length = accProfitSeries.length();
double strategyProfit = accProfitSeries[Length - 1];
double returnRate = log(strategyProfit / capital);
double tradingPeriod = (dateSeries[dateSeries.length() - 1] - dateSeries[0]) / 365;
double returnRateYear = returnRate / tradingPeriod;
double sharpeRatio = 0.0; // Initialize value
for (int i = 0; i < 8; i++) {
result[i] = 0;
}
NumericVector capitalLevel(Length), capitalReturn(Length);
// Initialize values
double meanReturn = 0.0, sumReturn = 0.0, stdReturn = 0.0;
double tmp = 0.0;
for (int i = 0; i < Length; i++) {
capitalLevel[i] = accProfitSeries[i] + capital;
capitalReturn[i] = log(capitalReturn[i] / capitalReturn[0]);
sumReturn = sumReturn + capitalReturn[i];
}
meanReturn = sumReturn / Length;
for (int i = 0; i < Length; i++)
tmp += pow(capitalReturn[i] - meanReturn, 2);
stdReturn = sqrt(tmp / Length);
sharpeRatio = (meanReturn - riskFreeRate) / stdReturn;
double maxCapital = 0, drawback = 0, maxDrawback = 0, maxDrawbackPercent = 0;
int drawbackPeriod = 0, maxDrawbackPeriod = 0;
for (int i = 0; i < Length; i++) {
maxCapital = max(NumericVector::create(maxCapital, capitalLevel[i]));
drawback = capitalLevel[i] - maxCapital;
maxDrawback = min(NumericVector::create(maxDrawback, drawback));
maxDrawbackPercent = maxDrawback / (maxCapital - capital);
if (drawback >= 0) {
drawbackPeriod = 0;
} else {
drawbackPeriod++;
maxDrawbackPeriod = max(NumericVector::create(maxDrawbackPeriod, drawbackPeriod));
}
}
result[0] = returnRate;
result[1] = tradingPeriod;
result[2] = returnRateYear;
result[3] = sharpeRatio;
result[4] = maxDrawback;
result[5] = maxDrawbackPeriod;
result[6] = strategyProfit;
result[7] = maxDrawbackPercent;
return(result);
}

Problems while using threading with arguments

I have been trying to get this working for a while and I am not very skilled at C++/CLI. I am trying to loop through a 2d array and when it contains certain number create a thread to run but I keep getting errors while compiling.
Here is the thread creation:
if (map[x][y] == 8)
{
Pos^ p = gcnew Pos(x, y, map);
Thread^ t = gcnew Thread(gcnew ParameterizedThreadStart(p, &Pos::moverX));
t->Start(p);
}
else if (map[x][y] == 9)
{
Pos^ p = gcnew Pos(x, y, map);
Thread^ t = gcnew Thread(gcnew ParameterizedThreadStart(p, &Pos::moverY));
t->Start(p);
}
Here is the Pos class:
public ref class Pos
{
public:
static int px, py;
static int ** mapa;
Pos(int x, int y, int ** map)
{
px = x;
py = y;
mapa = map;
}
int getX(){ return px; }
int getY(){ return py; }
int** getMap(){ return mapa; }
static void moverX(Pos p)
{
int dy = 1;
while (true)
{
if (mapa[p.getX()+dy][p.getY()] == 1){ dy *= -1; }
Console::BackgroundColor = ConsoleColor::Black;
Console::SetCursorPosition(p.getY() + 30, p.getX() + 5);
cout << " ";
Console::SetCursorPosition(p.getY() + 30, p.getX() + 5+dy);
cout << (char)164;
Thread::Sleep(1000);
}
}
static void moverY(Pos p)
{
int dy = 1;
while (true)
{
if (mapa[p.getX()][p.getY() + dy] == 1){ dy *= -1; }
Console::BackgroundColor = ConsoleColor::Black;
Console::SetCursorPosition(p.getY() + 30, p.getX() + 5);
cout << " ";
Console::SetCursorPosition(p.getY() + 30 + dy, p.getX() + 5);
cout << (char)164;
Thread::Sleep(1000);
}
}
};

Resources