Chess Part1 – Board and basic movement

Yesterday, while browsing some game development threads on Reddit, I stumbled on this thread where a guy ask some help to make a chess-like game. I figured it would be a nice exercise to do and decided to tackle the problem. I also see the possibility to add a lot of functionality as the project advance. It could be a nice opportunity to experiment with online multiplayer. I would also be interested in the process of making an AI, and maybe a learning AI that would get better and better as it plays. But for now, let’s set the bases of the project!

Codesource and Webplayer

You can find the Chess Part1 Unity project on github. I’ll try to keep branch for each part and have the master be the up-to-date version. A webplayer is also available if you want to try the project.

(Update : Since I haven’t come to do the part2, I updated my old code on github. It is now a semi-functional player vs player chess board. You can move pieces and eat other pieces but some rule aren’t fully functional. There is no check-mate or end-game. It’s on github Part 2 Branch. This is a work-in-progress code and will be cleaned when I post the second part of this tutorial.)

Create the chessboard

First thing I did, was to create two kind of Prefab. One for the light cubes, and one for the brown cubes. I then created manually a chess board with 64(8×8) cube at the position(0,0) to (7,7). The position will be used to know which cube has been clicked. If you need help to create the individuals cube and add textures to them, you can look at Part1 of my Voxel engine where I explained in more detail how to create a cube and apply a texture to it. I just found two basic wood texture on google for the cubes. Here  is the result :

1 - Board

Figure 1 – Unity’s UI with board created

Cool. Now that the board is created, I created a Pawn prefab from a capsule collider. I put 3 on the board just to test. We will now implement the selection of a piece and its displacement to a square on the board. For now, we won’t bother to look if the part can go there.

Add the C# scripts

I created a __GameManager GameObject and added two script to it : GameManager.cs  and PlayerControl.cs. The former will be responsible to make sure everything works while the latter will process interaction from the player. Here is the GameManager.cs script :

</pre>
using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

 public int gameState = 0; // In this state, the code is waiting for : 0 = Piece selection, 1 = Piece animation, 2 = Player2/AI movement
 //private int activePlayer = 0; // 0 = Player1, 1 = Player2, 2 = AI, to be used later
 private GameObject SelectedPiece; // Selected Piece

 //Update SlectedPiece with the GameObject inputted to this function
 public void SelectPiece(GameObject _PieceToSelect)
 {
 // Change color of the selected piece to make it apparent. Put it back to white when the piece is unselected
 if(SelectedPiece)
 {
 SelectedPiece.renderer.material.color = Color.white;
 }
 SelectedPiece = _PieceToSelect;
 SelectedPiece.renderer.material.color = Color.red;
 }

 // Move the SelectedPiece to the inputted coords
 public void MovePiece(Vector2 _coordToMove)
 {
 SelectedPiece.transform.position = _coordToMove; // Move the piece
 SelectedPiece.renderer.material.color = Color.white; // Change it's color back
 SelectedPiece = null; // Unselect the Piece
 }

 // Change the state of the game
 public void ChangeState(int _newState)
 {
 gameState = _newState;
 Debug.Log ("GameState = " + _newState);
 }
}
<pre>

GameManager hold the current gameState aswell as the selectedPiece. It is also responsible to test if the piece can be moved to where the player clicked(which will be added in the next parts). I decided to use a State system to keep the script separated as much as possible. This will allow for a simpler integration of a second player, AI and multiplayer easier. If it end up being too complicated for little advantages, I might remove it.

Here is the code for PlayerControl.cs :


using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour {

 private Camera PlayerCam; // Camera used by the player
 private GameManager _GameManager; // GameObject responsible for the management of the game

 // Use this for initialization
 void Start ()
 {
 PlayerCam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent(); // Find the Camera's GameObject from its tag
 _GameManager = gameObject.GetComponent();
 }

 // Update is called once per frame
 void Update () {
 // Look for Mouse Inputs
 GetMouseInputs();

 }

 // Detect Mouse Inputs
 void GetMouseInputs()
 {
 Ray _ray;
 RaycastHit _hitInfo;

 // Select a piece if the gameState is 0 or 1
 if(_GameManager.gameState < 2)
 {
 // On Left Click
 if(Input.GetMouseButtonDown(0))
 {
 _ray = PlayerCam.ScreenPointToRay(Input.mousePosition); // Specify the ray to be casted from the position of the mouse click

 // Raycast and verify that it collided
 if(Physics.Raycast (_ray,out _hitInfo))
 {
 // Select the piece if it has the good Tag
 if(_hitInfo.collider.gameObject.tag == ("PiecePlayer1"))
 {
 _GameManager.SelectPiece(_hitInfo.collider.gameObject);
 _GameManager.ChangeState (1);
 }
 }
 }
 }

 // Move the piece if the gameState is 1
 if(_GameManager.gameState == 1)
 {
 Vector2 selectedCoord;

 // On Left Click
 if(Input.GetMouseButtonDown(0))
 {
 _ray = PlayerCam.ScreenPointToRay(Input.mousePosition); // Specify the ray to be casted from the position of the mouse click

 // Raycast and verify that it collided
 if(Physics.Raycast (_ray,out _hitInfo))
 {

 // Select the piece if it has the good Tag
 if(_hitInfo.collider.gameObject.tag == ("Cube"))
 {
 selectedCoord = new Vector2(_hitInfo.collider.gameObject.transform.position.x,_hitInfo.collider.gameObject.transform.position.y);
 _GameManager.MovePiece(selectedCoord);
 _GameManager.ChangeState (0);
 }
 }
 }
 }
 }
}

This code cast a ray on mouse left click.

If the gameState is 0 or 1, it will look if it hit a piece and tell the GameManager to select that piece.

If the gameState is 1, it will look if it hit a cube. If it hit a cube, it will ask the GameManager to move the SelectedPiece to the position of the selected cube. In the following part, the manager will verify that the piece can move there.

2a

a) 3 unselected pawns

2b

b) 1 selected and 2 unselected pawns

2c

 

c) 3 unselected pawns after a movement

I hope you enjoyed this quick introduction on the making of a chess game. Next part of this tutorial will add different piece movement and a verification that the movement can be executed!

Advertisements