Vector problem - square addition

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Vector problem - square addition

Post by jhaglund »

Okay, I've been stuck on this far too long...

What I've come up with is this:

Code: Select all

//addsq.hpp
double a_count, b_count, c_count, d_count, e_count, f_count, g_count, h_count;

int i, ii, iii, iv, v, vi, vii, viii;

int *ti = new int;
int *tii = new int;
int *tiii = new int;
int *tiv = new int;
int *tv = new int;
int *tvi = new int;
int *tvii = new int;
int *tviii = new int;

int a, b, c, d, e, f, g, h, j;
int sq_total = 64;

int sq[64] = {
0, 1, 1, 2, 2, 1, 1, 0, 
1, 1, 2, 2, 2, 2, 1, 1,
1, 2, 3, 3, 3, 3, 2, 1,
2, 2, 3, 4, 4, 3, 2, 2,
2, 2, 3, 4, 4, 3, 2, 2,
1, 2, 3, 3, 3, 3, 2, 1, 
1, 1, 2, 2, 2, 2, 1, 1,
0, 1, 1, 2, 2, 1, 1, 0
}; 

/*
add "vertically" every 8 for each file;
a  b     c    d     e   f    g      h
i  ii   iii   iv    v   vi   vii   viii   
8  12    18   22    22  18   12     8


*/

Code: Select all

// addsq.cpp
#include "iostream"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include <cstring>   
#include <algorithm>
#include <vector>

#include "addsq.hpp"
using namespace std;


void add_sq&#40;int& i, int& ii, int& iii, int& iv, int& v, int& vi, int &vii, int &viii&#41;;

int main&#40;)
&#123;
   
        vector<int> myvector &#40;sq, sq+sq_total&#41;;
        a_count = &#40;int&#41; count &#40;myvector.begin&#40;), myvector.end&#40;), sq&#91;a&#93;);
        b_count = &#40;int&#41; count &#40;myvector.begin&#40;), myvector.end&#40;), sq&#91;b&#93;);
        c_count = &#40;int&#41; count &#40;myvector.begin&#40;), myvector.end&#40;), sq&#91;c&#93;);
        d_count = &#40;int&#41; count &#40;myvector.begin&#40;), myvector.end&#40;), sq&#91;d&#93;);
        e_count = &#40;int&#41; count &#40;myvector.begin&#40;), myvector.end&#40;), sq&#91;e&#93;);
        f_count = &#40;int&#41; count &#40;myvector.begin&#40;), myvector.end&#40;), sq&#91;f&#93;);
        g_count = &#40;int&#41; count &#40;myvector.begin&#40;), myvector.end&#40;), sq&#91;g&#93;);
        h_count = &#40;int&#41; count &#40;myvector.begin&#40;), myvector.end&#40;), sq&#91;h&#93;);
        
        a_count = &#40;double&#41; count &#40;sq, sq+sq_total, 0&#41;;
        b_count = &#40;double&#41; count &#40;sq, sq+sq_total, 0&#41;;
        c_count = &#40;double&#41; count &#40;sq, sq+sq_total, 0&#41;;
        d_count = &#40;double&#41; count &#40;sq, sq+sq_total, 0&#41;;
        e_count = &#40;double&#41; count &#40;sq, sq+sq_total, 0&#41;;
        f_count = &#40;double&#41; count &#40;sq, sq+sq_total, 0&#41;;
        g_count = &#40;double&#41; count &#40;sq, sq+sq_total, 0&#41;;
        h_count = &#40;double&#41; count &#40;sq, sq+sq_total, 0&#41;;
     
void add_sq&#40;int&,int&,int&,int&,int&,int&,int&,int&);

a = 0;
b = 1;
c = 2;
d = 3;
e = 4;
f = 5;
g = 6;
h = 7;
  
add_sq&#40;a,b,c,d,e,f,g,h&#41;;
system&#40;"pause");
&#125;


void add_sq&#40;int& i, int& ii, int& iii, int& iv, int& vi, int& vii, int& viii&#41;
&#123;
 
for&#40;j = 0; j < sq_total;) 
&#123;
         
cout << " a " << sq&#91;i&#93;;
cout << " b " << sq&#91;ii&#93;;
cout << " c " << sq&#91;iii&#93;;
cout << " d " << sq&#91;iv&#93;;
cout << " e " << sq&#91;v&#93;;
cout << " f " << sq&#91;vi&#93;;
cout << " g " << sq&#91;vii&#93;;
cout << " h " << sq&#91;viii&#93;;

*ti = i; 
*tii = ii; 
*tiii = iii; 
*tiv = iv; 
*tv = v; 
*tvi = vi;
*tvii = vii;
*tviii = viii;

i = i + 1;
ii = ii + 1;
iii = iii + 1;
iv = iv + 1;
v = v + 1;
vi = vi + 1;
vii = vii + 1;
viii = viii + 1;

cout << " a " << sq&#91;*ti&#93;+sq&#91;i&#93;;
cout << " b " << sq&#91;*tii&#93;+sq&#91;ii&#93;;
cout << " c " << sq&#91;*tiii&#93;+sq&#91;iii&#93;;
cout << " d " << sq&#91;*tiv&#93;+sq&#91;iv&#93;;
cout << " e " << sq&#91;*tv&#93;+sq&#91;v&#93;;
cout << " f " << sq&#91;*tvi&#93;+sq&#91;vi&#93;;
cout << " g " << sq&#91;*tvii&#93;+sq&#91;vii&#93;;
cout << " h " << sq&#91;*tviii&#93;+sq&#91;viii&#93; << endl;
j = j + 1;

 &#125;

&#125; 
I've looked at this for hours...

What I want to do is what it says in "addsq.hpp", add the file values in an array, but simplify it, using a vector, and not this:

Example:

Code: Select all

a = sq&#91;0&#93;+sq&#91;8&#93;+sq&#91;16&#93;+sq&#91;24&#93;+sq&#91;32&#93;+sq&#91;40&#93;+sq&#91;48&#93;+sq&#91;56&#93;;
b...
c...
d = sq&#91;3&#93;+sq&#91;11&#93;+sq&#91;19&#93;+sq&#91;27&#93;+sq&#91;35&#93;+sq&#91;43&#93;+sq&#91;51&#93;+sq&#91;59&#93;;
...
h = sq&#91;7&#93;+sq&#91;15&#93;+sq&#91;23&#93;+sq&#91;31&#93;+sq&#91;39&#93;+sq&#91;47&#93;+sq&#91;55&#93;+sq&#91;63&#93;;
Any help is much appreciated,

Joshua
smatovic
Posts: 2639
Joined: Wed Mar 10, 2010 10:18 pm
Location: Hamburg, Germany
Full name: Srdja Matovic

Re: Vector problem - square addition

Post by smatovic »

not sure if this is what you look for:

Code: Select all

result&#91;8&#93; /* 0-7 => a-h */

for &#40;i =0; i<64; i++)
    result&#91;i-&#40;i&56&#41;&#93;+=sq&#91;i&#93;
Edmund
Posts: 670
Joined: Mon Dec 03, 2007 3:01 pm
Location: Barcelona, Spain

Re: Vector problem - square addition

Post by Edmund »

Sorry I don't know enough about the vector class, but if you don't need the values to be 4 bytes long, I would suggest to map a 64 bit variable over the address space and use this to save a couple of instructions.
jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Re: Vector problem - square addition

Post by jhaglund »

Code: Select all

j = 8;
int n = 0;
int aa = 1;
int result&#91;8&#93;; /* 0-7 => a-h */ 
 
 
for &#40;i =0; i<64; i++) &#123;
    result&#91;i-&#40;i&56&#41;&#93;+=sq&#91;i&#93;;
     cout << sq&#91;i&#93;;  
     n = n + 1;
            if&#40;n==j*aa&#41;&#123;
            aa = aa + 1;
       cout << "" << endl;  
   &#125;      
      
&#125;
Displays the array...

What is result[] doing ?

I want to add the a,b,c,d,e,f,g,h files scores independently and display them...

Thanks though, for your reply though!

Anymore thoughts?

Joshua
smatovic
Posts: 2639
Joined: Wed Mar 10, 2010 10:18 pm
Location: Hamburg, Germany
Full name: Srdja Matovic

Re: Vector problem - square addition

Post by smatovic »

What is result[] doing ?
Assumed the score for an position is stored in sq[] then you got the total score of a file in result[]. Where result[0] is file a and result[7] is file h.

Code: Select all

for &#40;i =0; i<64; i++) &#123;
    result&#91;i-&#40;i&56&#41;&#93;+=sq&#91;i&#93;;
   &#125;     
for &#40;i =0; i<8; i++) &#123;
     cout << result&#91;i&#93;; 
   &#125;     
     
&#125;

Code: Select all

&#40;i&56&#41;
i&56 gives the rank number of the poistion i.
Edmund
Posts: 670
Joined: Mon Dec 03, 2007 3:01 pm
Location: Barcelona, Spain

Re: Vector problem - square addition

Post by Edmund »

smatovic wrote:not sure if this is what you look for:

Code: Select all

result&#91;8&#93; /* 0-7 => a-h */

for &#40;i =0; i<64; i++)
    result&#91;i-&#40;i&56&#41;&#93;+=sq&#91;i&#93;
i-(i&56)

is the same as

i & 7
jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Re: Vector problem - square addition

Post by jhaglund »

Thanks Srdja,

I had this code tried before your post:

Code: Select all

// addsq.cpp
#include "iostream"

using namespace std;

int main&#40;)
&#123;
  int i;
  int result&#91;8&#93;;/* 0-7 => a-h */ 
  
  int sq&#91;64&#93; = &#123;
0, 1, 1, 2, 2, 1, 1, 0, 
1, 1, 2, 2, 2, 2, 1, 1,
1, 2, 3, 3, 3, 3, 2, 1,
2, 2, 3, 4, 4, 3, 2, 2,
2, 2, 3, 4, 4, 3, 2, 2,
1, 2, 3, 3, 3, 3, 2, 1, 
1, 1, 2, 2, 2, 2, 1, 1,
0, 1, 1, 2, 2, 1, 1, 0
&#125;; 


for &#40;i =0; i<64; i++) &#123; 
    result&#91;i-&#40;i&56&#41;&#93;+=sq&#91;i&#93;; 
   &#125;      
for &#40;i =0; i<8; i++) &#123; 
     cout << " " << result&#91;i&#93;; 
   &#125;      
      system&#40;"pause");
&#125;
It's output:

//incorrect
1972941486 6492708 6492586 2293582 1972941303 18 12 2293624

//correct
8 12 18 22 22 18 12 8

Simplified, but it displays the wrong results. It has to be something simple?! This is a mini example that goes throughout my program. If I can get this, it will reduce the overall code by the thousands of lines. :shock:

Joshua
jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Re: Vector problem - square addition

Post by jhaglund »

Code:

Code: Select all

result&#91;8&#93; /* 0-7 => a-h */ 

for &#40;i =0; i<64; i++) 
    result&#91;i-&#40;i&56&#41;&#93;+=sq&#91;i&#93; 
 
i-(i&56)

is the same as

i & 7

I'm not familar with this technique. What is this technique called?
smatovic
Posts: 2639
Joined: Wed Mar 10, 2010 10:18 pm
Location: Hamburg, Germany
Full name: Srdja Matovic

Re: Vector problem - square addition

Post by smatovic »

hmm, maybe you should init the result array

Code: Select all

int result&#91;&#93; = &#123;0,0,0,0,0,0,0,0&#125;;
jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Re: Vector problem - square addition

Post by jhaglund »

Thanks for the idea