Search Unity

Techno Chess

Discussion in 'Works In Progress - Archive' started by ArchMageDiablo, Apr 4, 2012.

  1. ArchMageDiablo

    ArchMageDiablo

    Joined:
    Apr 4, 2012
    Posts:
    6
    Hello everyone,

    This is a project I've been working on for my capstone project at ITT Tech. I've planned several stages of development such that have an basic goal, a hopeful goal, and an ambitious goal.


    Edit: If the screenshot is still not working here is a link

    The basic goal is simply a 2-player game of Chess. The hopeful goal will replace my current assets (downloaded from turbosquid) with my own assets following a high tech, holographic art style. The ambitious goal is to change the rules so that it's not turn based, rather each piece has a cooldown timer and can be moved rapidly. Though I think that in itself will be easy after having built the rest of the game this will require AI and after a little research chess AI turns out to be quite an undertaking.

    Currently I'm about a third of the way to having a playable 2-player chess game. The chessboard is drawn by instantiating tiles 1.2 units away from each other, and giving them a variable to track their color. Each tile has code for rollover and click tracking.

    Pieces are then Instantiated in much the same way with a little extra tweaking required to properly offset them to the center of the tiles. Both the pieces and the tiles are currently highlightable/clickable, and return their co-ordinates when clicked. The plan is to make it such that when a piece is clicked the tiles highlight with the legal moves for that piece, and clicking is switched to tiles, until a tile is clicked, or the user right clicks to cancel.

    The main script, "board", is attached to an empty game object "gameBoard".

    Code (csharp):
    1. var tile : Transform;
    2. var Pawn : Transform;
    3. var Knight : Transform;
    4. var Bishop : Transform;
    5. var Rook : Transform;
    6. var King : Transform;
    7. var Queen : Transform;
    8.  
    9. var gameState = 0;
    10.  
    11. function Start(){
    12.     //variable for tile color
    13.     var c = 1;
    14.     //first part of 2d array loop, each iteration of this toggles the color of the tiles
    15.     for(var r=1;r<9;r++){
    16.         if(c>0){
    17.             c=0;
    18.         } else {
    19.             c=1;
    20.         }
    21.         //second dimension of array
    22.         for(var i=1;i<9;i++){
    23.             //creates tiles 1.2 away from each other
    24.             obj = Instantiate(tile, Vector3(i * 1.2, 0, (r * 1.2) - 0.4), Quaternion.identity);
    25.             //makes tiles a child of the board object
    26.             obj.transform.parent = transform;
    27.             var objScript = obj.GetComponent("Tile");
    28.             //renames tiles to co-ordinates
    29.             obj.name = i+","+r;
    30.             if(c>0){
    31.                 //sets a tile to white
    32.                 objScript.clr = Color(0.5,0.5,0.5);
    33.                 obj.renderer.material.color = Color(0.5,0.5,0.5);
    34.                 c=0;
    35.             } else {
    36.                 //sets a tile to black
    37.                 objScript.clr = Color(1,1,1);
    38.                 obj.renderer.material.color = Color(1,1,1);
    39.                 c=1;
    40.             }
    41.         }
    42.     }
    43.     //Runs the Piece Placement Functions
    44.     placeWhite();
    45.     placeBlack();  
    46. }
    47.  
    48. function placeWhite(){
    49.     //Sets Rank Position to the Second Rank
    50.     var pY = 2;
    51.     //Loops through the Files in the Rank
    52.     for(var pX=1;pX<9;pX++){
    53.         //Places Pawns at each File in the Rank
    54.         piece = Instantiate(Pawn, Vector3(pX * 1.2, 0, pY), Quaternion.identity);
    55.         piece.Rotate(270,0,0);
    56.         //Rename Pawns so Engine can tell Color
    57.         piece.name = "wPawn";
    58.         //Establish a connection to the Instantiated objects Script
    59.         pieceCode = piece.GetComponent("pawnScript");
    60.         //Passes the Rank and File co-ordinates to the Piece script for later use.
    61.         pieceCode.pX = pX;
    62.         pieceCode.pY = pY;
    63.     }
    64.     pY = 1;
    65.     pX = 1;
    66.     piece = Instantiate(Rook, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    67.     piece.Rotate(270,0,0);
    68.     piece.name = "wRook";
    69.     pieceCode = piece.GetComponent("rookScript");
    70.     pieceCode.pX = pX;
    71.     pieceCode.pY = pY;
    72.    
    73.     pX = 2;
    74.     piece = Instantiate(Knight, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    75.     piece.Rotate(270,0,0);
    76.     piece.name = "wKnight";
    77.     pieceCode = piece.GetComponent("knightScript");
    78.     pieceCode.pX = pX;
    79.     pieceCode.pY = pY;
    80.    
    81.     pX = 3;
    82.     piece = Instantiate(Bishop, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    83.     piece.Rotate(270,0,0);
    84.     piece.name = "wBishop";
    85.     pieceCode = piece.GetComponent("bishopScript");
    86.     pieceCode.pX = pX;
    87.     pieceCode.pY = pY;
    88.    
    89.     pX = 4;
    90.     piece = Instantiate(Queen, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    91.     piece.Rotate(270,0,0);
    92.     piece.name = "wQueen";
    93.     pieceCode = piece.GetComponent("queenScript");
    94.     pieceCode.pX = pX;
    95.     pieceCode.pY = pY;
    96.    
    97.     pX = 5;
    98.     piece = Instantiate(King, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    99.     piece.Rotate(270,0,0);
    100.     piece.name = "wKing";
    101.     pieceCode = piece.GetComponent("kingScript");
    102.     pieceCode.pX = pX;
    103.     pieceCode.pY = pY;
    104.    
    105.     pX = 6;
    106.     piece = Instantiate(Bishop, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    107.     piece.Rotate(270,0,0);
    108.     piece.name = "wBishop";
    109.     pieceCode = piece.GetComponent("bishopScript");
    110.     pieceCode.pX = pX;
    111.     pieceCode.pY = pY;
    112.    
    113.     pX = 7;
    114.     piece = Instantiate(Knight, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    115.     piece.Rotate(270,0,0);
    116.     piece.name = "wKnight";
    117.     pieceCode = piece.GetComponent("knightScript");
    118.     pieceCode.pX = pX;
    119.     pieceCode.pY = pY;
    120.    
    121.     pX = 8;
    122.     piece = Instantiate(Rook, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    123.     piece.Rotate(270,0,0);
    124.     piece.name = "wRook";
    125.     pieceCode = piece.GetComponent("rookScript");
    126.     pieceCode.pX = pX;
    127.     pieceCode.pY = pY;
    128.    
    129. }
    130.  
    131. function placeBlack(){
    132.     var pY = 7;
    133.     for(var pX=1;pX<9;pX++){
    134.         piece = Instantiate(Pawn, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    135.         piece.Rotate(270,180,0);
    136.         piece.name = "bPawn";
    137.         pieceCode = piece.GetComponent("pawnScript");
    138.         pieceCode.pX = pX;
    139.         pieceCode.pY = pY;
    140.         piece.renderer.material.color = Color(0.5,0.5,0.5);
    141.     }
    142.     pY = 8;
    143.     Debug.Log(pY);
    144.     pX = 1;
    145.     piece = Instantiate(Rook, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    146.     piece.Rotate(270,180,0);
    147.     piece.name = "bRook";
    148.     pieceCode = piece.GetComponent("rookScript");
    149.     pieceCode.pX = pX;
    150.     pieceCode.pY = pY;
    151.     piece.renderer.material.color = Color(0.5,0.5,0.5);
    152.    
    153.     pX = 2;
    154.     piece = Instantiate(Knight, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    155.     piece.Rotate(270,180,0);
    156.     piece.name = "bKnight";
    157.     pieceCode = piece.GetComponent("knightScript");
    158.     pieceCode.pX = pX;
    159.     pieceCode.pY = pY;
    160.     piece.renderer.material.color = Color(0.5,0.5,0.5);
    161.    
    162.     pX = 3;
    163.     piece = Instantiate(Bishop, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    164.     piece.Rotate(270,180,0);
    165.     piece.name = "bBishop";
    166.     pieceCode = piece.GetComponent("bishopScript");
    167.     pieceCode.pX = pX;
    168.     pieceCode.pY = pY;
    169.     piece.renderer.material.color = Color(0.5,0.5,0.5);
    170.    
    171.     pX = 4;
    172.     piece = Instantiate(Queen, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    173.     piece.Rotate(270,180,0);
    174.     piece.name = "bQueen";
    175.     pieceCode = piece.GetComponent("queenScript");
    176.     pieceCode.pX = pX;
    177.     pieceCode.pY = pY;
    178.     piece.renderer.material.color = Color(0.5,0.5,0.5);
    179.    
    180.     pX = 5;
    181.     piece = Instantiate(King, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    182.     piece.Rotate(270,180,0);
    183.     piece.name = "bKing";
    184.     pieceCode = piece.GetComponent("kingScript");
    185.     pieceCode.pX = pX;
    186.     pieceCode.pY = pY;
    187.     piece.renderer.material.color = Color(0.5,0.5,0.5);
    188.    
    189.     pX = 6;
    190.     piece = Instantiate(Bishop, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    191.     piece.Rotate(270,180,0);
    192.     piece.name = "bBishop";
    193.     pieceCode = piece.GetComponent("bishopScript");
    194.     pieceCode.pX = pX;
    195.     pieceCode.pY = pY;
    196.     piece.renderer.material.color = Color(0.5,0.5,0.5);
    197.    
    198.     pX = 7;
    199.     piece = Instantiate(Knight, Vector3(pX * 1.2, 0, pY *1.2 - .4), Quaternion.identity);
    200.     piece.Rotate(270,180,0);
    201.     piece.name = "bKnight";
    202.     pieceCode = piece.GetComponent("knightScript");
    203.     pieceCode.pX = pX;
    204.     pieceCode.pY = pY;
    205.     piece.renderer.material.color = Color(0.5,0.5,0.5);
    206.    
    207.     pX = 8;
    208.     piece = Instantiate(Rook, Vector3(pX * 1.2, 0, pY * 1.2 - .4), Quaternion.identity);
    209.     piece.Rotate(270,180,0);
    210.     piece.name = "bRook";
    211.     pieceCode = piece.GetComponent("rookScript");
    212.     pieceCode.pX = pX;
    213.     pieceCode.pY = pY;
    214.     piece.renderer.material.color = Color(0.5,0.5,0.5);
    215.    
    216. }
    var pX : int;
    var pY : int;


    Each piece has code similar to this on it to track it's position. The tiles are very similar right now as well, except that instead of grabbing a pX and pY they crab a color from the board script.
    Code (csharp):
    1. function OnMouseDown(){
    2.     renderer.material.color = Color(1,1.3,1);
    3.     movePiece();
    4. }
    5. function OnMouseEnter(){
    6.     renderer.material.color = Color(0.5,0.5,1.5);
    7. }
    8. function OnMouseExit(){
    9.     if(name == "wPawn"){
    10.         renderer.material.color = Color(1,1,1);
    11.     } else {
    12.         renderer.material.color = Color(0.5,0.5,0.5);
    13.     }
    14. }
    15. function OnMouseUp(){
    16.     if(name == "wPawn"){
    17.         renderer.material.color = Color(1,1,1);
    18.     } else {
    19.         renderer.material.color = Color(0.5,0.5,0.5);
    20.     }
    21. }
    22. function movePiece(){
    23.     Debug.Log("Move " + name + " at " + pX + "," + pY);
    24.    
    25. }
    My thought is to put a function within each piece like legalMoves() or something that will highlight it's legal moves based off its pX and pY. Each legal move will change tiles states to highlight and turn on a listener for it's destination.
    Upon clicking a tile the piece is moved there and if there was an opposing piece there it will be taken, then the legal moves will be checked to see if the king is present, if so check is declared. At the moment determining checkmate seems an insurmountable task.

    Currently I'm not using a 2d Array to store the board information, I think it might work better to have each piece keep track of its own position, and then when questioning a tile I'll just loop through all chlildren of the pieces gameObjects checking it there is a piece with that pX, pY.

    I've yet to do too much research on how to determine it but the challenge seems pretty huge, not only would I have to consider the legal moves of each of my pieces but if an opponent can move a piece in the way to block a piece that also needs to be considered and I have no idea how to approach that problem. It is likely that I will have a surrender button appear each time there is a check and if the opponent cannot get out it they must surrender.

    Several materials I've read on the subject of chess engines suggest using a "bit-board" which seems to be the most effecient model for a chess game, but the concept is a bit over my head. For the time being I want to make what I have work as a part of the learning experiance and at least get a game where each piece knows its legal moves and can be moved about, more a virtual chessboard than an actual Chess Game Engine.

    I do not have allot of experience with coding for games. The coding experience I do have comes from ActionScript 2.0, I'm working with JavaScript in unity and though the similarities have made it very quick to learn I still am having difficulty with classes and inheritance. I am constructing the game the simplest way possible as opposed to the most efficient as my knowledge limits me. I am however thriving from the challenge and any direction or encouragement I can get from the community would be much appreciated and helpful.
     
    Last edited: Apr 4, 2012
  2. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    Screenshot doesn't load
     
  3. Hebrewjose

    Hebrewjose

    Joined:
    Apr 4, 2012
    Posts:
    2
    Excalibur Talking Touch Chess Computer?
    Just a quick techno point if you don't mind, not sure how familiar you all are with the playing side of the game Excalibur Talking Touch Chess Computer

    I presume for example the Easy Beginners Level has various sub levels i.e 1 second thinking ,2 second thinking ,3 second thinking level etc all the way up to about 15 I think.

    Does the longer the computer think the harder level you would be playing against.

    Any help on this would be gratefully recived.Aslo any hints how to improve the lighting on the screen
     
  4. ArchMageDiablo

    ArchMageDiablo

    Joined:
    Apr 4, 2012
    Posts:
    6
    Yeah probobly, one of the AI methods I seen forms a list of all possible moves and then tries its best to sort them within a time limit the longer you give it the closer it gets to the best move.
     
  5. Beist

    Beist

    Joined:
    May 31, 2012
    Posts:
    2
    Hey.
    Good read.
    I'm not a coder at all, but I have decent experience with UDK and Uscripting. Going over to Unity now as some of my teams ideas are easier to produce in prototype mode, after what we've read and heard.

    Anyways as a first project we wanted to see if we could make a board type game with a dice.
    I found your code to be a good introductory into making the grid of the game, but for some reason the tiles don't replicate to make a full 8x8 grid. Is there something missing from the code?

    I also found a chess version with the complete programming made by some guys back in 2010, but it's in C# so I don't understand a squat :p
    Maybe you'll find it to be an interresting find. ClickyLink
     
  6. Artunique

    Artunique

    Joined:
    Jul 28, 2012
    Posts:
    1
    is this for the iphone ?