...More chess code?
1
Name:
n3n7i
2011-08-10 23:16
#include <stdlib.h>
//-----------------------------------
struct sPiece { int ID, x, y, Val;};
struct sGroup { int ID, PiD[4]; };
struct sHalfMap{ int ID, GiD[4]; };
struct sFullMap{ int ID, HiD[2]; };
//----------------
struct sFullMap *board, *Boards;
struct sHalfMap *half, *Halves;
struct sGroup *group, *Groups;
struct sPiece *piece, *Pieces;
int iBoards=0, iHalves=0, iGroups=0, iPieces=0;
const cInitPiece[] = { 2,3,4,5,6,4,3,2 };
const PieceA[] = {'-','P','C','H','B','Q','K'};
const PieceB[] = {'-','p','c','h','b','q','k'};
int ShortBoard[8][8];
int ShortPiD[4][4];
int SPx, SPy;
//-----------------------------------
//-------------------------------------------------------------------------------------------------
void iiBoard(){
if(iBoards==0){
Boards = (struct sFullMap *)malloc(sizeof(struct sFullMap));
} else {
board = realloc(Boards,(iBoards+2)* sizeof(struct sFullMap));
if (board!=NULL) Boards = board;
}
}
//------------------
int BEScanner(int a, int b, int c){
int iBe, iHalt=-1;
for(iBe=0;iBe<iBoards;iBe++){
if(Boards[iBe].HiD[0] == b)
if(Boards[iBe].HiD[1] == c) iHalt=1;
if(iHalt==1){printf("Found @ %i", iBe); break;}
}
if(iHalt==-1) iBe=1;
return (iHalt * iBe);
}
//-----------------------
void BoardEntry(int iD, int HiDa, int HiDb){
if (BEScanner(iD, HiDa, HiDb)!=-1) return;
if(iBoards<=iD) iiBoard();
Boards[iD].ID = iD;
Boards[iD].HiD[0] = HiDa;
Boards[iD].HiD[1] = HiDb;
iBoards++;
printf("added %i", iBoards-1);
}
//-----------------------------------
void iiHalf(){
if(iHalves==0){
Halves = (struct sHalfMap *)malloc(sizeof(struct sHalfMap));
} else {
half = realloc(Halves,(iHalves+2)* sizeof(struct sHalfMap));
if (half!=NULL) Halves = half;
}
}
//------------------
int HEScanner(int a, int b, int c, int d){
int iBe, iHalt=-1;
for(iBe=0;iBe<iHalves;iBe++){
if(Halves[iBe].GiD[0] == a)
if(Halves[iBe].GiD[1] == b)
if(Halves[iBe].GiD[2] == c)
if(Halves[iBe].GiD[3] == d) iHalt=1;
if(iHalt==1){printf("Found @ %i", iBe); break;}
}
if(iHalt==-1) iBe=1;
return (iHalt * iBe);
}
//-----------------------
void HalfEntry(int iD, int GiDa, int GiDb, int GiDc, int GiDd){
if (HEScanner(GiDa, GiDb, GiDc, GiDd)!=-1) return;
if(iHalves<=iD) iiHalf();
Halves[iD].ID = iD;
Halves[iD].GiD[0] = GiDa;
Halves[iD].GiD[1] = GiDb;
Halves[iD].GiD[2] = GiDc;
Halves[iD].GiD[3] = GiDd;
iHalves++;
}
//-----------------------------------
void iiGroup(){
if(iGroups==0){
Groups = (struct sGroup *)malloc(sizeof(struct sGroup));
} else {
group = realloc(Groups,(iGroups+2)* sizeof(struct sGroup));
if (group!=NULL) Groups = group;
}
}
//------------------
int GEScanner(int a, int b, int c, int d){
int iBe, iHalt=-1;
for(iBe=0;iBe<iGroups;iBe++){
if(Groups[iBe].PiD[0] == a)
if(Groups[iBe].PiD[1] == b)
if(Groups[iBe].PiD[2] == c)
if(Groups[iBe].PiD[3] == d) iHalt=1;
if(iHalt==1){printf("Found @ %i", iBe); break;}
}
if(iHalt==-1) iBe=1;
return (iHalt * iBe);
}
//-----------------------
void GroupEntry(int iD, int PiDa, int PiDb, int PiDc, int PiDd){
if (GEScanner(PiDa, PiDb, PiDc, PiDd)!=-1) return;
if(iGroups<=iD) iiGroup();
Groups[iD].ID = iD;
Groups[iD].PiD[0] = PiDa;
Groups[iD].PiD[1] = PiDb;
Groups[iD].PiD[2] = PiDc;
Groups[iD].PiD[3] = PiDd;
iGroups++;
}
//-----------------------------------
void iiPiece(){
if(iPieces==0){
Pieces = (struct sPiece *)malloc(sizeof(struct sPiece));
} else {
piece = realloc(Pieces,(iPieces+2)* sizeof(struct sPiece));
if (piece!=NULL) Pieces = piece;
}
}
//------------------
int PEScanner(int a, int b, int c){
int iBe, iHalt=-1;
for(iBe=0;iBe<iPieces;iBe++){
if(Pieces[a].x == a)
if(Pieces[a].y == b)
if(Pieces[a].Val == c) iHalt=1;
if(iHalt==1){printf("Found @ %i", iBe); break;}
}
if(iHalt==-1) iBe=1;
return (iHalt * iBe);
}
//-----------------------
void PieceEntry(int iD, int Px, int Py, int PVal){
if(iPieces<=iD) iiPiece();
Pieces[iD].ID = iD;
Pieces[iD].x = Px;
Pieces[iD].y = Py;
Pieces[iD].Val = PVal;
iPieces++;
}
//-----------------------------------
//------------------------------------------------------------------------------------
void Amnesia(){
BoardEntry(0, 0, 0);
//BoardEntry(1,0,1);
//BoardEntry(2,0,2);
HalfEntry(0,0,1,2,3);
//HalfEntry(1,0,1,2,4);
GroupEntry(0,0,1,2,3);
GroupEntry(1,4,5,6,7);
GroupEntry(2,8,9,10,11);
GroupEntry(3,12,13,14,15);
//GroupEntry(4,12,13,14,15);
int i, y=0;
for(i=0;i<=14;i+=2){
PieceEntry(i, 1,y,1);
PieceEntry(i+1,0,y,(int) cInitPiece[y]);
y++;
}
}
//---------------------------------
void GetBoardX(int i){
int Hint, Gint, Pint;
int z,y,x;
Hint = Boards[i].HiD[0];
for (y=0;y<4;y++){
Gint = Halves[ Hint ].GiD[y];
for (z=0;z<4;z++){
Pint = Groups[ Gint ].PiD[z];
ShortBoard[ Pieces[Pint].x ][ Pieces[Pint].y ] = Pieces[Pint].Val;
ShortPiD[y][z]=Pint;
}
}
Hint = Boards[i].HiD[1];
for (y=0;y<4;y++){
Gint = Halves[ Hint ].GiD[y];
for (z=0;z<4;z++){
Pint = Groups[ Gint ].PiD[z];
ShortBoard[7- Pieces[Pint].x ][7- Pieces[Pint].y ] = -Pieces[Pint].Val;
}
}
return;
}
//-------------------------------
void ClearBoard(){
int x, y;
for(x=0;x<8;x++){
for(y=0;y<8;y++){
ShortBoard[x][y] = 0;
}
}
}
//-----------------------------------
void DrawBoard(){
int x, y;
printf("\n");
for(y=0;y<8;y++){
for(x=0;x<8;x++){
if(ShortBoard[x][y]>=0){
printf("%c ", PieceA[ShortBoard[x][y]]);
} else {
printf("%c ", PieceB[-ShortBoard[x][y]]);
}
}
printf("\n");
}
}
//-----------------------------------
//---------------------------------------------------------------------------------------------------
//--------------------------------------------------------------
void PMove(int x, int y){
int FMove=0;
if(x==1)FMove=1;
if(ShortBoard[x+1][y]==0){
printf("Move %i%i,%i%i Valid\n",x,y,x+1,y);
if((FMove==1) & (ShortBoard[x+2][y]==0)) printf("Move %i%i,%i%i Valid\n",x,y,x+2,y);
}
}
//----------------
void PAttack(int x, int y){
if(ShortBoard[x+1][y+1]<0)
printf("Attack %i%i,%i%i Valid\n",x,y,x+1,y+1);
if(ShortBoard[x+1][y-1]<0)
printf("Attack %i%i,%i%i Valid\n",x,y,x+1,y-1);
}
//-----------------------------------------
int JumpX(int x, int y){
if(((0>x) + (x>7) + (0>y) + (y>7))>=1) return -2;
if(ShortBoard[x][y]<=-1) return -1;
if(ShortBoard[x][y]==0) return 0;
if(ShortBoard[x][y]>=1) return 1;
}
//-----------------------------------------
int BAttackXY(int x, int y, int b, int b2, int c, int d){
int Stepb=x, Stepb2=y;
int Jumpy=0;
while((Stepb!=c) + (Stepb2!=d)==2){
Stepb+=b;
Stepb2+=b2;
if((Jumpy=JumpX(Stepb, Stepb2))!=0) Stepb=c;
printf("%i", Jumpy);
}
}
//-----------------------------------------
void BAttack(int x, int y){
BAttackXY(x,y,1,1,8,8);
BAttackXY(x,y,1,-1,8,-1);
BAttackXY(x,y,-1,1,-1,8);
BAttackXY(x,y,-1,-1,-1,-1);
}
//=--------------------
void CAttackvII(int x, int y){
printf("cx%i cy%i ",x,y);
BAttackXY(x,y,1,0,8,8);
BAttackXY(x,y,0,-1,8,-1);
BAttackXY(x,y,0,1,-1,8);
BAttackXY(x,y,-1,0,-1,-1);
}
//-------------
void QAttack(int x, int y){
BAttack(x, y);
CAttackvII(x, y);
}
//---------------------------
void Jump(int x, int y){
if((0>x) + (x>7)>=1){ printf("Null%i%i",x,y); return;}
if((0>y) + (y>7)>=1){printf("Null%i%i",x,y); return;}
if(ShortBoard[x][y]>=1) printf("Blocked %i%i",x,y);
if(ShortBoard[x][y]==0) printf("Open %i%i",x,y);
if(ShortBoard[x][y]<=-1) printf("Hit %i%i",x,y);
//printf("?");
}
//---------------------------
//------------------
void KAttack(x,y){
Jump(x+1, y+1); Jump(x+1, y); Jump(x+1, y-1);
Jump(x-1, y+1); Jump(x-1, y); Jump(x-1, y-1);
Jump(x, y+1); Jump(x, y-1);
}
//-----------------
void HAttack(x,y){
Jump(x+2, y+1); Jump(x+2, y-1);
Jump(x-2, y+1); Jump(x-2, y-1);
Jump(x+1, y+2); Jump(x+1, y-2);
Jump(x-1, y+2); Jump(x-1, y-2);
}
//-----------------
void Moves(int x, int y){
int PieceType=ShortBoard[x][y];
printf("%i @ %i,%i", ShortBoard[x][y], x ,y);
if (PieceType==1)
printf("GotPawn\n");
else if(PieceType==2)
printf("\nGotCastle\n");
else if(PieceType==3)
printf("\nGotHorse\n");
else if(PieceType==4)
printf("\nGotBishop\n");
else if(PieceType==5)
printf("\nGotQueen\n");
else if(PieceType==6)
printf("\nGotKing\n");
switch (PieceType){
case 1: PMove(x, y);
PAttack(x, y);
break;
case 2: CAttackvII(x, y);
break;
case 3: HAttack(x, y);
break;
case 4: BAttack(x, y);
break;
case 5: QAttack(x, y);
break;
case 6: KAttack(x, y);
break;
}
//printf("Move?\n");
}
void GetMoves(){
for(SPx=0;SPx<4;SPx++){
for(SPy=0;SPy<4;SPy++){
if(ShortBoard[ Pieces[ShortPiD[SPx][SPy]].x ][ Pieces[ShortPiD[SPx][SPy]].y ]>0){
//printf("Found piece %i\n", ShortBoard[x][y]);
Moves(Pieces[ShortPiD[SPx][SPy]].x, Pieces[ShortPiD[SPx][SPy]].y);
}
}
}
}
//-----------------------------------------------------------------------
void main(){
Amnesia();
ClearBoard();
GetBoardX(0);
DrawBoard();
GetMoves();
free(Boards);
free(Halves);
free(Groups);
free(Pieces);
return;
}
2
Name:
Anonymous
2011-08-10 23:21
this isn't about anuses or touhou or autism
fuck off
3
Name:
n3n7i
2011-08-11 0:25
...Is that the joke?
...Or are you #2
I would but she's probably got an infection...? =)
Anyhow.....
Any Idea's for storing struct's on File?
Do i need to convert to string to Have the Data person-readable?
...It doesn't have to be, but seems like a nice idea..
4
Name:
Anonymous
2011-08-11 3:15
Any Idea's for storing struct's on File?
http://en.wikipedia.org/wiki/Serialization
Also, please learn some basic grammar so people won't think you're a moron.
5
Name:
n3n7i
2011-08-11 3:59
...<<that?
... =) Which ones?
There's always going to be someone that thinks i'm a moron...
btw cheers i'll have a read
6
Name:
Anonymous
2011-08-11 5:29
>>5
stop that shit, this isn't /gaia/
7
Name:
n3n7i
2011-08-11 5:52
... =)
You're mission should you choose to accept it :
Noob tier : Repair the PieceEntry function (Its missing something)
Next tier : On request / Completion of above
Deadline : 24hr ?!
8
Name:
Anonymous
2011-08-11 6:32
>>7
I'm not mission!
I also think you're a moron.
9
Name:
n3n7i
2011-08-11 7:34
=) i don't really mind...
I'm starting to think so myself....
btw perhaps it was intentional? ;D
10
Name:
n3n7i
2011-08-11 9:44
11
Name:
n3n7i
2011-08-12 8:21
...Here's hoping this thread is now beyond the trolls reach..?
...=) *blog potential?*
//---------------------------------
void AutoMapEntry(x, y){
printf("\nAuto :: %i, %i, %i, %i",iPieces, x, y, Pieces[ShortPiD[SPx][SPy]].Val);
int AMPid = PieceEntry(iPieces, x, y, Pieces[ShortPiD[SPx][SPy]].Val);
int iGetID;
int iGetIDX[4];
for(iGetID=0;iGetID<4;iGetID++){
if(iGetID==SPy) iGetIDX[iGetID]=AMPid;
if(iGetID!=SPy) iGetIDX[iGetID]=Pieces[ShortPiD[SPx][iGetID]].ID;
}
printf("\n AutoG %i, %i, %i, %i, %i",iGroups, iGetIDX[0], iGetIDX[1], iGetIDX[2], iGetIDX[3]);
int AMGid = GroupEntry(iGroups, iGetIDX[0], iGetIDX[1], iGetIDX[2], iGetIDX[3]);
for(iGetID=0;iGetID<4;iGetID++){
if(iGetID==SPx) iGetIDX[iGetID]=AMGid;
if(iGetID!=SPx) iGetIDX[iGetID]=Groups[ShortGiD[iGetID]].ID;
}
printf("\n AutoH %i, %i, %i, %i, %i\n",iHalves, iGetIDX[0], iGetIDX[1], iGetIDX[2], iGetIDX[3]);
int AMHid = HalfEntry(iHalves, iGetIDX[0], iGetIDX[1], iGetIDX[2], iGetIDX[3]);
printf("AutoB %i, %i, %i\n", iBoards, Boards[ShortBiD].HiD[1], AMHid);
int AMMid = BoardEntry(iBoards, Boards[ShortBiD].HiD[1], AMHid);
printf("SPx, SPy, %i, %i\n\n", SPx, SPy);
}
//--------------------------------------------------------------
int JumpZ(int x, int y, int Allow0, int Allow1){
if(((0>x) + (x>7) + (0>y) + (y>7))>=1) return -2;
if(ShortBoard[x][y]>=1) return 1;
if(((ShortBoard[x][y]==0) + (Allow0==1))==2){ AutoMapEntry(x, y); return 0; }
if((ShortBoard[x][y]<=-1) + (Allow1==1)==2) return -1;
}
//-----------------------------------------
//---------------------------------
12
Name:
n3n7i
2011-08-12 9:02
...Infinite depth recursion (till my computer gets bogged anyway.... No pruning yet, so a couple of moves deep at best..)
Can now start getting next boards(1-21) > next moves (22-421?) > next boards(22-421) > nextmoves.....
...I guess i'll have to start thinking a bit harder soon =/
...Time for storage?!
13
Name:
n3n7i
2011-08-12 10:02
...soon
some output[//15000 boards in a few seconds....]
C P - - - - p c
H P - - - - p h
B P - - - - p b
Q P - p - - - k
K P - - - - p q
B - P - - - p b
H P - - - - p h
C P - - - - p c
1 @ 1,0Found @ 16
Auto2 :: 16, 2, 0, 1Found @ 4
AutoG 4, 16, 1, 2, 3Found @ 26
AutoH 26, 4, 1, 14, 3Found @ 14906
AutoB 14906, 113, 26
Move 10,20 Valid
Found @ 17
Auto2 :: 17, 3, 0, 1Found @ 5
AutoG 5, 17, 1, 2, 3Found @ 41
AutoH 41, 5, 1, 14, 3Found @ 15015
AutoB 15015, 113, 41
Move 10,30 Valid
2 @ 0,01 @ 1,1Found @ 18
Auto2 :: 18, 2, 1, 1Found @ 6
AutoG 6, 0, 1, 18, 3Found @ 54
AutoH 54, 6, 1, 14, 3Found @ 15116
AutoB 15116, 113, 54
Move 11,21 Valid
Found @ 19
Auto2 :: 19, 3, 1, 1Found @ 7
AutoG 7, 0, 1, 19, 3Found @ 67
AutoH 67, 7, 1, 14, 3Found @ 15211
AutoB 15211, 113, 67
Move 11,31 Valid
3 @ 0,11 @ 1,2Found @ 20
Auto2 :: 20, 2, 2, 1Found @ 8
AutoG 8, 20, 5, 6, 7Found @ 78
AutoH 78, 0, 8, 14, 3Found @ 15298
AutoB 15298, 113, 78
Move 12,22 Valid
Found @ 21
Auto2 :: 21, 3, 2, 1Found @ 9
AutoG 9, 21, 5, 6, 7Found @ 89
AutoH 89, 0, 9, 14, 3Found @ 15379
AutoB 15379, 113, 89
Move 12,32 Valid
4 @ 0,21 @ 1,3Found @ 22
Auto2 :: 22, 2, 3, 1Found @ 10
AutoG 10, 4, 5, 22, 7Found @ 98
AutoH 98, 0, 10, 14, 3Found @ 15451
AutoB 15451, 113, 98
Move 13,23 Valid
5 @ 0,31 @ 1,4Found @ 24
Auto2 :: 24, 2, 4, 1Found @ 37
AutoG 37, 24, 9, 26, 11Found @ 114
AutoH 114, 0, 1, 37, 3Found @ 15567
AutoB 15567, 113, 114
Move 14,24 Valid
Found @ 25
Auto2 :: 25, 3, 4, 1Found @ 40
AutoG 40, 25, 9, 26, 11Found @ 121
AutoH 121, 0, 1, 40, 3Found @ 15622
AutoB 15622, 113, 121
Move 14,34 Valid
6 @ 0,41 @ 2,5Found @ 27
Auto2 :: 27, 3, 5, 1Found @ 15
AutoG 15, 8, 9, 27, 11Found @ 12
AutoH 12, 0, 1, 15, 3added 15670
AutoB 15670, 113, 12
Move 25,35 Valid
Found @ 42
Auto2 :: 42, 4, 5, 1Found @ 42
AutoG 42, 8, 9, 42, 11Found @ 127
AutoH 127, 0, 1, 42, 3added 15671
AutoB 15671, 113, 127
4 @ 0,51 @ 1,6Found @ 28
Auto2 :: 28, 2, 6, 1Found @ 16
AutoG 16, 28, 13, 14, 15Found @ 128
AutoH 128, 0, 1, 14, 16added 15672
AutoB 15672, 113, 128
Move 16,26 Valid
Found @ 29
Auto2 :: 29, 3, 6, 1Found @ 17
AutoG 17, 29, 13, 14, 15Found @ 129
AutoH 129, 0, 1, 14, 17added 15673
AutoB 15673, 113, 129
Move 16,36 Valid
3 @ 0,61 @ 1,7Found @ 30
Auto2 :: 30, 2, 7, 1Found @ 18
AutoG 18, 12, 13, 30, 15Found @ 130
AutoH 130, 0, 1, 14, 18added 15674
AutoB 15674, 113, 130
Move 17,27 Valid
Found @ 31
Auto2 :: 31, 3, 7, 1Found @ 19
AutoG 19, 12, 13, 31, 15Found @ 131
AutoH 131, 0, 1, 14, 19added 15675
AutoB 15675, 113, 131
Move 17,37 Valid
2 @ 0,7
C P - - - - p c
H P - - - - p h
B P - - - p - b
Q P - - - p - k
K P - - - - p q
B - P - - - p b
H P - - - - p h
C P - - - - p c
1 @ 1,0Found @ 16
Auto2 :: 16, 2, 0, 1Found @ 4
AutoG 4, 16, 1, 2, 3Found @ 26
AutoH 26, 4, 1, 14, 3Found @ 14922
AutoB 14922, 114, 26
Move 10,20 Valid
Found @ 17
Auto2 :: 17, 3, 0, 1FouTerminate batch job (Y/N)?
14
Name:
Anonymous
2011-08-12 14:52
Does it support protocols supported by xboard? No? fuck that shit.
15
Name:
Anonymous
2011-08-12 15:56
fuck you cose i change since 4 jeahrs in man how i wouldnt even talk with with out vomiting
16
Name:
n3n7i
2011-08-13 0:03
Just needs a start / stop switch // Move output like a3-b4 ?
...should be reasonably xboard compatiable?
...I want it playing chess before i build in that part anyways?!
...Storage / move eval / Alpha-beta pruning(hopefully)... etc first
17
Name:
n3n7i
2011-08-13 3:16
Thought i'd just leave this ere
http://bellard.org/tcc/
The Tiny C compiler [300Kb zip // ~ 1mb in full]
-Freeware- =)
...was going to put more code up... [eg whole thing] but was too large now (+100 to 500 Lines) // mostly all the same anyway...
This should be enough? hopefully =)
//------------------
int PEScanner(int a, int b, int c){
int iBe, iHalt=-1;
for(iBe=0;iBe<iPieces;iBe++){
if(Pieces[iBe].x == a)
if(Pieces[iBe].y == b)
if(Pieces[iBe].Val == c){/*printf("Found @ %i", iBe);*/ return iBe;}
}
return -1;
}
//-----------------------
int PieceEntry(int iD, int Px, int Py, int PVal){
int iPE=0;
if ((iPE=PEScanner(Px, Py, PVal))!=-1) return iPE;
if(iPieces<=iD) iiPiece();
Pieces[iD].ID = iD;
Pieces[iD].x = Px;
Pieces[iD].y = Py;
Pieces[iD].Val = PVal;
iPieces++;
return iPieces-1;
}
//-----------------------------------------------------------------------
void main(){
Amnesia();
ClearBoard();
GetBoardX(0);
DrawBoard();
GetMoves();
int i=1;
while(i<=5000){ //iBoards
ClearBoard();
GetBoardX(i);
DrawBoard();
GetMoves();
i++;
}
AllOut();
free(Boards);
free(Halves);
free(Groups);
free(Pieces);
printf("\n\n--Boards::%i--\n",iBoards);
return;
}
//-----------------------------------
//Scanned 5000 Boards in ~1.5 min +>text.txt printf Buffering
//Found --Boards::67648-- <<Fullmap entries (~1mb ?!)
// --287, 0, 1, 2, 133-- << 287 Halfmaps... (Tiny..)
18
Name:
Anonymous
2011-08-13 6:20
19
Name:
Anonymous
2011-08-13 7:56
>>17
no, go away. go to reddit.
20
Name:
n3n7i
2011-08-15 5:23
...Nobody will notice =)
#include <stdio.h>
#include <stdlib.h>
//-----------------------------------
struct sPiece { int ID, x, y, Val;};
struct sGroup { int ID, PiD[4]; };
struct sHalfMap{ int ID, GiD[4]; };
struct sFullMap{ int ID, HiD[2], FTotal; };
//----------------
struct sFullMap *board, *Boards, tempBoard;
struct sHalfMap *half, *Halves;
struct sGroup *group, *Groups;
struct sPiece *piece, *Pieces;
int iBoards=0, iHalves=0, iGroups=0, iPieces=0;
const cInitPiece[] = { 2,3,4,5,6,4,3,2 };
const PieceA[] = {'-','P','C','H','B','Q','K'};
const PieceB[] = {'-','p','c','h','b','q','k'};
int ShortBoard[8][8];
int BlockBoard[8][8];
int AttackTotal=0;
int BlockTotal=0;
int MoveTotal=0;
int FullTotal=0;
int AddEntryState=0;
int ShortBiD;
int ShortGiD[4];
int ShortPiD[4][4];
int SPx, SPy;
//-----------------------------------
//-------------------------------------------------------------------------------------------------
void iiBoard(){
if(iBoards==0){
Boards = (struct sFullMap *)malloc(sizeof(struct sFullMap));
} else {
board = realloc(Boards,(iBoards+2)* sizeof(struct sFullMap));
if (board!=NULL) Boards = board;
}
}
//------------------
int BEScanner(int a, int b, int c){
int iBe, iHalt=-1;
for(iBe=0;iBe<iBoards;iBe++){
if(Boards[iBe].HiD[0] == b)
if(Boards[iBe].HiD[1] == c) iHalt=1;
if(iHalt==1){/*printf("Found @ %i", iBe);*/ break;}
}
if(iHalt==-1) iBe=1;
return (iHalt * iBe);
}
//-----------------------
int BoardEntry(int iD, int HiDa, int HiDb){
int iBES=0;
if ((iBES = BEScanner(iD, HiDa, HiDb))!=-1) return iBES;
if(iBoards<=iD) iiBoard();
Boards[iD].ID = iD;
Boards[iD].HiD[0] = HiDa;
Boards[iD].HiD[1] = HiDb;
Boards[iD].FTotal = 0;
iBoards++;
//printf("added %i", iBoards-1);
return iBoards-1;
}
//-----------------------------------
void iiHalf(){
if(iHalves==0){
Halves = (struct sHalfMap *)malloc(sizeof(struct sHalfMap));
} else {
half = realloc(Halves,(iHalves+2)* sizeof(struct sHalfMap));
if (half!=NULL) Halves = half;
}
}
//------------------
int HEScanner(int a, int b, int c, int d){
int iBe, iHalt=-1;
for(iBe=0;iBe<iHalves;iBe++){
if(Halves[iBe].GiD[0] == a)
if(Halves[iBe].GiD[1] == b)
if(Halves[iBe].GiD[2] == c)
if(Halves[iBe].GiD[3] == d) iHalt=1;
if(iHalt==1){/*printf("Found @ %i", iBe);*/ break;}
}
if(iHalt==-1) iBe=1;
return (iHalt * iBe);
}
//-----------------------
int HalfEntry(int iD, int GiDa, int GiDb, int GiDc, int GiDd){
int iHES=0;
if ((iHES = HEScanner(GiDa, GiDb, GiDc, GiDd))!=-1) return iHES;
if(iHalves<=iD) iiHalf();
Halves[iD].ID = iD;
Halves[iD].GiD[0] = GiDa;
Halves[iD].GiD[1] = GiDb;
Halves[iD].GiD[2] = GiDc;
Halves[iD].GiD[3] = GiDd;
iHalves++;
return iHalves-1;
}
//-----------------------------------
void iiGroup(){
if(iGroups==0){
Groups = (struct sGroup *)malloc(sizeof(struct sGroup));
} else {
group = realloc(Groups,(iGroups+2)* sizeof(struct sGroup));
if (group!=NULL) Groups = group;
}
}
//------------------
int GEScanner(int a, int b, int c, int d){
int iBe, iHalt=-1;
for(iBe=0;iBe<iGroups;iBe++){
if(Groups[iBe].PiD[0] == a)
if(Groups[iBe].PiD[1] == b)
if(Groups[iBe].PiD[2] == c)
if(Groups[iBe].PiD[3] == d) iHalt=1;
if(iHalt==1){/*printf("Found @ %i", iBe);*/ break;}
}
if(iHalt==-1) iBe=1;
return (iHalt * iBe);
}
//-----------------------
int GroupEntry(int iD, int PiDa, int PiDb, int PiDc, int PiDd){
int iGES=0;
if ((iGES = GEScanner(PiDa, PiDb, PiDc, PiDd))!=-1) return iGES;
if(iGroups<=iD) iiGroup();
Groups[iD].ID = iD;
Groups[iD].PiD[0] = PiDa;
Groups[iD].PiD[1] = PiDb;
Groups[iD].PiD[2] = PiDc;
Groups[iD].PiD[3] = PiDd;
iGroups++;
return iGroups-1;
}
//-----------------------------------
void iiPiece(){
if(iPieces==0){
Pieces = (struct sPiece *)malloc(sizeof(struct sPiece));
} else {
piece = realloc(Pieces,(iPieces+2)* sizeof(struct sPiece));
if (piece!=NULL) Pieces = piece;
}
}
//------------------
int PEScanner(int a, int b, int c){
int iBe, iHalt=-1;
for(iBe=0;iBe<iPieces;iBe++){
if(Pieces[iBe].x == a)
if(Pieces[iBe].y == b)
if(Pieces[iBe].Val == c){/*printf("Found @ %i", iBe);*/ return iBe;}
}
return -1;
}
//-----------------------
int PieceEntry(int iD, int Px, int Py, int PVal){
int iPE=0;
if ((iPE=PEScanner(Px, Py, PVal))!=-1) return iPE;
if(iPieces<=iD) iiPiece();
Pieces[iD].ID = iD;
Pieces[iD].x = Px;
Pieces[iD].y = Py;
Pieces[iD].Val = PVal;
iPieces++;
return iPieces-1;
}
//-----------------------------------
//------------------------------------------------------------------------------------
void Amnesia(){
BoardEntry(0, 0, 0);
//BoardEntry(1,0,1);
//BoardEntry(2,0,2);
HalfEntry(0,0,1,2,3);
//HalfEntry(1,0,1,2,4);
GroupEntry(0,0,1,2,3);
GroupEntry(1,4,5,6,7);
GroupEntry(2,8,9,10,11);
GroupEntry(3,12,13,14,15);
//GroupEntry(4,12,13,14,15);
int i, y=0;
for(i=0;i<=14;i+=2){
PieceEntry(i, 1,y,1);
PieceEntry(i+1,0,y,(int) cInitPiece[y]);
y++;
//printf("Piece %i %i\n", i, i+1);
}
}
//---------------------------------
int AutoMapEntry(x, y){
if(AddEntryState==1){
int AMPid = PieceEntry(iPieces, x, y, Pieces[ShortPiD[SPx][SPy]].Val);
//printf("\nAuto2 :: %i, %i, %i, %i",AMPid, x, y, Pieces[ShortPiD[SPx][SPy]].Val);
//if(AMPid!=(iPieces-1)) printf("-----------\n\n%i?%i",AMPid, iPieces);
int iGetID;
int iGetIDX[4];
for(iGetID=0;iGetID<4;iGetID++){
if(iGetID==SPy) iGetIDX[iGetID]=AMPid;
if(iGetID!=SPy) iGetIDX[iGetID]=Pieces[ShortPiD[SPx][iGetID]].ID;
}
int AMGid = GroupEntry(iGroups, iGetIDX[0], iGetIDX[1], iGetIDX[2], iGetIDX[3]);
//printf("\n AutoG %i, %i, %i, %i, %i",AMGid, iGetIDX[0], iGetIDX[1], iGetIDX[2], iGetIDX[3]);
for(iGetID=0;iGetID<4;iGetID++){
if(iGetID==SPx) iGetIDX[iGetID]=AMGid;
if(iGetID!=SPx) iGetIDX[iGetID]=Groups[ShortGiD[iGetID]].ID;
}
int AMHid = HalfEntry(iHalves, iGetIDX[0], iGetIDX[1], iGetIDX[2], iGetIDX[3]);
//printf("\n AutoH %i, %i, %i, %i, %i",AMHid, iGetIDX[0], iGetIDX[1], iGetIDX[2], iGetIDX[3]);
int AMMid = BoardEntry(iBoards, Boards[ShortBiD].HiD[1], AMHid);
//int AMMid = BoardEntry(iBoards, AMHid, Boards[ShortBiD].HiD[1]);
//printf("\nAutoB %i, %i, %i\n", AMMid, Boards[ShortBiD].HiD[1], AMHid);
//printf("SPx, SPy, %i, %i\n\n", SPx, SPy);
}
}
//---------------------------------
void GetBoardX(int i){
int Hint, Gint, Pint;
int z,y,x;
ShortBiD = i;
Hint = Boards[i].HiD[0];
for (y=0;y<4;y++){
Gint = Halves[ Hint ].GiD[y];
ShortGiD[y] = Gint;
for (z=0;z<4;z++){
Pint = Groups[ Gint ].PiD[z];
ShortBoard[ Pieces[Pint].x ][ Pieces[Pint].y ] = Pieces[Pint].Val;
ShortPiD[y][z]=Pint;
}
}
Hint = Boards[i].HiD[1];
for (y=0;y<4;y++){
Gint = Halves[ Hint ].GiD[y];
for (z=0;z<4;z++){
Pint = Groups[ Gint ].PiD[z];
ShortBoard[7- Pieces[Pint].x ][ Pieces[Pint].y ] = -Pieces[Pint].Val;
}
}
return;
}
//-------------------------------
int ClearBoard(){
int x, y;
for(x=0;x<8;x++){
for(y=0;y<8;y++){
ShortBoard[x][y] = 0;
}
}
}
//-------------------------------
int ClearBlockBoard(){
int x, y;
BlockTotal=0;
for(x=0;x<8;x++){
for(y=0;y<8;y++){
BlockBoard[x][y] = 0;
}
}
}
//-----------------------------------
void DrawBoard(){
int x, y;
printf("\n\n");
for(y=0;y<8;y++){
for(x=0;x<8;x++){
if(ShortBoard[x][y]>=0){
printf("%c ", PieceA[ShortBoard[x][y]]);
} else {
printf("%c ", PieceB[-ShortBoard[x][y]]);
}
}
printf("\n");
}
printf("\n");
}
//-----------------------------------
void DrawBlockBoard(){
int x, y;
printf("\n\n");
for(y=0;y<8;y++){
for(x=0;x<8;x++){
printf("%i ", BlockBoard[x][y]);
}
printf("\n");
}
printf(" AttackTotal : %i\n", AttackTotal);
printf(" BlockTotal : %i\n", BlockTotal);
printf(" MoveTotal : %i\n", MoveTotal);
FullTotal = MoveTotal + BlockTotal + AttackTotal;
printf(" Total : %i\n", MoveTotal + BlockTotal + AttackTotal);
}
//-----------------------------------------
void GetTotal(){
FullTotal = MoveTotal + BlockTotal + AttackTotal;
printf(" Total : %i\n", FullTotal);
}
//-----------------------------------
21
Name:
n3n7i
2011-08-15 5:24
Part II
//--------------------------------------------------------------
int JumpZ(int x, int y, int Allow0, int Allow1){
if(((0>x) + (x>7) + (0>y) + (y>7))>=1) return -2;
if((ShortBoard[x][y]>=1) + (Allow1==1)==2){ BlockBoard[x][y]++; BlockTotal++; return 1;}
if(((ShortBoard[x][y]==0) + (Allow0==1))==2){ AutoMapEntry(x, y); MoveTotal++; return 0; }
if((ShortBoard[x][y]<=-1) + (Allow1==1)==2){ AttackTotal+=-ShortBoard[x][y]; return -1; }
return 1;
}
//-----------------------------------------
int Jump(int x, int y){
if(((0>x) + (x>7) + (0>y) + (y>7))>=1) return -2;
if(ShortBoard[x][y]>=1) { BlockBoard[x][y]++; BlockTotal++; return 1;}
if(ShortBoard[x][y]==0){ AutoMapEntry(x, y); MoveTotal++; return 0; }
if(ShortBoard[x][y]<=-1){ AttackTotal+=-ShortBoard[x][y]; return -1; }
}
//-----------------------------------------
void PMove(int x, int y){
int FMove=0;
if(x==1)FMove=1;
if(JumpZ(x+1, y, 1, 0)==0){
//printf("Move %i%i,%i%i Valid\n",x,y,x+1,y);
if( ((FMove==1) + (JumpZ(x+2, y, 1, 0)==0) )==2);// printf("Move %i%i,%i%i Valid\n",x,y,x+2,y);
}
}
//----------------
void PAttack(int x, int y){
if(JumpZ(x+1, y+1, 0, 1)==-1);
//printf("Attack %i%i,%i%i Valid\n",x,y,x+1,y+1);
if(JumpZ(x+1, y-1, 0, 1)==-1);
//printf("Attack %i%i,%i%i Valid\n",x,y,x+1,y-1);
}
//-----------------------------------------
/*
int JumpX(int x, int y){
if(((0>x) + (x>7) + (0>y) + (y>7))>=1) return -2;
if(ShortBoard[x][y]<=-1) return -1;
if(ShortBoard[x][y]==0) return 0;
if(ShortBoard[x][y]>=1) return 1;
}
*/
//-----------------------------------------
int BAttackXY(int x, int y, int b, int b2, int c, int d){
int Stepb=x, Stepb2=y;
int Jumpy=0;
while((Stepb!=c) + (Stepb2!=d)==2){
Stepb+=b;
Stepb2+=b2;
if((Jumpy=Jump(Stepb, Stepb2))!=0) Stepb=c;
//printf("%i", Jumpy);
}
}
//-----------------------------------------
void BAttack(int x, int y){
BAttackXY(x,y,1,1,8,8);
BAttackXY(x,y,1,-1,8,-1);
BAttackXY(x,y,-1,1,-1,8);
BAttackXY(x,y,-1,-1,-1,-1);
}
//=--------------------
void CAttackvII(int x, int y){
//printf("cx%i cy%i ",x,y);
BAttackXY(x,y,1,0,8,8);
BAttackXY(x,y,0,-1,8,-1);
BAttackXY(x,y,0,1,-1,8);
BAttackXY(x,y,-1,0,-1,-1);
}
//-------------
void QAttack(int x, int y){
BAttack(x, y);
CAttackvII(x, y);
}
//---------------------------
/*
void Jump(int x, int y){
if((0>x) + (x>7)>=1){ printf("Null%i%i",x,y); return;}
if((0>y) + (y>7)>=1){printf("Null%i%i",x,y); return;}
if(ShortBoard[x][y]>=1) printf("Blocked %i%i",x,y);
if(ShortBoard[x][y]==0) printf("Open %i%i",x,y);
if(ShortBoard[x][y]<=-1) printf("Hit %i%i",x,y);
//printf("?");
}*/
//---------------------------
//------------------
void KAttack(x,y){
Jump(x+1, y+1); Jump(x+1, y); Jump(x+1, y-1);
Jump(x-1, y+1); Jump(x-1, y); Jump(x-1, y-1);
Jump(x, y+1); Jump(x, y-1);
}
//-----------------
void HAttack(x,y){
Jump(x+2, y+1); Jump(x+2, y-1);
Jump(x-2, y+1); Jump(x-2, y-1);
Jump(x+1, y+2); Jump(x+1, y-2);
Jump(x-1, y+2); Jump(x-1, y-2);
}
//-----------------
void Moves(int x, int y){
int PieceType=ShortBoard[x][y];
//printf("%i @ %i,%i", ShortBoard[x][y], x ,y);
/*if (PieceType==1)
printf("GotPawn\n");
else if(PieceType==2)
printf("\nGotCastle\n");
else if(PieceType==3)
printf("\nGotHorse\n");
else if(PieceType==4)
printf("\nGotBishop\n");
else if(PieceType==5)
printf("\nGotQueen\n");
else if(PieceType==6)
printf("\nGotKing\n");
*/
switch (PieceType){
case 1: PMove(x, y);
PAttack(x, y);
break;
case 2: CAttackvII(x, y);
break;
case 3: HAttack(x, y);
break;
case 4: BAttack(x, y);
break;
case 5: QAttack(x, y);
break;
case 6: KAttack(x, y);
break;
}
//printf("Move?\n");
}
//-----------------------------
void GetMoves(int AddEntry){
AddEntryState=AddEntry;
MoveTotal=0;
AttackTotal=0;
for(SPx=0;SPx<4;SPx++){
for(SPy=0;SPy<4;SPy++){
if(ShortBoard[ Pieces[ShortPiD[SPx][SPy]].x ][ Pieces[ShortPiD[SPx][SPy]].y ]>0){
//printf("Found piece %i\n", ShortBoard[x][y]);
Moves(Pieces[ShortPiD[SPx][SPy]].x, Pieces[ShortPiD[SPx][SPy]].y);
}
}
}
}
//-----------------------------
void AllOut(){
int i;
for(i=0;i<iBoards;i++){
printf("Id-%i, Hid-0%i, Hid1-%i, FTotal-%i\n",Boards[i].ID, Boards[i].HiD[0], Boards[i].HiD[1], Boards[i].FTotal);
}
for(i=0;i<iHalves;i++){
printf("%i, %i, %i, %i, %i\n",Halves[i].ID, Halves[i].GiD[0], Halves[i].GiD[1],Halves[i].GiD[2], Halves[i].GiD[3]);
}
}
//-----------------------------------------------------------------------
void RunBoardX(int rbx){
ClearBoard();
GetBoardX(rbx);
GetMoves(0);
GetTotal();
}
//int BoardScore[40];
int Premed(int rip){
int i=1, zTotal=0, sLoop=0;
RunBoardX(rip);
sLoop = MoveTotal;
while(i<=sLoop){ //iBoards
RunBoardX(i);
zTotal+=FullTotal;
//BoardScore[i] = FullTotal;
ClearBlockBoard();
i++;
}
return (int) zTotal / sLoop;
}
//---------------------------------------------------
int GetTempBoard(int i){
if(i>=iBoards) return 0;
printf("B%i\n", i);
return 1;
}
void BoardStore(){
printf("BoardStore %i\n", iBoards);
FILE *fp;
int i=0;
int D=0;
int Err=0;
if((fp = fopen("./myfile.text", "wb"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
while(GetTempBoard(i)){
D=(int)Boards[i].ID;
if(fwrite(&Boards[i], sizeof Boards[i], 1, fp) != 1) {
printf("Write error.\n");
Err++;
}
if(Err>=1) break;
i++;
}
fclose(fp);
}
//--------------------------------------------------------
int LoadBoard(){
printf("BoardStore %i\n", iBoards);
FILE *fp;
int i=0;
int D=0;
int Err=0;
if((fp = fopen("./myfile.text", "rb"))==NULL) {
printf("Cannot open file.\n");
return 0;
}
iiBoard();
iBoards++;
while(1){
if(fread(&Boards[i], sizeof tempBoard, 1, fp)==0){
printf("Read Err");
Err++;
}
if(Err>=1) break;
//tempBoard = Boards[i];
//Boards[i] = tempBoard;
printf("Read %i %i %i %i", Boards[i].ID, Boards[i].HiD[0], Boards[i].HiD[1], Boards[i].FTotal);
i++;
iiBoard();
iBoards++;
}
fclose(fp);
printf("Boards %i", iBoards);
return iBoards;
}
//====================================================
void main(){
int xTotal=0;
int zTotal=0;
//if(LoadBoard()==0)
Amnesia();
//return 0;
//ClearBoard();
//ClearBlockBoard();
//GetBoardX(0);
//DrawBoard();
//GetMoves(1);
//GetTotal();
//Boards[0].FTotal = FullTotal;
//xTotal = FullTotal;
//DrawBlockBoard();
//ClearBlockBoard();
int i=1;
//int AVG = Premed(0);
ClearBoard();
GetBoardX(0);
DrawBoard();
GetMoves(1);
while(i<=20){ //iBoards
ClearBoard();
GetBoardX(i);
DrawBoard();
//GetMoves(0);
//DrawBlockBoard();
//GetTotal();
//Boards[i].FTotal = FullTotal;
//if(FullTotal>=AVG) GetMoves(1);
//zTotal+=FullTotal;
GetMoves(1);
ClearBlockBoard();
i++;
}
AllOut();
BoardStore();
free(Boards);
iBoards=0;
LoadBoard();
//printf("\n%d\n", zTotal / 20);
free(Boards);
free(Halves);
free(Groups);
free(Pieces);
printf("\n\n--Boards::%i--\n",iBoards);
return;
}