summaryrefslogtreecommitdiffstats
path: root/twin4/twin4/twin4proc.cpp
blob: 81f17324fb366ca5e4f4316ddfc49eb4c1974aa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/***************************************************************************
                          kproc4.cpp  -  description
                             -------------------
    begin                : Sun Apr 9 2000
    copyright            : (C) 2000 by Martin Heni
    email                : martin@heni-online.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "twin4proc.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <kgamemessage.h>
#include <kdebug.h>

#define MIN_TIME 1  // sec

#define NOOFPLAYER 2/* Zwei Spieler */
#define MAXANZAHL 6 /* Maximal 6 Steine pro Reihe */
#define WIN4 4      /* 4er Reihe hat gewonnen */
#define MAXZUG 42   /* Soviele Zuege moeglich */
#define FELD_OFF 10
#define LOWERT -999999999L
#define SIEG_WERT 9999999L


#define START_REK 1    // (0) 1:Nur Stellungsbewertung bei Level 1
                       //     0:Level 1 schon eine Rekursion

TDEComputer::TDEComputer() : TQObject(0,0)
{
    InitField();

    const char *s1="7777776666666123456654321123456654321";
    const char *s2="0000000000000000000123456000000123456";

    unsigned int i;
    for (i=0;i<strlen(s1);i++)
      lenofrow[i]=s1[i]-'0';
    for (i=0;i<strlen(s2);i++)
      startofrow[i]=s2[i]-'0';

  connect(&proc,TQT_SIGNAL(signalCommand(TQDataStream &,int ,int ,int )),
                   this,TQT_SLOT(slotCommand(TQDataStream & ,int ,int ,int )));
  connect(&proc,TQT_SIGNAL(signalInit(TQDataStream &,int)),
                   this,TQT_SLOT(slotInit(TQDataStream & ,int )));
  connect(&proc,TQT_SIGNAL(signalTurn(TQDataStream &,bool )),
                   this,TQT_SLOT(slotTurn(TQDataStream & ,bool )));
  fprintf(stderr, "----------------->\nTDEComputer::Computer\n");
}

void TDEComputer::slotInit(TQDataStream &in,int id)
{
  fprintf(stderr,"----------------->\nTDEComputer::slotInit\nid:%d\n",id);
  /*
  TQByteArray buffer;
  TQDataStream out(buffer,IO_WriteOnly);
  int msgid=KGameMessage::IdProcessQuery;
  out << (int)1;
  proc.sendSystemMessage(out,msgid,0);
  */
}

void TDEComputer::slotTurn(TQDataStream &in,bool turn)
{
  TQByteArray buffer;
  TQDataStream out(buffer,IO_WriteOnly);
  fprintf(stderr,"----------------->\nTDEComputer::slotTurn\nturn:%d\n",turn);
  if (turn)
  {
    // Create a move
    long value=think(in,out,false);
    int id=KGameMessage::IdPlayerInput;
    proc.sendSystemMessage(out,id,0);
    sendValue(value);
  }
}

void TDEComputer::sendValue(long value)
{
  TQ_INT8 cid=1; // notifies our KGameIO that this is a value message
  int id=KGameMessage::IdProcessQuery;
  TQByteArray buffer;
  TQDataStream out(buffer,IO_WriteOnly);
  out << cid << value;
  proc.sendSystemMessage(out,id,0);
}

long TDEComputer::think(TQDataStream &in,TQDataStream &out,bool hint)
{
  TQ_INT32 pl;
  TQ_INT32 move;
  TQ_INT32 tmp;
  in >> pl ;
  in >> tmp;
  aktzug=tmp;
  in >> tmp;
  // We need all the +1 because the main programm has different defines
  // for the colours. And chaning it here seems not to work....
  amZug=(Farbe)(tmp+1);
  in >> tmp;
  beginner=(Farbe)(tmp+1);
  in >> tmp;
  second=(Farbe)(tmp+1);
  in >> tmp;
  mymaxreklev=tmp;
  fprintf(stderr,"think: pl=%d, aktzug=%d amzug=%d begin=%d second=%d level=%d\n",
                 pl,aktzug,amZug,beginner,second,mymaxreklev);

  InitField();

  // Field as 42 TQ_INT8's
  int i,j;
  for (i=0;i<=SIZE_Y;i++)
  {
    for (j=0;j<=SIZE_X;j++)
    {
      TQ_INT8 col;
      in >> col;
      Farbe colour;
      if (col<2) colour=(Farbe)(col+1);
      else colour=(Farbe)col;
      DoMove(j,colour,feldmatrix,anzahlmatrix);
    }
  }
  
  for (i=0;i<=SIZE_Y;i++)
  {
    char tstr[1024];
    tstr[0]=0;
    for (j=0;j<=SIZE_X;j++)
    {
      sprintf(tstr+strlen(tstr),"%02d ", feldmatrix[i][j]);
    }
    fprintf(stderr,"%s\n",tstr);
  }

  in >> tmp;
  fprintf(stderr,"CHECKSUM=%ld should be 421256\n",(long)tmp);

  time_t timea,timee;
  timea=time(0);

  int mymove;
  mymove= GetCompMove();

  fprintf(stderr,"Computermove to %d value=%ld\n",mymove,aktwert);

  timee=time(0);
  // Sleep a minimum amount to slow down moves
  if (timee-timea < MIN_TIME) sleep((MIN_TIME-(timee-timea)));


  move=mymove;
  if (hint)
  {
    out << pl << move;
  }
  else
  {
    out << pl << move;
  }
  return aktwert;
}

void TDEComputer::slotCommand(TQDataStream &in,int msgid,int receiver,int sender)
{
  fprintf(stderr,"----------------->\nTDEComputer::slotCommand\nMsgid:%d\n",msgid);
  TQByteArray buffer;
  TQDataStream out(buffer,IO_WriteOnly);
  switch(msgid)
  {
    case 2:  // hint
    {
      TQ_INT8 cid=2;
      TQ_INT32 pl=0;
      TQ_INT32 move=3;
      out << cid;
      long value=think(in,out,true);
      out << value;
      int id=KGameMessage::IdProcessQuery;
      proc.sendSystemMessage(out,id,0);
    }
    break;
    default: 
      fprintf(stderr,"TDEComputer:: unknown command Msgid:%d\n",msgid);
  }
}

/**
 * Computer Routinen
 */
int TDEComputer::GetCompMove()
{
  int cmove;
  long cmax,wert;
  int x;
  FARBE lfeld[SIZE_Y_ALL+1][SIZE_X+1];
  char lanzahl[SIZE_Y_ALL+1];
  Farbe farbe;


  farbe=amZug;
  cmove=-1; /* Kein Zug */
  cmax=LOWERT;
  for (x=0;x<=SIZE_X;x++)
  {
    if (anzahlmatrix[6+x]>=MAXANZAHL) continue;
   memcpy(lanzahl,anzahlmatrix,sizeof(lanzahl));
   memcpy(lfeld,feldmatrix,sizeof(lfeld));

   DoMove(x,farbe,lfeld,lanzahl);
   wert=Wertung(farbe,lfeld,lanzahl,START_REK,aktzug+1);
   
   if (wert>=cmax)
   {
    cmax=wert;
    cmove=x;
    if (cmax>=SIEG_WERT) break;
   }
  }/*next x*/
  aktwert=cmax;
  amZug=farbe; // Wertung changes amZug!
return cmove;
}

long TDEComputer::Wertung(Farbe farbe,FARBE feld[][SIZE_X+1],char anzahl[],int reklev,int zug)
{
  static long gaus[]={10,50,300,500,300,50,10};
  FARBE lfeld[SIZE_Y_ALL+1][SIZE_X+1];
  char lanzahl[SIZE_Y_ALL+1];
  long max,wert;
  int x;
  Farbe winner;

   winner=GameOver(feld,anzahl);
   if (winner!=Niemand)
   {
		if (winner==farbe) return(SIEG_WERT);
        else return(-SIEG_WERT);
   }
   if (zug>=MAXZUG) return(0); /* Remis */
   if (reklev>=mymaxreklev)  return Bewertung(farbe,feld);


   farbe=SwitchPlayer(farbe);
   max=LOWERT;
   for (x=0;x<=SIZE_X;x++)
   {
	  if (anzahl[6+x]>=MAXANZAHL) continue;
	  memcpy(lfeld,feld,sizeof(lfeld));
    memcpy(lanzahl,anzahl,sizeof(lanzahl));
	  DoMove(x,farbe,lfeld,lanzahl);
	  wert=Wertung(farbe,lfeld,lanzahl,reklev+1,zug+1)+gaus[x];
	  if (wert>=max)
	  {
		max=wert;
		if (max>=SIEG_WERT) break;
	  }
    }/*next x*/
    return(-max);
}/*end wertung*/

long TDEComputer::Bewertung(Farbe farbe,FARBE feld[][SIZE_X+1])
{
/* Abstand:              0    1    2    3    4    5    */
static long myWERT[]={2200,600, 300,  75,  20,   0};
//static long myWERT[]={0,0,0,0,0,0};
/* Wieviele von Farbe:       0     1     2     3    4 */
static long steinWERT[4][5]=
{
	{     0,  500L, 40000L,200000L,SIEG_WERT}, // Leerfelder=0
	{     0,  500L,  8000L, 40000L,SIEG_WERT}, //           =1
	{     0,   00L,  4000L, 25000L,SIEG_WERT}, //			=2
	{     0,   00L,  2000L, 12500L,SIEG_WERT}, //           =3
};
  long gelb_wert,rot_wert,wert;
  int cntcol,cnt;
  Farbe color;
  FARBE field;
  int y,i,j;
	gelb_wert=random(2500);
	rot_wert=random(2500);
	for (y=0;y<=SIZE_Y_ALL;y++)
	{
		if (lenofrow[y]<WIN4) continue;
		for (i=0;i<=(lenofrow[y]-WIN4);i++)
		{
			color=Niemand;
            wert=0;
			cntcol=0;
			cnt=0;
			for (j=0;j<WIN4;j++)
			{
				field=feld[y][i+j+startofrow[y]];
				if ((Farbe)field==Rot)
				{
					if (color==Gelb) {color=Niemand;break;}
					cntcol++;
          color=Rot;
				}
				else if ((Farbe)field==Gelb)
				{
					if (color==Rot) {color=Niemand;break;}
					cntcol++;
          color=Gelb;
				}
				else
				{
					cnt+=field-FELD_OFF;
					wert+=myWERT[field-FELD_OFF];
				}
			}/*next j */
      if (cnt>3) cnt=3;
			if (color==Rot) rot_wert+=(wert+steinWERT[cnt][cntcol]);
			else if (color==Gelb) gelb_wert+=(wert+steinWERT[cnt][cntcol]);
    }/*next i*/
	}/*next y*/
	if (farbe==Rot) wert=rot_wert-gelb_wert;
	else wert=gelb_wert-rot_wert;
return(wert);
}

Farbe TDEComputer::GameOver(FARBE feld[][SIZE_X+1],char anzahl[])
{
  Farbe thiscolor,field;
  int x,y,cnt;
  for (y=0;y<=SIZE_Y_ALL;y++)
  {
	 if (anzahl[y]<WIN4) continue;
	 if ( lenofrow[y]<WIN4 ) continue;
	 cnt=0;
	 thiscolor=Niemand;
	 for (x=0;x<lenofrow[y];x++)
	 {
	   field=(Farbe)feld[y][x+startofrow[y]];
       if (field==thiscolor) cnt++;
       else {cnt=1;thiscolor=field;}
	   if ( (cnt>=WIN4)&&( (thiscolor==Gelb)||(thiscolor==Rot) ) ) return(thiscolor);
     }/*next x */
  }/*next y*/
  return(Niemand);
}

Farbe TDEComputer::SwitchPlayer(Farbe m_amZug)
{
  if (m_amZug==Niemand)
    m_amZug=amZug;
  if (m_amZug==Rot)
    amZug=Gelb;
  else if (m_amZug==Gelb)
     amZug=Rot;
  else amZug=beginner;
  return amZug;
}

void TDEComputer::DoMove(char move,Farbe farbe,FARBE feld[][SIZE_X+1],char anzahl[])
{
  int x,i,y;

  if (farbe==Tip || farbe==Niemand) return ;  // no real move
  x=move;
  y=anzahl[6+move];
  feld[y][x]=farbe;

  //if (farbe==Tip || farbe==Niemand) return ;  // no real move

  feld[6+x][y]=farbe;
  feld[13+x+y][x]=farbe;
  feld[30+x-y][x]=farbe;
  anzahl[y]++;
  anzahl[6+x]++;
  anzahl[13+x+y]++;
  anzahl[30+x-y]++;
  for (i=y+1;i<=SIZE_Y;i++)
  {
     feld[i][x]--;
     feld[6+x][i]--;
     feld[13+x+i][x]--;
     feld[30+x-i][x]--;
  }
}

void TDEComputer::InitField() {
  int x,y;
  for (y=0;y<=SIZE_Y_ALL;y++)
    anzahlmatrix[y]=0;
  for (y=0;y<=SIZE_Y;y++)
  {
    for (x=0;x<=SIZE_X;x++)
    {
      feldmatrix[y][x]=(FARBE)(y+FELD_OFF);
      feldmatrix[6+x][y]=(FARBE)(y+FELD_OFF);
      feldmatrix[13+x+y][x]=(FARBE)(y+FELD_OFF);
      feldmatrix[30+x-y][x]=(FARBE)(y+FELD_OFF);
    }
  }/* next y */
}

long TDEComputer::random(long max)
{
  long wert;
  wert=proc.random()->getLong(max);
  return wert;
}

// Main startup
int main(int argc ,char * argv[])
{
  // This is the computer player...it should do the calulation
  // It doesn't do much here
  fprintf(stderr,"Vor TDEComputer\n");
  fflush(stderr);
  TDEComputer comp;
  fprintf(stderr,"Vor exec\n");
  // And start the event loop
  comp.proc.exec(argc,argv);
  fprintf(stderr,"nach exec\n");
  return 1;
}                                                                                                                             
#include "twin4proc.moc"