Source Code Cross Referenced for Board.java in  » 6.0-JDK-Modules » java-3d » org » jdesktop » j3d » examples » four_by_four » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » java 3d » org.jdesktop.j3d.examples.four_by_four 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * $RCSfile: Board.java,v $
0003:         *
0004:         * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
0005:         *
0006:         * Redistribution and use in source and binary forms, with or without
0007:         * modification, are permitted provided that the following conditions
0008:         * are met:
0009:         *
0010:         * - Redistribution of source code must retain the above copyright
0011:         *   notice, this list of conditions and the following disclaimer.
0012:         *
0013:         * - Redistribution in binary form must reproduce the above copyright
0014:         *   notice, this list of conditions and the following disclaimer in
0015:         *   the documentation and/or other materials provided with the
0016:         *   distribution.
0017:         *
0018:         * Neither the name of Sun Microsystems, Inc. or the names of
0019:         * contributors may be used to endorse or promote products derived
0020:         * from this software without specific prior written permission.
0021:         *
0022:         * This software is provided "AS IS," without a warranty of any
0023:         * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
0024:         * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
0025:         * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
0026:         * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
0027:         * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
0028:         * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
0029:         * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
0030:         * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
0031:         * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
0032:         * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
0033:         * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
0034:         * POSSIBILITY OF SUCH DAMAGES.
0035:         *
0036:         * You acknowledge that this software is not designed, licensed or
0037:         * intended for use in the design, construction, operation or
0038:         * maintenance of any nuclear facility.
0039:         *
0040:         * $Revision: 1.2 $
0041:         * $Date: 2007/02/09 17:21:37 $
0042:         * $State: Exp $
0043:         */
0044:
0045:        package org.jdesktop.j3d.examples.four_by_four;
0046:
0047:        import java.awt.*;
0048:
0049:        /**
0050:         *  Class:       Board
0051:         *
0052:         *  Description: Handles all logic with respect to play. Also renders
0053:         *               the 2D window.
0054:         *
0055:         *  Version:     1.1
0056:         *
0057:         */
0058:        class Board {
0059:
0060:            final static int UNOCCUPIED = 0;
0061:            final static int HUMAN = 1;
0062:            final static int MACHINE = 2;
0063:            final static int END = 3;
0064:
0065:            private int[] moves;
0066:            private int[] occupied;
0067:            private int[][] combinations;
0068:            private int[][] outside_four;
0069:            private int[][] inside_four;
0070:            private int[][] faces;
0071:            private int[][] pos_to_comb;
0072:            private int[][] best_picks;
0073:            private int num_points;
0074:            private int num_balls;
0075:            private int num_polygons;
0076:            private int num_pt_indexes;
0077:            private int num_normal_indexes;
0078:            private int pt_start;
0079:            private int color_index;
0080:            private int width;
0081:            private int height;
0082:            private int center_x;
0083:            private int center_y;
0084:            private int player;
0085:            private int skill_level;
0086:            private int outside_four_index;
0087:            private int inside_four_index;
0088:            private int face_index;
0089:            private int nmoves;
0090:            private int current_face;
0091:            private int min = 1000;
0092:            private int max = 0;
0093:            private long[] sort_array;
0094:            private long time;
0095:            private long beg_time;
0096:            private long end_time;
0097:            private Color[] color_ramp;
0098:            private Color background;
0099:            private Color label_color;
0100:            private Color red;
0101:            private Color blue;
0102:            private Color white;
0103:            private Color gray;
0104:            private Color yellow;
0105:            private double max_dist;
0106:            private FourByFour panel;
0107:            private boolean debug;
0108:            private boolean outside_four_flag;
0109:            private boolean inside_four_flag;
0110:            private boolean face_flag;
0111:            private boolean label_flag;
0112:            private boolean block_chair_flag;
0113:            private boolean undoFlag;
0114:            private boolean[] highlight;
0115:            private int block_chair_next_move;
0116:            private int block_chair_face;
0117:            private Positions positions;
0118:            private Canvas2D canvas;
0119:
0120:            Board(FourByFour panel, Positions positions, int width, int height) {
0121:
0122:                // Set the debug state.
0123:                debug = false;
0124:
0125:                // Store arguments
0126:                this .width = width;
0127:                this .height = height;
0128:                this .panel = panel;
0129:                this .positions = positions;
0130:
0131:                // Initialize flags
0132:                label_flag = false;
0133:                outside_four_flag = false;
0134:                inside_four_flag = false;
0135:                block_chair_flag = false;
0136:                undoFlag = false;
0137:
0138:                // Total number of board positions.
0139:                num_points = 64;
0140:
0141:                // Allocate the logic arrays.
0142:                moves = new int[64];
0143:                occupied = new int[64];
0144:                combinations = new int[76][7];
0145:                outside_four = new int[18][6];
0146:                inside_four = new int[18][6];
0147:                faces = new int[18][18];
0148:                pos_to_comb = new int[64][8];
0149:                best_picks = new int[64][8];
0150:                highlight = new boolean[18];
0151:
0152:                // Initialize the logic arrays.
0153:                init_combinations();
0154:                init_faces();
0155:                init_outside_four();
0156:                init_inside_four();
0157:
0158:                // Set the player with the first move. 
0159:                player = HUMAN;
0160:
0161:                // Set the default skill level.
0162:                skill_level = 4;
0163:
0164:                // Initialize the number of moves.
0165:                nmoves = 0;
0166:
0167:                // Define colors
0168:                background = new Color(13, 13, 51);
0169:                red = new Color(230, 26, 51);
0170:                blue = new Color(51, 51, 230);
0171:                white = new Color(255, 255, 255);
0172:                gray = new Color(240, 240, 240);
0173:                yellow = new Color(240, 240, 0);
0174:
0175:                // Record the start time
0176:                beg_time = System.currentTimeMillis();
0177:            }
0178:
0179:            public void setCanvas(Canvas2D canvas) {
0180:                this .canvas = canvas;
0181:            }
0182:
0183:            public void init_combinations() {
0184:
0185:                // The combination array contains all possible winning combinations.
0186:                //
0187:                // Each combination has the following format:
0188:                //
0189:                // combinations[x][0] =  status: 0      = no player has selected positons in this row
0190:                //                              -1      = both players have men in this row
0191:                //                               1 to 4 = number of positions occupied by player
0192:                //
0193:                // combinations[x][1] =  player who owns this row (valid only if status = 1-4)
0194:                // combinations[x][2] =  postion that define the row 
0195:                // combinations[x][3] =  postion that define the row 
0196:                // combinations[x][4] =  postion that define the row 
0197:                // combinations[x][5] =  postion that define the row 
0198:
0199:                // Horizontal, Z
0200:
0201:                combinations[0][0] = 0;
0202:                combinations[1][0] = 0;
0203:                combinations[2][0] = 0;
0204:                combinations[3][0] = 0;
0205:                combinations[0][1] = 0;
0206:                combinations[1][1] = 0;
0207:                combinations[2][1] = 0;
0208:                combinations[3][1] = 0;
0209:                combinations[0][2] = 0;
0210:                combinations[1][2] = 4;
0211:                combinations[2][2] = 8;
0212:                combinations[3][2] = 12;
0213:                combinations[0][3] = 1;
0214:                combinations[1][3] = 5;
0215:                combinations[2][3] = 9;
0216:                combinations[3][3] = 13;
0217:                combinations[0][4] = 2;
0218:                combinations[1][4] = 6;
0219:                combinations[2][4] = 10;
0220:                combinations[3][4] = 14;
0221:                combinations[0][5] = 3;
0222:                combinations[1][5] = 7;
0223:                combinations[2][5] = 11;
0224:                combinations[3][5] = 15;
0225:
0226:                combinations[4][0] = 0;
0227:                combinations[5][0] = 0;
0228:                combinations[6][0] = 0;
0229:                combinations[7][0] = 0;
0230:                combinations[4][1] = 0;
0231:                combinations[5][1] = 0;
0232:                combinations[6][1] = 0;
0233:                combinations[7][1] = 0;
0234:                combinations[4][2] = 16;
0235:                combinations[5][2] = 20;
0236:                combinations[6][2] = 24;
0237:                combinations[7][2] = 28;
0238:                combinations[4][3] = 17;
0239:                combinations[5][3] = 21;
0240:                combinations[6][3] = 25;
0241:                combinations[7][3] = 29;
0242:                combinations[4][4] = 18;
0243:                combinations[5][4] = 22;
0244:                combinations[6][4] = 26;
0245:                combinations[7][4] = 30;
0246:                combinations[4][5] = 19;
0247:                combinations[5][5] = 23;
0248:                combinations[6][5] = 27;
0249:                combinations[7][5] = 31;
0250:
0251:                combinations[8][0] = 0;
0252:                combinations[9][0] = 0;
0253:                combinations[10][0] = 0;
0254:                combinations[11][0] = 0;
0255:                combinations[8][1] = 0;
0256:                combinations[9][1] = 0;
0257:                combinations[10][1] = 0;
0258:                combinations[11][1] = 0;
0259:                combinations[8][2] = 32;
0260:                combinations[9][2] = 36;
0261:                combinations[10][2] = 40;
0262:                combinations[11][2] = 44;
0263:                combinations[8][3] = 33;
0264:                combinations[9][3] = 37;
0265:                combinations[10][3] = 41;
0266:                combinations[11][3] = 45;
0267:                combinations[8][4] = 34;
0268:                combinations[9][4] = 38;
0269:                combinations[10][4] = 42;
0270:                combinations[11][4] = 46;
0271:                combinations[8][5] = 35;
0272:                combinations[9][5] = 39;
0273:                combinations[10][5] = 43;
0274:                combinations[11][5] = 47;
0275:
0276:                combinations[12][0] = 0;
0277:                combinations[13][0] = 0;
0278:                combinations[14][0] = 0;
0279:                combinations[15][0] = 0;
0280:                combinations[12][1] = 0;
0281:                combinations[13][1] = 0;
0282:                combinations[14][1] = 0;
0283:                combinations[15][1] = 0;
0284:                combinations[12][2] = 48;
0285:                combinations[13][2] = 52;
0286:                combinations[14][2] = 56;
0287:                combinations[15][2] = 60;
0288:                combinations[12][3] = 49;
0289:                combinations[13][3] = 53;
0290:                combinations[14][3] = 57;
0291:                combinations[15][3] = 61;
0292:                combinations[12][4] = 50;
0293:                combinations[13][4] = 54;
0294:                combinations[14][4] = 58;
0295:                combinations[15][4] = 62;
0296:                combinations[12][5] = 51;
0297:                combinations[13][5] = 55;
0298:                combinations[14][5] = 59;
0299:                combinations[15][5] = 63;
0300:
0301:                // Vertical, Z
0302:
0303:                combinations[16][0] = 0;
0304:                combinations[17][0] = 0;
0305:                combinations[18][0] = 0;
0306:                combinations[19][0] = 0;
0307:                combinations[16][1] = 0;
0308:                combinations[17][1] = 0;
0309:                combinations[18][1] = 0;
0310:                combinations[19][1] = 0;
0311:                combinations[16][2] = 0;
0312:                combinations[17][2] = 1;
0313:                combinations[18][2] = 2;
0314:                combinations[19][2] = 3;
0315:                combinations[16][3] = 4;
0316:                combinations[17][3] = 5;
0317:                combinations[18][3] = 6;
0318:                combinations[19][3] = 7;
0319:                combinations[16][4] = 8;
0320:                combinations[17][4] = 9;
0321:                combinations[18][4] = 10;
0322:                combinations[19][4] = 11;
0323:                combinations[16][5] = 12;
0324:                combinations[17][5] = 13;
0325:                combinations[18][5] = 14;
0326:                combinations[19][5] = 15;
0327:
0328:                combinations[20][0] = 0;
0329:                combinations[21][0] = 0;
0330:                combinations[22][0] = 0;
0331:                combinations[23][0] = 0;
0332:                combinations[20][1] = 0;
0333:                combinations[21][1] = 0;
0334:                combinations[22][1] = 0;
0335:                combinations[23][1] = 0;
0336:                combinations[20][2] = 16;
0337:                combinations[21][2] = 17;
0338:                combinations[22][2] = 18;
0339:                combinations[23][2] = 19;
0340:                combinations[20][3] = 20;
0341:                combinations[21][3] = 21;
0342:                combinations[22][3] = 22;
0343:                combinations[23][3] = 23;
0344:                combinations[20][4] = 24;
0345:                combinations[21][4] = 25;
0346:                combinations[22][4] = 26;
0347:                combinations[23][4] = 27;
0348:                combinations[20][5] = 28;
0349:                combinations[21][5] = 29;
0350:                combinations[22][5] = 30;
0351:                combinations[23][5] = 31;
0352:
0353:                combinations[24][0] = 0;
0354:                combinations[25][0] = 0;
0355:                combinations[26][0] = 0;
0356:                combinations[27][0] = 0;
0357:                combinations[24][1] = 0;
0358:                combinations[25][1] = 0;
0359:                combinations[26][1] = 0;
0360:                combinations[27][1] = 0;
0361:                combinations[24][2] = 32;
0362:                combinations[25][2] = 33;
0363:                combinations[26][2] = 34;
0364:                combinations[27][2] = 35;
0365:                combinations[24][3] = 36;
0366:                combinations[25][3] = 37;
0367:                combinations[26][3] = 38;
0368:                combinations[27][3] = 39;
0369:                combinations[24][4] = 40;
0370:                combinations[25][4] = 41;
0371:                combinations[26][4] = 42;
0372:                combinations[27][4] = 43;
0373:                combinations[24][5] = 44;
0374:                combinations[25][5] = 45;
0375:                combinations[26][5] = 46;
0376:                combinations[27][5] = 47;
0377:
0378:                combinations[28][0] = 0;
0379:                combinations[29][0] = 0;
0380:                combinations[30][0] = 0;
0381:                combinations[31][0] = 0;
0382:                combinations[28][1] = 0;
0383:                combinations[29][1] = 0;
0384:                combinations[30][1] = 0;
0385:                combinations[31][1] = 0;
0386:                combinations[28][2] = 48;
0387:                combinations[29][2] = 49;
0388:                combinations[30][2] = 50;
0389:                combinations[31][2] = 51;
0390:                combinations[28][3] = 52;
0391:                combinations[29][3] = 53;
0392:                combinations[30][3] = 54;
0393:                combinations[31][3] = 55;
0394:                combinations[28][4] = 56;
0395:                combinations[29][4] = 57;
0396:                combinations[30][4] = 58;
0397:                combinations[31][4] = 59;
0398:                combinations[28][5] = 60;
0399:                combinations[29][5] = 61;
0400:                combinations[30][5] = 62;
0401:                combinations[31][5] = 63;
0402:
0403:                // Diagonal, Z
0404:
0405:                combinations[32][0] = 0;
0406:                combinations[33][0] = 0;
0407:                combinations[34][0] = 0;
0408:                combinations[35][0] = 0;
0409:                combinations[32][1] = 0;
0410:                combinations[33][1] = 0;
0411:                combinations[34][1] = 0;
0412:                combinations[35][1] = 0;
0413:                combinations[32][2] = 0;
0414:                combinations[33][2] = 16;
0415:                combinations[34][2] = 32;
0416:                combinations[35][2] = 48;
0417:                combinations[32][3] = 5;
0418:                combinations[33][3] = 21;
0419:                combinations[34][3] = 37;
0420:                combinations[35][3] = 53;
0421:                combinations[32][4] = 10;
0422:                combinations[33][4] = 26;
0423:                combinations[34][4] = 42;
0424:                combinations[35][4] = 58;
0425:                combinations[32][5] = 15;
0426:                combinations[33][5] = 31;
0427:                combinations[34][5] = 47;
0428:                combinations[35][5] = 63;
0429:
0430:                combinations[36][0] = 0;
0431:                combinations[37][0] = 0;
0432:                combinations[38][0] = 0;
0433:                combinations[39][0] = 0;
0434:                combinations[36][1] = 0;
0435:                combinations[37][1] = 0;
0436:                combinations[38][1] = 0;
0437:                combinations[39][1] = 0;
0438:                combinations[36][2] = 3;
0439:                combinations[37][2] = 19;
0440:                combinations[38][2] = 35;
0441:                combinations[39][2] = 51;
0442:                combinations[36][3] = 6;
0443:                combinations[37][3] = 22;
0444:                combinations[38][3] = 38;
0445:                combinations[39][3] = 54;
0446:                combinations[36][4] = 9;
0447:                combinations[37][4] = 25;
0448:                combinations[38][4] = 41;
0449:                combinations[39][4] = 57;
0450:                combinations[36][5] = 12;
0451:                combinations[37][5] = 28;
0452:                combinations[38][5] = 44;
0453:                combinations[39][5] = 60;
0454:
0455:                // Horizontal, X
0456:
0457:                combinations[40][0] = 0;
0458:                combinations[41][0] = 0;
0459:                combinations[42][0] = 0;
0460:                combinations[43][0] = 0;
0461:                combinations[40][1] = 0;
0462:                combinations[41][1] = 0;
0463:                combinations[42][1] = 0;
0464:                combinations[43][1] = 0;
0465:                combinations[40][2] = 51;
0466:                combinations[41][2] = 55;
0467:                combinations[42][2] = 59;
0468:                combinations[43][2] = 63;
0469:                combinations[40][3] = 35;
0470:                combinations[41][3] = 39;
0471:                combinations[42][3] = 43;
0472:                combinations[43][3] = 47;
0473:                combinations[40][4] = 19;
0474:                combinations[41][4] = 23;
0475:                combinations[42][4] = 27;
0476:                combinations[43][4] = 31;
0477:                combinations[40][5] = 3;
0478:                combinations[41][5] = 7;
0479:                combinations[42][5] = 11;
0480:                combinations[43][5] = 15;
0481:
0482:                combinations[44][0] = 0;
0483:                combinations[45][0] = 0;
0484:                combinations[46][0] = 0;
0485:                combinations[47][0] = 0;
0486:                combinations[44][1] = 0;
0487:                combinations[45][1] = 0;
0488:                combinations[46][1] = 0;
0489:                combinations[47][1] = 0;
0490:                combinations[44][2] = 50;
0491:                combinations[45][2] = 54;
0492:                combinations[46][2] = 58;
0493:                combinations[47][2] = 62;
0494:                combinations[44][3] = 34;
0495:                combinations[45][3] = 38;
0496:                combinations[46][3] = 42;
0497:                combinations[47][3] = 46;
0498:                combinations[44][4] = 18;
0499:                combinations[45][4] = 22;
0500:                combinations[46][4] = 26;
0501:                combinations[47][4] = 30;
0502:                combinations[44][5] = 2;
0503:                combinations[45][5] = 6;
0504:                combinations[46][5] = 10;
0505:                combinations[47][5] = 14;
0506:
0507:                combinations[48][0] = 0;
0508:                combinations[49][0] = 0;
0509:                combinations[50][0] = 0;
0510:                combinations[51][0] = 0;
0511:                combinations[48][1] = 0;
0512:                combinations[49][1] = 0;
0513:                combinations[50][1] = 0;
0514:                combinations[51][1] = 0;
0515:                combinations[48][2] = 49;
0516:                combinations[49][2] = 53;
0517:                combinations[50][2] = 57;
0518:                combinations[51][2] = 61;
0519:                combinations[48][3] = 33;
0520:                combinations[49][3] = 37;
0521:                combinations[50][3] = 41;
0522:                combinations[51][3] = 45;
0523:                combinations[48][4] = 17;
0524:                combinations[49][4] = 21;
0525:                combinations[50][4] = 25;
0526:                combinations[51][4] = 29;
0527:                combinations[48][5] = 1;
0528:                combinations[49][5] = 5;
0529:                combinations[50][5] = 9;
0530:                combinations[51][5] = 13;
0531:
0532:                combinations[52][0] = 0;
0533:                combinations[53][0] = 0;
0534:                combinations[54][0] = 0;
0535:                combinations[55][0] = 0;
0536:                combinations[52][1] = 0;
0537:                combinations[53][1] = 0;
0538:                combinations[54][1] = 0;
0539:                combinations[55][1] = 0;
0540:                combinations[52][2] = 48;
0541:                combinations[53][2] = 52;
0542:                combinations[54][2] = 56;
0543:                combinations[55][2] = 60;
0544:                combinations[52][3] = 32;
0545:                combinations[53][3] = 36;
0546:                combinations[54][3] = 40;
0547:                combinations[55][3] = 44;
0548:                combinations[52][4] = 16;
0549:                combinations[53][4] = 20;
0550:                combinations[54][4] = 24;
0551:                combinations[55][4] = 28;
0552:                combinations[52][5] = 0;
0553:                combinations[53][5] = 4;
0554:                combinations[54][5] = 8;
0555:                combinations[55][5] = 12;
0556:
0557:                // Diagonal, X
0558:
0559:                combinations[56][0] = 0;
0560:                combinations[57][0] = 0;
0561:                combinations[58][0] = 0;
0562:                combinations[59][0] = 0;
0563:                combinations[56][1] = 0;
0564:                combinations[57][1] = 0;
0565:                combinations[58][1] = 0;
0566:                combinations[59][1] = 0;
0567:                combinations[56][2] = 51;
0568:                combinations[57][2] = 50;
0569:                combinations[58][2] = 49;
0570:                combinations[59][2] = 48;
0571:                combinations[56][3] = 39;
0572:                combinations[57][3] = 38;
0573:                combinations[58][3] = 37;
0574:                combinations[59][3] = 36;
0575:                combinations[56][4] = 27;
0576:                combinations[57][4] = 26;
0577:                combinations[58][4] = 25;
0578:                combinations[59][4] = 24;
0579:                combinations[56][5] = 15;
0580:                combinations[57][5] = 14;
0581:                combinations[58][5] = 13;
0582:                combinations[59][5] = 12;
0583:
0584:                combinations[60][0] = 0;
0585:                combinations[61][0] = 0;
0586:                combinations[62][0] = 0;
0587:                combinations[63][0] = 0;
0588:                combinations[60][1] = 0;
0589:                combinations[61][1] = 0;
0590:                combinations[62][1] = 0;
0591:                combinations[63][1] = 0;
0592:                combinations[60][2] = 3;
0593:                combinations[61][2] = 2;
0594:                combinations[62][2] = 1;
0595:                combinations[63][2] = 0;
0596:                combinations[60][3] = 23;
0597:                combinations[61][3] = 22;
0598:                combinations[62][3] = 21;
0599:                combinations[63][3] = 20;
0600:                combinations[60][4] = 43;
0601:                combinations[61][4] = 42;
0602:                combinations[62][4] = 41;
0603:                combinations[63][4] = 40;
0604:                combinations[60][5] = 63;
0605:                combinations[61][5] = 62;
0606:                combinations[62][5] = 61;
0607:                combinations[63][5] = 60;
0608:
0609:                // Diagonal, Y
0610:
0611:                combinations[64][0] = 0;
0612:                combinations[65][0] = 0;
0613:                combinations[66][0] = 0;
0614:                combinations[67][0] = 0;
0615:                combinations[64][1] = 0;
0616:                combinations[65][1] = 0;
0617:                combinations[66][1] = 0;
0618:                combinations[67][1] = 0;
0619:                combinations[64][2] = 63;
0620:                combinations[65][2] = 59;
0621:                combinations[66][2] = 55;
0622:                combinations[67][2] = 51;
0623:                combinations[64][3] = 46;
0624:                combinations[65][3] = 42;
0625:                combinations[66][3] = 38;
0626:                combinations[67][3] = 34;
0627:                combinations[64][4] = 29;
0628:                combinations[65][4] = 25;
0629:                combinations[66][4] = 21;
0630:                combinations[67][4] = 17;
0631:                combinations[64][5] = 12;
0632:                combinations[65][5] = 8;
0633:                combinations[66][5] = 4;
0634:                combinations[67][5] = 0;
0635:
0636:                combinations[68][0] = 0;
0637:                combinations[69][0] = 0;
0638:                combinations[70][0] = 0;
0639:                combinations[71][0] = 0;
0640:                combinations[68][1] = 0;
0641:                combinations[69][1] = 0;
0642:                combinations[70][1] = 0;
0643:                combinations[71][1] = 0;
0644:                combinations[68][2] = 15;
0645:                combinations[69][2] = 11;
0646:                combinations[70][2] = 7;
0647:                combinations[71][2] = 3;
0648:                combinations[68][3] = 30;
0649:                combinations[69][3] = 26;
0650:                combinations[70][3] = 22;
0651:                combinations[71][3] = 18;
0652:                combinations[68][4] = 45;
0653:                combinations[69][4] = 41;
0654:                combinations[70][4] = 37;
0655:                combinations[71][4] = 33;
0656:                combinations[68][5] = 60;
0657:                combinations[69][5] = 56;
0658:                combinations[70][5] = 52;
0659:                combinations[71][5] = 48;
0660:
0661:                // Corner to Corner
0662:
0663:                combinations[72][0] = 0;
0664:                combinations[73][0] = 0;
0665:                combinations[74][0] = 0;
0666:                combinations[75][0] = 0;
0667:                combinations[72][1] = 0;
0668:                combinations[73][1] = 0;
0669:                combinations[74][1] = 0;
0670:                combinations[75][1] = 0;
0671:                combinations[72][2] = 0;
0672:                combinations[73][2] = 3;
0673:                combinations[74][2] = 12;
0674:                combinations[75][2] = 15;
0675:                combinations[72][3] = 21;
0676:                combinations[73][3] = 22;
0677:                combinations[74][3] = 25;
0678:                combinations[75][3] = 26;
0679:                combinations[72][4] = 42;
0680:                combinations[73][4] = 41;
0681:                combinations[74][4] = 38;
0682:                combinations[75][4] = 37;
0683:                combinations[72][5] = 63;
0684:                combinations[73][5] = 60;
0685:                combinations[74][5] = 51;
0686:                combinations[75][5] = 48;
0687:
0688:                // Initialize the combination flags to zero.
0689:                for (int i = 0; i < 76; i++)
0690:                    combinations[i][6] = 0;
0691:
0692:                // Set up the pos_to_comb array to point to every winning combination that a given 
0693:                // position may have.
0694:                setup_pos_to_comb();
0695:
0696:                // Set up the best_picks array.
0697:                update_best_picks();
0698:            }
0699:
0700:            /**
0701:             *  Initialize the "outside four" array. 
0702:             */
0703:            public void init_outside_four() {
0704:                for (int i = 0; i < 18; i++) {
0705:                    outside_four[i][0] = 0;
0706:                    outside_four[i][1] = 0;
0707:                    outside_four[i][2] = faces[i][2];
0708:                    outside_four[i][3] = faces[i][5];
0709:                    outside_four[i][4] = faces[i][14];
0710:                    outside_four[i][5] = faces[i][17];
0711:                }
0712:            }
0713:
0714:            /**
0715:             *  Initialize the "inside four" array.
0716:             */
0717:            public void init_inside_four() {
0718:                for (int i = 0; i < 18; i++) {
0719:                    inside_four[i][0] = 0;
0720:                    inside_four[i][1] = 0;
0721:                    inside_four[i][2] = faces[i][7];
0722:                    inside_four[i][3] = faces[i][8];
0723:                    inside_four[i][4] = faces[i][11];
0724:                    inside_four[i][5] = faces[i][12];
0725:                }
0726:            }
0727:
0728:            /**
0729:             *  Initialize the "faces" array. 
0730:             */
0731:            public void init_faces() {
0732:
0733:                faces[0][0] = 0;
0734:                faces[0][1] = 0;
0735:                faces[0][2] = 12;
0736:                faces[0][6] = 13;
0737:                faces[0][10] = 14;
0738:                faces[0][14] = 15;
0739:                faces[0][3] = 8;
0740:                faces[0][7] = 9;
0741:                faces[0][11] = 10;
0742:                faces[0][15] = 11;
0743:                faces[0][4] = 4;
0744:                faces[0][8] = 5;
0745:                faces[0][12] = 6;
0746:                faces[0][16] = 7;
0747:                faces[0][5] = 0;
0748:                faces[0][9] = 1;
0749:                faces[0][13] = 2;
0750:                faces[0][17] = 3;
0751:
0752:                faces[1][0] = 0;
0753:                faces[1][1] = 0;
0754:                faces[1][2] = 28;
0755:                faces[1][6] = 29;
0756:                faces[1][10] = 30;
0757:                faces[1][14] = 31;
0758:                faces[1][3] = 24;
0759:                faces[1][7] = 25;
0760:                faces[1][11] = 26;
0761:                faces[1][15] = 27;
0762:                faces[1][4] = 20;
0763:                faces[1][8] = 21;
0764:                faces[1][12] = 22;
0765:                faces[1][16] = 23;
0766:                faces[1][5] = 16;
0767:                faces[1][9] = 17;
0768:                faces[1][13] = 18;
0769:                faces[1][17] = 19;
0770:
0771:                faces[2][0] = 0;
0772:                faces[2][1] = 0;
0773:                faces[2][2] = 44;
0774:                faces[2][6] = 45;
0775:                faces[2][10] = 46;
0776:                faces[2][14] = 47;
0777:                faces[2][3] = 40;
0778:                faces[2][7] = 41;
0779:                faces[2][11] = 42;
0780:                faces[2][15] = 43;
0781:                faces[2][4] = 36;
0782:                faces[2][8] = 37;
0783:                faces[2][12] = 38;
0784:                faces[2][16] = 39;
0785:                faces[2][5] = 32;
0786:                faces[2][9] = 33;
0787:                faces[2][13] = 34;
0788:                faces[2][17] = 35;
0789:
0790:                faces[3][0] = 0;
0791:                faces[3][1] = 0;
0792:                faces[3][2] = 60;
0793:                faces[3][6] = 61;
0794:                faces[3][10] = 62;
0795:                faces[3][14] = 63;
0796:                faces[3][3] = 56;
0797:                faces[3][7] = 57;
0798:                faces[3][11] = 58;
0799:                faces[3][15] = 59;
0800:                faces[3][4] = 52;
0801:                faces[3][8] = 53;
0802:                faces[3][12] = 54;
0803:                faces[3][16] = 55;
0804:                faces[3][5] = 48;
0805:                faces[3][9] = 49;
0806:                faces[3][13] = 50;
0807:                faces[3][17] = 51;
0808:
0809:                faces[4][0] = 0;
0810:                faces[4][1] = 0;
0811:                faces[4][2] = 12;
0812:                faces[4][6] = 28;
0813:                faces[4][10] = 44;
0814:                faces[4][14] = 60;
0815:                faces[4][3] = 8;
0816:                faces[4][7] = 24;
0817:                faces[4][11] = 40;
0818:                faces[4][15] = 56;
0819:                faces[4][4] = 4;
0820:                faces[4][8] = 20;
0821:                faces[4][12] = 36;
0822:                faces[4][16] = 52;
0823:                faces[4][5] = 0;
0824:                faces[4][9] = 16;
0825:                faces[4][13] = 32;
0826:                faces[4][17] = 48;
0827:
0828:                faces[5][0] = 0;
0829:                faces[5][1] = 0;
0830:                faces[5][2] = 13;
0831:                faces[5][6] = 29;
0832:                faces[5][10] = 45;
0833:                faces[5][14] = 61;
0834:                faces[5][3] = 9;
0835:                faces[5][7] = 25;
0836:                faces[5][11] = 41;
0837:                faces[5][15] = 57;
0838:                faces[5][4] = 5;
0839:                faces[5][8] = 21;
0840:                faces[5][12] = 37;
0841:                faces[5][16] = 53;
0842:                faces[5][5] = 1;
0843:                faces[5][9] = 17;
0844:                faces[5][13] = 33;
0845:                faces[5][17] = 49;
0846:
0847:                faces[6][0] = 0;
0848:                faces[6][1] = 0;
0849:                faces[6][2] = 14;
0850:                faces[6][6] = 30;
0851:                faces[6][10] = 46;
0852:                faces[6][14] = 62;
0853:                faces[6][3] = 10;
0854:                faces[6][7] = 26;
0855:                faces[6][11] = 42;
0856:                faces[6][15] = 58;
0857:                faces[6][4] = 6;
0858:                faces[6][8] = 22;
0859:                faces[6][12] = 38;
0860:                faces[6][16] = 54;
0861:                faces[6][5] = 2;
0862:                faces[6][9] = 18;
0863:                faces[6][13] = 34;
0864:                faces[6][17] = 50;
0865:
0866:                faces[7][0] = 0;
0867:                faces[7][1] = 0;
0868:                faces[7][2] = 15;
0869:                faces[7][6] = 31;
0870:                faces[7][10] = 47;
0871:                faces[7][14] = 63;
0872:                faces[7][3] = 11;
0873:                faces[7][7] = 27;
0874:                faces[7][11] = 43;
0875:                faces[7][15] = 59;
0876:                faces[7][4] = 7;
0877:                faces[7][8] = 23;
0878:                faces[7][12] = 39;
0879:                faces[7][16] = 55;
0880:                faces[7][5] = 3;
0881:                faces[7][9] = 19;
0882:                faces[7][13] = 35;
0883:                faces[7][17] = 51;
0884:
0885:                faces[8][0] = 0;
0886:                faces[8][1] = 0;
0887:                faces[8][2] = 12;
0888:                faces[8][6] = 28;
0889:                faces[8][10] = 44;
0890:                faces[8][14] = 60;
0891:                faces[8][3] = 13;
0892:                faces[8][7] = 29;
0893:                faces[8][11] = 45;
0894:                faces[8][15] = 61;
0895:                faces[8][4] = 14;
0896:                faces[8][8] = 30;
0897:                faces[8][12] = 46;
0898:                faces[8][16] = 62;
0899:                faces[8][5] = 15;
0900:                faces[8][9] = 31;
0901:                faces[8][13] = 47;
0902:                faces[8][17] = 63;
0903:
0904:                faces[9][0] = 0;
0905:                faces[9][1] = 0;
0906:                faces[9][2] = 8;
0907:                faces[9][6] = 24;
0908:                faces[9][10] = 40;
0909:                faces[9][14] = 56;
0910:                faces[9][3] = 9;
0911:                faces[9][7] = 25;
0912:                faces[9][11] = 41;
0913:                faces[9][15] = 57;
0914:                faces[9][4] = 10;
0915:                faces[9][8] = 26;
0916:                faces[9][12] = 42;
0917:                faces[9][16] = 58;
0918:                faces[9][5] = 11;
0919:                faces[9][9] = 27;
0920:                faces[9][13] = 43;
0921:                faces[9][17] = 59;
0922:
0923:                faces[10][0] = 0;
0924:                faces[10][1] = 0;
0925:                faces[10][2] = 4;
0926:                faces[10][6] = 20;
0927:                faces[10][10] = 36;
0928:                faces[10][14] = 52;
0929:                faces[10][3] = 5;
0930:                faces[10][7] = 21;
0931:                faces[10][11] = 37;
0932:                faces[10][15] = 53;
0933:                faces[10][4] = 6;
0934:                faces[10][8] = 22;
0935:                faces[10][12] = 38;
0936:                faces[10][16] = 54;
0937:                faces[10][5] = 7;
0938:                faces[10][9] = 23;
0939:                faces[10][13] = 39;
0940:                faces[10][17] = 55;
0941:
0942:                faces[11][0] = 0;
0943:                faces[11][1] = 0;
0944:                faces[11][2] = 0;
0945:                faces[11][6] = 16;
0946:                faces[11][10] = 32;
0947:                faces[11][14] = 48;
0948:                faces[11][3] = 1;
0949:                faces[11][7] = 17;
0950:                faces[11][11] = 33;
0951:                faces[11][15] = 49;
0952:                faces[11][4] = 2;
0953:                faces[11][8] = 18;
0954:                faces[11][12] = 34;
0955:                faces[11][16] = 50;
0956:                faces[11][5] = 3;
0957:                faces[11][9] = 19;
0958:                faces[11][13] = 35;
0959:                faces[11][17] = 51;
0960:
0961:                faces[12][0] = 0;
0962:                faces[12][1] = 0;
0963:                faces[12][2] = 12;
0964:                faces[12][6] = 13;
0965:                faces[12][10] = 14;
0966:                faces[12][14] = 15;
0967:                faces[12][3] = 24;
0968:                faces[12][7] = 25;
0969:                faces[12][11] = 26;
0970:                faces[12][15] = 27;
0971:                faces[12][4] = 36;
0972:                faces[12][8] = 37;
0973:                faces[12][12] = 38;
0974:                faces[12][16] = 39;
0975:                faces[12][5] = 48;
0976:                faces[12][9] = 49;
0977:                faces[12][13] = 50;
0978:                faces[12][17] = 51;
0979:
0980:                faces[13][0] = 0;
0981:                faces[13][1] = 0;
0982:                faces[13][2] = 0;
0983:                faces[13][6] = 1;
0984:                faces[13][10] = 2;
0985:                faces[13][14] = 3;
0986:                faces[13][3] = 20;
0987:                faces[13][7] = 21;
0988:                faces[13][11] = 22;
0989:                faces[13][15] = 23;
0990:                faces[13][4] = 40;
0991:                faces[13][8] = 41;
0992:                faces[13][12] = 42;
0993:                faces[13][16] = 43;
0994:                faces[13][5] = 60;
0995:                faces[13][9] = 61;
0996:                faces[13][13] = 62;
0997:                faces[13][17] = 63;
0998:
0999:                faces[14][0] = 0;
1000:                faces[14][1] = 0;
1001:                faces[14][2] = 12;
1002:                faces[14][6] = 28;
1003:                faces[14][10] = 44;
1004:                faces[14][14] = 60;
1005:                faces[14][3] = 9;
1006:                faces[14][7] = 25;
1007:                faces[14][11] = 41;
1008:                faces[14][15] = 57;
1009:                faces[14][4] = 6;
1010:                faces[14][8] = 22;
1011:                faces[14][12] = 38;
1012:                faces[14][16] = 54;
1013:                faces[14][5] = 3;
1014:                faces[14][9] = 19;
1015:                faces[14][13] = 35;
1016:                faces[14][17] = 51;
1017:
1018:                faces[15][0] = 0;
1019:                faces[15][1] = 0;
1020:                faces[15][2] = 15;
1021:                faces[15][6] = 31;
1022:                faces[15][10] = 47;
1023:                faces[15][14] = 63;
1024:                faces[15][3] = 10;
1025:                faces[15][7] = 26;
1026:                faces[15][11] = 42;
1027:                faces[15][15] = 58;
1028:                faces[15][4] = 5;
1029:                faces[15][8] = 21;
1030:                faces[15][12] = 37;
1031:                faces[15][16] = 53;
1032:                faces[15][5] = 0;
1033:                faces[15][9] = 16;
1034:                faces[15][13] = 32;
1035:                faces[15][17] = 48;
1036:
1037:                faces[16][0] = 0;
1038:                faces[16][1] = 0;
1039:                faces[16][2] = 12;
1040:                faces[16][6] = 29;
1041:                faces[16][10] = 46;
1042:                faces[16][14] = 63;
1043:                faces[16][3] = 8;
1044:                faces[16][7] = 25;
1045:                faces[16][11] = 42;
1046:                faces[16][15] = 59;
1047:                faces[16][4] = 4;
1048:                faces[16][8] = 21;
1049:                faces[16][12] = 38;
1050:                faces[16][16] = 55;
1051:                faces[16][5] = 0;
1052:                faces[16][9] = 17;
1053:                faces[16][13] = 34;
1054:                faces[16][17] = 51;
1055:
1056:                faces[17][0] = 0;
1057:                faces[17][1] = 0;
1058:                faces[17][2] = 15;
1059:                faces[17][6] = 30;
1060:                faces[17][10] = 45;
1061:                faces[17][14] = 60;
1062:                faces[17][3] = 11;
1063:                faces[17][7] = 26;
1064:                faces[17][11] = 41;
1065:                faces[17][15] = 56;
1066:                faces[17][4] = 7;
1067:                faces[17][8] = 22;
1068:                faces[17][12] = 37;
1069:                faces[17][16] = 52;
1070:                faces[17][5] = 3;
1071:                faces[17][9] = 18;
1072:                faces[17][13] = 33;
1073:                faces[17][17] = 48;
1074:            }
1075:
1076:            /**
1077:             *  Render the current face set in the 2D window.
1078:             */
1079:            public void render2D(Graphics gc) {
1080:
1081:                gc.setColor(background);
1082:                gc.fillRect(0, 0, width, height);
1083:
1084:                int id;
1085:                int x, y;
1086:
1087:                float begX;
1088:                float begY;
1089:
1090:                for (int l = 0; l < 3; l++) {
1091:                    begY = 28.0f + l * (5.f * 23.3f);
1092:                    for (int k = 0; k < 6; k++) {
1093:                        begX = 11.65f + k * (5.f * 11.65f);
1094:                        int count = 0;
1095:                        int face = l * 6 + k;
1096:                        for (int i = 0; i < 4; i++) {
1097:                            for (int j = 0; j < 4; j++) {
1098:                                x = (int) begX + i * 12;
1099:                                y = (int) begY + j * 12;
1100:                                id = faces[face][count + 2];
1101:                                if (occupied[id] == HUMAN) {
1102:                                    x -= 2;
1103:                                    y -= 2;
1104:                                    gc.setColor(red);
1105:                                    gc.fillRect(x, y, 5, 5);
1106:                                } else if (occupied[id] == MACHINE) {
1107:                                    x -= 2;
1108:                                    y -= 2;
1109:                                    gc.setColor(blue);
1110:                                    gc.fillRect(x, y, 5, 5);
1111:                                } else {
1112:                                    x -= 1;
1113:                                    y -= 1;
1114:                                    gc.setColor(gray);
1115:                                    gc.fillRect(x, y, 2, 2);
1116:                                }
1117:                                if (highlight[face]) {
1118:                                    gc.setColor(yellow);
1119:                                    positions
1120:                                            .setHighlight(faces[face][count + 2]);
1121:                                }
1122:                                count++;
1123:                            }
1124:                        }
1125:                        if (highlight[face])
1126:                            gc.setColor(yellow);
1127:                        else
1128:                            gc.setColor(white);
1129:                        if ((face + 1) < 10)
1130:                            gc.drawString("Face " + (face + 1), (int) begX - 2,
1131:                                    (int) begY + 60);
1132:                        else
1133:                            gc.drawString("Face " + (face + 1), (int) begX - 4,
1134:                                    (int) begY + 60);
1135:                    }
1136:                }
1137:            }
1138:
1139:            /**
1140:             *  Determine what position has been selected in the 2D window.
1141:             */
1142:            public void checkSelection2D(int x, int y, int player) {
1143:
1144:                int id;
1145:                int posX, posY;
1146:
1147:                float begX;
1148:                float begY;
1149:
1150:                for (int l = 0; l < 3; l++) {
1151:                    begY = 28.0f + l * (5.f * 23.3f);
1152:                    for (int k = 0; k < 6; k++) {
1153:                        begX = 11.65f + k * (5.f * 11.65f);
1154:                        int count = 0;
1155:                        int face = l * 6 + k;
1156:                        for (int i = 0; i < 4; i++) {
1157:                            for (int j = 0; j < 4; j++) {
1158:                                posX = (int) begX + i * 12;
1159:                                posY = (int) begY + j * 12;
1160:                                if (x > posX - 4 && x < posX + 4
1161:                                        && y > posY - 4 && y < posY + 4) {
1162:
1163:                                    id = faces[face][count + 2];
1164:
1165:                                    if (occupied[id] == UNOCCUPIED) {
1166:                                        positions.set(id, player);
1167:                                        selection(id, player);
1168:                                        canvas.repaint();
1169:                                    }
1170:                                    return;
1171:                                }
1172:                                count++;
1173:                            }
1174:                        }
1175:                        if ((x > begX - 4 && x < begX + 40)
1176:                                && (y > begY + 45 && y < begY + 60)) {
1177:
1178:                            count = 0;
1179:                            for (int i = 0; i < 4; i++) {
1180:                                for (int j = 0; j < 4; j++) {
1181:                                    if (highlight[face])
1182:                                        positions
1183:                                                .clearHighlight(faces[face][count + 2]);
1184:                                    count++;
1185:                                }
1186:                            }
1187:                            if (highlight[face])
1188:                                highlight[face] = false;
1189:                            else
1190:                                highlight[face] = true;
1191:                            canvas.repaint();
1192:                        }
1193:                    }
1194:                }
1195:
1196:            }
1197:
1198:            /**
1199:             *  Record the player's move.
1200:             */
1201:            public void selection(int pos, int player) {
1202:
1203:                int num_combinations;
1204:                int comb;
1205:
1206:                this .player = player;
1207:
1208:                if (player == HUMAN) {
1209:
1210:                    // If position is already occupied, return.
1211:                    if (occupied[pos] != 0)
1212:                        return;
1213:
1214:                    // Mark the position as HUMAN.
1215:                    occupied[pos] = HUMAN;
1216:
1217:                    // Update the logic arrays.
1218:                    this .player = update_logic_arrays(pos);
1219:
1220:                    // Have the computer determine its move.
1221:                    choose_move();
1222:                }
1223:            }
1224:
1225:            /**
1226:             *  Determine the computer's move.
1227:             */
1228:            public void choose_move() {
1229:
1230:                if (player == MACHINE) {
1231:
1232:                    // Babe in the woods.
1233:                    if (skill_level == 0) {
1234:                        if (!block_winning_move()) {
1235:                            if (!pick_7()) {
1236:                                if (!check_outside_four()) {
1237:                                    pick_best_position();
1238:                                }
1239:                            }
1240:                        }
1241:                    }
1242:
1243:                    // Walk and chew gum.
1244:                    else if (skill_level == 1) {
1245:                        if (!block_winning_move()) {
1246:                            if (!block_intersecting_rows()) {
1247:                                if (!block_inside_four()) {
1248:                                    if (!block_outside_four()) {
1249:                                        pick_best_position();
1250:                                    }
1251:                                }
1252:                            }
1253:                        }
1254:                    }
1255:
1256:                    // Jeopordy contestant.
1257:                    else if (skill_level == 2) {
1258:                        if (!block_winning_move()) {
1259:                            if (!block_intersecting_rows()) {
1260:                                if (!block_inside_four()) {
1261:                                    if (!block_outside_four()) {
1262:                                        if (!pick_7()) {
1263:                                            pick_best_position();
1264:                                        }
1265:                                    }
1266:                                }
1267:                            }
1268:                        }
1269:                    }
1270:
1271:                    // Rocket scientist.
1272:                    else if (skill_level == 3) {
1273:                        if (!block_winning_move()) {
1274:                            if (!block_intersecting_rows()) {
1275:                                if (!block_chair_move()) {
1276:                                    if (!check_face_three()) {
1277:                                        if (!block_central_four()) {
1278:                                            if (!block_inside_four()) {
1279:                                                if (!block_outside_four()) {
1280:                                                    if (!take_inside_four()) {
1281:                                                        if (!take_outside_four()) {
1282:                                                            if (!pick_7()) {
1283:                                                                if (!check_outside_four()) {
1284:                                                                    pick_best_position();
1285:                                                                }
1286:                                                            }
1287:                                                        }
1288:                                                    }
1289:                                                }
1290:                                            }
1291:                                        }
1292:                                    }
1293:                                }
1294:                            }
1295:                        }
1296:                    }
1297:
1298:                    // Be afraid, be very afraid.
1299:                    else if (skill_level == 4) {
1300:                        if (!block_winning_move()) {
1301:                            if (!block_intersecting_rows()) {
1302:                                if (!block_chair_move()) {
1303:                                    if (!block_walk_move()) {
1304:                                        if (!block_central_four()) {
1305:                                            if (!block_inside_four()) {
1306:                                                if (!block_outside_four()) {
1307:                                                    if (!check_face_three()) {
1308:                                                        if (!check_intersecting_rows2()) {
1309:                                                            if (!take_inside_four()) {
1310:                                                                if (!take_outside_four()) {
1311:                                                                    if (!pick_7()) {
1312:                                                                        if (!check_outside_four()) {
1313:                                                                            pick_best_position();
1314:                                                                        }
1315:                                                                    }
1316:                                                                }
1317:                                                            }
1318:                                                        }
1319:                                                    }
1320:                                                }
1321:                                            }
1322:                                        }
1323:                                    }
1324:                                }
1325:                            }
1326:                        }
1327:                    }
1328:                }
1329:            }
1330:
1331:            /**
1332:             *  Check for a winning move.
1333:             */
1334:            public boolean block_winning_move() {
1335:
1336:                // Loop through each combination and see if any player occupies 
1337:                // three positions. If so, take the last remaining position.
1338:                int pos;
1339:                for (int i = 0; i < 76; i++) {
1340:                    if (combinations[i][0] == 3) {
1341:                        for (int j = 2; j < 6; j++) {
1342:                            pos = combinations[i][j];
1343:                            if (occupied[pos] == 0) {
1344:                                occupied[pos] = MACHINE;
1345:                                positions.set(pos, MACHINE);
1346:                                player = update_logic_arrays(pos);
1347:                                if (debug)
1348:                                    System.out
1349:                                            .println("block_winning_move:  true");
1350:                                return true;
1351:                            }
1352:                        }
1353:                    }
1354:                }
1355:                if (debug)
1356:                    System.out.println("check_winning_move:  false");
1357:                return false;
1358:            }
1359:
1360:            /**
1361:             *  Block outside four
1362:             */
1363:            public boolean block_outside_four() {
1364:
1365:                int pos;
1366:                int index = 0;
1367:                int max = 0;
1368:
1369:                // Block the opponent, if necessary.
1370:                for (int i = 0; i < 18; i++) {
1371:                    if (outside_four[i][0] > 0 && outside_four[i][1] == HUMAN) {
1372:                        if (outside_four[i][0] > max) {
1373:                            index = i;
1374:                            max = outside_four[i][0];
1375:                        }
1376:                    }
1377:                }
1378:
1379:                if (max > 0) {
1380:                    for (int j = 2; j < 6; j++) {
1381:                        pos = outside_four[index][j];
1382:                        if (occupied[pos] == 0) {
1383:                            occupied[pos] = MACHINE;
1384:                            positions.set(pos, MACHINE);
1385:                            player = update_logic_arrays(pos);
1386:                            if (debug)
1387:                                System.out.println("block_outside_four:  true");
1388:                            return true;
1389:                        }
1390:                    }
1391:                }
1392:
1393:                if (debug)
1394:                    System.out.println("block_outside_four:  false");
1395:                return false;
1396:            }
1397:
1398:            /**
1399:             *  Block central four
1400:             */
1401:            public boolean block_central_four() {
1402:
1403:                int pos;
1404:                int index = 0;
1405:                int max = 0;
1406:
1407:                // Block the opponent, if necessary.
1408:                for (int i = 1; i < 3; i++) {
1409:                    if (inside_four[i][0] > 0 && inside_four[i][1] == HUMAN) {
1410:                        if (inside_four[i][0] > max) {
1411:                            index = i;
1412:                            max = inside_four[i][0];
1413:                        }
1414:                    }
1415:                }
1416:
1417:                if (max > 0) {
1418:                    for (int j = 2; j < 6; j++) {
1419:                        pos = inside_four[index][j];
1420:                        if (occupied[pos] == 0) {
1421:                            occupied[pos] = MACHINE;
1422:                            positions.set(pos, MACHINE);
1423:                            player = update_logic_arrays(pos);
1424:                            if (debug)
1425:                                System.out.println("block_central_four:  true");
1426:                            return true;
1427:                        }
1428:                    }
1429:                }
1430:
1431:                if (debug)
1432:                    System.out.println("block_central_four:  false");
1433:                return false;
1434:            }
1435:
1436:            /**   
1437:             *  Check each face for a forced win.
1438:             */
1439:            public boolean check_face_three() {
1440:
1441:                int pos;
1442:                int index = 0;
1443:                int human = 0;
1444:                int machine = 0;
1445:
1446:                // Block the opponent from a forced win.
1447:                for (int i = 0; i < 18; i++) {
1448:                    if (outside_four[i][0] == -1) {
1449:                        human = 0;
1450:                        machine = 0;
1451:                        for (int j = 2; j < 6; j++) {
1452:                            if (occupied[outside_four[i][j]] == MACHINE)
1453:                                machine++;
1454:                            else if (occupied[outside_four[i][j]] == HUMAN)
1455:                                human++;
1456:                        }
1457:                        if (debug)
1458:                            System.out.println("machine = " + machine);
1459:                        if (debug)
1460:                            System.out.println("human   = " + human);
1461:                        if (human == 3 && machine == 1) {
1462:                            if (debug)
1463:                                System.out
1464:                                        .println("human == 3 && machine == 1");
1465:                            for (int j = 2; j < 18; j++) {
1466:                                pos = faces[i][j];
1467:                                if (occupied[pos] == 0) {
1468:                                    for (int k = 0; k < 76; k++) {
1469:                                        if (combinations[i][0] == 2
1470:                                                & combinations[i][1] == HUMAN) {
1471:                                            for (int l = 0; l < 4; l++) {
1472:                                                if (combinations[i][l] == pos) {
1473:                                                    occupied[pos] = MACHINE;
1474:                                                    positions.set(pos, MACHINE);
1475:                                                    player = update_logic_arrays(pos);
1476:                                                    if (debug)
1477:                                                        System.out
1478:                                                                .println("check_face_three:  true");
1479:                                                    return true;
1480:                                                }
1481:                                            }
1482:                                        }
1483:                                    }
1484:                                }
1485:                            }
1486:                        }
1487:                    }
1488:                }
1489:
1490:                if (debug)
1491:                    System.out.println("check_face_three:  false");
1492:                return false;
1493:            }
1494:
1495:            /**
1496:             *  Block inside four
1497:             */
1498:            public boolean block_inside_four() {
1499:
1500:                int pos;
1501:                int index = 0;
1502:                int max = 0;
1503:
1504:                // Block the opponent, if necessary.
1505:                for (int i = 0; i < 18; i++) {
1506:                    if (inside_four[i][0] > 0 && inside_four[i][1] == HUMAN) {
1507:                        if (inside_four[i][0] > max) {
1508:                            index = i;
1509:                            max = inside_four[i][0];
1510:                        }
1511:                    }
1512:                }
1513:
1514:                if (max > 0) {
1515:                    for (int j = 2; j < 6; j++) {
1516:                        pos = inside_four[index][j];
1517:                        if (occupied[pos] == 0) {
1518:                            occupied[pos] = MACHINE;
1519:                            positions.set(pos, MACHINE);
1520:                            player = update_logic_arrays(pos);
1521:                            if (debug)
1522:                                System.out.println("block_inside_four:  true");
1523:                            return true;
1524:                        }
1525:                    }
1526:                }
1527:
1528:                if (debug)
1529:                    System.out.println("block_inside_four:  false");
1530:                return false;
1531:            }
1532:
1533:            public boolean block_chair_move() {
1534:
1535:                int pos;
1536:
1537:                int ncorners = 0; // Number of corners owned by human
1538:                int corner = 0; // Corner owned by machine
1539:
1540:                if (debug)
1541:                    System.out.println("inside block_chair_move");
1542:
1543:                // Loop through all of the faces.
1544:                for (int i = 0; i < 18; i++) {
1545:
1546:                    // Determine which corners the human owns.
1547:                    if (occupied[faces[i][2]] == HUMAN)
1548:                        ncorners++;
1549:                    else if (occupied[faces[i][2]] == MACHINE)
1550:                        corner = 2;
1551:                    if (occupied[faces[i][5]] == HUMAN)
1552:                        ncorners++;
1553:                    else if (occupied[faces[i][5]] == MACHINE)
1554:                        corner = 5;
1555:                    if (occupied[faces[i][14]] == HUMAN)
1556:                        ncorners++;
1557:                    else if (occupied[faces[i][14]] == MACHINE)
1558:                        corner = 14;
1559:                    if (occupied[faces[i][17]] == HUMAN)
1560:                        ncorners++;
1561:                    else if (occupied[faces[i][17]] == MACHINE)
1562:                        corner = 17;
1563:
1564:                    // If the human owns three corners, continue with the search.
1565:                    if (ncorners == 3) {
1566:                        if (corner == 2) {
1567:                            if (occupied[faces[i][3]] == HUMAN
1568:                                    && occupied[faces[i][7]] == 0
1569:                                    && occupied[faces[i][8]] == 0
1570:                                    && occupied[faces[i][11]] == 0
1571:                                    && occupied[faces[i][15]] == 0
1572:                                    && occupied[faces[i][16]] == 0) {
1573:                                pos = faces[i][11];
1574:                                occupied[pos] = MACHINE;
1575:                                positions.set(pos, MACHINE);
1576:                                player = update_logic_arrays(pos);
1577:                                if (debug)
1578:                                    System.out
1579:                                            .println("block_chair_move: found");
1580:                                return true;
1581:                            }
1582:                            if (occupied[faces[i][4]] == HUMAN
1583:                                    && occupied[faces[i][8]] == 0
1584:                                    && occupied[faces[i][11]] == 0
1585:                                    && occupied[faces[i][12]] == 0
1586:                                    && occupied[faces[i][15]] == 0
1587:                                    && occupied[faces[i][16]] == 0) {
1588:                                pos = faces[i][12];
1589:                                occupied[pos] = MACHINE;
1590:                                positions.set(pos, MACHINE);
1591:                                player = update_logic_arrays(pos);
1592:                                if (debug)
1593:                                    System.out
1594:                                            .println("block_chair_move: found");
1595:                                return true;
1596:                            }
1597:                            if (occupied[faces[i][6]] == HUMAN
1598:                                    && occupied[faces[i][7]] == 0
1599:                                    && occupied[faces[i][8]] == 0
1600:                                    && occupied[faces[i][9]] == 0
1601:                                    && occupied[faces[i][11]] == 0
1602:                                    && occupied[faces[i][13]] == 0) {
1603:                                pos = faces[i][8];
1604:                                occupied[pos] = MACHINE;
1605:                                positions.set(pos, MACHINE);
1606:                                player = update_logic_arrays(pos);
1607:                                if (debug)
1608:                                    System.out
1609:                                            .println("block_chair_move: found");
1610:                                return true;
1611:                            }
1612:                            if (occupied[faces[i][10]] == HUMAN
1613:                                    && occupied[faces[i][8]] == 0
1614:                                    && occupied[faces[i][9]] == 0
1615:                                    && occupied[faces[i][11]] == 0
1616:                                    && occupied[faces[i][12]] == 0
1617:                                    && occupied[faces[i][13]] == 0) {
1618:                                pos = faces[i][11];
1619:                                occupied[pos] = MACHINE;
1620:                                positions.set(pos, MACHINE);
1621:                                player = update_logic_arrays(pos);
1622:                                if (debug)
1623:                                    System.out
1624:                                            .println("block_chair_move: found");
1625:                                return true;
1626:                            }
1627:                            if (occupied[faces[i][7]] == HUMAN
1628:                                    && occupied[faces[i][3]] == 0
1629:                                    && occupied[faces[i][8]] == 0
1630:                                    && occupied[faces[i][11]] == 0
1631:                                    && occupied[faces[i][15]] == 0
1632:                                    && occupied[faces[i][16]] == 0) {
1633:                                pos = faces[i][11];
1634:                                occupied[pos] = MACHINE;
1635:                                positions.set(pos, MACHINE);
1636:                                player = update_logic_arrays(pos);
1637:                                if (debug)
1638:                                    System.out
1639:                                            .println("block_chair_move: found");
1640:                                return true;
1641:                            }
1642:                            if (occupied[faces[i][12]] == HUMAN
1643:                                    && occupied[faces[i][4]] == 0
1644:                                    && occupied[faces[i][8]] == 0
1645:                                    && occupied[faces[i][11]] == 0
1646:                                    && occupied[faces[i][15]] == 0
1647:                                    && occupied[faces[i][16]] == 0) {
1648:                                pos = faces[i][16];
1649:                                occupied[pos] = MACHINE;
1650:                                positions.set(pos, MACHINE);
1651:                                player = update_logic_arrays(pos);
1652:                                if (debug)
1653:                                    System.out
1654:                                            .println("block_chair_move: found");
1655:                                return true;
1656:                            }
1657:                        } else if (corner == 5) {
1658:                            if (occupied[faces[i][9]] == HUMAN
1659:                                    && occupied[faces[i][6]] == 0
1660:                                    && occupied[faces[i][7]] == 0
1661:                                    && occupied[faces[i][8]] == 0
1662:                                    && occupied[faces[i][10]] == 0
1663:                                    && occupied[faces[i][12]] == 0) {
1664:                                pos = faces[i][7];
1665:                                occupied[pos] = MACHINE;
1666:                                positions.set(pos, MACHINE);
1667:                                player = update_logic_arrays(pos);
1668:                                if (debug)
1669:                                    System.out
1670:                                            .println("block_chair_move: found");
1671:                                return true;
1672:                            }
1673:                            if (occupied[faces[i][13]] == HUMAN
1674:                                    && occupied[faces[i][7]] == 0
1675:                                    && occupied[faces[i][7]] == 0
1676:                                    && occupied[faces[i][10]] == 0
1677:                                    && occupied[faces[i][11]] == 0
1678:                                    && occupied[faces[i][12]] == 0) {
1679:                                pos = faces[i][12];
1680:                                occupied[pos] = MACHINE;
1681:                                positions.set(pos, MACHINE);
1682:                                player = update_logic_arrays(pos);
1683:                                if (debug)
1684:                                    System.out
1685:                                            .println("block_chair_move: found");
1686:                                return true;
1687:                            }
1688:                            if (occupied[faces[i][4]] == HUMAN
1689:                                    && occupied[faces[i][8]] == 0
1690:                                    && occupied[faces[i][11]] == 0
1691:                                    && occupied[faces[i][12]] == 0
1692:                                    && occupied[faces[i][15]] == 0
1693:                                    && occupied[faces[i][16]] == 0) {
1694:                                pos = faces[i][12];
1695:                                occupied[pos] = MACHINE;
1696:                                positions.set(pos, MACHINE);
1697:                                player = update_logic_arrays(pos);
1698:                                if (debug)
1699:                                    System.out
1700:                                            .println("block_chair_move: found");
1701:                                return true;
1702:                            }
1703:                            if (occupied[faces[i][3]] == HUMAN
1704:                                    && occupied[faces[i][7]] == 0
1705:                                    && occupied[faces[i][11]] == 0
1706:                                    && occupied[faces[i][12]] == 0
1707:                                    && occupied[faces[i][15]] == 0
1708:                                    && occupied[faces[i][16]] == 0) {
1709:                                pos = faces[i][7];
1710:                                occupied[pos] = MACHINE;
1711:                                positions.set(pos, MACHINE);
1712:                                player = update_logic_arrays(pos);
1713:                                if (debug)
1714:                                    System.out
1715:                                            .println("block_chair_move: found");
1716:                                return true;
1717:                            }
1718:                            if (occupied[faces[i][8]] == HUMAN
1719:                                    && occupied[faces[i][4]] == 0
1720:                                    && occupied[faces[i][11]] == 0
1721:                                    && occupied[faces[i][12]] == 0
1722:                                    && occupied[faces[i][15]] == 0
1723:                                    && occupied[faces[i][16]] == 0) {
1724:                                pos = faces[i][12];
1725:                                occupied[pos] = MACHINE;
1726:                                positions.set(pos, MACHINE);
1727:                                player = update_logic_arrays(pos);
1728:                                if (debug)
1729:                                    System.out
1730:                                            .println("block_chair_move: found");
1731:                                return true;
1732:                            }
1733:                            if (occupied[faces[i][11]] == HUMAN
1734:                                    && occupied[faces[i][3]] == 0
1735:                                    && occupied[faces[i][7]] == 0
1736:                                    && occupied[faces[i][12]] == 0
1737:                                    && occupied[faces[i][15]] == 0
1738:                                    && occupied[faces[i][16]] == 0) {
1739:                                pos = faces[i][7];
1740:                                occupied[pos] = MACHINE;
1741:                                positions.set(pos, MACHINE);
1742:                                player = update_logic_arrays(pos);
1743:                                if (debug)
1744:                                    System.out
1745:                                            .println("block_chair_move: found");
1746:                                return true;
1747:                            }
1748:                        } else if (corner == 14) {
1749:                            if (occupied[faces[i][6]] == HUMAN
1750:                                    && occupied[faces[i][7]] == 0
1751:                                    && occupied[faces[i][8]] == 0
1752:                                    && occupied[faces[i][9]] == 0
1753:                                    && occupied[faces[i][11]] == 0
1754:                                    && occupied[faces[i][13]] == 0) {
1755:                                pos = faces[i][7];
1756:                                occupied[pos] = MACHINE;
1757:                                positions.set(pos, MACHINE);
1758:                                player = update_logic_arrays(pos);
1759:                                if (debug)
1760:                                    System.out
1761:                                            .println("block_chair_move: found");
1762:                                return true;
1763:                            }
1764:                            if (occupied[faces[i][10]] == HUMAN
1765:                                    && occupied[faces[i][8]] == 0
1766:                                    && occupied[faces[i][9]] == 0
1767:                                    && occupied[faces[i][11]] == 0
1768:                                    && occupied[faces[i][12]] == 0
1769:                                    && occupied[faces[i][13]] == 0) {
1770:                                pos = faces[i][12];
1771:                                occupied[pos] = MACHINE;
1772:                                positions.set(pos, MACHINE);
1773:                                player = update_logic_arrays(pos);
1774:                                if (debug)
1775:                                    System.out
1776:                                            .println("block_chair_move: found");
1777:                                return true;
1778:                            }
1779:                            if (occupied[faces[i][15]] == HUMAN
1780:                                    && occupied[faces[i][3]] == 0
1781:                                    && occupied[faces[i][4]] == 0
1782:                                    && occupied[faces[i][7]] == 0
1783:                                    && occupied[faces[i][11]] == 0
1784:                                    && occupied[faces[i][12]] == 0) {
1785:                                pos = faces[i][3];
1786:                                occupied[pos] = MACHINE;
1787:                                positions.set(pos, MACHINE);
1788:                                player = update_logic_arrays(pos);
1789:                                if (debug)
1790:                                    System.out
1791:                                            .println("block_chair_move: found");
1792:                                return true;
1793:                            }
1794:                            if (occupied[faces[i][16]] == HUMAN
1795:                                    && occupied[faces[i][3]] == 0
1796:                                    && occupied[faces[i][4]] == 0
1797:                                    && occupied[faces[i][7]] == 0
1798:                                    && occupied[faces[i][8]] == 0
1799:                                    && occupied[faces[i][12]] == 0) {
1800:                                pos = faces[i][12];
1801:                                occupied[pos] = MACHINE;
1802:                                positions.set(pos, MACHINE);
1803:                                player = update_logic_arrays(pos);
1804:                                if (debug)
1805:                                    System.out
1806:                                            .println("block_chair_move: found");
1807:                                return true;
1808:                            }
1809:                            if (occupied[faces[i][11]] == HUMAN
1810:                                    && occupied[faces[i][3]] == 0
1811:                                    && occupied[faces[i][4]] == 0
1812:                                    && occupied[faces[i][7]] == 0
1813:                                    && occupied[faces[i][12]] == 0
1814:                                    && occupied[faces[i][15]] == 0) {
1815:                                pos = faces[i][7];
1816:                                occupied[pos] = MACHINE;
1817:                                positions.set(pos, MACHINE);
1818:                                player = update_logic_arrays(pos);
1819:                                if (debug)
1820:                                    System.out
1821:                                            .println("block_chair_move: found");
1822:                                return true;
1823:                            }
1824:                            if (occupied[faces[i][8]] == HUMAN
1825:                                    && occupied[faces[i][6]] == 0
1826:                                    && occupied[faces[i][7]] == 0
1827:                                    && occupied[faces[i][9]] == 0
1828:                                    && occupied[faces[i][12]] == 0
1829:                                    && occupied[faces[i][13]] == 0) {
1830:                                pos = faces[i][7];
1831:                                occupied[pos] = MACHINE;
1832:                                positions.set(pos, MACHINE);
1833:                                player = update_logic_arrays(pos);
1834:                                if (debug)
1835:                                    System.out
1836:                                            .println("block_chair_move: found");
1837:                                return true;
1838:                            }
1839:                        } else if (corner == 17) {
1840:                            if (occupied[faces[i][9]] == HUMAN
1841:                                    && occupied[faces[i][6]] == 0
1842:                                    && occupied[faces[i][7]] == 0
1843:                                    && occupied[faces[i][8]] == 0
1844:                                    && occupied[faces[i][10]] == 0
1845:                                    && occupied[faces[i][11]] == 0) {
1846:                                pos = faces[i][8];
1847:                                occupied[pos] = MACHINE;
1848:                                positions.set(pos, MACHINE);
1849:                                player = update_logic_arrays(pos);
1850:                                if (debug)
1851:                                    System.out
1852:                                            .println("block_chair_move: found");
1853:                                return true;
1854:                            }
1855:                            if (occupied[faces[i][13]] == HUMAN
1856:                                    && occupied[faces[i][6]] == 0
1857:                                    && occupied[faces[i][8]] == 0
1858:                                    && occupied[faces[i][10]] == 0
1859:                                    && occupied[faces[i][11]] == 0
1860:                                    && occupied[faces[i][12]] == 0) {
1861:                                pos = faces[i][11];
1862:                                occupied[pos] = MACHINE;
1863:                                positions.set(pos, MACHINE);
1864:                                player = update_logic_arrays(pos);
1865:                                if (debug)
1866:                                    System.out
1867:                                            .println("block_chair_move: found");
1868:                                return true;
1869:                            }
1870:                            if (occupied[faces[i][15]] == HUMAN
1871:                                    && occupied[faces[i][3]] == 0
1872:                                    && occupied[faces[i][4]] == 0
1873:                                    && occupied[faces[i][7]] == 0
1874:                                    && occupied[faces[i][8]] == 0
1875:                                    && occupied[faces[i][11]] == 0) {
1876:                                pos = faces[i][11];
1877:                                occupied[pos] = MACHINE;
1878:                                positions.set(pos, MACHINE);
1879:                                player = update_logic_arrays(pos);
1880:                                if (debug)
1881:                                    System.out
1882:                                            .println("block_chair_move: found");
1883:                                return true;
1884:                            }
1885:                            if (occupied[faces[i][16]] == HUMAN
1886:                                    && occupied[faces[i][3]] == 0
1887:                                    && occupied[faces[i][4]] == 0
1888:                                    && occupied[faces[i][8]] == 0
1889:                                    && occupied[faces[i][11]] == 0
1890:                                    && occupied[faces[i][12]] == 0) {
1891:                                pos = faces[i][8];
1892:                                occupied[pos] = MACHINE;
1893:                                positions.set(pos, MACHINE);
1894:                                player = update_logic_arrays(pos);
1895:                                if (debug)
1896:                                    System.out
1897:                                            .println("block_chair_move: found");
1898:                                return true;
1899:                            }
1900:                            if (occupied[faces[i][12]] == HUMAN
1901:                                    && occupied[faces[i][3]] == 0
1902:                                    && occupied[faces[i][4]] == 0
1903:                                    && occupied[faces[i][8]] == 0
1904:                                    && occupied[faces[i][11]] == 0
1905:                                    && occupied[faces[i][16]] == 0) {
1906:                                pos = faces[i][8];
1907:                                occupied[pos] = MACHINE;
1908:                                positions.set(pos, MACHINE);
1909:                                player = update_logic_arrays(pos);
1910:                                if (debug)
1911:                                    System.out
1912:                                            .println("block_chair_move: found");
1913:                                return true;
1914:                            }
1915:                            if (occupied[faces[i][7]] == HUMAN
1916:                                    && occupied[faces[i][3]] == 0
1917:                                    && occupied[faces[i][4]] == 0
1918:                                    && occupied[faces[i][8]] == 0
1919:                                    && occupied[faces[i][11]] == 0
1920:                                    && occupied[faces[i][15]] == 0) {
1921:                                pos = faces[i][11];
1922:                                occupied[pos] = MACHINE;
1923:                                positions.set(pos, MACHINE);
1924:                                player = update_logic_arrays(pos);
1925:                                if (debug)
1926:                                    System.out
1927:                                            .println("block_chair_move: found");
1928:                                return true;
1929:                            }
1930:                        }
1931:                    }
1932:                    ncorners = 0;
1933:                    corner = -1;
1934:                }
1935:                if (debug)
1936:                    System.out.println("block_chair_move: false");
1937:                return false;
1938:            }
1939:
1940:            public boolean block_walk_move() {
1941:
1942:                int pos;
1943:
1944:                if (debug)
1945:                    System.out.println("inside block_walk_move");
1946:
1947:                // Loop through all of the faces.
1948:                for (int i = 0; i < 18; i++) {
1949:
1950:                    // Look for a matching pattern.
1951:                    if (occupied[faces[i][2]] == HUMAN
1952:                            && occupied[faces[i][14]] == HUMAN
1953:                            && occupied[faces[i][3]] == HUMAN
1954:                            && occupied[faces[i][15]] == HUMAN
1955:                            && occupied[faces[i][6]] == 0
1956:                            && occupied[faces[i][10]] == 0
1957:                            && occupied[faces[i][7]] == 0
1958:                            && occupied[faces[i][11]] == 0) {
1959:
1960:                        if (occupied[faces[i][8]] == HUMAN
1961:                                && occupied[faces[i][9]] == 0) {
1962:                            pos = faces[i][6];
1963:                            occupied[pos] = MACHINE;
1964:                            positions.set(pos, MACHINE);
1965:                            player = update_logic_arrays(pos);
1966:                            return true;
1967:                        } else if (occupied[faces[i][12]] == HUMAN
1968:                                && occupied[faces[i][13]] == 0) {
1969:                            pos = faces[i][10];
1970:                            occupied[pos] = MACHINE;
1971:                            positions.set(pos, MACHINE);
1972:                            player = update_logic_arrays(pos);
1973:                            return true;
1974:                        }
1975:                    }
1976:
1977:                    // Look for a matching pattern.
1978:                    if (occupied[faces[i][14]] == HUMAN
1979:                            && occupied[faces[i][17]] == HUMAN
1980:                            && occupied[faces[i][10]] == HUMAN
1981:                            && occupied[faces[i][13]] == HUMAN
1982:                            && occupied[faces[i][15]] == 0
1983:                            && occupied[faces[i][16]] == 0
1984:                            && occupied[faces[i][11]] == 0
1985:                            && occupied[faces[i][12]] == 0) {
1986:
1987:                        if (occupied[faces[i][7]] == HUMAN
1988:                                && occupied[faces[i][3]] == 0) {
1989:                            pos = faces[i][15];
1990:                            occupied[pos] = MACHINE;
1991:                            positions.set(pos, MACHINE);
1992:                            player = update_logic_arrays(pos);
1993:                            return true;
1994:                        } else if (occupied[faces[i][8]] == HUMAN
1995:                                && occupied[faces[i][4]] == 0) {
1996:                            pos = faces[i][16];
1997:                            occupied[pos] = MACHINE;
1998:                            positions.set(pos, MACHINE);
1999:                            player = update_logic_arrays(pos);
2000:                            return true;
2001:                        }
2002:                    }
2003:
2004:                    // Look for a matching pattern.
2005:                    if (occupied[faces[i][4]] == HUMAN
2006:                            && occupied[faces[i][16]] == HUMAN
2007:                            && occupied[faces[i][5]] == HUMAN
2008:                            && occupied[faces[i][17]] == HUMAN
2009:                            && occupied[faces[i][8]] == 0
2010:                            && occupied[faces[i][12]] == 0
2011:                            && occupied[faces[i][9]] == 0
2012:                            && occupied[faces[i][13]] == 0) {
2013:
2014:                        if (occupied[faces[i][11]] == HUMAN
2015:                                && occupied[faces[i][10]] == 0) {
2016:                            pos = faces[i][18];
2017:                            occupied[pos] = MACHINE;
2018:                            positions.set(pos, MACHINE);
2019:                            player = update_logic_arrays(pos);
2020:                            return true;
2021:                        } else if (occupied[faces[i][7]] == HUMAN
2022:                                && occupied[faces[i][6]] == 0) {
2023:                            pos = faces[i][9];
2024:                            occupied[pos] = MACHINE;
2025:                            positions.set(pos, MACHINE);
2026:                            player = update_logic_arrays(pos);
2027:                            return true;
2028:                        }
2029:                    }
2030:
2031:                    // Look for a matching pattern.
2032:                    if (occupied[faces[i][6]] == HUMAN
2033:                            && occupied[faces[i][9]] == HUMAN
2034:                            && occupied[faces[i][2]] == HUMAN
2035:                            && occupied[faces[i][5]] == HUMAN
2036:                            && occupied[faces[i][7]] == 0
2037:                            && occupied[faces[i][8]] == 0
2038:                            && occupied[faces[i][3]] == 0
2039:                            && occupied[faces[i][4]] == 0) {
2040:
2041:                        if (occupied[faces[i][11]] == HUMAN
2042:                                && occupied[faces[i][15]] == 0) {
2043:                            pos = faces[i][3];
2044:                            occupied[pos] = MACHINE;
2045:                            positions.set(pos, MACHINE);
2046:                            player = update_logic_arrays(pos);
2047:                            return true;
2048:                        } else if (occupied[faces[i][12]] == HUMAN
2049:                                && occupied[faces[i][16]] == 0) {
2050:                            pos = faces[i][4];
2051:                            occupied[pos] = MACHINE;
2052:                            positions.set(pos, MACHINE);
2053:                            player = update_logic_arrays(pos);
2054:                            return true;
2055:                        }
2056:                    }
2057:
2058:                    // Look for a matching pattern.
2059:                    if (occupied[faces[i][2]] == HUMAN
2060:                            && occupied[faces[i][14]] == HUMAN
2061:                            && occupied[faces[i][4]] == HUMAN
2062:                            && occupied[faces[i][16]] == HUMAN
2063:                            && occupied[faces[i][6]] == 0
2064:                            && occupied[faces[i][10]] == 0
2065:                            && occupied[faces[i][8]] == 0
2066:                            && occupied[faces[i][12]] == 0) {
2067:
2068:                        if ((occupied[faces[i][7]] == HUMAN && occupied[faces[i][9]] == 0)
2069:                                || (occupied[faces[i][9]] == HUMAN && occupied[faces[i][7]] == 0)) {
2070:                            pos = faces[i][6];
2071:                            occupied[pos] = MACHINE;
2072:                            positions.set(pos, MACHINE);
2073:                            player = update_logic_arrays(pos);
2074:                            return true;
2075:                        } else if ((occupied[faces[i][11]] == HUMAN && occupied[faces[i][13]] == 0)
2076:                                || (occupied[faces[i][13]] == HUMAN && occupied[faces[i][11]] == 0)) {
2077:                            pos = faces[i][10];
2078:                            occupied[pos] = MACHINE;
2079:                            positions.set(pos, MACHINE);
2080:                            player = update_logic_arrays(pos);
2081:                            return true;
2082:                        }
2083:                    }
2084:
2085:                    // Look for a matching pattern.
2086:                    if (occupied[faces[i][14]] == HUMAN
2087:                            && occupied[faces[i][17]] == HUMAN
2088:                            && occupied[faces[i][6]] == HUMAN
2089:                            && occupied[faces[i][9]] == HUMAN
2090:                            && occupied[faces[i][15]] == 0
2091:                            && occupied[faces[i][16]] == 0
2092:                            && occupied[faces[i][7]] == 0
2093:                            && occupied[faces[i][8]] == 0) {
2094:
2095:                        if ((occupied[faces[i][11]] == HUMAN && occupied[faces[i][3]] == 0)
2096:                                || (occupied[faces[i][3]] == HUMAN && occupied[faces[i][11]] == 0)) {
2097:                            pos = faces[i][15];
2098:                            occupied[pos] = MACHINE;
2099:                            positions.set(pos, MACHINE);
2100:                            player = update_logic_arrays(pos);
2101:                            return true;
2102:                        } else if ((occupied[faces[i][12]] == HUMAN && occupied[faces[i][4]] == 0)
2103:                                || (occupied[faces[i][4]] == HUMAN && occupied[faces[i][12]] == 0)) {
2104:                            pos = faces[i][16];
2105:                            occupied[pos] = MACHINE;
2106:                            positions.set(pos, MACHINE);
2107:                            player = update_logic_arrays(pos);
2108:                            return true;
2109:                        }
2110:                    }
2111:
2112:                    // Look for a matching pattern.
2113:                    if (occupied[faces[i][3]] == HUMAN
2114:                            && occupied[faces[i][15]] == HUMAN
2115:                            && occupied[faces[i][5]] == HUMAN
2116:                            && occupied[faces[i][17]] == HUMAN
2117:                            && occupied[faces[i][7]] == 0
2118:                            && occupied[faces[i][11]] == 0
2119:                            && occupied[faces[i][9]] == 0
2120:                            && occupied[faces[i][13]] == 0) {
2121:
2122:                        if ((occupied[faces[i][6]] == HUMAN && occupied[faces[i][8]] == 0)
2123:                                || (occupied[faces[i][8]] == HUMAN && occupied[faces[i][6]] == 0)) {
2124:                            pos = faces[i][9];
2125:                            occupied[pos] = MACHINE;
2126:                            positions.set(pos, MACHINE);
2127:                            player = update_logic_arrays(pos);
2128:                            return true;
2129:                        } else if ((occupied[faces[i][10]] == HUMAN && occupied[faces[i][12]] == 0)
2130:                                || (occupied[faces[i][12]] == HUMAN && occupied[faces[i][10]] == 0)) {
2131:                            pos = faces[i][13];
2132:                            occupied[pos] = MACHINE;
2133:                            positions.set(pos, MACHINE);
2134:                            player = update_logic_arrays(pos);
2135:                            return true;
2136:                        }
2137:                    }
2138:
2139:                    // Look for a matching pattern.
2140:                    if (occupied[faces[i][10]] == HUMAN
2141:                            && occupied[faces[i][13]] == HUMAN
2142:                            && occupied[faces[i][2]] == HUMAN
2143:                            && occupied[faces[i][5]] == HUMAN
2144:                            && occupied[faces[i][11]] == 0
2145:                            && occupied[faces[i][12]] == 0
2146:                            && occupied[faces[i][3]] == 0
2147:                            && occupied[faces[i][4]] == 0) {
2148:
2149:                        if ((occupied[faces[i][7]] == HUMAN && occupied[faces[i][15]] == 0)
2150:                                || (occupied[faces[i][15]] == HUMAN && occupied[faces[i][7]] == 0)) {
2151:                            pos = faces[i][3];
2152:                            occupied[pos] = MACHINE;
2153:                            positions.set(pos, MACHINE);
2154:                            player = update_logic_arrays(pos);
2155:                            return true;
2156:                        } else if ((occupied[faces[i][8]] == HUMAN && occupied[faces[i][16]] == 0)
2157:                                || (occupied[faces[i][16]] == HUMAN && occupied[faces[i][8]] == 0)) {
2158:                            pos = faces[i][4];
2159:                            occupied[pos] = MACHINE;
2160:                            positions.set(pos, MACHINE);
2161:                            player = update_logic_arrays(pos);
2162:                            return true;
2163:                        }
2164:                    }
2165:
2166:                }
2167:
2168:                if (debug)
2169:                    System.out.println("block_walk_move: false");
2170:                return false;
2171:            }
2172:
2173:            public boolean check_chair_move() {
2174:
2175:                int pos;
2176:
2177:                // If the "block chair flag" is set, all we need to do is
2178:                // block the winning path...
2179:                if (block_chair_flag) {
2180:                    pos = faces[block_chair_face][block_chair_next_move];
2181:                    occupied[pos] = MACHINE;
2182:                    positions.set(pos, MACHINE);
2183:                    player = update_logic_arrays(pos);
2184:                    if (debug)
2185:                        System.out.println("block_chair_move: march");
2186:                    return true;
2187:                }
2188:
2189:                int ncorners = 0; // Number of corners owned by human
2190:                int corner = 0; // Corner owned by machine
2191:
2192:                // Loop through all of the faces.
2193:                for (int i = 0; i < 18; i++) {
2194:
2195:                    // Determine which corners the human owns.
2196:                    if (faces[i][2] == HUMAN)
2197:                        ncorners++;
2198:                    else
2199:                        corner = 2;
2200:                    if (faces[i][5] == HUMAN)
2201:                        ncorners++;
2202:                    else
2203:                        corner = 5;
2204:                    if (faces[i][14] == HUMAN)
2205:                        ncorners++;
2206:                    else
2207:                        corner = 14;
2208:                    if (faces[i][17] == HUMAN)
2209:                        ncorners++;
2210:                    else
2211:                        corner = 17;
2212:
2213:                    // If the human owns three corners, continue with the search.
2214:                    if (ncorners == 3) {
2215:                        if (corner == 2) {
2216:                            if (faces[i][3] == HUMAN && faces[i][7] == 0
2217:                                    && faces[i][8] == 0 && faces[i][11] == 0
2218:                                    && faces[i][15] == 0 && faces[i][16] == 0) {
2219:                                block_chair_flag = true;
2220:                                block_chair_next_move = 11;
2221:                                block_chair_face = i;
2222:                                pos = faces[i][15];
2223:                                occupied[pos] = MACHINE;
2224:                                positions.set(pos, MACHINE);
2225:                                player = update_logic_arrays(pos);
2226:                                if (debug)
2227:                                    System.out
2228:                                            .println("block_chair_move: found");
2229:                                return true;
2230:                            }
2231:                            if (faces[i][4] == HUMAN && faces[i][8] == 0
2232:                                    && faces[i][11] == 0 && faces[i][12] == 0
2233:                                    && faces[i][15] == 0 && faces[i][16] == 0) {
2234:                                block_chair_flag = true;
2235:                                block_chair_next_move = 16;
2236:                                block_chair_face = i;
2237:                                pos = faces[i][8];
2238:                                occupied[pos] = MACHINE;
2239:                                positions.set(pos, MACHINE);
2240:                                player = update_logic_arrays(pos);
2241:                                if (debug)
2242:                                    System.out
2243:                                            .println("block_chair_move: found");
2244:                                return true;
2245:                            }
2246:                        } else if (corner == 5) {
2247:                            block_chair_flag = true;
2248:                            block_chair_next_move = 11;
2249:                            block_chair_face = i;
2250:                            pos = faces[i][15];
2251:                            occupied[pos] = MACHINE;
2252:                            positions.set(pos, MACHINE);
2253:                            player = update_logic_arrays(pos);
2254:                            if (debug)
2255:                                System.out.println("check_face_three:  true");
2256:                            return true;
2257:                        } else if (corner == 14) {
2258:                            block_chair_flag = true;
2259:                            block_chair_next_move = 11;
2260:                            block_chair_face = i;
2261:                            pos = faces[i][15];
2262:                            occupied[pos] = MACHINE;
2263:                            positions.set(pos, MACHINE);
2264:                            player = update_logic_arrays(pos);
2265:                            if (debug)
2266:                                System.out.println("check_face_three:  true");
2267:                            return true;
2268:                        } else if (corner == 17) {
2269:                            block_chair_flag = true;
2270:                            block_chair_next_move = 11;
2271:                            block_chair_face = i;
2272:                            pos = faces[i][15];
2273:                            occupied[pos] = MACHINE;
2274:                            positions.set(pos, MACHINE);
2275:                            player = update_logic_arrays(pos);
2276:                            if (debug)
2277:                                System.out.println("check_face_three:  true");
2278:                            return true;
2279:                        }
2280:                    }
2281:                }
2282:                return false;
2283:            }
2284:
2285:            /**
2286:             *  Take inside four
2287:             */
2288:            public boolean take_inside_four() {
2289:
2290:                int pos = 0;
2291:                boolean found = false;
2292:
2293:                if (occupied[21] == 0) {
2294:                    found = true;
2295:                    pos = 21;
2296:                } else if (occupied[22] == 0) {
2297:                    found = true;
2298:                    pos = 22;
2299:                } else if (occupied[25] == 0) {
2300:                    found = true;
2301:                    pos = 25;
2302:                } else if (occupied[26] == 0) {
2303:                    found = true;
2304:                    pos = 26;
2305:                } else if (occupied[37] == 0) {
2306:                    found = true;
2307:                    pos = 37;
2308:                } else if (occupied[38] == 0) {
2309:                    found = true;
2310:                    pos = 38;
2311:                } else if (occupied[41] == 0) {
2312:                    found = true;
2313:                    pos = 41;
2314:                } else if (occupied[42] == 0) {
2315:                    found = true;
2316:                    pos = 42;
2317:                }
2318:
2319:                if (found) {
2320:                    occupied[pos] = MACHINE;
2321:                    positions.set(pos, MACHINE);
2322:                    player = update_logic_arrays(pos);
2323:                    if (debug)
2324:                        System.out.println("take_inside_four:  true");
2325:                    return true;
2326:                }
2327:
2328:                if (debug)
2329:                    System.out.println("take_inside_four:  false");
2330:                return false;
2331:            }
2332:
2333:            /**
2334:             *  Check occupancy of outside four.
2335:             */
2336:            public boolean check_outside_four() {
2337:
2338:                int pos = 0;
2339:
2340:                // Finish off the four corner combination.
2341:                if (outside_four_flag) {
2342:                    if (occupied[faces[face_index][7]] == 0) {
2343:                        pos = faces[face_index][7];
2344:                    } else if (occupied[faces[face_index][6]] == 0) {
2345:                        pos = faces[face_index][6];
2346:                    }
2347:
2348:                    if (occupied[pos] == 0) {
2349:                        occupied[pos] = MACHINE;
2350:                        positions.set(pos, MACHINE);
2351:                        player = update_logic_arrays(pos);
2352:                        return true;
2353:                    }
2354:                }
2355:
2356:                // Look for a four corner combination.
2357:                for (int i = 0; i < 18; i++) {
2358:                    if (outside_four[i][0] == 4
2359:                            && outside_four[i][1] == MACHINE) {
2360:                        if (faces[i][0] > 0 && faces[i][1] == MACHINE) {
2361:                            if (occupied[faces[i][8]] == 0) {
2362:                                pos = faces[i][8];
2363:                                outside_four_flag = true;
2364:                                face_index = i;
2365:                            }
2366:                            if (occupied[pos] == 0) {
2367:                                occupied[pos] = MACHINE;
2368:                                positions.set(pos, MACHINE);
2369:                                player = update_logic_arrays(pos);
2370:                                if (debug)
2371:                                    System.out
2372:                                            .println("check_outside_four:  true");
2373:                                return true;
2374:                            }
2375:                        }
2376:                    }
2377:                }
2378:
2379:                // Take the corners, if available.
2380:                for (int i = 0; i < 18; i++) {
2381:                    if (outside_four[i][0] > 0 && outside_four[i][1] == MACHINE) {
2382:                        if (faces[i][0] > 0 && faces[i][1] == MACHINE) {
2383:                            for (int j = 2; j < 6; j++) {
2384:                                pos = outside_four[i][j];
2385:                                if (occupied[pos] == 0) {
2386:                                    occupied[pos] = MACHINE;
2387:                                    positions.set(pos, MACHINE);
2388:                                    player = update_logic_arrays(pos);
2389:                                    if (debug)
2390:                                        System.out
2391:                                                .println("check_outside_four:  true");
2392:                                    return true;
2393:                                }
2394:                            }
2395:                        }
2396:                    }
2397:                }
2398:
2399:                // Look for an "outside four" combination in a face in which the 
2400:                // opponent holds no positions.
2401:                for (int i = 0; i < 18; i++) {
2402:                    if (outside_four[i][0] == 0
2403:                            || (outside_four[i][0] > 0 && outside_four[i][1] == MACHINE)) {
2404:
2405:                        if (outside_four[i][1] == MACHINE)
2406:                            outside_four_flag = true;
2407:                        for (int j = 2; j < 6; j++) {
2408:                            pos = outside_four[i][j];
2409:                            if (occupied[pos] == 0) {
2410:                                occupied[pos] = MACHINE;
2411:                                positions.set(pos, MACHINE);
2412:                                player = update_logic_arrays(pos);
2413:                                if (debug)
2414:                                    System.out
2415:                                            .println("check_outside_four:  true");
2416:                                return true;
2417:                            }
2418:                        }
2419:                    }
2420:                }
2421:
2422:                if (debug)
2423:                    System.out.println("check_outside_four:  false");
2424:                return false;
2425:            }
2426:
2427:            /**
2428:             *  Take outside four
2429:             */
2430:            public boolean take_outside_four() {
2431:
2432:                int pos = 0;
2433:                boolean found = false;
2434:
2435:                if (occupied[0] == 0) {
2436:                    found = true;
2437:                    pos = 0;
2438:                } else if (occupied[3] == 0) {
2439:                    found = true;
2440:                    pos = 3;
2441:                } else if (occupied[12] == 0) {
2442:                    found = true;
2443:                    pos = 12;
2444:                } else if (occupied[15] == 0) {
2445:                    found = true;
2446:                    pos = 15;
2447:                } else if (occupied[48] == 0) {
2448:                    found = true;
2449:                    pos = 48;
2450:                } else if (occupied[51] == 0) {
2451:                    found = true;
2452:                    pos = 51;
2453:                } else if (occupied[60] == 0) {
2454:                    found = true;
2455:                    pos = 60;
2456:                } else if (occupied[63] == 0) {
2457:                    found = true;
2458:                    pos = 63;
2459:                }
2460:
2461:                if (found) {
2462:                    occupied[pos] = MACHINE;
2463:                    positions.set(pos, MACHINE);
2464:                    player = update_logic_arrays(pos);
2465:                    if (debug)
2466:                        System.out.println("take_outside_four:  true");
2467:                    return true;
2468:                }
2469:
2470:                if (debug)
2471:                    System.out.println("take_outside_four:  false");
2472:                return false;
2473:            }
2474:
2475:            /**
2476:             *  Check for a forced win by intersecting rows. Block
2477:             *  if necessary.
2478:             */
2479:            public boolean block_intersecting_rows() {
2480:
2481:                int pos;
2482:
2483:                // Loop through each row and check for rows that have two
2484:                // positions occupied by the human and two positions which are empty.
2485:                // Make sure that none of the empty positions in this row intersect 
2486:                // with another row that also contains two positions held by the human. 
2487:                // If so, block the row by taking the position at the intersection
2488:                // of these two row.
2489:
2490:                // Loop through each row.
2491:                for (int i = 0; i < 76; i++) {
2492:
2493:                    // Look for a row that has two positions held by the human. 
2494:                    if (combinations[i][0] == 2 && combinations[i][1] == HUMAN) {
2495:
2496:                        if (debug)
2497:                            System.out.println("   row " + i
2498:                                    + "has 2 positions occupied by the human");
2499:
2500:                        // Mark this row with a flag.
2501:                        combinations[i][6] = 1;
2502:
2503:                        // Check each position in the row.
2504:                        for (int j = 2; j < 6; j++) {
2505:
2506:                            // Look for the empty positions in the row.
2507:                            pos = combinations[i][j];
2508:                            if (occupied[pos] == 0) {
2509:
2510:                                // Loop through the rows again.
2511:                                for (int k = 0; k < 76; k++) {
2512:
2513:                                    if (debug)
2514:                                        System.out.println("   row " + k);
2515:
2516:                                    // Look for another row that has two positions held
2517:                                    // by the human (and which is unmarked.) modified
2518:                                    if (combinations[k][0] == 2
2519:                                            && combinations[k][1] == HUMAN
2520:                                            && combinations[k][6] == 0) {
2521:
2522:                                        if (debug)
2523:                                            System.out
2524:                                                    .println("found an intersecting row:   row "
2525:                                                            + k);
2526:
2527:                                        // Check the positions in this row and see if
2528:                                        // any match the position we're looking for. If
2529:                                        // we find a match, grab the position and return.
2530:                                        for (int l = 2; l < 6; l++) {
2531:                                            if (pos == combinations[k][l]) {
2532:                                                combinations[i][6] = 0;
2533:                                                occupied[pos] = MACHINE;
2534:                                                positions.set(pos, MACHINE);
2535:                                                player = update_logic_arrays(pos);
2536:                                                if (debug)
2537:                                                    System.out
2538:                                                            .println("block_intersecting_rows:  true");
2539:                                                return true;
2540:                                            }
2541:                                        }
2542:                                    }
2543:                                }
2544:                            }
2545:                        }
2546:
2547:                        // Unmark the combination before moving on.
2548:                        combinations[i][6] = 0;
2549:                    }
2550:
2551:                }
2552:                if (debug)
2553:                    System.out.println("block_intersecting_rows:  false");
2554:                return false;
2555:            }
2556:
2557:            /**
2558:             *  Check for a forced win by intersecting rows. Block
2559:             *  if necessary.
2560:             */
2561:            public boolean check_intersecting_rows2() {
2562:
2563:                int pos;
2564:
2565:                // Loop through each row and check for rows that have two
2566:                // positions occupied by the human and two positions which are empty.
2567:                // Make sure that none of the empty positions in this row intersect 
2568:                // with another row that also contains two positions held by the human. 
2569:                // If so, block the row by taking the position at the intersection
2570:                // of these two row.
2571:
2572:                // Loop through each row.
2573:                for (int i = 0; i < 76; i++) {
2574:
2575:                    // Look for a row that has two positions held by the human. 
2576:                    if (combinations[i][0] == 2 && combinations[i][1] == HUMAN) {
2577:
2578:                        if (debug) {
2579:                            System.out.println("   row " + i
2580:                                    + "has 2 positions occupied by the human");
2581:                        }
2582:
2583:                        // Mark this row with a flag.
2584:                        combinations[i][6] = 1;
2585:
2586:                        // Check each position in the row.
2587:                        for (int j = 2; j < 6; j++) {
2588:
2589:                            // Look for the empty positions in the row.
2590:                            pos = combinations[i][j];
2591:                            if (occupied[pos] == 0) {
2592:
2593:                                // Loop through the rows again.
2594:                                for (int k = 0; k < 76; k++) {
2595:
2596:                                    if (debug)
2597:                                        System.out.println("   row " + k);
2598:
2599:                                    // Look for another row that has two positions held
2600:                                    // by the human (and which is unmarked.) modified
2601:                                    if (combinations[k][0] == 1
2602:                                            && combinations[k][1] == HUMAN
2603:                                            && combinations[k][6] == 0) {
2604:
2605:                                        if (debug)
2606:                                            System.out
2607:                                                    .println("found an intersecting row:   row "
2608:                                                            + k);
2609:
2610:                                        // Check the positions in this row and see if
2611:                                        // any match the position we're looking for. If
2612:                                        // we find a match, grab the position and return.
2613:                                        for (int l = 2; l < 6; l++) {
2614:                                            if (pos == combinations[k][l]) {
2615:                                                combinations[i][6] = 0;
2616:                                                occupied[pos] = MACHINE;
2617:                                                positions.set(pos, MACHINE);
2618:                                                player = update_logic_arrays(pos);
2619:                                                if (debug)
2620:                                                    System.out
2621:                                                            .println("check_intersecting_rows:  true");
2622:                                                return true;
2623:                                            }
2624:                                        }
2625:                                    }
2626:                                }
2627:                            }
2628:                        }
2629:
2630:                        // Unmark the combination before moving on.
2631:                        combinations[i][6] = 0;
2632:                    }
2633:
2634:                }
2635:                if (debug)
2636:                    System.out.println("check_intersecting_rows:  false");
2637:                return false;
2638:            }
2639:
2640:            /**
2641:             *  Check for a forced win by intersecting rows. Block
2642:             *  if necessary.
2643:             */
2644:            public boolean check_for_two() {
2645:
2646:                int pos;
2647:
2648:                // Loop through the rows.
2649:                for (int i = 0; i < 76; i++) {
2650:
2651:                    // Look for a row that has two positions held
2652:                    // by the human (and which is unmarked.)
2653:                    if (combinations[i][0] == 2 && combinations[i][1] == HUMAN
2654:                            && combinations[i][6] == 0) {
2655:
2656:                        // Take the first available spot.
2657:                        for (int j = 2; j < 6; j++) {
2658:                            pos = combinations[i][j];
2659:                            if (occupied[pos] == 0) {
2660:                                occupied[pos] = MACHINE;
2661:                                positions.set(pos, MACHINE);
2662:                                player = update_logic_arrays(pos);
2663:                                if (debug)
2664:                                    System.out.println("check_for_two:  true");
2665:                                return true;
2666:                            }
2667:                        }
2668:
2669:                    }
2670:                }
2671:                if (debug)
2672:                    System.out.println("check_for_two:  false");
2673:                return false;
2674:            }
2675:
2676:            public void undo_move() {
2677:
2678:                // Return if no moves are recorded
2679:                if (nmoves == 0)
2680:                    return;
2681:
2682:                // Set the undo flag
2683:                undoFlag = true;
2684:
2685:                // Undo the last two moves
2686:                positions.clear(moves[--nmoves]);
2687:                positions.clear(moves[--nmoves]);
2688:
2689:                // Undo the winner flag in the positions object
2690:                positions.noWinner();
2691:
2692:                // Repaint the 2D canvas.
2693:                canvas.repaint();
2694:
2695:                // Reset the inside/outside flags
2696:                inside_four_flag = false;
2697:                outside_four_flag = false;
2698:                block_chair_flag = false;
2699:
2700:                // Reset the board
2701:                for (int i = 0; i < 64; i++) {
2702:                    occupied[i] = 0;
2703:                }
2704:
2705:                // Reset the inside/outside arrays
2706:                for (int i = 0; i < 18; i++) {
2707:                    inside_four[i][0] = 0;
2708:                    inside_four[i][1] = 0;
2709:                    outside_four[i][0] = 0;
2710:                    outside_four[i][1] = 0;
2711:                }
2712:
2713:                // Reset the faces array
2714:                for (int i = 0; i < 18; i++) {
2715:                    faces[i][0] = 0;
2716:                    faces[i][1] = 0;
2717:                }
2718:
2719:                // Reset the combinations array
2720:                for (int i = 0; i < 76; i++) {
2721:                    combinations[i][0] = 0;
2722:                    combinations[i][1] = 0;
2723:                }
2724:
2725:                if (nmoves == 0) {
2726:                    undoFlag = false;
2727:                    player = HUMAN;
2728:                    return;
2729:                }
2730:
2731:                // Update the logic arrays
2732:                int pos;
2733:                player = HUMAN;
2734:                for (int i = 0; i < nmoves; i++) {
2735:                    pos = moves[i];
2736:                    occupied[pos] = player;
2737:                    player = update_logic_arrays(pos);
2738:                }
2739:
2740:                // Reset the "best picks" array
2741:                update_best_picks();
2742:
2743:                // Reset the player and undo flag
2744:                player = HUMAN;
2745:                undoFlag = false;
2746:            }
2747:
2748:            /**
2749:             *  Update the logic arrays that keep track of positions and status.
2750:             *  If we have a winner, stop the game.
2751:             */
2752:            public int update_logic_arrays(int pos) {
2753:
2754:                // Record the move.
2755:                if (!undoFlag) {
2756:                    moves[nmoves++] = pos;
2757:                }
2758:
2759:                // Get the number of combinations that this position has.
2760:                int num_combinations = pos_to_comb[pos][0];
2761:
2762:                // Go through each combination associated with this position 
2763:                // and update the status. If we have a winner, stop the game.
2764:                int comb;
2765:                for (int j = 0; j < num_combinations; j++) {
2766:                    comb = pos_to_comb[pos][j + 1];
2767:                    if (combinations[comb][1] != player
2768:                            && combinations[comb][1] != 0) {
2769:                        combinations[comb][0] = -1;
2770:                    } else {
2771:                        combinations[comb][0]++;
2772:                        if (combinations[comb][0] == 4) {
2773:                            end_time = System.currentTimeMillis();
2774:                            time = (end_time - beg_time) / 1000;
2775:                            panel.winner(player, skill_level, nmoves, time);
2776:                            panel.repaint();
2777:                            canvas.repaint();
2778:                            positions.winner();
2779:                            return END;
2780:                        } else {
2781:                            combinations[comb][1] = player;
2782:                        }
2783:                    }
2784:                }
2785:
2786:                // Update the best_picks array.
2787:                update_best_picks();
2788:
2789:                // Update the inside_four array.
2790:                for (int i = 0; i < 18; i++) {
2791:                    for (int j = 2; j < 6; j++) {
2792:                        if (pos == inside_four[i][j]) {
2793:                            if (inside_four[i][0] == 0) {
2794:                                inside_four[i][0] = 1;
2795:                                inside_four[i][1] = player;
2796:                            } else if (inside_four[i][1] == player) {
2797:                                inside_four[i][0]++;
2798:                                inside_four[i][1] = player;
2799:                            } else {
2800:                                inside_four[i][0] = -1;
2801:                            }
2802:                        }
2803:                    }
2804:                }
2805:
2806:                // Update the outside_four array.
2807:                for (int i = 0; i < 18; i++) {
2808:                    for (int j = 2; j < 6; j++) {
2809:                        if (pos == outside_four[i][j]) {
2810:                            if (outside_four[i][0] == 0) {
2811:                                outside_four[i][0] = 1;
2812:                                outside_four[i][1] = player;
2813:                            } else if (outside_four[i][1] == player) {
2814:                                outside_four[i][0]++;
2815:                                outside_four[i][1] = player;
2816:                            } else {
2817:                                outside_four[i][0] = -1;
2818:                            }
2819:                        }
2820:                    }
2821:                }
2822:
2823:                // Update the faces array.
2824:                for (int i = 0; i < 18; i++) {
2825:                    for (int j = 2; j < 18; j++) {
2826:                        if (pos == faces[i][j]) {
2827:                            if (faces[i][0] == 0) {
2828:                                faces[i][0] = 1;
2829:                                faces[i][1] = player;
2830:                            } else if (faces[i][1] == player) {
2831:                                faces[i][0]++;
2832:                            } else {
2833:                                faces[i][0] = -1;
2834:                            }
2835:                        }
2836:                    }
2837:
2838:                }
2839:
2840:                // Switch players.
2841:                if (player == HUMAN)
2842:                    return MACHINE;
2843:                else
2844:                    return HUMAN;
2845:            }
2846:
2847:            /**
2848:             *  Start a new game.
2849:             */
2850:            public void newGame() {
2851:
2852:                // Initialize the inside/outside flags.
2853:                inside_four_flag = false;
2854:                outside_four_flag = false;
2855:                block_chair_flag = false;
2856:
2857:                // Initialize the inside/outside arrays.
2858:                for (int i = 0; i < 18; i++) {
2859:                    inside_four[i][0] = 0;
2860:                    inside_four[i][1] = 0;
2861:                    outside_four[i][0] = 0;
2862:                    outside_four[i][1] = 0;
2863:                }
2864:
2865:                // Initialize the faces array.
2866:                for (int i = 0; i < 18; i++) {
2867:                    faces[i][0] = 0;
2868:                    faces[i][1] = 0;
2869:                }
2870:
2871:                // Initialize the board.
2872:                for (int i = 0; i < 64; i++) {
2873:                    occupied[i] = 0;
2874:                }
2875:                for (int i = 0; i < 76; i++) {
2876:                    combinations[i][0] = 0;
2877:                    combinations[i][1] = 0;
2878:                }
2879:
2880:                // Reset the best_picks array.
2881:                update_best_picks();
2882:
2883:                // Set the player with the first move.
2884:                player = HUMAN;
2885:
2886:                // Initialize the number of moves.
2887:                nmoves = 0;
2888:
2889:                // Reset the playing positions.
2890:                positions.newGame();
2891:            }
2892:
2893:            /**
2894:             *  Set the skill level.
2895:             */
2896:            public void set_skill_level(int level) {
2897:                skill_level = level;
2898:            }
2899:
2900:            /**
2901:             *  Set up the pos_to_comb array.
2902:             */
2903:            public void setup_pos_to_comb() {
2904:
2905:                // Set up the pos_to_comb array to point to every winning
2906:                // combination a given position may have.
2907:                int count;
2908:                for (int i = 0; i < 64; i++) {
2909:                    count = 1;
2910:                    pos_to_comb[i][0] = 0;
2911:                    for (int j = 0; j < 76; j++) {
2912:                        for (int k = 2; k < 6; k++) {
2913:                            if (combinations[j][k] == i) {
2914:                                pos_to_comb[i][0]++;
2915:                                pos_to_comb[i][count++] = j;
2916:                            }
2917:                        }
2918:                    }
2919:                }
2920:
2921:                if (debug) {
2922:                    for (int i = 0; i < 64; i++) {
2923:                        System.out.println("");
2924:                        for (int j = 0; j < 8; j++) {
2925:                            System.out.println("pos_to_comb[" + i + "][" + j
2926:                                    + "] = " + pos_to_comb[i][j]);
2927:                        }
2928:                    }
2929:                }
2930:
2931:            }
2932:
2933:            /**
2934:             *  Update the best_picks array.
2935:             */
2936:            public void update_best_picks() {
2937:
2938:                // Re-calculate the best_picks array to point to every (current) winning 
2939:                // combination a given position may have.
2940:                int count;
2941:                for (int i = 0; i < 64; i++) {
2942:
2943:                    count = 1;
2944:                    best_picks[i][0] = 0;
2945:                    if (occupied[i] == 0) {
2946:                        for (int j = 0; j < 76; j++) {
2947:
2948:                            if (combinations[j][0] == 0
2949:                                    || combinations[j][1] == MACHINE) {
2950:
2951:                                for (int k = 2; k < 6; k++) {
2952:                                    if (combinations[j][k] == i) {
2953:                                        best_picks[i][0]++;
2954:                                        best_picks[i][count++] = j;
2955:                                    }
2956:                                }
2957:                            }
2958:                        }
2959:                    }
2960:                }
2961:
2962:                if (debug) {
2963:                    for (int i = 0; i < 64; i++) {
2964:                        System.out.println("");
2965:                        for (int j = 0; j < 8; j++) {
2966:                            System.out.println("best_picks[" + i + "][" + j
2967:                                    + "] = " + best_picks[i][j]);
2968:                        }
2969:                    }
2970:                }
2971:            }
2972:
2973:            /**
2974:             *  Pick the computer's best possible move based on the number
2975:             *  of combinations per position. Choose the position with the
2976:             *  most combinations.
2977:             */
2978:            public void pick_best_position() {
2979:
2980:                int pos = 0;
2981:                int max_num = 0;
2982:                for (int i = 0; i < 64; i++) {
2983:                    if (best_picks[i][0] > max_num && occupied[i] == 0) {
2984:                        pos = i;
2985:                        max_num = best_picks[i][0];
2986:                    }
2987:                }
2988:
2989:                // Mark the position as MACHINE.
2990:                occupied[pos] = MACHINE;
2991:
2992:                positions.set(pos, MACHINE);
2993:
2994:                // Udate the logic arrays and reset the player.
2995:                player = update_logic_arrays(pos);
2996:            }
2997:
2998:            public boolean pick_7() {
2999:
3000:                for (int i = 0; i < 64; i++) {
3001:                    if (best_picks[i][0] == 7) {
3002:                        occupied[i] = MACHINE;
3003:                        positions.set(i, MACHINE);
3004:                        player = update_logic_arrays(i);
3005:                        return true;
3006:                    }
3007:                }
3008:                return false;
3009:
3010:            }
3011:
3012:            public void change_face() {
3013:                current_face = ++current_face % 18;
3014:            }
3015:
3016:            public void label() {
3017:                label_flag ^= true;
3018:            }
3019:
3020:            public boolean unoccupied(int pos) {
3021:                if (occupied[pos] == UNOCCUPIED)
3022:                    return true;
3023:                else
3024:                    return false;
3025:            }
3026:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.