-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathMoveAlgorithm.java
executable file
·793 lines (712 loc) · 21.9 KB
/
MoveAlgorithm.java
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Java Chess *
* Copyright (C) 2005 Arvydas Bancewicz *
* *
* 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. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with Java Chess; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
* * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* Created on Mar 16, 2005
*/
package chess.algorithms;
import java.io.Serializable;
import java.util.*;
import chess.core.*;
/**
*
* MoveAlgorithm is a super class for search algorithms. It consists of all the
* required utilities defining a playable chess game, such as: move definitions,
* composing moves from move definitions, applying board position circumstances
* to the move definition (eg. a piece cannot move to put it's king in check),
* composing lists of possible moves, and board evaluations based on piece and
* position costs.
*
* @author Arvydas Bancewicz
*
*/
public class MoveAlgorithm implements Constants,Serializable{
// The chess board to be worked o
protected Board board;
protected final int INFINITY = 100000;
private boolean stopped = false;
protected Random rnd = new Random();
// The number of levels in the tree to be searched.
protected int dd = 3; // Depth
protected int stepcounter; // Total node count
protected Move mm; // Current move
protected int val = 0; // Current estimation value
private int estnow; // base estimation
private int est_cost; // Cost estimation value
private int est_attackCost; // Attack cost estimation value
// Speed test
private Date stamp1; // Time stamp start
private Date stamp2; // Time stamp end
/**
* Create a new MoveAlgorithm object.
*/
public MoveAlgorithm() {
setBoard(new Board());
}
/**
* Create a new MoveAlgorithm object with a game to use.
* @param game - the game to work with
*/
public MoveAlgorithm (ChessGame game){
setGame(game);
}
/**
* Create a new MoveAlgorithm object with a board to use
* @param board - the board to work with
*/
public MoveAlgorithm (Board board) {
setBoard(board);
}
// Set the default values for the variables
private void setDefaults() {
dd = 3; // Depth
stepcounter = 0; // Total node count
mm = null; // Current move
val = 0; // Current estimation value
estnow = 0; // base estimation
est_cost = 0; // Cost estimation value
est_attackCost = 0; // Attack cost estimation value
}
/**
* Get the board being used by this MoveAlgorithm.
* @return the Board
*/
public Board getBoard() {
return board;
}
/**
* Set the depth to use by this MoveAlgorithm.
* @param depth
*/
public void setDepth(int depth) {
if (depth > 0)
dd = depth;
}
/**
* Get the depth used by this MoveAlgorithm.
* @return
*/
public int getDepth() {
return dd;
}
/**
* Set the game for this MoveAlgorithm.
* @param game
*/
public void setGame(ChessGame game) {
this.board = copyBoard(game.board);
}
/**
* Set the board for this MoveAlgorithm.
* @param board
*/
public void setBoard(Board board) {
this.board = copyBoard(board);
}
// Has the searching stopped
protected boolean isStopped() {
return stopped;
}
/**
* Stop the move searching if so
*/
public void stop() {
stopped = true;
}
/**
* Get a reply
* @param white - White player ?
* @return the move
*/
public Move reply(boolean white) {
//setDefaults();
stopped = false;
estnow = estimateBase();
testSpeed(); // Start time stamp
stepcounter = 0;
// Get the reply from the subclass
Move move = getReply(white);
if (move == null) {
System.out.println("ALLLLL");
Vector v = successors(board.turn);
if(v!=null) {
System.out.println("SOME");
int siz = v.size();
move = (Move)v.remove(0);
mm = move;
}
}
testSpeed(); // End time stamp
board.movePiece(move);
return move;
}
/** Subclasses must implement this method */
protected Move getReply(boolean white) { return mm; }
private Vector getPawnMoves(Coord c){
Vector v = new Vector();
if(board.b[c.x][c.y].white){
if(board.b[c.x][c.y+1]==null){ //empty block
v.add(new Move(c.x,c.y,c.x,c.y+1));
if (c.y==1) //beginning jump
v.add(new Move(c.x,c.y,c.x,c.y+2));
}
}else{
if(board.b[c.x][c.y-1]==null){
v.add(new Move(c.x,c.y,c.x,c.y-1));
if (c.y==6)
v.add(new Move(c.x,c.y,c.x,c.y-2));
}
}
return (v.size()==0)?null:v;
}
private Vector getPawnAttacks(Coord c) {
Vector v = new Vector();
if(board.b[c.x][c.y].white) {
if(c.x+1<=7)
v.add(new Move(c.x,c.y,c.x+1,c.y+1));
if(c.x-1>=0)
v.add(new Move(c.x,c.y,c.x-1,c.y+1));
} else {
if(c.x+1<=7)
v.add(new Move(c.x,c.y,c.x+1,c.y-1));
if(c.x-1>=0)
v.add(new Move(c.x,c.y,c.x-1,c.y-1));
}
return (v.size()==0)?null:v;
}
private Vector getKnightMoves(Coord c) {
return getKnightAttacks(c);
}
private Vector getKnightAttacks(Coord c){
Vector v = new Vector();
if(c.x+1<=7) {
if(c.y+2<=7)
v.add(new Move(c.x,c.y,c.x+1,c.y+2));
if(c.y-2>=0)
v.add(new Move(c.x,c.y,c.x+1,c.y-2));
if(c.x+2<=7) {
if(c.y+1<=7)
v.add(new Move(c.x,c.y,c.x+2,c.y+1));
if(c.y-1>=0)
v.add(new Move(c.x,c.y,c.x+2,c.y-1));
}
}
if(c.x-1>=0) {
if(c.y+2<=7)
v.add(new Move(c.x,c.y,c.x-1,c.y+2));
if(c.y-2>=0)
v.add(new Move(c.x,c.y,c.x-1,c.y-2));
if(c.x-2>=0) {
if(c.y+1<=7)
v.add(new Move(c.x,c.y,c.x-2,c.y+1));
if(c.y-1>=0)
v.add(new Move(c.x,c.y,c.x-2,c.y-1));
}
}
return (v.size()==0)?null:v;
}
private Vector getBishopMoves(Coord c) {
return getBishopAttacks(c);
}
private Vector getBishopAttacks(Coord c) {
Vector v = new Vector();
byte i; // Increment
// Top Right
i = 1;
while(c.x+i<=7 && c.y+i<=7) {
v.add(new Move(c.x,c.y,c.x+i,c.y+i));
if(board.b[c.x+i][c.y+i]!=null)
break;
i++;
}
// Bottom Right
i = 1;
while(c.x+i<=7 && c.y-i>=0) {
v.add(new Move(c.x,c.y,c.x+i,c.y-i));
if(board.b[c.x+i][c.y-i]!=null)
break;
i++;
}
// Bottom Left
i = 1;
while(c.x-i>=0 && c.y-i>=0) {
v.add(new Move(c.x,c.y,c.x-i,c.y-i));
if(board.b[c.x-i][c.y-i]!=null)
break;
i++;
}
// Top Left
i = 1;
while(c.x-i>=0 && c.y+i<=7) {
v.add(new Move(c.x,c.y,c.x-i,c.y+i));
if(board.b[c.x-i][c.y+i]!=null)
break;
i++;
}
return (v.size()==0)?null:v;
}
private Vector getRookMoves(Coord c) {
return getRookAttacks(c);
}
private Vector getRookAttacks(Coord c) {
Vector v = new Vector();
byte i;
// Top
i = 1;
while(c.y+i<=7) {
v.add(new Move(c.x,c.y,c.x,c.y+i));
if(board.b[c.x][c.y+i]!=null)
break;
i++;
}
// Left
i = 1;
while(c.x-i>=0) {
v.add(new Move(c.x,c.y,c.x-i,c.y));
if(board.b[c.x-i][c.y]!=null)
break;
i++;
}
// Bottom
i = 1;
while(c.y-i>=0) {
v.add(new Move(c.x,c.y,c.x,c.y-i));
if(board.b[c.x][c.y-i]!=null)
break;
i++;
}
// Right
i = 1;
while(c.x+i<=7) {
v.add(new Move(c.x,c.y,c.x+i,c.y));
if(board.b[c.x+i][c.y]!=null)
break;
i++;
}
return (v.size()==0)?null:v;
}
private Vector getQueenMoves(Coord c) {
// Queen acts as both bishop and rook
return sumVectors(getBishopMoves(c), getRookMoves(c));
}
private Vector getQueenAttacks(Coord c) {
// All Queen moves are attacks
return getQueenMoves(c);
}
private Vector getBasicKingMoves(Coord c) {
Vector v = new Vector();
if(c.x+1<=7) {
v.add(new Move(c.x,c.y,c.x+1,c.y));
if(c.y+1<=7)
v.add(new Move(c.x,c.y,c.x+1,c.y+1));
if(c.y-1>=0)
v.add(new Move(c.x,c.y,c.x+1,c.y-1));
}
if(c.x-1>=0) {
v.add(new Move(c.x,c.y,c.x-1,c.y));
if(c.y+1<=7)
v.add(new Move(c.x,c.y,c.x-1,c.y+1));
if(c.y-1>=0)
v.add(new Move(c.x,c.y,c.x-1,c.y-1));
}
if(c.y+1<=7)
v.add(new Move(c.x,c.y,c.x,c.y+1));
if(c.y-1>=0)
v.add(new Move(c.x,c.y,c.x,c.y-1));
return (v.size()==0)?null:v;
}
private Vector getKingMoves(Coord c) {
Vector v = getBasicKingMoves(c);
if (v==null)
v = new Vector();
// Castling
if (board.b[c.x][c.y].white) {
if (c.x==4 && c.y==0 && !isAttacked(new Coord(4,0),false)) {
// Short
if (board.b[5][0]==null && !isAttacked(new Coord(5,0),false) &&
board.b[6][0]==null && !isAttacked(new Coord(6,0),false) &&
board.b[7][0]!=null && board.b[7][0].type==TYPE_ROOK && board.b[7][0].white)
v.add(new Move(4,0,6,0));
// Long
if (board.b[3][0]==null && !isAttacked(new Coord(3,0),false) &&
board.b[2][0]==null && !isAttacked(new Coord(2,0),false) &&
board.b[1][0]==null &&
board.b[0][0]!=null && board.b[0][0].type==TYPE_ROOK && board.b[0][0].white)
v.add(new Move(4,0,2,0));
}
} else {
if (c.x==4 && c.y==7 && !isAttacked(new Coord(4,7),true)) {
// Short
if (board.b[5][7]==null && !isAttacked(new Coord(5,7),true) &&
board.b[6][7]==null && !isAttacked(new Coord(6,7),true) &&
board.b[7][7]!=null && board.b[7][7].type==TYPE_ROOK && !board.b[7][7].white)
v.add(new Move(4,7,6,7));
// Long
if (board.b[3][7]==null && !isAttacked(new Coord(3,7),true) &&
board.b[2][7]==null && !isAttacked(new Coord(2,7),true) &&
board.b[1][7]==null &&
board.b[0][7]!=null && board.b[0][7].type==TYPE_ROOK && !board.b[0][7].white)
v.add(new Move(4,7,2,7));
}
}
return (v.size()==0)?null:v;
}
private Vector getKingAttacks(Coord c) {
return getBasicKingMoves(c);
}
private Vector sumVectors(Vector a, Vector b) {
if(a==null && b==null)
return null;
Vector v=new Vector();
if(a!=null)
for(int i=0;i<a.size();i++)
v.add(a.elementAt(i));
if(b!=null)
for(int i=0;i<b.size();i++)
v.add(b.elementAt(i));
return v;
}
private Vector getMoves(Coord c) {
Piece p = board.b[c.x][c.y];
if(p==null)
return null;
switch(p.type) {
case TYPE_PAWN:
return getPawnMoves(c);
case TYPE_KNIGHT:
return getKnightMoves(c);
case TYPE_BISHOP:
return getBishopMoves(c);
case TYPE_ROOK:
return getRookMoves(c);
case TYPE_QUEEN:
return getQueenMoves(c);
case TYPE_KING:
return getKingMoves(c);
}
return null;
}
public Vector getAttacks(Coord c) {
Piece p = board.b[c.x][c.y];
if(p==null)
return null;
switch(p.type) {
case TYPE_PAWN:
return getPawnAttacks(c);
case TYPE_KNIGHT:
return getKnightAttacks(c);
case TYPE_BISHOP:
return getBishopAttacks(c);
case TYPE_ROOK:
return getRookAttacks(c);
case TYPE_QUEEN:
return getQueenAttacks(c);
case TYPE_KING:
return getKingAttacks(c);
}
return null;
}
private Vector getRealMoves(Coord c){
Piece p = board.b[c.x][c.y];
Vector v = getMoves(c);
if(v!=null) {
for(int k=0;k<v.size();k++) {
Move m = (Move)v.elementAt(k);
if(board.b[m.x2][m.y2]!=null) {
v.remove(k);
k--;
} else {
m.perform(board);
//m.perform(board, game);
if(isAttacked(p.white?board.whiteKing:board.blackKing,!p.white)) {
v.remove(k);
k--;
}
m.undo(board);
//m.undo(board);
}
}
if (v.size()==0)
v = null;
}
if (v==null)return null;
return v.size()==0?null:v;
}
private Vector getRealAttacks(Coord c){
Piece p = board.b[c.x][c.y];
Vector v = getAttacks(c);
if(v!=null) {
for(int k=0;k<v.size();k++) {
Move m = (Move)v.elementAt(k);
if(board.b[m.x2][m.y2]==null || board.b[m.x2][m.y2].white==p.white) {
v.remove(k);
k--;
} else {
m.perform(board);
//m.perform(board, game);
if (isAttacked(p.white?board.whiteKing:board.blackKing,!p.white)) {
v.remove(k);
k--;
}
m.undo(board);
//m.undo(board);
}
}
if (v.size()==0)
v = null;
}
return v;
}
/**
* Get a vector containing all the legal moves for the piece in the
* coordinate location
* @param c - the coordinate location
* @return the vector with legal moves
*/
public Vector getRealAll(Coord c) {
Piece p = board.b[c.x][c.y];
if (p==null)
return null;
Vector v = null;
switch(p.type) {
case TYPE_PAWN:
return sumVectors(getRealMoves(c),getRealAttacks(c));
case TYPE_KNIGHT:
v = getKnightMoves(c);
break;
case TYPE_BISHOP:
v = getBishopAttacks(c);
break;
case TYPE_ROOK:
v = getRookAttacks(c);
break;
case TYPE_QUEEN:
v = sumVectors(getBishopAttacks(c),getRookAttacks(c));
break;
case TYPE_KING:
v = getKingMoves(c);
break;
}
if(v!=null) {
for(int k=0;k<v.size();k++) {
Move mmm = (Move)v.elementAt(k);
// Make sure piece can not take one of it's own
if(board.b[mmm.x2][mmm.y2]!=null && board.b[mmm.x2][mmm.y2].white==p.white) {
v.remove(k);
k--;
} else {
mmm.perform(board);
//mmm.perform(board, game);
if (isAttacked(p.white?board.whiteKing:board.blackKing,!p.white)) {
v.remove(k);
k--;
}
mmm.undo(board);
//mmm.undo(board);
}
}
if(v.size()==0)
v = null;
}
return v;
}
/**
* Is this coordinate attacked by !white
* @param c - the coordinate
* @param white - is the piece white ?
* @return coordinate attacked ?
*/
public boolean isAttacked(Coord c, boolean white) {
for (byte a=0;a<8;a++)
for (byte r=0;r<8;r++)
if(board.b[a][r]!=null && board.b[a][r].white==white) {
Vector v = getAttacks(new Coord(a,r));
if (v!=null) {
for (int i=0;i<v.size();i++) {
Move m = (Move)v.elementAt(i);
if (m.x2==c.x && m.y2==c.y)
return true;
}
}
}
return false;
}
protected Vector successors(boolean white) {
Vector v = null;
for(byte i=0;i<8;i++)
for(byte j=0;j<8;j++)
if(board.b[i][j]!=null && board.b[i][j].white==white)
v = sumVectors(v, getRealAll(new Coord(i,j)));
return v;
}
private int getCost() {
int out = 0;
for(int c=0;c<8;c++)
for(int r=0;r<8;r++)
if(board.b[c][r]!=null)
out+=board.b[c][r].getCost();
return out;
}
private int getAttackCost() {
int out = 0;
for(byte c=0;c<8;c++)
for(byte r=0;r<8;r++)
if(board.b[c][r]!=null) {
Vector v = getRealAttacks(new Coord(c,r));
if(v!=null)
for(int k=0;k<v.size();k++) {
Move m = (Move)v.elementAt(k);
out += board.b[m.x2][m.y2].getCost();
}
}
return -out;
}
/**
* Is their are checkmate for either player
* @return checkmate ?
*/
public boolean checkMate() {
return isAttacked(board.blackKing,true) && !replyForMate(false) ||
isAttacked(board.whiteKing,false) && !replyForMate(true);
}
/**
* Is the move legal for this board
* @param m - the move
* @return legal ?
*/
public boolean canMove(Move m) {
if(board.b[m.x1][m.y1]==null)
return false;
Vector v = getRealAll(new Coord(m.x1,m.y1));
if(v!=null) {
for(int k=0;k<v.size();k++) {
Move mm = (Move)v.elementAt(k);
if(m.x2==mm.x2 && m.y2==mm.y2)
return true;
}
}
return false;
}
/**
* Can the white ? player apply for mate
* @param white - white ?
* @return can it reply ?
*/
public boolean replyForMate(boolean white) {
for(byte c=0;c<8;c++)
for(byte r=0;r<8;r++)
if(board.b[c][r]!=null && board.b[c][r].white==white)
if(getRealAll(new Coord(c,r))!=null)
return true;
return false;
}
protected int estimate() {
stepcounter++;
int dc = getCost()-est_cost;
int dac = getAttackCost()-est_attackCost;
//System.out.println("getCost => " + getCost());
//System.out.println("getAttackCost => " + getAttackCost());
return dc*10+dac;
}
private int estimateBase() {
//cc.progress.setValue(0);
mm = null;
est_cost = 0;
est_attackCost = 0;
int out = estimate();
est_cost = getCost();
est_attackCost = getAttackCost();
return out;
}
protected Vector randomize(Piece[][] board, Vector v) {
int s=v.size();
int b=0;
for(int i=0;i<s;i++) {
Move m = (Move)v.elementAt(i);
if(board[m.x2][m.y2]!=null) {
v.remove(i);
v.insertElementAt(m, 0);
b++;
}
}
for(int i=b;i<s-1;i++) {
Object o = v.elementAt(i);
int j = rnd.nextInt(s-i-1)+i+1;
v.setElementAt(v.elementAt(j), i);
v.setElementAt(o, j);
}
return v;
}
private Piece[][] copyBoard (Piece[][] g){
Piece[][] newGrid = new Piece[8][8];
for(byte c = 0; c < 8; c++)
for(byte r = 0; r < 8; r++)
newGrid[c][r] = g[c][r];
return newGrid;
}
private Board copyBoard(Board board) {
Board newBoard = new Board();
newBoard.b = new Piece[8][8];
for(byte c = 0; c < 8; c++)
for(byte r = 0; r < 8; r++)
newBoard.b[c][r] = board.b[c][r];
newBoard.blackKing = board.blackKing;
newBoard.whiteKing = board.whiteKing;
return newBoard;
}
private void testSpeed() {
if (stamp1==null)
stamp1 = new Date(); // Start time
else {
stamp2 = new Date(); // End time
// Get seconds and milliseconds
long milliDiff = stamp2.getTime() - stamp1.getTime();
long seconds = (long)(milliDiff/1000);
long millisec = (long)(milliDiff-(seconds*1000));
stamp1 = stamp2 = null;
// Output the move and the time for it
System.out.println("reply => " + mm.toString());
System.out.println("time = seconds: " + seconds +
" milliseconds: " + millisec);
// Draw a table with the obtained information
String col1 = "depth";
String col2 = "# of nodes";
String col3 = "evaluation";
// Table specifications
int spacing = 5;
int col1Width = col1.length()+spacing;
int col2Width = col2.length()+spacing;
int col3Width = col3.length()+spacing;
// Empty string to fill in any gaps
String empty = " ";
// Table header (right justified)
System.out.println(
empty.substring(0,col1Width-col1.length())+col1+
empty.substring(0,col2Width-col2.length())+col2+
empty.substring(0,col3Width-col3.length())+col3
);
// Table values (right justified)
System.out.println(
empty.substring(0,col1Width-(dd+"").length()) +dd+
empty.substring(0,col2Width-(stepcounter+"").length())+stepcounter+
empty.substring(0,col3Width-(val+"").length()) +val
);
}
}
}